tc_mysql.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #ifndef __TC_MYSQL_H
  17. #define __TC_MYSQL_H
  18. #include "util/tc_platform.h"
  19. #include "mysql.h"
  20. #include "util/tc_ex.h"
  21. #include <map>
  22. #include <vector>
  23. #include <stdlib.h>
  24. #include <functional>
  25. namespace tars
  26. {
  27. /////////////////////////////////////////////////
  28. /**
  29. * @file tc_mysql.h
  30. * @brief mysql操作类.
  31. * @brief MySQL operation class.
  32. *
  33. */
  34. /////////////////////////////////////////////////
  35. /**
  36. * @brief 数据库异常类
  37. * @brief Database exception class
  38. */
  39. struct TC_Mysql_Exception : public TC_Exception
  40. {
  41. TC_Mysql_Exception(const string &sBuffer) : TC_Exception(sBuffer){};
  42. ~TC_Mysql_Exception() throw(){};
  43. };
  44. /**
  45. * @brief 数据库配置接口
  46. * @brief Database configuration interface
  47. */
  48. struct TC_DBConf
  49. {
  50. /**
  51. * 主机地址
  52. * Host Address
  53. */
  54. string _host;
  55. /**
  56. * 用户名
  57. * User name
  58. */
  59. string _user;
  60. /**
  61. * 密码
  62. */
  63. string _password;
  64. /**
  65. * 数据库
  66. * Database
  67. */
  68. string _database;
  69. /**
  70. * 字符集
  71. * Character Set
  72. */
  73. string _charset;
  74. /**
  75. * 端口
  76. * Port
  77. */
  78. int _port;
  79. /**
  80. * 客户端标识
  81. * Client Identity
  82. */
  83. int _flag;
  84. /**
  85. * 连接超时
  86. * Port
  87. */
  88. int _connectTimeout;
  89. /**
  90. * 读写超时
  91. * Port
  92. */
  93. int _writeReadTimeout;
  94. /**
  95. * @brief 构造函数
  96. * @brief Constructor
  97. */
  98. TC_DBConf()
  99. : _port(0)
  100. , _flag(0)
  101. {
  102. }
  103. /**
  104. * @brief 读取数据库配置.
  105. * @brief Read Database Configuration.
  106. *
  107. * @param mpParam 存放数据库配置的map
  108. * @param mpParam Map holding database configuration
  109. * dbhost: 主机地址
  110. * dbhost: host address
  111. * dbuser:用户名
  112. * dbuser:username
  113. * dbpass:密码
  114. * dbpass:password
  115. * dbname:数据库名称
  116. * dbname:database name
  117. * dbport:端口
  118. * dbport:port
  119. */
  120. void loadFromMap(const map<string, string> &mpParam)
  121. {
  122. map<string, string> mpTmp = mpParam;
  123. _host = mpTmp["dbhost"];
  124. _user = mpTmp["dbuser"];
  125. _password = mpTmp["dbpass"];
  126. _database = mpTmp["dbname"];
  127. _charset = mpTmp["charset"];
  128. _port = atoi(mpTmp["dbport"].c_str());
  129. _flag = 0;
  130. _connectTimeout = atoi(mpTmp["connectTimeout"].c_str());
  131. _writeReadTimeout = atoi(mpTmp["writeReadTimeout"].c_str());
  132. if(mpTmp["dbport"] == "")
  133. {
  134. _port = 3306;
  135. }
  136. if(mpTmp["connectTimeout"] == "")
  137. {
  138. _connectTimeout = 5;
  139. }
  140. if(mpTmp["writeReadTimeout"] == "")
  141. {
  142. _writeReadTimeout = 15;
  143. }
  144. }
  145. };
  146. /**
  147. * @brief Mysql数据库操作类
  148. * @brief Mysql database operation class
  149. *
  150. * 非线程安全,通常一个线程一个TC_Mysql对象;
  151. * Non-thread safe, usually one thread and one TC_Mysql object;
  152. *
  153. * 对于insert/update可以有更好的函数封装,保证SQL注入;
  154. * For insert/update better function encapsulation guarantees SQL injection:
  155. *
  156. * TC_Mysql::DB_INT表示组装sql语句时,不加””和转义;
  157. * TC_Mysql:: DB_INT means no "" and escape when assembling SQL statements;
  158. *
  159. * TC_Mysql::DB_STR表示组装sql语句时,加””并转义;
  160. * TC_Mysql:: DB_STR means to add "" and escape when assembling SQL statements;
  161. */
  162. class TC_Mysql
  163. {
  164. public:
  165. /**
  166. * @brief 构造函数
  167. * @brief Constructor
  168. */
  169. TC_Mysql();
  170. /**
  171. * @brief 构造函数.
  172. * @brief Constructor.
  173. *
  174. * @param sHost 主机IP
  175. * @param sHost Host IP
  176. * @param sUser 用户
  177. * @param sUser User
  178. * @param sPasswd 密码
  179. * @param sPasswd Password
  180. * @param sDatebase 数据库
  181. * @param sDatebase Database
  182. * @param port 端口
  183. * @param port Port
  184. * @param iUnixSocket socket
  185. * @param iUnixSocket Socket
  186. * @param iFlag 客户端标识
  187. * @param iFlag Client Identity
  188. */
  189. TC_Mysql(const string& sHost, const string& sUser = "", const string& sPasswd = "", const string& sDatabase = "", const string &sCharSet = "", int port = 0, int iFlag = 0);
  190. /**
  191. * @brief 构造函数.
  192. * @brief Constructor.
  193. * @param tcDBConf 数据库配置
  194. * @param tcDBConf Database Configuration
  195. */
  196. TC_Mysql(const TC_DBConf& tcDBConf);
  197. /**
  198. * @brief 析构函数.
  199. * @brief Destructor.
  200. */
  201. ~TC_Mysql();
  202. /**
  203. * @brief 初始化.
  204. * @brief Initialization
  205. *
  206. * @param sHost 主机IP
  207. * @param sHost Host IP
  208. * @param sUser 用户
  209. * @param sUser User
  210. * @param sPasswd 密码
  211. * @param sPasswd Password
  212. * @param sDatebase 数据库
  213. * @param sDatebase Database
  214. * @param port 端口
  215. * @param port Port
  216. * @param iUnixSocket socket
  217. * @param iFlag 客户端标识
  218. * @param iFlag Client Identity
  219. * @return 无
  220. * @return none
  221. */
  222. void init(const string& sHost, const string& sUser = "", const string& sPasswd = "", const string& sDatabase = "", const string &sCharSet = "", int port = 0, int iFlag = 0);
  223. /**
  224. * @brief 初始化.
  225. * @brief Initialization
  226. *
  227. * @param tcDBConf 数据库配置
  228. * @param tcDBConf Database Configuration
  229. */
  230. void init(const TC_DBConf& tcDBConf);
  231. /**
  232. * @brief 读取mysql配置
  233. * @brief Read MySQL configuration
  234. *
  235. * @return tcDBConf
  236. */
  237. TC_DBConf getDBConf();
  238. /**
  239. * @brief 连接数据库.
  240. * @brief Connect to database.
  241. *
  242. * @throws TC_Mysql_Exception
  243. * @return 无
  244. * @return none
  245. */
  246. void connect();
  247. /**
  248. * @brief 断开数据库连接.
  249. * @brief Disconnect database.
  250. * @return 无
  251. * @return none
  252. */
  253. void disconnect();
  254. /**
  255. * @brief 获取数据库变量.
  256. * @brief Get database variables.
  257. *
  258. * @return 数据库变量
  259. * @return Database Variables
  260. */
  261. string getVariables(const string &sName);
  262. /**
  263. * @brief 直接获取数据库指针.
  264. * @brief Get database pointer directly.
  265. *
  266. * @return MYSQL* 数据库指针
  267. * @return MYSQL*Database Pointer
  268. */
  269. MYSQL *getMysql();
  270. /**
  271. * @brief 字符转义, 需要连接到数据库,考虑了字符集
  272. * @brief Character escape, need to connect to database, consider character set
  273. *
  274. * @param sFrom 源字符串
  275. * @param sFrom Source string
  276. * @param sTo 输出字符串
  277. * @param sTo Output string
  278. * @return 输出字符串
  279. * @return Output string
  280. */
  281. string realEscapeString(const string& sFrom);
  282. /**
  283. * @brief 更新或者插入数据.
  284. * @brief Update or insert data.
  285. *
  286. * @param sSql sql语句
  287. * @param sSql SQL statement
  288. * @throws TC_Mysql_Exception
  289. * @return
  290. */
  291. void execute(const string& sSql);
  292. /**
  293. * @brief mysql的一条记录
  294. * @brief A record of MySQL
  295. */
  296. class MysqlRecord
  297. {
  298. public:
  299. /**
  300. * @brief 构造函数.
  301. * @brief Constructor.
  302. *
  303. * @param record
  304. */
  305. MysqlRecord(const map<string, string> &record);
  306. /**
  307. * @brief 获取数据,s一般是指数据表的某个字段名
  308. * @brief Get data, s is generally a field name of an index table
  309. * @param s 要获取的字段
  310. * @param s Fields to get
  311. * @return 符合查询条件的记录的s字段名
  312. * @return s field name of the record that meets the query criteria
  313. */
  314. const string& operator[](const string &s);
  315. protected:
  316. const map<string, string> &_record;
  317. };
  318. /**
  319. * @brief 查询出来的mysql数据
  320. * @brief MySQL data queried
  321. */
  322. class MysqlData
  323. {
  324. public:
  325. /**
  326. * @brief 所有数据.
  327. * @brief All data.
  328. *
  329. * @return vector<map<string,string>>&
  330. */
  331. vector<map<string, string> >& data();
  332. /**
  333. * 数据的记录条数
  334. * Number of records for data
  335. *
  336. * @return size_t
  337. */
  338. size_t size();
  339. /**
  340. * @brief 获取某一条记录.
  341. * @brief Get a record
  342. *
  343. * @param i 要获取第几条记录
  344. * @param i Which record to get
  345. * @return MysqlRecord类型的数据,可以根据字段获取相关信息,
  346. * @return MysqlRecord type data, you can get relevant information based on the field,
  347. */
  348. MysqlRecord operator[](size_t i);
  349. protected:
  350. vector<map<string, string> > _data;
  351. };
  352. /**
  353. * @brief Query Record.
  354. *
  355. * @param sSql sql语句
  356. * @param sSql SQL statement
  357. * @throws TC_Mysql_Exception
  358. * @return MysqlData类型的数据,可以根据字段获取相关信息
  359. * @return MysqlData type of data, you can get information based on the field
  360. */
  361. MysqlData queryRecord(const string& sSql);
  362. /**
  363. * @brief Query Record.
  364. *
  365. * @param sSql sql语句
  366. * @param sSql SQL statement
  367. * @param pdatarsunc ,函数参数为map<string,string> ,key为column 名,value为数据
  368. * @param pdatarsunc , Function parameter is map<string, string>, key is column name, value is data
  369. * @throws TC_Mysql_Exception
  370. * @return MysqlData类型的数据,可以根据字段获取相关信息
  371. * @return MysqlData type of data, you can get information based on the field
  372. */
  373. size_t travelRecord(const string& sSql, const std::function<void(const map<string,string> &)> & pdatarsunc);
  374. /**
  375. * @brief 定义字段类型,
  376. * @brief Define the field type,
  377. * DB_INT:数字类型
  378. * DB_INT:Number type
  379. * DB_STR:字符串类型
  380. * DB_STR:String Type
  381. */
  382. enum FT
  383. {
  384. DB_INT,
  385. DB_STR,
  386. };
  387. /**
  388. * 数据记录
  389. * Data Rercord
  390. */
  391. typedef map<string, pair<FT, string> > RECORD_DATA;
  392. /**
  393. * @brief 更新记录.
  394. * @brief Update Records.
  395. *
  396. * @param sTableName 表名
  397. * @param sTableName Table name
  398. * @param mpColumns 列名/值对
  399. * @param mpColumns Column Name/Value Pair
  400. * @param sCondition where子语句,例如:where A = B
  401. * @param sCondition Where clause, for example, where A = B
  402. * @throws TC_Mysql_Exception
  403. * @return size_t 影响的行数
  404. * @return Number of rows affected by size_t
  405. */
  406. size_t updateRecord(const string &sTableName, const map<string, pair<FT, string> > &mpColumns, const string &sCondition);
  407. /**
  408. * @brief 插入记录.
  409. * @brief insert record.
  410. *
  411. * @param sTableName 表名
  412. * @param sTableName Table Name
  413. * @param mpColumns 列名/值对
  414. * @param mpColumns Column Name/Value Pair
  415. * @throws TC_Mysql_Exception
  416. * @return size_t 影响的行数
  417. * @return Number of rows affected by size_t
  418. */
  419. size_t insertRecord(const string &sTableName, const map<string, pair<FT, string> > &mpColumns);
  420. /**
  421. * @brief 插入记录(一次插入多条).
  422. * @brief Insert a record (insert more than one record at a time).
  423. *
  424. * @param sTableName 表名
  425. * @param sTableName Table Name
  426. * @param mpColumns 列名/值对数组
  427. * @param mpColumns Column name / value pair array
  428. * @throws TC_Mysql_Exception
  429. * @return size_t 影响的行数
  430. * @return Number of rows affected by size_t
  431. */
  432. size_t insertRecord(const string &sTableName, const map<string, pair<FT,vector<string>>> &mpColumns);
  433. /**
  434. * @brief 替换记录.
  435. * @brief Replace record.
  436. *
  437. * @param sTableName 表名
  438. * @param sTableName Table Name
  439. * @param mpColumns 列名/值对
  440. * @param mpColumns Column name / value pair
  441. * @throws TC_Mysql_Exception
  442. * @return size_t 影响的行数
  443. * @return Number of rows affected by size_t
  444. */
  445. size_t replaceRecord(const string &sTableName, const map<string, pair<FT, string> > &mpColumns);
  446. /**
  447. * @brief 替换记录(一次替换多条).
  448. * @brief Replace records (replace multiple records at a time).
  449. *
  450. * @param sTableName 表名
  451. * @param sTableName Table Name
  452. * @param mpColumns 列名/值对数组
  453. * @param mpColumns Column name / value pair array
  454. * @throws TC_Mysql_Exception
  455. * @return size_t 影响的行数
  456. * @return Number of rows affected by size_t
  457. */
  458. size_t replaceRecord(const string &sTableName, const map<string, pair<FT, vector<string>>> &mpColumns);
  459. /**
  460. * @brief 删除记录.
  461. * @brief Delete record.
  462. *
  463. * @param sTableName 表名
  464. * @param sTableName Table Name
  465. * @param sCondition where子语句,例如:where A = B
  466. * @param sCondition Where clause, for example, where A = B
  467. * @throws TC_Mysql_Exception
  468. * @return size_t 影响的行数
  469. * @return Number of rows affected by size_t
  470. */
  471. size_t deleteRecord(const string &sTableName, const string &sCondition = "");
  472. /**
  473. * @brief 获取Table查询结果的数目.
  474. * @brief Number of Tables query results.
  475. *
  476. * @param sTableName 用于查询的表名
  477. * @param sTableName Table name for query
  478. * @param sCondition where子语句,例如:where A = B
  479. * @param sCondition Where clause, for example, where A = B
  480. * @throws TC_Mysql_Exception
  481. * @return size_t 查询的记录数目
  482. * @return size_t Number of records queried
  483. */
  484. size_t getRecordCount(const string& sTableName, const string &sCondition = "");
  485. /**
  486. * @brief 获取Sql返回结果集的个数.
  487. * @brief Gets the number of result sets returned by Sql.
  488. *
  489. * @param sCondition where子语句,例如:where A = B
  490. * @param sCondition Where clause, for example, where A = B
  491. * @throws TC_Mysql_Exception
  492. * @return 查询的记录数目
  493. * @return Number of records queried
  494. */
  495. size_t getSqlCount(const string &sCondition = "");
  496. /**
  497. * @brief 存在记录.
  498. * @brief Existing records.
  499. *
  500. * @param sql sql语句
  501. * @param sql SQL statement
  502. * @throws TC_Mysql_Exception
  503. * @return 操作是否成功
  504. * @return Is the operation successful
  505. */
  506. bool existRecord(const string& sql);
  507. /**
  508. * @brief 获取字段最大值.
  509. *
  510. * @brief Get the maximum value of a field.
  511. * @param sTableName 用于查询的表名
  512. * @param sTableName Table name for query
  513. * @param sFieldName 用于查询的字段
  514. * @param sFieldName Fields for Query
  515. * @param sCondition where子语句,例如:where A = B
  516. * @param sCondition Where clause, for example, where A = B
  517. * @throws TC_Mysql_Exception
  518. * @return 查询的记录数目
  519. * @return Number of records queried
  520. */
  521. int getMaxValue(const string& sTableName, const string& sFieldName, const string &sCondition = "");
  522. /**
  523. * @brief 获取auto_increment最后插入得ID.
  524. * @brief Get the auto_increment last inserted ID.
  525. *
  526. * @return ID值
  527. * @return ID value
  528. */
  529. long lastInsertID();
  530. /**
  531. * @brief 构造Insert-SQL语句.
  532. * @brief Construct Insert-SQL statements.
  533. *
  534. * @param sTableName 表名
  535. * @param sTableName Table Name
  536. * @param mpColumns 列名/值对
  537. * @param mpColumns Column Name/Value Pair
  538. * @return string insert-SQL语句
  539. * @return String insert-SQL statement
  540. */
  541. string buildInsertSQL(const string &sTableName, const map<string, pair<FT, string> > &mpColumns);
  542. /**
  543. * @brief 构造Insert-SQL语句(一次插入多条).
  544. * @brief Construct Insert-SQL statements (insert more than one at a time).
  545. *
  546. * @param sTableName 表名
  547. * @param sTableName table name
  548. * @param mpColumns 列名/值对
  549. * @param mpColumns Column Name/Value Pair
  550. * @return string insert-SQL语句
  551. * @return String insert-SQL statement
  552. */
  553. string buildInsertSQL(const string &sTableName, const map<string, pair<FT, vector<string> >> &mpColumns);
  554. /**
  555. * @brief 构造Replace-SQL语句.
  556. * @brief Construct Replace-SQL statements.
  557. *
  558. * @param sTableName 表名
  559. * @param sTableName Table Name
  560. * @param mpColumns 列名/值对
  561. * @param mpColumns Column name / value pair
  562. * @return string insert-SQL语句
  563. * @return string insert-SQL statement
  564. */
  565. string buildReplaceSQL(const string &sTableName, const map<string, pair<FT, string> > &mpColumns);
  566. /**
  567. * @brief 构造Replace-SQL语句(一次替换多条).
  568. * @brief Construct replace SQL statement (replace multiple statements at a time)
  569. *
  570. * @param sTableName 表名
  571. * @param sTableName Tabel Name
  572. * @param mpColumns 列名/值对
  573. * @param mpColumns Column name / value pair
  574. * @return string insert-SQL语句
  575. * @return string insert-SQL statement
  576. */
  577. string buildReplaceSQL(const string &sTableName, const map<string, pair<FT, vector<string> >> &mpColumns);
  578. /**
  579. * @brief 构造Update-SQL语句.
  580. * @brief Construct update SQL statement
  581. *
  582. * @param sTableName 表名
  583. * @param sTableName Table Name
  584. * @param mpColumns 列名/值对
  585. * @param mpColumns Column name / value pair
  586. * @param sCondition where子语句
  587. * @param sCondition where clause
  588. * @return string Update-SQL语句
  589. * @return string Update-SQL statement
  590. */
  591. string buildUpdateSQL(const string &sTableName,const map<string, pair<FT, string> > &mpColumns, const string &sCondition);
  592. /**
  593. * @brief 获取最后执行的SQL语句.
  594. * @brief Get the last SQL statement executed.
  595. *
  596. * @return SQL语句
  597. * @return SQL statement
  598. */
  599. string getLastSQL() { return _sLastSql; }
  600. /**
  601. * @brief 获取查询影响数
  602. * @brief Get query influence number
  603. * @return int
  604. */
  605. size_t getAffectedRows();
  606. /**
  607. * @brief 字符转义, 不考虑字符集(有一定的风险).
  608. * @brief Character escape, regardless of character set (there is a certain risk)
  609. *
  610. * @param sFrom 源字符串
  611. * @param sFrom Source string
  612. * @param sTo 输出字符串
  613. * @param sTo Output string
  614. * @return 输出字符串
  615. * @return Output string
  616. */
  617. static string escapeString(const string& sFrom);
  618. /**
  619. * @brief 构造Insert-SQL语句.
  620. * @brief Construct insert SQL statement.
  621. *
  622. * @param sTableName 表名
  623. * @param sTableName Table Name
  624. * @param mpColumns 列名/值对
  625. * @param mpColumns Column name / value pair
  626. * @return string insert-SQL语句
  627. * @return String insert SQL statement
  628. */
  629. static string buildInsertSQLNoSafe(const string &sTableName, const map<string, pair<FT, string> > &mpColumns);
  630. /**
  631. * @brief 构造Insert-SQL语句(批量).
  632. * @brief Construct insert SQL statement (batch).
  633. *
  634. * @param sTableName 表名
  635. * @param sTableName Table Name
  636. * @param mpColumns 列名/值对
  637. * @param mpColumns Column name / value pair
  638. * @return string insert-SQL语句
  639. * @return string insert-SQL statement
  640. */
  641. static string buildInsertSQLNoSafe(const string &sTableName, const map<string, pair<FT, vector<string> >> &mpColumns);
  642. /**
  643. * @brief 构造Replace-SQL语句.
  644. * @brief Construct replace SQL statement.
  645. *
  646. * @param sTableName 表名
  647. * @param sTableName Table Name
  648. * @param mpColumns 列名/值对
  649. * @param mpColumns Column name / value pair
  650. * @return string insert-SQL语句
  651. * @return string Insert SQL statement
  652. */
  653. static string buildReplaceSQLNoSafe(const string &sTableName, const map<string, pair<FT, string> > &mpColumns);
  654. /**
  655. * @brief 构造Replace-SQL语句(批量).
  656. * @brief Construct replace SQL statement (batch)
  657. *
  658. * @param sTableName 表名
  659. * @param sTableName Table Name
  660. * @param mpColumns 列名/值对
  661. * @param mpColumns Column name / value pair
  662. * @return string insert-SQL语句
  663. * @return String insert SQL statement
  664. */
  665. static string buildReplaceSQLNoSafe(const string &sTableName, const map<string, pair<FT, vector<string> >> &mpColumns);
  666. /**
  667. * @brief 构造Update-SQL语句.
  668. * @brief Construct an Update-SQL statement.
  669. *
  670. * @param sTableName 表名
  671. * @param sTableName Table Name
  672. * @param mpColumns 列名/值对
  673. * @param mpColumns Column Name/Value Pair
  674. * @param sCondition where子语句
  675. * @param sCondition where cluase
  676. * @return string Update-SQL语句
  677. * @return string Update-SQL statement
  678. */
  679. static string buildUpdateSQLNoSafe(const string &sTableName,const map<string, pair<FT, string> > &mpColumns, const string &sCondition);
  680. protected:
  681. /**
  682. * @brief copy contructor,只申明,不定义,保证不被使用
  683. * @brief copy contructor. Statement only, undefined, guaranteed not to be used
  684. */
  685. TC_Mysql(const TC_Mysql &tcMysql);
  686. /**
  687. *
  688. * @brief 只申明,不定义,保证不被使用
  689. * @brief Statement only, undefined, guaranteed not to be used
  690. */
  691. TC_Mysql &operator=(const TC_Mysql &tcMysql);
  692. /**
  693. * @brief 构造SQL语句(批量).
  694. * @brief Construct SQL statements (batches).
  695. *
  696. * @param sTableName 表名
  697. * @param sTableName Table Name
  698. * @param mpColumns 列名/值对
  699. * @param mpColumns Column Name/Value Pair
  700. * @return string insert-SQL语句
  701. * @return string insert-SQL statement
  702. */
  703. string buildSQL(const string &sTableName, const string &command, const map<string, pair<FT, string> > &mpColumns);
  704. string buildBatchSQL(const string &sTableName, const string &command, const map<string, pair<FT, vector<string> >> &mpColumns);
  705. static string buildSQLNoSafe(const string &sTableName, const string &command, const map<string, pair<FT, string> > &mpColumns);
  706. static string buildBatchSQLNoSafe(const string &sTableName, const string &command, const map<string, pair<FT, vector<string> >> &mpColumns);
  707. private:
  708. /**
  709. * 数据库指针
  710. * Database Pointer
  711. */
  712. MYSQL *_pstMql;
  713. /**
  714. * 数据库配置
  715. * Database Configuration
  716. */
  717. TC_DBConf _dbConf;
  718. /**
  719. * 是否已经连接
  720. * Is Connected
  721. */
  722. bool _bConnected;
  723. /**
  724. * 最后执行的sql
  725. * Last executed SQL
  726. */
  727. string _sLastSql;
  728. };
  729. }
  730. #endif //_TC_MYSQL_H