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 45 by tony, Tue Dec 6 10:33:46 2016 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 < uses Windows, SysUtils, Graphics, Classes, Controls, Db, StdVCL,
34 <     IB, IBDatabase, IBCustomDataSet, IBHeader, IBSQL, IBUtils;
38 > {$codepage UTF8}
39  
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;
# Line 178 | Line 182 | type
182  
183   implementation
184  
185 + uses FBMessages;
186 +
187   { TIBTable }
188  
189   constructor TIBTable.Create(AOwner: TComponent);
# Line 234 | Line 240 | begin
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;
# Line 285 | Line 295 | begin
295      QRefresh.SQL.Assign(WherePrimaryRefreshSQL)
296    else
297      QRefresh.SQL.Assign(WhereAllRefreshSQL);
298 <  inherited;
298 >  inherited InternalRefreshRow;
299   end;
300  
301   procedure TIBTable.DefChanged(Sender: TObject);
# Line 295 | Line 305 | 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}
327 <                        'F.RDB$FIELD_TYPE, F.RDB$FIELD_SUB_TYPE, ' + {do not localize}
328 <                        'F.RDB$EXTERNAL_LENGTH, F.RDB$EXTERNAL_SCALE, F.RDB$EXTERNAL_TYPE ' + {do not localize}
329 <                        '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;
348 <              end;
349 <              blr_long: 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 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;
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 >                StartTransaction;
329 >                DidActivate := true
330                end;
331 <              blr_timestamp: DataType := ftDateTime;
332 <              blr_sql_time: DataType := ftTime;
333 <              blr_sql_date: DataType := ftDate;
334 <              blr_blob:
335 <                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;
331 >         end;
332 >         Query.Prepare;
333 >         FieldDefsFromQuery(Query);
334 >         if DidActivate then
335 >            Query.Transaction.Rollback;
336      finally
337 <      Query.free;
402 <      Database.InternalTransaction.Commit;
337 >         Query.Free;
338      end;
339    end;
340   end;
# Line 425 | Line 360 | begin
360      if (ixPrimary in Options) then
361      begin
362       Query.SQL.Text := 'Alter Table ' + {do not localize}
363 <       FormatIdentifier(Database.SQLDialect, FTableName) +
363 >       QuoteIdentifier(Database.SQLDialect, FTableName) +
364         ' Add CONSTRAINT ' +   {do not localize}
365 <       FormatIdentifier(Database.SQLDialect, Name)
365 >       QuoteIdentifier(Database.SQLDialect, Name)
366         + ' Primary Key (' + {do not localize}
367         FormatFieldsList(Fields) +
368         ')';
369      end
370      else if ([ixUnique, ixDescending] * Options = [ixUnique, ixDescending]) then
371        Query.SQL.Text := 'Create unique Descending Index ' + {do not localize}
372 <                        FormatIdentifier(Database.SQLDialect, Name) +
372 >                        QuoteIdentifier(Database.SQLDialect, Name) +
373                          ' on ' + {do not localize}
374 <                        FormatIdentifier(Database.SQLDialect, FTableName) +
374 >                        QuoteIdentifier(Database.SQLDialect, FTableName) +
375                          ' (' + FieldList + ')'
376      else if (ixUnique in Options) then
377        Query.SQL.Text := 'Create unique Index ' + {do not localize}
378 <                        FormatIdentifier(Database.SQLDialect, Name) +
378 >                        QuoteIdentifier(Database.SQLDialect, Name) +
379                          ' on ' + {do not localize}
380 <                        FormatIdentifier(Database.SQLDialect, FTableName) +
380 >                        QuoteIdentifier(Database.SQLDialect, FTableName) +
381                          ' (' + FieldList + ')'
382      else if (ixDescending in Options) then
383        Query.SQL.Text := 'Create Descending Index ' + {do not localize}
384 <                        FormatIdentifier(Database.SQLDialect, Name) +
384 >                        QuoteIdentifier(Database.SQLDialect, Name) +
385                          ' on ' + {do not localize}
386 <                        FormatIdentifier(Database.SQLDialect, FTableName) +
386 >                        QuoteIdentifier(Database.SQLDialect, FTableName) +
387                          ' (' + FieldList + ')'
388      else
389        Query.SQL.Text := 'Create Index ' + {do not localize}
390 <                        FormatIdentifier(Database.SQLDialect, Name) +
390 >                        QuoteIdentifier(Database.SQLDialect, Name) +
391                          ' on ' + {do not localize}
392 <                        FormatIdentifier(Database.SQLDialect, FTableName) +
392 >                        QuoteIdentifier(Database.SQLDialect, FTableName) +
393                          ' (' + FieldList + ')';
394      Query.Prepare;
395      Query.ExecQuery;
# Line 475 | Line 410 | var
410        Query.Database := DataBase;
411        Query.Transaction := Transaction;
412        Query.SQL.Text := 'Drop index ' +  {do not localize}
413 <                         FormatIdentifier(Database.SQLDialect, Name);
413 >                         QuoteIdentifier(Database.SQLDialect, Name);
414        Query.Prepare;
415        Query.ExecQuery;
416        IndexDefs.Updated := False;
# Line 494 | Line 429 | var
429        Query.SQL.Text := 'Select ''foo'' from RDB$RELATION_CONSTRAINTS ' +
430          'where RDB$RELATION_NAME = ' +
431          '''' +
432 <        FormatIdentifierValue(Database.SQLDialect, FTableName) +
432 >        FormatIdentifierValue(Database.SQLDialect,
433 >          QuoteIdentifier(DataBase.SQLDialect, FTableName)) +
434          ''' ' +
435          ' AND RDB$CONSTRAINT_NAME = ' +
436          '''' +
437 <        FormatIdentifierValue(Database.SQLDialect, Name) +
437 >        FormatIdentifierValue(Database.SQLDialect,
438 >          QuoteIdentifier(DataBase.SQLDialect, Name)) +
439          ''' ' +
440          'AND RDB$CONSTRAINT_TYPE = ''PRIMARY KEY''';
441        Query.Prepare;
# Line 507 | Line 444 | var
444        begin
445          Query.Close;
446          Query.SQL.Text := 'Alter Table ' +  {do not localize}
447 <          FormatIdentifier(Database.SQLDialect, FTableName) +
447 >          QuoteIdentifier(DataBase.SQLDialect, FTableName) +
448            ' Drop Constraint ' +
449 <          FormatIdentifier(Database.SQLDialect, Name);
449 >          QuoteIdentifier(DataBase.SQLDialect, Name);
450          Query.Prepare;
451          Query.ExecQuery;
452          IndexDefs.Updated := False;
# Line 529 | Line 466 | var
466        Query.SQL.Text := 'Select RDB$CONSTRAINT_NAME from RDB$RELATION_CONSTRAINTS ' +
467          'where RDB$RELATION_NAME = ' +
468          '''' +
469 <        FormatIdentifierValue(Database.SQLDialect, FTableName) +
469 >        FormatIdentifierValue(Database.SQLDialect,
470 >          QuoteIdentifier(DataBase.SQLDialect, FTableName)) +
471          ''' ' +
472          'AND RDB$INDEX_NAME = ' +
473          '''' +
474 <        FormatIdentifierValue(Database.SQLDialect, Name) +
474 >        FormatIdentifierValue(Database.SQLDialect,
475 >          QuoteIdentifier(DataBase.SQLDialect, Name)) +
476          ''' ' +
477          'AND RDB$CONSTRAINT_TYPE = ''PRIMARY KEY''';
478        Query.Prepare;
# Line 542 | Line 481 | var
481        begin
482          Query.Close;
483          Query.SQL.Text := 'Alter Table ' +  {do not localize}
484 <          FormatIdentifier(Database.SQLDialect, FTableName) +
484 >          QuoteIdentifier(DataBase.SQLDialect, FTableName) +
485            ' Drop Constraint ' +
486 <          FormatIdentifier(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;
# Line 629 | Line 568 | var
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
# Line 646 | Line 587 | begin
587      'I.RDB$SEGMENT_COUNT, S.RDB$FIELD_NAME from RDB$INDICES I, ' + {do not localize}
588      'RDB$INDEX_SEGMENTS S where I.RDB$INDEX_NAME = S.RDB$INDEX_NAME '+ {do not localize}
589      'and I.RDB$RELATION_NAME = ' + '''' + {do not localize}
590 <     FormatIdentifierValue(Database.SQLDialect, FTableName) + '''';
590 >     FormatIdentifierValue(Database.SQLDialect,
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
# Line 670 | Line 619 | begin
619            SubQuery.SQL.Text :=
620           'Select RDB$FIELD_NAME from RDB$INDEX_SEGMENTS where RDB$INDEX_NAME = ' + {do not localize}
621            '''' +
622 <          FormatIdentifierValue(Database.SQLDialect, Name) +
622 >          FormatIdentifierValue(Database.SQLDialect,
623 >            QuoteIdentifier(DataBase.SQLDialect, Name)) +
624            '''' + 'ORDER BY RDB$FIELD_POSITION'; {do not localize}
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;
# Line 716 | Line 670 | begin
670      Query.SQL.Text :=
671      'Select USER from RDB$RELATIONS where RDB$RELATION_NAME = ' + {do not localize}
672      '''' +
673 <    FormatIdentifierValue(Database.SQLDialect, FTableName) + '''';
673 >    FormatIdentifierValue(Database.SQLDialect,
674 >      QuoteIdentifier(DataBase.SQLDialect, FTableName)) + '''';
675      Query.Prepare;
676      Query.ExecQuery;
677      Result := not Query.EOF;
# Line 758 | Line 713 | var
713          case DataType of
714            ftString:
715              FieldList := FieldList +
716 <              FormatIdentifier(Database.SQLDialect, Name) +
716 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
717                ' VARCHAR(' + IntToStr(Size) + ')'; {do not localize}
718            ftFixedChar:
719              FieldList := FieldList +
720 <              FormatIdentifier(Database.SQLDialect, Name) +
720 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
721                ' CHAR(' + IntToStr(Size) + ')'; {do not localize}
722 <          ftBoolean, ftSmallint, ftWord:
722 >          ftBoolean:
723              FieldList := FieldList +
724 <              FormatIdentifier(Database.SQLDialect, Name) +
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}
730            ftInteger:
731              FieldList := FieldList +
732 <              FormatIdentifier(Database.SQLDialect, Name) +
732 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
733                ' INTEGER'; {do not localize}
734            ftFloat, ftCurrency:
735              FieldList := FieldList +
736 <              FormatIdentifier(Database.SQLDialect, Name) +
736 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
737                ' DOUBLE PRECISION'; {do not localize}
738            ftBCD: begin
739              if (Database.SQLDialect = 1) then begin
# Line 785 | Line 744 | var
744              end;
745              if (Precision <= 4 ) then
746                FieldList := FieldList +
747 <                FormatIdentifier(Database.SQLDialect, Name) +
747 >                QuoteIdentifier(DataBase.SQLDialect, Name) +
748                  ' Numeric(18, 4)' {do not localize}
749              else
750                FieldList := FieldList +
751 <                FormatIdentifier(Database.SQLDialect, Name) +
751 >                QuoteIdentifier(DataBase.SQLDialect, Name) +
752                  ' Numeric(' + IntToStr(Precision) + ', 4)'; {do not localize}
753            end;
754            ftDate:
755              FieldList := FieldList +
756 <              FormatIdentifier(Database.SQLDialect, Name) +
756 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
757                ' DATE'; {do not localize}
758            ftTime:
759              FieldList := FieldList +
760 <              FormatIdentifier(Database.SQLDialect, Name) +
760 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
761                ' TIME'; {do not localize}
762            ftDateTime:
763              if (Database.SQLDialect = 1) then
764                FieldList := FieldList +
765 <              FormatIdentifier(Database.SQLDialect, Name) +
765 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
766                ' DATE' {do not localize}
767              else
768                FieldList := FieldList +
769 <              FormatIdentifier(Database.SQLDialect, Name) +
769 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
770                ' TIMESTAMP'; {do not localize}
771            ftLargeInt:
772              if (Database.SQLDialect = 1) then
773                IBError(ibxeFieldUnsupportedType,[nil])
774              else
775                FieldList := FieldList +
776 <                FormatIdentifier(Database.SQLDialect, Name) +
776 >                QuoteIdentifier(DataBase.SQLDialect, Name) +
777                  ' Numeric(18, 0)'; {do not localize}
778            ftBlob, ftMemo:
779              FieldList := FieldList +
780 <              FormatIdentifier(Database.SQLDialect, Name) +
780 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
781                ' BLOB SUB_TYPE 1'; {do not localize}
782            ftBytes, ftVarBytes, ftGraphic..ftTypedBinary:
783              FieldList := FieldList +
784 <              FormatIdentifier(Database.SQLDialect, Name) +
784 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
785                ' BLOB SUB_TYPE 0'; {do not localize}
786            ftUnknown, ftADT, ftArray, ftReference, ftDataSet,
787            ftCursor, ftWideString, ftAutoInc:
# Line 848 | Line 807 | var
807        Query.Database := Database;
808        Query.transaction := Transaction;
809        Query.SQL.Text := 'Create Table ' +
810 <        FormatIdentifier(Database.SQLDialect, FTableName) +
810 >        QuoteIdentifier(DataBase.SQLDialect, FTableName) +
811          ' (' + FieldList; {do not localize}
812        for I := 0 to IndexDefs.Count - 1 do
813        with IndexDefs[I] do
814          if ixPrimary in Options then
815          begin
816            Query.SQL.Text := Query.SQL.Text + ', CONSTRAINT ' +
817 <            FormatIdentifier(Database.SQLDialect, Name) +
817 >            QuoteIdentifier(DataBase.SQLDialect, Name) +
818              ' Primary Key (' +
819              FormatFieldsList(Fields) +
820              ')';
# Line 895 | Line 854 | begin
854      Query.Database := DataBase;
855      Query.Transaction := Transaction;
856      Query.SQL.Text := 'drop table ' +  {do not localize}
857 <      FormatIdentifier(Database.SQLDialect, FTableName);
857 >      QuoteIdentifier(DataBase.SQLDialect, FTableName);
858      Query.Prepare;
859      Query.ExecQuery;
860    finally
# Line 914 | Line 873 | begin
873      Query.Database := DataBase;
874      Query.Transaction := Transaction;
875      Query.SQL.Text := 'delete from ' + {do not localize}
876 <      FormatIdentifier(Database.SQLDialect, FTableName);
876 >      QuoteIdentifier(DataBase.SQLDialect, FTableName);
877      Query.Prepare;
878      Query.ExecQuery;
879      if Active then
# Line 927 | Line 886 | begin
886    end;
887   end;
888  
889 < procedure TIBTable.DataEvent(Event: TDataEvent; Info: Longint);
889 > procedure TIBTable.DataEvent(Event: TDataEvent; Info: Ptrint);
890   begin
891    if Event = dePropertyChange then begin
892      IndexDefs.Updated := False;
# Line 958 | Line 917 | begin
917      Query.SQL.Text := 'Select RDB$SYSTEM_FLAG, RDB$DBKEY_LENGTH ' + {do not localize}
918                      'from RDB$RELATIONS where RDB$RELATION_NAME = ' + {do not localize}
919                      '''' +
920 <                    FormatIdentifierValue(Database.SQLDialect, FTableName) + '''';
920 >                    FormatIdentifierValue(Database.SQLDialect,
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;
# Line 1048 | Line 1008 | var
1008    i: Integer;
1009   begin
1010    if Database.SQLDialect = 1 then begin
1011 <    Value := FormatIdentifier(Database.SQLDialect, Value);
1011 >    Value := QuoteIdentifier(Database.SQLDialect, Value);
1012      Result := StringReplace (Value, ';', ', ', [rfReplaceAll]);
1013    end
1014    else begin
# Line 1058 | Line 1018 | begin
1018      begin
1019        FieldName := ExtractFieldName(Value, i);
1020        if Result = '' then
1021 <        Result := FormatIdentifier(Database.SQLDialect, FieldName)
1021 >        Result := QuoteIdentifier(Database.SQLDialect, FieldName)
1022        else
1023 <        Result := Result + ', ' + FormatIdentifier(Database.SQLDialect, FieldName);
1023 >        Result := Result + ', ' + QuoteIdentifier(Database.SQLDialect, FieldName);
1024      end;
1025    end;
1026   end;
# Line 1245 | Line 1205 | begin
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;
# Line 1279 | Line 1239 | var
1239    SQL: TStrings;
1240    OrderByStr: string;
1241    bWhereClausePresent: Boolean;
1242 +  fn: string;
1243 +  aField: TFieldDef;
1244   begin
1245    bWhereClausePresent := False;
1246    Database.CheckActive;
# Line 1293 | Line 1255 | begin
1255      OrderByStr := FormatFieldsList(FPrimaryIndexFields);
1256    SQL := TStringList.Create;
1257    SQL.Text := 'select ' + {do not localize}
1258 <    FormatIdentifier(Database.SQLDialect, FTableName) + '.*, '
1258 >    QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' {do not localize}
1259      + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1260 <    + FormatIdentifier(Database.SQLDialect, FTableName);
1260 >    + QuoteIdentifier(DataBase.SQLDialect, FTableName);
1261    if Filtered and (Filter <> '') then
1262    begin
1263      SQL.Text := SQL.Text + ' where ' + Filter; {do not localize}
# Line 1314 | Line 1276 | begin
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 <        FormatIdentifier(Database.SQLDialect, FDetailFieldsList.Strings[i]) +
1319 <        ' = :' +
1320 <        FormatIdentifier(Database.SQLDialect, FMasterFieldsList.Strings[i]);
1285 >        QuoteIdentifier(DataBase.SQLDialect, fn) + ' = :' + FMasterFieldsList.Strings[i];
1286      end;
1287    end;
1288    if OrderByStr <> '' then
1289      SQL.Text := SQL.Text + ' order by ' + OrderByStr; {do not localize}
1290    SelectSQL.Assign(SQL);
1291    RefreshSQL.Text := 'select ' + {do not localize}
1292 <    FormatIdentifier(Database.SQLDialect, FTableName) + '.*, '
1292 >    QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' {do not localize}
1293      + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1294 <    + FormatIdentifier(Database.SQLDialect, FTableName) +
1294 >    + QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1295      ' where RDB$DB_KEY = :IBX_INTERNAL_DBKEY'; {do not localize}
1296    WhereDBKeyRefreshSQL.Assign(RefreshSQL);
1297    InternalPrepare;
# Line 1355 | Line 1320 | var
1320                WhereAllFieldList := WhereAllFieldList + ' AND ';
1321            end;
1322            InsertFieldList := InsertFieldList +
1323 <            FormatIdentifier(Database.SQLDialect, Name);
1324 <          InsertParamList := InsertParamList + ':' +
1360 <            FormatIdentifier(Database.SQLDialect, Name);
1323 >            QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo));
1324 >          InsertParamList := InsertParamList + ':' +  Name;
1325            UpdateFieldList := UpdateFieldList +
1326 <            FormatIdentifier(Database.SQLDialect, Name) +
1327 <            ' = :' +
1364 <            FormatIdentifier(Database.SQLDialect, Name);
1326 >            QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) +
1327 >            ' = :' + Name;
1328            if (DataType <> ftBlob) and (DataType <>ftMemo) then
1329              WhereAllFieldList := WhereAllFieldList +
1330 <              FormatIdentifier(Database.SQLDialect, Name) + ' = :' +
1368 <              FormatIdentifier(Database.SQLDialect, Name);{do not localize}
1330 >              QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) + ' = :' +  Name;
1331          end;
1332        end;
1333      end;
# Line 1374 | Line 1336 | var
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 <        FormatIdentifier(Database.SQLDialect, tmp) +  ' = :' +
1353 <        FormatIdentifier(Database.SQLDialect, tmp);{do not localize}
1352 >        QuoteIdentifier(DataBase.SQLDialect, fn) +  ' = :' +
1353 >        QuoteIdentifier(DataBase.SQLDialect, tmp);{do not localize}
1354        if WherePrimaryFieldList <> '' then
1355          WherePrimaryFieldList :=
1356            WherePrimaryFieldList + ' AND ' + tmp
# Line 1397 | Line 1365 | begin
1365    else
1366    begin
1367      DeleteSQL.Text := 'delete from ' + {do not localize}
1368 <      FormatIdentifier(Database.SQLDialect, FTableName) +
1368 >      QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1369        ' where RDB$DB_KEY = ' + ':IBX_INTERNAL_DBKEY'; {do not localize}
1370      GenerateFieldLists;
1371      InsertSQL.Text := 'insert into ' + {do not localize}
1372 <      FormatIdentifier(Database.SQLDialect, FTableName) +
1372 >      QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1373      ' (' + InsertFieldList + {do not localize}
1374        ') values (' + InsertParamList + ')'; {do not localize}
1375      ModifySQL.Text := 'update ' +
1376 <      FormatIdentifier(Database.SQLDialect, FTableName) +
1376 >      QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1377        ' set ' + UpdateFieldList + {do not localize}
1378        ' where RDB$DB_KEY = :IBX_INTERNAL_DBKEY'; {do not localize}
1379      WhereAllRefreshSQL.Text := 'select ' +  {do not localize}
1380 <      FormatIdentifier(Database.SQLDialect, FTableName) + '.*, '
1380 >      QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, '
1381        + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1382 <      + FormatIdentifier(Database.SQLDialect, FTableName) +
1382 >      + QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1383        ' where ' + WhereAllFieldList; {do not localize}
1384      if FPrimaryIndexFields <> '' then
1385      begin
1386        GenerateWherePrimaryFieldList;
1387        WherePrimaryRefreshSQL.Text := 'select ' + {do not localize}
1388 <        FormatIdentifier(Database.SQLDialect, FTableName) + '.*, ' {do not localize}
1388 >        QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' {do not localize}
1389          + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1390 <        + FormatIdentifier(Database.SQLDialect, FTableName) +
1390 >        + QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1391          ' where ' + WherePrimaryFieldList; {do not localize}
1392      end;
1393      try
# Line 1499 | Line 1467 | begin
1467      begin
1468        SetState(dsInactive);
1469        CloseCursor;
1470 <      OpenCursor;
1470 >      OpenCursor(false);
1471        SetState(dsBrowse);
1472      end;
1473    finally
# Line 1507 | Line 1475 | begin
1475    end;
1476   end;
1477  
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
1478   end.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines