1 |
< |
{************************************************************************} |
2 |
< |
{ } |
3 |
< |
{ Borland Delphi Visual Component Library } |
4 |
< |
{ InterBase Express core components } |
5 |
< |
{ } |
6 |
< |
{ Copyright (c) 1998-2000 Inprise Corporation } |
7 |
< |
{ } |
8 |
< |
{ InterBase Express is based in part on the product } |
9 |
< |
{ Free IB Components, written by Gregory H. Deatz for } |
10 |
< |
{ Hoagland, Longo, Moran, Dunst & Doukas Company. } |
11 |
< |
{ Free IB Components is used under license. } |
12 |
< |
{ } |
13 |
< |
{ The contents of this file are subject to the InterBase } |
14 |
< |
{ Public License Version 1.0 (the "License"); you may not } |
15 |
< |
{ use this file except in compliance with the License. You } |
16 |
< |
{ may obtain a copy of the License at http://www.Inprise.com/IPL.html } |
17 |
< |
{ Software distributed under the License is distributed on } |
18 |
< |
{ an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either } |
19 |
< |
{ express or implied. See the License for the specific language } |
20 |
< |
{ governing rights and limitations under the License. } |
21 |
< |
{ The Original Code was created by InterBase Software Corporation } |
22 |
< |
{ and its successors. } |
23 |
< |
{ Portions created by Inprise Corporation are Copyright (C) Inprise } |
24 |
< |
{ Corporation. All Rights Reserved. } |
25 |
< |
{ Contributor(s): Jeff Overcash } |
26 |
< |
{ } |
27 |
< |
{************************************************************************} |
28 |
< |
|
29 |
< |
unit IBTable; |
30 |
< |
|
31 |
< |
{$Mode Delphi} |
32 |
< |
|
33 |
< |
interface |
34 |
< |
|
35 |
< |
uses SysUtils, Classes, DB, IB, IBCustomDataSet, |
36 |
< |
IBHeader, IBSQL, IBUtils; |
37 |
< |
|
38 |
< |
type |
39 |
< |
|
40 |
< |
{ TIBTable } |
41 |
< |
|
42 |
< |
TIBTableType = (ttSystem, ttView); |
43 |
< |
TIBTableTypes = set of TIBTableType; |
44 |
< |
TIndexName = String; |
45 |
< |
|
46 |
< |
TIBTable = class; |
47 |
< |
|
48 |
< |
TIBTable = class(TIBCustomDataSet) |
49 |
< |
private |
50 |
< |
FSystemTable: Boolean; |
51 |
< |
FMultiTableView: Boolean; |
52 |
< |
FMasterLink: TMasterDataLink; |
53 |
< |
FMasterFieldsList: TStringList; |
54 |
< |
FDetailFieldsList: TStringList; |
55 |
< |
FStoreDefs: Boolean; |
56 |
< |
FIndexDefs: TIndexDefs; |
57 |
< |
FDefaultIndex: Boolean; |
58 |
< |
FReadOnly: Boolean; |
59 |
< |
FFieldsIndex: Boolean; |
60 |
< |
FTableName: String; |
61 |
< |
FIndexName: TIndexName; |
62 |
< |
FRegenerateSQL: Boolean; |
63 |
< |
FNameList: TStrings; |
64 |
< |
FSwitchingIndex: Boolean; |
65 |
< |
FPrimaryIndexFields: string; |
66 |
< |
FTableTypes: TIBTableTypes; |
67 |
< |
WhereAllRefreshSQL: TStrings; |
68 |
< |
WhereDBKeyRefreshSQL: TStrings; |
69 |
< |
WherePrimaryRefreshSQL: TStrings; |
70 |
< |
|
71 |
< |
function GetIndexFieldCount: Integer; |
72 |
< |
function GetIndexField(Index: Integer): TField; |
73 |
< |
procedure MasterChanged(Sender: TObject); |
74 |
< |
procedure MasterDisabled(Sender: TObject); |
75 |
< |
procedure SetDataSource(Value: TDataSource); |
76 |
< |
procedure SetIndexField(Index: Integer; Value: TField); |
77 |
< |
procedure SetIndexFieldNames(const Value: string); |
78 |
< |
procedure GenerateSQL; |
79 |
< |
procedure GenerateUpdateSQL; |
80 |
< |
procedure SwitchToIndex(); |
81 |
< |
procedure InternalTableRefresh(); |
82 |
< |
function GetTableNames: TStrings; |
83 |
< |
procedure GetTableNamesFromServer; |
84 |
< |
procedure SetTableTypes( |
85 |
< |
const Value: TIBTableTypes); |
86 |
< |
function InternalGotoDBKey(DBKey: TIBDBKey): Boolean; |
87 |
< |
function FormatFieldsList(Value: string): string; |
88 |
< |
function GetCurrentDBKey: TIBDBKey; |
89 |
< |
function InternalGetUpdatable: Boolean; |
90 |
< |
function GetExists: Boolean; |
91 |
< |
procedure SetIndexDefs(Value: TIndexDefs); |
92 |
< |
procedure ExtractLinkFields; |
93 |
< |
function FieldDefsStored: Boolean; |
94 |
< |
function IndexDefsStored: Boolean; |
95 |
< |
function GetMasterFields: string; |
96 |
< |
procedure SetMasterFields(const Value: string); |
97 |
< |
function GetIndexFieldNames: string; |
98 |
< |
function GetIndexName: string; |
99 |
< |
procedure SetIndexName(const Value: string); |
100 |
< |
procedure SetParams; |
101 |
< |
procedure SetReadOnly(Value: Boolean); |
102 |
< |
procedure SetTableName(Value: String); |
103 |
< |
procedure SetIndex(const Value: string; FieldsIndex: Boolean); |
104 |
< |
procedure ResetSQLStatements; |
105 |
< |
procedure Reopen; |
106 |
< |
|
107 |
< |
protected |
108 |
< |
|
109 |
< |
procedure DoOnNewRecord; override; |
110 |
< |
procedure GetIndexParams(const IndexName: string; FieldsIndex: Boolean; |
111 |
< |
var IndexedName: string); |
112 |
< |
function GetCanModify: Boolean; override; |
113 |
< |
procedure UpdateIndexDefs; override; |
114 |
< |
procedure DataEvent(Event: TDataEvent; Info: Ptrint); override; |
115 |
< |
procedure DefChanged(Sender: TObject); virtual; |
116 |
< |
function GetDataSource: TDataSource; override; |
117 |
< |
procedure InitFieldDefs; override; |
118 |
< |
procedure InternalClose; override; |
119 |
< |
procedure InternalOpen; override; |
120 |
< |
procedure InternalRefresh; override; |
121 |
< |
procedure SetFiltered(Value: Boolean); override; |
122 |
< |
procedure SetFilterText(const Value: string); override; |
123 |
< |
procedure SetFilterOptions(Value: TFilterOptions); override; |
124 |
< |
procedure InternalRefreshRow; override; |
125 |
< |
|
126 |
< |
public |
127 |
< |
constructor Create(AOwner: TComponent); override; |
128 |
< |
destructor Destroy; override; |
129 |
< |
procedure AddIndex(const Name, Fields: string; Options: TIndexOptions; |
130 |
< |
const DescFields: string = ''); |
131 |
< |
procedure CreateTable; |
132 |
< |
procedure DeleteIndex(const Name: string); |
133 |
< |
procedure DeleteTable; |
134 |
< |
procedure EmptyTable; |
135 |
< |
procedure GetDetailLinkFields(MasterFields, DetailFields: TList); virtual; |
136 |
< |
procedure GetIndexNames(List: TStrings); |
137 |
< |
procedure GotoCurrent(Table: TIBTable); |
138 |
< |
property CurrentDBKey: TIBDBKey read GetCurrentDBKey; |
139 |
< |
property Exists: Boolean read GetExists; |
140 |
< |
property IndexFieldCount: Integer read GetIndexFieldCount; |
141 |
< |
property IndexFields[Index: Integer]: TField read GetIndexField write SetIndexField; |
142 |
< |
property TableNames: TStrings read GetTableNames; |
143 |
< |
|
144 |
< |
published |
145 |
< |
property Active; |
146 |
< |
property BufferChunks; |
147 |
< |
property CachedUpdates; |
148 |
< |
property Constraints stored ConstraintsStored; |
149 |
< |
property DefaultIndex: Boolean read FDefaultIndex write FDefaultIndex default True; |
150 |
< |
property FieldDefs stored FieldDefsStored; |
151 |
< |
property Filter; |
152 |
< |
property Filtered; |
153 |
< |
property IndexDefs: TIndexDefs read FIndexDefs write SetIndexDefs stored IndexDefsStored; |
154 |
< |
property IndexFieldNames: string read GetIndexFieldNames write SetIndexFieldNames; |
155 |
< |
property IndexName: string read GetIndexName write SetIndexName; |
156 |
< |
property MasterFields: string read GetMasterFields write SetMasterFields; |
157 |
< |
property MasterSource: TDataSource read GetDataSource write SetDataSource; |
158 |
< |
property ReadOnly: Boolean read FReadOnly write SetReadOnly default False; |
159 |
< |
property StoreDefs: Boolean read FStoreDefs write FStoreDefs default False; |
160 |
< |
property TableName: String read FTableName write SetTableName; |
161 |
< |
property TableTypes: TIBTableTypes read FTableTypes write SetTableTypes default []; |
162 |
< |
property UpdateObject; |
163 |
< |
property UniDirectional; |
164 |
< |
|
165 |
< |
property BeforeDatabaseDisconnect; |
166 |
< |
property AfterDatabaseDisconnect; |
167 |
< |
property DatabaseFree; |
168 |
< |
property BeforeTransactionEnd; |
169 |
< |
property AfterTransactionEnd; |
170 |
< |
property TransactionFree; |
171 |
< |
property OnFilterRecord; |
172 |
< |
end; |
173 |
< |
|
174 |
< |
implementation |
175 |
< |
|
176 |
< |
{ TIBTable } |
177 |
< |
|
178 |
< |
constructor TIBTable.Create(AOwner: TComponent); |
179 |
< |
begin |
180 |
< |
inherited Create(AOwner); |
181 |
< |
FNameList := TStringList.Create; |
182 |
< |
FSwitchingIndex := False; |
183 |
< |
FIndexDefs := TIndexDefs.Create(Self); |
184 |
< |
WhereAllRefreshSQL := TStringList.Create; |
185 |
< |
WhereDBKeyRefreshSQL := TStringList.Create; |
186 |
< |
WherePrimaryRefreshSQL := TStringList.Create; |
187 |
< |
FDefaultIndex := True; |
188 |
< |
FRegenerateSQL := True; |
189 |
< |
FMasterFieldsList := TStringList.Create; |
190 |
< |
FDetailFieldsList := TStringList.Create; |
191 |
< |
FMasterLink := TMasterDataLink.Create(Self); |
192 |
< |
FMasterLink.OnMasterChange := MasterChanged; |
193 |
< |
FMasterLink.OnMasterDisable := MasterDisabled; |
194 |
< |
QRefresh.OnSQLChanging := nil; |
195 |
< |
QDelete.OnSQLChanging := nil; |
196 |
< |
QInsert.OnSQLChanging := nil; |
197 |
< |
QModify.OnSQLChanging := nil; |
198 |
< |
end; |
199 |
< |
|
200 |
< |
destructor TIBTable.Destroy; |
201 |
< |
begin |
202 |
< |
FNameList.Free; |
203 |
< |
FIndexDefs.Free; |
204 |
< |
FMasterFieldsList.Free; |
205 |
< |
FDetailFieldsList.Free; |
206 |
< |
FMasterLink.Free; |
207 |
< |
WhereAllRefreshSQL.Free; |
208 |
< |
WhereDBKeyRefreshSQL.Free; |
209 |
< |
WherePrimaryRefreshSQL.Free; |
210 |
< |
inherited Destroy; |
211 |
< |
end; |
212 |
< |
|
213 |
< |
procedure TIBTable.InternalClose; |
214 |
< |
begin |
215 |
< |
DataEvent(dePropertyChange, 0); |
216 |
< |
inherited InternalClose; |
217 |
< |
end; |
218 |
< |
|
219 |
< |
procedure TIBTable.InternalOpen; |
220 |
< |
begin |
221 |
< |
if FTableName = '' then IBError(ibxeNoTableName, [nil]); |
222 |
< |
ActivateConnection; |
223 |
< |
ActivateTransaction; |
224 |
< |
if FRegenerateSQL then |
225 |
< |
begin |
226 |
< |
InternalUnprepare; |
227 |
< |
GenerateSQL; |
228 |
< |
if not FReadOnly then |
229 |
< |
GenerateUpdateSQL; |
230 |
< |
FRegenerateSQL := False; |
231 |
< |
end; |
232 |
< |
SetParams; |
233 |
< |
inherited InternalOpen; |
234 |
< |
end; |
235 |
< |
|
236 |
< |
procedure TIBTable.InternalRefresh; |
237 |
< |
var |
238 |
< |
DBKey: TIBDBKey; |
239 |
< |
begin |
240 |
< |
DBKey := CurrentDBKey; |
241 |
< |
Reopen; |
242 |
< |
if DBKey.DBKey[0] <> 0 then |
243 |
< |
InternalGotoDBKey(DBKey); |
244 |
< |
end; |
245 |
< |
|
246 |
< |
procedure TIBTable.SetFiltered(Value: Boolean); |
247 |
< |
begin |
248 |
< |
if(Filtered <> Value) then |
249 |
< |
begin |
250 |
< |
inherited SetFiltered(value); |
251 |
< |
if Active then |
252 |
< |
InternalTableRefresh; |
253 |
< |
end |
254 |
< |
else |
255 |
< |
inherited SetFiltered(value); |
256 |
< |
end; |
257 |
< |
|
258 |
< |
procedure TIBTable.SetFilterText(const Value: string); |
259 |
< |
begin |
260 |
< |
if Filtered and (Value <> Filter) then |
261 |
< |
begin |
262 |
< |
inherited SetFilterText(value); |
263 |
< |
InternalTableRefresh; |
264 |
< |
end |
265 |
< |
else |
266 |
< |
inherited SetFilterText(value); |
267 |
< |
end; |
268 |
< |
|
269 |
< |
procedure TIBTable.SetFilterOptions(Value: TFilterOptions); |
270 |
< |
begin |
271 |
< |
if Value <> [] then |
272 |
< |
IBError(ibxeNotSupported, [nil]); |
273 |
< |
end; |
274 |
< |
|
275 |
< |
procedure TIBTable.InternalRefreshRow; |
276 |
< |
begin |
277 |
< |
if CurrentDBKey.DBKey[0] <> 0 then |
278 |
< |
QRefresh.SQL.Assign(WhereDBKeyRefreshSQL) |
279 |
< |
else if WherePrimaryRefreshSQL.Text <> '' then |
280 |
< |
QRefresh.SQL.Assign(WherePrimaryRefreshSQL) |
281 |
< |
else |
282 |
< |
QRefresh.SQL.Assign(WhereAllRefreshSQL); |
283 |
< |
inherited InternalRefreshRow; |
284 |
< |
end; |
285 |
< |
|
286 |
< |
procedure TIBTable.DefChanged(Sender: TObject); |
287 |
< |
begin |
288 |
< |
StoreDefs := True; |
289 |
< |
end; |
290 |
< |
|
291 |
< |
procedure TIBTable.InitFieldDefs; |
292 |
< |
var |
293 |
< |
sqlscale: Integer; |
294 |
< |
Query: TIBSQL; |
295 |
< |
begin |
296 |
< |
if FTableName = '' then IBError(ibxeNoTableName, [nil]); |
297 |
< |
if (InternalPrepared) then InternalInitFieldDefs else |
298 |
< |
begin |
299 |
< |
Database.InternalTransaction.StartTransaction; |
300 |
< |
Query := TIBSQL.Create(self); |
301 |
< |
try |
302 |
< |
Query.GoToFirstRecordOnExecute := False; |
303 |
< |
Query.Database := DataBase; |
304 |
< |
Query.Transaction := Database.InternalTransaction; |
305 |
< |
Query.SQL.Text := 'Select R.RDB$FIELD_NAME, R.RDB$FIELD_POSITION, ' + {do not localize} |
306 |
< |
'F.RDB$COMPUTED_BLR, F.RDB$DEFAULT_VALUE, ' + {do not localize} |
307 |
< |
'F.RDB$NULL_FLAG, ' + {do not localize} |
308 |
< |
'F.RDB$FIELD_LENGTH, F.RDB$FIELD_SCALE, ' + {do not localize} |
309 |
< |
'F.RDB$FIELD_TYPE, F.RDB$FIELD_SUB_TYPE, ' + {do not localize} |
310 |
< |
'F.RDB$EXTERNAL_LENGTH, F.RDB$EXTERNAL_SCALE, F.RDB$EXTERNAL_TYPE ' + {do not localize} |
311 |
< |
'from RDB$RELATION_FIELDS R, RDB$FIELDS F ' + {do not localize} |
312 |
< |
'where R.RDB$RELATION_NAME = ' + {do not localize} |
313 |
< |
'''' + |
314 |
< |
FormatIdentifierValue(Database.SQLDialect, |
315 |
< |
QuoteIdentifier(DataBase.SQLDialect, FTableName)) + |
316 |
< |
''' ' + |
317 |
< |
'and R.RDB$FIELD_SOURCE = F.RDB$FIELD_NAME '+ {do not localize} |
318 |
< |
'order by R.RDB$FIELD_POSITION'; {do not localize} |
319 |
< |
|
320 |
< |
Query.Prepare; |
321 |
< |
Query.ExecQuery; |
322 |
< |
FieldDefs.BeginUpdate; |
323 |
< |
FieldDefs.Clear; |
324 |
< |
while (not Query.EOF) and (Query.Next <> nil) do |
325 |
< |
begin |
326 |
< |
with FieldDefs.AddFieldDef do |
327 |
< |
begin |
328 |
< |
(* FieldNo := Query.Current.ByName('RDB$FIELD_POSITION').AsInteger; {do not localize}*) |
329 |
< |
Name := TrimRight(Query.Current.ByName('RDB$FIELD_NAME').AsString); {do not localize} |
330 |
< |
case Query.Current.ByName('RDB$FIELD_TYPE').AsInteger of {do not localize} |
331 |
< |
blr_varying, blr_text: |
332 |
< |
begin |
333 |
< |
DataType := ftString; |
334 |
< |
Size := Query.Current.ByName('RDB$FIELD_LENGTH').AsInteger; {do not localize} |
335 |
< |
end; |
336 |
< |
blr_float, blr_double, blr_d_float: DataType := ftFloat; |
337 |
< |
blr_short: |
338 |
< |
begin |
339 |
< |
sqlscale := Query.Current.ByName('RDB$FIELD_SCALE').AsInteger; {do not localize} |
340 |
< |
if (sqlscale = 0) then |
341 |
< |
DataType := ftSmallInt |
342 |
< |
else |
343 |
< |
begin |
344 |
< |
DataType := ftBCD; |
345 |
< |
Precision := 4; |
346 |
< |
end; |
347 |
< |
end; |
348 |
< |
blr_long: |
349 |
< |
begin |
350 |
< |
sqlscale := Query.Current.ByName('RDB$FIELD_SCALE').AsInteger; {do not localize} |
351 |
< |
if (sqlscale = 0) then |
352 |
< |
DataType := ftInteger |
353 |
< |
else if (sqlscale >= (-4)) then |
354 |
< |
begin |
355 |
< |
DataType := ftBCD; |
356 |
< |
Precision := 9; |
357 |
< |
end |
358 |
< |
else |
359 |
< |
DataType := ftFloat; |
360 |
< |
end; |
361 |
< |
blr_int64: |
362 |
< |
begin |
363 |
< |
sqlscale := Query.Current.ByName('RDB$FIELD_SCALE').AsInteger; {do not localize} |
364 |
< |
if (sqlscale = 0) then |
365 |
< |
DataType := ftLargeInt |
366 |
< |
else if (sqlscale >= (-4)) then |
367 |
< |
begin |
368 |
< |
DataType := ftBCD; |
369 |
< |
Precision := 18; |
370 |
< |
end |
371 |
< |
else |
372 |
< |
DataType := ftFloat; |
373 |
< |
end; |
374 |
< |
blr_timestamp: DataType := ftDateTime; |
375 |
< |
blr_sql_time: DataType := ftTime; |
376 |
< |
blr_sql_date: DataType := ftDate; |
377 |
< |
blr_blob: |
378 |
< |
if (Query.Current.ByName('RDB$FIELD_SUB_TYPE').AsInteger = 1) then {do not localize} |
379 |
< |
DataType := ftMemo |
380 |
< |
else |
381 |
< |
DataType := ftBlob; |
382 |
< |
blr_quad: |
383 |
< |
begin |
384 |
< |
DataType := ftUnknown; |
385 |
< |
Size := sizeof (TISC_QUAD); |
386 |
< |
end; |
387 |
< |
else |
388 |
< |
DataType := ftUnknown; |
389 |
< |
end; |
390 |
< |
if not (Query.Current.ByName('RDB$COMPUTED_BLR').IsNull) then {do not localize} |
391 |
< |
begin |
392 |
< |
Attributes := [faReadOnly]; |
393 |
< |
InternalCalcField := True |
394 |
< |
end |
395 |
< |
else |
396 |
< |
InternalCalcField := False; |
397 |
< |
if ((not InternalCalcField) and |
398 |
< |
Query.Current.ByName('RDB$DEFAULT_VALUE').IsNull and {do not localize} |
399 |
< |
(Query.Current.ByName('RDB$NULL_FLAG').AsInteger = 1) )then {do not localize} |
400 |
< |
begin |
401 |
< |
Attributes := [faRequired]; |
402 |
< |
Required := True; |
403 |
< |
end; |
404 |
< |
end; |
405 |
< |
end; |
406 |
< |
FieldDefs.EndUpdate; |
407 |
< |
finally |
408 |
< |
Query.free; |
409 |
< |
Database.InternalTransaction.Commit; |
410 |
< |
end; |
411 |
< |
end; |
412 |
< |
end; |
413 |
< |
|
414 |
< |
{ Index / Ranges / Keys } |
415 |
< |
|
416 |
< |
procedure TIBTable.AddIndex(const Name, Fields: string; Options: TIndexOptions; |
417 |
< |
const DescFields: string); |
418 |
< |
var |
419 |
< |
Query: TIBSQL; |
420 |
< |
FieldList: string; |
421 |
< |
begin |
422 |
< |
FieldDefs.Update; |
423 |
< |
if Active then begin |
424 |
< |
CheckBrowseMode; |
425 |
< |
CursorPosChanged; |
426 |
< |
end; |
427 |
< |
Query := TIBSQL.Create(self); |
428 |
< |
try |
429 |
< |
Query.Database := DataBase; |
430 |
< |
Query.Transaction := Transaction; |
431 |
< |
FieldList := FormatFieldsList(Fields); |
432 |
< |
if (ixPrimary in Options) then |
433 |
< |
begin |
434 |
< |
Query.SQL.Text := 'Alter Table ' + {do not localize} |
435 |
< |
QuoteIdentifier(Database.SQLDialect, FTableName) + |
436 |
< |
' Add CONSTRAINT ' + {do not localize} |
437 |
< |
QuoteIdentifier(Database.SQLDialect, Name) |
438 |
< |
+ ' Primary Key (' + {do not localize} |
439 |
< |
FormatFieldsList(Fields) + |
440 |
< |
')'; |
441 |
< |
end |
442 |
< |
else if ([ixUnique, ixDescending] * Options = [ixUnique, ixDescending]) then |
443 |
< |
Query.SQL.Text := 'Create unique Descending Index ' + {do not localize} |
444 |
< |
QuoteIdentifier(Database.SQLDialect, Name) + |
445 |
< |
' on ' + {do not localize} |
446 |
< |
QuoteIdentifier(Database.SQLDialect, FTableName) + |
447 |
< |
' (' + FieldList + ')' |
448 |
< |
else if (ixUnique in Options) then |
449 |
< |
Query.SQL.Text := 'Create unique Index ' + {do not localize} |
450 |
< |
QuoteIdentifier(Database.SQLDialect, Name) + |
451 |
< |
' on ' + {do not localize} |
452 |
< |
QuoteIdentifier(Database.SQLDialect, FTableName) + |
453 |
< |
' (' + FieldList + ')' |
454 |
< |
else if (ixDescending in Options) then |
455 |
< |
Query.SQL.Text := 'Create Descending Index ' + {do not localize} |
456 |
< |
QuoteIdentifier(Database.SQLDialect, Name) + |
457 |
< |
' on ' + {do not localize} |
458 |
< |
QuoteIdentifier(Database.SQLDialect, FTableName) + |
459 |
< |
' (' + FieldList + ')' |
460 |
< |
else |
461 |
< |
Query.SQL.Text := 'Create Index ' + {do not localize} |
462 |
< |
QuoteIdentifier(Database.SQLDialect, Name) + |
463 |
< |
' on ' + {do not localize} |
464 |
< |
QuoteIdentifier(Database.SQLDialect, FTableName) + |
465 |
< |
' (' + FieldList + ')'; |
466 |
< |
Query.Prepare; |
467 |
< |
Query.ExecQuery; |
468 |
< |
IndexDefs.Updated := False; |
469 |
< |
finally |
470 |
< |
Query.free |
471 |
< |
end; |
472 |
< |
end; |
473 |
< |
|
474 |
< |
procedure TIBTable.DeleteIndex(const Name: string); |
475 |
< |
var |
476 |
< |
Query: TIBSQL; |
477 |
< |
|
478 |
< |
procedure DeleteByIndex; |
479 |
< |
begin |
480 |
< |
Query := TIBSQL.Create(self); |
481 |
< |
try |
482 |
< |
Query.Database := DataBase; |
483 |
< |
Query.Transaction := Transaction; |
484 |
< |
Query.SQL.Text := 'Drop index ' + {do not localize} |
485 |
< |
QuoteIdentifier(Database.SQLDialect, Name); |
486 |
< |
Query.Prepare; |
487 |
< |
Query.ExecQuery; |
488 |
< |
IndexDefs.Updated := False; |
489 |
< |
finally |
490 |
< |
Query.Free; |
491 |
< |
end; |
492 |
< |
end; |
493 |
< |
|
494 |
< |
function DeleteByConstraint: Boolean; |
495 |
< |
begin |
496 |
< |
Result := False; |
497 |
< |
Query := TIBSQL.Create(self); |
498 |
< |
try |
499 |
< |
Query.Database := DataBase; |
500 |
< |
Query.Transaction := Transaction; |
501 |
< |
Query.SQL.Text := 'Select ''foo'' from RDB$RELATION_CONSTRAINTS ' + |
502 |
< |
'where RDB$RELATION_NAME = ' + |
503 |
< |
'''' + |
504 |
< |
FormatIdentifierValue(Database.SQLDialect, |
505 |
< |
QuoteIdentifier(DataBase.SQLDialect, FTableName)) + |
506 |
< |
''' ' + |
507 |
< |
' AND RDB$CONSTRAINT_NAME = ' + |
508 |
< |
'''' + |
509 |
< |
FormatIdentifierValue(Database.SQLDialect, |
510 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name)) + |
511 |
< |
''' ' + |
512 |
< |
'AND RDB$CONSTRAINT_TYPE = ''PRIMARY KEY'''; |
513 |
< |
Query.Prepare; |
514 |
< |
Query.ExecQuery; |
515 |
< |
if not Query.EOF then |
516 |
< |
begin |
517 |
< |
Query.Close; |
518 |
< |
Query.SQL.Text := 'Alter Table ' + {do not localize} |
519 |
< |
QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
520 |
< |
' Drop Constraint ' + |
521 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name); |
522 |
< |
Query.Prepare; |
523 |
< |
Query.ExecQuery; |
524 |
< |
IndexDefs.Updated := False; |
525 |
< |
Result := True; |
526 |
< |
end; |
527 |
< |
finally |
528 |
< |
Query.Free; |
529 |
< |
end; |
530 |
< |
end; |
531 |
< |
|
532 |
< |
procedure DeleteByKey; |
533 |
< |
begin |
534 |
< |
Query := TIBSQL.Create(self); |
535 |
< |
try |
536 |
< |
Query.Database := DataBase; |
537 |
< |
Query.Transaction := Transaction; |
538 |
< |
Query.SQL.Text := 'Select RDB$CONSTRAINT_NAME from RDB$RELATION_CONSTRAINTS ' + |
539 |
< |
'where RDB$RELATION_NAME = ' + |
540 |
< |
'''' + |
541 |
< |
FormatIdentifierValue(Database.SQLDialect, |
542 |
< |
QuoteIdentifier(DataBase.SQLDialect, FTableName)) + |
543 |
< |
''' ' + |
544 |
< |
'AND RDB$INDEX_NAME = ' + |
545 |
< |
'''' + |
546 |
< |
FormatIdentifierValue(Database.SQLDialect, |
547 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name)) + |
548 |
< |
''' ' + |
549 |
< |
'AND RDB$CONSTRAINT_TYPE = ''PRIMARY KEY'''; |
550 |
< |
Query.Prepare; |
551 |
< |
Query.ExecQuery; |
552 |
< |
if not Query.EOF then |
553 |
< |
begin |
554 |
< |
Query.Close; |
555 |
< |
Query.SQL.Text := 'Alter Table ' + {do not localize} |
556 |
< |
QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
557 |
< |
' Drop Constraint ' + |
558 |
< |
QuoteIdentifier(DataBase.SQLDialect, Query.Current.ByName('RDB$CONSTRAINT_NAME').AsString); |
559 |
< |
Query.Prepare; |
560 |
< |
Query.ExecQuery; |
561 |
< |
IndexDefs.Updated := False; |
562 |
< |
end; |
563 |
< |
finally |
564 |
< |
Query.Free; |
565 |
< |
end; |
566 |
< |
end; |
567 |
< |
|
568 |
< |
begin |
569 |
< |
if Active then |
570 |
< |
CheckBrowseMode; |
571 |
< |
IndexDefs.Update; |
572 |
< |
if (Pos('RDB$PRIMARY', Name) <> 0 ) then {do not localize} {mbcs ok} |
573 |
< |
DeleteByKey |
574 |
< |
else if not DeleteByConstraint then |
575 |
< |
DeleteByIndex; |
576 |
< |
end; |
577 |
< |
|
578 |
< |
function TIBTable.GetIndexFieldNames: string; |
579 |
< |
begin |
580 |
< |
if FFieldsIndex then Result := FIndexName else Result := ''; |
581 |
< |
end; |
582 |
< |
|
583 |
< |
function TIBTable.GetIndexName: string; |
584 |
< |
begin |
585 |
< |
if FFieldsIndex then Result := '' else Result := FIndexName; |
586 |
< |
end; |
587 |
< |
|
588 |
< |
procedure TIBTable.GetIndexNames(List: TStrings); |
589 |
< |
begin |
590 |
< |
IndexDefs.Update; |
591 |
< |
IndexDefs.GetItemNames(List); |
592 |
< |
end; |
593 |
< |
|
594 |
< |
procedure TIBTable.GetIndexParams(const IndexName: string; |
595 |
< |
FieldsIndex: Boolean; var IndexedName: string); |
596 |
< |
var |
597 |
< |
IndexStr: TIndexName; |
598 |
< |
begin |
599 |
< |
if IndexName <> '' then |
600 |
< |
begin |
601 |
< |
IndexDefs.Update; |
602 |
< |
IndexStr := IndexName; |
603 |
< |
if FieldsIndex then |
604 |
< |
IndexStr := IndexDefs.FindIndexForFields(IndexName).Name; |
605 |
< |
end; |
606 |
< |
IndexedName := IndexStr; |
607 |
< |
end; |
608 |
< |
|
609 |
< |
procedure TIBTable.SetIndexDefs(Value: TIndexDefs); |
610 |
< |
begin |
611 |
< |
IndexDefs.Assign(Value); |
612 |
< |
end; |
613 |
< |
|
614 |
< |
procedure TIBTable.SetIndex(const Value: string; FieldsIndex: Boolean); |
615 |
< |
begin |
616 |
< |
if Active then CheckBrowseMode; |
617 |
< |
if (FIndexName <> Value) or (FFieldsIndex <> FieldsIndex) then |
618 |
< |
begin |
619 |
< |
FIndexName := Value; |
620 |
< |
FFieldsIndex := FieldsIndex; |
621 |
< |
if Active then |
622 |
< |
begin |
623 |
< |
SwitchToIndex; |
624 |
< |
end; |
625 |
< |
end; |
626 |
< |
end; |
627 |
< |
|
628 |
< |
procedure TIBTable.SetIndexFieldNames(const Value: string); |
629 |
< |
begin |
630 |
< |
SetIndex(Value, Value <> ''); |
631 |
< |
end; |
632 |
< |
|
633 |
< |
procedure TIBTable.SetIndexName(const Value: string); |
634 |
< |
begin |
635 |
< |
SetIndex(Value, False); |
636 |
< |
end; |
637 |
< |
|
638 |
< |
procedure TIBTable.UpdateIndexDefs; |
639 |
< |
var |
640 |
< |
Opts: TIndexOptions; |
641 |
< |
Flds: string; |
642 |
< |
Query, SubQuery: TIBSQL; |
643 |
< |
begin |
644 |
< |
if not (csReading in ComponentState) then begin |
645 |
< |
if not Active and not FSwitchingIndex then |
646 |
< |
FieldDefs.Update; |
647 |
< |
IndexDefs.Clear; |
648 |
< |
Database.InternalTransaction.StartTransaction; |
649 |
< |
Query := TIBSQL.Create(self); |
650 |
< |
try |
651 |
< |
FPrimaryIndexFields := ''; |
652 |
< |
Query.GoToFirstRecordOnExecute := False; |
653 |
< |
Query.Database := DataBase; |
654 |
< |
Query.Transaction := Database.InternalTransaction; |
655 |
< |
Query.SQL.Text := |
656 |
< |
'Select I.RDB$INDEX_NAME, I.RDB$UNIQUE_FLAG, I.RDB$INDEX_TYPE, ' + {do not localize} |
657 |
< |
'I.RDB$SEGMENT_COUNT, S.RDB$FIELD_NAME from RDB$INDICES I, ' + {do not localize} |
658 |
< |
'RDB$INDEX_SEGMENTS S where I.RDB$INDEX_NAME = S.RDB$INDEX_NAME '+ {do not localize} |
659 |
< |
'and I.RDB$RELATION_NAME = ' + '''' + {do not localize} |
660 |
< |
FormatIdentifierValue(Database.SQLDialect, |
661 |
< |
QuoteIdentifier(DataBase.SQLDialect, FTableName)) + ''''; |
662 |
< |
Query.Prepare; |
663 |
< |
Query.ExecQuery; |
664 |
< |
while (not Query.EOF) and (Query.Next <> nil) do |
665 |
< |
begin |
666 |
< |
with IndexDefs.AddIndexDef do |
667 |
< |
begin |
668 |
< |
Name := TrimRight(Query.Current.ByName('RDB$INDEX_NAME').AsString); {do not localize} |
669 |
< |
Opts := []; |
670 |
< |
if Pos ('RDB$PRIMARY', Name) = 1 then Include(Opts, ixPrimary); {do not localize} {mbcs ok} |
671 |
< |
if Query.Current.ByName('RDB$UNIQUE_FLAG').AsInteger = 1 then Include(Opts, ixUnique); {do not localize} |
672 |
< |
if Query.Current.ByName('RDB$INDEX_TYPE').AsInteger = 2 then Include(Opts, ixDescending); {do not localize} |
673 |
< |
Options := Opts; |
674 |
< |
if (Query.Current.ByName('RDB$SEGMENT_COUNT').AsInteger = 1) then {do not localize} |
675 |
< |
Fields := Trim(Query.Current.ByName('RDB$FIELD_NAME').AsString) {do not localize} |
676 |
< |
else begin |
677 |
< |
SubQuery := TIBSQL.Create(self); |
678 |
< |
try |
679 |
< |
SubQuery.GoToFirstRecordOnExecute := False; |
680 |
< |
SubQuery.Database := DataBase; |
681 |
< |
SubQuery.Transaction := Database.InternalTransaction; |
682 |
< |
SubQuery.SQL.Text := |
683 |
< |
'Select RDB$FIELD_NAME from RDB$INDEX_SEGMENTS where RDB$INDEX_NAME = ' + {do not localize} |
684 |
< |
'''' + |
685 |
< |
FormatIdentifierValue(Database.SQLDialect, |
686 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name)) + |
687 |
< |
'''' + 'ORDER BY RDB$FIELD_POSITION'; {do not localize} |
688 |
< |
SubQuery.Prepare; |
689 |
< |
SubQuery.ExecQuery; |
690 |
< |
Flds := ''; |
691 |
< |
while (not SubQuery.EOF) and (SubQuery.Next <> nil) do |
692 |
< |
begin |
693 |
< |
if (Flds = '') then |
694 |
< |
Flds := TrimRight(SubQuery.Current.ByName('RDB$FIELD_NAME').AsString) {do not localize} |
695 |
< |
else begin |
696 |
< |
Query.Next; |
697 |
< |
Flds := Flds + ';' + TrimRight(SubQuery.Current[0].AsString); |
698 |
< |
end; |
699 |
< |
end; |
700 |
< |
Fields := Flds; |
701 |
< |
finally |
702 |
< |
SubQuery.Free; |
703 |
< |
end; |
704 |
< |
end; |
705 |
< |
if (ixDescending in Opts) then |
706 |
< |
DescFields := Fields; |
707 |
< |
if ixPrimary in Opts then |
708 |
< |
FPrimaryIndexFields := Fields; |
709 |
< |
end; |
710 |
< |
end; |
711 |
< |
finally |
712 |
< |
Query.Free; |
713 |
< |
Database.InternalTransaction.Commit; |
714 |
< |
end; |
715 |
< |
end; |
716 |
< |
end; |
717 |
< |
|
718 |
< |
function TIBTable.GetExists: Boolean; |
719 |
< |
var |
720 |
< |
Query: TIBSQL; |
721 |
< |
begin |
722 |
< |
Result := Active; |
723 |
< |
if Result or (TableName = '') then Exit; |
724 |
< |
Database.InternalTransaction.StartTransaction; |
725 |
< |
Query := TIBSQL.Create(self); |
726 |
< |
try |
727 |
< |
Query.Database := DataBase; |
728 |
< |
Query.Transaction := Database.InternalTransaction; |
729 |
< |
Query.SQL.Text := |
730 |
< |
'Select USER from RDB$RELATIONS where RDB$RELATION_NAME = ' + {do not localize} |
731 |
< |
'''' + |
732 |
< |
FormatIdentifierValue(Database.SQLDialect, |
733 |
< |
QuoteIdentifier(DataBase.SQLDialect, FTableName)) + ''''; |
734 |
< |
Query.Prepare; |
735 |
< |
Query.ExecQuery; |
736 |
< |
Result := not Query.EOF; |
737 |
< |
finally |
738 |
< |
Query.Free; |
739 |
< |
Database.InternalTransaction.Commit; |
740 |
< |
end; |
741 |
< |
end; |
742 |
< |
|
743 |
< |
procedure TIBTable.GotoCurrent(Table: TIBTable); |
744 |
< |
begin |
745 |
< |
CheckBrowseMode; |
746 |
< |
Table.CheckBrowseMode; |
747 |
< |
if (Database <> Table.Database) or |
748 |
< |
(CompareText(TableName, Table.TableName) <> 0) then |
749 |
< |
IBError(ibxeTableNameMismatch, [nil]); |
750 |
< |
Table.UpdateCursorPos; |
751 |
< |
InternalGotoDBKey(Table.CurrentDBKey); |
752 |
< |
DoBeforeScroll; |
753 |
< |
Resync([rmExact, rmCenter]); |
754 |
< |
DoAfterScroll; |
755 |
< |
end; |
756 |
< |
|
757 |
< |
|
758 |
< |
procedure TIBTable.CreateTable; |
759 |
< |
var |
760 |
< |
FieldList: string; |
761 |
< |
|
762 |
< |
procedure InitFieldsList; |
763 |
< |
var |
764 |
< |
I: Integer; |
765 |
< |
begin |
766 |
< |
InitFieldDefsFromFields; |
767 |
< |
for I := 0 to FieldDefs.Count - 1 do begin |
768 |
< |
if ( I > 0) then |
769 |
< |
FieldList := FieldList + ', '; |
770 |
< |
with FieldDefs[I] do |
771 |
< |
begin |
772 |
< |
case DataType of |
773 |
< |
ftString: |
774 |
< |
FieldList := FieldList + |
775 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
776 |
< |
' VARCHAR(' + IntToStr(Size) + ')'; {do not localize} |
777 |
< |
ftFixedChar: |
778 |
< |
FieldList := FieldList + |
779 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
780 |
< |
' CHAR(' + IntToStr(Size) + ')'; {do not localize} |
781 |
< |
ftBoolean, ftSmallint, ftWord: |
782 |
< |
FieldList := FieldList + |
783 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
784 |
< |
' SMALLINT'; {do not localize} |
785 |
< |
ftInteger: |
786 |
< |
FieldList := FieldList + |
787 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
788 |
< |
' INTEGER'; {do not localize} |
789 |
< |
ftFloat, ftCurrency: |
790 |
< |
FieldList := FieldList + |
791 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
792 |
< |
' DOUBLE PRECISION'; {do not localize} |
793 |
< |
ftBCD: begin |
794 |
< |
if (Database.SQLDialect = 1) then begin |
795 |
< |
if (Precision > 9) then |
796 |
< |
IBError(ibxeFieldUnsupportedType,[nil]); |
797 |
< |
if (Precision <= 4) then |
798 |
< |
Precision := 9; |
799 |
< |
end; |
800 |
< |
if (Precision <= 4 ) then |
801 |
< |
FieldList := FieldList + |
802 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
803 |
< |
' Numeric(18, 4)' {do not localize} |
804 |
< |
else |
805 |
< |
FieldList := FieldList + |
806 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
807 |
< |
' Numeric(' + IntToStr(Precision) + ', 4)'; {do not localize} |
808 |
< |
end; |
809 |
< |
ftDate: |
810 |
< |
FieldList := FieldList + |
811 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
812 |
< |
' DATE'; {do not localize} |
813 |
< |
ftTime: |
814 |
< |
FieldList := FieldList + |
815 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
816 |
< |
' TIME'; {do not localize} |
817 |
< |
ftDateTime: |
818 |
< |
if (Database.SQLDialect = 1) then |
819 |
< |
FieldList := FieldList + |
820 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
821 |
< |
' DATE' {do not localize} |
822 |
< |
else |
823 |
< |
FieldList := FieldList + |
824 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
825 |
< |
' TIMESTAMP'; {do not localize} |
826 |
< |
ftLargeInt: |
827 |
< |
if (Database.SQLDialect = 1) then |
828 |
< |
IBError(ibxeFieldUnsupportedType,[nil]) |
829 |
< |
else |
830 |
< |
FieldList := FieldList + |
831 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
832 |
< |
' Numeric(18, 0)'; {do not localize} |
833 |
< |
ftBlob, ftMemo: |
834 |
< |
FieldList := FieldList + |
835 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
836 |
< |
' BLOB SUB_TYPE 1'; {do not localize} |
837 |
< |
ftBytes, ftVarBytes, ftGraphic..ftTypedBinary: |
838 |
< |
FieldList := FieldList + |
839 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
840 |
< |
' BLOB SUB_TYPE 0'; {do not localize} |
841 |
< |
ftUnknown, ftADT, ftArray, ftReference, ftDataSet, |
842 |
< |
ftCursor, ftWideString, ftAutoInc: |
843 |
< |
IBError(ibxeFieldUnsupportedType,[nil]); |
844 |
< |
else |
845 |
< |
IBError(ibxeFieldUnsupportedType,[nil]); |
846 |
< |
end; |
847 |
< |
if faRequired in Attributes then |
848 |
< |
FieldList := FieldList + ' NOT NULL'; {do not localize} |
849 |
< |
end; |
850 |
< |
end; |
851 |
< |
end; |
852 |
< |
|
853 |
< |
procedure InternalCreateTable; |
854 |
< |
var |
855 |
< |
I: Integer; |
856 |
< |
Query: TIBSQL; |
857 |
< |
begin |
858 |
< |
if (FieldList = '') then |
859 |
< |
IBError(ibxeFieldUnsupportedType,[nil]); |
860 |
< |
Query := TIBSQL.Create(self); |
861 |
< |
try |
862 |
< |
Query.Database := Database; |
863 |
< |
Query.transaction := Transaction; |
864 |
< |
Query.SQL.Text := 'Create Table ' + |
865 |
< |
QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
866 |
< |
' (' + FieldList; {do not localize} |
867 |
< |
for I := 0 to IndexDefs.Count - 1 do |
868 |
< |
with IndexDefs[I] do |
869 |
< |
if ixPrimary in Options then |
870 |
< |
begin |
871 |
< |
Query.SQL.Text := Query.SQL.Text + ', CONSTRAINT ' + |
872 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
873 |
< |
' Primary Key (' + |
874 |
< |
FormatFieldsList(Fields) + |
875 |
< |
')'; |
876 |
< |
end; |
877 |
< |
Query.SQL.Text := Query.SQL.Text + ')'; |
878 |
< |
Query.Prepare; |
879 |
< |
Query.ExecQuery; |
880 |
< |
finally |
881 |
< |
Query.Free; |
882 |
< |
end; |
883 |
< |
end; |
884 |
< |
|
885 |
< |
procedure InternalCreateIndex; |
886 |
< |
var |
887 |
< |
I: Integer; |
888 |
< |
begin |
889 |
< |
for I := 0 to IndexDefs.Count - 1 do |
890 |
< |
with IndexDefs[I] do |
891 |
< |
if not (ixPrimary in Options) then |
892 |
< |
AddIndex(Name, Fields, Options); |
893 |
< |
end; |
894 |
< |
|
895 |
< |
begin |
896 |
< |
CheckInactive; |
897 |
< |
InitFieldsList; |
898 |
< |
InternalCreateTable; |
899 |
< |
InternalCreateIndex; |
900 |
< |
end; |
901 |
< |
|
902 |
< |
procedure TIBTable.DeleteTable; |
903 |
< |
var |
904 |
< |
Query: TIBSQL; |
905 |
< |
begin |
906 |
< |
CheckInactive; |
907 |
< |
Query := TIBSQL.Create(self); |
908 |
< |
try |
909 |
< |
Query.Database := DataBase; |
910 |
< |
Query.Transaction := Transaction; |
911 |
< |
Query.SQL.Text := 'drop table ' + {do not localize} |
912 |
< |
QuoteIdentifier(DataBase.SQLDialect, FTableName); |
913 |
< |
Query.Prepare; |
914 |
< |
Query.ExecQuery; |
915 |
< |
finally |
916 |
< |
Query.Free; |
917 |
< |
end; |
918 |
< |
end; |
919 |
< |
|
920 |
< |
procedure TIBTable.EmptyTable; |
921 |
< |
var |
922 |
< |
Query: TIBSQL; |
923 |
< |
begin |
924 |
< |
if Active then |
925 |
< |
CheckBrowseMode; |
926 |
< |
Query := TIBSQL.Create(self); |
927 |
< |
try |
928 |
< |
Query.Database := DataBase; |
929 |
< |
Query.Transaction := Transaction; |
930 |
< |
Query.SQL.Text := 'delete from ' + {do not localize} |
931 |
< |
QuoteIdentifier(DataBase.SQLDialect, FTableName); |
932 |
< |
Query.Prepare; |
933 |
< |
Query.ExecQuery; |
934 |
< |
if Active then |
935 |
< |
begin |
936 |
< |
ClearBuffers; |
937 |
< |
DataEvent(deDataSetChange, 0); |
938 |
< |
end; |
939 |
< |
finally |
940 |
< |
Query.Free; |
941 |
< |
end; |
942 |
< |
end; |
943 |
< |
|
944 |
< |
procedure TIBTable.DataEvent(Event: TDataEvent; Info: Ptrint); |
945 |
< |
begin |
946 |
< |
if Event = dePropertyChange then begin |
947 |
< |
IndexDefs.Updated := False; |
948 |
< |
FRegenerateSQL := True; |
949 |
< |
end; |
950 |
< |
inherited DataEvent(Event, Info); |
951 |
< |
end; |
952 |
< |
|
953 |
< |
{ Informational & Property } |
954 |
< |
|
955 |
< |
function TIBTable.GetCanModify: Boolean; |
956 |
< |
begin |
957 |
< |
Result := True; |
958 |
< |
if (FTableName = '') or FReadOnly |
959 |
< |
or FSystemTable or FMultiTableView then |
960 |
< |
Result := False; |
961 |
< |
end; |
962 |
< |
|
963 |
< |
function TIBTable.InternalGetUpdatable: Boolean; |
964 |
< |
var |
965 |
< |
Query : TIBSQL; |
966 |
< |
begin |
967 |
< |
Database.InternalTransaction.StartTransaction; |
968 |
< |
Query := TIBSQL.Create(self); |
969 |
< |
try |
970 |
< |
Query.Database := DataBase; |
971 |
< |
Query.Transaction := Database.InternalTransaction; |
972 |
< |
Query.SQL.Text := 'Select RDB$SYSTEM_FLAG, RDB$DBKEY_LENGTH ' + {do not localize} |
973 |
< |
'from RDB$RELATIONS where RDB$RELATION_NAME = ' + {do not localize} |
974 |
< |
'''' + |
975 |
< |
FormatIdentifierValue(Database.SQLDialect, |
976 |
< |
QuoteIdentifier(DataBase.SQLDialect, FTableName)) + ''''; |
977 |
< |
Query.Prepare; |
978 |
< |
Query.ExecQuery; |
979 |
< |
if (Query.Current[0].AsInteger <> 0) or |
980 |
< |
(Query.Current[1].AsInteger <> 8) then |
981 |
< |
Result := False |
982 |
< |
else |
983 |
< |
Result := True; |
984 |
< |
finally |
985 |
< |
Query.Free; |
986 |
< |
Database.InternalTransaction.Commit; |
987 |
< |
end; |
988 |
< |
end; |
989 |
< |
|
990 |
< |
function TIBTable.FieldDefsStored: Boolean; |
991 |
< |
begin |
992 |
< |
Result := StoreDefs and (FieldDefs.Count > 0); |
993 |
< |
end; |
994 |
< |
|
995 |
< |
function TIBTable.IndexDefsStored: Boolean; |
996 |
< |
begin |
997 |
< |
Result := StoreDefs and (IndexDefs.Count > 0); |
998 |
< |
end; |
999 |
< |
|
1000 |
< |
procedure TIBTable.SetParams; |
1001 |
< |
var |
1002 |
< |
i: Integer; |
1003 |
< |
begin |
1004 |
< |
if (MasterSource = nil) or (MasterSource.DataSet = nil) or |
1005 |
< |
(not MasterSource.DataSet.Active) or (FMasterFieldsList.Count = 0) then |
1006 |
< |
exit; |
1007 |
< |
for i := 0 to FMasterFieldsList.Count - 1 do |
1008 |
< |
QSelect.Params.ByName(FMasterFieldsList.Strings[i]).Value := |
1009 |
< |
MasterSource.DataSet.FieldByName(FMasterFieldsList.Strings[i]).Value; |
1010 |
< |
end; |
1011 |
< |
|
1012 |
< |
procedure TIBTable.MasterChanged(Sender: TObject); |
1013 |
< |
begin |
1014 |
< |
CheckBrowseMode; |
1015 |
< |
SetParams; |
1016 |
< |
ReQuery; |
1017 |
< |
end; |
1018 |
< |
|
1019 |
< |
procedure TIBTable.MasterDisabled(Sender: TObject); |
1020 |
< |
begin |
1021 |
< |
DataEvent(dePropertyChange, 0); |
1022 |
< |
ReQuery; |
1023 |
< |
end; |
1024 |
< |
|
1025 |
< |
function TIBTable.GetDataSource: TDataSource; |
1026 |
< |
begin |
1027 |
< |
Result := FMasterLink.DataSource; |
1028 |
< |
end; |
1029 |
< |
|
1030 |
< |
procedure TIBTable.SetDataSource(Value: TDataSource); |
1031 |
< |
begin |
1032 |
< |
if IsLinkedTo(Value) then IBError(ibxeCircularDataLink, [Self]); |
1033 |
< |
if FMasterLink.DataSource <> Value then |
1034 |
< |
DataEvent(dePropertyChange, 0); |
1035 |
< |
FMasterLink.DataSource := Value; |
1036 |
< |
end; |
1037 |
< |
|
1038 |
< |
function TIBTable.GetMasterFields: string; |
1039 |
< |
begin |
1040 |
< |
Result := FMasterLink.FieldNames; |
1041 |
< |
end; |
1042 |
< |
|
1043 |
< |
procedure TIBTable.SetMasterFields(const Value: string); |
1044 |
< |
begin |
1045 |
< |
if FMasterLink.FieldNames <> Value then |
1046 |
< |
DataEvent(dePropertyChange, 0); |
1047 |
< |
FMasterLink.FieldNames := Value; |
1048 |
< |
end; |
1049 |
< |
|
1050 |
< |
procedure TIBTable.DoOnNewRecord; |
1051 |
< |
var |
1052 |
< |
I: Integer; |
1053 |
< |
begin |
1054 |
< |
if FMasterLink.Active and (FMasterLink.Fields.Count > 0) then |
1055 |
< |
for I := 0 to FMasterLink.Fields.Count - 1 do |
1056 |
< |
IndexFields[I] := TField(FMasterLink.Fields[I]); |
1057 |
< |
inherited DoOnNewRecord; |
1058 |
< |
end; |
1059 |
< |
|
1060 |
< |
function TIBTable.FormatFieldsList(Value: String): String; |
1061 |
< |
var |
1062 |
< |
FieldName: string; |
1063 |
< |
i: Integer; |
1064 |
< |
begin |
1065 |
< |
if Database.SQLDialect = 1 then begin |
1066 |
< |
Value := QuoteIdentifier(Database.SQLDialect, Value); |
1067 |
< |
Result := StringReplace (Value, ';', ', ', [rfReplaceAll]); |
1068 |
< |
end |
1069 |
< |
else begin |
1070 |
< |
i := 1; |
1071 |
< |
Result := ''; |
1072 |
< |
while i <= Length(Value) do |
1073 |
< |
begin |
1074 |
< |
FieldName := ExtractFieldName(Value, i); |
1075 |
< |
if Result = '' then |
1076 |
< |
Result := QuoteIdentifier(Database.SQLDialect, FieldName) |
1077 |
< |
else |
1078 |
< |
Result := Result + ', ' + QuoteIdentifier(Database.SQLDialect, FieldName); |
1079 |
< |
end; |
1080 |
< |
end; |
1081 |
< |
end; |
1082 |
< |
|
1083 |
< |
procedure TIBTable.ExtractLinkFields; |
1084 |
< |
var |
1085 |
< |
i: Integer; |
1086 |
< |
DetailFieldNames: String; |
1087 |
< |
begin |
1088 |
< |
FMasterFieldsList.Clear; |
1089 |
< |
FDetailFieldsList.Clear; |
1090 |
< |
i := 1; |
1091 |
< |
while i <= Length(MasterFields) do |
1092 |
< |
FMasterFieldsList.Add(ExtractFieldName(MasterFields, i)); |
1093 |
< |
i := 1; |
1094 |
< |
if IndexFieldNames = '' then |
1095 |
< |
DetailFieldNames := FPrimaryIndexFields |
1096 |
< |
else |
1097 |
< |
DetailFieldNames := IndexFieldNames; |
1098 |
< |
while i <= Length(DetailFieldNames) do |
1099 |
< |
FDetailFieldsList.Add(ExtractFieldName(DetailFieldNames, i)); |
1100 |
< |
end; |
1101 |
< |
|
1102 |
< |
procedure TIBTable.GetDetailLinkFields(MasterFields, DetailFields: TList); |
1103 |
< |
var |
1104 |
< |
i: Integer; |
1105 |
< |
Idx: TIndexDef; |
1106 |
< |
begin |
1107 |
< |
MasterFields.Clear; |
1108 |
< |
DetailFields.Clear; |
1109 |
< |
if (MasterSource <> nil) and (MasterSource.DataSet <> nil) and |
1110 |
< |
(Self.MasterFields <> '') then |
1111 |
< |
begin |
1112 |
< |
Idx := nil; |
1113 |
< |
MasterSource.DataSet.GetFieldList(MasterFields, Self.MasterFields); |
1114 |
< |
UpdateIndexDefs; |
1115 |
< |
if IndexName <> '' then |
1116 |
< |
Idx := IndexDefs.Find(IndexName) |
1117 |
< |
else if IndexFieldNames <> '' then |
1118 |
< |
Idx := IndexDefs.GetIndexForFields(IndexFieldNames, False) |
1119 |
< |
else |
1120 |
< |
for i := 0 to IndexDefs.Count - 1 do |
1121 |
< |
if ixPrimary in IndexDefs[i].Options then |
1122 |
< |
begin |
1123 |
< |
Idx := IndexDefs[i]; |
1124 |
< |
break; |
1125 |
< |
end; |
1126 |
< |
if Idx <> nil then |
1127 |
< |
GetFieldList(DetailFields, Idx.Fields); |
1128 |
< |
end; |
1129 |
< |
end; |
1130 |
< |
|
1131 |
< |
procedure TIBTable.SetReadOnly(Value: Boolean); |
1132 |
< |
begin |
1133 |
< |
CheckInactive; |
1134 |
< |
FReadOnly := Value; |
1135 |
< |
end; |
1136 |
< |
|
1137 |
< |
procedure TIBTable.SetTableName(Value: String); |
1138 |
< |
begin |
1139 |
< |
if not (csReading in ComponentState) then |
1140 |
< |
begin |
1141 |
< |
CheckInactive; |
1142 |
< |
if Value <> FTableName then |
1143 |
< |
begin |
1144 |
< |
ResetSQLStatements; |
1145 |
< |
FRegenerateSQL := True; |
1146 |
< |
FTableName := Value; |
1147 |
< |
IndexName := ''; |
1148 |
< |
IndexFieldNames := ''; |
1149 |
< |
FPrimaryIndexFields := ''; |
1150 |
< |
DataEvent(dePropertyChange, 0); |
1151 |
< |
end; |
1152 |
< |
end |
1153 |
< |
else if Value <> FTableName then |
1154 |
< |
FTableName := Value; |
1155 |
< |
end; |
1156 |
< |
|
1157 |
< |
function TIBTable.GetIndexField(Index: Integer): TField; |
1158 |
< |
var |
1159 |
< |
I, Count: Integer; |
1160 |
< |
FieldNames, FieldName: String; |
1161 |
< |
begin |
1162 |
< |
Result := nil; |
1163 |
< |
FieldName := ''; |
1164 |
< |
FieldNames := IndexFieldNames; |
1165 |
< |
if FieldNames = '' then |
1166 |
< |
begin |
1167 |
< |
for I := 0 to IndexDefs.Count - 1 do |
1168 |
< |
if (IndexDefs[i].Name = FIndexName) then |
1169 |
< |
begin |
1170 |
< |
FieldNames := IndexDefs[i].Fields; |
1171 |
< |
break; |
1172 |
< |
end; |
1173 |
< |
end; |
1174 |
< |
for I := 0 to Index do |
1175 |
< |
begin |
1176 |
< |
Count := Pos(';', FieldNames); {mbcs OK} |
1177 |
< |
if Count = 0 then |
1178 |
< |
FieldName := FieldNames |
1179 |
< |
else begin |
1180 |
< |
FieldName := Copy(FieldNames, 0, Count - 1); |
1181 |
< |
System.Delete(FieldNames, 1, Count); |
1182 |
< |
end; |
1183 |
< |
end; |
1184 |
< |
if FieldName <> '' then |
1185 |
< |
Result := FieldByName(FieldName) |
1186 |
< |
else |
1187 |
< |
IBError(ibxeIndexFieldMissing, [nil]); |
1188 |
< |
end; |
1189 |
< |
|
1190 |
< |
|
1191 |
< |
procedure TIBTable.SetIndexField(Index: Integer; Value: TField); |
1192 |
< |
begin |
1193 |
< |
GetIndexField(Index).Assign(Value); |
1194 |
< |
end; |
1195 |
< |
|
1196 |
< |
function TIBTable.GetIndexFieldCount: Integer; |
1197 |
< |
var |
1198 |
< |
I, Index: Integer; |
1199 |
< |
FieldNames: String; |
1200 |
< |
done: Boolean; |
1201 |
< |
begin |
1202 |
< |
FieldNames := IndexFieldNames; |
1203 |
< |
if FieldNames = '' then |
1204 |
< |
begin |
1205 |
< |
for I := 0 to IndexDefs.Count - 1 do |
1206 |
< |
if (IndexDefs[i].Name = FIndexName) then |
1207 |
< |
begin |
1208 |
< |
FieldNames := IndexDefs[i].Fields; |
1209 |
< |
break; |
1210 |
< |
end; |
1211 |
< |
end; |
1212 |
< |
if FieldNames = '' then |
1213 |
< |
Result := 0 |
1214 |
< |
else |
1215 |
< |
begin |
1216 |
< |
done := False; |
1217 |
< |
Result := 1; |
1218 |
< |
while not done do |
1219 |
< |
begin |
1220 |
< |
Index := Pos(';', FieldNames); {mbcs ok} |
1221 |
< |
if Index <> 0 then |
1222 |
< |
begin |
1223 |
< |
System.Delete(FieldNames, 1, Index); |
1224 |
< |
Inc(Result); |
1225 |
< |
end else |
1226 |
< |
done := True; |
1227 |
< |
end; |
1228 |
< |
end; |
1229 |
< |
end; |
1230 |
< |
|
1231 |
< |
function TIBTable.GetTableNames: TStrings; |
1232 |
< |
begin |
1233 |
< |
FNameList.clear; |
1234 |
< |
GetTableNamesFromServer; |
1235 |
< |
Result := FNameList; |
1236 |
< |
end; |
1237 |
< |
|
1238 |
< |
procedure TIBTable.GetTableNamesFromServer; |
1239 |
< |
var |
1240 |
< |
Query : TIBSQL; |
1241 |
< |
begin |
1242 |
< |
if not (csReading in ComponentState) then begin |
1243 |
< |
ActivateConnection; |
1244 |
< |
Database.InternalTransaction.StartTransaction; |
1245 |
< |
Query := TIBSQL.Create(self); |
1246 |
< |
try |
1247 |
< |
Query.GoToFirstRecordOnExecute := False; |
1248 |
< |
Query.Database := DataBase; |
1249 |
< |
Query.Transaction := Database.InternalTransaction; |
1250 |
< |
if (TableTypes * [ttSystem, ttView] = [ttSystem, ttView]) then |
1251 |
< |
Query.SQL.Text := 'Select RDB$RELATION_NAME from RDB$RELATIONS' {do not localize} |
1252 |
< |
else if ttSystem in TableTypes then |
1253 |
< |
Query.SQL.Text := 'Select RDB$RELATION_NAME from RDB$RELATIONS' + {do not localize} |
1254 |
< |
' where RDB$VIEW_BLR is NULL' {do not localize} |
1255 |
< |
else if ttView in TableTypes then |
1256 |
< |
Query.SQL.Text := 'Select RDB$RELATION_NAME from RDB$RELATIONS' + {do not localize} |
1257 |
< |
' where RDB$SYSTEM_FLAG = 0' {do not localize} |
1258 |
< |
else |
1259 |
< |
Query.SQL.Text := 'Select RDB$RELATION_NAME from RDB$RELATIONS' + {do not localize} |
1260 |
< |
' where RDB$VIEW_BLR is NULL and RDB$SYSTEM_FLAG = 0'; {do not localize} |
1261 |
< |
Query.Prepare; |
1262 |
< |
Query.ExecQuery; |
1263 |
< |
while (not Query.EOF) and (Query.Next <> nil) do |
1264 |
< |
FNameList.Add (TrimRight(Query.Current[0].AsString)); |
1265 |
< |
finally |
1266 |
< |
Query.Free; |
1267 |
< |
Database.InternalTransaction.Commit; |
1268 |
< |
end; |
1269 |
< |
end; |
1270 |
< |
end; |
1271 |
< |
|
1272 |
< |
procedure TIBTable.SwitchToIndex(); |
1273 |
< |
begin |
1274 |
< |
FSwitchingIndex := True; |
1275 |
< |
InternalTableRefresh; |
1276 |
< |
FSwitchingIndex := False; |
1277 |
< |
end; |
1278 |
< |
|
1279 |
< |
procedure TIBTable.InternalTableRefresh(); |
1280 |
< |
var |
1281 |
< |
DBKey: TIBDBKey; |
1282 |
< |
begin |
1283 |
< |
CheckActive; |
1284 |
< |
DBKey := CurrentDBKey; |
1285 |
< |
FRegenerateSQL := True; |
1286 |
< |
Reopen; |
1287 |
< |
if DBKey.DBKey[0] <> 0 then |
1288 |
< |
InternalGotoDBKey(DBKey); |
1289 |
< |
end; |
1290 |
< |
|
1291 |
< |
procedure TIBTable.GenerateSQL; |
1292 |
< |
var |
1293 |
< |
i: Integer; |
1294 |
< |
SQL: TStrings; |
1295 |
< |
OrderByStr: string; |
1296 |
< |
bWhereClausePresent: Boolean; |
1297 |
< |
begin |
1298 |
< |
bWhereClausePresent := False; |
1299 |
< |
Database.CheckActive; |
1300 |
< |
Transaction.CheckInTransaction; |
1301 |
< |
if IndexDefs.Updated = False then |
1302 |
< |
IndexDefs.Update; |
1303 |
< |
if IndexFieldNames <> '' then |
1304 |
< |
OrderByStr := FormatFieldsList(IndexFieldNames) |
1305 |
< |
else if IndexName <> '' then |
1306 |
< |
OrderByStr := FormatFieldsList(IndexDefs[IndexDefs.Indexof (IndexName)].Fields) |
1307 |
< |
else if FDefaultIndex and (FPrimaryIndexFields <> '') then |
1308 |
< |
OrderByStr := FormatFieldsList(FPrimaryIndexFields); |
1309 |
< |
SQL := TStringList.Create; |
1310 |
< |
SQL.Text := 'select ' + {do not localize} |
1311 |
< |
QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' {do not localize} |
1312 |
< |
+ 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize} |
1313 |
< |
+ QuoteIdentifier(DataBase.SQLDialect, FTableName); |
1314 |
< |
if Filtered and (Filter <> '') then |
1315 |
< |
begin |
1316 |
< |
SQL.Text := SQL.Text + ' where ' + Filter; {do not localize} |
1317 |
< |
bWhereClausePresent := True; |
1318 |
< |
end; |
1319 |
< |
if (MasterSource <> nil) and (MasterSource.DataSet <> nil) and (MasterFields <> '') then |
1320 |
< |
begin |
1321 |
< |
if bWhereClausePresent then |
1322 |
< |
SQL.Text := SQL.Text + ' AND ' {do not localize} |
1323 |
< |
else |
1324 |
< |
SQL.Text := SQL.Text + ' WHERE '; {do not localize} |
1325 |
< |
ExtractLinkfields; |
1326 |
< |
if FDetailFieldsList.Count < FMasterFieldsList.Count then |
1327 |
< |
IBError(ibxeUnknownError, [nil]); |
1328 |
< |
for i := 0 to FMasterFieldsList.Count - 1 do |
1329 |
< |
begin |
1330 |
< |
if i > 0 then |
1331 |
< |
SQL.Text := SQL.Text + 'AND '; |
1332 |
< |
SQL.Text := SQL.Text + |
1333 |
< |
QuoteIdentifier(DataBase.SQLDialect, FDetailFieldsList.Strings[i]) + |
1334 |
< |
' = :' + |
1335 |
< |
QuoteIdentifier(DataBase.SQLDialect, FMasterFieldsList.Strings[i]); |
1336 |
< |
end; |
1337 |
< |
end; |
1338 |
< |
if OrderByStr <> '' then |
1339 |
< |
SQL.Text := SQL.Text + ' order by ' + OrderByStr; {do not localize} |
1340 |
< |
SelectSQL.Assign(SQL); |
1341 |
< |
RefreshSQL.Text := 'select ' + {do not localize} |
1342 |
< |
QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' {do not localize} |
1343 |
< |
+ 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize} |
1344 |
< |
+ QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
1345 |
< |
' where RDB$DB_KEY = :IBX_INTERNAL_DBKEY'; {do not localize} |
1346 |
< |
WhereDBKeyRefreshSQL.Assign(RefreshSQL); |
1347 |
< |
InternalPrepare; |
1348 |
< |
SQL.Free; |
1349 |
< |
end; |
1350 |
< |
|
1351 |
< |
procedure TIBTable.GenerateUpdateSQL; |
1352 |
< |
var |
1353 |
< |
InsertFieldList, InsertParamList, UpdateFieldList: string; |
1354 |
< |
WherePrimaryFieldList, WhereAllFieldList: string; |
1355 |
< |
|
1356 |
< |
procedure GenerateFieldLists; |
1357 |
< |
var |
1358 |
< |
I: Integer; |
1359 |
< |
begin |
1360 |
< |
for I := 0 to FieldDefs.Count - 1 do begin |
1361 |
< |
with FieldDefs[I] do begin |
1362 |
< |
if not (InternalCalcField or (faReadOnly in Attributes) or |
1363 |
< |
(DataType = ftUnknown)) then |
1364 |
< |
begin |
1365 |
< |
if ( InsertFieldList <> '' ) then begin |
1366 |
< |
InsertFieldList := InsertFieldList + ', '; |
1367 |
< |
InsertParamList := InsertParamList + ', '; |
1368 |
< |
UpdateFieldList := UpdateFieldList + ', '; |
1369 |
< |
if (DataType <> ftBlob) and (DataType <>ftMemo) then |
1370 |
< |
WhereAllFieldList := WhereAllFieldList + ' AND '; |
1371 |
< |
end; |
1372 |
< |
InsertFieldList := InsertFieldList + |
1373 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name); |
1374 |
< |
InsertParamList := InsertParamList + ':' + |
1375 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name); |
1376 |
< |
UpdateFieldList := UpdateFieldList + |
1377 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
1378 |
< |
' = :' + |
1379 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name); |
1380 |
< |
if (DataType <> ftBlob) and (DataType <>ftMemo) then |
1381 |
< |
WhereAllFieldList := WhereAllFieldList + |
1382 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + ' = :' + |
1383 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name);{do not localize} |
1384 |
< |
end; |
1385 |
< |
end; |
1386 |
< |
end; |
1387 |
< |
end; |
1388 |
< |
|
1389 |
< |
procedure GenerateWherePrimaryFieldList; |
1390 |
< |
var |
1391 |
< |
i: Integer; |
1392 |
< |
tmp: String; |
1393 |
< |
begin |
1394 |
< |
i := 1; |
1395 |
< |
while i <= Length(FPrimaryIndexFields) do |
1396 |
< |
begin |
1397 |
< |
tmp := ExtractFieldName(FPrimaryIndexFields, i); |
1398 |
< |
tmp := |
1399 |
< |
QuoteIdentifier(DataBase.SQLDialect, tmp) + ' = :' + |
1400 |
< |
QuoteIdentifier(DataBase.SQLDialect, tmp);{do not localize} |
1401 |
< |
if WherePrimaryFieldList <> '' then |
1402 |
< |
WherePrimaryFieldList := |
1403 |
< |
WherePrimaryFieldList + ' AND ' + tmp |
1404 |
< |
else |
1405 |
< |
WherePrimaryFieldList := tmp; |
1406 |
< |
end; |
1407 |
< |
end; |
1408 |
< |
|
1409 |
< |
begin |
1410 |
< |
if InternalGetUpdatable = False then |
1411 |
< |
FReadOnly := True |
1412 |
< |
else |
1413 |
< |
begin |
1414 |
< |
DeleteSQL.Text := 'delete from ' + {do not localize} |
1415 |
< |
QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
1416 |
< |
' where RDB$DB_KEY = ' + ':IBX_INTERNAL_DBKEY'; {do not localize} |
1417 |
< |
GenerateFieldLists; |
1418 |
< |
InsertSQL.Text := 'insert into ' + {do not localize} |
1419 |
< |
QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
1420 |
< |
' (' + InsertFieldList + {do not localize} |
1421 |
< |
') values (' + InsertParamList + ')'; {do not localize} |
1422 |
< |
ModifySQL.Text := 'update ' + |
1423 |
< |
QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
1424 |
< |
' set ' + UpdateFieldList + {do not localize} |
1425 |
< |
' where RDB$DB_KEY = :IBX_INTERNAL_DBKEY'; {do not localize} |
1426 |
< |
WhereAllRefreshSQL.Text := 'select ' + {do not localize} |
1427 |
< |
QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' |
1428 |
< |
+ 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize} |
1429 |
< |
+ QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
1430 |
< |
' where ' + WhereAllFieldList; {do not localize} |
1431 |
< |
if FPrimaryIndexFields <> '' then |
1432 |
< |
begin |
1433 |
< |
GenerateWherePrimaryFieldList; |
1434 |
< |
WherePrimaryRefreshSQL.Text := 'select ' + {do not localize} |
1435 |
< |
QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' {do not localize} |
1436 |
< |
+ 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize} |
1437 |
< |
+ QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
1438 |
< |
' where ' + WherePrimaryFieldList; {do not localize} |
1439 |
< |
end; |
1440 |
< |
try |
1441 |
< |
InternalPrepare; |
1442 |
< |
except |
1443 |
< |
FReadonly := True; |
1444 |
< |
end; |
1445 |
< |
end; |
1446 |
< |
end; |
1447 |
< |
|
1448 |
< |
procedure TIBTable.ResetSQLStatements; |
1449 |
< |
begin |
1450 |
< |
SelectSQL.Text := ''; |
1451 |
< |
DeleteSQL.Text := ''; |
1452 |
< |
InsertSQL.Text := ''; |
1453 |
< |
ModifySQL.Text := ''; |
1454 |
< |
RefreshSQL.Text := ''; |
1455 |
< |
end; |
1456 |
< |
|
1457 |
< |
procedure TIBTable.SetTableTypes( |
1458 |
< |
const Value: TIBTableTypes); |
1459 |
< |
begin |
1460 |
< |
FTableTypes := Value; |
1461 |
< |
end; |
1462 |
< |
|
1463 |
< |
function TIBTable.InternalGotoDBKey(DBKey: TIBDBKey): Boolean; |
1464 |
< |
|
1465 |
< |
function DBKeyCompare (DBKey1, DBKey2: TIBDBKey): Boolean; |
1466 |
< |
var |
1467 |
< |
I: Integer; |
1468 |
< |
begin |
1469 |
< |
for I := 0 to 7 do |
1470 |
< |
if (DBKey1.DBKey[i] <> DBKey2.DBKey[i]) then begin |
1471 |
< |
result := False; |
1472 |
< |
exit; |
1473 |
< |
end; |
1474 |
< |
result := True; |
1475 |
< |
end; |
1476 |
< |
begin |
1477 |
< |
CheckActive; |
1478 |
< |
DisableControls; |
1479 |
< |
try |
1480 |
< |
result := False; |
1481 |
< |
First; |
1482 |
< |
while ((not result) and (not EOF)) do begin |
1483 |
< |
if (DBKeyCompare (DBKey, PRecordData(GetActiveBuf)^.rdDBKey)) then |
1484 |
< |
result := True |
1485 |
< |
else |
1486 |
< |
Next; |
1487 |
< |
end; |
1488 |
< |
if not result then |
1489 |
< |
First |
1490 |
< |
else |
1491 |
< |
CursorPosChanged; |
1492 |
< |
finally |
1493 |
< |
EnableControls; |
1494 |
< |
end; |
1495 |
< |
end; |
1496 |
< |
|
1497 |
< |
function TIBTable.GetCurrentDBKey: TIBDBKey; |
1498 |
< |
var |
1499 |
< |
Buf: pChar; |
1500 |
< |
begin |
1501 |
< |
CheckActive; |
1502 |
< |
buf := GetActiveBuf; |
1503 |
< |
if Buf <> nil then |
1504 |
< |
Result := PRecordData(Buf)^.rdDBKey |
1505 |
< |
else |
1506 |
< |
Result.DBKey[0] := 0; |
1507 |
< |
end; |
1508 |
< |
|
1509 |
< |
procedure TIBTable.Reopen; |
1510 |
< |
begin |
1511 |
< |
DisableControls; |
1512 |
< |
try |
1513 |
< |
if Active then |
1514 |
< |
begin |
1515 |
< |
SetState(dsInactive); |
1516 |
< |
CloseCursor; |
1517 |
< |
OpenCursor(false); |
1518 |
< |
SetState(dsBrowse); |
1519 |
< |
end; |
1520 |
< |
finally |
1521 |
< |
EnableControls; |
1522 |
< |
end; |
1523 |
< |
end; |
1524 |
< |
|
1525 |
< |
end. |
1 |
> |
{************************************************************************} |
2 |
> |
{ } |
3 |
> |
{ Borland Delphi Visual Component Library } |
4 |
> |
{ InterBase Express core components } |
5 |
> |
{ } |
6 |
> |
{ Copyright (c) 1998-2000 Inprise Corporation } |
7 |
> |
{ } |
8 |
> |
{ InterBase Express is based in part on the product } |
9 |
> |
{ Free IB Components, written by Gregory H. Deatz for } |
10 |
> |
{ Hoagland, Longo, Moran, Dunst & Doukas Company. } |
11 |
> |
{ Free IB Components is used under license. } |
12 |
> |
{ } |
13 |
> |
{ The contents of this file are subject to the InterBase } |
14 |
> |
{ Public License Version 1.0 (the "License"); you may not } |
15 |
> |
{ use this file except in compliance with the License. You } |
16 |
> |
{ may obtain a copy of the License at http://www.Inprise.com/IPL.html } |
17 |
> |
{ Software distributed under the License is distributed on } |
18 |
> |
{ an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either } |
19 |
> |
{ express or implied. See the License for the specific language } |
20 |
> |
{ governing rights and limitations under the License. } |
21 |
> |
{ The Original Code was created by InterBase Software Corporation } |
22 |
> |
{ and its successors. } |
23 |
> |
{ Portions created by Inprise Corporation are Copyright (C) Inprise } |
24 |
> |
{ Corporation. All Rights Reserved. } |
25 |
> |
{ Contributor(s): Jeff Overcash } |
26 |
> |
{ } |
27 |
> |
{ IBX For Lazarus (Firebird Express) } |
28 |
> |
{ Contributor: Tony Whyman, MWA Software http://www.mwasoftware.co.uk } |
29 |
> |
{ Portions created by MWA Software are copyright McCallum Whyman } |
30 |
> |
{ Associates Ltd 2011 } |
31 |
> |
{ } |
32 |
> |
{************************************************************************} |
33 |
> |
|
34 |
> |
unit IBTable; |
35 |
> |
|
36 |
> |
{$Mode Delphi} |
37 |
> |
|
38 |
> |
interface |
39 |
> |
|
40 |
> |
uses SysUtils, Classes, DB, IB, IBCustomDataSet, |
41 |
> |
IBSQL, IBUtils; |
42 |
> |
|
43 |
> |
type |
44 |
> |
|
45 |
> |
{ TIBTable } |
46 |
> |
|
47 |
> |
TIBTableType = (ttSystem, ttView); |
48 |
> |
TIBTableTypes = set of TIBTableType; |
49 |
> |
TIndexName = String; |
50 |
> |
|
51 |
> |
TIBTable = class; |
52 |
> |
|
53 |
> |
TIBTable = class(TIBCustomDataSet) |
54 |
> |
private |
55 |
> |
FSystemTable: Boolean; |
56 |
> |
FMultiTableView: Boolean; |
57 |
> |
FMasterLink: TMasterDataLink; |
58 |
> |
FMasterFieldsList: TStringList; |
59 |
> |
FDetailFieldsList: TStringList; |
60 |
> |
FStoreDefs: Boolean; |
61 |
> |
FIndexDefs: TIndexDefs; |
62 |
> |
FDefaultIndex: Boolean; |
63 |
> |
FReadOnly: Boolean; |
64 |
> |
FFieldsIndex: Boolean; |
65 |
> |
FTableName: String; |
66 |
> |
FIndexName: TIndexName; |
67 |
> |
FRegenerateSQL: Boolean; |
68 |
> |
FNameList: TStrings; |
69 |
> |
FSwitchingIndex: Boolean; |
70 |
> |
FPrimaryIndexFields: string; |
71 |
> |
FTableTypes: TIBTableTypes; |
72 |
> |
WhereAllRefreshSQL: TStrings; |
73 |
> |
WhereDBKeyRefreshSQL: TStrings; |
74 |
> |
WherePrimaryRefreshSQL: TStrings; |
75 |
> |
|
76 |
> |
function GetIndexFieldCount: Integer; |
77 |
> |
function GetIndexField(Index: Integer): TField; |
78 |
> |
procedure MasterChanged(Sender: TObject); |
79 |
> |
procedure MasterDisabled(Sender: TObject); |
80 |
> |
procedure SetDataSource(Value: TDataSource); |
81 |
> |
procedure SetIndexField(Index: Integer; Value: TField); |
82 |
> |
procedure SetIndexFieldNames(const Value: string); |
83 |
> |
procedure GenerateSQL; |
84 |
> |
procedure GenerateUpdateSQL; |
85 |
> |
procedure SwitchToIndex(); |
86 |
> |
procedure InternalTableRefresh(); |
87 |
> |
function GetTableNames: TStrings; |
88 |
> |
procedure GetTableNamesFromServer; |
89 |
> |
procedure SetTableTypes( |
90 |
> |
const Value: TIBTableTypes); |
91 |
> |
function InternalGotoDBKey(DBKey: TIBDBKey): Boolean; |
92 |
> |
function FormatFieldsList(Value: string): string; |
93 |
> |
function GetCurrentDBKey: TIBDBKey; |
94 |
> |
function InternalGetUpdatable: Boolean; |
95 |
> |
function GetExists: Boolean; |
96 |
> |
procedure SetIndexDefs(Value: TIndexDefs); |
97 |
> |
procedure ExtractLinkFields; |
98 |
> |
function FieldDefsStored: Boolean; |
99 |
> |
function IndexDefsStored: Boolean; |
100 |
> |
function GetMasterFields: string; |
101 |
> |
procedure SetMasterFields(const Value: string); |
102 |
> |
function GetIndexFieldNames: string; |
103 |
> |
function GetIndexName: string; |
104 |
> |
procedure SetIndexName(const Value: string); |
105 |
> |
procedure SetParams; |
106 |
> |
procedure SetReadOnly(Value: Boolean); |
107 |
> |
procedure SetTableName(Value: String); |
108 |
> |
procedure SetIndex(const Value: string; FieldsIndex: Boolean); |
109 |
> |
procedure ResetSQLStatements; |
110 |
> |
procedure Reopen; |
111 |
> |
|
112 |
> |
protected |
113 |
> |
|
114 |
> |
procedure DoOnNewRecord; override; |
115 |
> |
procedure GetIndexParams(const IndexName: string; FieldsIndex: Boolean; |
116 |
> |
var IndexedName: string); |
117 |
> |
function GetCanModify: Boolean; override; |
118 |
> |
procedure UpdateIndexDefs; override; |
119 |
> |
procedure DataEvent(Event: TDataEvent; Info: Ptrint); override; |
120 |
> |
procedure DefChanged(Sender: TObject); virtual; |
121 |
> |
function GetDataSource: TDataSource; override; |
122 |
> |
procedure InitFieldDefs; override; |
123 |
> |
procedure InternalClose; override; |
124 |
> |
procedure InternalOpen; override; |
125 |
> |
procedure InternalRefresh; override; |
126 |
> |
procedure SetFiltered(Value: Boolean); override; |
127 |
> |
procedure SetFilterText(const Value: string); override; |
128 |
> |
procedure SetFilterOptions(Value: TFilterOptions); override; |
129 |
> |
procedure InternalRefreshRow; override; |
130 |
> |
|
131 |
> |
public |
132 |
> |
constructor Create(AOwner: TComponent); override; |
133 |
> |
destructor Destroy; override; |
134 |
> |
procedure AddIndex(const Name, Fields: string; Options: TIndexOptions; |
135 |
> |
const DescFields: string = ''); |
136 |
> |
procedure CreateTable; |
137 |
> |
procedure DeleteIndex(const Name: string); |
138 |
> |
procedure DeleteTable; |
139 |
> |
procedure EmptyTable; |
140 |
> |
procedure GetDetailLinkFields(MasterFields, DetailFields: TList); virtual; |
141 |
> |
procedure GetIndexNames(List: TStrings); |
142 |
> |
procedure GotoCurrent(Table: TIBTable); |
143 |
> |
property CurrentDBKey: TIBDBKey read GetCurrentDBKey; |
144 |
> |
property Exists: Boolean read GetExists; |
145 |
> |
property IndexFieldCount: Integer read GetIndexFieldCount; |
146 |
> |
property IndexFields[Index: Integer]: TField read GetIndexField write SetIndexField; |
147 |
> |
property TableNames: TStrings read GetTableNames; |
148 |
> |
|
149 |
> |
published |
150 |
> |
property AutoCommit; |
151 |
> |
property Active; |
152 |
> |
property BufferChunks; |
153 |
> |
property CachedUpdates; |
154 |
> |
property DataSetCloseAction; |
155 |
> |
// property Constraints stored ConstraintsStored; |
156 |
> |
property DefaultIndex: Boolean read FDefaultIndex write FDefaultIndex default True; |
157 |
> |
property FieldDefs stored FieldDefsStored; |
158 |
> |
property Filter; |
159 |
> |
property Filtered; |
160 |
> |
property IndexDefs: TIndexDefs read FIndexDefs write SetIndexDefs stored IndexDefsStored; |
161 |
> |
property IndexFieldNames: string read GetIndexFieldNames write SetIndexFieldNames; |
162 |
> |
property IndexName: string read GetIndexName write SetIndexName; |
163 |
> |
property MasterFields: string read GetMasterFields write SetMasterFields; |
164 |
> |
property MasterSource: TDataSource read GetDataSource write SetDataSource; |
165 |
> |
property ReadOnly: Boolean read FReadOnly write SetReadOnly default False; |
166 |
> |
property StoreDefs: Boolean read FStoreDefs write FStoreDefs default False; |
167 |
> |
property TableName: String read FTableName write SetTableName; |
168 |
> |
property TableTypes: TIBTableTypes read FTableTypes write SetTableTypes default []; |
169 |
> |
property UpdateObject; |
170 |
> |
property UniDirectional; |
171 |
> |
|
172 |
> |
property BeforeDatabaseDisconnect; |
173 |
> |
property AfterDatabaseDisconnect; |
174 |
> |
property DatabaseFree; |
175 |
> |
property BeforeTransactionEnd; |
176 |
> |
property AfterTransactionEnd; |
177 |
> |
property TransactionFree; |
178 |
> |
property OnFilterRecord; |
179 |
> |
end; |
180 |
> |
|
181 |
> |
implementation |
182 |
> |
|
183 |
> |
{ TIBTable } |
184 |
> |
|
185 |
> |
constructor TIBTable.Create(AOwner: TComponent); |
186 |
> |
begin |
187 |
> |
inherited Create(AOwner); |
188 |
> |
FNameList := TStringList.Create; |
189 |
> |
FSwitchingIndex := False; |
190 |
> |
FIndexDefs := TIndexDefs.Create(Self); |
191 |
> |
WhereAllRefreshSQL := TStringList.Create; |
192 |
> |
WhereDBKeyRefreshSQL := TStringList.Create; |
193 |
> |
WherePrimaryRefreshSQL := TStringList.Create; |
194 |
> |
FDefaultIndex := True; |
195 |
> |
FRegenerateSQL := True; |
196 |
> |
FMasterFieldsList := TStringList.Create; |
197 |
> |
FDetailFieldsList := TStringList.Create; |
198 |
> |
FMasterLink := TMasterDataLink.Create(Self); |
199 |
> |
FMasterLink.OnMasterChange := MasterChanged; |
200 |
> |
FMasterLink.OnMasterDisable := MasterDisabled; |
201 |
> |
QRefresh.OnSQLChanging := nil; |
202 |
> |
QDelete.OnSQLChanging := nil; |
203 |
> |
QInsert.OnSQLChanging := nil; |
204 |
> |
QModify.OnSQLChanging := nil; |
205 |
> |
end; |
206 |
> |
|
207 |
> |
destructor TIBTable.Destroy; |
208 |
> |
begin |
209 |
> |
FNameList.Free; |
210 |
> |
FIndexDefs.Free; |
211 |
> |
FMasterFieldsList.Free; |
212 |
> |
FDetailFieldsList.Free; |
213 |
> |
FMasterLink.Free; |
214 |
> |
WhereAllRefreshSQL.Free; |
215 |
> |
WhereDBKeyRefreshSQL.Free; |
216 |
> |
WherePrimaryRefreshSQL.Free; |
217 |
> |
inherited Destroy; |
218 |
> |
end; |
219 |
> |
|
220 |
> |
procedure TIBTable.InternalClose; |
221 |
> |
begin |
222 |
> |
DataEvent(dePropertyChange, 0); |
223 |
> |
inherited InternalClose; |
224 |
> |
end; |
225 |
> |
|
226 |
> |
procedure TIBTable.InternalOpen; |
227 |
> |
begin |
228 |
> |
if FTableName = '' then IBError(ibxeNoTableName, [nil]); |
229 |
> |
ActivateConnection; |
230 |
> |
ActivateTransaction; |
231 |
> |
if FRegenerateSQL then |
232 |
> |
begin |
233 |
> |
InternalUnprepare; |
234 |
> |
GenerateSQL; |
235 |
> |
if not FReadOnly then |
236 |
> |
GenerateUpdateSQL; |
237 |
> |
FRegenerateSQL := False; |
238 |
> |
end; |
239 |
> |
{ writeln(SelectSQL.Text); |
240 |
> |
writeln(InsertSQL.Text); |
241 |
> |
writeln(DeleteSQL.Text); |
242 |
> |
writeln(ModifySQL.Text); } |
243 |
> |
SetParams; |
244 |
> |
inherited InternalOpen; |
245 |
> |
end; |
246 |
> |
|
247 |
> |
procedure TIBTable.InternalRefresh; |
248 |
> |
var |
249 |
> |
DBKey: TIBDBKey; |
250 |
> |
begin |
251 |
> |
DBKey := CurrentDBKey; |
252 |
> |
Reopen; |
253 |
> |
if DBKey.DBKey[0] <> 0 then |
254 |
> |
InternalGotoDBKey(DBKey); |
255 |
> |
end; |
256 |
> |
|
257 |
> |
procedure TIBTable.SetFiltered(Value: Boolean); |
258 |
> |
begin |
259 |
> |
if(Filtered <> Value) then |
260 |
> |
begin |
261 |
> |
inherited SetFiltered(value); |
262 |
> |
if Active then |
263 |
> |
InternalTableRefresh; |
264 |
> |
end |
265 |
> |
else |
266 |
> |
inherited SetFiltered(value); |
267 |
> |
end; |
268 |
> |
|
269 |
> |
procedure TIBTable.SetFilterText(const Value: string); |
270 |
> |
begin |
271 |
> |
if Filtered and (Value <> Filter) then |
272 |
> |
begin |
273 |
> |
inherited SetFilterText(value); |
274 |
> |
InternalTableRefresh; |
275 |
> |
end |
276 |
> |
else |
277 |
> |
inherited SetFilterText(value); |
278 |
> |
end; |
279 |
> |
|
280 |
> |
procedure TIBTable.SetFilterOptions(Value: TFilterOptions); |
281 |
> |
begin |
282 |
> |
if Value <> [] then |
283 |
> |
IBError(ibxeNotSupported, [nil]); |
284 |
> |
end; |
285 |
> |
|
286 |
> |
procedure TIBTable.InternalRefreshRow; |
287 |
> |
begin |
288 |
> |
if CurrentDBKey.DBKey[0] <> 0 then |
289 |
> |
QRefresh.SQL.Assign(WhereDBKeyRefreshSQL) |
290 |
> |
else if WherePrimaryRefreshSQL.Text <> '' then |
291 |
> |
QRefresh.SQL.Assign(WherePrimaryRefreshSQL) |
292 |
> |
else |
293 |
> |
QRefresh.SQL.Assign(WhereAllRefreshSQL); |
294 |
> |
inherited InternalRefreshRow; |
295 |
> |
end; |
296 |
> |
|
297 |
> |
procedure TIBTable.DefChanged(Sender: TObject); |
298 |
> |
begin |
299 |
> |
StoreDefs := True; |
300 |
> |
end; |
301 |
> |
|
302 |
> |
procedure TIBTable.InitFieldDefs; |
303 |
> |
var |
304 |
> |
DidActivate: Boolean; |
305 |
> |
Query: TIBSQL; |
306 |
> |
begin |
307 |
> |
if FTableName = '' then IBError(ibxeNoTableName, [nil]); |
308 |
> |
if (InternalPrepared) then |
309 |
> |
InternalInitFieldDefs |
310 |
> |
else |
311 |
> |
begin |
312 |
> |
{Get the field defs from a simple query on the table} |
313 |
> |
ActivateConnection; |
314 |
> |
Query := TIBSQL.Create(self); |
315 |
> |
try |
316 |
> |
Query.Database := DataBase; |
317 |
> |
Query.Transaction := Database.InternalTransaction; |
318 |
> |
DidActivate := false; |
319 |
> |
Query.SQL.Text := 'Select * from ' + QuoteIdentifier(DataBase.SQLDialect, FTableName); |
320 |
> |
with Query.Transaction do |
321 |
> |
begin |
322 |
> |
if not InTransaction then |
323 |
> |
begin |
324 |
> |
StartTransaction; |
325 |
> |
DidActivate := true |
326 |
> |
end; |
327 |
> |
end; |
328 |
> |
Query.Prepare; |
329 |
> |
if DidActivate then |
330 |
> |
Query.Transaction.Rollback; |
331 |
> |
FieldDefsFromQuery(Query); |
332 |
> |
finally |
333 |
> |
Query.Free; |
334 |
> |
end; |
335 |
> |
end; |
336 |
> |
end; |
337 |
> |
|
338 |
> |
{ Index / Ranges / Keys } |
339 |
> |
|
340 |
> |
procedure TIBTable.AddIndex(const Name, Fields: string; Options: TIndexOptions; |
341 |
> |
const DescFields: string); |
342 |
> |
var |
343 |
> |
Query: TIBSQL; |
344 |
> |
FieldList: string; |
345 |
> |
begin |
346 |
> |
FieldDefs.Update; |
347 |
> |
if Active then begin |
348 |
> |
CheckBrowseMode; |
349 |
> |
CursorPosChanged; |
350 |
> |
end; |
351 |
> |
Query := TIBSQL.Create(self); |
352 |
> |
try |
353 |
> |
Query.Database := DataBase; |
354 |
> |
Query.Transaction := Transaction; |
355 |
> |
FieldList := FormatFieldsList(Fields); |
356 |
> |
if (ixPrimary in Options) then |
357 |
> |
begin |
358 |
> |
Query.SQL.Text := 'Alter Table ' + {do not localize} |
359 |
> |
QuoteIdentifier(Database.SQLDialect, FTableName) + |
360 |
> |
' Add CONSTRAINT ' + {do not localize} |
361 |
> |
QuoteIdentifier(Database.SQLDialect, Name) |
362 |
> |
+ ' Primary Key (' + {do not localize} |
363 |
> |
FormatFieldsList(Fields) + |
364 |
> |
')'; |
365 |
> |
end |
366 |
> |
else if ([ixUnique, ixDescending] * Options = [ixUnique, ixDescending]) then |
367 |
> |
Query.SQL.Text := 'Create unique Descending Index ' + {do not localize} |
368 |
> |
QuoteIdentifier(Database.SQLDialect, Name) + |
369 |
> |
' on ' + {do not localize} |
370 |
> |
QuoteIdentifier(Database.SQLDialect, FTableName) + |
371 |
> |
' (' + FieldList + ')' |
372 |
> |
else if (ixUnique in Options) then |
373 |
> |
Query.SQL.Text := 'Create unique Index ' + {do not localize} |
374 |
> |
QuoteIdentifier(Database.SQLDialect, Name) + |
375 |
> |
' on ' + {do not localize} |
376 |
> |
QuoteIdentifier(Database.SQLDialect, FTableName) + |
377 |
> |
' (' + FieldList + ')' |
378 |
> |
else if (ixDescending in Options) then |
379 |
> |
Query.SQL.Text := 'Create Descending Index ' + {do not localize} |
380 |
> |
QuoteIdentifier(Database.SQLDialect, Name) + |
381 |
> |
' on ' + {do not localize} |
382 |
> |
QuoteIdentifier(Database.SQLDialect, FTableName) + |
383 |
> |
' (' + FieldList + ')' |
384 |
> |
else |
385 |
> |
Query.SQL.Text := 'Create Index ' + {do not localize} |
386 |
> |
QuoteIdentifier(Database.SQLDialect, Name) + |
387 |
> |
' on ' + {do not localize} |
388 |
> |
QuoteIdentifier(Database.SQLDialect, FTableName) + |
389 |
> |
' (' + FieldList + ')'; |
390 |
> |
Query.Prepare; |
391 |
> |
Query.ExecQuery; |
392 |
> |
IndexDefs.Updated := False; |
393 |
> |
finally |
394 |
> |
Query.free |
395 |
> |
end; |
396 |
> |
end; |
397 |
> |
|
398 |
> |
procedure TIBTable.DeleteIndex(const Name: string); |
399 |
> |
var |
400 |
> |
Query: TIBSQL; |
401 |
> |
|
402 |
> |
procedure DeleteByIndex; |
403 |
> |
begin |
404 |
> |
Query := TIBSQL.Create(self); |
405 |
> |
try |
406 |
> |
Query.Database := DataBase; |
407 |
> |
Query.Transaction := Transaction; |
408 |
> |
Query.SQL.Text := 'Drop index ' + {do not localize} |
409 |
> |
QuoteIdentifier(Database.SQLDialect, Name); |
410 |
> |
Query.Prepare; |
411 |
> |
Query.ExecQuery; |
412 |
> |
IndexDefs.Updated := False; |
413 |
> |
finally |
414 |
> |
Query.Free; |
415 |
> |
end; |
416 |
> |
end; |
417 |
> |
|
418 |
> |
function DeleteByConstraint: Boolean; |
419 |
> |
begin |
420 |
> |
Result := False; |
421 |
> |
Query := TIBSQL.Create(self); |
422 |
> |
try |
423 |
> |
Query.Database := DataBase; |
424 |
> |
Query.Transaction := Transaction; |
425 |
> |
Query.SQL.Text := 'Select ''foo'' from RDB$RELATION_CONSTRAINTS ' + |
426 |
> |
'where RDB$RELATION_NAME = ' + |
427 |
> |
'''' + |
428 |
> |
FormatIdentifierValue(Database.SQLDialect, |
429 |
> |
QuoteIdentifier(DataBase.SQLDialect, FTableName)) + |
430 |
> |
''' ' + |
431 |
> |
' AND RDB$CONSTRAINT_NAME = ' + |
432 |
> |
'''' + |
433 |
> |
FormatIdentifierValue(Database.SQLDialect, |
434 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name)) + |
435 |
> |
''' ' + |
436 |
> |
'AND RDB$CONSTRAINT_TYPE = ''PRIMARY KEY'''; |
437 |
> |
Query.Prepare; |
438 |
> |
Query.ExecQuery; |
439 |
> |
if not Query.EOF then |
440 |
> |
begin |
441 |
> |
Query.Close; |
442 |
> |
Query.SQL.Text := 'Alter Table ' + {do not localize} |
443 |
> |
QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
444 |
> |
' Drop Constraint ' + |
445 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name); |
446 |
> |
Query.Prepare; |
447 |
> |
Query.ExecQuery; |
448 |
> |
IndexDefs.Updated := False; |
449 |
> |
Result := True; |
450 |
> |
end; |
451 |
> |
finally |
452 |
> |
Query.Free; |
453 |
> |
end; |
454 |
> |
end; |
455 |
> |
|
456 |
> |
procedure DeleteByKey; |
457 |
> |
begin |
458 |
> |
Query := TIBSQL.Create(self); |
459 |
> |
try |
460 |
> |
Query.Database := DataBase; |
461 |
> |
Query.Transaction := Transaction; |
462 |
> |
Query.SQL.Text := 'Select RDB$CONSTRAINT_NAME from RDB$RELATION_CONSTRAINTS ' + |
463 |
> |
'where RDB$RELATION_NAME = ' + |
464 |
> |
'''' + |
465 |
> |
FormatIdentifierValue(Database.SQLDialect, |
466 |
> |
QuoteIdentifier(DataBase.SQLDialect, FTableName)) + |
467 |
> |
''' ' + |
468 |
> |
'AND RDB$INDEX_NAME = ' + |
469 |
> |
'''' + |
470 |
> |
FormatIdentifierValue(Database.SQLDialect, |
471 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name)) + |
472 |
> |
''' ' + |
473 |
> |
'AND RDB$CONSTRAINT_TYPE = ''PRIMARY KEY'''; |
474 |
> |
Query.Prepare; |
475 |
> |
Query.ExecQuery; |
476 |
> |
if not Query.EOF then |
477 |
> |
begin |
478 |
> |
Query.Close; |
479 |
> |
Query.SQL.Text := 'Alter Table ' + {do not localize} |
480 |
> |
QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
481 |
> |
' Drop Constraint ' + |
482 |
> |
QuoteIdentifier(DataBase.SQLDialect, Query.Current.ByName('RDB$CONSTRAINT_NAME').AsString); |
483 |
> |
Query.Prepare; |
484 |
> |
Query.ExecQuery; |
485 |
> |
IndexDefs.Updated := False; |
486 |
> |
end; |
487 |
> |
finally |
488 |
> |
Query.Free; |
489 |
> |
end; |
490 |
> |
end; |
491 |
> |
|
492 |
> |
begin |
493 |
> |
if Active then |
494 |
> |
CheckBrowseMode; |
495 |
> |
IndexDefs.Update; |
496 |
> |
if (Pos('RDB$PRIMARY', Name) <> 0 ) then {do not localize} {mbcs ok} |
497 |
> |
DeleteByKey |
498 |
> |
else if not DeleteByConstraint then |
499 |
> |
DeleteByIndex; |
500 |
> |
end; |
501 |
> |
|
502 |
> |
function TIBTable.GetIndexFieldNames: string; |
503 |
> |
begin |
504 |
> |
if FFieldsIndex then Result := FIndexName else Result := ''; |
505 |
> |
end; |
506 |
> |
|
507 |
> |
function TIBTable.GetIndexName: string; |
508 |
> |
begin |
509 |
> |
if FFieldsIndex then Result := '' else Result := FIndexName; |
510 |
> |
end; |
511 |
> |
|
512 |
> |
procedure TIBTable.GetIndexNames(List: TStrings); |
513 |
> |
begin |
514 |
> |
IndexDefs.Update; |
515 |
> |
IndexDefs.GetItemNames(List); |
516 |
> |
end; |
517 |
> |
|
518 |
> |
procedure TIBTable.GetIndexParams(const IndexName: string; |
519 |
> |
FieldsIndex: Boolean; var IndexedName: string); |
520 |
> |
var |
521 |
> |
IndexStr: TIndexName; |
522 |
> |
begin |
523 |
> |
if IndexName <> '' then |
524 |
> |
begin |
525 |
> |
IndexDefs.Update; |
526 |
> |
IndexStr := IndexName; |
527 |
> |
if FieldsIndex then |
528 |
> |
IndexStr := IndexDefs.FindIndexForFields(IndexName).Name; |
529 |
> |
end; |
530 |
> |
IndexedName := IndexStr; |
531 |
> |
end; |
532 |
> |
|
533 |
> |
procedure TIBTable.SetIndexDefs(Value: TIndexDefs); |
534 |
> |
begin |
535 |
> |
IndexDefs.Assign(Value); |
536 |
> |
end; |
537 |
> |
|
538 |
> |
procedure TIBTable.SetIndex(const Value: string; FieldsIndex: Boolean); |
539 |
> |
begin |
540 |
> |
if Active then CheckBrowseMode; |
541 |
> |
if (FIndexName <> Value) or (FFieldsIndex <> FieldsIndex) then |
542 |
> |
begin |
543 |
> |
FIndexName := Value; |
544 |
> |
FFieldsIndex := FieldsIndex; |
545 |
> |
if Active then |
546 |
> |
begin |
547 |
> |
SwitchToIndex; |
548 |
> |
end; |
549 |
> |
end; |
550 |
> |
end; |
551 |
> |
|
552 |
> |
procedure TIBTable.SetIndexFieldNames(const Value: string); |
553 |
> |
begin |
554 |
> |
SetIndex(Value, Value <> ''); |
555 |
> |
end; |
556 |
> |
|
557 |
> |
procedure TIBTable.SetIndexName(const Value: string); |
558 |
> |
begin |
559 |
> |
SetIndex(Value, False); |
560 |
> |
end; |
561 |
> |
|
562 |
> |
procedure TIBTable.UpdateIndexDefs; |
563 |
> |
var |
564 |
> |
Opts: TIndexOptions; |
565 |
> |
Flds: string; |
566 |
> |
Query, SubQuery: TIBSQL; |
567 |
> |
fn: string; |
568 |
> |
aField: TFieldDef; |
569 |
> |
begin |
570 |
> |
if not (csReading in ComponentState) then begin |
571 |
> |
if not Active and not FSwitchingIndex then |
572 |
> |
FieldDefs.Update; |
573 |
> |
IndexDefs.Clear; |
574 |
> |
Database.InternalTransaction.StartTransaction; |
575 |
> |
Query := TIBSQL.Create(self); |
576 |
> |
try |
577 |
> |
FPrimaryIndexFields := ''; |
578 |
> |
Query.GoToFirstRecordOnExecute := False; |
579 |
> |
Query.Database := DataBase; |
580 |
> |
Query.Transaction := Database.InternalTransaction; |
581 |
> |
Query.SQL.Text := |
582 |
> |
'Select I.RDB$INDEX_NAME, I.RDB$UNIQUE_FLAG, I.RDB$INDEX_TYPE, ' + {do not localize} |
583 |
> |
'I.RDB$SEGMENT_COUNT, S.RDB$FIELD_NAME from RDB$INDICES I, ' + {do not localize} |
584 |
> |
'RDB$INDEX_SEGMENTS S where I.RDB$INDEX_NAME = S.RDB$INDEX_NAME '+ {do not localize} |
585 |
> |
'and I.RDB$RELATION_NAME = ' + '''' + {do not localize} |
586 |
> |
FormatIdentifierValue(Database.SQLDialect, |
587 |
> |
QuoteIdentifier(DataBase.SQLDialect, FTableName)) + ''''; |
588 |
> |
Query.Prepare; |
589 |
> |
Query.ExecQuery; |
590 |
> |
while (not Query.EOF) and (Query.Next <> nil) do |
591 |
> |
begin |
592 |
> |
with IndexDefs.AddIndexDef do |
593 |
> |
begin |
594 |
> |
Name := TrimRight(Query.Current.ByName('RDB$INDEX_NAME').AsString); {do not localize} |
595 |
> |
Opts := []; |
596 |
> |
if Pos ('RDB$PRIMARY', Name) = 1 then Include(Opts, ixPrimary); {do not localize} {mbcs ok} |
597 |
> |
if Query.Current.ByName('RDB$UNIQUE_FLAG').AsInteger = 1 then Include(Opts, ixUnique); {do not localize} |
598 |
> |
if Query.Current.ByName('RDB$INDEX_TYPE').AsInteger = 2 then Include(Opts, ixDescending); {do not localize} |
599 |
> |
Options := Opts; |
600 |
> |
if (Query.Current.ByName('RDB$SEGMENT_COUNT').AsInteger = 1) then {do not localize} |
601 |
> |
begin |
602 |
> |
fn := Trim(Query.Current.ByName('RDB$FIELD_NAME').AsString); {do not localize} |
603 |
> |
aField := GetFieldDefFromAlias(fn); |
604 |
> |
if assigned(aField) then |
605 |
> |
Fields := aField.Name |
606 |
> |
else |
607 |
> |
Fields := fn; |
608 |
> |
end |
609 |
> |
else begin |
610 |
> |
SubQuery := TIBSQL.Create(self); |
611 |
> |
try |
612 |
> |
SubQuery.GoToFirstRecordOnExecute := False; |
613 |
> |
SubQuery.Database := DataBase; |
614 |
> |
SubQuery.Transaction := Database.InternalTransaction; |
615 |
> |
SubQuery.SQL.Text := |
616 |
> |
'Select RDB$FIELD_NAME from RDB$INDEX_SEGMENTS where RDB$INDEX_NAME = ' + {do not localize} |
617 |
> |
'''' + |
618 |
> |
FormatIdentifierValue(Database.SQLDialect, |
619 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name)) + |
620 |
> |
'''' + 'ORDER BY RDB$FIELD_POSITION'; {do not localize} |
621 |
> |
SubQuery.Prepare; |
622 |
> |
SubQuery.ExecQuery; |
623 |
> |
Flds := ''; |
624 |
> |
while (not SubQuery.EOF) and (SubQuery.Next <> nil) do |
625 |
> |
begin |
626 |
> |
fn := TrimRight(SubQuery.Current.ByName('RDB$FIELD_NAME').AsString); {do not localize} |
627 |
> |
aField := GetFieldDefFromAlias(fn); |
628 |
> |
if assigned(aField) then |
629 |
> |
fn := aField.Name; |
630 |
> |
if (Flds = '') then |
631 |
> |
Flds := fn |
632 |
> |
else begin |
633 |
> |
Query.Next; |
634 |
> |
Flds := Flds + ';' + fn; |
635 |
> |
end; |
636 |
> |
end; |
637 |
> |
Fields := Flds; |
638 |
> |
finally |
639 |
> |
SubQuery.Free; |
640 |
> |
end; |
641 |
> |
end; |
642 |
> |
if (ixDescending in Opts) then |
643 |
> |
DescFields := Fields; |
644 |
> |
if ixPrimary in Opts then |
645 |
> |
FPrimaryIndexFields := Fields; |
646 |
> |
end; |
647 |
> |
end; |
648 |
> |
finally |
649 |
> |
Query.Free; |
650 |
> |
Database.InternalTransaction.Commit; |
651 |
> |
end; |
652 |
> |
end; |
653 |
> |
end; |
654 |
> |
|
655 |
> |
function TIBTable.GetExists: Boolean; |
656 |
> |
var |
657 |
> |
Query: TIBSQL; |
658 |
> |
begin |
659 |
> |
Result := Active; |
660 |
> |
if Result or (TableName = '') then Exit; |
661 |
> |
Database.InternalTransaction.StartTransaction; |
662 |
> |
Query := TIBSQL.Create(self); |
663 |
> |
try |
664 |
> |
Query.Database := DataBase; |
665 |
> |
Query.Transaction := Database.InternalTransaction; |
666 |
> |
Query.SQL.Text := |
667 |
> |
'Select USER from RDB$RELATIONS where RDB$RELATION_NAME = ' + {do not localize} |
668 |
> |
'''' + |
669 |
> |
FormatIdentifierValue(Database.SQLDialect, |
670 |
> |
QuoteIdentifier(DataBase.SQLDialect, FTableName)) + ''''; |
671 |
> |
Query.Prepare; |
672 |
> |
Query.ExecQuery; |
673 |
> |
Result := not Query.EOF; |
674 |
> |
finally |
675 |
> |
Query.Free; |
676 |
> |
Database.InternalTransaction.Commit; |
677 |
> |
end; |
678 |
> |
end; |
679 |
> |
|
680 |
> |
procedure TIBTable.GotoCurrent(Table: TIBTable); |
681 |
> |
begin |
682 |
> |
CheckBrowseMode; |
683 |
> |
Table.CheckBrowseMode; |
684 |
> |
if (Database <> Table.Database) or |
685 |
> |
(CompareText(TableName, Table.TableName) <> 0) then |
686 |
> |
IBError(ibxeTableNameMismatch, [nil]); |
687 |
> |
Table.UpdateCursorPos; |
688 |
> |
InternalGotoDBKey(Table.CurrentDBKey); |
689 |
> |
DoBeforeScroll; |
690 |
> |
Resync([rmExact, rmCenter]); |
691 |
> |
DoAfterScroll; |
692 |
> |
end; |
693 |
> |
|
694 |
> |
|
695 |
> |
procedure TIBTable.CreateTable; |
696 |
> |
var |
697 |
> |
FieldList: string; |
698 |
> |
|
699 |
> |
procedure InitFieldsList; |
700 |
> |
var |
701 |
> |
I: Integer; |
702 |
> |
begin |
703 |
> |
InitFieldDefsFromFields; |
704 |
> |
for I := 0 to FieldDefs.Count - 1 do begin |
705 |
> |
if ( I > 0) then |
706 |
> |
FieldList := FieldList + ', '; |
707 |
> |
with FieldDefs[I] do |
708 |
> |
begin |
709 |
> |
case DataType of |
710 |
> |
ftString: |
711 |
> |
FieldList := FieldList + |
712 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
713 |
> |
' VARCHAR(' + IntToStr(Size) + ')'; {do not localize} |
714 |
> |
ftFixedChar: |
715 |
> |
FieldList := FieldList + |
716 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
717 |
> |
' CHAR(' + IntToStr(Size) + ')'; {do not localize} |
718 |
> |
ftBoolean: |
719 |
> |
FieldList := FieldList + |
720 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
721 |
> |
' BOOLEAN'; {do not localize} |
722 |
> |
ftSmallint, ftWord: |
723 |
> |
FieldList := FieldList + |
724 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
725 |
> |
' SMALLINT'; {do not localize} |
726 |
> |
ftInteger: |
727 |
> |
FieldList := FieldList + |
728 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
729 |
> |
' INTEGER'; {do not localize} |
730 |
> |
ftFloat, ftCurrency: |
731 |
> |
FieldList := FieldList + |
732 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
733 |
> |
' DOUBLE PRECISION'; {do not localize} |
734 |
> |
ftBCD: begin |
735 |
> |
if (Database.SQLDialect = 1) then begin |
736 |
> |
if (Precision > 9) then |
737 |
> |
IBError(ibxeFieldUnsupportedType,[nil]); |
738 |
> |
if (Precision <= 4) then |
739 |
> |
Precision := 9; |
740 |
> |
end; |
741 |
> |
if (Precision <= 4 ) then |
742 |
> |
FieldList := FieldList + |
743 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
744 |
> |
' Numeric(18, 4)' {do not localize} |
745 |
> |
else |
746 |
> |
FieldList := FieldList + |
747 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
748 |
> |
' Numeric(' + IntToStr(Precision) + ', 4)'; {do not localize} |
749 |
> |
end; |
750 |
> |
ftDate: |
751 |
> |
FieldList := FieldList + |
752 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
753 |
> |
' DATE'; {do not localize} |
754 |
> |
ftTime: |
755 |
> |
FieldList := FieldList + |
756 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
757 |
> |
' TIME'; {do not localize} |
758 |
> |
ftDateTime: |
759 |
> |
if (Database.SQLDialect = 1) then |
760 |
> |
FieldList := FieldList + |
761 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
762 |
> |
' DATE' {do not localize} |
763 |
> |
else |
764 |
> |
FieldList := FieldList + |
765 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
766 |
> |
' TIMESTAMP'; {do not localize} |
767 |
> |
ftLargeInt: |
768 |
> |
if (Database.SQLDialect = 1) then |
769 |
> |
IBError(ibxeFieldUnsupportedType,[nil]) |
770 |
> |
else |
771 |
> |
FieldList := FieldList + |
772 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
773 |
> |
' Numeric(18, 0)'; {do not localize} |
774 |
> |
ftBlob, ftMemo: |
775 |
> |
FieldList := FieldList + |
776 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
777 |
> |
' BLOB SUB_TYPE 1'; {do not localize} |
778 |
> |
ftBytes, ftVarBytes, ftGraphic..ftTypedBinary: |
779 |
> |
FieldList := FieldList + |
780 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
781 |
> |
' BLOB SUB_TYPE 0'; {do not localize} |
782 |
> |
ftUnknown, ftADT, ftArray, ftReference, ftDataSet, |
783 |
> |
ftCursor, ftWideString, ftAutoInc: |
784 |
> |
IBError(ibxeFieldUnsupportedType,[nil]); |
785 |
> |
else |
786 |
> |
IBError(ibxeFieldUnsupportedType,[nil]); |
787 |
> |
end; |
788 |
> |
if faRequired in Attributes then |
789 |
> |
FieldList := FieldList + ' NOT NULL'; {do not localize} |
790 |
> |
end; |
791 |
> |
end; |
792 |
> |
end; |
793 |
> |
|
794 |
> |
procedure InternalCreateTable; |
795 |
> |
var |
796 |
> |
I: Integer; |
797 |
> |
Query: TIBSQL; |
798 |
> |
begin |
799 |
> |
if (FieldList = '') then |
800 |
> |
IBError(ibxeFieldUnsupportedType,[nil]); |
801 |
> |
Query := TIBSQL.Create(self); |
802 |
> |
try |
803 |
> |
Query.Database := Database; |
804 |
> |
Query.transaction := Transaction; |
805 |
> |
Query.SQL.Text := 'Create Table ' + |
806 |
> |
QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
807 |
> |
' (' + FieldList; {do not localize} |
808 |
> |
for I := 0 to IndexDefs.Count - 1 do |
809 |
> |
with IndexDefs[I] do |
810 |
> |
if ixPrimary in Options then |
811 |
> |
begin |
812 |
> |
Query.SQL.Text := Query.SQL.Text + ', CONSTRAINT ' + |
813 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
814 |
> |
' Primary Key (' + |
815 |
> |
FormatFieldsList(Fields) + |
816 |
> |
')'; |
817 |
> |
end; |
818 |
> |
Query.SQL.Text := Query.SQL.Text + ')'; |
819 |
> |
Query.Prepare; |
820 |
> |
Query.ExecQuery; |
821 |
> |
finally |
822 |
> |
Query.Free; |
823 |
> |
end; |
824 |
> |
end; |
825 |
> |
|
826 |
> |
procedure InternalCreateIndex; |
827 |
> |
var |
828 |
> |
I: Integer; |
829 |
> |
begin |
830 |
> |
for I := 0 to IndexDefs.Count - 1 do |
831 |
> |
with IndexDefs[I] do |
832 |
> |
if not (ixPrimary in Options) then |
833 |
> |
AddIndex(Name, Fields, Options); |
834 |
> |
end; |
835 |
> |
|
836 |
> |
begin |
837 |
> |
CheckInactive; |
838 |
> |
InitFieldsList; |
839 |
> |
InternalCreateTable; |
840 |
> |
InternalCreateIndex; |
841 |
> |
end; |
842 |
> |
|
843 |
> |
procedure TIBTable.DeleteTable; |
844 |
> |
var |
845 |
> |
Query: TIBSQL; |
846 |
> |
begin |
847 |
> |
CheckInactive; |
848 |
> |
Query := TIBSQL.Create(self); |
849 |
> |
try |
850 |
> |
Query.Database := DataBase; |
851 |
> |
Query.Transaction := Transaction; |
852 |
> |
Query.SQL.Text := 'drop table ' + {do not localize} |
853 |
> |
QuoteIdentifier(DataBase.SQLDialect, FTableName); |
854 |
> |
Query.Prepare; |
855 |
> |
Query.ExecQuery; |
856 |
> |
finally |
857 |
> |
Query.Free; |
858 |
> |
end; |
859 |
> |
end; |
860 |
> |
|
861 |
> |
procedure TIBTable.EmptyTable; |
862 |
> |
var |
863 |
> |
Query: TIBSQL; |
864 |
> |
begin |
865 |
> |
if Active then |
866 |
> |
CheckBrowseMode; |
867 |
> |
Query := TIBSQL.Create(self); |
868 |
> |
try |
869 |
> |
Query.Database := DataBase; |
870 |
> |
Query.Transaction := Transaction; |
871 |
> |
Query.SQL.Text := 'delete from ' + {do not localize} |
872 |
> |
QuoteIdentifier(DataBase.SQLDialect, FTableName); |
873 |
> |
Query.Prepare; |
874 |
> |
Query.ExecQuery; |
875 |
> |
if Active then |
876 |
> |
begin |
877 |
> |
ClearBuffers; |
878 |
> |
DataEvent(deDataSetChange, 0); |
879 |
> |
end; |
880 |
> |
finally |
881 |
> |
Query.Free; |
882 |
> |
end; |
883 |
> |
end; |
884 |
> |
|
885 |
> |
procedure TIBTable.DataEvent(Event: TDataEvent; Info: Ptrint); |
886 |
> |
begin |
887 |
> |
if Event = dePropertyChange then begin |
888 |
> |
IndexDefs.Updated := False; |
889 |
> |
FRegenerateSQL := True; |
890 |
> |
end; |
891 |
> |
inherited DataEvent(Event, Info); |
892 |
> |
end; |
893 |
> |
|
894 |
> |
{ Informational & Property } |
895 |
> |
|
896 |
> |
function TIBTable.GetCanModify: Boolean; |
897 |
> |
begin |
898 |
> |
Result := True; |
899 |
> |
if (FTableName = '') or FReadOnly |
900 |
> |
or FSystemTable or FMultiTableView then |
901 |
> |
Result := False; |
902 |
> |
end; |
903 |
> |
|
904 |
> |
function TIBTable.InternalGetUpdatable: Boolean; |
905 |
> |
var |
906 |
> |
Query : TIBSQL; |
907 |
> |
begin |
908 |
> |
Database.InternalTransaction.StartTransaction; |
909 |
> |
Query := TIBSQL.Create(self); |
910 |
> |
try |
911 |
> |
Query.Database := DataBase; |
912 |
> |
Query.Transaction := Database.InternalTransaction; |
913 |
> |
Query.SQL.Text := 'Select RDB$SYSTEM_FLAG, RDB$DBKEY_LENGTH ' + {do not localize} |
914 |
> |
'from RDB$RELATIONS where RDB$RELATION_NAME = ' + {do not localize} |
915 |
> |
'''' + |
916 |
> |
FormatIdentifierValue(Database.SQLDialect, |
917 |
> |
QuoteIdentifier(DataBase.SQLDialect, FTableName)) + ''''; |
918 |
> |
Query.Prepare; |
919 |
> |
Query.ExecQuery; |
920 |
> |
if (Query.Current[0].AsInteger <> 0) or |
921 |
> |
(Query.Current[1].AsInteger <> 8) then |
922 |
> |
Result := False |
923 |
> |
else |
924 |
> |
Result := True; |
925 |
> |
finally |
926 |
> |
Query.Free; |
927 |
> |
Database.InternalTransaction.Commit; |
928 |
> |
end; |
929 |
> |
end; |
930 |
> |
|
931 |
> |
function TIBTable.FieldDefsStored: Boolean; |
932 |
> |
begin |
933 |
> |
Result := StoreDefs and (FieldDefs.Count > 0); |
934 |
> |
end; |
935 |
> |
|
936 |
> |
function TIBTable.IndexDefsStored: Boolean; |
937 |
> |
begin |
938 |
> |
Result := StoreDefs and (IndexDefs.Count > 0); |
939 |
> |
end; |
940 |
> |
|
941 |
> |
procedure TIBTable.SetParams; |
942 |
> |
var |
943 |
> |
i: Integer; |
944 |
> |
begin |
945 |
> |
if (MasterSource = nil) or (MasterSource.DataSet = nil) or |
946 |
> |
(not MasterSource.DataSet.Active) or (FMasterFieldsList.Count = 0) then |
947 |
> |
exit; |
948 |
> |
for i := 0 to FMasterFieldsList.Count - 1 do |
949 |
> |
QSelect.Params.ByName(FMasterFieldsList.Strings[i]).Value := |
950 |
> |
MasterSource.DataSet.FieldByName(FMasterFieldsList.Strings[i]).Value; |
951 |
> |
end; |
952 |
> |
|
953 |
> |
procedure TIBTable.MasterChanged(Sender: TObject); |
954 |
> |
begin |
955 |
> |
CheckBrowseMode; |
956 |
> |
SetParams; |
957 |
> |
ReQuery; |
958 |
> |
end; |
959 |
> |
|
960 |
> |
procedure TIBTable.MasterDisabled(Sender: TObject); |
961 |
> |
begin |
962 |
> |
DataEvent(dePropertyChange, 0); |
963 |
> |
ReQuery; |
964 |
> |
end; |
965 |
> |
|
966 |
> |
function TIBTable.GetDataSource: TDataSource; |
967 |
> |
begin |
968 |
> |
Result := FMasterLink.DataSource; |
969 |
> |
end; |
970 |
> |
|
971 |
> |
procedure TIBTable.SetDataSource(Value: TDataSource); |
972 |
> |
begin |
973 |
> |
if IsLinkedTo(Value) then IBError(ibxeCircularDataLink, [Self]); |
974 |
> |
if FMasterLink.DataSource <> Value then |
975 |
> |
DataEvent(dePropertyChange, 0); |
976 |
> |
FMasterLink.DataSource := Value; |
977 |
> |
end; |
978 |
> |
|
979 |
> |
function TIBTable.GetMasterFields: string; |
980 |
> |
begin |
981 |
> |
Result := FMasterLink.FieldNames; |
982 |
> |
end; |
983 |
> |
|
984 |
> |
procedure TIBTable.SetMasterFields(const Value: string); |
985 |
> |
begin |
986 |
> |
if FMasterLink.FieldNames <> Value then |
987 |
> |
DataEvent(dePropertyChange, 0); |
988 |
> |
FMasterLink.FieldNames := Value; |
989 |
> |
end; |
990 |
> |
|
991 |
> |
procedure TIBTable.DoOnNewRecord; |
992 |
> |
var |
993 |
> |
I: Integer; |
994 |
> |
begin |
995 |
> |
if FMasterLink.Active and (FMasterLink.Fields.Count > 0) then |
996 |
> |
for I := 0 to FMasterLink.Fields.Count - 1 do |
997 |
> |
IndexFields[I] := TField(FMasterLink.Fields[I]); |
998 |
> |
inherited DoOnNewRecord; |
999 |
> |
end; |
1000 |
> |
|
1001 |
> |
function TIBTable.FormatFieldsList(Value: String): String; |
1002 |
> |
var |
1003 |
> |
FieldName: string; |
1004 |
> |
i: Integer; |
1005 |
> |
begin |
1006 |
> |
if Database.SQLDialect = 1 then begin |
1007 |
> |
Value := QuoteIdentifier(Database.SQLDialect, Value); |
1008 |
> |
Result := StringReplace (Value, ';', ', ', [rfReplaceAll]); |
1009 |
> |
end |
1010 |
> |
else begin |
1011 |
> |
i := 1; |
1012 |
> |
Result := ''; |
1013 |
> |
while i <= Length(Value) do |
1014 |
> |
begin |
1015 |
> |
FieldName := ExtractFieldName(Value, i); |
1016 |
> |
if Result = '' then |
1017 |
> |
Result := QuoteIdentifier(Database.SQLDialect, FieldName) |
1018 |
> |
else |
1019 |
> |
Result := Result + ', ' + QuoteIdentifier(Database.SQLDialect, FieldName); |
1020 |
> |
end; |
1021 |
> |
end; |
1022 |
> |
end; |
1023 |
> |
|
1024 |
> |
procedure TIBTable.ExtractLinkFields; |
1025 |
> |
var |
1026 |
> |
i: Integer; |
1027 |
> |
DetailFieldNames: String; |
1028 |
> |
begin |
1029 |
> |
FMasterFieldsList.Clear; |
1030 |
> |
FDetailFieldsList.Clear; |
1031 |
> |
i := 1; |
1032 |
> |
while i <= Length(MasterFields) do |
1033 |
> |
FMasterFieldsList.Add(ExtractFieldName(MasterFields, i)); |
1034 |
> |
i := 1; |
1035 |
> |
if IndexFieldNames = '' then |
1036 |
> |
DetailFieldNames := FPrimaryIndexFields |
1037 |
> |
else |
1038 |
> |
DetailFieldNames := IndexFieldNames; |
1039 |
> |
while i <= Length(DetailFieldNames) do |
1040 |
> |
FDetailFieldsList.Add(ExtractFieldName(DetailFieldNames, i)); |
1041 |
> |
end; |
1042 |
> |
|
1043 |
> |
procedure TIBTable.GetDetailLinkFields(MasterFields, DetailFields: TList); |
1044 |
> |
var |
1045 |
> |
i: Integer; |
1046 |
> |
Idx: TIndexDef; |
1047 |
> |
begin |
1048 |
> |
MasterFields.Clear; |
1049 |
> |
DetailFields.Clear; |
1050 |
> |
if (MasterSource <> nil) and (MasterSource.DataSet <> nil) and |
1051 |
> |
(Self.MasterFields <> '') then |
1052 |
> |
begin |
1053 |
> |
Idx := nil; |
1054 |
> |
MasterSource.DataSet.GetFieldList(MasterFields, Self.MasterFields); |
1055 |
> |
UpdateIndexDefs; |
1056 |
> |
if IndexName <> '' then |
1057 |
> |
Idx := IndexDefs.Find(IndexName) |
1058 |
> |
else if IndexFieldNames <> '' then |
1059 |
> |
Idx := IndexDefs.GetIndexForFields(IndexFieldNames, False) |
1060 |
> |
else |
1061 |
> |
for i := 0 to IndexDefs.Count - 1 do |
1062 |
> |
if ixPrimary in IndexDefs[i].Options then |
1063 |
> |
begin |
1064 |
> |
Idx := IndexDefs[i]; |
1065 |
> |
break; |
1066 |
> |
end; |
1067 |
> |
if Idx <> nil then |
1068 |
> |
GetFieldList(DetailFields, Idx.Fields); |
1069 |
> |
end; |
1070 |
> |
end; |
1071 |
> |
|
1072 |
> |
procedure TIBTable.SetReadOnly(Value: Boolean); |
1073 |
> |
begin |
1074 |
> |
CheckInactive; |
1075 |
> |
FReadOnly := Value; |
1076 |
> |
end; |
1077 |
> |
|
1078 |
> |
procedure TIBTable.SetTableName(Value: String); |
1079 |
> |
begin |
1080 |
> |
if not (csReading in ComponentState) then |
1081 |
> |
begin |
1082 |
> |
CheckInactive; |
1083 |
> |
if Value <> FTableName then |
1084 |
> |
begin |
1085 |
> |
ResetSQLStatements; |
1086 |
> |
FRegenerateSQL := True; |
1087 |
> |
FTableName := Value; |
1088 |
> |
IndexName := ''; |
1089 |
> |
IndexFieldNames := ''; |
1090 |
> |
FPrimaryIndexFields := ''; |
1091 |
> |
DataEvent(dePropertyChange, 0); |
1092 |
> |
end; |
1093 |
> |
end |
1094 |
> |
else if Value <> FTableName then |
1095 |
> |
FTableName := Value; |
1096 |
> |
end; |
1097 |
> |
|
1098 |
> |
function TIBTable.GetIndexField(Index: Integer): TField; |
1099 |
> |
var |
1100 |
> |
I, Count: Integer; |
1101 |
> |
FieldNames, FieldName: String; |
1102 |
> |
begin |
1103 |
> |
Result := nil; |
1104 |
> |
FieldName := ''; |
1105 |
> |
FieldNames := IndexFieldNames; |
1106 |
> |
if FieldNames = '' then |
1107 |
> |
begin |
1108 |
> |
for I := 0 to IndexDefs.Count - 1 do |
1109 |
> |
if (IndexDefs[i].Name = FIndexName) then |
1110 |
> |
begin |
1111 |
> |
FieldNames := IndexDefs[i].Fields; |
1112 |
> |
break; |
1113 |
> |
end; |
1114 |
> |
end; |
1115 |
> |
for I := 0 to Index do |
1116 |
> |
begin |
1117 |
> |
Count := Pos(';', FieldNames); {mbcs OK} |
1118 |
> |
if Count = 0 then |
1119 |
> |
FieldName := FieldNames |
1120 |
> |
else begin |
1121 |
> |
FieldName := Copy(FieldNames, 0, Count - 1); |
1122 |
> |
System.Delete(FieldNames, 1, Count); |
1123 |
> |
end; |
1124 |
> |
end; |
1125 |
> |
if FieldName <> '' then |
1126 |
> |
Result := FieldByName(FieldName) |
1127 |
> |
else |
1128 |
> |
IBError(ibxeIndexFieldMissing, [nil]); |
1129 |
> |
end; |
1130 |
> |
|
1131 |
> |
|
1132 |
> |
procedure TIBTable.SetIndexField(Index: Integer; Value: TField); |
1133 |
> |
begin |
1134 |
> |
GetIndexField(Index).Assign(Value); |
1135 |
> |
end; |
1136 |
> |
|
1137 |
> |
function TIBTable.GetIndexFieldCount: Integer; |
1138 |
> |
var |
1139 |
> |
I, Index: Integer; |
1140 |
> |
FieldNames: String; |
1141 |
> |
done: Boolean; |
1142 |
> |
begin |
1143 |
> |
FieldNames := IndexFieldNames; |
1144 |
> |
if FieldNames = '' then |
1145 |
> |
begin |
1146 |
> |
for I := 0 to IndexDefs.Count - 1 do |
1147 |
> |
if (IndexDefs[i].Name = FIndexName) then |
1148 |
> |
begin |
1149 |
> |
FieldNames := IndexDefs[i].Fields; |
1150 |
> |
break; |
1151 |
> |
end; |
1152 |
> |
end; |
1153 |
> |
if FieldNames = '' then |
1154 |
> |
Result := 0 |
1155 |
> |
else |
1156 |
> |
begin |
1157 |
> |
done := False; |
1158 |
> |
Result := 1; |
1159 |
> |
while not done do |
1160 |
> |
begin |
1161 |
> |
Index := Pos(';', FieldNames); {mbcs ok} |
1162 |
> |
if Index <> 0 then |
1163 |
> |
begin |
1164 |
> |
System.Delete(FieldNames, 1, Index); |
1165 |
> |
Inc(Result); |
1166 |
> |
end else |
1167 |
> |
done := True; |
1168 |
> |
end; |
1169 |
> |
end; |
1170 |
> |
end; |
1171 |
> |
|
1172 |
> |
function TIBTable.GetTableNames: TStrings; |
1173 |
> |
begin |
1174 |
> |
FNameList.clear; |
1175 |
> |
GetTableNamesFromServer; |
1176 |
> |
Result := FNameList; |
1177 |
> |
end; |
1178 |
> |
|
1179 |
> |
procedure TIBTable.GetTableNamesFromServer; |
1180 |
> |
var |
1181 |
> |
Query : TIBSQL; |
1182 |
> |
begin |
1183 |
> |
if not (csReading in ComponentState) then begin |
1184 |
> |
ActivateConnection; |
1185 |
> |
Database.InternalTransaction.StartTransaction; |
1186 |
> |
Query := TIBSQL.Create(self); |
1187 |
> |
try |
1188 |
> |
Query.GoToFirstRecordOnExecute := False; |
1189 |
> |
Query.Database := DataBase; |
1190 |
> |
Query.Transaction := Database.InternalTransaction; |
1191 |
> |
if (TableTypes * [ttSystem, ttView] = [ttSystem, ttView]) then |
1192 |
> |
Query.SQL.Text := 'Select RDB$RELATION_NAME from RDB$RELATIONS' {do not localize} |
1193 |
> |
else if ttSystem in TableTypes then |
1194 |
> |
Query.SQL.Text := 'Select RDB$RELATION_NAME from RDB$RELATIONS' + {do not localize} |
1195 |
> |
' where RDB$VIEW_BLR is NULL' {do not localize} |
1196 |
> |
else if ttView in TableTypes then |
1197 |
> |
Query.SQL.Text := 'Select RDB$RELATION_NAME from RDB$RELATIONS' + {do not localize} |
1198 |
> |
' where RDB$SYSTEM_FLAG = 0' {do not localize} |
1199 |
> |
else |
1200 |
> |
Query.SQL.Text := 'Select RDB$RELATION_NAME from RDB$RELATIONS' + {do not localize} |
1201 |
> |
' where RDB$VIEW_BLR is NULL and RDB$SYSTEM_FLAG = 0'; {do not localize} |
1202 |
> |
Query.Prepare; |
1203 |
> |
Query.ExecQuery; |
1204 |
> |
while (not Query.EOF) and (Query.Next <> nil) do |
1205 |
> |
FNameList.Add (TrimRight(Query.Current[0].AsString)); |
1206 |
> |
finally |
1207 |
> |
Query.Free; |
1208 |
> |
Database.InternalTransaction.Commit; |
1209 |
> |
end; |
1210 |
> |
end; |
1211 |
> |
end; |
1212 |
> |
|
1213 |
> |
procedure TIBTable.SwitchToIndex(); |
1214 |
> |
begin |
1215 |
> |
FSwitchingIndex := True; |
1216 |
> |
InternalTableRefresh; |
1217 |
> |
FSwitchingIndex := False; |
1218 |
> |
end; |
1219 |
> |
|
1220 |
> |
procedure TIBTable.InternalTableRefresh(); |
1221 |
> |
var |
1222 |
> |
DBKey: TIBDBKey; |
1223 |
> |
begin |
1224 |
> |
CheckActive; |
1225 |
> |
DBKey := CurrentDBKey; |
1226 |
> |
FRegenerateSQL := True; |
1227 |
> |
Reopen; |
1228 |
> |
if DBKey.DBKey[0] <> 0 then |
1229 |
> |
InternalGotoDBKey(DBKey); |
1230 |
> |
end; |
1231 |
> |
|
1232 |
> |
procedure TIBTable.GenerateSQL; |
1233 |
> |
var |
1234 |
> |
i: Integer; |
1235 |
> |
SQL: TStrings; |
1236 |
> |
OrderByStr: string; |
1237 |
> |
bWhereClausePresent: Boolean; |
1238 |
> |
fn: string; |
1239 |
> |
aField: TFieldDef; |
1240 |
> |
begin |
1241 |
> |
bWhereClausePresent := False; |
1242 |
> |
Database.CheckActive; |
1243 |
> |
Transaction.CheckInTransaction; |
1244 |
> |
if IndexDefs.Updated = False then |
1245 |
> |
IndexDefs.Update; |
1246 |
> |
if IndexFieldNames <> '' then |
1247 |
> |
OrderByStr := FormatFieldsList(IndexFieldNames) |
1248 |
> |
else if IndexName <> '' then |
1249 |
> |
OrderByStr := FormatFieldsList(IndexDefs[IndexDefs.Indexof (IndexName)].Fields) |
1250 |
> |
else if FDefaultIndex and (FPrimaryIndexFields <> '') then |
1251 |
> |
OrderByStr := FormatFieldsList(FPrimaryIndexFields); |
1252 |
> |
SQL := TStringList.Create; |
1253 |
> |
SQL.Text := 'select ' + {do not localize} |
1254 |
> |
QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' {do not localize} |
1255 |
> |
+ 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize} |
1256 |
> |
+ QuoteIdentifier(DataBase.SQLDialect, FTableName); |
1257 |
> |
if Filtered and (Filter <> '') then |
1258 |
> |
begin |
1259 |
> |
SQL.Text := SQL.Text + ' where ' + Filter; {do not localize} |
1260 |
> |
bWhereClausePresent := True; |
1261 |
> |
end; |
1262 |
> |
if (MasterSource <> nil) and (MasterSource.DataSet <> nil) and (MasterFields <> '') then |
1263 |
> |
begin |
1264 |
> |
if bWhereClausePresent then |
1265 |
> |
SQL.Text := SQL.Text + ' AND ' {do not localize} |
1266 |
> |
else |
1267 |
> |
SQL.Text := SQL.Text + ' WHERE '; {do not localize} |
1268 |
> |
ExtractLinkfields; |
1269 |
> |
if FDetailFieldsList.Count < FMasterFieldsList.Count then |
1270 |
> |
IBError(ibxeUnknownError, [nil]); |
1271 |
> |
for i := 0 to FMasterFieldsList.Count - 1 do |
1272 |
> |
begin |
1273 |
> |
if i > 0 then |
1274 |
> |
SQL.Text := SQL.Text + 'AND '; |
1275 |
> |
aField := FieldDefs.Find(FDetailFieldsList.Strings[i]); |
1276 |
> |
if assigned(aField) then |
1277 |
> |
fn := GetDBAliasName(aField.FieldNo) |
1278 |
> |
else |
1279 |
> |
fn := FDetailFieldsList.Strings[i]; {something wrong if we get here - but should word} |
1280 |
> |
SQL.Text := SQL.Text + |
1281 |
> |
QuoteIdentifier(DataBase.SQLDialect, fn) + ' = :' + FMasterFieldsList.Strings[i]; |
1282 |
> |
end; |
1283 |
> |
end; |
1284 |
> |
if OrderByStr <> '' then |
1285 |
> |
SQL.Text := SQL.Text + ' order by ' + OrderByStr; {do not localize} |
1286 |
> |
SelectSQL.Assign(SQL); |
1287 |
> |
RefreshSQL.Text := 'select ' + {do not localize} |
1288 |
> |
QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' {do not localize} |
1289 |
> |
+ 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize} |
1290 |
> |
+ QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
1291 |
> |
' where RDB$DB_KEY = :IBX_INTERNAL_DBKEY'; {do not localize} |
1292 |
> |
WhereDBKeyRefreshSQL.Assign(RefreshSQL); |
1293 |
> |
InternalPrepare; |
1294 |
> |
SQL.Free; |
1295 |
> |
end; |
1296 |
> |
|
1297 |
> |
procedure TIBTable.GenerateUpdateSQL; |
1298 |
> |
var |
1299 |
> |
InsertFieldList, InsertParamList, UpdateFieldList: string; |
1300 |
> |
WherePrimaryFieldList, WhereAllFieldList: string; |
1301 |
> |
|
1302 |
> |
procedure GenerateFieldLists; |
1303 |
> |
var |
1304 |
> |
I: Integer; |
1305 |
> |
begin |
1306 |
> |
for I := 0 to FieldDefs.Count - 1 do begin |
1307 |
> |
with FieldDefs[I] do begin |
1308 |
> |
if not (InternalCalcField or (faReadOnly in Attributes) or |
1309 |
> |
(DataType = ftUnknown)) then |
1310 |
> |
begin |
1311 |
> |
if ( InsertFieldList <> '' ) then begin |
1312 |
> |
InsertFieldList := InsertFieldList + ', '; |
1313 |
> |
InsertParamList := InsertParamList + ', '; |
1314 |
> |
UpdateFieldList := UpdateFieldList + ', '; |
1315 |
> |
if (DataType <> ftBlob) and (DataType <>ftMemo) then |
1316 |
> |
WhereAllFieldList := WhereAllFieldList + ' AND '; |
1317 |
> |
end; |
1318 |
> |
InsertFieldList := InsertFieldList + |
1319 |
> |
QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)); |
1320 |
> |
InsertParamList := InsertParamList + ':' + Name; |
1321 |
> |
UpdateFieldList := UpdateFieldList + |
1322 |
> |
QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) + |
1323 |
> |
' = :' + Name; |
1324 |
> |
if (DataType <> ftBlob) and (DataType <>ftMemo) then |
1325 |
> |
WhereAllFieldList := WhereAllFieldList + |
1326 |
> |
QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) + ' = :' + Name; |
1327 |
> |
end; |
1328 |
> |
end; |
1329 |
> |
end; |
1330 |
> |
end; |
1331 |
> |
|
1332 |
> |
procedure GenerateWherePrimaryFieldList; |
1333 |
> |
var |
1334 |
> |
i: Integer; |
1335 |
> |
tmp, fn: String; |
1336 |
> |
aField: TFieldDef; |
1337 |
> |
begin |
1338 |
> |
i := 1; |
1339 |
> |
while i <= Length(FPrimaryIndexFields) do |
1340 |
> |
begin |
1341 |
> |
tmp := ExtractFieldName(FPrimaryIndexFields, i); |
1342 |
> |
aField := FieldDefs.Find(tmp); |
1343 |
> |
if assigned(aField) then |
1344 |
> |
fn := GetDBAliasName(aField.FieldNo) |
1345 |
> |
else |
1346 |
> |
fn := tmp; {something wrong if we get here - but will work in most cases} |
1347 |
> |
tmp := |
1348 |
> |
QuoteIdentifier(DataBase.SQLDialect, fn) + ' = :' + |
1349 |
> |
QuoteIdentifier(DataBase.SQLDialect, tmp);{do not localize} |
1350 |
> |
if WherePrimaryFieldList <> '' then |
1351 |
> |
WherePrimaryFieldList := |
1352 |
> |
WherePrimaryFieldList + ' AND ' + tmp |
1353 |
> |
else |
1354 |
> |
WherePrimaryFieldList := tmp; |
1355 |
> |
end; |
1356 |
> |
end; |
1357 |
> |
|
1358 |
> |
begin |
1359 |
> |
if InternalGetUpdatable = False then |
1360 |
> |
FReadOnly := True |
1361 |
> |
else |
1362 |
> |
begin |
1363 |
> |
DeleteSQL.Text := 'delete from ' + {do not localize} |
1364 |
> |
QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
1365 |
> |
' where RDB$DB_KEY = ' + ':IBX_INTERNAL_DBKEY'; {do not localize} |
1366 |
> |
GenerateFieldLists; |
1367 |
> |
InsertSQL.Text := 'insert into ' + {do not localize} |
1368 |
> |
QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
1369 |
> |
' (' + InsertFieldList + {do not localize} |
1370 |
> |
') values (' + InsertParamList + ')'; {do not localize} |
1371 |
> |
ModifySQL.Text := 'update ' + |
1372 |
> |
QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
1373 |
> |
' set ' + UpdateFieldList + {do not localize} |
1374 |
> |
' where RDB$DB_KEY = :IBX_INTERNAL_DBKEY'; {do not localize} |
1375 |
> |
WhereAllRefreshSQL.Text := 'select ' + {do not localize} |
1376 |
> |
QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' |
1377 |
> |
+ 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize} |
1378 |
> |
+ QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
1379 |
> |
' where ' + WhereAllFieldList; {do not localize} |
1380 |
> |
if FPrimaryIndexFields <> '' then |
1381 |
> |
begin |
1382 |
> |
GenerateWherePrimaryFieldList; |
1383 |
> |
WherePrimaryRefreshSQL.Text := 'select ' + {do not localize} |
1384 |
> |
QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' {do not localize} |
1385 |
> |
+ 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize} |
1386 |
> |
+ QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
1387 |
> |
' where ' + WherePrimaryFieldList; {do not localize} |
1388 |
> |
end; |
1389 |
> |
try |
1390 |
> |
InternalPrepare; |
1391 |
> |
except |
1392 |
> |
FReadonly := True; |
1393 |
> |
end; |
1394 |
> |
end; |
1395 |
> |
end; |
1396 |
> |
|
1397 |
> |
procedure TIBTable.ResetSQLStatements; |
1398 |
> |
begin |
1399 |
> |
SelectSQL.Text := ''; |
1400 |
> |
DeleteSQL.Text := ''; |
1401 |
> |
InsertSQL.Text := ''; |
1402 |
> |
ModifySQL.Text := ''; |
1403 |
> |
RefreshSQL.Text := ''; |
1404 |
> |
end; |
1405 |
> |
|
1406 |
> |
procedure TIBTable.SetTableTypes( |
1407 |
> |
const Value: TIBTableTypes); |
1408 |
> |
begin |
1409 |
> |
FTableTypes := Value; |
1410 |
> |
end; |
1411 |
> |
|
1412 |
> |
function TIBTable.InternalGotoDBKey(DBKey: TIBDBKey): Boolean; |
1413 |
> |
|
1414 |
> |
function DBKeyCompare (DBKey1, DBKey2: TIBDBKey): Boolean; |
1415 |
> |
var |
1416 |
> |
I: Integer; |
1417 |
> |
begin |
1418 |
> |
for I := 0 to 7 do |
1419 |
> |
if (DBKey1.DBKey[i] <> DBKey2.DBKey[i]) then begin |
1420 |
> |
result := False; |
1421 |
> |
exit; |
1422 |
> |
end; |
1423 |
> |
result := True; |
1424 |
> |
end; |
1425 |
> |
begin |
1426 |
> |
CheckActive; |
1427 |
> |
DisableControls; |
1428 |
> |
try |
1429 |
> |
result := False; |
1430 |
> |
First; |
1431 |
> |
while ((not result) and (not EOF)) do begin |
1432 |
> |
if (DBKeyCompare (DBKey, PRecordData(GetActiveBuf)^.rdDBKey)) then |
1433 |
> |
result := True |
1434 |
> |
else |
1435 |
> |
Next; |
1436 |
> |
end; |
1437 |
> |
if not result then |
1438 |
> |
First |
1439 |
> |
else |
1440 |
> |
CursorPosChanged; |
1441 |
> |
finally |
1442 |
> |
EnableControls; |
1443 |
> |
end; |
1444 |
> |
end; |
1445 |
> |
|
1446 |
> |
function TIBTable.GetCurrentDBKey: TIBDBKey; |
1447 |
> |
var |
1448 |
> |
Buf: pChar; |
1449 |
> |
begin |
1450 |
> |
CheckActive; |
1451 |
> |
buf := GetActiveBuf; |
1452 |
> |
if Buf <> nil then |
1453 |
> |
Result := PRecordData(Buf)^.rdDBKey |
1454 |
> |
else |
1455 |
> |
Result.DBKey[0] := 0; |
1456 |
> |
end; |
1457 |
> |
|
1458 |
> |
procedure TIBTable.Reopen; |
1459 |
> |
begin |
1460 |
> |
DisableControls; |
1461 |
> |
try |
1462 |
> |
if Active then |
1463 |
> |
begin |
1464 |
> |
SetState(dsInactive); |
1465 |
> |
CloseCursor; |
1466 |
> |
OpenCursor(false); |
1467 |
> |
SetState(dsBrowse); |
1468 |
> |
end; |
1469 |
> |
finally |
1470 |
> |
EnableControls; |
1471 |
> |
end; |
1472 |
> |
end; |
1473 |
> |
|
1474 |
> |
end. |