35 |
|
|
36 |
|
{$Mode Delphi} |
37 |
|
|
38 |
+ |
{$codepage UTF8} |
39 |
+ |
|
40 |
|
interface |
41 |
|
|
42 |
|
uses SysUtils, Classes, DB, IB, IBCustomDataSet, |
159 |
|
property FieldDefs stored FieldDefsStored; |
160 |
|
property Filter; |
161 |
|
property Filtered; |
162 |
+ |
property GeneratorField; |
163 |
|
property IndexDefs: TIndexDefs read FIndexDefs write SetIndexDefs stored IndexDefsStored; |
164 |
|
property IndexFieldNames: string read GetIndexFieldNames write SetIndexFieldNames; |
165 |
|
property IndexName: string read GetIndexName write SetIndexName; |
183 |
|
|
184 |
|
implementation |
185 |
|
|
186 |
+ |
uses FBMessages; |
187 |
+ |
|
188 |
|
{ TIBTable } |
189 |
|
|
190 |
|
constructor TIBTable.Create(AOwner: TComponent); |
331 |
|
end; |
332 |
|
end; |
333 |
|
Query.Prepare; |
334 |
+ |
FieldDefsFromQuery(Query); |
335 |
|
if DidActivate then |
336 |
|
Query.Transaction.Rollback; |
331 |
– |
FieldDefsFromQuery(Query); |
337 |
|
finally |
338 |
|
Query.Free; |
339 |
|
end; |
430 |
|
Query.SQL.Text := 'Select ''foo'' from RDB$RELATION_CONSTRAINTS ' + |
431 |
|
'where RDB$RELATION_NAME = ' + |
432 |
|
'''' + |
433 |
< |
FormatIdentifierValue(Database.SQLDialect, |
433 |
> |
ExtractIdentifier(Database.SQLDialect, |
434 |
|
QuoteIdentifier(DataBase.SQLDialect, FTableName)) + |
435 |
|
''' ' + |
436 |
|
' AND RDB$CONSTRAINT_NAME = ' + |
437 |
|
'''' + |
438 |
< |
FormatIdentifierValue(Database.SQLDialect, |
438 |
> |
ExtractIdentifier(Database.SQLDialect, |
439 |
|
QuoteIdentifier(DataBase.SQLDialect, Name)) + |
440 |
|
''' ' + |
441 |
|
'AND RDB$CONSTRAINT_TYPE = ''PRIMARY KEY'''; |
467 |
|
Query.SQL.Text := 'Select RDB$CONSTRAINT_NAME from RDB$RELATION_CONSTRAINTS ' + |
468 |
|
'where RDB$RELATION_NAME = ' + |
469 |
|
'''' + |
470 |
< |
FormatIdentifierValue(Database.SQLDialect, |
470 |
> |
ExtractIdentifier(Database.SQLDialect, |
471 |
|
QuoteIdentifier(DataBase.SQLDialect, FTableName)) + |
472 |
|
''' ' + |
473 |
|
'AND RDB$INDEX_NAME = ' + |
474 |
|
'''' + |
475 |
< |
FormatIdentifierValue(Database.SQLDialect, |
475 |
> |
ExtractIdentifier(Database.SQLDialect, |
476 |
|
QuoteIdentifier(DataBase.SQLDialect, Name)) + |
477 |
|
''' ' + |
478 |
|
'AND RDB$CONSTRAINT_TYPE = ''PRIMARY KEY'''; |
484 |
|
Query.SQL.Text := 'Alter Table ' + {do not localize} |
485 |
|
QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
486 |
|
' Drop Constraint ' + |
487 |
< |
QuoteIdentifier(DataBase.SQLDialect, Query.Current.ByName('RDB$CONSTRAINT_NAME').AsString); |
487 |
> |
QuoteIdentifier(DataBase.SQLDialect, Query.FieldByName('RDB$CONSTRAINT_NAME').AsString); |
488 |
|
Query.Prepare; |
489 |
|
Query.ExecQuery; |
490 |
|
IndexDefs.Updated := False; |
588 |
|
'I.RDB$SEGMENT_COUNT, S.RDB$FIELD_NAME from RDB$INDICES I, ' + {do not localize} |
589 |
|
'RDB$INDEX_SEGMENTS S where I.RDB$INDEX_NAME = S.RDB$INDEX_NAME '+ {do not localize} |
590 |
|
'and I.RDB$RELATION_NAME = ' + '''' + {do not localize} |
591 |
< |
FormatIdentifierValue(Database.SQLDialect, |
591 |
> |
ExtractIdentifier(Database.SQLDialect, |
592 |
|
QuoteIdentifier(DataBase.SQLDialect, FTableName)) + ''''; |
593 |
|
Query.Prepare; |
594 |
|
Query.ExecQuery; |
595 |
< |
while (not Query.EOF) and (Query.Next <> nil) do |
595 |
> |
while (not Query.EOF) and Query.Next do |
596 |
|
begin |
597 |
|
with IndexDefs.AddIndexDef do |
598 |
|
begin |
599 |
< |
Name := TrimRight(Query.Current.ByName('RDB$INDEX_NAME').AsString); {do not localize} |
599 |
> |
Name := TrimRight(Query.FieldByName('RDB$INDEX_NAME').AsString); {do not localize} |
600 |
|
Opts := []; |
601 |
|
if Pos ('RDB$PRIMARY', Name) = 1 then Include(Opts, ixPrimary); {do not localize} {mbcs ok} |
602 |
< |
if Query.Current.ByName('RDB$UNIQUE_FLAG').AsInteger = 1 then Include(Opts, ixUnique); {do not localize} |
603 |
< |
if Query.Current.ByName('RDB$INDEX_TYPE').AsInteger = 2 then Include(Opts, ixDescending); {do not localize} |
602 |
> |
if Query.FieldByName('RDB$UNIQUE_FLAG').AsInteger = 1 then Include(Opts, ixUnique); {do not localize} |
603 |
> |
if Query.FieldByName('RDB$INDEX_TYPE').AsInteger = 2 then Include(Opts, ixDescending); {do not localize} |
604 |
|
Options := Opts; |
605 |
< |
if (Query.Current.ByName('RDB$SEGMENT_COUNT').AsInteger = 1) then {do not localize} |
605 |
> |
if (Query.FieldByName('RDB$SEGMENT_COUNT').AsInteger = 1) then {do not localize} |
606 |
|
begin |
607 |
< |
fn := Trim(Query.Current.ByName('RDB$FIELD_NAME').AsString); {do not localize} |
607 |
> |
fn := Trim(Query.FieldByName('RDB$FIELD_NAME').AsString); {do not localize} |
608 |
|
aField := GetFieldDefFromAlias(fn); |
609 |
|
if assigned(aField) then |
610 |
|
Fields := aField.Name |
620 |
|
SubQuery.SQL.Text := |
621 |
|
'Select RDB$FIELD_NAME from RDB$INDEX_SEGMENTS where RDB$INDEX_NAME = ' + {do not localize} |
622 |
|
'''' + |
623 |
< |
FormatIdentifierValue(Database.SQLDialect, |
623 |
> |
ExtractIdentifier(Database.SQLDialect, |
624 |
|
QuoteIdentifier(DataBase.SQLDialect, Name)) + |
625 |
|
'''' + 'ORDER BY RDB$FIELD_POSITION'; {do not localize} |
626 |
|
SubQuery.Prepare; |
627 |
|
SubQuery.ExecQuery; |
628 |
|
Flds := ''; |
629 |
< |
while (not SubQuery.EOF) and (SubQuery.Next <> nil) do |
629 |
> |
while (not SubQuery.EOF) and SubQuery.Next do |
630 |
|
begin |
631 |
< |
fn := TrimRight(SubQuery.Current.ByName('RDB$FIELD_NAME').AsString); {do not localize} |
631 |
> |
fn := TrimRight(SubQuery.FieldByName('RDB$FIELD_NAME').AsString); {do not localize} |
632 |
|
aField := GetFieldDefFromAlias(fn); |
633 |
|
if assigned(aField) then |
634 |
|
fn := aField.Name; |
671 |
|
Query.SQL.Text := |
672 |
|
'Select USER from RDB$RELATIONS where RDB$RELATION_NAME = ' + {do not localize} |
673 |
|
'''' + |
674 |
< |
FormatIdentifierValue(Database.SQLDialect, |
674 |
> |
ExtractIdentifier(Database.SQLDialect, |
675 |
|
QuoteIdentifier(DataBase.SQLDialect, FTableName)) + ''''; |
676 |
|
Query.Prepare; |
677 |
|
Query.ExecQuery; |
918 |
|
Query.SQL.Text := 'Select RDB$SYSTEM_FLAG, RDB$DBKEY_LENGTH ' + {do not localize} |
919 |
|
'from RDB$RELATIONS where RDB$RELATION_NAME = ' + {do not localize} |
920 |
|
'''' + |
921 |
< |
FormatIdentifierValue(Database.SQLDialect, |
921 |
> |
ExtractIdentifier(Database.SQLDialect, |
922 |
|
QuoteIdentifier(DataBase.SQLDialect, FTableName)) + ''''; |
923 |
|
Query.Prepare; |
924 |
|
Query.ExecQuery; |
925 |
< |
if (Query.Current[0].AsInteger <> 0) or |
926 |
< |
(Query.Current[1].AsInteger <> 8) then |
925 |
> |
if (Query.Fields[0].AsInteger <> 0) or |
926 |
> |
(Query.Fields[1].AsInteger <> 8) then |
927 |
|
Result := False |
928 |
|
else |
929 |
|
Result := True; |
1206 |
|
' where RDB$VIEW_BLR is NULL and RDB$SYSTEM_FLAG = 0'; {do not localize} |
1207 |
|
Query.Prepare; |
1208 |
|
Query.ExecQuery; |
1209 |
< |
while (not Query.EOF) and (Query.Next <> nil) do |
1210 |
< |
FNameList.Add (TrimRight(Query.Current[0].AsString)); |
1209 |
> |
while (not Query.EOF) and Query.Next do |
1210 |
> |
FNameList.Add (TrimRight(Query.Fields[0].AsString)); |
1211 |
|
finally |
1212 |
|
Query.Free; |
1213 |
|
Database.InternalTransaction.Commit; |
1302 |
|
procedure TIBTable.GenerateUpdateSQL; |
1303 |
|
var |
1304 |
|
InsertFieldList, InsertParamList, UpdateFieldList: string; |
1305 |
< |
WherePrimaryFieldList, WhereAllFieldList: string; |
1305 |
> |
WherePrimaryFieldList, WhereAllFieldList, |
1306 |
> |
InsertReturningFieldList, UpdateReturningFieldList: string; |
1307 |
|
|
1308 |
|
procedure GenerateFieldLists; |
1309 |
|
var |
1310 |
|
I: Integer; |
1311 |
+ |
AllowReturningClause: boolean; |
1312 |
|
begin |
1313 |
+ |
{Is database Firebird 2.1 or later?} |
1314 |
+ |
AllowReturningClause := (DatabaseInfo.ODSMajorVersion > 11) or |
1315 |
+ |
((DatabaseInfo.ODSMajorVersion = 11) and (DatabaseInfo.ODSMinorVersion >= 1)); |
1316 |
|
for I := 0 to FieldDefs.Count - 1 do begin |
1317 |
< |
with FieldDefs[I] do begin |
1317 |
> |
with TIBFieldDef(FieldDefs[I]) do begin |
1318 |
> |
if AllowReturningClause then |
1319 |
> |
begin |
1320 |
> |
if (faReadOnly in Attributes) or IdentityColumn then |
1321 |
> |
begin |
1322 |
> |
if InsertReturningFieldList <> '' then |
1323 |
> |
InsertReturningFieldList := InsertReturningFieldList + ', ' + |
1324 |
> |
QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) |
1325 |
> |
else |
1326 |
> |
InsertReturningFieldList := ' RETURNING ' + |
1327 |
> |
QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) |
1328 |
> |
end; |
1329 |
> |
if (faReadOnly in Attributes) then |
1330 |
> |
begin |
1331 |
> |
if UpdateReturningFieldList <> '' then |
1332 |
> |
UpdateReturningFieldList := UpdateReturningFieldList + ', ' + |
1333 |
> |
QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) |
1334 |
> |
else |
1335 |
> |
UpdateReturningFieldList := ' RETURNING ' + |
1336 |
> |
QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) |
1337 |
> |
end; |
1338 |
> |
end; |
1339 |
|
if not (InternalCalcField or (faReadOnly in Attributes) or |
1340 |
< |
(DataType = ftUnknown)) then |
1340 |
> |
(DataType = ftUnknown)) then |
1341 |
|
begin |
1342 |
< |
if ( InsertFieldList <> '' ) then begin |
1343 |
< |
InsertFieldList := InsertFieldList + ', '; |
1344 |
< |
InsertParamList := InsertParamList + ', '; |
1342 |
> |
if not IdentityColumn then |
1343 |
> |
begin |
1344 |
> |
if ( InsertFieldList <> '' ) then begin |
1345 |
> |
InsertFieldList := InsertFieldList + ', '; |
1346 |
> |
InsertParamList := InsertParamList + ', '; |
1347 |
> |
end; |
1348 |
> |
InsertFieldList := InsertFieldList + |
1349 |
> |
QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)); |
1350 |
> |
InsertParamList := InsertParamList + ':' + Name; |
1351 |
> |
end; |
1352 |
> |
if (UpdateFieldList <> '') then begin |
1353 |
|
UpdateFieldList := UpdateFieldList + ', '; |
1354 |
|
if (DataType <> ftBlob) and (DataType <>ftMemo) then |
1355 |
|
WhereAllFieldList := WhereAllFieldList + ' AND '; |
1356 |
|
end; |
1318 |
– |
InsertFieldList := InsertFieldList + |
1319 |
– |
QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)); |
1320 |
– |
InsertParamList := InsertParamList + ':' + Name; |
1357 |
|
UpdateFieldList := UpdateFieldList + |
1358 |
|
QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) + |
1359 |
|
' = :' + Name; |
1403 |
|
InsertSQL.Text := 'insert into ' + {do not localize} |
1404 |
|
QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
1405 |
|
' (' + InsertFieldList + {do not localize} |
1406 |
< |
') values (' + InsertParamList + ')'; {do not localize} |
1406 |
> |
') values (' + InsertParamList + ')' + InsertReturningFieldList; {do not localize} |
1407 |
|
ModifySQL.Text := 'update ' + |
1408 |
|
QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
1409 |
|
' set ' + UpdateFieldList + {do not localize} |
1410 |
< |
' where RDB$DB_KEY = :IBX_INTERNAL_DBKEY'; {do not localize} |
1410 |
> |
' where RDB$DB_KEY = :IBX_INTERNAL_DBKEY' + UpdateReturningFieldList; {do not localize} |
1411 |
|
WhereAllRefreshSQL.Text := 'select ' + {do not localize} |
1412 |
|
QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' |
1413 |
|
+ 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize} |
1414 |
|
+ QuoteIdentifier(DataBase.SQLDialect, FTableName) + |
1415 |
< |
' where ' + WhereAllFieldList; {do not localize} |
1415 |
> |
' where ' + WhereAllFieldList ; {do not localize} |
1416 |
|
if FPrimaryIndexFields <> '' then |
1417 |
|
begin |
1418 |
|
GenerateWherePrimaryFieldList; |