forked from motaz/turbobird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neweditfield.pas
311 lines (273 loc) · 8.67 KB
/
neweditfield.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
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
unit NewEditField;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
StdCtrls, Spin, Buttons, turbocommon;
type
TFormMode = (foNew, foEdit);
{ TfmNewEditField }
TfmNewEditField = class(TForm)
bbAdd: TBitBtn;
cbCharset: TComboBox;
cbCollation: TComboBox;
cbType: TComboBox;
cxAllowNull: TCheckBox;
edDescription: TEdit;
edFieldName: TEdit;
edDefault: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
lblCharset: TLabel;
lblCollation: TLabel;
seSize: TSpinEdit;
seOrder: TSpinEdit;
seScale: TSpinEdit;
procedure bbAddClick(Sender: TObject);
procedure cbCharsetEditingDone(Sender: TObject);
procedure cbTypeEditingDone(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
private
FDBIndex: Integer;
FTableName: string;
FRefreshButton: TBitBtn;
public
fFormMode: TFormMode;
OldFieldName: string;
OldFieldType: string;
OldFieldSize: integer;
OldFieldScale: integer;
OldAllowNull: Boolean;
OldOrder: integer;
OldDefault: string;
OldCharacterSet: string;
OldCollation: string;
OldDescription: string;
procedure Init(dbIndex: Integer; TableName: string;
FormMode: TFormMode;
FieldName, FieldType,
CharacterSet, Collation,
DefaultValue, Description: string;
Size, Scale, Order: Integer;
AllowNull: Boolean;
RefreshButton: TBitBtn);
{ public declarations }
end;
var
fmNewEditField: TfmNewEditField;
implementation
{ TfmNewEditField }
uses Main, SysTables;
procedure TfmNewEditField.bbAddClick(Sender: TObject);
var
Line: string;
Nullflag: string;
Clk: TNotifyEvent;
begin
if FRefreshButton = nil then
clk:= nil
else
clk:= FRefreshButton.OnClick;
if fFormMode = foNew then // New field
begin
Line:= cbType.Text;
if (Line='CHAR') or
(Line='CSTRING') or
(Line='VARCHAR') then
Line:= Line + '(' + IntToStr(seSize.Value) + ')';
// Default value
if Trim(edDefault.Text) <> '' then
begin
if ((cbType.Text='CHAR') or
(cbType.Text='CSTRING') or
(cbType.Text='VARCHAR')) and
(Pos('''', edDefault.Text) = 0) then
Line:= Line + ' default ' + QuotedStr(edDefault.Text)
else
Line:= Line + ' default ' + edDefault.Text;
end;
// Null/Not null
if not cxAllowNull.Checked then
Line:= Line + ' not null';
fmMain.ShowCompleteQueryWindow(FDBIndex, 'Add new field to Table: ' + FTableName,
'ALTER TABLE ' + FTableName +
' ADD ' + edFieldName.Text + ' ' + Line,
Clk);
end
else // Update
begin
Line:= '';
// Check name change
if UpperCase(Trim(edFieldName.Text)) <> OldFieldName then
Line:= 'ALTER TABLE ' + FTableName + ' ALTER ' + OldFieldName + ' TO ' +
edFieldName.Text + ';' + LineEnding;
// Check type/size/scale change
if (cbType.Text <> OldFieldType) or
(seSize.Value <> OldFieldSize) or
(seScale.Value <> OldFieldScale) then
begin
Line:= Line + 'ALTER TABLE ' + FTableName +
' ALTER ' + UpperCase(Trim(edFieldName.Text)) +
' TYPE ' + cbType.Text;
if (cbType.Text='NUMERIC') or
(cbType.Text='DECIMAL') then
Line:= Line + '(' + IntToStr(seSize.Value) + ',' +
IntToStr(seScale.Value)+');' + LineEnding
else
if (cbType.Text='CHAR') or
(cbType.Text='CSTRING') or
(cbType.Text='VARCHAR') then
Line:= Line + '(' + IntToStr(seSize.Value) + ');' + LineEnding;
end;
//Character set and collation
Line:= Line + '-- warning: character set changed for field ' + edFieldName.Text + '. Please do this manually (e.g. with the fbclone tool)' + LineEnding;
Line:= Line + '-- warning: collation changed for field ' + edFieldName.Text + '. Please do this manually (e.g. with the fbclone tool)' + LineEnding;
// Field Order
if seOrder.Value <> OldOrder then
begin
Line:= Line + 'ALTER TABLE ' + FTableName + ' ALTER ' + edFieldName.Text + ' POSITION ' +
IntToStr(seOrder.Value) + ';' + LineEnding;
end;
// Allow Null
if cxAllowNull.Checked <> OldAllowNull then
begin
if cxAllowNull.Checked then
NullFlag:= 'NULL'
else
NullFlag:= '1';
Line:= Line + 'UPDATE RDB$RELATION_FIELDS SET RDB$NULL_FLAG = ' + NullFlag + LineEnding +
'WHERE RDB$FIELD_NAME = ' + QuotedStr(UpperCase(Trim(edFieldName.Text))) + ' ' +
'AND RDB$RELATION_NAME = ' + QuotedStr(FTableName) + LineEnding;
end;
// Description
if edDescription.Text <> OldDescription then
begin
Line:= Line + 'UPDATE RDB$RELATION_FIELDS ' +
'set RDB$DESCRIPTION = ' + QuotedStr(edDescription.Text) + ' ' +
'where RDB$FIELD_NAME = ' + QuotedStr(UpperCase(Trim(edFieldName.Text))) + ' ' +
'and RDB$RELATION_NAME = ' + QuotedStr(FTableName) + ';' + LineEnding;
end;
// Default value
if edDefault.Text <> OldDefault then
begin
Line:= Line + 'UPDATE RDB$RELATION_FIELDS set RDB$Default_Source = ''' + edDefault.Text +
''' where RDB$FIELD_NAME = ''' + UpperCase(Trim(edFieldName.Text)) +
''' and RDB$RELATION_NAME = ''' + FTableName + ''';' + LineEnding;
end;
if Line <> '' then
fmMain.ShowCompleteQueryWindow(FDBIndex, 'Edit field: ' + OldFieldName, Line, clk);
end;
Close;
end;
procedure TfmNewEditField.cbCharsetEditingDone(Sender: TObject);
var
Collations: TStringList;
begin
// Available collations depend on the chosen character set,
// so update that whenever user changes character set
Collations:= TStringList.Create;
try
GetCollations(cbCharSet.Text,Collations);
cbCollation.Items.Assign(Collations);
finally
Collations.Free;
end;
end;
procedure TfmNewEditField.cbTypeEditingDone(Sender: TObject);
begin
seSize.Value:= dmSysTables.GetDefaultTypeSize(FDBIndex, cbType.Text);
{todo: (low priority) allow/disallow gui elements when using domain datatypes.
Check what can be overridden (e.g. collate for text-type domain fields)}
// Allow character set, lblCollation for text type fields; otherwise disable
case cbType.Text of
'CHAR','CSTRING','VARCHAR':
begin
// Allow character set/lblCollation for text type fields
cbCharset.Enabled:= true;
cbCollation.Enabled:= true;
seScale.Enabled:= false;
end;
'DECIMAL','NUMERIC':
begin
// Allow scale for numeric, decimal
seScale.Enabled:= true;
cbCharset.Enabled:= false;
cbCollation.Enabled:= false;
end
else
begin
cbCharset.Enabled:= false;
cbCollation.Enabled:= false;
seScale.Enabled:= false;
end;
end;
end;
procedure TfmNewEditField.FormClose(Sender: TObject;
var CloseAction: TCloseAction);
begin
CloseAction:= caFree;
end;
procedure TfmNewEditField.FormCreate(Sender: TObject);
begin
// Load available character sets
// todo: (low priority) character sets should be retrieved from current database server
CbCharSet.Items.AddStrings(FBCharacterSets);
// Do not set a default value, but leave it empty, because specifying a charset
// is not mandatory - it should only be done if the charset differs from the
// db charset
end;
procedure TfmNewEditField.Init(dbIndex: Integer; TableName: string;
FormMode: TFormMode;
FieldName, FieldType,
CharacterSet, Collation,
DefaultValue, Description: string;
Size, Scale, Order: integer;
AllowNull: Boolean;
RefreshButton: TBitBtn);
begin
cbType.Clear;
// Load basic datatypes for fields into combobox....
dmSysTables.GetBasicTypes(cbType.Items);
// ... add domain types for fields
dmSysTables.GetDomainTypes(dbIndex, cbType.Items);
FDBIndex:= dbIndex;
FTableName:= TableName;
fFormMode:= FormMode;
FRefreshButton:= RefreshButton;
OldFieldName:= FieldName;
OldFieldSize:= Size;
OldFieldScale:= Scale;
OldFieldType:= FieldType;
OldAllowNull:= AllowNull;
OldOrder:= Order;
OldDefault:= DefaultValue;
OldCharacterSet:= CharacterSet;
OldCollation:= Collation;
OldDescription:= Description;
edFieldName.Text:= OldFieldName;
seSize.Value:= OldFieldSize;
cbType.Text:= OldFieldType;
cxAllowNull.Checked:= OldAllowNull;
seOrder.Value:= OldOrder;
edDefault.Text:= OldDefault;
edDescription.Text:= OldDescription;
if FormMode = foEdit then
begin
bbAdd.Caption:= 'Update';
Caption:= 'Edit field: ' + FieldName + ' on : ' + TableName;
end
else
begin
bbAdd.Caption:= 'Add';
Caption:= 'Add new field in : ' + TableName;
end;
end;
initialization
{$I neweditfield.lrs}
end.