forked from motaz/turbobird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopytable.pas
198 lines (173 loc) · 5.25 KB
/
copytable.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
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
unit CopyTable;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
StdCtrls, Buttons, SynEdit, SynHighlighterSQL, sqldb, turbocommon;
type
{ TfmCopyTable }
TfmCopyTable = class(TForm)
bbCopy: TBitBtn;
bbClose: TBitBtn;
cbSourceTable: TComboBox;
cbDestDatabase: TComboBox;
cbDestTable: TComboBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
laDatabase: TLabel;
laDatabase1: TLabel;
laSourceDatabase: TLabel;
SynSQLSyn1: TSynSQLSyn;
syScript: TSynEdit;
procedure bbCopyClick(Sender: TObject);
procedure bbCloseClick(Sender: TObject);
procedure cbDestDatabaseChange(Sender: TObject);
procedure cbSourceTableChange(Sender: TObject);
private
FSourceIndex: Integer;
{ private declarations }
public
{ public declarations }
procedure Init(SourceIndex: Integer; ATableName: string);
end;
var
fmCopyTable: TfmCopyTable;
implementation
{ TfmCopyTable }
uses main, SysTables, EnterPass, Reg;
procedure TfmCopyTable.cbDestDatabaseChange(Sender: TObject);
var
Count: Integer;
begin
laDatabase.Caption:= fmMain.RegisteredDatabases[cbDestDatabase.ItemIndex].RegRec.DatabaseName;
cbDestDatabase.SetFocus;
dmSysTables.Init(cbDestDatabase.ItemIndex);
cbDestTable.Items.CommaText:= dmSysTables.GetDBObjectNames(cbDestDatabase.ItemIndex, otTables, count);
if cbDestTable.Items.IndexOf(cbSourceTable.Text) <> -1 then
cbDestTable.Text:= cbSourceTable.Text;
end;
procedure TfmCopyTable.cbSourceTableChange(Sender: TObject);
var
List: TStringList;
Line: string;
begin
List:= TStringList.Create;
try
fmMain.GetFields(FSourceIndex, cbSourceTable.Text, List);
Line:= List.CommaText;
finally
List.Free;
end;
syScript.Lines.Text:= 'select ' + Line;
syScript.Lines.Add(' from ' + cbSourceTable.Text);
end;
procedure TfmCopyTable.bbCloseClick(Sender: TObject);
begin
Close;
end;
procedure TfmCopyTable.bbCopyClick(Sender: TObject);
var
i: Integer;
Statement: string;
Values: string;
SQLTarget: TSQLQuery;
Num: Integer;
begin
Statement:= 'insert into ' + cbDestTable.Text + ' (';
dmSysTables.sqQuery.Close;
dmSysTables.Init(FSourceIndex);
dmSysTables.sqQuery.SQL.Text:= syScript.Lines.Text;
dmSysTables.sqQuery.Open;
Values:= '';
// Get field names
with dmSysTables.sqQuery do
for i:= 0 to Fields.Count - 1 do
begin
Statement:= Statement + Fields[i].FieldName + ',';
Values:= Values + ':' + Fields[i].FieldName + ',';
Next;
end;
Delete(Statement, Length(Statement), 1);
Delete(Values, Length(Values), 1);
Statement:= Statement + ') Values (' + Values + ')';
// Enter password if it is not saved
with fmMain.RegisteredDatabases[cbDestDatabase.ItemIndex] do
begin
if IBConnection.Password = '' then
begin
if fmEnterPass.ShowModal = mrOk then
begin
if fmReg.TestConnection(RegRec.DatabaseName, fmEnterPass.edUser.Text, fmEnterPass.edPassword.Text,
RegRec.Charset) then
with fmMain do
begin
RegisteredDatabases[cbDestDatabase.ItemIndex].RegRec.UserName:= fmEnterPass.edUser.Text;
RegisteredDatabases[cbDestDatabase.ItemIndex].RegRec.Password:= fmEnterPass.edPassword.Text;
RegisteredDatabases[cbDestDatabase.ItemIndex].RegRec.Role:= fmEnterPass.cbRole.Text;
end
else
begin
Exit;
end;
end
end;
SQLTarget:= TSQLQuery.Create(nil);
try
SQLTarget.DataBase:= IBConnection;
SQLTarget.Transaction:= SQLTrans;
SQLTarget.SQL.Text:= Statement;
// Start copy
try
dmSysTables.sqQuery.First;
Num:= 0;
with dmSysTables.sqQuery do
while not EOF do
begin
for i:= 0 to Fields.Count - 1 do
SQLTarget.Params.ParamByName(Fields[i].FieldName).Value:= Fields[i].Value;
SQLTarget.ExecSQL;
Inc(Num);
Next;
end;
SQLTrans.Commit;
ShowMessage(IntToStr(Num) + ' record(s) has been copied' + LineEnding + 'Don''t forget to set the Generator to the new value, ' +
'if it exists');
dmSysTables.sqQuery.Close;
Close;
except
on E: Exception do
begin
MessageDlg('Error while copy: ' + e.Message, mtError, [mbOk], 0);
SQLTrans.Rollback;
end;
end;
finally
SQLTarget.Free;
end;
end;
dmSysTables.sqQuery.Close;
end;
procedure TfmCopyTable.Init(SourceIndex: Integer; ATableName: string);
var
i: Integer;
Count: Integer;
begin
dmSysTables.sqQuery.Close; //todo: (low priority) is closing sqquery really necessary?
FSourceIndex:= SourceIndex;
laSourceDatabase.Caption:= fmMain.RegisteredDatabases[SourceIndex].RegRec.Title;
cbDestDatabase.Clear;
// Display databases in destination combo box
for i:= 0 to High(fmMain.RegisteredDatabases) do
cbDestDatabase.Items.Add(fmMain.RegisteredDatabases[i].RegRec.Title);
laDatabase.Caption:= '';
cbSourceTable.Items.CommaText:= dmSysTables.GetDBObjectNames(SourceIndex, otTables, count);
cbSourceTable.Text:= ATableName;
SynSQLSyn1.TableNames.Text:= cbSourceTable.Text;
cbSourceTableChange(nil);
end;
initialization
{$I copytable.lrs}
end.