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 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,
|
35 |
ExtCtrls, StdCtrls, ComCtrls, IBDatabase, IBCustomDataSet, IBSystemTables;
|
36 |
|
37 |
type
|
38 |
|
39 |
{ TGeneratorEditor }
|
40 |
|
41 |
TGeneratorEditor = class(TForm)
|
42 |
Bevel1: TBevel;
|
43 |
Button1: TButton;
|
44 |
Button2: TButton;
|
45 |
GeneratorNames: TComboBox;
|
46 |
FieldNames: TComboBox;
|
47 |
IBTransaction1: TIBTransaction;
|
48 |
IncrementBy: TEdit;
|
49 |
Label1: TLabel;
|
50 |
Label2: TLabel;
|
51 |
Label3: TLabel;
|
52 |
OnNewRecord: TRadioButton;
|
53 |
OnPost: TRadioButton;
|
54 |
UpDown1: TUpDown;
|
55 |
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
|
56 |
procedure FormShow(Sender: TObject);
|
57 |
private
|
58 |
FGenerator: TIBGenerator;
|
59 |
FTableName: string;
|
60 |
FIBSystemTables: TIBSystemTables;
|
61 |
{ private declarations }
|
62 |
procedure LoadGenerators;
|
63 |
procedure LoadFieldNames;
|
64 |
function GetPrimaryKey: string;
|
65 |
procedure SetGenerator(const AValue: TIBGenerator);
|
66 |
procedure SetDatabase(ADatabase: TIBDatabase; ATransaction: TIBTransaction);
|
67 |
public
|
68 |
{ public declarations }
|
69 |
constructor Create(TheOwner: TComponent); override;
|
70 |
destructor Destroy; override;
|
71 |
property Generator: TIBGenerator read FGenerator write SetGenerator;
|
72 |
end;
|
73 |
|
74 |
function EditGenerator(AGenerator: TIBGenerator): boolean;
|
75 |
|
76 |
implementation
|
77 |
|
78 |
uses IBQuery;
|
79 |
|
80 |
{$R *.lfm}
|
81 |
|
82 |
function EditGenerator(AGenerator: TIBGenerator): boolean;
|
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
|
88 |
begin
|
89 |
ShowMessage('No Select SQL Found!');
|
90 |
Exit
|
91 |
end;
|
92 |
Database := AGenerator.Owner.Database;
|
93 |
|
94 |
if assigned(Database) then
|
95 |
try
|
96 |
Database.Connected := true;
|
97 |
except on E: Exception do
|
98 |
ShowMessage(E.Message)
|
99 |
end;
|
100 |
|
101 |
with TGeneratorEditor.Create(Application) do
|
102 |
try
|
103 |
Generator := AGenerator;
|
104 |
Result := ShowModal = mrOK
|
105 |
finally
|
106 |
Free
|
107 |
end;
|
108 |
end;
|
109 |
|
110 |
{ TGeneratorEditor }
|
111 |
|
112 |
procedure TGeneratorEditor.FormShow(Sender: TObject);
|
113 |
begin
|
114 |
LoadGenerators;
|
115 |
LoadFieldNames;
|
116 |
if Generator.Generator <> '' then
|
117 |
GeneratorNames.ItemIndex := GeneratorNames.Items.IndexOf(Generator.Generator);
|
118 |
if Generator.Field <> '' then
|
119 |
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;
|
125 |
|
126 |
if Generator.ApplyOnEvent = gaeOnNewRecord then
|
127 |
OnNewRecord.Checked := true
|
128 |
else
|
129 |
OnPost.Checked := true;
|
130 |
IncrementBy.Text := IntToStr(Generator.Increment);
|
131 |
end;
|
132 |
|
133 |
procedure TGeneratorEditor.FormClose(Sender: TObject;
|
134 |
var CloseAction: TCloseAction);
|
135 |
begin
|
136 |
if ModalResult = mrOK then
|
137 |
begin
|
138 |
Generator.Generator := GeneratorNames.Text;
|
139 |
Generator.Field := FieldNames.Text;
|
140 |
if OnNewRecord.Checked then
|
141 |
Generator.ApplyOnEvent := gaeOnNewRecord
|
142 |
else
|
143 |
Generator.ApplyOnEvent := gaeOnPostRecord;
|
144 |
Generator.Increment := StrToInt(IncrementBy.Text)
|
145 |
|
146 |
end;
|
147 |
end;
|
148 |
|
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 |
|
181 |
procedure TGeneratorEditor.SetGenerator(const AValue: TIBGenerator);
|
182 |
begin
|
183 |
FGenerator := AValue;
|
184 |
IBTransaction1.DefaultDatabase := Generator.Owner.Database;
|
185 |
SetDatabase(Generator.Owner.Database,IBTransaction1);
|
186 |
end;
|
187 |
|
188 |
procedure TGeneratorEditor.SetDatabase(ADatabase: TIBDatabase; ATransaction: TIBTransaction);
|
189 |
begin
|
190 |
if not assigned(ADatabase) then
|
191 |
raise Exception.Create('A Database must be assigned');
|
192 |
if not assigned(ATransaction) then
|
193 |
raise Exception.Create('A Transaction must be assigned');
|
194 |
FIBSystemTables.SelectDatabase( ADatabase,ATransaction)
|
195 |
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;
|
207 |
end;
|
208 |
|
209 |
end.
|
210 |
|