MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
emb_samples.cpp
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 //---------------------------------------------------------------------------
18 #include <vcl.h>
19 #pragma hdrstop
20 
21 #include "emb_samples.h"
22 #include <winsock2.h>
23 #include <mysql.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <deque.h>
29 bool b_new_line = false;
30 const char *server_groups[] = {
31  "", "embedded", "server", NULL
32 };
33 MYSQL *MySQL;
34 deque<string> fill_rows(MYSQL_RES *res);
35 //---------------------------------------------------------------------------
36 #pragma package(smart_init)
37 #pragma resource "*.dfm"
38 TForm1 *Form1;
39 //---------------------------------------------------------------------------
40 deque<string> fill_rows(MYSQL_RES *res)
41 {
42  MYSQL_ROW row;
43  deque<string> rows;
44 
45  while ((row=mysql_fetch_row(res)) != 0)
46  {
47  mysql_field_seek(res,0);
48  for (unsigned int i=0 ; i < mysql_num_fields(res); i++)
49  rows.push_back(row[i]);
50  }
51  return rows;
52 }
53 //---------------------------------------------------------------------------
54 __fastcall TForm1::TForm1(TComponent* Owner)
55  : TForm(Owner)
56 {
57 }
58 //---------------------------------------------------------------------------
59 
60 void __fastcall TForm1::Timer1Timer(TObject *Sender)
61 {
62  if (is_server_started)
63  {
64  ToggleButton->Caption = "Quit";
65  Timer1->Enabled = false;
66  }
67 }
68 //---------------------------------------------------------------------------
69 void __fastcall TForm1::FormCreate(TObject *Sender)
70 {
71  is_server_started = false;
72  computer_ip(); /* get the computer name and IP number */
73  /* init the tree database screen */
74  db_root = DBView->Items->Add(NULL, db_root_caption.UpperCase());
75  db_root->ImageIndex = 0;
76 }
77 //---------------------------------------------------------------------------
78 /* button which handle the init of mysql server or quit the app */
79 void __fastcall TForm1::ToggleButtonClick(TObject *Sender)
80 {
81  if (!is_server_started)
82  {
83  mysql_server_init(NULL, NULL, (char **)server_groups) ;
84  connect_server();
85  get_dbs();
86  }
87  else
88  {
89  mysql_server_end();
90  Close();
91  }
92 }
93 //---------------------------------------------------------------------------
94 void __fastcall TForm1::computer_ip(void)
95 {
96  WORD wVersionRequested;
97  WSADATA WSAData;
98  wVersionRequested = MAKEWORD(1,1);
99  WSAStartup(wVersionRequested,&WSAData);
100  hostent *P;
101  char s[128];
102  in_addr in;
103  char *P2;
104 
105  gethostname(s, 128);
106  P = gethostbyname(s);
107  db_root_caption = P->h_name;
108  in.S_un.S_un_b.s_b1 = P->h_addr_list[0][0];
109  in.S_un.S_un_b.s_b2 = P->h_addr_list[0][1];
110  in.S_un.S_un_b.s_b3 = P->h_addr_list[0][2];
111  in.S_un.S_un_b.s_b4 = P->h_addr_list[0][3];
112  P2 = inet_ntoa(in);
113  db_root_caption += " ( " + (AnsiString)P2 + " )";
114 }
115 //---------------------------------------------------------------------------
116 bool __fastcall TForm1::connect_server()
117 {
118  bool ret_value = false;
119 
120  MySQL = mysql_init(MySQL);
121  if (!MySQL)
122  return ret_value;
123  if (mysql_real_connect(MySQL, NULL, NULL, NULL, NULL, 0, NULL, 0))
124  {
125  ret_value = true;
126  is_server_started = true;
127  }
128  MySQL->reconnect= 1;
129  return ret_value;
130 }
131 //---------------------------------------------------------------------------
132 void __fastcall TForm1::FormDestroy(TObject *Sender)
133 {
134  if (is_server_started)
135  mysql_server_end();
136 }
137 //---------------------------------------------------------------------------
138 
139 void __fastcall TForm1::DBViewClick(TObject *Sender)
140 {
141  if (DBView->Selected != db_root && DBView->Selected != NULL)
142  {
143  get_tables(DBView->Selected->Text);
144  clean_desc_grid();
145  }
146 }
147 //---------------------------------------------------------------------------
148 bool __fastcall TForm1::get_tables(String db_name)
149 {
150  MYSQL_RES *res;
151  AnsiString s_cmd;
152 
153  TableView->Items->Clear();
154  s_cmd = "use ";
155  s_cmd+= db_name.c_str();
156 
157  if (mysql_query(MySQL, s_cmd.c_str()) ||
158  !(res=mysql_list_tables(MySQL,"%")))
159  return false;
160 
161  tables_node = TableView->Items->Add(NULL, db_name.c_str());
162  tables_node->ImageIndex = 1;
163  tables_node->SelectedIndex = 1;
164 
165  deque<string> rows = fill_rows(res);
166 
167  mysql_free_result(res);
168  fill_tree(rows,tables_tree,tables_node,TableView,2);
169 
170  return true;
171 }
172 //---------------------------------------------------------------------------
173 bool __fastcall TForm1::get_dbs(void)
174 {
175  MYSQL_RES *res;
176 
177  if (!is_server_started)
178  return false;
179 
180  if (!(res=mysql_list_dbs(MySQL,"%")))
181  return false;
182 
183  deque<string> rows = fill_rows(res);
184 
185  mysql_free_result(res);
186  fill_tree(rows,MySQLDbs,db_root,DBView,1);
187  info_server->Text = mysql_get_server_info(MySQL);
188 
189  return true;
190 }
191 //---------------------------------------------------------------------------
192 void __fastcall TForm1::fill_tree(deque<string> rows,
193  TTreeNode *root,
194  TTreeNode *child,
195  TTreeView *View,
196  int image_index)
197 {
198  deque<string>::iterator r;
199  for(r = rows.begin(); r != rows.end() ; r++)
200  {
201  root = View->Items->AddChild(child, (*r).c_str());
202  root->ImageIndex = image_index;
203  root->SelectedIndex = image_index;
204  }
205  child->Expanded = true;
206 }
207 //---------------------------------------------------------------------------
208 bool __fastcall TForm1::get_desc_table(String table_name)
209 {
210  MYSQL_RES *res, *res1;
211  MYSQL_ROW row;
212  AnsiString use_db, show_cols, show_desc;
213  unsigned int num_fields;
214  int fields_control = 0, grid_row = 1, fields_number;
215  b_new_line= true;
216 
217  clean_desc_grid();
218  use_db = "use ";
219  use_db+= DBView->Selected->Text.c_str();
220  show_desc = "desc ";
221  show_cols = "show full columns from ";
222  show_cols+= table_name.c_str();
223  show_desc+= table_name.c_str();
224 
225  if (mysql_query(MySQL, use_db.c_str() ))
226  return false;
227 
228  if (mysql_query(MySQL, show_cols.c_str() ) ||
229  !(res1=mysql_store_result(MySQL)))
230  {
231  if (mysql_query(MySQL, show_desc.c_str() ) ||
232  !(res1=mysql_store_result(MySQL)))
233  return false ;
234  }
235  mysql_fetch_row(res1);
236  mysql_field_seek(res1,0);
237  fields_number = (mysql_num_fields(res1) - 2);
238  mysql_free_result(res1);
239 
240  if (mysql_query(MySQL, show_cols.c_str() ) ||
241  !(res=mysql_store_result(MySQL)))
242  {
243  if (mysql_query(MySQL, show_desc.c_str() ) ||
244  !(res=mysql_store_result(MySQL)))
245  return false ;
246  }
247  titles_grid();
248  while ((row=mysql_fetch_row(res)) != 0)
249  {
250  mysql_field_seek(res,0);
251  for (num_fields=0 ; num_fields < mysql_num_fields(res); num_fields++)
252  {
253  if (fields_control <= fields_number )
254  {
255  desc_table_grid->Cells[fields_control][grid_row] = row[num_fields];
256  fields_control++;
257  }
258  else
259  {
260  desc_table_grid->Cells[(fields_control)][grid_row] = row[num_fields];
261  fields_control = 0;
262  grid_row++ ;
263  desc_table_grid->RowCount++;
264  }
265  }
266  }
267  desc_table_grid->RowCount--;
268  mysql_free_result(res);
269  return true;
270 }
271 //---------------------------------------------------------------------------
272 void __fastcall TForm1::TableViewClick(TObject *Sender)
273 {
274  if (DBView->Selected != db_root && DBView->Selected != NULL)
275  if (DBView->Selected != tables_tree)
276  get_desc_table(TableView->Selected->Text);
277 }
278 //---------------------------------------------------------------------------
279 void __fastcall TForm1::clean_desc_grid(void)
280 {
281  desc_table_grid->RowCount= 2;
282  desc_table_grid->Cells[0][1] = "";
283  desc_table_grid->Cells[1][1] = "";
284  desc_table_grid->Cells[2][1] = "";
285  desc_table_grid->Cells[3][1] = "";
286  desc_table_grid->Cells[4][1] = "";
287  desc_table_grid->Cells[5][1] = "";
288 }
289 //---------------------------------------------------------------------------
290 void __fastcall TForm1::titles_grid(void)
291 {
292  desc_table_grid->Cells[0][0] = "Field";
293  desc_table_grid->Cells[1][0] = "Type";
294  desc_table_grid->Cells[2][0] = "Null";
295  desc_table_grid->Cells[3][0] = "Key";
296  desc_table_grid->Cells[4][0] = "Default";
297  desc_table_grid->Cells[5][0] = "Extra";
298  desc_table_grid->Cells[6][0] = "Privileges";
299 }
300