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, IBTable; |
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 |
|
|
83 |
|
var Database: TIBDatabase; |
84 |
|
begin |
85 |
|
Result := false; |
86 |
< |
if (AGenerator.Owner is TIBQuery and ((AGenerator.Owner as TIBQuery).SQL.Text = '')) or |
87 |
< |
(AGenerator.Owner is TIBDataSet and ((AGenerator.Owner as TIBDataSet).SelectSQL.Text = '')) then |
86 |
> |
if not (AGenerator.Owner is TIBTable) and |
87 |
> |
(((AGenerator.Owner is TIBQuery) and ((AGenerator.Owner as TIBQuery).SQL.Text = '')) or |
88 |
> |
((AGenerator.Owner is TIBDataSet) and ((AGenerator.Owner as TIBDataSet).SelectSQL.Text = ''))) then |
89 |
|
begin |
90 |
|
ShowMessage('No Select SQL Found!'); |
91 |
|
Exit |
112 |
|
|
113 |
|
procedure TGeneratorEditor.FormShow(Sender: TObject); |
114 |
|
begin |
115 |
< |
LoadGenerators; |
116 |
< |
LoadFieldNames; |
115 |
> |
if (PrimaryKeys.Database = nil) or not PrimaryKeys.Database.Connected then Exit; |
116 |
> |
SQLTransaction.Active := true; |
117 |
> |
PrimaryKeys.Active := true; |
118 |
> |
GeneratorQuery.Active := true; |
119 |
|
if Generator.Generator <> '' then |
120 |
< |
GeneratorNames.ItemIndex := GeneratorNames.Items.IndexOf(Generator.Generator); |
120 |
> |
GeneratorQuery.Locate('RDB$GENERATOR_NAME',Generator.Generator,[]); |
121 |
|
if Generator.Field <> '' then |
122 |
< |
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; |
122 |
> |
PrimaryKeys.Locate('ColumnName',UpperCase(Generator.Field),[]); |
123 |
|
|
124 |
|
if Generator.ApplyOnEvent = gaeOnNewRecord then |
125 |
|
OnNewRecord.Checked := true |
128 |
|
IncrementBy.Text := IntToStr(Generator.Increment); |
129 |
|
end; |
130 |
|
|
131 |
+ |
procedure TGeneratorEditor.PrimaryKeysBeforeOpen(DataSet: TDataSet); |
132 |
+ |
begin |
133 |
+ |
PrimaryKeys.ParamByName('RDB$RELATION_NAME').AsString := GetTableName; |
134 |
+ |
end; |
135 |
+ |
|
136 |
+ |
function TGeneratorEditor.GetTableName: string; |
137 |
+ |
begin |
138 |
+ |
Result := ''; |
139 |
+ |
with IdentifyStatementSQL do |
140 |
+ |
begin |
141 |
+ |
Transaction.Active := true; |
142 |
+ |
if FGenerator.Owner is TIBTable then |
143 |
+ |
begin |
144 |
+ |
Result := TIBTable(FGenerator.Owner).TableName; |
145 |
+ |
Exit; |
146 |
+ |
end; |
147 |
+ |
if FGenerator.Owner is TIBQuery then |
148 |
+ |
SQL.Assign((FGenerator.Owner as TIBQuery).SQL) |
149 |
+ |
else |
150 |
+ |
SQL.Assign((FGenerator.Owner as TIBDataset).SelectSQL); |
151 |
+ |
try |
152 |
+ |
Prepare; |
153 |
+ |
if (SQLStatementType = SQLSelect) and (MetaData.Count > 0) then |
154 |
+ |
Result := MetaData[0].GetRelationName; |
155 |
+ |
except on E:EIBError do |
156 |
+ |
// ShowMessage(E.Message); |
157 |
+ |
end; |
158 |
+ |
end; |
159 |
+ |
end; |
160 |
+ |
|
161 |
|
procedure TGeneratorEditor.FormClose(Sender: TObject; |
162 |
|
var CloseAction: TCloseAction); |
163 |
|
begin |
174 |
|
end; |
175 |
|
end; |
176 |
|
|
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 |
– |
|
177 |
|
procedure TGeneratorEditor.SetGenerator(const AValue: TIBGenerator); |
178 |
|
begin |
179 |
|
FGenerator := AValue; |
180 |
< |
IBTransaction1.DefaultDatabase := Generator.Owner.Database; |
185 |
< |
SetDatabase(Generator.Owner.Database,IBTransaction1); |
180 |
> |
SetDatabase(Generator.Owner.Database); |
181 |
|
end; |
182 |
|
|
183 |
< |
procedure TGeneratorEditor.SetDatabase(ADatabase: TIBDatabase; ATransaction: TIBTransaction); |
183 |
> |
procedure TGeneratorEditor.SetDatabase(aDatabase: TIBDatabase); |
184 |
|
begin |
185 |
|
if not assigned(ADatabase) then |
186 |
|
raise Exception.Create('A Database must be assigned'); |
187 |
< |
if not assigned(ATransaction) then |
188 |
< |
raise Exception.Create('A Transaction must be assigned'); |
189 |
< |
FIBSystemTables.SelectDatabase( ADatabase,ATransaction) |
190 |
< |
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; |
187 |
> |
PrimaryKeys.Database := aDatabase; |
188 |
> |
GeneratorQuery.Database := aDatabase; |
189 |
> |
IdentifyStatementSQL.Database := aDatabase; |
190 |
> |
SQLTransaction.DefaultDatabase := aDatabase; |
191 |
|
end; |
192 |
|
|
193 |
|
end. |