1. 程式人生 > >mysql client api 的封裝

mysql client api 的封裝

前言

這幾天,在測試程式中要用mysql client api 去資料庫插入記錄和查詢記錄。
好久不用mysql client api, 沒有封裝好的存貨。只能用api直接幹活。
前天閒下來了,將mysql client api的使用封裝成類,對執行sql語句的操作封裝了成員函式,實現庫的建立,表的建立,use database, 記錄的增刪改查. 等下次再用到,就直接用這個類幹活了。

實驗環境

debian8.8 + mysql5.5.60-0+deb8u1

前置任務

如果連線的不是本地mysql資料庫,需要在資料庫改使用者許可權,使遠端訪問資料庫成為可能。
這活,如果是在工作中,已經由運維人員搞好了。
如果是自己做實驗,這步要自己做。

# 在debian8.8中安裝mysql 
aptitude install mysql-server mysql-client

# 登陸mysql客戶端 ,連線本地資料庫
mysql -h 127.0.0.1 -u root -p 

# 如果不是迴環ip訪問, 需要在配置檔案中繫結允許訪問的客戶端ip
vi /etc/mysql/my.cnf 
# bind-address          = 127.0.0.1 
# ref https://dev.mysql.com/doc/refman/8.0/en/server-options.html 
bind-address            = * 

# 重啟mysql服務 
/etc/init.d/mysql restart 

# 修改mysql配置之後, 就可以用迴環地址或者本地IP地址登陸msyql客戶端工具了
mysql -h 192.168.11.140 -u root -p

建立供遠端訪問的mysql使用者

mysql> select host,user,password from mysql.user;

mysql口令的使用者長度是定長的
mysql> create user remote_user identified by password '12345678901234567890123456789012345678901'; 
Query OK, 0 rows affected (0.00 sec) 

給剛建立遠端使用者設定所有資料庫操作的許可權.設定完後,就可以用本地活遠端用伺服器的IP來登陸mysql了
實際的工作資料庫,運維人員都設定好了,我們不管.
grant all privileges on *.* to remote_user identified by '12345678901234567890123456789012345678901';

其他一些可能用到的mysql客戶端的命令. 其實到了這步,就可以在windows上搞個圖形化的資料庫工具來做實驗了.
mysql> SHOW DATABASES;   //顯示資料庫
mysql> USE my_db         //進入資料庫
mysql> SHOW TABLES;      //顯示錶
mysql> DESCRIBE mytable; //顯示錶結構
mysql> CREATE DATABASE abccs;    //建立一個數據庫
mysql> select version();  // 顯示資料庫版本

工程下載

src_test_mysql.7z

工程執行效果

