PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/mysql-5.5.25a/libmysqld/examples/builder-sample/emb_samples.cpp

#
C++ | 300 lines | 242 code | 23 blank | 35 comment | 32 complexity | 0658d0cf1c8c5bba460e2ce800387ec1 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. // Copyright (c) 2002, 2004, 2007 MySQL AB
  2. // Use is subject to license terms.
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 2 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. //---------------------------------------------------------------------------
  17. #include <vcl.h>
  18. #pragma hdrstop
  19. #include "emb_samples.h"
  20. #include <winsock2.h>
  21. #include <mysql.h>
  22. #include <stdarg.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <deque.h>
  27. bool b_new_line = false;
  28. const char *server_groups[] = {
  29. "", "embedded", "server", NULL
  30. };
  31. MYSQL *MySQL;
  32. deque<string> fill_rows(MYSQL_RES *res);
  33. //---------------------------------------------------------------------------
  34. #pragma package(smart_init)
  35. #pragma resource "*.dfm"
  36. TForm1 *Form1;
  37. //---------------------------------------------------------------------------
  38. deque<string> fill_rows(MYSQL_RES *res)
  39. {
  40. MYSQL_ROW row;
  41. deque<string> rows;
  42. while ((row=mysql_fetch_row(res)) != 0)
  43. {
  44. mysql_field_seek(res,0);
  45. for (unsigned int i=0 ; i < mysql_num_fields(res); i++)
  46. rows.push_back(row[i]);
  47. }
  48. return rows;
  49. }
  50. //---------------------------------------------------------------------------
  51. __fastcall TForm1::TForm1(TComponent* Owner)
  52. : TForm(Owner)
  53. {
  54. }
  55. //---------------------------------------------------------------------------
  56. void __fastcall TForm1::Timer1Timer(TObject *Sender)
  57. {
  58. if (is_server_started)
  59. {
  60. ToggleButton->Caption = "Quit";
  61. Timer1->Enabled = false;
  62. }
  63. }
  64. //---------------------------------------------------------------------------
  65. void __fastcall TForm1::FormCreate(TObject *Sender)
  66. {
  67. is_server_started = false;
  68. computer_ip(); /* get the computer name and IP number */
  69. /* init the tree database screen */
  70. db_root = DBView->Items->Add(NULL, db_root_caption.UpperCase());
  71. db_root->ImageIndex = 0;
  72. }
  73. //---------------------------------------------------------------------------
  74. /* button which handle the init of mysql server or quit the app */
  75. void __fastcall TForm1::ToggleButtonClick(TObject *Sender)
  76. {
  77. if (!is_server_started)
  78. {
  79. mysql_server_init(NULL, NULL, (char **)server_groups) ;
  80. connect_server();
  81. get_dbs();
  82. }
  83. else
  84. {
  85. mysql_server_end();
  86. Close();
  87. }
  88. }
  89. //---------------------------------------------------------------------------
  90. void __fastcall TForm1::computer_ip(void)
  91. {
  92. WORD wVersionRequested;
  93. WSADATA WSAData;
  94. wVersionRequested = MAKEWORD(1,1);
  95. WSAStartup(wVersionRequested,&WSAData);
  96. hostent *P;
  97. char s[128];
  98. in_addr in;
  99. char *P2;
  100. gethostname(s, 128);
  101. P = gethostbyname(s);
  102. db_root_caption = P->h_name;
  103. in.S_un.S_un_b.s_b1 = P->h_addr_list[0][0];
  104. in.S_un.S_un_b.s_b2 = P->h_addr_list[0][1];
  105. in.S_un.S_un_b.s_b3 = P->h_addr_list[0][2];
  106. in.S_un.S_un_b.s_b4 = P->h_addr_list[0][3];
  107. P2 = inet_ntoa(in);
  108. db_root_caption += " ( " + (AnsiString)P2 + " )";
  109. }
  110. //---------------------------------------------------------------------------
  111. bool __fastcall TForm1::connect_server()
  112. {
  113. bool ret_value = false;
  114. MySQL = mysql_init(MySQL);
  115. if (!MySQL)
  116. return ret_value;
  117. if (mysql_real_connect(MySQL, NULL, NULL, NULL, NULL, 0, NULL, 0))
  118. {
  119. ret_value = true;
  120. is_server_started = true;
  121. }
  122. MySQL->reconnect= 1;
  123. return ret_value;
  124. }
  125. //---------------------------------------------------------------------------
  126. void __fastcall TForm1::FormDestroy(TObject *Sender)
  127. {
  128. if (is_server_started)
  129. mysql_server_end();
  130. }
  131. //---------------------------------------------------------------------------
  132. void __fastcall TForm1::DBViewClick(TObject *Sender)
  133. {
  134. if (DBView->Selected != db_root && DBView->Selected != NULL)
  135. {
  136. get_tables(DBView->Selected->Text);
  137. clean_desc_grid();
  138. }
  139. }
  140. //---------------------------------------------------------------------------
  141. bool __fastcall TForm1::get_tables(String db_name)
  142. {
  143. MYSQL_RES *res;
  144. AnsiString s_cmd;
  145. TableView->Items->Clear();
  146. s_cmd = "use ";
  147. s_cmd+= db_name.c_str();
  148. if (mysql_query(MySQL, s_cmd.c_str()) ||
  149. !(res=mysql_list_tables(MySQL,"%")))
  150. return false;
  151. tables_node = TableView->Items->Add(NULL, db_name.c_str());
  152. tables_node->ImageIndex = 1;
  153. tables_node->SelectedIndex = 1;
  154. deque<string> rows = fill_rows(res);
  155. mysql_free_result(res);
  156. fill_tree(rows,tables_tree,tables_node,TableView,2);
  157. return true;
  158. }
  159. //---------------------------------------------------------------------------
  160. bool __fastcall TForm1::get_dbs(void)
  161. {
  162. MYSQL_RES *res;
  163. if (!is_server_started)
  164. return false;
  165. if (!(res=mysql_list_dbs(MySQL,"%")))
  166. return false;
  167. deque<string> rows = fill_rows(res);
  168. mysql_free_result(res);
  169. fill_tree(rows,MySQLDbs,db_root,DBView,1);
  170. info_server->Text = mysql_get_server_info(MySQL);
  171. return true;
  172. }
  173. //---------------------------------------------------------------------------
  174. void __fastcall TForm1::fill_tree(deque<string> rows,
  175. TTreeNode *root,
  176. TTreeNode *child,
  177. TTreeView *View,
  178. int image_index)
  179. {
  180. deque<string>::iterator r;
  181. for(r = rows.begin(); r != rows.end() ; r++)
  182. {
  183. root = View->Items->AddChild(child, (*r).c_str());
  184. root->ImageIndex = image_index;
  185. root->SelectedIndex = image_index;
  186. }
  187. child->Expanded = true;
  188. }
  189. //---------------------------------------------------------------------------
  190. bool __fastcall TForm1::get_desc_table(String table_name)
  191. {
  192. MYSQL_RES *res, *res1;
  193. MYSQL_ROW row;
  194. AnsiString use_db, show_cols, show_desc;
  195. unsigned int num_fields;
  196. int fields_control = 0, grid_row = 1, fields_number;
  197. b_new_line= true;
  198. clean_desc_grid();
  199. use_db = "use ";
  200. use_db+= DBView->Selected->Text.c_str();
  201. show_desc = "desc ";
  202. show_cols = "show full columns from ";
  203. show_cols+= table_name.c_str();
  204. show_desc+= table_name.c_str();
  205. if (mysql_query(MySQL, use_db.c_str() ))
  206. return false;
  207. if (mysql_query(MySQL, show_cols.c_str() ) ||
  208. !(res1=mysql_store_result(MySQL)))
  209. {
  210. if (mysql_query(MySQL, show_desc.c_str() ) ||
  211. !(res1=mysql_store_result(MySQL)))
  212. return false ;
  213. }
  214. mysql_fetch_row(res1);
  215. mysql_field_seek(res1,0);
  216. fields_number = (mysql_num_fields(res1) - 2);
  217. mysql_free_result(res1);
  218. if (mysql_query(MySQL, show_cols.c_str() ) ||
  219. !(res=mysql_store_result(MySQL)))
  220. {
  221. if (mysql_query(MySQL, show_desc.c_str() ) ||
  222. !(res=mysql_store_result(MySQL)))
  223. return false ;
  224. }
  225. titles_grid();
  226. while ((row=mysql_fetch_row(res)) != 0)
  227. {
  228. mysql_field_seek(res,0);
  229. for (num_fields=0 ; num_fields < mysql_num_fields(res); num_fields++)
  230. {
  231. if (fields_control <= fields_number )
  232. {
  233. desc_table_grid->Cells[fields_control][grid_row] = row[num_fields];
  234. fields_control++;
  235. }
  236. else
  237. {
  238. desc_table_grid->Cells[(fields_control)][grid_row] = row[num_fields];
  239. fields_control = 0;
  240. grid_row++ ;
  241. desc_table_grid->RowCount++;
  242. }
  243. }
  244. }
  245. desc_table_grid->RowCount--;
  246. mysql_free_result(res);
  247. return true;
  248. }
  249. //---------------------------------------------------------------------------
  250. void __fastcall TForm1::TableViewClick(TObject *Sender)
  251. {
  252. if (DBView->Selected != db_root && DBView->Selected != NULL)
  253. if (DBView->Selected != tables_tree)
  254. get_desc_table(TableView->Selected->Text);
  255. }
  256. //---------------------------------------------------------------------------
  257. void __fastcall TForm1::clean_desc_grid(void)
  258. {
  259. desc_table_grid->RowCount= 2;
  260. desc_table_grid->Cells[0][1] = "";
  261. desc_table_grid->Cells[1][1] = "";
  262. desc_table_grid->Cells[2][1] = "";
  263. desc_table_grid->Cells[3][1] = "";
  264. desc_table_grid->Cells[4][1] = "";
  265. desc_table_grid->Cells[5][1] = "";
  266. }
  267. //---------------------------------------------------------------------------
  268. void __fastcall TForm1::titles_grid(void)
  269. {
  270. desc_table_grid->Cells[0][0] = "Field";
  271. desc_table_grid->Cells[1][0] = "Type";
  272. desc_table_grid->Cells[2][0] = "Null";
  273. desc_table_grid->Cells[3][0] = "Key";
  274. desc_table_grid->Cells[4][0] = "Default";
  275. desc_table_grid->Cells[5][0] = "Extra";
  276. desc_table_grid->Cells[6][0] = "Privileges";
  277. }