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 5 by tony, Fri Feb 18 16:26:16 2011 UTC vs.
Revision 107 by tony, Thu Jan 18 14:37:40 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   {$Mode Delphi}
37  
38 + {$codepage UTF8}
39 +
40   interface
41  
42   uses SysUtils, Classes, DB, IB,  IBCustomDataSet,
43 <     IBHeader, IBSQL, IBUtils;
43 >     IBSQL, IBUtils;
44      
45   type
46  
# Line 142 | 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 173 | Line 183 | type
183  
184   implementation
185  
186 + uses FBMessages;
187 +
188   { TIBTable }
189  
190   constructor TIBTable.Create(AOwner: TComponent);
# Line 229 | 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 290 | 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}
309 <                        'F.RDB$FIELD_TYPE, F.RDB$FIELD_SUB_TYPE, ' + {do not localize}
310 <                        'F.RDB$EXTERNAL_LENGTH, F.RDB$EXTERNAL_SCALE, F.RDB$EXTERNAL_TYPE ' + {do not localize}
311 <                        'from RDB$RELATION_FIELDS R, RDB$FIELDS F ' + {do not localize}
312 <                        'where R.RDB$RELATION_NAME = ' + {do not localize}
313 <                        '''' +
314 <                        FormatIdentifierValue(Database.SQLDialect,
315 <                          QuoteIdentifier(DataBase.SQLDialect, FTableName)) +
316 <                        ''' ' +
317 <                        'and R.RDB$FIELD_SOURCE = F.RDB$FIELD_NAME '+ {do not localize}
318 <                        'order by R.RDB$FIELD_POSITION'; {do not localize}
319 <
320 <      Query.Prepare;
321 <      Query.ExecQuery;
322 <      FieldDefs.BeginUpdate;
323 <      FieldDefs.Clear;
324 <      while (not Query.EOF) and (Query.Next <> nil) do
325 <      begin
326 <          with FieldDefs.AddFieldDef do
327 <          begin
328 < (*           FieldNo := Query.Current.ByName('RDB$FIELD_POSITION').AsInteger; {do not localize}*)
329 <            Name := TrimRight(Query.Current.ByName('RDB$FIELD_NAME').AsString); {do not localize}
330 <            case Query.Current.ByName('RDB$FIELD_TYPE').AsInteger of {do not localize}
331 <              blr_varying, blr_text:
332 <              begin
333 <                DataType := ftString;
334 <                Size := Query.Current.ByName('RDB$FIELD_LENGTH').AsInteger; {do not localize}
335 <              end;
336 <              blr_float, blr_double, blr_d_float: DataType := ftFloat;
337 <              blr_short:
338 <              begin
339 <                sqlscale := Query.Current.ByName('RDB$FIELD_SCALE').AsInteger; {do not localize}
340 <                if (sqlscale = 0) then
341 <                  DataType := ftSmallInt
342 <                else
343 <                begin
344 <                  DataType := ftBCD;
345 <                  Precision := 4;
346 <                end;
347 <              end;
348 <              blr_long:
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 <                sqlscale := Query.Current.ByName('RDB$FIELD_SCALE').AsInteger; {do not localize}
330 <                if (sqlscale = 0) then
352 <                  DataType := ftInteger
353 <                else if (sqlscale >= (-4)) then
354 <                begin
355 <                  DataType := ftBCD;
356 <                  Precision := 9;
357 <                end
358 <                else
359 <                  DataType := ftFloat;
329 >                StartTransaction;
330 >                DidActivate := true
331                end;
332 <              blr_int64:
333 <              begin
334 <                sqlscale := Query.Current.ByName('RDB$FIELD_SCALE').AsInteger; {do not localize}
335 <                if (sqlscale = 0) then
336 <                  DataType := ftLargeInt
366 <                else if (sqlscale >= (-4)) then
367 <                begin
368 <                  DataType := ftBCD;
369 <                  Precision := 18;
370 <                end
371 <                else
372 <                  DataType := ftFloat;
373 <              end;
374 <              blr_timestamp: DataType := ftDateTime;
375 <              blr_sql_time: DataType := ftTime;
376 <              blr_sql_date: DataType := ftDate;
377 <              blr_blob:
378 <                if (Query.Current.ByName('RDB$FIELD_SUB_TYPE').AsInteger = 1) then {do not localize}
379 <                  DataType := ftMemo
380 <                else
381 <                  DataType := ftBlob;
382 <              blr_quad:
383 <              begin
384 <                DataType := ftUnknown;
385 <                Size := sizeof (TISC_QUAD);
386 <              end;
387 <              else
388 <                DataType := ftUnknown;
389 <            end;
390 <            if not (Query.Current.ByName('RDB$COMPUTED_BLR').IsNull) then {do not localize}
391 <            begin
392 <              Attributes := [faReadOnly];
393 <              InternalCalcField := True
394 <            end
395 <            else
396 <              InternalCalcField := False;
397 <            if ((not InternalCalcField) and
398 <                 Query.Current.ByName('RDB$DEFAULT_VALUE').IsNull and {do not localize}
399 <                 (Query.Current.ByName('RDB$NULL_FLAG').AsInteger = 1) )then {do not localize}
400 <            begin
401 <              Attributes := [faRequired];
402 <              Required := True;
403 <            end;
404 <          end;
405 <      end;
406 <      FieldDefs.EndUpdate;
332 >         end;
333 >         Query.Prepare;
334 >         FieldDefsFromQuery(Query);
335 >         if DidActivate then
336 >            Query.Transaction.Rollback;
337      finally
338 <      Query.free;
409 <      Database.InternalTransaction.Commit;
338 >         Query.Free;
339      end;
340    end;
341   end;
# Line 555 | Line 484 | var
484          Query.SQL.Text := 'Alter Table ' +  {do not localize}
485            QuoteIdentifier(DataBase.SQLDialect, FTableName) +
486            ' Drop Constraint ' +
487 <          QuoteIdentifier(DataBase.SQLDialect, Query.Current.ByName('RDB$CONSTRAINT_NAME').AsString);
487 >          QuoteIdentifier(DataBase.SQLDialect, Query.FieldByName('RDB$CONSTRAINT_NAME').AsString);
488          Query.Prepare;
489          Query.ExecQuery;
490          IndexDefs.Updated := False;
# Line 640 | 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 661 | Line 592 | begin
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 688 | Line 626 | begin
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 778 | Line 720 | var
720              FieldList := FieldList +
721                QuoteIdentifier(DataBase.SQLDialect, Name) +
722                ' CHAR(' + IntToStr(Size) + ')'; {do not localize}
723 <          ftBoolean, ftSmallint, ftWord:
723 >          ftBoolean:
724 >            FieldList := FieldList +
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}
# Line 976 | Line 922 | begin
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 1260 | 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 1294 | 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 1329 | 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 <        QuoteIdentifier(DataBase.SQLDialect, FDetailFieldsList.Strings[i]) +
1334 <        ' = :' +
1335 <        QuoteIdentifier(DataBase.SQLDialect, FMasterFieldsList.Strings[i]);
1286 >        QuoteIdentifier(DataBase.SQLDialect, fn) + ' = :' + FMasterFieldsList.Strings[i];
1287      end;
1288    end;
1289    if OrderByStr <> '' then
# Line 1351 | 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;
1372          InsertFieldList := InsertFieldList +
1373            QuoteIdentifier(DataBase.SQLDialect, Name);
1374          InsertParamList := InsertParamList + ':' +
1375            QuoteIdentifier(DataBase.SQLDialect, Name);
1357            UpdateFieldList := UpdateFieldList +
1358 <            QuoteIdentifier(DataBase.SQLDialect, Name) +
1359 <            ' = :' +
1379 <            QuoteIdentifier(DataBase.SQLDialect, Name);
1358 >            QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) +
1359 >            ' = :' + Name;
1360            if (DataType <> ftBlob) and (DataType <>ftMemo) then
1361              WhereAllFieldList := WhereAllFieldList +
1362 <              QuoteIdentifier(DataBase.SQLDialect, Name) + ' = :' +
1383 <              QuoteIdentifier(DataBase.SQLDialect, Name);{do not localize}
1362 >              QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) + ' = :' +  Name;
1363          end;
1364        end;
1365      end;
# Line 1389 | 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 <        QuoteIdentifier(DataBase.SQLDialect, tmp) +  ' = :' +
1384 >        QuoteIdentifier(DataBase.SQLDialect, fn) +  ' = :' +
1385          QuoteIdentifier(DataBase.SQLDialect, tmp);{do not localize}
1386        if WherePrimaryFieldList <> '' then
1387          WherePrimaryFieldList :=
# Line 1418 | Line 1403 | begin
1403      InsertSQL.Text := 'insert into ' + {do not localize}
1404        QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1405      ' (' + InsertFieldList + {do not localize}
1406 <      ') values (' + InsertParamList + ')'; {do not localize}
1406 >      ') values (' + InsertParamList + ')' + InsertReturningFieldList; {do not localize}
1407      ModifySQL.Text := 'update ' +
1408        QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1409        ' set ' + UpdateFieldList + {do not localize}
1410 <      ' where RDB$DB_KEY = :IBX_INTERNAL_DBKEY'; {do not localize}
1410 >      ' where RDB$DB_KEY = :IBX_INTERNAL_DBKEY' + UpdateReturningFieldList; {do not localize}
1411      WhereAllRefreshSQL.Text := 'select ' +  {do not localize}
1412        QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, '
1413        + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1414        + QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1415 <      ' where ' + WhereAllFieldList; {do not localize}
1415 >      ' where ' + WhereAllFieldList ; {do not localize}
1416      if FPrimaryIndexFields <> '' then
1417      begin
1418        GenerateWherePrimaryFieldList;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines