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 ibselectsqleditor; |
28 |
|
29 |
{$mode objfpc}{$H+} |
30 |
|
31 |
interface |
32 |
|
33 |
uses |
34 |
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, |
35 |
StdCtrls, ComCtrls, IBSystemTables, IBDatabase, IBCustomDataSet; |
36 |
|
37 |
type |
38 |
|
39 |
{ TIBSelectSQLEditorForm } |
40 |
|
41 |
TIBSelectSQLEditorForm = class(TForm) |
42 |
Button1: TButton; |
43 |
Button2: TButton; |
44 |
GenerateBtn: TButton; |
45 |
GenerateParams: TCheckBox; |
46 |
SelectProcedure: TLabel; |
47 |
TestBtn: TButton; |
48 |
FieldList: TListBox; |
49 |
IBTransaction1: TIBTransaction; |
50 |
Label1: TLabel; |
51 |
Label16: TLabel; |
52 |
Label17: TLabel; |
53 |
Label18: TLabel; |
54 |
Label2: TLabel; |
55 |
Label3: TLabel; |
56 |
Label4: TLabel; |
57 |
PageControl: TPageControl; |
58 |
PrimaryKeyList: TListBox; |
59 |
ProcedureNames: TComboBox; |
60 |
ProcInputList: TListBox; |
61 |
ProcOutputList: TListBox; |
62 |
QuoteFields: TCheckBox; |
63 |
SQLText: TMemo; |
64 |
TableNamesCombo: TComboBox; |
65 |
SelectPage: TTabSheet; |
66 |
ExecutePage: TTabSheet; |
67 |
procedure GenerateBtnClick(Sender: TObject); |
68 |
procedure TestBtnClick(Sender: TObject); |
69 |
procedure ExecutePageShow(Sender: TObject); |
70 |
procedure FieldListDblClick(Sender: TObject); |
71 |
procedure FormShow(Sender: TObject); |
72 |
procedure PrimaryKeyListDblClick(Sender: TObject); |
73 |
procedure ProcedureNamesCloseUp(Sender: TObject); |
74 |
procedure SelectPageShow(Sender: TObject); |
75 |
procedure TableNamesComboCloseUp(Sender: TObject); |
76 |
private |
77 |
{ private declarations } |
78 |
FIBSystemTables: TIBSystemTables; |
79 |
FExecuteOnly: boolean; |
80 |
public |
81 |
{ public declarations } |
82 |
constructor Create(TheOwner: TComponent); override; |
83 |
destructor Destroy; override; |
84 |
procedure SetDatabase(Database: TIBDatabase); |
85 |
end; |
86 |
|
87 |
|
88 |
function EditSQL(DataSet: TIBCustomDataSet; SelectSQL: TStrings): boolean; |
89 |
|
90 |
implementation |
91 |
|
92 |
uses IBSQL; |
93 |
|
94 |
{$R *.lfm} |
95 |
|
96 |
function EditSQL(DataSet: TIBCustomDataSet; SelectSQL: TStrings): boolean; |
97 |
begin |
98 |
Result := false; |
99 |
if assigned(DataSet) and assigned(DataSet.Database) then |
100 |
try |
101 |
DataSet.Database.Connected := true; |
102 |
except on E: Exception do |
103 |
ShowMessage(E.Message) |
104 |
end; |
105 |
|
106 |
with TIBSelectSQLEditorForm.Create(Application) do |
107 |
try |
108 |
if assigned(DataSet) then |
109 |
begin |
110 |
SetDatabase(DataSet.Database); |
111 |
GenerateParams.Checked := DataSet.GenerateParamNames; |
112 |
end; |
113 |
SQLText.Lines.Assign(SelectSQL); |
114 |
Result := ShowModal = mrOK; |
115 |
if Result then |
116 |
begin |
117 |
SelectSQL.Assign(SQLText.Lines); |
118 |
if assigned(DataSet) then |
119 |
DataSet.GenerateParamNames := GenerateParams.Checked |
120 |
end; |
121 |
finally |
122 |
Free |
123 |
end; |
124 |
end; |
125 |
|
126 |
{ TIBSelectSQLEditorForm } |
127 |
|
128 |
procedure TIBSelectSQLEditorForm.FormShow(Sender: TObject); |
129 |
var IsProcedureName: boolean; |
130 |
begin |
131 |
GenerateBtn.Enabled := (IBTransaction1.DefaultDatabase <> nil) and IBTransaction1.DefaultDatabase.Connected; |
132 |
TestBtn.Enabled := (IBTransaction1.DefaultDatabase <> nil) and IBTransaction1.DefaultDatabase.Connected; |
133 |
if Trim(SQLText.Text) <> '' then |
134 |
begin |
135 |
try |
136 |
FIBSystemTables.GetStatementType(SQLText.Text,IsProcedureName); |
137 |
except end; |
138 |
if IsProcedureName then |
139 |
PageControl.ActivePage := ExecutePage |
140 |
else |
141 |
PageControl.ActivePage := SelectPage; |
142 |
end |
143 |
else |
144 |
PageControl.ActivePage := SelectPage; |
145 |
end; |
146 |
|
147 |
procedure TIBSelectSQLEditorForm.PrimaryKeyListDblClick(Sender: TObject); |
148 |
begin |
149 |
SQLText.SelText := PrimaryKeyList.Items[PrimaryKeyList.ItemIndex]; |
150 |
SQLText.SetFocus |
151 |
end; |
152 |
|
153 |
procedure TIBSelectSQLEditorForm.ProcedureNamesCloseUp(Sender: TObject); |
154 |
begin |
155 |
FIBSystemTables.GetProcParams(ProcedureNames.Text,FExecuteOnly,ProcInputList.Items,ProcOutputList.Items); |
156 |
SelectProcedure.Visible := not FExecuteOnly |
157 |
end; |
158 |
|
159 |
procedure TIBSelectSQLEditorForm.SelectPageShow(Sender: TObject); |
160 |
var TableName: string; |
161 |
begin |
162 |
TableNamesCombo.Items.Clear; |
163 |
FIBSystemTables.GetTableNames(TableNamesCombo.Items); |
164 |
if TableNamesCombo.Items.Count > 0 then |
165 |
begin |
166 |
TableNamesCombo.ItemIndex := 0; |
167 |
if Trim(SQLText.Text) <> '' then |
168 |
begin |
169 |
FIBSystemTables.GetTableAndColumns(SQLText.Text,TableName,nil); |
170 |
TableNamesCombo.ItemIndex := TableNamesCombo.Items.IndexOf(TableName) |
171 |
end; |
172 |
FIBSystemTables.GetFieldNames(TableNamesCombo.Text,FieldList.Items); |
173 |
FIBSystemTables.GetPrimaryKeys(TableNamesCombo.Text,PrimaryKeyList.Items); |
174 |
end; |
175 |
end; |
176 |
|
177 |
procedure TIBSelectSQLEditorForm.FieldListDblClick(Sender: TObject); |
178 |
begin |
179 |
SQLText.SelText := FieldList.Items[FieldList.ItemIndex]; |
180 |
SQLText.SetFocus |
181 |
end; |
182 |
|
183 |
procedure TIBSelectSQLEditorForm.GenerateBtnClick(Sender: TObject); |
184 |
var FieldNames: TStrings; |
185 |
begin |
186 |
if PageControl.ActivePage = ExecutePage then |
187 |
FIBSystemTables.GenerateExecuteSQL(ProcedureNames.Text,QuoteFields.Checked,FExecuteOnly, |
188 |
ProcInputList.Items,ProcOutputList.Items,SQLText.Lines) |
189 |
else |
190 |
begin |
191 |
FieldNames := FIBSystemTables.GetFieldNames(FieldList); |
192 |
try |
193 |
FIBSystemTables.GenerateSelectSQL(TableNamesCombo.Text,QuoteFields.Checked,FieldNames,SQLText.Lines) |
194 |
finally |
195 |
FieldNames.Free |
196 |
end; |
197 |
end; |
198 |
end; |
199 |
|
200 |
procedure TIBSelectSQLEditorForm.TestBtnClick(Sender: TObject); |
201 |
begin |
202 |
FIBSystemTables.TestSQL(SQLText.Lines.Text,GenerateParams.Checked) |
203 |
end; |
204 |
|
205 |
procedure TIBSelectSQLEditorForm.ExecutePageShow(Sender: TObject); |
206 |
var ProcName: string; |
207 |
IsProcedureName: boolean; |
208 |
begin |
209 |
FIBSystemTables.GetProcedureNames(ProcedureNames.Items,true); |
210 |
if ProcedureNames.Items.Count > 0 then |
211 |
begin |
212 |
if (FIBSystemTables.GetStatementType(SQLText.Text,IsProcedureName) = SQLExecProcedure) or IsProcedureName then |
213 |
begin |
214 |
FIBSystemTables.GetTableAndColumns(SQLText.Text,ProcName,nil); |
215 |
ProcedureNames.ItemIndex := ProcedureNames.Items.IndexOf(ProcName) |
216 |
end |
217 |
else |
218 |
ProcedureNames.ItemIndex := 0; |
219 |
end; |
220 |
FIBSystemTables.GetProcParams(ProcedureNames.Text,FExecuteOnly,ProcInputList.Items,ProcOutputList.Items); |
221 |
SelectProcedure.Visible := not FExecuteOnly |
222 |
end; |
223 |
|
224 |
procedure TIBSelectSQLEditorForm.TableNamesComboCloseUp(Sender: TObject); |
225 |
begin |
226 |
FIBSystemTables.GetFieldNames(TableNamesCombo.Text,FieldList.Items); |
227 |
FIBSystemTables.GetPrimaryKeys(TableNamesCombo.Text,PrimaryKeyList.Items); |
228 |
end; |
229 |
|
230 |
constructor TIBSelectSQLEditorForm.Create(TheOwner: TComponent); |
231 |
begin |
232 |
inherited Create(TheOwner); |
233 |
FIBSystemTables := TIBSystemTables.Create; |
234 |
end; |
235 |
|
236 |
destructor TIBSelectSQLEditorForm.Destroy; |
237 |
begin |
238 |
if assigned(FIBSystemTables) then FIBSystemTables.Free; |
239 |
inherited Destroy; |
240 |
end; |
241 |
|
242 |
procedure TIBSelectSQLEditorForm.SetDatabase(Database: TIBDatabase); |
243 |
begin |
244 |
IBTransaction1.DefaultDatabase := Database; |
245 |
FIBSystemTables.SelectDatabase(Database,IBTransaction1) |
246 |
end; |
247 |
|
248 |
end. |