unit dbeditor;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls, ExtCtrls,
mysql, sEdit, sButton, sSkinManager,
sLabel;
type
TForm1 =
class(TForm)
Bevel1: TBevel;
DatabaseListBox: TListBox;
TableListBox: TListBox;
TableStringGrid: TStringGrid;
FieldListGrid: TStringGrid;
sSkinManager1: TsSkinManager;
LoginButton: TsButton;
PasswordEdit: TsEdit;
UserEdit: TsEdit;
HostEdit: TsEdit;
sLabelFX1: TsLabelFX;
sLabelFX2: TsLabelFX;
sLabelFX3: TsLabelFX;
sLabelFX4: TsLabelFX;
sLabelFX5: TsLabelFX;
sLabelFX7: TsLabelFX;
sLabelFX8: TsLabelFX;
sLabelFX9: TsLabelFX;
sLabelFX10: TsLabelFX;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure DatabaseListBoxClick(Sender: TObject);
procedure TableListBoxClick(Sender: TObject);
procedure LoginButtonClick(Sender: TObject);
private
LibHandle: PMYSQL;
ConHandle: PMYSQL;
function DoQuote(
const s:
String):
String;
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
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_Res: PMYSQL_RES;
MYSQL_ROW: PMYSQL_ROW;
begin
TableListBox.Items.Clear;
ClearGrid(FieldListGrid);
TableListBox.Enabled := False;
with DatabaseListBox
do
MyResult := mysql_select_db(ConHandle, PAnsiChar(Items[ItemIndex]));
if MyResult<>0
then
raise Exception.Create(mysql_error(ConHandle));
mySQL_Res := mysql_list_tables(ConHandle,
nil);
if mySQL_Res=nil
then
raise Exception.Create(mysql_error(ConHandle));
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);
end;
if TableListBox.Items.Count>0
then begin
TableListBox.Enabled := True;
end;
end;
function TForm1.DoQuote(
const s:
String):
String;
begin
SetLength(Result, Length(s)*2+1);
SetLength(Result, mysql_real_escape_string(ConHandle,
Pointer(Result),
PChar(s),
Length(s)));
Result := '
`' + Result + '
`';
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
libmysql_fast_load(
nil);
LibHandle := mysql_init(
nil);
FieldListGrid.Cells[0, 0] := '
Name';
FieldListGrid.Cells[1, 0] := '
Type';
FieldListGrid.Cells[2, 0] := '
Len';
FieldListGrid.Cells[3, 0] := '
Flags';
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
mysql_close(LibHandle);
libmysql_free;
end;
procedure TForm1.LoginButtonClick(Sender: TObject);
var
mySQL_Res: PMYSQL_RES;
MYSQL_ROW: PMYSQL_ROW;
begin
DatabaseListBox.Items.Clear;
DatabaseListBox.Enabled := False;
ConHandle := mysql_real_connect(LibHandle,
PAnsiChar(HostEdit.Text),
PAnsiChar(UserEdit.Text),
PAnsiChar(PasswordEdit.Text),
nil, 0,
nil, 0);
if ConHandle=nil
then
raise Exception.Create(mysql_error(LibHandle));
Caption := HostEdit.Text + '
' + mysql_get_server_info(ConHandle);
mySQL_Res := mysql_list_dbs(ConHandle,
nil);
if mySQL_Res=nil
then
raise Exception.Create(mysql_error(ConHandle));
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);
end;
if DatabaseListBox.Items.Count>0
then begin
LoginButton.Enabled := False;
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 GetFieldFlagString(mySQL_Field: PMYSQL_FIELD):
String;
begin
Result := '
';
if IS_NUM_FIELD(mySQL_Field)
then begin
if (mysql_field_flag(mySQL_Field)
and UNSIGNED_FLAG) <> 0
then
Result := '
UNSIGNED';
if (mysql_field_flag(mySQL_Field)
and AUTO_INCREMENT_FLAG) <> 0
then
Result := Result + '
INC';
end
else begin
if (mysql_field_flag(mySQL_Field)
and ENUM_FLAG)<>0
then
Result := '
ENUM'
else
if (mysql_field_flag(mySQL_Field)
and SET_FLAG)<>0
then
Result := '
SET'
else
if (mysql_field_flag(mySQL_Field)
and BLOB_FLAG)<>0
then
Result := '
BLOB';
end;
if IS_NOT_NULL(mysql_field_flag(mySQL_Field))
then
Result := Result + '
NOT NULL';
end;
procedure TForm1.TableListBoxClick(Sender: TObject);
var
i, r, field_count, row_count: Integer;
mySQL_Res: PMYSQL_RES;
MYSQL_ROW: PMYSQL_ROW;
mySQL_Field: PMYSQL_FIELD;
sql:
String;
tablename:
String;
begin
ClearGrid(FieldListGrid);
with TableListBox
do
tablename := Items[ItemIndex];
tablename := DoQuote(tablename);
sql := '
select * from ' + tablename;
if mysql_real_query(ConHandle, PChar(
sql), Length(
sql))<>0
then
raise Exception.Create(mysql_error(ConHandle));
mySQL_Res := mysql_store_result(ConHandle);
if mySQL_Res<>
nil
then begin
try
field_count := mysql_num_fields(mySQL_Res);
FieldListGrid.RowCount := field_count+1;
TableStringGrid.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
TableStringGrid.Cells[i, 0] := mysql_field_name(mySQL_Field);
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);
end;
end;
//Get Data
row_count := mysql_num_rows(mySQL_Res);
if row_count>0
then begin
TableStringGrid.RowCount := row_count + 1;
for r := 1
to row_count
do
begin
MYSQL_ROW := mysql_fetch_row(mySQL_Res);
if MYSQL_ROW<>
nil
then begin
for i := 0
to field_count - 1
do
TableStringGrid.Cells[i, r] := MYSQL_ROW^[i];
end;
end;
end
else begin
TableStringGrid.RowCount := 2;
for i := 0
to field_count - 1
do
TableStringGrid.Cells[i, 1] := '
';
end;
finally
mysql_free_result(mySQL_Res);
end;
end;
end;
end.