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 1 by tony, Mon Jul 31 16:43:00 2000 UTC vs.
Revision 118 by tony, Mon Jan 22 13:58:14 2018 UTC

# Line 24 | Line 24
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 < interface
36 > {$Mode Delphi}
37 >
38 > {$codepage UTF8}
39  
40 < uses Windows, SysUtils, Graphics, Classes, Controls, Db, StdVCL,
34 <     IB, IBDatabase, IBCustomDataSet, IBHeader, IBSQL, IBUtils;
40 > interface
41  
42 + uses SysUtils, Classes, DB, IB,  IBCustomDataSet,
43 +     IBSQL, IBUtils;
44 +    
45   type
46  
47   { TIBTable }
# Line 103 | Line 112 | type
112      procedure Reopen;
113  
114    protected
106    { IProviderSupport }
107    function PSGetDefaultOrder: TIndexDef; override;
108    function PSGetKeyFields: string; override;
109    function PSGetTableName: string; override;
110    function PSGetIndexDefs(IndexTypes: TIndexOptions): TIndexDefs; override;
111    procedure PSSetCommandText(const CommandText: string); override;
112    procedure PSSetParams(AParams: TParams); override;
115  
116      procedure DoOnNewRecord; override;
117      procedure GetIndexParams(const IndexName: string; FieldsIndex: Boolean;
118        var IndexedName: string);
119      function GetCanModify: Boolean; override;
120      procedure UpdateIndexDefs; override;
121 <    procedure DataEvent(Event: TDataEvent; Info: Longint); override;
122 <    procedure DefChanged(Sender: TObject); override;
121 >    procedure DataEvent(Event: TDataEvent; Info: Ptrint); override;
122 >    procedure DefChanged(Sender: TObject); virtual;
123      function GetDataSource: TDataSource; override;
124      procedure InitFieldDefs; override;
125      procedure InternalClose; override;
# Line 137 | Line 139 | type
139      procedure DeleteIndex(const Name: string);
140      procedure DeleteTable;
141      procedure EmptyTable;
142 <    procedure GetDetailLinkFields(MasterFields, DetailFields: TList); override;
142 >    procedure GetDetailLinkFields(MasterFields, DetailFields: TList); virtual;
143      procedure GetIndexNames(List: TStrings);
144      procedure GotoCurrent(Table: TIBTable);
145      property CurrentDBKey: TIBDBKey read GetCurrentDBKey;
# Line 147 | Line 149 | type
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;
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;
# Line 178 | Line 183 | type
183  
184   implementation
185  
186 + uses FBMessages;
187 +
188   { TIBTable }
189  
190   constructor TIBTable.Create(AOwner: TComponent);
# Line 234 | Line 241 | begin
241        GenerateUpdateSQL;
242      FRegenerateSQL := False;
243    end;
244 + {  writeln(SelectSQL.Text);
245 +  writeln(InsertSQL.Text);
246 +  writeln(DeleteSQL.Text);
247 +  writeln(ModifySQL.Text); }
248    SetParams;
249    inherited InternalOpen;
250   end;
# Line 285 | Line 296 | begin
296      QRefresh.SQL.Assign(WherePrimaryRefreshSQL)
297    else
298      QRefresh.SQL.Assign(WhereAllRefreshSQL);
299 <  inherited;
299 >  inherited InternalRefreshRow;
300   end;
301  
302   procedure TIBTable.DefChanged(Sender: TObject);
# Line 295 | Line 306 | end;
306  
307   procedure TIBTable.InitFieldDefs;
308   var
309 <  sqlscale: Integer;
309 >  DidActivate: Boolean;
310    Query: TIBSQL;
311   begin
312    if FTableName = '' then IBError(ibxeNoTableName, [nil]);
313 <  if (InternalPrepared) then InternalInitFieldDefs else
313 >  if (InternalPrepared) then
314 >     InternalInitFieldDefs
315 >  else
316    begin
317 <    Database.InternalTransaction.StartTransaction;
317 >    {Get the field defs from a simple query on the table}
318 >    ActivateConnection;
319      Query := TIBSQL.Create(self);
320      try
321 <      Query.GoToFirstRecordOnExecute := False;
322 <      Query.Database := DataBase;
323 <      Query.Transaction := Database.InternalTransaction;
324 <      Query.SQL.Text := 'Select R.RDB$FIELD_NAME, R.RDB$FIELD_POSITION, ' + {do not localize}
325 <                        'F.RDB$COMPUTED_BLR, F.RDB$DEFAULT_VALUE, ' + {do not localize}
326 <                        'F.RDB$NULL_FLAG, ' + {do not localize}
327 <                        'F.RDB$FIELD_LENGTH, F.RDB$FIELD_SCALE, ' + {do not localize}
328 <                        'F.RDB$FIELD_TYPE, F.RDB$FIELD_SUB_TYPE, ' + {do not localize}
329 <                        'F.RDB$EXTERNAL_LENGTH, F.RDB$EXTERNAL_SCALE, F.RDB$EXTERNAL_TYPE ' + {do not localize}
330 <                        'from RDB$RELATION_FIELDS R, RDB$FIELDS F ' + {do not localize}
317 <                        'where R.RDB$RELATION_NAME = ' + {do not localize}
318 <                        '''' +
319 <                        FormatIdentifierValue(Database.SQLDialect, FTableName) +
320 <                        ''' ' +
321 <                        'and R.RDB$FIELD_SOURCE = F.RDB$FIELD_NAME '+ {do not localize}
322 <                        'order by R.RDB$FIELD_POSITION'; {do not localize}
323 <
324 <      Query.Prepare;
325 <      Query.ExecQuery;
326 <      FieldDefs.BeginUpdate;
327 <      FieldDefs.Clear;
328 <      while (not Query.EOF) and (Query.Next <> nil) do
329 <      begin
330 <          with FieldDefs.AddFieldDef do
331 <          begin
332 <            FieldNo := Query.Current.ByName('RDB$FIELD_POSITION').AsInteger; {do not localize}
333 <            Name := TrimRight(Query.Current.ByName('RDB$FIELD_NAME').AsString); {do not localize}
334 <            case Query.Current.ByName('RDB$FIELD_TYPE').AsInteger of {do not localize}
335 <              blr_varying, blr_text: begin
336 <                DataType := ftString;
337 <                Size := Query.Current.ByName('RDB$FIELD_LENGTH').AsInteger; {do not localize}
338 <              end;
339 <              blr_float, blr_double, blr_d_float: DataType := ftFloat;
340 <              blr_short: begin
341 <                sqlscale := Query.Current.ByName('RDB$FIELD_SCALE').AsInteger; {do not localize}
342 <                if (sqlscale = 0) then
343 <                  DataType := ftSmallInt
344 <                else begin
345 <                  DataType := ftBCD;
346 <                  Precision := 4;
347 <                end;
321 >         Query.Database := DataBase;
322 >         Query.Transaction := Database.InternalTransaction;
323 >         DidActivate := false;
324 >         Query.SQL.Text := 'Select * from ' + QuoteIdentifier(DataBase.SQLDialect, FTableName);
325 >         with Query.Transaction do
326 >         begin
327 >              if not InTransaction then
328 >              begin
329 >                StartTransaction;
330 >                DidActivate := true
331                end;
332 <              blr_long: begin
333 <                sqlscale := Query.Current.ByName('RDB$FIELD_SCALE').AsInteger; {do not localize}
334 <                if (sqlscale = 0) then
335 <                  DataType := ftInteger
336 <                else if (sqlscale >= (-4)) then begin
354 <                  DataType := ftBCD;
355 <                  Precision := 9;
356 <                end else
357 <                  DataType := ftFloat;
358 <              end;
359 <              blr_int64: begin
360 <                sqlscale := Query.Current.ByName('RDB$FIELD_SCALE').AsInteger; {do not localize}
361 <                if (sqlscale = 0) then
362 <                  DataType := ftLargeInt
363 <                else if (sqlscale >= (-4)) then begin
364 <                  DataType := ftBCD;
365 <                  Precision := 18;
366 <                end else
367 <                  DataType := ftFloat;
368 <              end;
369 <              blr_timestamp: DataType := ftDateTime;
370 <              blr_sql_time: DataType := ftTime;
371 <              blr_sql_date: DataType := ftDate;
372 <              blr_blob:
373 <                if (Query.Current.ByName('RDB$FIELD_SUB_TYPE').AsInteger = 1) then {do not localize}
374 <                  DataType := ftMemo
375 <                else
376 <                  DataType := ftBlob;
377 <              blr_quad: begin
378 <                DataType := ftUnknown;
379 <                Size := sizeof (TISC_QUAD);
380 <              end;
381 <              else
382 <                DataType := ftUnknown;
383 <            end;
384 <            if not (Query.Current.ByName('RDB$COMPUTED_BLR').IsNull) then {do not localize}
385 <            begin
386 <              Attributes := [faReadOnly];
387 <              InternalCalcField := True
388 <            end
389 <            else
390 <              InternalCalcField := False;
391 <            if ((not InternalCalcField) and
392 <                 Query.Current.ByName('RDB$DEFAULT_VALUE').IsNull and {do not localize}
393 <                 (Query.Current.ByName('RDB$NULL_FLAG').AsInteger = 1) )then begin {do not localize}
394 <              Attributes := [faRequired];
395 <              Required := True;
396 <            end;
397 <          end;
398 <      end;
399 <      FieldDefs.EndUpdate;
332 >         end;
333 >         Query.Prepare;
334 >         FieldDefsFromQuery(Query);
335 >         if DidActivate then
336 >            Query.Transaction.Rollback;
337      finally
338 <      Query.free;
402 <      Database.InternalTransaction.Commit;
338 >         Query.Free;
339      end;
340    end;
341   end;
# Line 425 | Line 361 | begin
361      if (ixPrimary in Options) then
362      begin
363       Query.SQL.Text := 'Alter Table ' + {do not localize}
364 <       FormatIdentifier(Database.SQLDialect, FTableName) +
364 >       QuoteIdentifier(Database.SQLDialect, FTableName) +
365         ' Add CONSTRAINT ' +   {do not localize}
366 <       FormatIdentifier(Database.SQLDialect, Name)
366 >       QuoteIdentifier(Database.SQLDialect, Name)
367         + ' Primary Key (' + {do not localize}
368         FormatFieldsList(Fields) +
369         ')';
370      end
371      else if ([ixUnique, ixDescending] * Options = [ixUnique, ixDescending]) then
372        Query.SQL.Text := 'Create unique Descending Index ' + {do not localize}
373 <                        FormatIdentifier(Database.SQLDialect, Name) +
373 >                        QuoteIdentifier(Database.SQLDialect, Name) +
374                          ' on ' + {do not localize}
375 <                        FormatIdentifier(Database.SQLDialect, FTableName) +
375 >                        QuoteIdentifier(Database.SQLDialect, FTableName) +
376                          ' (' + FieldList + ')'
377      else if (ixUnique in Options) then
378        Query.SQL.Text := 'Create unique Index ' + {do not localize}
379 <                        FormatIdentifier(Database.SQLDialect, Name) +
379 >                        QuoteIdentifier(Database.SQLDialect, Name) +
380                          ' on ' + {do not localize}
381 <                        FormatIdentifier(Database.SQLDialect, FTableName) +
381 >                        QuoteIdentifier(Database.SQLDialect, FTableName) +
382                          ' (' + FieldList + ')'
383      else if (ixDescending in Options) then
384        Query.SQL.Text := 'Create Descending Index ' + {do not localize}
385 <                        FormatIdentifier(Database.SQLDialect, Name) +
385 >                        QuoteIdentifier(Database.SQLDialect, Name) +
386                          ' on ' + {do not localize}
387 <                        FormatIdentifier(Database.SQLDialect, FTableName) +
387 >                        QuoteIdentifier(Database.SQLDialect, FTableName) +
388                          ' (' + FieldList + ')'
389      else
390        Query.SQL.Text := 'Create Index ' + {do not localize}
391 <                        FormatIdentifier(Database.SQLDialect, Name) +
391 >                        QuoteIdentifier(Database.SQLDialect, Name) +
392                          ' on ' + {do not localize}
393 <                        FormatIdentifier(Database.SQLDialect, FTableName) +
393 >                        QuoteIdentifier(Database.SQLDialect, FTableName) +
394                          ' (' + FieldList + ')';
395      Query.Prepare;
396      Query.ExecQuery;
# Line 475 | Line 411 | var
411        Query.Database := DataBase;
412        Query.Transaction := Transaction;
413        Query.SQL.Text := 'Drop index ' +  {do not localize}
414 <                         FormatIdentifier(Database.SQLDialect, Name);
414 >                         QuoteIdentifier(Database.SQLDialect, Name);
415        Query.Prepare;
416        Query.ExecQuery;
417        IndexDefs.Updated := False;
# Line 494 | Line 430 | var
430        Query.SQL.Text := 'Select ''foo'' from RDB$RELATION_CONSTRAINTS ' +
431          'where RDB$RELATION_NAME = ' +
432          '''' +
433 <        FormatIdentifierValue(Database.SQLDialect, FTableName) +
433 >        ExtractIdentifier(Database.SQLDialect,
434 >          QuoteIdentifier(DataBase.SQLDialect, FTableName)) +
435          ''' ' +
436          ' AND RDB$CONSTRAINT_NAME = ' +
437          '''' +
438 <        FormatIdentifierValue(Database.SQLDialect, Name) +
438 >        ExtractIdentifier(Database.SQLDialect,
439 >          QuoteIdentifier(DataBase.SQLDialect, Name)) +
440          ''' ' +
441          'AND RDB$CONSTRAINT_TYPE = ''PRIMARY KEY''';
442        Query.Prepare;
# Line 507 | Line 445 | var
445        begin
446          Query.Close;
447          Query.SQL.Text := 'Alter Table ' +  {do not localize}
448 <          FormatIdentifier(Database.SQLDialect, FTableName) +
448 >          QuoteIdentifier(DataBase.SQLDialect, FTableName) +
449            ' Drop Constraint ' +
450 <          FormatIdentifier(Database.SQLDialect, Name);
450 >          QuoteIdentifier(DataBase.SQLDialect, Name);
451          Query.Prepare;
452          Query.ExecQuery;
453          IndexDefs.Updated := False;
# Line 529 | Line 467 | var
467        Query.SQL.Text := 'Select RDB$CONSTRAINT_NAME from RDB$RELATION_CONSTRAINTS ' +
468          'where RDB$RELATION_NAME = ' +
469          '''' +
470 <        FormatIdentifierValue(Database.SQLDialect, FTableName) +
470 >        ExtractIdentifier(Database.SQLDialect,
471 >          QuoteIdentifier(DataBase.SQLDialect, FTableName)) +
472          ''' ' +
473          'AND RDB$INDEX_NAME = ' +
474          '''' +
475 <        FormatIdentifierValue(Database.SQLDialect, Name) +
475 >        ExtractIdentifier(Database.SQLDialect,
476 >          QuoteIdentifier(DataBase.SQLDialect, Name)) +
477          ''' ' +
478          'AND RDB$CONSTRAINT_TYPE = ''PRIMARY KEY''';
479        Query.Prepare;
# Line 542 | Line 482 | var
482        begin
483          Query.Close;
484          Query.SQL.Text := 'Alter Table ' +  {do not localize}
485 <          FormatIdentifier(Database.SQLDialect, FTableName) +
485 >          QuoteIdentifier(DataBase.SQLDialect, FTableName) +
486            ' Drop Constraint ' +
487 <          FormatIdentifier(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;
# Line 629 | Line 569 | var
569    Opts: TIndexOptions;
570    Flds: string;
571    Query, SubQuery: TIBSQL;
572 +  fn: string;
573 +  aField: TFieldDef;
574   begin
575    if not (csReading in ComponentState) then begin
576    if not Active and not FSwitchingIndex  then
# Line 646 | Line 588 | begin
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, FTableName) + '''';
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}
606 <          Fields := Trim(Query.Current.ByName('RDB$FIELD_NAME').AsString) {do not localize}
605 >        if (Query.FieldByName('RDB$SEGMENT_COUNT').AsInteger = 1) then {do not localize}
606 >        begin
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
611 >          else
612 >              Fields := fn;
613 >        end
614          else begin
615            SubQuery := TIBSQL.Create(self);
616          try
# Line 670 | Line 620 | begin
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, Name) +
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.FieldByName('RDB$FIELD_NAME').AsString); {do not localize}
632 +            aField := GetFieldDefFromAlias(fn);
633 +            if assigned(aField) then
634 +               fn := aField.Name;
635              if (Flds = '') then
636 <              Flds := TrimRight(SubQuery.Current.ByName('RDB$FIELD_NAME').AsString) {do not localize}
636 >               Flds := fn
637              else begin
638                Query.Next;
639 <              Flds := Flds + ';' + TrimRight(SubQuery.Current[0].AsString);
639 >              Flds := Flds + ';' + fn;
640              end;
641            end;
642            Fields := Flds;
# Line 716 | Line 671 | begin
671      Query.SQL.Text :=
672      'Select USER from RDB$RELATIONS where RDB$RELATION_NAME = ' + {do not localize}
673      '''' +
674 <    FormatIdentifierValue(Database.SQLDialect, FTableName) + '''';
674 >    ExtractIdentifier(Database.SQLDialect,
675 >      QuoteIdentifier(DataBase.SQLDialect, FTableName)) + '''';
676      Query.Prepare;
677      Query.ExecQuery;
678      Result := not Query.EOF;
# Line 758 | Line 714 | var
714          case DataType of
715            ftString:
716              FieldList := FieldList +
717 <              FormatIdentifier(Database.SQLDialect, Name) +
717 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
718                ' VARCHAR(' + IntToStr(Size) + ')'; {do not localize}
719            ftFixedChar:
720              FieldList := FieldList +
721 <              FormatIdentifier(Database.SQLDialect, Name) +
721 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
722                ' CHAR(' + IntToStr(Size) + ')'; {do not localize}
723 <          ftBoolean, ftSmallint, ftWord:
723 >          ftBoolean:
724              FieldList := FieldList +
725 <              FormatIdentifier(Database.SQLDialect, Name) +
725 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
726 >              ' BOOLEAN'; {do not localize}
727 >          ftSmallint, ftWord:
728 >            FieldList := FieldList +
729 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
730                ' SMALLINT'; {do not localize}
731            ftInteger:
732              FieldList := FieldList +
733 <              FormatIdentifier(Database.SQLDialect, Name) +
733 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
734                ' INTEGER'; {do not localize}
735            ftFloat, ftCurrency:
736              FieldList := FieldList +
737 <              FormatIdentifier(Database.SQLDialect, Name) +
737 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
738                ' DOUBLE PRECISION'; {do not localize}
739            ftBCD: begin
740              if (Database.SQLDialect = 1) then begin
# Line 785 | Line 745 | var
745              end;
746              if (Precision <= 4 ) then
747                FieldList := FieldList +
748 <                FormatIdentifier(Database.SQLDialect, Name) +
748 >                QuoteIdentifier(DataBase.SQLDialect, Name) +
749                  ' Numeric(18, 4)' {do not localize}
750              else
751                FieldList := FieldList +
752 <                FormatIdentifier(Database.SQLDialect, Name) +
752 >                QuoteIdentifier(DataBase.SQLDialect, Name) +
753                  ' Numeric(' + IntToStr(Precision) + ', 4)'; {do not localize}
754            end;
755            ftDate:
756              FieldList := FieldList +
757 <              FormatIdentifier(Database.SQLDialect, Name) +
757 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
758                ' DATE'; {do not localize}
759            ftTime:
760              FieldList := FieldList +
761 <              FormatIdentifier(Database.SQLDialect, Name) +
761 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
762                ' TIME'; {do not localize}
763            ftDateTime:
764              if (Database.SQLDialect = 1) then
765                FieldList := FieldList +
766 <              FormatIdentifier(Database.SQLDialect, Name) +
766 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
767                ' DATE' {do not localize}
768              else
769                FieldList := FieldList +
770 <              FormatIdentifier(Database.SQLDialect, Name) +
770 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
771                ' TIMESTAMP'; {do not localize}
772            ftLargeInt:
773              if (Database.SQLDialect = 1) then
774                IBError(ibxeFieldUnsupportedType,[nil])
775              else
776                FieldList := FieldList +
777 <                FormatIdentifier(Database.SQLDialect, Name) +
777 >                QuoteIdentifier(DataBase.SQLDialect, Name) +
778                  ' Numeric(18, 0)'; {do not localize}
779            ftBlob, ftMemo:
780              FieldList := FieldList +
781 <              FormatIdentifier(Database.SQLDialect, Name) +
781 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
782                ' BLOB SUB_TYPE 1'; {do not localize}
783            ftBytes, ftVarBytes, ftGraphic..ftTypedBinary:
784              FieldList := FieldList +
785 <              FormatIdentifier(Database.SQLDialect, Name) +
785 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
786                ' BLOB SUB_TYPE 0'; {do not localize}
787            ftUnknown, ftADT, ftArray, ftReference, ftDataSet,
788            ftCursor, ftWideString, ftAutoInc:
# Line 848 | Line 808 | var
808        Query.Database := Database;
809        Query.transaction := Transaction;
810        Query.SQL.Text := 'Create Table ' +
811 <        FormatIdentifier(Database.SQLDialect, FTableName) +
811 >        QuoteIdentifier(DataBase.SQLDialect, FTableName) +
812          ' (' + FieldList; {do not localize}
813        for I := 0 to IndexDefs.Count - 1 do
814        with IndexDefs[I] do
815          if ixPrimary in Options then
816          begin
817            Query.SQL.Text := Query.SQL.Text + ', CONSTRAINT ' +
818 <            FormatIdentifier(Database.SQLDialect, Name) +
818 >            QuoteIdentifier(DataBase.SQLDialect, Name) +
819              ' Primary Key (' +
820              FormatFieldsList(Fields) +
821              ')';
# Line 895 | Line 855 | begin
855      Query.Database := DataBase;
856      Query.Transaction := Transaction;
857      Query.SQL.Text := 'drop table ' +  {do not localize}
858 <      FormatIdentifier(Database.SQLDialect, FTableName);
858 >      QuoteIdentifier(DataBase.SQLDialect, FTableName);
859      Query.Prepare;
860      Query.ExecQuery;
861    finally
# Line 914 | Line 874 | begin
874      Query.Database := DataBase;
875      Query.Transaction := Transaction;
876      Query.SQL.Text := 'delete from ' + {do not localize}
877 <      FormatIdentifier(Database.SQLDialect, FTableName);
877 >      QuoteIdentifier(DataBase.SQLDialect, FTableName);
878      Query.Prepare;
879      Query.ExecQuery;
880      if Active then
# Line 927 | Line 887 | begin
887    end;
888   end;
889  
890 < procedure TIBTable.DataEvent(Event: TDataEvent; Info: Longint);
890 > procedure TIBTable.DataEvent(Event: TDataEvent; Info: Ptrint);
891   begin
892    if Event = dePropertyChange then begin
893      IndexDefs.Updated := False;
# Line 958 | Line 918 | begin
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, FTableName) + '''';
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;
# Line 1048 | Line 1009 | var
1009    i: Integer;
1010   begin
1011    if Database.SQLDialect = 1 then begin
1012 <    Value := FormatIdentifier(Database.SQLDialect, Value);
1012 >    Value := QuoteIdentifier(Database.SQLDialect, Value);
1013      Result := StringReplace (Value, ';', ', ', [rfReplaceAll]);
1014    end
1015    else begin
# Line 1058 | Line 1019 | begin
1019      begin
1020        FieldName := ExtractFieldName(Value, i);
1021        if Result = '' then
1022 <        Result := FormatIdentifier(Database.SQLDialect, FieldName)
1022 >        Result := QuoteIdentifier(Database.SQLDialect, FieldName)
1023        else
1024 <        Result := Result + ', ' + FormatIdentifier(Database.SQLDialect, FieldName);
1024 >        Result := Result + ', ' + QuoteIdentifier(Database.SQLDialect, FieldName);
1025      end;
1026    end;
1027   end;
# Line 1245 | Line 1206 | begin
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;
# Line 1279 | Line 1240 | var
1240    SQL: TStrings;
1241    OrderByStr: string;
1242    bWhereClausePresent: Boolean;
1243 +  fn: string;
1244 +  aField: TFieldDef;
1245   begin
1246    bWhereClausePresent := False;
1247    Database.CheckActive;
# Line 1293 | Line 1256 | begin
1256      OrderByStr := FormatFieldsList(FPrimaryIndexFields);
1257    SQL := TStringList.Create;
1258    SQL.Text := 'select ' + {do not localize}
1259 <    FormatIdentifier(Database.SQLDialect, FTableName) + '.*, '
1259 >    QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' {do not localize}
1260      + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1261 <    + FormatIdentifier(Database.SQLDialect, FTableName);
1261 >    + QuoteIdentifier(DataBase.SQLDialect, FTableName);
1262    if Filtered and (Filter <> '') then
1263    begin
1264      SQL.Text := SQL.Text + ' where ' + Filter; {do not localize}
# Line 1314 | Line 1277 | begin
1277      begin
1278        if i > 0 then
1279          SQL.Text := SQL.Text + 'AND ';
1280 +      aField := FieldDefs.Find(FDetailFieldsList.Strings[i]);
1281 +      if assigned(aField) then
1282 +         fn := GetDBAliasName(aField.FieldNo)
1283 +      else
1284 +          fn := FDetailFieldsList.Strings[i]; {something wrong if we get here - but should word}
1285        SQL.Text := SQL.Text +
1286 <        FormatIdentifier(Database.SQLDialect, FDetailFieldsList.Strings[i]) +
1319 <        ' = :' +
1320 <        FormatIdentifier(Database.SQLDialect, FMasterFieldsList.Strings[i]);
1286 >        QuoteIdentifier(DataBase.SQLDialect, fn) + ' = :' + FMasterFieldsList.Strings[i];
1287      end;
1288    end;
1289    if OrderByStr <> '' then
1290      SQL.Text := SQL.Text + ' order by ' + OrderByStr; {do not localize}
1291    SelectSQL.Assign(SQL);
1292    RefreshSQL.Text := 'select ' + {do not localize}
1293 <    FormatIdentifier(Database.SQLDialect, FTableName) + '.*, '
1293 >    QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' {do not localize}
1294      + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1295 <    + FormatIdentifier(Database.SQLDialect, FTableName) +
1295 >    + QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1296      ' where RDB$DB_KEY = :IBX_INTERNAL_DBKEY'; {do not localize}
1297    WhereDBKeyRefreshSQL.Assign(RefreshSQL);
1298    InternalPrepare;
# Line 1336 | Line 1302 | end;
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;
1357          InsertFieldList := InsertFieldList +
1358            FormatIdentifier(Database.SQLDialect, Name);
1359          InsertParamList := InsertParamList + ':' +
1360            FormatIdentifier(Database.SQLDialect, Name);
1357            UpdateFieldList := UpdateFieldList +
1358 <            FormatIdentifier(Database.SQLDialect, Name) +
1359 <            ' = :' +
1364 <            FormatIdentifier(Database.SQLDialect, Name);
1358 >            QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) +
1359 >            ' = :' + Name;
1360            if (DataType <> ftBlob) and (DataType <>ftMemo) then
1361              WhereAllFieldList := WhereAllFieldList +
1362 <              FormatIdentifier(Database.SQLDialect, Name) + ' = :' +
1368 <              FormatIdentifier(Database.SQLDialect, Name);{do not localize}
1362 >              QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) + ' = :' +  Name;
1363          end;
1364        end;
1365      end;
# Line 1374 | Line 1368 | var
1368    procedure GenerateWherePrimaryFieldList;
1369    var
1370      i: Integer;
1371 <    tmp: String;
1371 >    tmp, fn: String;
1372 >    aField: TFieldDef;
1373    begin
1374      i := 1;
1375      while i <= Length(FPrimaryIndexFields) do
1376      begin
1377        tmp := ExtractFieldName(FPrimaryIndexFields, i);
1378 +      aField := FieldDefs.Find(tmp);
1379 +      if assigned(aField) then
1380 +         fn := GetDBAliasName(aField.FieldNo)
1381 +      else
1382 +         fn := tmp; {something wrong if we get here - but will work in most cases}
1383        tmp :=
1384 <        FormatIdentifier(Database.SQLDialect, tmp) +  ' = :' +
1385 <        FormatIdentifier(Database.SQLDialect, tmp);{do not localize}
1384 >        QuoteIdentifier(DataBase.SQLDialect, fn) +  ' = :' +
1385 >        QuoteIdentifier(DataBase.SQLDialect, tmp);{do not localize}
1386        if WherePrimaryFieldList <> '' then
1387          WherePrimaryFieldList :=
1388            WherePrimaryFieldList + ' AND ' + tmp
# Line 1397 | Line 1397 | begin
1397    else
1398    begin
1399      DeleteSQL.Text := 'delete from ' + {do not localize}
1400 <      FormatIdentifier(Database.SQLDialect, FTableName) +
1400 >      QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1401        ' where RDB$DB_KEY = ' + ':IBX_INTERNAL_DBKEY'; {do not localize}
1402      GenerateFieldLists;
1403      InsertSQL.Text := 'insert into ' + {do not localize}
1404 <      FormatIdentifier(Database.SQLDialect, FTableName) +
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 <      FormatIdentifier(Database.SQLDialect, FTableName) +
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 <      FormatIdentifier(Database.SQLDialect, FTableName) + '.*, '
1412 >      QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, '
1413        + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1414 <      + FormatIdentifier(Database.SQLDialect, FTableName) +
1415 <      ' where ' + WhereAllFieldList; {do not localize}
1414 >      + QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1415 >      ' where ' + WhereAllFieldList ; {do not localize}
1416      if FPrimaryIndexFields <> '' then
1417      begin
1418        GenerateWherePrimaryFieldList;
1419        WherePrimaryRefreshSQL.Text := 'select ' + {do not localize}
1420 <        FormatIdentifier(Database.SQLDialect, FTableName) + '.*, ' {do not localize}
1420 >        QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' {do not localize}
1421          + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1422 <        + FormatIdentifier(Database.SQLDialect, FTableName) +
1422 >        + QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1423          ' where ' + WherePrimaryFieldList; {do not localize}
1424      end;
1425      try
# Line 1499 | Line 1499 | begin
1499      begin
1500        SetState(dsInactive);
1501        CloseCursor;
1502 <      OpenCursor;
1502 >      OpenCursor(false);
1503        SetState(dsBrowse);
1504      end;
1505    finally
# Line 1507 | Line 1507 | begin
1507    end;
1508   end;
1509  
1510 { TIBTable IProviderSupport }
1511
1512 function TIBTable.PSGetDefaultOrder: TIndexDef;
1513
1514  function GetIdx(IdxType: TIndexOption): TIndexDef;
1515  var
1516    i: Integer;
1517  begin
1518    Result := nil;
1519    for i := 0 to IndexDefs.Count - 1 do
1520      if IdxType in IndexDefs[i].Options then
1521      try
1522        Result := IndexDefs[i];
1523        GetFieldList(nil, Result.Fields);
1524        break;
1525      except
1526        Result := nil;
1527      end;
1528  end;
1529
1530 var
1531  DefIdx: TIndexDef;
1532 begin
1533  DefIdx := nil;
1534  IndexDefs.Update;
1535  try
1536    if IndexName <> '' then
1537      DefIdx := IndexDefs.Find(IndexName)
1538    else if IndexFieldNames <> '' then
1539      DefIdx := IndexDefs.FindIndexForFields(IndexFieldNames);
1540    if Assigned(DefIdx) then
1541      GetFieldList(nil, DefIdx.Fields);
1542  except
1543    DefIdx := nil;
1544  end;
1545  if not Assigned(DefIdx) then
1546    DefIdx := GetIdx(ixPrimary);
1547  if not Assigned(DefIdx) then
1548    DefIdx := GetIdx(ixUnique);
1549  if Assigned(DefIdx) then
1550  begin
1551    Result := TIndexDef.Create(nil);
1552    Result.Assign(DefIdx);
1553  end else
1554    Result := nil;
1555 end;
1556
1557 function TIBTable.PSGetIndexDefs(IndexTypes: TIndexOptions): TIndexDefs;
1558 begin
1559  Result := GetIndexDefs(IndexDefs, IndexTypes);
1560 end;
1561
1562 function TIBTable.PSGeTTableName: string;
1563 begin
1564  Result := FTableName;
1565 end;
1566
1567 procedure TIBTable.PSSetParams(AParams: TParams);
1568 begin
1569  if AParams.Count > 0 then
1570    Open;
1571  PSReset;
1572 end;
1573
1574 procedure TIBTable.PSSetCommandText(const CommandText: string);
1575 begin
1576  if CommandText <> '' then
1577    TableName := CommandText;
1578 end;
1579
1580 function TIBTable.PSGetKeyFields: string;
1581 var
1582  i, Idx: Integer;
1583  IndexFound: Boolean;
1584 begin
1585  Result := inherited PSGetKeyFields;
1586  if Result = '' then
1587  begin
1588    if not Exists then Exit;
1589    IndexFound := False;
1590    IndexDefs.Update;
1591    FieldDefs.Update;
1592    for i := 0 to IndexDefs.Count - 1 do
1593      if ixUnique in IndexDefs[I].Options then
1594      begin
1595        Idx := 1;
1596        Result := IndexDefs[I].Fields;
1597        IndexFound := False;
1598        while Idx <= Length(Result) do
1599        begin
1600          IndexFound := FindField(ExtractFieldName(Result, Idx)) <> nil;
1601          if not IndexFound then Break;
1602        end;
1603        if IndexFound then Break;
1604      end;
1605    if not IndexFound then
1606      Result := '';
1607  end;
1608 end;
1609
1510   end.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines