ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/design/ibgeneratoreditor.pas
Revision: 80
Committed: Mon Jan 1 11:31:07 2018 UTC (6 years, 10 months ago) by tony
Content type: text/x-pascal
File size: 5116 byte(s)
Log Message:
Fixes merged into public release

File Contents

# User Rev Content
1 tony 33 (*
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 tony 80 * The Original Code is (C) 2011-17 Tony Whyman, MWA Software
19 tony 33 * (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 tony 80 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
35     StdCtrls, ComCtrls, db, IBDatabase, IBCustomDataSet, IBQuery, IBSQL,
36     IBLookupComboEditBox, IB;
37 tony 33
38     type
39    
40     { TGeneratorEditor }
41    
42     TGeneratorEditor = class(TForm)
43     Bevel1: TBevel;
44     Button1: TButton;
45     Button2: TButton;
46 tony 80 GeneratorSource: TDataSource;
47     GeneratorQuery: TIBQuery;
48     GeneratorNames: TIBLookupComboEditBox;
49     FieldNames: TIBLookupComboEditBox;
50     IdentifyStatementSQL: TIBSQL;
51 tony 33 IncrementBy: TEdit;
52     Label1: TLabel;
53     Label2: TLabel;
54     Label3: TLabel;
55     OnNewRecord: TRadioButton;
56     OnPost: TRadioButton;
57 tony 80 PrimaryKeys: TIBQuery;
58     PrimaryKeySource: TDataSource;
59     SQLTransaction: TIBTransaction;
60 tony 33 UpDown1: TUpDown;
61     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
62     procedure FormShow(Sender: TObject);
63 tony 80 procedure PrimaryKeysBeforeOpen(DataSet: TDataSet);
64 tony 33 private
65     FGenerator: TIBGenerator;
66     { private declarations }
67 tony 80 function GetTableName: string;
68 tony 33 procedure SetGenerator(const AValue: TIBGenerator);
69 tony 80 procedure SetDatabase(aDatabase: TIBDatabase);
70 tony 33 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 (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 tony 80 SQLTransaction.Active := true;
115     PrimaryKeys.Active := true;
116     GeneratorQuery.Active := true;
117 tony 33 if Generator.Generator <> '' then
118 tony 80 GeneratorQuery.Locate('RDB$GENERATOR_NAME',Generator.Generator,[]);
119 tony 33 if Generator.Field <> '' then
120 tony 80 PrimaryKeys.Locate('ColumnName',UpperCase(Generator.Field),[]);
121 tony 33
122     if Generator.ApplyOnEvent = gaeOnNewRecord then
123     OnNewRecord.Checked := true
124     else
125     OnPost.Checked := true;
126     IncrementBy.Text := IntToStr(Generator.Increment);
127     end;
128    
129 tony 80 procedure TGeneratorEditor.PrimaryKeysBeforeOpen(DataSet: TDataSet);
130     begin
131     PrimaryKeys.ParamByName('RDB$RELATION_NAME').AsString := GetTableName;
132     end;
133    
134     function TGeneratorEditor.GetTableName: string;
135     begin
136     Result := '';
137     with IdentifyStatementSQL do
138     begin
139     Transaction.Active := true;
140     if FGenerator.Owner is TIBQuery then
141     SQL.Assign((FGenerator.Owner as TIBQuery).SQL)
142     else
143     SQL.Assign((FGenerator.Owner as TIBDataset).SelectSQL);
144     try
145     Prepare;
146     if (SQLStatementType = SQLSelect) and (MetaData.Count > 0) then
147     Result := MetaData[0].GetRelationName;
148     except on E:EIBError do
149     // ShowMessage(E.Message);
150     end;
151     end;
152     end;
153    
154 tony 33 procedure TGeneratorEditor.FormClose(Sender: TObject;
155     var CloseAction: TCloseAction);
156     begin
157     if ModalResult = mrOK then
158     begin
159     Generator.Generator := GeneratorNames.Text;
160     Generator.Field := FieldNames.Text;
161     if OnNewRecord.Checked then
162     Generator.ApplyOnEvent := gaeOnNewRecord
163     else
164     Generator.ApplyOnEvent := gaeOnPostRecord;
165     Generator.Increment := StrToInt(IncrementBy.Text)
166    
167     end;
168     end;
169    
170     procedure TGeneratorEditor.SetGenerator(const AValue: TIBGenerator);
171     begin
172     FGenerator := AValue;
173 tony 80 SetDatabase(Generator.Owner.Database);
174 tony 33 end;
175    
176 tony 80 procedure TGeneratorEditor.SetDatabase(aDatabase: TIBDatabase);
177 tony 33 begin
178     if not assigned(ADatabase) then
179     raise Exception.Create('A Database must be assigned');
180 tony 80 PrimaryKeys.Database := aDatabase;
181     GeneratorQuery.Database := aDatabase;
182     IdentifyStatementSQL.Database := aDatabase;
183     SQLTransaction.DefaultDatabase := aDatabase;
184 tony 33 end;
185    
186     end.
187