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