forked from motaz/turbobird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlhistory.pas
154 lines (131 loc) · 3.86 KB
/
sqlhistory.pas
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
unit SQLHistory;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, db, FileUtil, LResources, Forms, Controls, Graphics,
Dialogs, DBGrids, Buttons, StdCtrls, EditBtn;
type
{ TfmSQLHistory }
TfmSQLHistory = class(TForm)
bbInsert: TBitBtn;
bbDelete: TBitBtn;
bbExport: TBitBtn;
cbSQLType: TComboBox;
cxOverwrite: TCheckBox;
cxAfterDate: TCheckBox;
Datasource1: TDatasource;
DateEdit1: TDateEdit;
DBGrid1: TDBGrid;
GroupBox1: TGroupBox;
Label1: TLabel;
SaveDialog1: TSaveDialog;
procedure bbDeleteClick(Sender: TObject);
procedure bbExportClick(Sender: TObject);
procedure bbInsertClick(Sender: TObject);
procedure cxAfterDateClick(Sender: TObject);
procedure DBGrid1DblClick(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
private
FQueryForm: TForm;
{ private declarations }
public
{ public declarations }
procedure Init(DatabaseTitle: string; QueryForm: TForm);
end;
var
fmSQLHistory: TfmSQLHistory;
implementation
{ TfmSQLHistory }
uses Main, QueryWindow;
procedure TfmSQLHistory.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
Datasource1.DataSet:= nil;
end;
procedure TfmSQLHistory.FormCreate(Sender: TObject);
begin
DateEdit1.Date:= Now - 7;
end;
procedure TfmSQLHistory.bbInsertClick(Sender: TObject);
var
SQLStatement: string;
i: Integer;
aStatement: string;
begin
// SQLStatement:= (fmMain.mdsHistory.FieldByName('SQLStatement').AsString);
for i:=0 to DBGrid1.SelectedRows.Count - 1 do
begin
Datasource1.DataSet.GotoBookmark(DBGrid1.SelectedRows.Items[i]);
aStatement := fmMain.mdsHistory.FieldByName('SQLStatement').AsString;
if Pos(';', aStatement) = 0 then
aStatement:= aStatement + ';';
SQLStatement += aStatement;
end;
if cxOverwrite.Checked then
(FQueryForm as TfmQueryWindow).meQuery.Lines.Clear;
(FQueryForm as TfmQueryWindow).meQuery.Lines.Text:= (FQueryForm as TfmQueryWindow).meQuery.Lines.Text + SQLStatement;
Close;
end;
procedure TfmSQLHistory.cxAfterDateClick(Sender: TObject);
begin
DateEdit1.Visible:= cxAfterDate.Checked;
end;
procedure TfmSQLHistory.DBGrid1DblClick(Sender: TObject);
begin
bbInsertClick(nil);
end;
procedure TfmSQLHistory.bbDeleteClick(Sender: TObject);
begin
if MessageDlg('Are you sure you want to delete current record of history', mtConfirmation, [mbYes, mbNo], 0) = mrYes
then
fmMain.mdsHistory.Delete;
end;
procedure TfmSQLHistory.bbExportClick(Sender: TObject);
var
CurrType: string;
List: TStringList;
Line: string;
begin
if SaveDialog1.Execute then
with fmMain.mdsHistory do
begin
DBGrid1.Visible:= False;
First;
List:= TStringList.Create;
try
while not Eof do
begin
if (not cxAfterDate.Checked) or (FieldByName('Time').AsDateTime > DateEdit1.Date) then
begin
CurrType:= FieldByName('SQLType').AsString;
if (cbSQLType.ItemIndex = 0) or
((CurrType = 'DDL') and (cbSQLType.ItemIndex in [1, 2])) or
((CurrType = 'DML') and (cbSQLType.ItemIndex in [1, 3])) or
((CurrType = 'SELECT') and (cbSQLType.ItemIndex = 4)) then
begin
List.Add('-- ' + FieldByName('Time').AsString);
Line:= FieldByName('SQLStatement').AsString;
if Pos(';', Line) = 0 then
Line:= Line + ';';
List.Add(Line);
end;
end;
Next;
end;
List.SaveToFile(SaveDialog1.FileName);
finally
List.Free;
end;
DBGrid1.Visible:= True;
end;
end;
procedure TfmSQLHistory.Init(DatabaseTitle: string; QueryForm: TForm);
begin
FQueryForm:= QueryForm;
Caption:= 'SQL History for: ' + DatabaseTitle;
Datasource1.DataSet:= fmMain.mdsHistory;
fmMain.mdsHistory.Last;
end;
initialization
{$I sqlhistory.lrs}
end.