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 33 by tony, Sat Jul 18 12:30:52 2015 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 > interface
39  
40 + uses SysUtils, Classes, DB, IB,  IBCustomDataSet,
41 +     IBSQL, IBUtils;
42 +    
43   type
44  
45   { TIBTable }
# Line 103 | Line 110 | type
110      procedure Reopen;
111  
112    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;
113  
114      procedure DoOnNewRecord; override;
115      procedure GetIndexParams(const IndexName: string; FieldsIndex: Boolean;
116        var IndexedName: string);
117      function GetCanModify: Boolean; override;
118      procedure UpdateIndexDefs; override;
119 <    procedure DataEvent(Event: TDataEvent; Info: Longint); override;
120 <    procedure DefChanged(Sender: TObject); override;
119 >    procedure DataEvent(Event: TDataEvent; Info: Ptrint); override;
120 >    procedure DefChanged(Sender: TObject); virtual;
121      function GetDataSource: TDataSource; override;
122      procedure InitFieldDefs; override;
123      procedure InternalClose; override;
# Line 137 | Line 137 | type
137      procedure DeleteIndex(const Name: string);
138      procedure DeleteTable;
139      procedure EmptyTable;
140 <    procedure GetDetailLinkFields(MasterFields, DetailFields: TList); override;
140 >    procedure GetDetailLinkFields(MasterFields, DetailFields: TList); virtual;
141      procedure GetIndexNames(List: TStrings);
142      procedure GotoCurrent(Table: TIBTable);
143      property CurrentDBKey: TIBDBKey read GetCurrentDBKey;
# Line 147 | Line 147 | type
147      property TableNames: TStrings read GetTableNames;
148  
149    published
150 +    property AutoCommit;
151      property Active;
152      property BufferChunks;
153      property CachedUpdates;
154 <    property Constraints stored ConstraintsStored;
154 >    property DataSetCloseAction;
155 > //    property Constraints stored ConstraintsStored;
156      property DefaultIndex: Boolean read FDefaultIndex write FDefaultIndex default True;
157      property FieldDefs stored FieldDefsStored;
158      property Filter;
# Line 234 | Line 236 | begin
236        GenerateUpdateSQL;
237      FRegenerateSQL := False;
238    end;
239 + {  writeln(SelectSQL.Text);
240 +  writeln(InsertSQL.Text);
241 +  writeln(DeleteSQL.Text);
242 +  writeln(ModifySQL.Text); }
243    SetParams;
244    inherited InternalOpen;
245   end;
# Line 285 | Line 291 | begin
291      QRefresh.SQL.Assign(WherePrimaryRefreshSQL)
292    else
293      QRefresh.SQL.Assign(WhereAllRefreshSQL);
294 <  inherited;
294 >  inherited InternalRefreshRow;
295   end;
296  
297   procedure TIBTable.DefChanged(Sender: TObject);
# Line 295 | Line 301 | end;
301  
302   procedure TIBTable.InitFieldDefs;
303   var
304 <  sqlscale: Integer;
304 >  DidActivate: Boolean;
305    Query: TIBSQL;
306   begin
307    if FTableName = '' then IBError(ibxeNoTableName, [nil]);
308 <  if (InternalPrepared) then InternalInitFieldDefs else
308 >  if (InternalPrepared) then
309 >     InternalInitFieldDefs
310 >  else
311    begin
312 <    Database.InternalTransaction.StartTransaction;
312 >    {Get the field defs from a simple query on the table}
313 >    ActivateConnection;
314      Query := TIBSQL.Create(self);
315      try
316 <      Query.GoToFirstRecordOnExecute := False;
317 <      Query.Database := DataBase;
318 <      Query.Transaction := Database.InternalTransaction;
319 <      Query.SQL.Text := 'Select R.RDB$FIELD_NAME, R.RDB$FIELD_POSITION, ' + {do not localize}
320 <                        'F.RDB$COMPUTED_BLR, F.RDB$DEFAULT_VALUE, ' + {do not localize}
321 <                        'F.RDB$NULL_FLAG, ' + {do not localize}
322 <                        'F.RDB$FIELD_LENGTH, F.RDB$FIELD_SCALE, ' + {do not localize}
323 <                        'F.RDB$FIELD_TYPE, F.RDB$FIELD_SUB_TYPE, ' + {do not localize}
324 <                        'F.RDB$EXTERNAL_LENGTH, F.RDB$EXTERNAL_SCALE, F.RDB$EXTERNAL_TYPE ' + {do not localize}
325 <                        '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;
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);
316 >         Query.Database := DataBase;
317 >         Query.Transaction := Database.InternalTransaction;
318 >         DidActivate := false;
319 >         Query.SQL.Text := 'Select * from ' + QuoteIdentifier(DataBase.SQLDialect, FTableName);
320 >         with Query.Transaction do
321 >         begin
322 >              if not InTransaction then
323 >              begin
324 >                StartTransaction;
325 >                DidActivate := true
326                end;
327 <              else
328 <                DataType := ftUnknown;
329 <            end;
330 <            if not (Query.Current.ByName('RDB$COMPUTED_BLR').IsNull) then {do not localize}
331 <            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;
327 >         end;
328 >         Query.Prepare;
329 >         if DidActivate then
330 >            Query.Transaction.Rollback;
331 >         FieldDefsFromQuery(Query);
332      finally
333 <      Query.free;
402 <      Database.InternalTransaction.Commit;
333 >         Query.Free;
334      end;
335    end;
336   end;
# Line 425 | Line 356 | begin
356      if (ixPrimary in Options) then
357      begin
358       Query.SQL.Text := 'Alter Table ' + {do not localize}
359 <       FormatIdentifier(Database.SQLDialect, FTableName) +
359 >       QuoteIdentifier(Database.SQLDialect, FTableName) +
360         ' Add CONSTRAINT ' +   {do not localize}
361 <       FormatIdentifier(Database.SQLDialect, Name)
361 >       QuoteIdentifier(Database.SQLDialect, Name)
362         + ' Primary Key (' + {do not localize}
363         FormatFieldsList(Fields) +
364         ')';
365      end
366      else if ([ixUnique, ixDescending] * Options = [ixUnique, ixDescending]) then
367        Query.SQL.Text := 'Create unique Descending Index ' + {do not localize}
368 <                        FormatIdentifier(Database.SQLDialect, Name) +
368 >                        QuoteIdentifier(Database.SQLDialect, Name) +
369                          ' on ' + {do not localize}
370 <                        FormatIdentifier(Database.SQLDialect, FTableName) +
370 >                        QuoteIdentifier(Database.SQLDialect, FTableName) +
371                          ' (' + FieldList + ')'
372      else if (ixUnique in Options) then
373        Query.SQL.Text := 'Create unique Index ' + {do not localize}
374 <                        FormatIdentifier(Database.SQLDialect, Name) +
374 >                        QuoteIdentifier(Database.SQLDialect, Name) +
375                          ' on ' + {do not localize}
376 <                        FormatIdentifier(Database.SQLDialect, FTableName) +
376 >                        QuoteIdentifier(Database.SQLDialect, FTableName) +
377                          ' (' + FieldList + ')'
378      else if (ixDescending in Options) then
379        Query.SQL.Text := 'Create Descending Index ' + {do not localize}
380 <                        FormatIdentifier(Database.SQLDialect, Name) +
380 >                        QuoteIdentifier(Database.SQLDialect, Name) +
381                          ' on ' + {do not localize}
382 <                        FormatIdentifier(Database.SQLDialect, FTableName) +
382 >                        QuoteIdentifier(Database.SQLDialect, FTableName) +
383                          ' (' + FieldList + ')'
384      else
385        Query.SQL.Text := 'Create Index ' + {do not localize}
386 <                        FormatIdentifier(Database.SQLDialect, Name) +
386 >                        QuoteIdentifier(Database.SQLDialect, Name) +
387                          ' on ' + {do not localize}
388 <                        FormatIdentifier(Database.SQLDialect, FTableName) +
388 >                        QuoteIdentifier(Database.SQLDialect, FTableName) +
389                          ' (' + FieldList + ')';
390      Query.Prepare;
391      Query.ExecQuery;
# Line 475 | Line 406 | var
406        Query.Database := DataBase;
407        Query.Transaction := Transaction;
408        Query.SQL.Text := 'Drop index ' +  {do not localize}
409 <                         FormatIdentifier(Database.SQLDialect, Name);
409 >                         QuoteIdentifier(Database.SQLDialect, Name);
410        Query.Prepare;
411        Query.ExecQuery;
412        IndexDefs.Updated := False;
# Line 494 | Line 425 | var
425        Query.SQL.Text := 'Select ''foo'' from RDB$RELATION_CONSTRAINTS ' +
426          'where RDB$RELATION_NAME = ' +
427          '''' +
428 <        FormatIdentifierValue(Database.SQLDialect, FTableName) +
428 >        FormatIdentifierValue(Database.SQLDialect,
429 >          QuoteIdentifier(DataBase.SQLDialect, FTableName)) +
430          ''' ' +
431          ' AND RDB$CONSTRAINT_NAME = ' +
432          '''' +
433 <        FormatIdentifierValue(Database.SQLDialect, Name) +
433 >        FormatIdentifierValue(Database.SQLDialect,
434 >          QuoteIdentifier(DataBase.SQLDialect, Name)) +
435          ''' ' +
436          'AND RDB$CONSTRAINT_TYPE = ''PRIMARY KEY''';
437        Query.Prepare;
# Line 507 | Line 440 | var
440        begin
441          Query.Close;
442          Query.SQL.Text := 'Alter Table ' +  {do not localize}
443 <          FormatIdentifier(Database.SQLDialect, FTableName) +
443 >          QuoteIdentifier(DataBase.SQLDialect, FTableName) +
444            ' Drop Constraint ' +
445 <          FormatIdentifier(Database.SQLDialect, Name);
445 >          QuoteIdentifier(DataBase.SQLDialect, Name);
446          Query.Prepare;
447          Query.ExecQuery;
448          IndexDefs.Updated := False;
# Line 529 | Line 462 | var
462        Query.SQL.Text := 'Select RDB$CONSTRAINT_NAME from RDB$RELATION_CONSTRAINTS ' +
463          'where RDB$RELATION_NAME = ' +
464          '''' +
465 <        FormatIdentifierValue(Database.SQLDialect, FTableName) +
465 >        FormatIdentifierValue(Database.SQLDialect,
466 >          QuoteIdentifier(DataBase.SQLDialect, FTableName)) +
467          ''' ' +
468          'AND RDB$INDEX_NAME = ' +
469          '''' +
470 <        FormatIdentifierValue(Database.SQLDialect, Name) +
470 >        FormatIdentifierValue(Database.SQLDialect,
471 >          QuoteIdentifier(DataBase.SQLDialect, Name)) +
472          ''' ' +
473          'AND RDB$CONSTRAINT_TYPE = ''PRIMARY KEY''';
474        Query.Prepare;
# Line 542 | Line 477 | var
477        begin
478          Query.Close;
479          Query.SQL.Text := 'Alter Table ' +  {do not localize}
480 <          FormatIdentifier(Database.SQLDialect, FTableName) +
480 >          QuoteIdentifier(DataBase.SQLDialect, FTableName) +
481            ' Drop Constraint ' +
482 <          FormatIdentifier(Database.SQLDialect, Query.Current.ByName('RDB$CONSTRAINT_NAME').AsString);
482 >          QuoteIdentifier(DataBase.SQLDialect, Query.Current.ByName('RDB$CONSTRAINT_NAME').AsString);
483          Query.Prepare;
484          Query.ExecQuery;
485          IndexDefs.Updated := False;
# Line 629 | Line 564 | var
564    Opts: TIndexOptions;
565    Flds: string;
566    Query, SubQuery: TIBSQL;
567 +  fn: string;
568 +  aField: TFieldDef;
569   begin
570    if not (csReading in ComponentState) then begin
571    if not Active and not FSwitchingIndex  then
# Line 646 | Line 583 | begin
583      'I.RDB$SEGMENT_COUNT, S.RDB$FIELD_NAME from RDB$INDICES I, ' + {do not localize}
584      'RDB$INDEX_SEGMENTS S where I.RDB$INDEX_NAME = S.RDB$INDEX_NAME '+ {do not localize}
585      'and I.RDB$RELATION_NAME = ' + '''' + {do not localize}
586 <     FormatIdentifierValue(Database.SQLDialect, FTableName) + '''';
586 >     FormatIdentifierValue(Database.SQLDialect,
587 >       QuoteIdentifier(DataBase.SQLDialect, FTableName)) + '''';
588      Query.Prepare;
589      Query.ExecQuery;
590      while (not Query.EOF) and (Query.Next <> nil) do
# Line 660 | Line 598 | begin
598          if Query.Current.ByName('RDB$INDEX_TYPE').AsInteger = 2  then Include(Opts, ixDescending); {do not localize}
599          Options := Opts;
600          if (Query.Current.ByName('RDB$SEGMENT_COUNT').AsInteger = 1) then {do not localize}
601 <          Fields := Trim(Query.Current.ByName('RDB$FIELD_NAME').AsString) {do not localize}
601 >        begin
602 >          fn := Trim(Query.Current.ByName('RDB$FIELD_NAME').AsString); {do not localize}
603 >          aField := GetFieldDefFromAlias(fn);
604 >          if assigned(aField) then
605 >             Fields := aField.Name
606 >          else
607 >              Fields := fn;
608 >        end
609          else begin
610            SubQuery := TIBSQL.Create(self);
611          try
# Line 670 | Line 615 | begin
615            SubQuery.SQL.Text :=
616           'Select RDB$FIELD_NAME from RDB$INDEX_SEGMENTS where RDB$INDEX_NAME = ' + {do not localize}
617            '''' +
618 <          FormatIdentifierValue(Database.SQLDialect, Name) +
618 >          FormatIdentifierValue(Database.SQLDialect,
619 >            QuoteIdentifier(DataBase.SQLDialect, Name)) +
620            '''' + 'ORDER BY RDB$FIELD_POSITION'; {do not localize}
621            SubQuery.Prepare;
622            SubQuery.ExecQuery;
623            Flds := '';
624            while (not SubQuery.EOF) and (SubQuery.Next <> nil) do
625            begin
626 +            fn := TrimRight(SubQuery.Current.ByName('RDB$FIELD_NAME').AsString); {do not localize}
627 +            aField := GetFieldDefFromAlias(fn);
628 +            if assigned(aField) then
629 +               fn := aField.Name;
630              if (Flds = '') then
631 <              Flds := TrimRight(SubQuery.Current.ByName('RDB$FIELD_NAME').AsString) {do not localize}
631 >               Flds := fn
632              else begin
633                Query.Next;
634 <              Flds := Flds + ';' + TrimRight(SubQuery.Current[0].AsString);
634 >              Flds := Flds + ';' + fn;
635              end;
636            end;
637            Fields := Flds;
# Line 716 | Line 666 | begin
666      Query.SQL.Text :=
667      'Select USER from RDB$RELATIONS where RDB$RELATION_NAME = ' + {do not localize}
668      '''' +
669 <    FormatIdentifierValue(Database.SQLDialect, FTableName) + '''';
669 >    FormatIdentifierValue(Database.SQLDialect,
670 >      QuoteIdentifier(DataBase.SQLDialect, FTableName)) + '''';
671      Query.Prepare;
672      Query.ExecQuery;
673      Result := not Query.EOF;
# Line 758 | Line 709 | var
709          case DataType of
710            ftString:
711              FieldList := FieldList +
712 <              FormatIdentifier(Database.SQLDialect, Name) +
712 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
713                ' VARCHAR(' + IntToStr(Size) + ')'; {do not localize}
714            ftFixedChar:
715              FieldList := FieldList +
716 <              FormatIdentifier(Database.SQLDialect, Name) +
716 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
717                ' CHAR(' + IntToStr(Size) + ')'; {do not localize}
718 <          ftBoolean, ftSmallint, ftWord:
718 >          ftBoolean:
719 >            FieldList := FieldList +
720 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
721 >              ' BOOLEAN'; {do not localize}
722 >          ftSmallint, ftWord:
723              FieldList := FieldList +
724 <              FormatIdentifier(Database.SQLDialect, Name) +
724 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
725                ' SMALLINT'; {do not localize}
726            ftInteger:
727              FieldList := FieldList +
728 <              FormatIdentifier(Database.SQLDialect, Name) +
728 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
729                ' INTEGER'; {do not localize}
730            ftFloat, ftCurrency:
731              FieldList := FieldList +
732 <              FormatIdentifier(Database.SQLDialect, Name) +
732 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
733                ' DOUBLE PRECISION'; {do not localize}
734            ftBCD: begin
735              if (Database.SQLDialect = 1) then begin
# Line 785 | Line 740 | var
740              end;
741              if (Precision <= 4 ) then
742                FieldList := FieldList +
743 <                FormatIdentifier(Database.SQLDialect, Name) +
743 >                QuoteIdentifier(DataBase.SQLDialect, Name) +
744                  ' Numeric(18, 4)' {do not localize}
745              else
746                FieldList := FieldList +
747 <                FormatIdentifier(Database.SQLDialect, Name) +
747 >                QuoteIdentifier(DataBase.SQLDialect, Name) +
748                  ' Numeric(' + IntToStr(Precision) + ', 4)'; {do not localize}
749            end;
750            ftDate:
751              FieldList := FieldList +
752 <              FormatIdentifier(Database.SQLDialect, Name) +
752 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
753                ' DATE'; {do not localize}
754            ftTime:
755              FieldList := FieldList +
756 <              FormatIdentifier(Database.SQLDialect, Name) +
756 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
757                ' TIME'; {do not localize}
758            ftDateTime:
759              if (Database.SQLDialect = 1) then
760                FieldList := FieldList +
761 <              FormatIdentifier(Database.SQLDialect, Name) +
761 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
762                ' DATE' {do not localize}
763              else
764                FieldList := FieldList +
765 <              FormatIdentifier(Database.SQLDialect, Name) +
765 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
766                ' TIMESTAMP'; {do not localize}
767            ftLargeInt:
768              if (Database.SQLDialect = 1) then
769                IBError(ibxeFieldUnsupportedType,[nil])
770              else
771                FieldList := FieldList +
772 <                FormatIdentifier(Database.SQLDialect, Name) +
772 >                QuoteIdentifier(DataBase.SQLDialect, Name) +
773                  ' Numeric(18, 0)'; {do not localize}
774            ftBlob, ftMemo:
775              FieldList := FieldList +
776 <              FormatIdentifier(Database.SQLDialect, Name) +
776 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
777                ' BLOB SUB_TYPE 1'; {do not localize}
778            ftBytes, ftVarBytes, ftGraphic..ftTypedBinary:
779              FieldList := FieldList +
780 <              FormatIdentifier(Database.SQLDialect, Name) +
780 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
781                ' BLOB SUB_TYPE 0'; {do not localize}
782            ftUnknown, ftADT, ftArray, ftReference, ftDataSet,
783            ftCursor, ftWideString, ftAutoInc:
# Line 848 | Line 803 | var
803        Query.Database := Database;
804        Query.transaction := Transaction;
805        Query.SQL.Text := 'Create Table ' +
806 <        FormatIdentifier(Database.SQLDialect, FTableName) +
806 >        QuoteIdentifier(DataBase.SQLDialect, FTableName) +
807          ' (' + FieldList; {do not localize}
808        for I := 0 to IndexDefs.Count - 1 do
809        with IndexDefs[I] do
810          if ixPrimary in Options then
811          begin
812            Query.SQL.Text := Query.SQL.Text + ', CONSTRAINT ' +
813 <            FormatIdentifier(Database.SQLDialect, Name) +
813 >            QuoteIdentifier(DataBase.SQLDialect, Name) +
814              ' Primary Key (' +
815              FormatFieldsList(Fields) +
816              ')';
# Line 895 | Line 850 | begin
850      Query.Database := DataBase;
851      Query.Transaction := Transaction;
852      Query.SQL.Text := 'drop table ' +  {do not localize}
853 <      FormatIdentifier(Database.SQLDialect, FTableName);
853 >      QuoteIdentifier(DataBase.SQLDialect, FTableName);
854      Query.Prepare;
855      Query.ExecQuery;
856    finally
# Line 914 | Line 869 | begin
869      Query.Database := DataBase;
870      Query.Transaction := Transaction;
871      Query.SQL.Text := 'delete from ' + {do not localize}
872 <      FormatIdentifier(Database.SQLDialect, FTableName);
872 >      QuoteIdentifier(DataBase.SQLDialect, FTableName);
873      Query.Prepare;
874      Query.ExecQuery;
875      if Active then
# Line 927 | Line 882 | begin
882    end;
883   end;
884  
885 < procedure TIBTable.DataEvent(Event: TDataEvent; Info: Longint);
885 > procedure TIBTable.DataEvent(Event: TDataEvent; Info: Ptrint);
886   begin
887    if Event = dePropertyChange then begin
888      IndexDefs.Updated := False;
# Line 958 | Line 913 | begin
913      Query.SQL.Text := 'Select RDB$SYSTEM_FLAG, RDB$DBKEY_LENGTH ' + {do not localize}
914                      'from RDB$RELATIONS where RDB$RELATION_NAME = ' + {do not localize}
915                      '''' +
916 <                    FormatIdentifierValue(Database.SQLDialect, FTableName) + '''';
916 >                    FormatIdentifierValue(Database.SQLDialect,
917 >                      QuoteIdentifier(DataBase.SQLDialect, FTableName)) + '''';
918      Query.Prepare;
919      Query.ExecQuery;
920      if (Query.Current[0].AsInteger <> 0) or
# Line 1048 | Line 1004 | var
1004    i: Integer;
1005   begin
1006    if Database.SQLDialect = 1 then begin
1007 <    Value := FormatIdentifier(Database.SQLDialect, Value);
1007 >    Value := QuoteIdentifier(Database.SQLDialect, Value);
1008      Result := StringReplace (Value, ';', ', ', [rfReplaceAll]);
1009    end
1010    else begin
# Line 1058 | Line 1014 | begin
1014      begin
1015        FieldName := ExtractFieldName(Value, i);
1016        if Result = '' then
1017 <        Result := FormatIdentifier(Database.SQLDialect, FieldName)
1017 >        Result := QuoteIdentifier(Database.SQLDialect, FieldName)
1018        else
1019 <        Result := Result + ', ' + FormatIdentifier(Database.SQLDialect, FieldName);
1019 >        Result := Result + ', ' + QuoteIdentifier(Database.SQLDialect, FieldName);
1020      end;
1021    end;
1022   end;
# Line 1279 | Line 1235 | var
1235    SQL: TStrings;
1236    OrderByStr: string;
1237    bWhereClausePresent: Boolean;
1238 +  fn: string;
1239 +  aField: TFieldDef;
1240   begin
1241    bWhereClausePresent := False;
1242    Database.CheckActive;
# Line 1293 | Line 1251 | begin
1251      OrderByStr := FormatFieldsList(FPrimaryIndexFields);
1252    SQL := TStringList.Create;
1253    SQL.Text := 'select ' + {do not localize}
1254 <    FormatIdentifier(Database.SQLDialect, FTableName) + '.*, '
1254 >    QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' {do not localize}
1255      + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1256 <    + FormatIdentifier(Database.SQLDialect, FTableName);
1256 >    + QuoteIdentifier(DataBase.SQLDialect, FTableName);
1257    if Filtered and (Filter <> '') then
1258    begin
1259      SQL.Text := SQL.Text + ' where ' + Filter; {do not localize}
# Line 1314 | Line 1272 | begin
1272      begin
1273        if i > 0 then
1274          SQL.Text := SQL.Text + 'AND ';
1275 +      aField := FieldDefs.Find(FDetailFieldsList.Strings[i]);
1276 +      if assigned(aField) then
1277 +         fn := GetDBAliasName(aField.FieldNo)
1278 +      else
1279 +          fn := FDetailFieldsList.Strings[i]; {something wrong if we get here - but should word}
1280        SQL.Text := SQL.Text +
1281 <        FormatIdentifier(Database.SQLDialect, FDetailFieldsList.Strings[i]) +
1319 <        ' = :' +
1320 <        FormatIdentifier(Database.SQLDialect, FMasterFieldsList.Strings[i]);
1281 >        QuoteIdentifier(DataBase.SQLDialect, fn) + ' = :' + FMasterFieldsList.Strings[i];
1282      end;
1283    end;
1284    if OrderByStr <> '' then
1285      SQL.Text := SQL.Text + ' order by ' + OrderByStr; {do not localize}
1286    SelectSQL.Assign(SQL);
1287    RefreshSQL.Text := 'select ' + {do not localize}
1288 <    FormatIdentifier(Database.SQLDialect, FTableName) + '.*, '
1288 >    QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' {do not localize}
1289      + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1290 <    + FormatIdentifier(Database.SQLDialect, FTableName) +
1290 >    + QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1291      ' where RDB$DB_KEY = :IBX_INTERNAL_DBKEY'; {do not localize}
1292    WhereDBKeyRefreshSQL.Assign(RefreshSQL);
1293    InternalPrepare;
# Line 1355 | Line 1316 | var
1316                WhereAllFieldList := WhereAllFieldList + ' AND ';
1317            end;
1318            InsertFieldList := InsertFieldList +
1319 <            FormatIdentifier(Database.SQLDialect, Name);
1320 <          InsertParamList := InsertParamList + ':' +
1360 <            FormatIdentifier(Database.SQLDialect, Name);
1319 >            QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo));
1320 >          InsertParamList := InsertParamList + ':' +  Name;
1321            UpdateFieldList := UpdateFieldList +
1322 <            FormatIdentifier(Database.SQLDialect, Name) +
1323 <            ' = :' +
1364 <            FormatIdentifier(Database.SQLDialect, Name);
1322 >            QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) +
1323 >            ' = :' + Name;
1324            if (DataType <> ftBlob) and (DataType <>ftMemo) then
1325              WhereAllFieldList := WhereAllFieldList +
1326 <              FormatIdentifier(Database.SQLDialect, Name) + ' = :' +
1368 <              FormatIdentifier(Database.SQLDialect, Name);{do not localize}
1326 >              QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) + ' = :' +  Name;
1327          end;
1328        end;
1329      end;
# Line 1374 | Line 1332 | var
1332    procedure GenerateWherePrimaryFieldList;
1333    var
1334      i: Integer;
1335 <    tmp: String;
1335 >    tmp, fn: String;
1336 >    aField: TFieldDef;
1337    begin
1338      i := 1;
1339      while i <= Length(FPrimaryIndexFields) do
1340      begin
1341        tmp := ExtractFieldName(FPrimaryIndexFields, i);
1342 +      aField := FieldDefs.Find(tmp);
1343 +      if assigned(aField) then
1344 +         fn := GetDBAliasName(aField.FieldNo)
1345 +      else
1346 +         fn := tmp; {something wrong if we get here - but will work in most cases}
1347        tmp :=
1348 <        FormatIdentifier(Database.SQLDialect, tmp) +  ' = :' +
1349 <        FormatIdentifier(Database.SQLDialect, tmp);{do not localize}
1348 >        QuoteIdentifier(DataBase.SQLDialect, fn) +  ' = :' +
1349 >        QuoteIdentifier(DataBase.SQLDialect, tmp);{do not localize}
1350        if WherePrimaryFieldList <> '' then
1351          WherePrimaryFieldList :=
1352            WherePrimaryFieldList + ' AND ' + tmp
# Line 1397 | Line 1361 | begin
1361    else
1362    begin
1363      DeleteSQL.Text := 'delete from ' + {do not localize}
1364 <      FormatIdentifier(Database.SQLDialect, FTableName) +
1364 >      QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1365        ' where RDB$DB_KEY = ' + ':IBX_INTERNAL_DBKEY'; {do not localize}
1366      GenerateFieldLists;
1367      InsertSQL.Text := 'insert into ' + {do not localize}
1368 <      FormatIdentifier(Database.SQLDialect, FTableName) +
1368 >      QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1369      ' (' + InsertFieldList + {do not localize}
1370        ') values (' + InsertParamList + ')'; {do not localize}
1371      ModifySQL.Text := 'update ' +
1372 <      FormatIdentifier(Database.SQLDialect, FTableName) +
1372 >      QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1373        ' set ' + UpdateFieldList + {do not localize}
1374        ' where RDB$DB_KEY = :IBX_INTERNAL_DBKEY'; {do not localize}
1375      WhereAllRefreshSQL.Text := 'select ' +  {do not localize}
1376 <      FormatIdentifier(Database.SQLDialect, FTableName) + '.*, '
1376 >      QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, '
1377        + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1378 <      + FormatIdentifier(Database.SQLDialect, FTableName) +
1378 >      + QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1379        ' where ' + WhereAllFieldList; {do not localize}
1380      if FPrimaryIndexFields <> '' then
1381      begin
1382        GenerateWherePrimaryFieldList;
1383        WherePrimaryRefreshSQL.Text := 'select ' + {do not localize}
1384 <        FormatIdentifier(Database.SQLDialect, FTableName) + '.*, ' {do not localize}
1384 >        QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' {do not localize}
1385          + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1386 <        + FormatIdentifier(Database.SQLDialect, FTableName) +
1386 >        + QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1387          ' where ' + WherePrimaryFieldList; {do not localize}
1388      end;
1389      try
# Line 1499 | Line 1463 | begin
1463      begin
1464        SetState(dsInactive);
1465        CloseCursor;
1466 <      OpenCursor;
1466 >      OpenCursor(false);
1467        SetState(dsBrowse);
1468      end;
1469    finally
# Line 1507 | Line 1471 | begin
1471    end;
1472   end;
1473  
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
1474   end.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines