15 |
|
* |
16 |
|
* The Initial Developer of the Original Code is Tony Whyman. |
17 |
|
* |
18 |
< |
* The Original Code is (C) 2011 Tony Whyman, MWA Software |
18 |
> |
* The Original Code is (C) 2011-17 Tony Whyman, MWA Software |
19 |
|
* (http://www.mwasoftware.co.uk). |
20 |
|
* |
21 |
|
* All Rights Reserved. |
31 |
|
interface |
32 |
|
|
33 |
|
uses |
34 |
< |
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, |
35 |
< |
ExtCtrls, StdCtrls, ComCtrls, IBDatabase, IBCustomDataSet, IBSystemTables; |
34 |
> |
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls, |
35 |
> |
StdCtrls, ComCtrls, db, IBDatabase, IBCustomDataSet, IBQuery, IBSQL, |
36 |
> |
IBLookupComboEditBox, IB; |
37 |
|
|
38 |
|
type |
39 |
|
|
43 |
|
Bevel1: TBevel; |
44 |
|
Button1: TButton; |
45 |
|
Button2: TButton; |
46 |
< |
GeneratorNames: TComboBox; |
47 |
< |
FieldNames: TComboBox; |
48 |
< |
IBTransaction1: TIBTransaction; |
46 |
> |
GeneratorSource: TDataSource; |
47 |
> |
GeneratorQuery: TIBQuery; |
48 |
> |
GeneratorNames: TIBLookupComboEditBox; |
49 |
> |
FieldNames: TIBLookupComboEditBox; |
50 |
> |
IdentifyStatementSQL: TIBSQL; |
51 |
|
IncrementBy: TEdit; |
52 |
|
Label1: TLabel; |
53 |
|
Label2: TLabel; |
54 |
|
Label3: TLabel; |
55 |
|
OnNewRecord: TRadioButton; |
56 |
|
OnPost: TRadioButton; |
57 |
+ |
PrimaryKeys: TIBQuery; |
58 |
+ |
PrimaryKeySource: TDataSource; |
59 |
+ |
SQLTransaction: TIBTransaction; |
60 |
|
UpDown1: TUpDown; |
61 |
|
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); |
62 |
|
procedure FormShow(Sender: TObject); |
63 |
+ |
procedure PrimaryKeysBeforeOpen(DataSet: TDataSet); |
64 |
|
private |
65 |
|
FGenerator: TIBGenerator; |
59 |
– |
FTableName: string; |
60 |
– |
FIBSystemTables: TIBSystemTables; |
66 |
|
{ private declarations } |
67 |
< |
procedure LoadGenerators; |
63 |
< |
procedure LoadFieldNames; |
64 |
< |
function GetPrimaryKey: string; |
67 |
> |
function GetTableName: string; |
68 |
|
procedure SetGenerator(const AValue: TIBGenerator); |
69 |
< |
procedure SetDatabase(ADatabase: TIBDatabase; ATransaction: TIBTransaction); |
69 |
> |
procedure SetDatabase(aDatabase: TIBDatabase); |
70 |
|
public |
71 |
|
{ public declarations } |
69 |
– |
constructor Create(TheOwner: TComponent); override; |
70 |
– |
destructor Destroy; override; |
72 |
|
property Generator: TIBGenerator read FGenerator write SetGenerator; |
73 |
|
end; |
74 |
|
|
76 |
|
|
77 |
|
implementation |
78 |
|
|
78 |
– |
uses IBQuery; |
79 |
|
|
80 |
|
{$R *.lfm} |
81 |
|
|
111 |
|
|
112 |
|
procedure TGeneratorEditor.FormShow(Sender: TObject); |
113 |
|
begin |
114 |
< |
LoadGenerators; |
115 |
< |
LoadFieldNames; |
114 |
> |
if (PrimaryKeys.Database = nil) or not PrimaryKeys.Database.Connected then Exit; |
115 |
> |
SQLTransaction.Active := true; |
116 |
> |
PrimaryKeys.Active := true; |
117 |
> |
GeneratorQuery.Active := true; |
118 |
|
if Generator.Generator <> '' then |
119 |
< |
GeneratorNames.ItemIndex := GeneratorNames.Items.IndexOf(Generator.Generator); |
119 |
> |
GeneratorQuery.Locate('RDB$GENERATOR_NAME',Generator.Generator,[]); |
120 |
|
if Generator.Field <> '' then |
121 |
< |
FieldNames.ItemIndex := FieldNames.Items.IndexOf(UpperCase(Generator.Field)) |
120 |
< |
else |
121 |
< |
FieldNames.ItemIndex := FieldNames.Items.IndexOf(GetPrimaryKey); |
122 |
< |
|
123 |
< |
if FieldNames.ItemIndex = -1 then |
124 |
< |
FieldNames.Text := Generator.Field; |
121 |
> |
PrimaryKeys.Locate('ColumnName',UpperCase(Generator.Field),[]); |
122 |
|
|
123 |
|
if Generator.ApplyOnEvent = gaeOnNewRecord then |
124 |
|
OnNewRecord.Checked := true |
127 |
|
IncrementBy.Text := IntToStr(Generator.Increment); |
128 |
|
end; |
129 |
|
|
130 |
+ |
procedure TGeneratorEditor.PrimaryKeysBeforeOpen(DataSet: TDataSet); |
131 |
+ |
begin |
132 |
+ |
PrimaryKeys.ParamByName('RDB$RELATION_NAME').AsString := GetTableName; |
133 |
+ |
end; |
134 |
+ |
|
135 |
+ |
function TGeneratorEditor.GetTableName: string; |
136 |
+ |
begin |
137 |
+ |
Result := ''; |
138 |
+ |
with IdentifyStatementSQL do |
139 |
+ |
begin |
140 |
+ |
Transaction.Active := true; |
141 |
+ |
if FGenerator.Owner is TIBQuery then |
142 |
+ |
SQL.Assign((FGenerator.Owner as TIBQuery).SQL) |
143 |
+ |
else |
144 |
+ |
SQL.Assign((FGenerator.Owner as TIBDataset).SelectSQL); |
145 |
+ |
try |
146 |
+ |
Prepare; |
147 |
+ |
if (SQLStatementType = SQLSelect) and (MetaData.Count > 0) then |
148 |
+ |
Result := MetaData[0].GetRelationName; |
149 |
+ |
except on E:EIBError do |
150 |
+ |
// ShowMessage(E.Message); |
151 |
+ |
end; |
152 |
+ |
end; |
153 |
+ |
end; |
154 |
+ |
|
155 |
|
procedure TGeneratorEditor.FormClose(Sender: TObject; |
156 |
|
var CloseAction: TCloseAction); |
157 |
|
begin |
168 |
|
end; |
169 |
|
end; |
170 |
|
|
149 |
– |
procedure TGeneratorEditor.LoadGenerators; |
150 |
– |
begin |
151 |
– |
FIBSystemTables.GetGenerators(GeneratorNames.Items); |
152 |
– |
if GeneratorNames.Items.Count > 0 then |
153 |
– |
GeneratorNames.ItemIndex := 0 |
154 |
– |
end; |
155 |
– |
|
156 |
– |
procedure TGeneratorEditor.LoadFieldNames; |
157 |
– |
begin |
158 |
– |
if FGenerator.Owner is TIBDataSet then |
159 |
– |
FIBSystemTables.GetTableAndColumns((FGenerator.Owner as TIBDataSet).SelectSQL.Text,FTableName,FieldNames.Items) |
160 |
– |
else |
161 |
– |
if FGenerator.Owner is TIBQuery then |
162 |
– |
FIBSystemTables.GetTableAndColumns((FGenerator.Owner as TIBQuery).SQL.Text,FTableName,FieldNames.Items) |
163 |
– |
else |
164 |
– |
raise Exception.CreateFmt('Don''t know how to edit a %s',[FGenerator.Owner.ClassName]) |
165 |
– |
end; |
166 |
– |
|
167 |
– |
function TGeneratorEditor.GetPrimaryKey: string; |
168 |
– |
var Keys: TStringList; |
169 |
– |
begin |
170 |
– |
Result := ''; |
171 |
– |
Keys := TStringList.Create; |
172 |
– |
try |
173 |
– |
FIBSystemTables.GetPrimaryKeys(FTableName,Keys); |
174 |
– |
if Keys.Count > 0 then |
175 |
– |
Result := Keys[0]; |
176 |
– |
finally |
177 |
– |
Keys.Free |
178 |
– |
end; |
179 |
– |
end; |
180 |
– |
|
171 |
|
procedure TGeneratorEditor.SetGenerator(const AValue: TIBGenerator); |
172 |
|
begin |
173 |
|
FGenerator := AValue; |
174 |
< |
IBTransaction1.DefaultDatabase := Generator.Owner.Database; |
185 |
< |
SetDatabase(Generator.Owner.Database,IBTransaction1); |
174 |
> |
SetDatabase(Generator.Owner.Database); |
175 |
|
end; |
176 |
|
|
177 |
< |
procedure TGeneratorEditor.SetDatabase(ADatabase: TIBDatabase; ATransaction: TIBTransaction); |
177 |
> |
procedure TGeneratorEditor.SetDatabase(aDatabase: TIBDatabase); |
178 |
|
begin |
179 |
|
if not assigned(ADatabase) then |
180 |
|
raise Exception.Create('A Database must be assigned'); |
181 |
< |
if not assigned(ATransaction) then |
182 |
< |
raise Exception.Create('A Transaction must be assigned'); |
183 |
< |
FIBSystemTables.SelectDatabase( ADatabase,ATransaction) |
184 |
< |
end; |
196 |
< |
|
197 |
< |
constructor TGeneratorEditor.Create(TheOwner: TComponent); |
198 |
< |
begin |
199 |
< |
inherited Create(TheOwner); |
200 |
< |
FIBSystemTables := TIBSystemTables.Create |
201 |
< |
end; |
202 |
< |
|
203 |
< |
destructor TGeneratorEditor.Destroy; |
204 |
< |
begin |
205 |
< |
if assigned(FIBSystemTables) then FIBSystemTables.Free; |
206 |
< |
inherited Destroy; |
181 |
> |
PrimaryKeys.Database := aDatabase; |
182 |
> |
GeneratorQuery.Database := aDatabase; |
183 |
> |
IdentifyStatementSQL.Database := aDatabase; |
184 |
> |
SQLTransaction.DefaultDatabase := aDatabase; |
185 |
|
end; |
186 |
|
|
187 |
|
end. |