ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/design/ibgeneratoreditor.pas
Revision: 81
Committed: Mon Jan 1 11:31:10 2018 UTC (6 years, 4 months ago) by tony
Content type: text/x-pascal
File size: 5199 byte(s)
Log Message:
Protect missing database name error in property editor

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 81 if (PrimaryKeys.Database = nil) or not PrimaryKeys.Database.Connected then Exit;
115 tony 80 SQLTransaction.Active := true;
116     PrimaryKeys.Active := true;
117     GeneratorQuery.Active := true;
118 tony 33 if Generator.Generator <> '' then
119 tony 80 GeneratorQuery.Locate('RDB$GENERATOR_NAME',Generator.Generator,[]);
120 tony 33 if Generator.Field <> '' then
121 tony 80 PrimaryKeys.Locate('ColumnName',UpperCase(Generator.Field),[]);
122 tony 33
123     if Generator.ApplyOnEvent = gaeOnNewRecord then
124     OnNewRecord.Checked := true
125     else
126     OnPost.Checked := true;
127     IncrementBy.Text := IntToStr(Generator.Increment);
128     end;
129    
130 tony 80 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 tony 33 procedure TGeneratorEditor.FormClose(Sender: TObject;
156     var CloseAction: TCloseAction);
157     begin
158     if ModalResult = mrOK then
159     begin
160     Generator.Generator := GeneratorNames.Text;
161     Generator.Field := FieldNames.Text;
162     if OnNewRecord.Checked then
163     Generator.ApplyOnEvent := gaeOnNewRecord
164     else
165     Generator.ApplyOnEvent := gaeOnPostRecord;
166     Generator.Increment := StrToInt(IncrementBy.Text)
167    
168     end;
169     end;
170    
171     procedure TGeneratorEditor.SetGenerator(const AValue: TIBGenerator);
172     begin
173     FGenerator := AValue;
174 tony 80 SetDatabase(Generator.Owner.Database);
175 tony 33 end;
176    
177 tony 80 procedure TGeneratorEditor.SetDatabase(aDatabase: TIBDatabase);
178 tony 33 begin
179     if not assigned(ADatabase) then
180     raise Exception.Create('A Database must be assigned');
181 tony 80 PrimaryKeys.Database := aDatabase;
182     GeneratorQuery.Database := aDatabase;
183     IdentifyStatementSQL.Database := aDatabase;
184     SQLTransaction.DefaultDatabase := aDatabase;
185 tony 33 end;
186    
187     end.
188