ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/runtime/IBTable.pas
(Generate patch)

Comparing ibx/trunk/runtime/IBTable.pas (file contents):
Revision 39 by tony, Tue May 17 08:14:52 2016 UTC vs.
Revision 143 by tony, Fri Feb 23 12:11:21 2018 UTC

# Line 27 | Line 27
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                                                 }
30 > {    Associates Ltd 2011 - 2018                                               }
31   {                                                                        }
32   {************************************************************************}
33  
# Line 35 | Line 35 | unit IBTable;
35  
36   {$Mode Delphi}
37  
38 {$IF FPC_FULLVERSION >= 20700 }
38   {$codepage UTF8}
40 {$ENDIF}
39  
40   interface
41  
# Line 61 | Line 59 | type
59      FMasterLink: TMasterDataLink;
60      FMasterFieldsList: TStringList;
61      FDetailFieldsList: TStringList;
64    FStoreDefs: Boolean;
62      FIndexDefs: TIndexDefs;
63      FDefaultIndex: Boolean;
64      FReadOnly: Boolean;
# Line 73 | Line 70 | type
70      FSwitchingIndex: Boolean;
71      FPrimaryIndexFields: string;
72      FTableTypes: TIBTableTypes;
73 +    FStoreDefs: boolean;
74      WhereAllRefreshSQL: TStrings;
75      WhereDBKeyRefreshSQL: TStrings;
76      WherePrimaryRefreshSQL: TStrings;
# Line 99 | Line 97 | type
97      function GetExists: Boolean;
98      procedure SetIndexDefs(Value: TIndexDefs);
99      procedure ExtractLinkFields;
102    function FieldDefsStored: Boolean;
100      function IndexDefsStored: Boolean;
101      function GetMasterFields: string;
102      procedure SetMasterFields(const Value: string);
# Line 158 | Line 155 | type
155      property DataSetCloseAction;
156   //    property Constraints stored ConstraintsStored;
157      property DefaultIndex: Boolean read FDefaultIndex write FDefaultIndex default True;
161    property FieldDefs stored FieldDefsStored;
158      property Filter;
159      property Filtered;
160 +    property GeneratorField;
161      property IndexDefs: TIndexDefs read FIndexDefs write SetIndexDefs stored IndexDefsStored;
162      property IndexFieldNames: string read GetIndexFieldNames write SetIndexFieldNames;
163      property IndexName: string read GetIndexName write SetIndexName;
# Line 184 | Line 181 | type
181  
182   implementation
183  
184 + uses FBMessages;
185 +
186   { TIBTable }
187  
188   constructor TIBTable.Create(AOwner: TComponent);
# Line 330 | Line 329 | begin
329                end;
330           end;
331           Query.Prepare;
332 +         FieldDefsFromQuery(Query);
333           if DidActivate then
334              Query.Transaction.Rollback;
335         FieldDefsFromQuery(Query);
335      finally
336           Query.Free;
337      end;
# Line 429 | Line 428 | var
428        Query.SQL.Text := 'Select ''foo'' from RDB$RELATION_CONSTRAINTS ' +
429          'where RDB$RELATION_NAME = ' +
430          '''' +
431 <        FormatIdentifierValue(Database.SQLDialect,
431 >        ExtractIdentifier(Database.SQLDialect,
432            QuoteIdentifier(DataBase.SQLDialect, FTableName)) +
433          ''' ' +
434          ' AND RDB$CONSTRAINT_NAME = ' +
435          '''' +
436 <        FormatIdentifierValue(Database.SQLDialect,
436 >        ExtractIdentifier(Database.SQLDialect,
437            QuoteIdentifier(DataBase.SQLDialect, Name)) +
438          ''' ' +
439          'AND RDB$CONSTRAINT_TYPE = ''PRIMARY KEY''';
# Line 466 | Line 465 | var
465        Query.SQL.Text := 'Select RDB$CONSTRAINT_NAME from RDB$RELATION_CONSTRAINTS ' +
466          'where RDB$RELATION_NAME = ' +
467          '''' +
468 <        FormatIdentifierValue(Database.SQLDialect,
468 >        ExtractIdentifier(Database.SQLDialect,
469            QuoteIdentifier(DataBase.SQLDialect, FTableName)) +
470          ''' ' +
471          'AND RDB$INDEX_NAME = ' +
472          '''' +
473 <        FormatIdentifierValue(Database.SQLDialect,
473 >        ExtractIdentifier(Database.SQLDialect,
474            QuoteIdentifier(DataBase.SQLDialect, Name)) +
475          ''' ' +
476          'AND RDB$CONSTRAINT_TYPE = ''PRIMARY KEY''';
# Line 483 | Line 482 | var
482          Query.SQL.Text := 'Alter Table ' +  {do not localize}
483            QuoteIdentifier(DataBase.SQLDialect, FTableName) +
484            ' Drop Constraint ' +
485 <          QuoteIdentifier(DataBase.SQLDialect, Query.Current.ByName('RDB$CONSTRAINT_NAME').AsString);
485 >          QuoteIdentifier(DataBase.SQLDialect, Query.FieldByName('RDB$CONSTRAINT_NAME').AsString);
486          Query.Prepare;
487          Query.ExecQuery;
488          IndexDefs.Updated := False;
# Line 587 | Line 586 | begin
586      'I.RDB$SEGMENT_COUNT, S.RDB$FIELD_NAME from RDB$INDICES I, ' + {do not localize}
587      'RDB$INDEX_SEGMENTS S where I.RDB$INDEX_NAME = S.RDB$INDEX_NAME '+ {do not localize}
588      'and I.RDB$RELATION_NAME = ' + '''' + {do not localize}
589 <     FormatIdentifierValue(Database.SQLDialect,
589 >     ExtractIdentifier(Database.SQLDialect,
590         QuoteIdentifier(DataBase.SQLDialect, FTableName)) + '''';
591      Query.Prepare;
592      Query.ExecQuery;
593 <    while (not Query.EOF) and (Query.Next <> nil) do
593 >    while (not Query.EOF) and Query.Next do
594      begin
595        with IndexDefs.AddIndexDef do
596        begin
597 <        Name := TrimRight(Query.Current.ByName('RDB$INDEX_NAME').AsString); {do not localize}
597 >        Name := TrimRight(Query.FieldByName('RDB$INDEX_NAME').AsString); {do not localize}
598          Opts := [];
599          if Pos ('RDB$PRIMARY', Name) = 1 then Include(Opts, ixPrimary); {do not localize} {mbcs ok}
600 <        if Query.Current.ByName('RDB$UNIQUE_FLAG').AsInteger = 1 then Include(Opts, ixUnique); {do not localize}
601 <        if Query.Current.ByName('RDB$INDEX_TYPE').AsInteger = 2  then Include(Opts, ixDescending); {do not localize}
600 >        if Query.FieldByName('RDB$UNIQUE_FLAG').AsInteger = 1 then Include(Opts, ixUnique); {do not localize}
601 >        if Query.FieldByName('RDB$INDEX_TYPE').AsInteger = 2  then Include(Opts, ixDescending); {do not localize}
602          Options := Opts;
603 <        if (Query.Current.ByName('RDB$SEGMENT_COUNT').AsInteger = 1) then {do not localize}
603 >        if (Query.FieldByName('RDB$SEGMENT_COUNT').AsInteger = 1) then {do not localize}
604          begin
605 <          fn := Trim(Query.Current.ByName('RDB$FIELD_NAME').AsString); {do not localize}
605 >          fn := Trim(Query.FieldByName('RDB$FIELD_NAME').AsString); {do not localize}
606            aField := GetFieldDefFromAlias(fn);
607            if assigned(aField) then
608               Fields := aField.Name
# Line 619 | Line 618 | begin
618            SubQuery.SQL.Text :=
619           'Select RDB$FIELD_NAME from RDB$INDEX_SEGMENTS where RDB$INDEX_NAME = ' + {do not localize}
620            '''' +
621 <          FormatIdentifierValue(Database.SQLDialect,
621 >          ExtractIdentifier(Database.SQLDialect,
622              QuoteIdentifier(DataBase.SQLDialect, Name)) +
623            '''' + 'ORDER BY RDB$FIELD_POSITION'; {do not localize}
624            SubQuery.Prepare;
625            SubQuery.ExecQuery;
626            Flds := '';
627 <          while (not SubQuery.EOF) and (SubQuery.Next <> nil) do
627 >          while (not SubQuery.EOF) and SubQuery.Next do
628            begin
629 <            fn := TrimRight(SubQuery.Current.ByName('RDB$FIELD_NAME').AsString); {do not localize}
629 >            fn := TrimRight(SubQuery.FieldByName('RDB$FIELD_NAME').AsString); {do not localize}
630              aField := GetFieldDefFromAlias(fn);
631              if assigned(aField) then
632                 fn := aField.Name;
# Line 670 | Line 669 | begin
669      Query.SQL.Text :=
670      'Select USER from RDB$RELATIONS where RDB$RELATION_NAME = ' + {do not localize}
671      '''' +
672 <    FormatIdentifierValue(Database.SQLDialect,
672 >    ExtractIdentifier(Database.SQLDialect,
673        QuoteIdentifier(DataBase.SQLDialect, FTableName)) + '''';
674      Query.Prepare;
675      Query.ExecQuery;
# Line 917 | Line 916 | begin
916      Query.SQL.Text := 'Select RDB$SYSTEM_FLAG, RDB$DBKEY_LENGTH ' + {do not localize}
917                      'from RDB$RELATIONS where RDB$RELATION_NAME = ' + {do not localize}
918                      '''' +
919 <                    FormatIdentifierValue(Database.SQLDialect,
919 >                    ExtractIdentifier(Database.SQLDialect,
920                        QuoteIdentifier(DataBase.SQLDialect, FTableName)) + '''';
921      Query.Prepare;
922      Query.ExecQuery;
923 <    if (Query.Current[0].AsInteger <> 0) or
924 <       (Query.Current[1].AsInteger <> 8) then
923 >    if (Query.Fields[0].AsInteger <> 0) or
924 >       (Query.Fields[1].AsInteger <> 8) then
925        Result := False
926      else
927        Result := True;
# Line 932 | Line 931 | begin
931    end;
932   end;
933  
935 function TIBTable.FieldDefsStored: Boolean;
936 begin
937  Result := StoreDefs and (FieldDefs.Count > 0);
938 end;
939
934   function TIBTable.IndexDefsStored: Boolean;
935   begin
936    Result := StoreDefs and (IndexDefs.Count > 0);
# Line 1205 | Line 1199 | begin
1199                            ' where RDB$VIEW_BLR is NULL and RDB$SYSTEM_FLAG = 0'; {do not localize}
1200        Query.Prepare;
1201        Query.ExecQuery;
1202 <      while (not Query.EOF) and (Query.Next <> nil) do
1203 <        FNameList.Add (TrimRight(Query.Current[0].AsString));
1202 >      while (not Query.EOF) and Query.Next  do
1203 >        FNameList.Add (TrimRight(Query.Fields[0].AsString));
1204      finally
1205        Query.Free;
1206        Database.InternalTransaction.Commit;
# Line 1301 | Line 1295 | end;
1295   procedure TIBTable.GenerateUpdateSQL;
1296   var
1297    InsertFieldList, InsertParamList, UpdateFieldList: string;
1298 <  WherePrimaryFieldList, WhereAllFieldList: string;
1298 >  WherePrimaryFieldList, WhereAllFieldList,
1299 >    InsertReturningFieldList, UpdateReturningFieldList: string;
1300  
1301    procedure GenerateFieldLists;
1302    var
1303      I: Integer;
1304 +    AllowReturningClause: boolean;
1305    begin
1306 +     {Is database Firebird 2.1 or later?}
1307 +    AllowReturningClause := (DatabaseInfo.ODSMajorVersion > 11) or
1308 +        ((DatabaseInfo.ODSMajorVersion = 11) and (DatabaseInfo.ODSMinorVersion >= 1));
1309      for I := 0 to FieldDefs.Count - 1 do begin
1310 <      with FieldDefs[I] do begin
1310 >      with TIBFieldDef(FieldDefs[I]) do begin
1311 >        if AllowReturningClause then
1312 >        begin
1313 >          if (faReadOnly in Attributes) or IdentityColumn then
1314 >          begin
1315 >            if InsertReturningFieldList <> '' then
1316 >              InsertReturningFieldList := InsertReturningFieldList + ', ' +
1317 >                       QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo))
1318 >            else
1319 >              InsertReturningFieldList := ' RETURNING ' +
1320 >                QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo))
1321 >          end;
1322 >          if (faReadOnly in Attributes)  then
1323 >          begin
1324 >            if UpdateReturningFieldList <> '' then
1325 >              UpdateReturningFieldList := UpdateReturningFieldList + ', ' +
1326 >                       QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo))
1327 >            else
1328 >              UpdateReturningFieldList := ' RETURNING ' +
1329 >                QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo))
1330 >          end;
1331 >        end;
1332          if not (InternalCalcField or (faReadOnly in Attributes) or
1333 <          (DataType = ftUnknown)) then
1333 >           (DataType = ftUnknown)) then
1334          begin
1335 <          if ( InsertFieldList <> '' ) then begin
1336 <            InsertFieldList := InsertFieldList + ', ';
1337 <            InsertParamList := InsertParamList + ', ';
1335 >          if not IdentityColumn then
1336 >          begin
1337 >            if ( InsertFieldList <> '' ) then begin
1338 >              InsertFieldList := InsertFieldList + ', ';
1339 >              InsertParamList := InsertParamList + ', ';
1340 >            end;
1341 >            InsertFieldList := InsertFieldList +
1342 >              QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo));
1343 >            InsertParamList := InsertParamList + ':' +  Name;
1344 >          end;
1345 >          if (UpdateFieldList <> '') then begin
1346              UpdateFieldList := UpdateFieldList + ', ';
1347              if (DataType <> ftBlob) and (DataType <>ftMemo) then
1348                WhereAllFieldList := WhereAllFieldList + ' AND ';
1349            end;
1322          InsertFieldList := InsertFieldList +
1323            QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo));
1324          InsertParamList := InsertParamList + ':' +  Name;
1350            UpdateFieldList := UpdateFieldList +
1351              QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) +
1352              ' = :' + Name;
# Line 1371 | Line 1396 | begin
1396      InsertSQL.Text := 'insert into ' + {do not localize}
1397        QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1398      ' (' + InsertFieldList + {do not localize}
1399 <      ') values (' + InsertParamList + ')'; {do not localize}
1399 >      ') values (' + InsertParamList + ')' + InsertReturningFieldList; {do not localize}
1400      ModifySQL.Text := 'update ' +
1401        QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1402        ' set ' + UpdateFieldList + {do not localize}
1403 <      ' where RDB$DB_KEY = :IBX_INTERNAL_DBKEY'; {do not localize}
1403 >      ' where RDB$DB_KEY = :IBX_INTERNAL_DBKEY' + UpdateReturningFieldList; {do not localize}
1404      WhereAllRefreshSQL.Text := 'select ' +  {do not localize}
1405        QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, '
1406        + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1407        + QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1408 <      ' where ' + WhereAllFieldList; {do not localize}
1408 >      ' where ' + WhereAllFieldList ; {do not localize}
1409      if FPrimaryIndexFields <> '' then
1410      begin
1411        GenerateWherePrimaryFieldList;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines