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 |
+ |
{$codepage UTF8} |
39 |
+ |
|
40 |
|
interface |
41 |
|
|
42 |
|
uses SysUtils, Classes, DB, IB, IBCustomDataSet, |
43 |
< |
IBHeader, IBSQL, IBUtils; |
43 |
> |
IBSQL, IBUtils; |
44 |
|
|
45 |
|
type |
46 |
|
|
149 |
|
property TableNames: TStrings read GetTableNames; |
150 |
|
|
151 |
|
published |
152 |
+ |
property AutoCommit; |
153 |
|
property Active; |
154 |
|
property BufferChunks; |
155 |
|
property CachedUpdates; |
156 |
< |
property Constraints stored ConstraintsStored; |
156 |
> |
property DataSetCloseAction; |
157 |
> |
// property Constraints stored ConstraintsStored; |
158 |
|
property DefaultIndex: Boolean read FDefaultIndex write FDefaultIndex default True; |
159 |
|
property FieldDefs stored FieldDefsStored; |
160 |
|
property Filter; |
182 |
|
|
183 |
|
implementation |
184 |
|
|
185 |
+ |
uses FBMessages; |
186 |
+ |
|
187 |
|
{ TIBTable } |
188 |
|
|
189 |
|
constructor TIBTable.Create(AOwner: TComponent); |
240 |
|
GenerateUpdateSQL; |
241 |
|
FRegenerateSQL := False; |
242 |
|
end; |
243 |
+ |
{ writeln(SelectSQL.Text); |
244 |
+ |
writeln(InsertSQL.Text); |
245 |
+ |
writeln(DeleteSQL.Text); |
246 |
+ |
writeln(ModifySQL.Text); } |
247 |
|
SetParams; |
248 |
|
inherited InternalOpen; |
249 |
|
end; |
305 |
|
|
306 |
|
procedure TIBTable.InitFieldDefs; |
307 |
|
var |
308 |
< |
sqlscale: Integer; |
308 |
> |
DidActivate: Boolean; |
309 |
|
Query: TIBSQL; |
310 |
|
begin |
311 |
|
if FTableName = '' then IBError(ibxeNoTableName, [nil]); |
312 |
< |
if (InternalPrepared) then InternalInitFieldDefs else |
312 |
> |
if (InternalPrepared) then |
313 |
> |
InternalInitFieldDefs |
314 |
> |
else |
315 |
|
begin |
316 |
< |
Database.InternalTransaction.StartTransaction; |
316 |
> |
{Get the field defs from a simple query on the table} |
317 |
> |
ActivateConnection; |
318 |
|
Query := TIBSQL.Create(self); |
319 |
|
try |
320 |
< |
Query.GoToFirstRecordOnExecute := False; |
321 |
< |
Query.Database := DataBase; |
322 |
< |
Query.Transaction := Database.InternalTransaction; |
323 |
< |
Query.SQL.Text := 'Select R.RDB$FIELD_NAME, R.RDB$FIELD_POSITION, ' + {do not localize} |
324 |
< |
'F.RDB$COMPUTED_BLR, F.RDB$DEFAULT_VALUE, ' + {do not localize} |
325 |
< |
'F.RDB$NULL_FLAG, ' + {do not localize} |
326 |
< |
'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: |
320 |
> |
Query.Database := DataBase; |
321 |
> |
Query.Transaction := Database.InternalTransaction; |
322 |
> |
DidActivate := false; |
323 |
> |
Query.SQL.Text := 'Select * from ' + QuoteIdentifier(DataBase.SQLDialect, FTableName); |
324 |
> |
with Query.Transaction do |
325 |
> |
begin |
326 |
> |
if not InTransaction then |
327 |
|
begin |
328 |
< |
sqlscale := Query.Current.ByName('RDB$FIELD_SCALE').AsInteger; {do not localize} |
329 |
< |
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; |
328 |
> |
StartTransaction; |
329 |
> |
DidActivate := true |
330 |
|
end; |
331 |
< |
blr_int64: |
332 |
< |
begin |
333 |
< |
sqlscale := Query.Current.ByName('RDB$FIELD_SCALE').AsInteger; {do not localize} |
334 |
< |
if (sqlscale = 0) then |
335 |
< |
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; |
331 |
> |
end; |
332 |
> |
Query.Prepare; |
333 |
> |
FieldDefsFromQuery(Query); |
334 |
> |
if DidActivate then |
335 |
> |
Query.Transaction.Rollback; |
336 |
|
finally |
337 |
< |
Query.free; |
409 |
< |
Database.InternalTransaction.Commit; |
337 |
> |
Query.Free; |
338 |
|
end; |
339 |
|
end; |
340 |
|
end; |
483 |
|
Query.SQL.Text := 'Alter Table ' + {do not localize} |
484 |
|
QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
485 |
|
' Drop Constraint ' + |
486 |
< |
QuoteIdentifier(DataBase.SQLDialect, Query.Current.ByName('RDB$CONSTRAINT_NAME').AsString); |
486 |
> |
QuoteIdentifier(DataBase.SQLDialect, Query.FieldByName('RDB$CONSTRAINT_NAME').AsString); |
487 |
|
Query.Prepare; |
488 |
|
Query.ExecQuery; |
489 |
|
IndexDefs.Updated := False; |
568 |
|
Opts: TIndexOptions; |
569 |
|
Flds: string; |
570 |
|
Query, SubQuery: TIBSQL; |
571 |
+ |
fn: string; |
572 |
+ |
aField: TFieldDef; |
573 |
|
begin |
574 |
|
if not (csReading in ComponentState) then begin |
575 |
|
if not Active and not FSwitchingIndex then |
591 |
|
QuoteIdentifier(DataBase.SQLDialect, FTableName)) + ''''; |
592 |
|
Query.Prepare; |
593 |
|
Query.ExecQuery; |
594 |
< |
while (not Query.EOF) and (Query.Next <> nil) do |
594 |
> |
while (not Query.EOF) and Query.Next do |
595 |
|
begin |
596 |
|
with IndexDefs.AddIndexDef do |
597 |
|
begin |
598 |
< |
Name := TrimRight(Query.Current.ByName('RDB$INDEX_NAME').AsString); {do not localize} |
598 |
> |
Name := TrimRight(Query.FieldByName('RDB$INDEX_NAME').AsString); {do not localize} |
599 |
|
Opts := []; |
600 |
|
if Pos ('RDB$PRIMARY', Name) = 1 then Include(Opts, ixPrimary); {do not localize} {mbcs ok} |
601 |
< |
if Query.Current.ByName('RDB$UNIQUE_FLAG').AsInteger = 1 then Include(Opts, ixUnique); {do not localize} |
602 |
< |
if Query.Current.ByName('RDB$INDEX_TYPE').AsInteger = 2 then Include(Opts, ixDescending); {do not localize} |
601 |
> |
if Query.FieldByName('RDB$UNIQUE_FLAG').AsInteger = 1 then Include(Opts, ixUnique); {do not localize} |
602 |
> |
if Query.FieldByName('RDB$INDEX_TYPE').AsInteger = 2 then Include(Opts, ixDescending); {do not localize} |
603 |
|
Options := Opts; |
604 |
< |
if (Query.Current.ByName('RDB$SEGMENT_COUNT').AsInteger = 1) then {do not localize} |
605 |
< |
Fields := Trim(Query.Current.ByName('RDB$FIELD_NAME').AsString) {do not localize} |
604 |
> |
if (Query.FieldByName('RDB$SEGMENT_COUNT').AsInteger = 1) then {do not localize} |
605 |
> |
begin |
606 |
> |
fn := Trim(Query.FieldByName('RDB$FIELD_NAME').AsString); {do not localize} |
607 |
> |
aField := GetFieldDefFromAlias(fn); |
608 |
> |
if assigned(aField) then |
609 |
> |
Fields := aField.Name |
610 |
> |
else |
611 |
> |
Fields := fn; |
612 |
> |
end |
613 |
|
else begin |
614 |
|
SubQuery := TIBSQL.Create(self); |
615 |
|
try |
625 |
|
SubQuery.Prepare; |
626 |
|
SubQuery.ExecQuery; |
627 |
|
Flds := ''; |
628 |
< |
while (not SubQuery.EOF) and (SubQuery.Next <> nil) do |
628 |
> |
while (not SubQuery.EOF) and SubQuery.Next do |
629 |
|
begin |
630 |
+ |
fn := TrimRight(SubQuery.FieldByName('RDB$FIELD_NAME').AsString); {do not localize} |
631 |
+ |
aField := GetFieldDefFromAlias(fn); |
632 |
+ |
if assigned(aField) then |
633 |
+ |
fn := aField.Name; |
634 |
|
if (Flds = '') then |
635 |
< |
Flds := TrimRight(SubQuery.Current.ByName('RDB$FIELD_NAME').AsString) {do not localize} |
635 |
> |
Flds := fn |
636 |
|
else begin |
637 |
|
Query.Next; |
638 |
< |
Flds := Flds + ';' + TrimRight(SubQuery.Current[0].AsString); |
638 |
> |
Flds := Flds + ';' + fn; |
639 |
|
end; |
640 |
|
end; |
641 |
|
Fields := Flds; |
719 |
|
FieldList := FieldList + |
720 |
|
QuoteIdentifier(DataBase.SQLDialect, Name) + |
721 |
|
' CHAR(' + IntToStr(Size) + ')'; {do not localize} |
722 |
< |
ftBoolean, ftSmallint, ftWord: |
722 |
> |
ftBoolean: |
723 |
> |
FieldList := FieldList + |
724 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
725 |
> |
' BOOLEAN'; {do not localize} |
726 |
> |
ftSmallint, ftWord: |
727 |
|
FieldList := FieldList + |
728 |
|
QuoteIdentifier(DataBase.SQLDialect, Name) + |
729 |
|
' SMALLINT'; {do not localize} |
921 |
|
QuoteIdentifier(DataBase.SQLDialect, FTableName)) + ''''; |
922 |
|
Query.Prepare; |
923 |
|
Query.ExecQuery; |
924 |
< |
if (Query.Current[0].AsInteger <> 0) or |
925 |
< |
(Query.Current[1].AsInteger <> 8) then |
924 |
> |
if (Query.Fields[0].AsInteger <> 0) or |
925 |
> |
(Query.Fields[1].AsInteger <> 8) then |
926 |
|
Result := False |
927 |
|
else |
928 |
|
Result := True; |
1205 |
|
' where RDB$VIEW_BLR is NULL and RDB$SYSTEM_FLAG = 0'; {do not localize} |
1206 |
|
Query.Prepare; |
1207 |
|
Query.ExecQuery; |
1208 |
< |
while (not Query.EOF) and (Query.Next <> nil) do |
1209 |
< |
FNameList.Add (TrimRight(Query.Current[0].AsString)); |
1208 |
> |
while (not Query.EOF) and Query.Next do |
1209 |
> |
FNameList.Add (TrimRight(Query.Fields[0].AsString)); |
1210 |
|
finally |
1211 |
|
Query.Free; |
1212 |
|
Database.InternalTransaction.Commit; |
1239 |
|
SQL: TStrings; |
1240 |
|
OrderByStr: string; |
1241 |
|
bWhereClausePresent: Boolean; |
1242 |
+ |
fn: string; |
1243 |
+ |
aField: TFieldDef; |
1244 |
|
begin |
1245 |
|
bWhereClausePresent := False; |
1246 |
|
Database.CheckActive; |
1276 |
|
begin |
1277 |
|
if i > 0 then |
1278 |
|
SQL.Text := SQL.Text + 'AND '; |
1279 |
+ |
aField := FieldDefs.Find(FDetailFieldsList.Strings[i]); |
1280 |
+ |
if assigned(aField) then |
1281 |
+ |
fn := GetDBAliasName(aField.FieldNo) |
1282 |
+ |
else |
1283 |
+ |
fn := FDetailFieldsList.Strings[i]; {something wrong if we get here - but should word} |
1284 |
|
SQL.Text := SQL.Text + |
1285 |
< |
QuoteIdentifier(DataBase.SQLDialect, FDetailFieldsList.Strings[i]) + |
1334 |
< |
' = :' + |
1335 |
< |
QuoteIdentifier(DataBase.SQLDialect, FMasterFieldsList.Strings[i]); |
1285 |
> |
QuoteIdentifier(DataBase.SQLDialect, fn) + ' = :' + FMasterFieldsList.Strings[i]; |
1286 |
|
end; |
1287 |
|
end; |
1288 |
|
if OrderByStr <> '' then |
1320 |
|
WhereAllFieldList := WhereAllFieldList + ' AND '; |
1321 |
|
end; |
1322 |
|
InsertFieldList := InsertFieldList + |
1323 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name); |
1324 |
< |
InsertParamList := InsertParamList + ':' + |
1375 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name); |
1323 |
> |
QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)); |
1324 |
> |
InsertParamList := InsertParamList + ':' + Name; |
1325 |
|
UpdateFieldList := UpdateFieldList + |
1326 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
1327 |
< |
' = :' + |
1379 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name); |
1326 |
> |
QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) + |
1327 |
> |
' = :' + Name; |
1328 |
|
if (DataType <> ftBlob) and (DataType <>ftMemo) then |
1329 |
|
WhereAllFieldList := WhereAllFieldList + |
1330 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + ' = :' + |
1383 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name);{do not localize} |
1330 |
> |
QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) + ' = :' + Name; |
1331 |
|
end; |
1332 |
|
end; |
1333 |
|
end; |
1336 |
|
procedure GenerateWherePrimaryFieldList; |
1337 |
|
var |
1338 |
|
i: Integer; |
1339 |
< |
tmp: String; |
1339 |
> |
tmp, fn: String; |
1340 |
> |
aField: TFieldDef; |
1341 |
|
begin |
1342 |
|
i := 1; |
1343 |
|
while i <= Length(FPrimaryIndexFields) do |
1344 |
|
begin |
1345 |
|
tmp := ExtractFieldName(FPrimaryIndexFields, i); |
1346 |
+ |
aField := FieldDefs.Find(tmp); |
1347 |
+ |
if assigned(aField) then |
1348 |
+ |
fn := GetDBAliasName(aField.FieldNo) |
1349 |
+ |
else |
1350 |
+ |
fn := tmp; {something wrong if we get here - but will work in most cases} |
1351 |
|
tmp := |
1352 |
< |
QuoteIdentifier(DataBase.SQLDialect, tmp) + ' = :' + |
1352 |
> |
QuoteIdentifier(DataBase.SQLDialect, fn) + ' = :' + |
1353 |
|
QuoteIdentifier(DataBase.SQLDialect, tmp);{do not localize} |
1354 |
|
if WherePrimaryFieldList <> '' then |
1355 |
|
WherePrimaryFieldList := |