1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
| unit UDemo;
{
Copyright (C) 2009 Samuel Soldat <samuel.soldat@audio-data.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls, ExtCtrls, ComCtrls, mysql;
type
TForm1 = class(TForm)
HostEdit: TEdit;
Bevel1: TBevel;
Label1: TLabel;
Label2: TLabel;
UserEdit: TEdit;
Label3: TLabel;
PasswordEdit: TEdit;
LoginButton: TButton;
DatabaseListBox: TListBox;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
TableListBox: TListBox;
TableGrid: TDrawGrid;
Label7: TLabel;
FieldListGrid: TStringGrid;
StatusBar: TStatusBar;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure LoginButtonClick(Sender: TObject);
procedure DatabaseListBoxClick(Sender: TObject);
procedure TableListBoxClick(Sender: TObject);
procedure TableGridDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
LibHandle: PMYSQL;
mySQL_Res: PMYSQL_RES;
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{$WARNINGS OFF}
procedure ClearGrid(Grid: TStringGrid);
var
row, col: Integer;
begin
for row := 1 to Grid.RowCount - 1 do
for col := 0 to Grid.ColCount - 1 do
Grid.Cells[col, row] := '';
end;
procedure TForm1.DatabaseListBoxClick(Sender: TObject);
var
MyResult: Integer;
MYSQL_ROW: PMYSQL_ROW;
begin
if mySQL_Res<>nil
then
mysql_free_result(mySQL_Res);
mySQL_Res := nil;
TableListBox.Items.Clear;
ClearGrid(FieldListGrid);
TableListBox.Enabled := False;
with DatabaseListBox do
MyResult := mysql_select_db(LibHandle, PAnsiChar(AnsiString(Items[ItemIndex])));
if MyResult<>0
then
raise Exception.Create(mysql_error(LibHandle));
//Get Tablenames
mySQL_Res := mysql_list_tables(LibHandle, nil);
if mySQL_Res=nil
then
raise Exception.Create(mysql_error(LibHandle));
try
repeat
MYSQL_ROW := mysql_fetch_row(mySQL_Res);
if MYSQL_ROW<>nil
then begin
TableListBox.Items.Add(MYSQL_ROW^[0]);
end;
until MYSQL_ROW=nil;
finally
mysql_free_result(mySQL_Res);
mySQL_Res := nil;
end;
if TableListBox.Items.Count>0
then begin
TableListBox.Enabled := True;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
{ for embeeded server use
libmysql_fast_load('c:\MySql\bin\libmysqld.dll');
}
libmysql_fast_load(nil);
StatusBar.Panels[0].Text := 'Client ' + mysql_get_client_info;
FieldListGrid.Cells[0, 0] := 'Name';
FieldListGrid.Cells[1, 0] := 'Type';
FieldListGrid.Cells[2, 0] := 'Len';
FieldListGrid.Cells[3, 0] := 'Flags';
FieldListGrid.Cells[4, 0] := 'Default';
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
if mySQL_Res<>nil
then
mysql_free_result(mySQL_Res);
if libmysql_status=LIBMYSQL_READY
then
mysql_close(LibHandle);
libmysql_free;
end;
procedure TForm1.LoginButtonClick(Sender: TObject);
var
MYSQL_ROW: PMYSQL_ROW;
begin
if mySQL_Res<>nil
then
mysql_free_result(mySQL_Res);
mySQL_Res := nil;
DatabaseListBox.Items.Clear;
DatabaseListBox.Enabled := False;
if LibHandle<>nil
then begin
mysql_close(LibHandle);
LibHandle := nil;
end;
{
Examble for embeeded server init (only needed in special cases)
const
DefaultFile: array [0..1] of PAnsiChar=('', '--defaults-file=c:\windows\my.ini');
...
if mysql_server_init(2, @DefaultFile, nil)<>0
then
raise Exception.Create('Embedded Server Init Failed');
}
LibHandle := mysql_init(nil);
if LibHandle=nil
then
raise Exception.Create('mysql_init failed');
if (mysql_real_connect(LibHandle,
PAnsiChar(AnsiString(HostEdit.Text)),
PAnsiChar(AnsiString(UserEdit.Text)),
PAnsiChar(AnsiString(PasswordEdit.Text)),
nil, 0, nil, 0)=nil)
then
raise Exception.Create(mysql_error(LibHandle));
Caption := HostEdit.Text + ' ' + mysql_get_server_info(LibHandle);
StatusBar.Panels[1].Text := mysql_character_set_name(LibHandle);
//Get Databasenames
mySQL_Res := mysql_list_dbs(LibHandle, nil);
if mySQL_Res=nil
then
raise Exception.Create(mysql_error(LibHandle));
try
repeat
MYSQL_ROW := mysql_fetch_row(mySQL_Res);
if MYSQL_ROW<>nil
then begin
DatabaseListBox.Items.Add(MYSQL_ROW^[0]);
end;
until MYSQL_ROW=nil;
finally
mysql_free_result(mySQL_Res);
mySQL_Res := nil;
end;
if DatabaseListBox.Items.Count>0
then begin
DatabaseListBox.Enabled := True;
end;
end;
function GetFieldTypeString(mySQL_Field: PMYSQL_FIELD): String;
const
FieldtypeString1: array [MYSQL_TYPE_DECIMAL..MYSQL_TYPE_BIT] of String=(
'NUMERIC', 'TINYINT', 'SMALLINT', 'INTEGER',
'FLOAT', 'DOUBLE', 'T_NULL', 'TIMESTAMP',
'BIGINT', 'MEDIUMINT', 'DATE', 'TIME',
'DATETIME', 'YEAR', 'NEWDATE', 'VARCHAR',
'BIT');
FieldtypeString2: array [MYSQL_TYPE_NEWDECIMAL..MYSQL_TYPE_GEOMETRY] of String=(
'NEWDECIMAL', 'ENUM', 'SET',
'TINY_BLOB', 'MEDIUM_BLOB', 'LONG_BLOB', 'BLOB',
'VAR_STRING', 'STRING', 'GEOMETRY');
begin
if mysql_field_type(mySQL_Field) in [MYSQL_TYPE_DECIMAL..MYSQL_TYPE_BIT]
then
Result := FieldtypeString1[mysql_field_type(mySQL_Field)]
else
if mysql_field_type(mySQL_Field) in [MYSQL_TYPE_NEWDECIMAL..MYSQL_TYPE_GEOMETRY]
then
Result := FieldtypeString2[mysql_field_type(mySQL_Field)]
else
Result := 'unknown';
end;
function GetFieldFlagString1(flag: longword): String;
begin
// UpdateField(mySQL_Field);
Result := '';
if IS_NUM_FLAG(flag)
then begin
if (flag and UNSIGNED_FLAG) <> 0
then
Result := ' UNSIGNED';
if (flag and AUTO_INCREMENT_FLAG) <> 0
then
Result := Result + ' INC';
end
else begin
if (flag and ENUM_FLAG)<>0
then
Result := ' ENUM'
else
if (flag and SET_FLAG)<>0
then
Result := ' SET'
else
if (flag and BLOB_FLAG)<>0
then
Result := ' BLOB';
end;
if IS_NOT_NULL(flag)
then
Result := Result + ' NOT NULL';
if IS_PRI_KEY(flag)
then
Result := Result + ' PRI_KEY';
if flag and MULTIPLE_KEY_FLAG<>0
then
Result := Result + ' KEY';
end;
function GetFieldFlagString(Flags: longword): String;
procedure Append(var s: String; const FlagStr: String);
begin
if s=''
then
s := FlagStr
else
s := s + ',' + FlagStr;
end;
begin
Result := '';
if IS_NUM_FLAG(Flags)
then begin
Append(Result, 'NUM');
if (Flags and UNSIGNED_FLAG) <> 0
then
Append(Result, 'UNSIGNED');
if (Flags and AUTO_INCREMENT_FLAG) <> 0
then
Append(Result, 'AUTO_INCREMENT');
end
else begin
if (Flags and ENUM_FLAG)<>0
then
Append(Result, 'ENUM');
if (Flags and SET_FLAG)<>0
then
Append(Result, 'SET');
if (Flags and BLOB_FLAG)<>0
then
Append(Result, 'BLOB');
end;
if (Flags and NOT_NULL_FLAG)<>0
then
Append(Result, 'NOT_NULL');
if (Flags and PRI_KEY_FLAG)<>0
then
Append(Result, 'PRI_KEY');
if (Flags and UNIQUE_KEY_FLAG)<>0
then
Append(Result, 'UNIQUE_KEY');
if (Flags and MULTIPLE_KEY_FLAG)<>0
then
Append(Result, 'MULTIPLE_KEY');
if (Flags and ZEROFILL_FLAG)<>0
then
Append(Result, 'ZEROFILL');
if (Flags and BINARY_FLAG)<>0
then
Append(Result, 'BINARY');
if (Flags and NO_DEFAULT_VALUE_FLAG)<>0
then
Append(Result, 'NO_DEFAULT_VALUE');
if (Flags and TIMESTAMP_FLAG)<>0
then
Append(Result, 'TIMESTAMP');
end;
procedure TForm1.TableListBoxClick(Sender: TObject);
var
i, field_count, row_count: Integer;
mySQL_Field: PMYSQL_FIELD;
sql: AnsiString;
tablename: String;
ticks: Cardinal;
begin
ClearGrid(FieldListGrid);
Statusbar.Panels[2].Text := '';
with TableListBox do
tablename := Items[ItemIndex];
if mySQL_Res<>nil
then
mysql_free_result(mySQL_Res);
mySQL_Res := mysql_list_fields(LibHandle, PAnsiChar(AnsiString(tablename)), nil);
if mySQL_Res<>nil
then begin
//Get Fieldnames
field_count := mysql_num_fields(mySQL_Res);
FieldListGrid.RowCount := field_count+1;
TableGrid.ColCount := field_count;
for i := 0 to field_count - 1 do
begin
mySQL_Field := mysql_fetch_field(mySQL_Res);
if mySQL_Field<>nil
then begin
FieldListGrid.Cells[0, i+1] := mysql_field_name(mySQL_Field);
FieldListGrid.Cells[1, i+1] := GetFieldTypeString(mySQL_Field);
FieldListGrid.Cells[2, i+1] := IntToStr(mysql_field_length(mySQL_Field));
FieldListGrid.Cells[3, i+1] := GetFieldFlagString(mysql_field_flag(mySQL_Field));
FieldListGrid.Cells[4, i+1] := mysql_field_default(mySQL_Field)
end;
end;
mysql_free_result(mySQL_Res);
mySQL_Res := nil;
end;
sql := 'select * from ' + QuoteName(tablename);
ticks := GetTickCount;
if mysql_real_query(LibHandle, PAnsiChar(sql), Length(sql))<>0
then
raise Exception.Create(mysql_error(LibHandle));
//Get Data
mySQL_Res := mysql_store_result(LibHandle);
if mySQL_Res<>nil
then begin
row_count := mysql_num_rows(mySQL_Res);
Statusbar.Panels[2].Text := Format('Rowcount: %d - Time: %d ms', [row_count, (GetTickCount-ticks)]);
if row_count>0
then begin
TableGrid.RowCount := row_count + 1;
end
else begin
TableGrid.RowCount := 2;
end;
end;
end;
procedure TForm1.TableGridDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
MYSQL_ROW: PMYSQL_ROW;
mySQL_Field: PMYSQL_FIELD;
begin
if mySQL_Res<>nil
then begin
if (ARow=0)
then begin
mySQL_Field := mysql_fetch_field_direct(mySQL_Res, ACol);
if mySQL_Field<>nil
then begin
TableGrid.Canvas.TextRect(Rect, Rect.Left+2, Rect.Top+2, mysql_field_name(mySQL_Field));
end;
end
else
if (ARow>0) and (ARow<=mysql_num_rows(mySQL_Res))
then begin
mysql_data_seek(mySQL_Res, ARow-1);
MYSQL_ROW := mysql_fetch_row(mySQL_Res);
if MYSQL_ROW<>nil
then begin
TableGrid.Canvas.TextRect(Rect, Rect.Left+2, Rect.Top+2, MYSQL_ROW^[ACol]);
end;
end;
end;
end;
end. |
Partager