main.cpp.31 : main()] : MAKE_FILE_MACRO__BIN_NAME = [test_mysql]
main.cpp.71 : fn_test()] : >> fn_test()
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'my_test_db';)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(is_db_exist)
db_opt_mysql.cpp.309 : process_result_set()] : result set : row = 1, col = 1
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.0] = [my_test_db]
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(USE my_test_db;)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = 'my_test_db' AND table_name = 'tbl_app_data_index_files' LIMIT 1;)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(is_tbl_exist)
db_opt_mysql.cpp.309 : process_result_set()] : result set : row = 1, col = 1
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(insert into tbl_app_data_index_files(app_id, file_path_name)values('01', '/var/log/test1_1.dat');)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_ADD_1)
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(insert into tbl_app_data_index_files(app_id, file_path_name)values('01', '/var/log/test1_2.dat');)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_ADD_2)
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(insert into tbl_app_data_index_files(app_id, file_path_name)values('01', '/var/log/test1_3.dat');)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_ADD_3)
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(insert into tbl_app_data_index_files(app_id, file_path_name)values('01', '/var/log/test1_4.dat');)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_ADD_4)
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(insert into tbl_app_data_index_files(app_id, file_path_name)values('02', '/var/log/test2_1.dat');)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_ADD_5)
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(insert into tbl_app_data_index_files(app_id, file_path_name)values('02', '/var/log/test2_2.dat');)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_ADD_6)
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(select * from tbl_app_data_index_files where app_id = '01';)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_QUERY_1)
db_opt_mysql.cpp.309 : process_result_set()] : result set : row = 4, col = 4
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.0] = [85]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.1] = [2018-11-06 15:34:00]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.2] = [01]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.3] = [/var/log/test1_1.dat]
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.0] = [86]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.1] = [2018-11-06 15:34:00]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.2] = [01]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.3] = [/var/log/test1_2.dat]
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.0] = [87]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.1] = [2018-11-06 15:34:00]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.2] = [01]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.3] = [/var/log/test1_3.dat]
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.0] = [88]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.1] = [2018-11-06 15:34:00]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.2] = [01]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.3] = [/var/log/test1_4.dat]
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(delete from tbl_app_data_index_files where app_id = '01';)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_DEL_1)
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(select * from tbl_app_data_index_files where app_id = '01';)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_QUERY_1)
db_opt_mysql.cpp.309 : process_result_set()] : result set : row = 0, col = 4
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[-1.-1] = [NULL]
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(select * from tbl_app_data_index_files where app_id = '02';)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_QUERY_2)
db_opt_mysql.cpp.309 : process_result_set()] : result set : row = 30, col = 4
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.0] = [5]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.1] = [2018-11-06 10:16:31]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.0] = [6]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.1] = [2018-11-06 10:16:31]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.0] = [11]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.1] = [2018-11-06 13:06:34]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.0] = [12]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.1] = [2018-11-06 13:06:34]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[4.0] = [17]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[4.1] = [2018-11-06 13:07:49]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[4.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[4.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[5.0] = [18]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[5.1] = [2018-11-06 13:07:49]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[5.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[5.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[6.0] = [23]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[6.1] = [2018-11-06 13:09:36]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[6.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[6.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[7.0] = [24]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[7.1] = [2018-11-06 13:09:36]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[7.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[7.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[8.0] = [29]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[8.1] = [2018-11-06 14:14:47]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[8.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[8.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[9.0] = [30]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[9.1] = [2018-11-06 14:14:48]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[9.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[9.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[10.0] = [35]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[10.1] = [2018-11-06 14:23:12]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[10.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[10.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[11.0] = [36]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[11.1] = [2018-11-06 14:23:13]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[11.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[11.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[12.0] = [41]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[12.1] = [2018-11-06 14:29:14]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[12.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[12.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[13.0] = [42]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[13.1] = [2018-11-06 14:29:27]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[13.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[13.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[14.0] = [47]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[14.1] = [2018-11-06 14:33:17]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[14.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[14.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[15.0] = [48]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[15.1] = [2018-11-06 14:33:19]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[15.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[15.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[16.0] = [53]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[16.1] = [2018-11-06 14:52:44]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[16.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[16.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[17.0] = [54]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[17.1] = [2018-11-06 14:52:49]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[17.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[17.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[18.0] = [59]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[18.1] = [2018-11-06 15:04:49]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[18.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[18.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[19.0] = [60]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[19.1] = [2018-11-06 15:04:49]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[19.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[19.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[20.0] = [65]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[20.1] = [2018-11-06 15:05:09]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[20.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[20.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[21.0] = [66]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[21.1] = [2018-11-06 15:05:09]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[21.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[21.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[22.0] = [71]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[22.1] = [2018-11-06 15:07:56]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[22.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[22.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[23.0] = [72]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[23.1] = [2018-11-06 15:07:56]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[23.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[23.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[24.0] = [77]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[24.1] = [2018-11-06 15:14:38]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[24.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[24.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[25.0] = [78]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[25.1] = [2018-11-06 15:14:38]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[25.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[25.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[26.0] = [83]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[26.1] = [2018-11-06 15:28:19]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[26.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[26.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[27.0] = [84]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[27.1] = [2018-11-06 15:28:20]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[27.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[27.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[28.0] = [89]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[28.1] = [2018-11-06 15:34:00]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[28.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[28.3] = [/var/log/test2_1.dat]
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[29.0] = [90]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[29.1] = [2018-11-06 15:34:00]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[29.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[29.3] = [/var/log/test2_2.dat]
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(update tbl_app_data_index_files set file_path_name = '' where app_id = '02';)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_MODIFY)
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(select * from tbl_app_data_index_files where app_id = '02';)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_QUERY_2)
db_opt_mysql.cpp.309 : process_result_set()] : result set : row = 30, col = 4
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.0] = [5]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.1] = [2018-11-06 10:16:31]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.0] = [6]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.1] = [2018-11-06 10:16:31]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.0] = [11]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.1] = [2018-11-06 13:06:34]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.0] = [12]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.1] = [2018-11-06 13:06:34]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[4.0] = [17]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[4.1] = [2018-11-06 13:07:49]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[4.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[4.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[5.0] = [18]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[5.1] = [2018-11-06 13:07:49]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[5.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[5.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[6.0] = [23]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[6.1] = [2018-11-06 13:09:36]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[6.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[6.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[7.0] = [24]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[7.1] = [2018-11-06 13:09:36]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[7.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[7.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[8.0] = [29]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[8.1] = [2018-11-06 14:14:47]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[8.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[8.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[9.0] = [30]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[9.1] = [2018-11-06 14:14:48]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[9.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[9.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[10.0] = [35]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[10.1] = [2018-11-06 14:23:12]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[10.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[10.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[11.0] = [36]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[11.1] = [2018-11-06 14:23:13]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[11.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[11.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[12.0] = [41]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[12.1] = [2018-11-06 14:29:14]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[12.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[12.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[13.0] = [42]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[13.1] = [2018-11-06 14:29:27]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[13.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[13.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[14.0] = [47]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[14.1] = [2018-11-06 14:33:17]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[14.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[14.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[15.0] = [48]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[15.1] = [2018-11-06 14:33:19]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[15.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[15.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[16.0] = [53]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[16.1] = [2018-11-06 14:52:44]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[16.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[16.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[17.0] = [54]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[17.1] = [2018-11-06 14:52:49]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[17.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[17.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[18.0] = [59]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[18.1] = [2018-11-06 15:04:49]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[18.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[18.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[19.0] = [60]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[19.1] = [2018-11-06 15:04:49]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[19.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[19.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[20.0] = [65]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[20.1] = [2018-11-06 15:05:09]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[20.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[20.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[21.0] = [66]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[21.1] = [2018-11-06 15:05:09]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[21.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[21.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[22.0] = [71]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[22.1] = [2018-11-06 15:07:56]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[22.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[22.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[23.0] = [72]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[23.1] = [2018-11-06 15:07:56]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[23.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[23.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[24.0] = [77]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[24.1] = [2018-11-06 15:14:38]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[24.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[24.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[25.0] = [78]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[25.1] = [2018-11-06 15:14:38]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[25.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[25.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[26.0] = [83]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[26.1] = [2018-11-06 15:28:19]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[26.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[26.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[27.0] = [84]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[27.1] = [2018-11-06 15:28:20]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[27.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[27.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[28.0] = [89]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[28.1] = [2018-11-06 15:34:00]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[28.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[28.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[29.0] = [90]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[29.1] = [2018-11-06 15:34:00]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[29.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[29.3] = []
main.cpp.75 : fn_test()] : test_db_mysql ok
main.cpp.39 : main()] : THE END

工程預覽

// @file \test_mysql\src\db_business_logic.h

#ifndef __DB_BUSINESS_LOGIC_H__
#define __DB_BUSINESS_LOGIC_H__

bool test_db_mysql();

#endif // #ifndef __DB_BUSINESS_LOGIC_H__


// @file \test_mysql\src\db_business_logic.cpp

#include "db_business_logic.h"

#include "db_opt_mysql.h"
#include "const_define.h"

#define APP_ID "01"
#define APP_DATA_FILE_PATH_NAME "/var/log/test1.dat"

#define DB_IP "192.168.16.44"
#define DB_PORT 3306
#define DB_NAME "my_test_db"
#define DB_CHARSET "utf8" // 是utf8, 不是UTF-8 !!
#define DB_USER_NAME "root"
#define DB_USER_PWD "123456"
#define DB_TBL_NAME "tbl_app_data_index_files"

// 增
#define SQL_ADD_1 "insert into tbl_app_data_index_files(app_id, file_path_name)values('01', '/var/log/test1_1.dat');"
#define SQL_ADD_2 "insert into tbl_app_data_index_files(app_id, file_path_name)values('01', '/var/log/test1_2.dat');"
#define SQL_ADD_3 "insert into tbl_app_data_index_files(app_id, file_path_name)values('01', '/var/log/test1_3.dat');"
#define SQL_ADD_4 "insert into tbl_app_data_index_files(app_id, file_path_name)values('01', '/var/log/test1_4.dat');"
#define SQL_ADD_5 "insert into tbl_app_data_index_files(app_id, file_path_name)values('02', '/var/log/test2_1.dat');"
#define SQL_ADD_6 "insert into tbl_app_data_index_files(app_id, file_path_name)values('02', '/var/log/test2_2.dat');"

// 刪
#define SQL_DEL_1 "delete from tbl_app_data_index_files where app_id = '01';"

// 查 - for SQL_DEL_1
#define SQL_QUERY_1 "select * from tbl_app_data_index_files where app_id = '01';"

// 改
#define SQL_MODIFY "update tbl_app_data_index_files set file_path_name = '' where app_id = '02';"

// 查  - for SQL_MODIFY
#define SQL_QUERY_2 "select * from tbl_app_data_index_files where app_id = '02';"

bool test_db_mysql()
{
	bool b_rc = false;
	cls_db_opt_mysql db_opt;

	do {
		db_opt.set_db_connect_info(
			DB_IP, 
			DB_PORT, 
			DB_NAME, 
			DB_CHARSET, 
			DB_USER_NAME, 
			DB_USER_PWD,
			DB_TBL_NAME);

		if (!db_opt.connect_server(true)) {
			break;
		}
		
		if (!db_opt.create_db()) {
			break;
		}

		if (!db_opt.use_db()) {
			break;
		}

		if (!db_opt.create_tbl()) {
			break;
		}

		// SQL-增
		if (!db_opt.exec_sql(SQL_ADD_1)) {
			break;
		}
		db_opt.process_result_set("SQL_ADD_1", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		if (!db_opt.exec_sql(SQL_ADD_2)) {
			break;
		}
		db_opt.process_result_set("SQL_ADD_2", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		if (!db_opt.exec_sql(SQL_ADD_3)) {
			break;
		}
		db_opt.process_result_set("SQL_ADD_3", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		if (!db_opt.exec_sql(SQL_ADD_4)) {
			break;
		}
		db_opt.process_result_set("SQL_ADD_4", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		if (!db_opt.exec_sql(SQL_ADD_5)) {
			break;
		}
		db_opt.process_result_set("SQL_ADD_5", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		if (!db_opt.exec_sql(SQL_ADD_6)) {
			break;
		}
		db_opt.process_result_set("SQL_ADD_6", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		// SQL-刪 + 查
		// query before del
		if (!db_opt.exec_sql(SQL_QUERY_1)) {
			break;
		}
		db_opt.process_result_set("SQL_QUERY_1", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		// sql delete
		if (!db_opt.exec_sql(SQL_DEL_1)) {
			break;
		}
		db_opt.process_result_set("SQL_DEL_1", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		// query after delete
		if (!db_opt.exec_sql(SQL_QUERY_1)) {
			break;
		}
		db_opt.process_result_set("SQL_QUERY_1", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		// SQL-改 + 查
		// query befor modify
		if (!db_opt.exec_sql(SQL_QUERY_2)) {
			break;
		}
		db_opt.process_result_set("SQL_QUERY_2", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		// sql modify
		if (!db_opt.exec_sql(SQL_MODIFY)) {
			break;
		}
		db_opt.process_result_set("SQL_MODIFY", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		// query after modify
		if (!db_opt.exec_sql(SQL_QUERY_2)) {
			break;
		}
		db_opt.process_result_set("SQL_QUERY_2", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		b_rc = true;
	} while (0);

	db_opt.db_close();
	return b_rc;
}


// @file \test_mysql\src\db_opt_base.h

#ifndef __DB_OPT_BASE_H__
#define __DB_OPT_BASE_H__

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>

class cls_db_opt_base
{
public:
	cls_db_opt_base();
	virtual ~cls_db_opt_base();

	void set_db_connect_info(
		const char* ip, 
		int port,
		const char* db_name, 
		const char* psz_charset,
		const char* user_name, 
		const char* user_pwd,
		const char* tbl_name);

	virtual bool connect_server(bool b_force) = 0; // 連線伺服器是否成功

	virtual bool is_db_exist() = 0; // 資料庫是否存在
	
	virtual bool create_db() = 0; // 建立資料庫
	virtual bool use_db() = 0; // 切換到指定的資料庫
	virtual bool is_tbl_exist() = 0; // 表是否存在
	virtual bool create_tbl() = 0; // 建立表
	virtual bool exec_sql(const char* psz_sql) = 0; // 執行SQL語句
	virtual bool db_close() = 0; // 關閉資料庫
	virtual std::string get_err_string() = 0; // 取當前錯誤字串
	virtual uint get_err_sn() = 0; // 取當前錯誤號

	const char* get_db_name() {return m_str_db_name.c_str();}
	const char* get_charset() {return m_str_charset.c_str();}
	const char* get_tbl_name() {return m_str_tbl_name.c_str();}

protected:
	bool m_is_mysql_was_init; // 是否初始化了mysql
	bool m_is_server_was_connected; // 是否已經連線到了伺服器
	
	bool m_is_db_was_exist; // 資料庫是否存在
	bool m_is_db_was_create; // 是否已經建立了資料庫
	bool m_is_db_was_switched; // 是否已經切換到了引數指定的資料庫
	
	bool m_is_tbl_exist; // 表是否存在
	bool m_is_tbl_was_create; // 表是否已經建立

	std::string m_str_ip;
	int m_i_db_port;
	std::string m_str_db_name;
	std::string m_str_charset;
	
	std::string m_str_user_name;
	std::string m_str_user_pwd;

	std::string m_str_tbl_name;
};

#endif // #ifndef __DB_OPT_BASE_H__


// @file \test_mysql\src\db_opt_base.cpp

#include "db_opt_base.h"

cls_db_opt_base::cls_db_opt_base()
{
	m_is_mysql_was_init = false;
	m_is_server_was_connected = false;
	
	m_is_db_was_exist = false;
	m_is_db_was_create = false;
	m_is_db_was_switched = false;

	m_str_ip = "";
	m_i_db_port = 0;
	m_str_db_name = "";
	m_str_charset = "";
	
	m_str_user_name = "";
	m_str_user_pwd = "";

	m_str_tbl_name = "";
}

cls_db_opt_base::~cls_db_opt_base()
{
}

void cls_db_opt_base::set_db_connect_info(
	const char* ip, 
	int port,
	const char* db_name, 
	const char* psz_charset,
	const char* user_name, 
	const char* user_pwd,
	const char* tbl_name)
{
	m_str_ip = (NULL != ip) ? ip : "";
	m_i_db_port = port;
	m_str_db_name = (NULL != db_name) ? db_name : "";
	m_str_charset = (NULL != psz_charset) ? psz_charset : "";
	
	m_str_user_name = (NULL != user_name) ? user_name : "";
	m_str_user_pwd = (NULL != user_pwd) ? user_pwd : "";

	m_str_tbl_name = (NULL != tbl_name) ? tbl_name : "";
}


// @file \test_mysql\src\db_opt_mysql.h

#ifndef __DB_OPT_MYSQL_H__
#define __DB_OPT_MYSQL_H__

#include "db_opt_base.h"
#include "mysql.h"

class cls_db_opt_mysql : public cls_db_opt_base
{
public:
	typedef bool (cls_db_opt_mysql::*PFN_cb_proc_resultset)(
		int i_rows, int i_cols, int i_row_index, int i_col_index, const char* psz_grid_content, bool* b_process_ok);

public:
	cls_db_opt_mysql();
	virtual ~cls_db_opt_mysql();

	bool process_result_set(const char* psz_tip, PFN_cb_proc_resultset cb);
	bool cb_proc_resultset_to_disp(
		int i_rows, int i_cols, int i_row_index, int i_col_index, const char* psz_grid_content, bool* b_process_ok);

	virtual bool connect_server(bool b_force);

	virtual bool is_db_exist();
	bool cb_proc_resultset_for_is_db_exist(
		int i_rows, int i_cols, int i_row_index, int i_col_index, const char* psz_grid_content, bool* b_process_ok);

	virtual bool create_db();
	virtual bool use_db();
	
	virtual bool is_tbl_exist();
	bool cb_proc_resultset_for_is_tbl_exist(
		int i_rows, int i_cols, int i_row_index, int i_col_index, const char* psz_grid_content, bool* b_process_ok);

	virtual bool create_tbl();
	virtual bool exec_sql(const char* psz_sql);
	virtual bool db_close();

	virtual std::string get_err_string();
	virtual uint get_err_sn();

	MYSQL* get_db_handle() {return     &m_h_mysql;}

private:
	MYSQL m_h_mysql; // mysql庫的控制代碼
};

#endif // #ifndef __DB_OPT_MYSQL_H__


// @file \test_mysql\src\db_opt_mysql.cpp

#include "db_opt_mysql.h"
#include "const_define.h"

cls_db_opt_mysql::cls_db_opt_mysql()
{
	memset(&m_h_mysql, 0, sizeof(MYSQL));
}

cls_db_opt_mysql::~cls_db_opt_mysql()
{
}

bool cls_db_opt_mysql::connect_server(bool b_force)
{
	do {
		if (b_force) {
			db_close();
		}

		if (!m_is_mysql_was_init) {
		    if (mysql_init(&m_h_mysql) == NULL) {
				MYLOG_E("error : %s\n", get_err_string().c_str());
		        break;
		    }

			m_is_mysql_was_init = true;
		}

		if (!m_is_server_was_connected) {
		    if (NULL == mysql_real_connect(&m_h_mysql, 
					m_str_ip.c_str(), 
					m_str_user_name.c_str(), 
					m_str_user_pwd.c_str(), 
					NULL, 
					m_i_db_port, 
					NULL, 
					0))
			{
				MYLOG_E("error : %s\n", get_err_string().c_str());
		        break;
		    }

			m_is_server_was_connected = true;
		}
	} while (0);

	return m_is_server_was_connected;
}

bool cls_db_opt_mysql::is_db_exist()
{
	// SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA;
	// SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'my_test_db';

	bool b_rc = false;
	char sz_buf[1024] = {'\0'};

	do {
		if (!m_is_db_was_exist) {
			if (!m_is_server_was_connected) {
				b_rc = connect_server(false);
				if (!b_rc) {
					break;
				}
			}

			sprintf(sz_buf, 
				"SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '%s';",
				get_db_name());
			
			b_rc = exec_sql(sz_buf);
			if (!b_rc) {
				break;
			}

			m_is_db_was_exist = process_result_set("is_db_exist", &cls_db_opt_mysql::cb_proc_resultset_for_is_db_exist);
		}
	} while (0);

	return m_is_db_was_exist;
}

bool cls_db_opt_mysql::cb_proc_resultset_for_is_db_exist(
	int i_rows, int i_cols, int i_row_index, int i_col_index, const char* psz_grid_content, bool* b_process_ok)
{
	if (NULL != b_process_ok) {
		*b_process_ok = false;
	}

	cb_proc_resultset_to_disp(i_rows, i_cols, i_row_index, i_col_index, psz_grid_content, NULL);

	if ((1 == i_rows) 
		&& (1 == i_cols) 
		&& (0 == i_row_index) 
		&& (0 == i_col_index) 
		&& (NULL != psz_grid_content))
	{
		// 一行一列, 且格點內容為資料庫名稱
		if (0 == strcmp(get_db_name(), psz_grid_content)) {
			if (NULL != b_process_ok) {
				*b_process_ok = true;
			}
		}
	}
	
	return false; // 資料庫是否存在的判斷, 只處理一次
}

bool cls_db_opt_mysql::create_db()
{
	bool b_rc = false;
	char sz_buf[1024] = {'\0'};

	do {
		b_rc = is_db_exist();
		if (b_rc) {
			m_is_db_was_create = true;
			break;
		}

		sprintf(sz_buf, 
			"create database %s;",
			get_db_name());
		
		b_rc = exec_sql(sz_buf);
		if (!b_rc) {
			break;
		}

		// 建立資料庫的SQL是沒有結果集的
		m_is_db_was_create = true;
	} while (0);

	return b_rc;
}

bool cls_db_opt_mysql::use_db()
{
	char sz_buf[1024] = {'\0'};

	do {
		if (!connect_server(false)) {
			break;
		}

		if (!m_is_db_was_switched) {
			// USE database_name;
			sprintf(sz_buf, 
				"USE %s;",
				get_db_name());
			
			if (!exec_sql(sz_buf)) {
				break;
			}

			// 切換資料庫是沒有結果集的
			m_is_db_was_switched = true;

			// 切換到了具體的資料庫, 才能設定字符集
			if (mysql_set_character_set(&m_h_mysql, get_charset()) != 0) {
				// 字符集設定錯了,還要繼續, 不能算錯
				MYLOG_E("error : %s\n", get_err_string().c_str());
			}
		}
	} while (0);

	return m_is_db_was_switched;
}

bool cls_db_opt_mysql::cb_proc_resultset_for_is_tbl_exist(
	int i_rows, int i_cols, int i_row_index, int i_col_index, const char* psz_grid_content, bool* b_process_ok)
{
	if (NULL != b_process_ok) {
		*b_process_ok = false;
	}

	if ((1 == i_rows) 
		&& (1 == i_cols) 
		&& (0 == i_row_index) 
		&& (0 == i_col_index) 
		&& (NULL != psz_grid_content))
	{
		// 一行一列, 且格點內容為資料庫名稱
		if (0 == strcmp(get_tbl_name(), psz_grid_content)) {
			if (NULL != b_process_ok) {
				*b_process_ok = true;
			}
		}
	}
	
	return false; // 表是否存在的判斷, 只處理一次
}

bool cls_db_opt_mysql::is_tbl_exist()
{
	char sz_buf[1024] = {'\0'};

	do {
		if (!m_is_tbl_exist) {
			if (!use_db()) {
				break;
			}

			/*
			SELECT TABLE_NAME FROM information_schema.tables 
			WHERE table_schema = 'my_test_db' AND table_name = 'tbl_app_data_index_files';
			*/
			
			sprintf(sz_buf, 
				"SELECT TABLE_NAME "
				"FROM information_schema.tables "
				"WHERE table_schema = '%s' "
			    "AND table_name = '%s' "
				"LIMIT 1;",
				get_db_name(),
				get_tbl_name());
			
			if (!exec_sql(sz_buf)) {
				break;
			}

			m_is_tbl_exist = process_result_set("is_tbl_exist", &cls_db_opt_mysql::cb_proc_resultset_for_is_tbl_exist);
		}
	} while (0);

	return m_is_tbl_exist;
}

bool cls_db_opt_mysql::create_tbl()
{
	char sz_buf[1024] = {'\0'};

	do {
		if (!m_is_tbl_was_create) {
			if (is_tbl_exist()) {
				m_is_tbl_was_create = true;
				break;
			}

			/**
			CREATE TABLE my_test_db.tbl_app_data_index_files (
				`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
				`time_stamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
				`app_id` VARCHAR(64) NOT NULL,
				`file_path_name` varchar(1024) NOT NULL,
				PRIMARY KEY (`id`)
				) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
			*/

			// CREATE TABLE IF NOT EXISTS
			// 這種語法不行(IF NOT EXISTS)
			sprintf(sz_buf, 
				"CREATE TABLE %s.%s ("
				"`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,"
				"`time_stamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,"
				"`app_id` VARCHAR(64) NOT NULL,"
				"`file_path_name` varchar(1024) NOT NULL,"
				"PRIMARY KEY (`id`)"
				") ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=%s;",
				get_db_name(),
				get_tbl_name(),
				get_charset());
			
			if (!exec_sql(sz_buf)) {
				break;
			}

			// 建立表的SQL沒有結果集
			m_is_tbl_was_create = true;
		}
	} while (0);

	return m_is_tbl_was_create;
}

bool cls_db_opt_mysql::process_result_set(const char* psz_tip, PFN_cb_proc_resultset cb)
{
	bool b_process_ok = false;
	bool b_break_loop = false;
	
	MYSQL_RES* db_result = NULL;
	MYSQL_ROW db_row;
	uint i_num_fields = 0;
	ull ull_rows = 0;
	ull i = 0;
	ull j = 0;

	do {
		if (NULL != psz_tip) {
			MYLOG_D("cls_db_opt_mysql::process_result_set(%s)\n", psz_tip);
		}
		
		if (NULL == cb) {
			b_process_ok = true;
			break;
		}
		
		db_result = mysql_store_result(get_db_handle());
		if (NULL == db_result) {
			b_process_ok = false;
			break;
		}
		
		ull_rows = mysql_num_rows(db_result);
		i_num_fields = mysql_num_fields(db_result);

		MYLOG_D("result set : row = %d, col = %d\n", (int)ull_rows, (int)i_num_fields);
		if (ull_rows <= 0) {
			if (!(this->*cb)((int)ull_rows, (int)i_num_fields, -1, -1, NULL, &b_process_ok)) {
				break;
			}
		}
		
		do {
			db_row = mysql_fetch_row(db_result);
			if (NULL == db_row) {
				break;
			}

			if (NULL != cb) {
				MYLOG_D("%s\n", LINE80);
				for (j = 0; j < i_num_fields; j++) {
					if (!(this->*cb)((int)ull_rows, (int)i_num_fields, (int)i, (int)j, db_row[j], &b_process_ok)) {
						b_break_loop = true;
						break;
					}
				}
			}
			
			i++;
		} while (!b_break_loop);
	} while (0);

	return b_process_ok;
}

bool cls_db_opt_mysql::cb_proc_resultset_to_disp(
	int i_rows, int i_cols, int i_row_index, int i_col_index, const char* psz_grid_content, bool* b_process_ok)
{
	MYLOG_D("col[%d.%d] = [%s]", i_row_index, i_col_index, (NULL != psz_grid_content) ? psz_grid_content : "NULL");

	if (NULL != b_process_ok) {
		*b_process_ok = true; // 處理成功
	}
	
	return true; // 繼續處理下一個回撥
}

bool cls_db_opt_mysql::db_close()
{
	if (m_is_mysql_was_init) {
		mysql_close(&m_h_mysql);
	}

	m_is_mysql_was_init = false;
	m_is_server_was_connected = false;

	m_is_db_was_exist = false;
	m_is_db_was_create = false;
	m_is_db_was_switched = false;

	m_is_tbl_exist = false;
	m_is_tbl_was_create = false;

	return true;
}

bool cls_db_opt_mysql::exec_sql(const char* psz_sql)
{
	int i_rc = -1;
	int failed_count = 0;
	std::string str = "";
	
	do {
		if (NULL == psz_sql) {
			break;
		}

		MYLOG_D("# %s\n", LINE80);
		MYLOG_D("# exec_sql(%s)\n", psz_sql);
		MYLOG_D("# %s\n", LINE80);
		do {
			// 當執行 delete from tbl_app_data_index_files where app_id = '01';
			// 第一次會報錯如下:
			// Commands out of sync; you can't run this command now
			// 重連資料庫 + use db後成功
			i_rc = mysql_query(&m_h_mysql, psz_sql);
			if (0 == i_rc) {
				MYLOG_D("ok : exec_sql\n");
				break;
			} else {
				MYLOG_E("error : %s\n", get_err_string().c_str());
				connect_server(true);
				use_db();
			}

			// 失敗重試, 假設是sql沒寫錯, 首次執行SQL, 沒連線資料庫引起的
			// 如果是SQL寫錯了, 那就沒折了
		} while (failed_count++ < 1);
	} while (0);

	return (0 == i_rc);
}

std::string cls_db_opt_mysql::get_err_string()
{
	return mysql_error(&m_h_mysql);
}

uint cls_db_opt_mysql::get_err_sn()
{
	return mysql_errno(&m_h_mysql);
}


// @file main.cpp
// @brief 對mysql操作進行封裝, 使操作mysql資料庫方便些
// @note 
//		實驗環境:
//				debian8.8

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <string>
#include <errno.h>

#include "const_define.h"
#include "db_business_logic.h"

void init(const char* psz_log_owner_name);
void uninit();
void proc_sig_term(int num);
int fn_test(int argc, char** argv);

int main(int argc, char** argv)
{
    char sz_buf[MAX_MSG_LENGTH] = {'\0'};

#ifdef MAKE_FILE_MACRO__BIN_NAME
    sprintf(sz_buf, "%s", MAKE_FILE_MACRO__BIN_NAME);
    init(sz_buf);
    MYLOG_D("MAKE_FILE_MACRO__BIN_NAME = [%s]\n", MAKE_FILE_MACRO__BIN_NAME);
#else
    init(NULL);
#endif // #ifdef MAKE_FILE_MACRO__BIN_NAME

    fn_test(argc, argv);
    uninit();

    MYLOG_D("THE END\n");
    return EXIT_SUCCESS;
}

void uninit()
{
}

void proc_sig_term(int num)
{
    MYLOG_D("SIGTERM = %d, num = %d\n", SIGTERM, num);
    MYLOG_D("maybe can do some clean task before quit\n");
    exit(1);	
}

void init(const char* psz_log_owner_name)
{
    int i = 0;

    // daemon(0, 0);

    // clear screen (print 25 empty line)
    for (i = 0; i < 25; i++) {
        MYLOG_D("\n");
    }

    signal(SIGTERM, proc_sig_term);
}

int fn_test(int argc, char** argv)
{
	bool b_rc = false;
    MYLOG_D(">> fn_test()\n");

	do {
		b_rc = test_db_mysql();
		MYLOG_D("test_db_mysql %s\n", b_rc ? "ok" : "failed");
		if (!b_rc) {
			break;
		}
	
		b_rc = true;
	} while (0);

	if (!b_rc) {
		MYLOG_D("test failed r\n");
	}

    return 0;
}


// @file const_define.h

#if not defined(__CONST_DEFINE_H__)
#define __CONST_DEFINE_H__

#include <string.h>
#include <stdint.h>
#include <syslog.h>

#include <string>
#include <list>

typedef unsigned int uint;
typedef unsigned short ushort;
typedef unsigned long long ull;

#ifndef SAFE_DELETE
#define SAFE_DELETE(p) \
    if (NULL != (p)) { \
        delete (p); \
        (p) = NULL; \
    }
#endif // #ifndef SAFE_DELETE

#ifndef SAFE_DELETE_ARY
#define SAFE_DELETE_ARY(p) \
    if (NULL != (p)) { \
        delete[] (p); \
        (p) = NULL; \
    }
#endif // #ifndef SAFE_DELETE

#define TITLE_LINE80 "================================================================================"
#define LINE80 "--------------------------------------------------------------------------------"

#if not defined(MYLOG_D)

#define USE_SYSLOG

#ifdef USE_SYSLOG
#define MYLOG_V(fmt, ...) \
	syslog(LOG_INFO, "[%s : %s.%d : %s()] : " fmt, "MYLOG_V", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__);

#define MYLOG_D(fmt, ...) \
	syslog(LOG_INFO, "[%s : %s.%d : %s()] : " fmt, "MYLOG_D", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__);

#define MYLOG_I(fmt, ...) \
	syslog(LOG_INFO, "[%s : %s.%d : %s()] : " fmt, "MYLOG_I", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__);

#define MYLOG_W(fmt, ...) \
	syslog(LOG_INFO, "[%s : %s.%d : %s()] : " fmt, "MYLOG_W", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__);

#define MYLOG_E(fmt, ...) \
	syslog(LOG_INFO, "[%s : %s.%d : %s()] : " fmt, "MYLOG_E", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__);

#define MYLOG_F(fmt, ...) \
	syslog(LOG_INFO, "[%s : %s.%d : %s()] : " fmt, "MYLOG_F", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__);
#else // #ifdef USE_SYSLOG
#define MYLOG_V printf
#define MYLOG_D printf
#define MYLOG_I printf
#define MYLOG_W printf
#define MYLOG_E printf
#define MYLOG_F printf
#endif // #ifdef USE_SYSLOG

#endif // #if not defined(MYLOG_D)

#define MAX_SQL_LENGTH (1024 * 8)
#define MAX_MSG_LENGTH (1024 * 4)

#endif // #if not defined(__CONST_DEFINE_H__)


# ==============================================================================
# @file makefile
# ==============================================================================
# @note 
# howto build project
# 		make BIN_NAME="bin_name_by_you_want" rebuild
# makefile code need tab key not sapce key

MY_MAKE_FILE_PATH_NAME = $(MAKEFILE_LIST)

# macro from Makefile command line
# BIN_NAME

# macro to C project
MAKE_FILE_MACRO__BIN_NAME="make_file_macro__bin_name"

# var define on Makefile
BIN = output_not_give_bin_name
IS_BUILD_TYPE_VALID = 0

ifdef BIN_NAME
	IS_BUILD_TYPE_VALID = 1
	BIN = $(BIN_NAME)
	MAKE_FILE_MACRO__BIN_NAME=$(BIN_NAME)
else
	IS_BUILD_TYPE_VALID = 0
endif

LINE80 = --------------------------------------------------------------------------------

# CC = g++ -std=c++98
CC = g++

# -Werror is "warning as error"
CFLAGS = -Wall -Werror -g

INC = -I. -I../doc/mysql/include/
LIBPATH = -L/usr/lib/ -L/usr/local/lib/ -L../doc/mysql/lib/

ifeq (1, $(IS_BUILD_TYPE_VALID))
	LIBS = -lstdc++ -pthread -lmysqlclient
else
	LIBS =
endif

DEPEND_CODE_DIR = ../common/ \

DEPEND_CODE_SRC = $(shell find $(DEPEND_CODE_DIR) -name '*.cpp')
DEPEND_CODE_OBJ = $(DEPEND_CODE_SRC:.cpp=.o)

ROOT_CODE_SRC = $(shell find ./ -name '*.cpp')
ROOT_CODE_OBJ = $(ROOT_CODE_SRC:.cpp=.o)

SUB_CODE_DIR = ./empty_dir
SUB_CODE_SRC = $(shell find $(SUB_CODE_DIR) -name '*.cpp')
SUB_CODE_OBJ = $(SUB_CODE_SRC:.cpp=.o)

.PHONY: help
help:
	clear
	@echo "usage:"
	@echo
	@echo "build project by given bin name"
	@echo "make BIN_NAME=\"bin_name_by_you_want\" rebuild"
	@echo

.PHONY: clean
clean:
	clear

	@echo
	@echo
	@echo
	@echo
	@echo
	@echo
	@echo
	@echo
	@echo
	@echo

	@echo
	@echo
	@echo
	@echo
	@echo
	@echo
	@echo
	@echo
	@echo
	@echo

	@echo
	@echo
	@echo
	@echo
	@echo

	@echo "make clean begin"
	@echo $(LINE80)

	@echo "@file $(MY_MAKE_FILE_PATH_NAME)"
	@echo "IS_BUILD_TYPE_VALID = $(IS_BUILD_TYPE_VALID)"
	@echo "BIN = $(BIN)"

	@echo $(LINE80)

	rm -f $(ROOT_CODE_OBJ) $(DEPEND_CODE_OBJ) $(SUB_CODE_OBJ)

ifeq (1, $(IS_BUILD_TYPE_VALID))
	rm -f ./$(BIN)
endif

	@echo "make clean over"

.PHONY: all
all:$(BIN)
	@echo $(LINE80)
	@echo make all
	chmod 777 ./$(BIN)
	find . -name "$(BIN)"

$(BIN) : $(ROOT_CODE_OBJ) $(DEPEND_CODE_OBJ) $(SUB_CODE_OBJ)
	$(CC) $(CFLAGS) -o [email protected] $^ $(SHLIBS) $(INC) $(LIBPATH) $(LIBS)

.cpp.o:
	$(CC) -c $(CFLAGS) -DMAKE_FILE_MACRO__BIN_NAME="\"$(MAKE_FILE_MACRO__BIN_NAME)\"" $^ -o [email protected] $(INC) $(LIBPATH) $(LIBS)

.PHONY: rebuild
rebuild:
	make -f $(MY_MAKE_FILE_PATH_NAME) clean

ifeq (1, $(IS_BUILD_TYPE_VALID))
	@echo $(LINE80)
	make -f $(MY_MAKE_FILE_PATH_NAME) all
	chmod 775 ./$(BIN)
	ldd ./$(BIN)
else
	@echo $(LINE80)
	@echo "error : Makefile command line input error, please see help"	
	@echo "please run => make help"	
	@echo $(LINE80)
endif


#!/bin/bash
# ==============================================================================
# @file build_all_project.sh
# ==============================================================================

make BIN_NAME="test_mysql" rebuild