1 |
(*
|
2 |
* IBX For Lazarus (Firebird Express)
|
3 |
*
|
4 |
* The contents of this file are subject to the Initial Developer's
|
5 |
* Public License Version 1.0 (the "License"); you may not use this
|
6 |
* file except in compliance with the License. You may obtain a copy
|
7 |
* of the License here:
|
8 |
*
|
9 |
* http://www.firebirdsql.org/index.php?op=doc&id=idpl
|
10 |
*
|
11 |
* Software distributed under the License is distributed on an "AS
|
12 |
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
13 |
* implied. See the License for the specific language governing rights
|
14 |
* and limitations under the License.
|
15 |
*
|
16 |
* The Initial Developer of the Original Code is Tony Whyman.
|
17 |
*
|
18 |
* The Original Code is (C) 2011-17 Tony Whyman, MWA Software
|
19 |
* (http://www.mwasoftware.co.uk).
|
20 |
*
|
21 |
* All Rights Reserved.
|
22 |
*
|
23 |
* Contributor(s): ______________________________________.
|
24 |
*
|
25 |
*)
|
26 |
|
27 |
unit IBGeneratorEditor;
|
28 |
|
29 |
{$mode objfpc}{$H+}
|
30 |
|
31 |
interface
|
32 |
|
33 |
uses
|
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 |
|
40 |
{ TGeneratorEditor }
|
41 |
|
42 |
TGeneratorEditor = class(TForm)
|
43 |
Bevel1: TBevel;
|
44 |
Button1: TButton;
|
45 |
Button2: TButton;
|
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;
|
66 |
{ private declarations }
|
67 |
function GetTableName: string;
|
68 |
procedure SetGenerator(const AValue: TIBGenerator);
|
69 |
procedure SetDatabase(aDatabase: TIBDatabase);
|
70 |
public
|
71 |
{ public declarations }
|
72 |
property Generator: TIBGenerator read FGenerator write SetGenerator;
|
73 |
end;
|
74 |
|
75 |
function EditGenerator(AGenerator: TIBGenerator): boolean;
|
76 |
|
77 |
implementation
|
78 |
|
79 |
|
80 |
{$R *.lfm}
|
81 |
|
82 |
function EditGenerator(AGenerator: TIBGenerator): boolean;
|
83 |
var Database: TIBDatabase;
|
84 |
begin
|
85 |
Result := false;
|
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
|
92 |
end;
|
93 |
Database := AGenerator.Owner.Database;
|
94 |
|
95 |
if assigned(Database) then
|
96 |
try
|
97 |
Database.Connected := true;
|
98 |
except on E: Exception do
|
99 |
ShowMessage(E.Message)
|
100 |
end;
|
101 |
|
102 |
with TGeneratorEditor.Create(Application) do
|
103 |
try
|
104 |
Generator := AGenerator;
|
105 |
Result := ShowModal = mrOK
|
106 |
finally
|
107 |
Free
|
108 |
end;
|
109 |
end;
|
110 |
|
111 |
{ TGeneratorEditor }
|
112 |
|
113 |
procedure TGeneratorEditor.FormShow(Sender: TObject);
|
114 |
begin
|
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 |
GeneratorQuery.Locate('RDB$GENERATOR_NAME',Generator.Generator,[]);
|
121 |
if Generator.Field <> '' then
|
122 |
PrimaryKeys.Locate('ColumnName',UpperCase(Generator.Field),[]);
|
123 |
|
124 |
if Generator.ApplyOnEvent = gaeOnNewRecord then
|
125 |
OnNewRecord.Checked := true
|
126 |
else
|
127 |
OnPost.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
|
164 |
if ModalResult = mrOK then
|
165 |
begin
|
166 |
Generator.Generator := GeneratorNames.Text;
|
167 |
Generator.Field := FieldNames.Text;
|
168 |
if OnNewRecord.Checked then
|
169 |
Generator.ApplyOnEvent := gaeOnNewRecord
|
170 |
else
|
171 |
Generator.ApplyOnEvent := gaeOnPostRecord;
|
172 |
Generator.Increment := StrToInt(IncrementBy.Text)
|
173 |
|
174 |
end;
|
175 |
end;
|
176 |
|
177 |
procedure TGeneratorEditor.SetGenerator(const AValue: TIBGenerator);
|
178 |
begin
|
179 |
FGenerator := AValue;
|
180 |
SetDatabase(Generator.Owner.Database);
|
181 |
end;
|
182 |
|
183 |
procedure TGeneratorEditor.SetDatabase(aDatabase: TIBDatabase);
|
184 |
begin
|
185 |
if not assigned(ADatabase) then
|
186 |
raise Exception.Create('A Database must be assigned');
|
187 |
PrimaryKeys.Database := aDatabase;
|
188 |
GeneratorQuery.Database := aDatabase;
|
189 |
IdentifyStatementSQL.Database := aDatabase;
|
190 |
SQLTransaction.DefaultDatabase := aDatabase;
|
191 |
end;
|
192 |
|
193 |
end.
|
194 |
|