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

Comparing ibx/trunk/runtime/IBCustomDataSet.pas (file contents):
Revision 50 by tony, Thu Feb 23 15:22:18 2017 UTC vs.
Revision 108 by tony, Thu Jan 18 14:37:46 2018 UTC

# Line 27 | Line 27
27   {    IBX For Lazarus (Firebird Express)                                  }
28   {    Contributor: Tony Whyman, MWA Software http://www.mwasoftware.co.uk }
29   {    Portions created by MWA Software are copyright McCallum Whyman      }
30 < {    Associates Ltd 2011 - 2015                                                }
30 > {    Associates Ltd 2011 - 2015                                          }
31   {                                                                        }
32   {************************************************************************}
33  
# Line 53 | Line 53 | uses
53    unix,
54   {$ENDIF}
55    SysUtils, Classes, IBDatabase, IBExternals, IB,  IBSQL, Db,
56 <  IBUtils, IBBlob, IBSQLParser;
56 >  IBUtils, IBBlob, IBSQLParser, IBDatabaseInfo;
57  
58   const
59    BufferCacheSize    =  1000;  { Allocate cache in this many record chunks}
# Line 74 | Line 74 | type
74      procedure SetDataSet(ADataSet: TIBCustomDataSet); virtual; abstract;
75      procedure Apply(UpdateKind: TUpdateKind; buff: PChar); virtual; abstract;
76      function GetSQL(UpdateKind: TUpdateKind): TStrings; virtual; abstract;
77 <    procedure InternalSetParams(Query: TIBSQL; buff: PChar);
77 >    procedure InternalSetParams(Params: ISQLParams; buff: PChar); overload;
78 >    procedure InternalSetParams(Query: TIBSQL; buff: PChar); overload;
79 >    procedure UpdateRecordFromQuery(QryResults: IResults; Buffer: PChar);
80      property DataSet: TIBCustomDataSet read GetDataSet write SetDataSet;
81    public
82      constructor Create(AOwner: TComponent); override;
# Line 181 | Line 183 | type
183      FCharacterSetSize: integer;
184      FAutoFieldSize: boolean;
185      FCodePage: TSystemCodePage;
186 +    FDataSize: integer;
187    protected
188      procedure Bind(Binding: Boolean); override;
189      function GetDataSize: Integer; override;
# Line 206 | Line 209 | type
209       Note: y > 4 will default to Floats
210    }
211    TIBBCDField = class(TBCDField)
212 +  private
213 +    FIdentityColumn: boolean;
214    protected
215 +    procedure Bind(Binding: Boolean); override;
216      class procedure CheckTypeSize(Value: Integer); override;
217      function GetAsCurrency: Currency; override;
218      function GetAsString: string; override;
# Line 214 | Line 220 | type
220      function GetDataSize: Integer; override;
221    public
222      constructor Create(AOwner: TComponent); override;
223 +    property IdentityColumn: boolean read FIdentityColumn;
224    published
225      property Size default 8;
226    end;
227  
228 +  {The following integer field types extend the built in versions to enable IBX appplications
229 +   to check for an Identity column}
230 +
231 +  { TIBSmallintField }
232 +
233 +  TIBSmallintField = class(TSmallintField)
234 +  private
235 +    FIdentityColumn: boolean;
236 +  protected
237 +    procedure Bind(Binding: Boolean); override;
238 +  public
239 +    property IdentityColumn: boolean read FIdentityColumn;
240 +  end;
241 +
242 +  { TIBIntegerField }
243 +
244 +  TIBIntegerField = class(TIntegerField)
245 +  private
246 +    FIdentityColumn: boolean;
247 +  protected
248 +    procedure Bind(Binding: Boolean); override;
249 +  public
250 +    property IdentityColumn: boolean read FIdentityColumn;
251 +  end;
252 +
253 +  { TIBLargeIntField }
254 +
255 +  TIBLargeIntField = class(TLargeIntField)
256 +  private
257 +    FIdentityColumn: boolean;
258 +  protected
259 +    procedure Bind(Binding: Boolean); override;
260 +  public
261 +    property IdentityColumn: boolean read FIdentityColumn;
262 +  end;
263 +
264    {TIBMemoField}
265    {Allows us to show truncated text in DBGrids and anything else that uses
266     DisplayText}
# Line 272 | Line 315 | type
315      FFieldName: string;
316      FGeneratorName: string;
317      FIncrement: integer;
318 +    FQuery: TIBSQL;
319 +    function GetDatabase: TIBDatabase;
320 +    function GetTransaction: TIBTransaction;
321 +    procedure SetDatabase(AValue: TIBDatabase);
322 +    procedure SetGeneratorName(AValue: string);
323      procedure SetIncrement(const AValue: integer);
324 +    procedure SetTransaction(AValue: TIBTransaction);
325 +    procedure SetQuerySQL;
326    protected
327 <    function GetNextValue(ADatabase: TIBDatabase; ATransaction: TIBTransaction): integer;
327 >    function GetNextValue: integer;
328    public
329      constructor Create(Owner: TIBCustomDataSet);
330 +    destructor Destroy; override;
331      procedure Apply;
332      property Owner: TIBCustomDataSet read FOwner;
333 +    property Database: TIBDatabase read GetDatabase write SetDatabase;
334 +    property Transaction: TIBTransaction read GetTransaction write SetTransaction;
335    published
336 <    property Generator: string read FGeneratorName write FGeneratorName;
336 >    property Generator: string read FGeneratorName write SetGeneratorName;
337      property Field: string read FFieldName write FFieldName;
338      property Increment: integer read FIncrement write SetIncrement default 1;
339      property ApplyOnEvent: TIBGeneratorApplyOnEvent read FApplyOnEvent write FApplyOnEvent;
# Line 358 | Line 411 | type
411      FQRefresh,
412      FQSelect,
413      FQModify: TIBSQL;
414 +    FDatabaseInfo: TIBDatabaseInfo;
415      FRecordBufferSize: Integer;
416      FRecordCount: Integer;
417      FRecordSize: Integer;
# Line 387 | Line 441 | type
441      FInTransactionEnd: boolean;
442      FIBLinks: TList;
443      FFieldColumns: PFieldColumns;
444 +    FBufferUpdatedOnQryReturn: boolean;
445 +    procedure ColumnDataToBuffer(QryResults: IResults; ColumnIndex,
446 +      FieldIndex: integer; Buffer: PChar);
447      procedure InitModelBuffer(Qry: TIBSQL; Buffer: PChar);
448      function GetSelectStmtIntf: IStatement;
449      procedure SetUpdateMode(const Value: TUpdateMode);
# Line 434 | Line 491 | type
491      procedure SetDatabase(Value: TIBDatabase);
492      procedure SetDeleteSQL(Value: TStrings);
493      procedure SetInsertSQL(Value: TStrings);
494 <    procedure SetInternalSQLParams(Qry: TIBSQL; Buffer: Pointer);
494 >    procedure SetInternalSQLParams(Params: ISQLParams; Buffer: Pointer);
495      procedure SetRefreshSQL(Value: TStrings);
496      procedure SetSelectSQL(Value: TStrings);
497      procedure SetModifySQL(Value: TStrings);
498      procedure SetTransaction(Value: TIBTransaction);
499      procedure SetUpdateRecordTypes(Value: TIBUpdateRecordTypes);
500      procedure SetUniDirectional(Value: Boolean);
501 +    procedure UpdateRecordFromQuery(QryResults: IResults; Buffer: PChar);
502      procedure RefreshParams;
503      function AdjustPosition(FCache: PChar; Offset: DWORD;
504                              Origin: Integer): DWORD;
# Line 628 | Line 686 | type
686      procedure Post; override;
687      function ParamByName(ParamName: String): ISQLParam;
688      property ArrayFieldCount: integer read FArrayFieldCount;
689 +    property DatabaseInfo: TIBDatabaseInfo read FDatabaseInfo;
690      property UpdateObject: TIBDataSetUpdateObject read FUpdateObject write SetUpdateObject;
691      property UpdatesPending: Boolean read FUpdatesPending;
692      property UpdateRecordTypes: TIBUpdateRecordTypes read FUpdateRecordTypes
# Line 789 | Line 848 | type
848      FCharacterSetName: RawByteString;
849      FCharacterSetSize: integer;
850      FCodePage: TSystemCodePage;
851 +    FIdentityColumn: boolean;
852      FRelationName: string;
853 +    FDataSize: integer;
854    published
855      property CharacterSetName: RawByteString read FCharacterSetName write FCharacterSetName;
856      property CharacterSetSize: integer read FCharacterSetSize write FCharacterSetSize;
857      property CodePage: TSystemCodePage read FCodePage write FCodePage;
858 +    property DataSize: integer read FDataSize write FDataSize;
859      property RelationName: string read FRelationName write FRelationName;
860      property ArrayDimensions: integer read FArrayDimensions write FArrayDimensions;
861      property ArrayBounds: TArrayBounds read FArrayBounds write FArrayBounds;
862 +    property IdentityColumn: boolean read FIdentityColumn write FIdentityColumn default false;
863    end;
864  
865   const
866   DefaultFieldClasses: array[TFieldType] of TFieldClass = (
867      nil,                { ftUnknown }
868      TIBStringField,     { ftString }
869 <    TSmallintField,     { ftSmallint }
870 <    TIntegerField,      { ftInteger }
869 >    TIBSmallintField,   { ftSmallint }
870 >    TIBIntegerField,      { ftInteger }
871      TWordField,         { ftWord }
872      TBooleanField,      { ftBoolean }
873      TFloatField,        { ftFloat }
# Line 826 | Line 889 | DefaultFieldClasses: array[TFieldType] o
889      nil,                { ftCursor }
890      TStringField,       { ftFixedChar }
891      nil,    { ftWideString }
892 <    TLargeIntField,     { ftLargeInt }
892 >    TIBLargeIntField,     { ftLargeInt }
893      nil,          { ftADT }
894      TIBArrayField,        { ftArray }
895      nil,    { ftReference }
# Line 870 | Line 933 | type
933      FieldName : String;
934      COMPUTED_BLR : Boolean;
935      DEFAULT_VALUE : boolean;
936 +    IDENTITY_COLUMN : boolean;
937      NextField : TFieldNode;
938    end;
939  
# Line 922 | Line 986 | type
986      Result := str;
987    end;
988  
989 + { TIBLargeIntField }
990 +
991 + procedure TIBLargeIntField.Bind(Binding: Boolean);
992 + begin
993 +  inherited Bind(Binding);
994 +  if Binding and (FieldDef <> nil) then
995 +     FIdentityColumn := (FieldDef as TIBFieldDef).IdentityColumn;
996 + end;
997 +
998 + { TIBIntegerField }
999 +
1000 + procedure TIBIntegerField.Bind(Binding: Boolean);
1001 + begin
1002 +  inherited Bind(Binding);
1003 +  if Binding and (FieldDef <> nil) then
1004 +     FIdentityColumn := (FieldDef as TIBFieldDef).IdentityColumn;
1005 + end;
1006 +
1007 + { TIBSmallintField }
1008 +
1009 + procedure TIBSmallintField.Bind(Binding: Boolean);
1010 + begin
1011 +  inherited Bind(Binding);
1012 +  if Binding and (FieldDef <> nil) then
1013 +     FIdentityColumn := (FieldDef as TIBFieldDef).IdentityColumn;
1014 + end;
1015 +
1016   { TIBArray }
1017  
1018   procedure TIBArray.EventHandler(Sender: IArray; Reason: TArrayEventReason);
# Line 1031 | Line 1122 | begin
1122         {2: case 2 ignored. This should be handled by TIBWideMemo}
1123  
1124         3, {Assume UNICODE_FSS is really UTF8}
1125 <       4: {Include GB18030 - assuming UTF8 routine work for this codeset}
1125 >       4: {Include GB18030 - assuming UTF8 routines work for this codeset}
1126           if DisplayWidth = 0 then
1127             Result := ValidUTF8String(TextToSingleLine(Result))
1128           else
# Line 1089 | Line 1180 | procedure TIBMemoField.SetAsString(const
1180   var s: RawByteString;
1181   begin
1182    s := AValue;
1183 <  if StringCodePage(Value) <> CodePage then
1183 >  if StringCodePage(s) <> CodePage then
1184      SetCodePage(s,CodePage,CodePage<>CP_NONE);
1185    inherited SetAsString(s);
1186   end;
# Line 1141 | Line 1232 | begin
1232      IBFieldDef := FieldDef as TIBFieldDef;
1233      CharacterSetSize := IBFieldDef.CharacterSetSize;
1234      CharacterSetName := IBFieldDef.CharacterSetName;
1235 +    FDataSize := IBFieldDef.DataSize + 1;
1236      if AutoFieldSize then
1237        Size := IBFieldDef.Size;
1238      CodePage := IBFieldDef.CodePage;
# Line 1149 | Line 1241 | end;
1241  
1242   function TIBStringField.GetDataSize: Integer;
1243   begin
1244 <  Result := Size * CharacterSetSize + 1;
1244 >  Result := FDataSize;
1245   end;
1246  
1247   constructor TIBStringField.Create(aOwner: TComponent);
# Line 1208 | Line 1300 | var
1300    s: RawByteString;
1301   begin
1302    Buffer := nil;
1303 <  IBAlloc(Buffer, 0, Size + 1);
1303 >  IBAlloc(Buffer, 0, DataSize);
1304    try
1305      s := Value;
1306      if StringCodePage(s) <> CodePage then
1307        SetCodePage(s,CodePage,CodePage<>CP_NONE);
1308 <    StrLCopy(Buffer, PChar(s), Size);
1308 >    StrLCopy(Buffer, PChar(s), DataSize-1);
1309      if Transliterate then
1310        DataSet.Translate(Buffer, Buffer, True);
1311      SetData(Buffer);
# Line 1232 | Line 1324 | begin
1324    Size := 8;
1325   end;
1326  
1327 + procedure TIBBCDField.Bind(Binding: Boolean);
1328 + begin
1329 +  inherited Bind(Binding);
1330 +  if Binding and (FieldDef <> nil) then
1331 +     FIdentityColumn := (FieldDef as TIBFieldDef).IdentityColumn;
1332 + end;
1333 +
1334   class procedure TIBBCDField.CheckTypeSize(Value: Integer);
1335   begin
1336   { No need to check as the base type is currency, not BCD }
# Line 1317 | Line 1416 | constructor TIBCustomDataSet.Create(AOwn
1416   begin
1417    inherited Create(AOwner);
1418    FBase := TIBBase.Create(Self);
1419 +  FDatabaseInfo := TIBDatabaseInfo.Create(self);
1420    FIBLinks := TList.Create;
1421    FCurrentRecord := -1;
1422    FDeletedRecords := 0;
# Line 1955 | Line 2055 | begin
2055    end;
2056   end;
2057  
2058 + {Update Buffer Fields from Query Results}
2059 +
2060 + procedure TIBCustomDataSet.UpdateRecordFromQuery(QryResults: IResults;
2061 +  Buffer: PChar);
2062 + var i, j: integer;
2063 + begin
2064 +  for i := 0 to QryResults.Count - 1 do
2065 +  begin
2066 +    j := GetFieldPosition(QryResults[i].GetAliasName);
2067 +    if j > 0 then
2068 +    begin
2069 +      ColumnDataToBuffer(QryResults,i,j,Buffer);
2070 +      FBufferUpdatedOnQryReturn := true;
2071 +    end;
2072 +  end;
2073 + end;
2074 +
2075 +
2076 + {Move column data returned from query to row buffer}
2077 +
2078 + procedure TIBCustomDataSet.ColumnDataToBuffer(QryResults: IResults;
2079 +               ColumnIndex, FieldIndex: integer; Buffer: PChar);
2080 + var
2081 +  LocalData: PByte;
2082 +  LocalDate: TDateTime;
2083 +  LocalDouble: Double;
2084 +  LocalInt: Integer;
2085 +  LocalBool: wordBool;
2086 +  LocalInt64: Int64;
2087 +  LocalCurrency: Currency;
2088 +  ColData: ISQLData;
2089 + begin
2090 +  LocalData := nil;
2091 +  with PRecordData(Buffer)^.rdFields[FieldIndex], FFieldColumns^[FieldIndex] do
2092 +  begin
2093 +    QryResults.GetData(ColumnIndex,fdIsNull,fdDataLength,LocalData);
2094 +    if not fdIsNull then
2095 +    begin
2096 +      ColData := QryResults[ColumnIndex];
2097 +      case fdDataType of  {Get Formatted data for column types that need formatting}
2098 +        SQL_TYPE_DATE,
2099 +        SQL_TYPE_TIME,
2100 +        SQL_TIMESTAMP:
2101 +        begin
2102 +          {This is an IBX native format and not the TDataset approach. See also GetFieldData}
2103 +          LocalDate := ColData.AsDateTime;
2104 +          LocalData := PByte(@LocalDate);
2105 +        end;
2106 +        SQL_SHORT, SQL_LONG:
2107 +        begin
2108 +          if (fdDataScale = 0) then
2109 +          begin
2110 +            LocalInt := ColData.AsLong;
2111 +            LocalData := PByte(@LocalInt);
2112 +          end
2113 +          else
2114 +          if (fdDataScale >= (-4)) then
2115 +          begin
2116 +            LocalCurrency := ColData.AsCurrency;
2117 +            LocalData := PByte(@LocalCurrency);
2118 +          end
2119 +          else
2120 +          begin
2121 +           LocalDouble := ColData.AsDouble;
2122 +           LocalData := PByte(@LocalDouble);
2123 +          end;
2124 +        end;
2125 +        SQL_INT64:
2126 +        begin
2127 +          if (fdDataScale = 0) then
2128 +          begin
2129 +            LocalInt64 := ColData.AsInt64;
2130 +            LocalData := PByte(@LocalInt64);
2131 +          end
2132 +          else
2133 +          if (fdDataScale >= (-4)) then
2134 +          begin
2135 +            LocalCurrency := ColData.AsCurrency;
2136 +            LocalData := PByte(@LocalCurrency);
2137 +            end
2138 +            else
2139 +            begin
2140 +              LocalDouble := ColData.AsDouble;
2141 +              LocalData := PByte(@LocalDouble);
2142 +            end
2143 +        end;
2144 +        SQL_DOUBLE, SQL_FLOAT, SQL_D_FLOAT:
2145 +        begin
2146 +          LocalDouble := ColData.AsDouble;
2147 +          LocalData := PByte(@LocalDouble);
2148 +        end;
2149 +        SQL_BOOLEAN:
2150 +        begin
2151 +          LocalBool := ColData.AsBoolean;
2152 +          LocalData := PByte(@LocalBool);
2153 +        end;
2154 +      end;
2155 +
2156 +      if fdDataType = SQL_VARYING then
2157 +        Move(LocalData^, Buffer[fdDataOfs], fdDataLength)
2158 +      else
2159 +        Move(LocalData^, Buffer[fdDataOfs], fdDataSize)
2160 +    end
2161 +    else {Null column}
2162 +    if fdDataType = SQL_VARYING then
2163 +      FillChar(Buffer[fdDataOfs],fdDataLength,0)
2164 +    else
2165 +      FillChar(Buffer[fdDataOfs],fdDataSize,0);
2166 +  end;
2167 + end;
2168 +
2169   { Read the record from FQSelect.Current into the record buffer
2170    Then write the buffer to in memory cache }
2171   procedure TIBCustomDataSet.FetchCurrentRecordToBuffer(Qry: TIBSQL;
# Line 1963 | Line 2174 | var
2174    pbd: PBlobDataArray;
2175    pda: PArrayDataArray;
2176    i, j: Integer;
1966  LocalData: PChar;
1967  LocalDate, LocalDouble: Double;
1968  LocalInt: Integer;
1969  LocalBool: wordBool;
1970  LocalInt64: Int64;
1971  LocalCurrency: Currency;
2177    FieldsLoaded: Integer;
2178    p: PRecordData;
2179   begin
# Line 2019 | Line 2224 | begin
2224          continue;
2225        end;
2226      if j > 0 then
2227 <    begin
2023 <      LocalData := nil;
2024 <      with p^.rdFields[j], FFieldColumns^[j] do
2025 <      begin
2026 <        Qry.Current.GetData(i,fdIsNull,fdDataLength,LocalData);
2027 <        if not fdIsNull then
2028 <        begin
2029 <          case fdDataType of  {Get Formatted data for column types that need formatting}
2030 <            SQL_TIMESTAMP:
2031 <            begin
2032 <              LocalDate := TimeStampToMSecs(DateTimeToTimeStamp(Qry[i].AsDateTime));
2033 <              LocalData := PChar(@LocalDate);
2034 <            end;
2035 <            SQL_TYPE_DATE:
2036 <            begin
2037 <              LocalInt := DateTimeToTimeStamp(Qry[i].AsDateTime).Date;
2038 <              LocalData := PChar(@LocalInt);
2039 <            end;
2040 <            SQL_TYPE_TIME:
2041 <            begin
2042 <              LocalInt := DateTimeToTimeStamp(Qry[i].AsDateTime).Time;
2043 <              LocalData := PChar(@LocalInt);
2044 <            end;
2045 <            SQL_SHORT, SQL_LONG:
2046 <            begin
2047 <              if (fdDataScale = 0) then
2048 <              begin
2049 <                LocalInt := Qry[i].AsLong;
2050 <                LocalData := PChar(@LocalInt);
2051 <              end
2052 <              else
2053 <              if (fdDataScale >= (-4)) then
2054 <              begin
2055 <                LocalCurrency := Qry[i].AsCurrency;
2056 <                LocalData := PChar(@LocalCurrency);
2057 <              end
2058 <              else
2059 <              begin
2060 <               LocalDouble := Qry[i].AsDouble;
2061 <               LocalData := PChar(@LocalDouble);
2062 <              end;
2063 <            end;
2064 <            SQL_INT64:
2065 <            begin
2066 <              if (fdDataScale = 0) then
2067 <              begin
2068 <                LocalInt64 := Qry[i].AsInt64;
2069 <                LocalData := PChar(@LocalInt64);
2070 <              end
2071 <              else
2072 <              if (fdDataScale >= (-4)) then
2073 <              begin
2074 <                LocalCurrency := Qry[i].AsCurrency;
2075 <                LocalData := PChar(@LocalCurrency);
2076 <                end
2077 <                else
2078 <                begin
2079 <                  LocalDouble := Qry[i].AsDouble;
2080 <                  LocalData := PChar(@LocalDouble);
2081 <                end
2082 <            end;
2083 <            SQL_DOUBLE, SQL_FLOAT, SQL_D_FLOAT:
2084 <            begin
2085 <              LocalDouble := Qry[i].AsDouble;
2086 <              LocalData := PChar(@LocalDouble);
2087 <            end;
2088 <            SQL_BOOLEAN:
2089 <            begin
2090 <              LocalBool := Qry[i].AsBoolean;
2091 <              LocalData := PChar(@LocalBool);
2092 <            end;
2093 <          end;
2094 <
2095 <          if fdDataType = SQL_VARYING then
2096 <            Move(LocalData^, Buffer[fdDataOfs], fdDataLength)
2097 <          else
2098 <            Move(LocalData^, Buffer[fdDataOfs], fdDataSize)
2099 <        end
2100 <        else {Null column}
2101 <        if fdDataType = SQL_VARYING then
2102 <          FillChar(Buffer[fdDataOfs],fdDataLength,0)
2103 <        else
2104 <          FillChar(Buffer[fdDataOfs],fdDataSize,0);
2105 <      end;
2106 <    end;
2227 >      ColumnDataToBuffer(Qry.Current,i,j,Buffer);
2228    end;
2229    WriteRecordCache(RecordNumber, Buffer);
2230   end;
# Line 2198 | Line 2319 | begin
2319      FUpdateObject.Apply(ukDelete,Buff)
2320    else
2321    begin
2322 <    SetInternalSQLParams(FQDelete, Buff);
2322 >    SetInternalSQLParams(FQDelete.Params, Buff);
2323      FQDelete.ExecQuery;
2324    end;
2325    with PRecordData(Buff)^ do
# Line 2337 | Line 2458 | begin
2458        end;
2459        Inc(arr);
2460      end;
2461 +  FBufferUpdatedOnQryReturn := false;
2462    if Assigned(FUpdateObject) then
2463    begin
2464      if (Qry = FQDelete) then
# Line 2347 | Line 2469 | begin
2469        FUpdateObject.Apply(ukModify,Buff);
2470    end
2471    else begin
2472 <    SetInternalSQLParams(Qry, Buff);
2472 >    SetInternalSQLParams(Qry.Params, Buff);
2473      Qry.ExecQuery;
2474 +    if Qry.FieldCount > 0 then {Has RETURNING Clause}
2475 +      UpdateRecordFromQuery(Qry.Current,Buff);
2476    end;
2477    PRecordData(Buff)^.rdUpdateStatus := usUnmodified;
2478    PRecordData(Buff)^.rdCachedUpdateStatus := cusUnmodified;
2479    SetModified(False);
2480    WriteRecordCache(PRecordData(Buff)^.rdRecordNumber, Buff);
2481 <  if (FForcedRefresh or FNeedsRefresh) and CanRefresh then
2481 >  if (FForcedRefresh or (FNeedsRefresh and not FBufferUpdatedOnQryReturn)) and CanRefresh then
2482      InternalRefreshRow;
2483   end;
2484  
# Line 2381 | Line 2505 | begin
2505          end
2506          else
2507            Qry := FQRefresh;
2508 <        SetInternalSQLParams(Qry, Buff);
2508 >        SetInternalSQLParams(Qry.Params, Buff);
2509          Qry.ExecQuery;
2510          try
2511            if (Qry.SQLStatementType = SQLExecProcedure) or Qry.Next then
# Line 2590 | Line 2714 | end;
2714  
2715   procedure TIBCustomDataSet.SetDatabase(Value: TIBDatabase);
2716   begin
2717 <  if (FBase.Database <> Value) then
2717 >  if (csLoading in ComponentState) or (FBase.Database <> Value) then
2718    begin
2719      CheckDatasetClosed;
2720 +    InternalUnPrepare;
2721      FBase.Database := Value;
2722      FQDelete.Database := Value;
2723      FQInsert.Database := Value;
2724      FQRefresh.Database := Value;
2725      FQSelect.Database := Value;
2726      FQModify.Database := Value;
2727 +    FDatabaseInfo.Database := Value;
2728 +    FGeneratorField.Database := Value;
2729    end;
2730   end;
2731  
# Line 2620 | Line 2747 | begin
2747    end;
2748   end;
2749  
2750 < procedure TIBCustomDataSet.SetInternalSQLParams(Qry: TIBSQL; Buffer: Pointer);
2750 > procedure TIBCustomDataSet.SetInternalSQLParams(Params: ISQLParams; Buffer: Pointer);
2751   var
2752    i, j: Integer;
2753    cr, data: PChar;
# Line 2636 | Line 2763 | begin
2763      InternalPrepare;
2764    OldBuffer := nil;
2765    try
2766 <    for i := 0 to Qry.Params.GetCount - 1 do
2766 >    for i := 0 to Params.GetCount - 1 do
2767      begin
2768 <      Param := Qry.Params[i];
2768 >      Param := Params[i];
2769        fn := Param.Name;
2770        if (Pos('OLD_', fn) = 1) then {mbcs ok}
2771        begin
# Line 2702 | Line 2829 | begin
2829              end;
2830              SQL_BLOB, SQL_ARRAY, SQL_QUAD:
2831                Param.AsQuad := PISC_QUAD(data)^;
2832 <            SQL_TYPE_DATE:
2833 <            begin
2707 <              ts.Date := PInt(data)^;
2708 <              ts.Time := 0;
2709 <              Param.AsDate := TimeStampToDateTime(ts);
2710 <            end;
2711 <            SQL_TYPE_TIME:
2712 <            begin
2713 <              ts.Date := 0;
2714 <              ts.Time := PInt(data)^;
2715 <              Param.AsTime := TimeStampToDateTime(ts);
2716 <            end;
2832 >            SQL_TYPE_DATE,
2833 >            SQL_TYPE_TIME,
2834              SQL_TIMESTAMP:
2835 <              Param.AsDateTime :=
2836 <                       TimeStampToDateTime(MSecsToTimeStamp(trunc(PDouble(data)^)));
2835 >            {This is an IBX native format and not the TDataset approach. See also SetFieldData}
2836 >              Param.AsDateTime := PDateTime(data)^;
2837              SQL_BOOLEAN:
2838                Param.AsBoolean := PWordBool(data)^;
2839            end;
# Line 2767 | Line 2884 | begin
2884      FQRefresh.Transaction := Value;
2885      FQSelect.Transaction := Value;
2886      FQModify.Transaction := Value;
2887 +    FGeneratorField.Transaction := Value;
2888    end;
2889   end;
2890  
# Line 3668 | Line 3786 | const
3786                 'and R.RDB$FIELD_SOURCE = F.RDB$FIELD_NAME '+ {do not localize}
3787                 'and ((not F.RDB$COMPUTED_BLR is NULL) or ' + {do not localize}
3788                 '     (not F.RDB$DEFAULT_VALUE is NULL)) '; {do not localize}
3789 +
3790 +  DefaultSQLODS12 = 'Select F.RDB$COMPUTED_BLR, ' + {do not localize}
3791 +               'F.RDB$DEFAULT_VALUE, R.RDB$FIELD_NAME, R.RDB$IDENTITY_TYPE ' + {do not localize}
3792 +               'from RDB$RELATION_FIELDS R, RDB$FIELDS F ' + {do not localize}
3793 +               'where R.RDB$RELATION_NAME = :RELATION ' +  {do not localize}
3794 +               'and R.RDB$FIELD_SOURCE = F.RDB$FIELD_NAME '+ {do not localize}
3795 +               'and ((not F.RDB$COMPUTED_BLR is NULL) or ' + {do not localize}
3796 +               '     (not F.RDB$DEFAULT_VALUE is NULL) or ' + {do not localize}
3797 +               '     ( not R.RDB$IDENTITY_TYPE is NULL))' ; {do not localize}
3798 +
3799   var
3800    FieldType: TFieldType;
3801    FieldSize: Word;
3802 +  FieldDataSize: integer;
3803    charSetID: short;
3804    CharSetSize: integer;
3805    CharSetName: RawByteString;
# Line 3707 | Line 3836 | var
3836        FField.FieldName := Query.Fields[2].AsString;
3837        FField.DEFAULT_VALUE := not Query.Fields[1].IsNull;
3838        FField.COMPUTED_BLR := not Query.Fields[0].IsNull;
3839 +      FField.IDENTITY_COLUMN := (Query.FieldCount > 3) and not Query.Fields[3].IsNull;
3840        FField.NextField := Result.FieldNodes;
3841        Result.FieldNodes := FField;
3842        Query.Next;
# Line 3760 | Line 3890 | var
3890          FField := Ffield.NextField;
3891    end;
3892  
3893 +  function Is_IDENTITY_COLUMN(Relation, Field : String) : Boolean;
3894 +  var
3895 +    FRelation : TRelationNode;
3896 +    FField : TFieldNode;
3897 +  begin
3898 +    FRelation := FRelationNodes;
3899 +    while Assigned(FRelation) and
3900 +         (FRelation.RelationName <> Relation) do
3901 +      FRelation := FRelation.NextRelation;
3902 +    if not Assigned(FRelation) then
3903 +      FRelation := Add_Node(Relation, Field);
3904 +    Result := false;
3905 +    FField := FRelation.FieldNodes;
3906 +    while Assigned(FField) do
3907 +      if FField.FieldName = Field then
3908 +      begin
3909 +        Result := Ffield.IDENTITY_COLUMN;
3910 +        Exit;
3911 +      end
3912 +      else
3913 +        FField := Ffield.NextField;
3914 +  end;
3915 +
3916    Procedure FreeNodes;
3917    var
3918      FRelation : TRelationNode;
# Line 3793 | Line 3946 | begin
3946      FieldIndex := 0;
3947      if (Length(FMappedFieldPosition) < SourceQuery.MetaData.Count) then
3948        SetLength(FMappedFieldPosition, SourceQuery.MetaData.Count);
3949 <    Query.SQL.Text := DefaultSQL;
3949 >    if FDatabaseInfo.ODSMajorVersion >= 12 then
3950 >      Query.SQL.Text := DefaultSQLODS12
3951 >    else
3952 >      Query.SQL.Text := DefaultSQL;
3953      Query.Prepare;
3954      SetLength(FAliasNameMap, SourceQuery.MetaData.Count);
3955      SetLength(FAliasNameList, SourceQuery.MetaData.Count);
# Line 3807 | Line 3963 | begin
3963          FieldName := getSQLName;
3964          FAliasNameList[i] := DBAliasName;
3965          FieldSize := 0;
3966 +        FieldDataSize := GetSize;
3967          FieldPrecision := 0;
3968          FieldNullable := IsNullable;
3969          CharSetSize := 0;
# Line 3819 | Line 3976 | begin
3976             their values }
3977            SQL_VARYING, SQL_TEXT:
3978            begin
3979 <            FirebirdAPI.CharSetWidth(getCharSetID,CharSetSize);
3980 <            CharSetName := FirebirdAPI.GetCharsetName(getCharSetID);
3981 <            FirebirdAPI.CharSetID2CodePage(getCharSetID,FieldCodePage);
3982 <            FieldSize := GetSize div CharSetSize;
3979 >            if not Database.Attachment.CharSetWidth(getCharSetID,CharSetSize) then
3980 >              CharSetSize := 1;
3981 >            CharSetName := Database.Attachment.GetCharsetName(getCharSetID);
3982 >            Database.Attachment.CharSetID2CodePage(getCharSetID,FieldCodePage);
3983 >            FieldSize := FieldDataSize div CharSetSize;
3984              FieldType := ftString;
3985            end;
3986            { All Doubles/Floats should be cast to doubles }
# Line 3873 | Line 4031 | begin
4031                FieldSize := -getScale;
4032              end
4033              else
4034 <              FieldType := ftFloat
4034 >              FieldType := ftFloat;
4035            end;
4036            SQL_TIMESTAMP: FieldType := ftDateTime;
4037            SQL_TYPE_TIME: FieldType := ftTime;
# Line 3883 | Line 4041 | begin
4041              FieldSize := sizeof (TISC_QUAD);
4042              if (getSubtype = 1) then
4043              begin
4044 <              FirebirdAPI.CharSetWidth(getCharSetID,CharSetSize);
4045 <              CharSetName := FirebirdAPI.GetCharsetName(getCharSetID);
4046 <              FirebirdAPI.CharSetID2CodePage(getCharSetID,FieldCodePage);
4044 >              if not Database.Attachment.CharSetWidth(getCharSetID,CharSetSize) then
4045 >                CharSetSize := 1;
4046 >              CharSetName := Database.Attachment.GetCharsetName(getCharSetID);
4047 >              Database.Attachment.CharSetID2CodePage(getCharSetID,FieldCodePage);
4048                FieldType := ftMemo;
4049              end
4050              else
# Line 3917 | Line 4076 | begin
4076              Name := FieldAliasName;
4077              FAliasNameMap[FieldNo-1] := DBAliasName;
4078              Size := FieldSize;
4079 +            DataSize := FieldDataSize;
4080              Precision := FieldPrecision;
4081              Required := not FieldNullable;
4082              RelationName := aRelationName;
# Line 3928 | Line 4088 | begin
4088              ArrayBounds := aArrayBounds;
4089              if (FieldName <> '') and (RelationName <> '') then
4090              begin
4091 +              IdentityColumn := Is_IDENTITY_COLUMN(RelationName, FieldName);
4092                if Has_COMPUTED_BLR(RelationName, FieldName) then
4093                begin
4094                  Attributes := [faReadOnly];
# Line 3999 | Line 4160 | begin
4160      for i := 0 to SQLParams.GetCount - 1 do
4161      begin
4162        cur_field := DataSource.DataSet.FindField(SQLParams[i].Name);
4163 <      cur_param := SQLParams[i];
4164 <      if (cur_field <> nil) then begin
4163 >      if (cur_field <> nil) then
4164 >      begin
4165 >        cur_param := SQLParams[i];
4166          if (cur_field.IsNull) then
4167            cur_param.IsNull := True
4168 <        else case cur_field.DataType of
4168 >        else
4169 >        case cur_field.DataType of
4170            ftString:
4171              cur_param.AsString := cur_field.AsString;
4172            ftBoolean:
# Line 4013 | Line 4176 | begin
4176            ftInteger:
4177              cur_param.AsLong := cur_field.AsInteger;
4178            ftLargeInt:
4179 <            cur_param.AsInt64 := TLargeIntField(cur_field).AsLargeInt;
4179 >            cur_param.AsInt64 := cur_field.AsLargeInt;
4180            ftFloat, ftCurrency:
4181             cur_param.AsDouble := cur_field.AsFloat;
4182            ftBCD:
# Line 4672 | Line 4835 | begin
4835    Transaction.StartTransaction;
4836   end;
4837  
4838 < function TIBCustomDataSet.PSGetTableName: string;
4838 > function TIBCustomDataSet.PsGetTableName: string;
4839   begin
4840   //  if not FInternalPrepared then
4841   //    InternalPrepare;
# Line 4767 | Line 4930 | end;
4930   function TIBCustomDataSet.GetFieldData(Field: TField; Buffer: Pointer;
4931    NativeFormat: Boolean): Boolean;
4932   begin
4933 <  if (Field.DataType = ftBCD) and not NativeFormat then
4933 >  {These datatypes use IBX conventions and not TDataset conventions}
4934 >  if (Field.DataType in [ftBCD,ftDateTime,ftDate,ftTime]) and not NativeFormat then
4935      Result := InternalGetFieldData(Field, Buffer)
4936    else
4937      Result := inherited GetFieldData(Field, Buffer, NativeFormat);
# Line 4793 | Line 4957 | end;
4957   procedure TIBCustomDataSet.SetFieldData(Field: TField; Buffer: Pointer;
4958    NativeFormat: Boolean);
4959   begin
4960 <  if (not NativeFormat) and (Field.DataType = ftBCD) then
4960 >  {These datatypes use IBX conventions and not TDataset conventions}
4961 >  if (not NativeFormat) and (Field.DataType in [ftBCD,ftDateTime,ftDate,ftTime]) then
4962      InternalSetfieldData(Field, Buffer)
4963    else
4964      inherited SetFieldData(Field, buffer, NativeFormat);
# Line 4813 | Line 4978 | begin
4978    inherited Destroy;
4979   end;
4980  
4981 < procedure TIBDataSetUpdateObject.SetRefreshSQL(Value: TStrings);
4981 > procedure TIBDataSetUpdateObject.SetRefreshSQL(value: TStrings);
4982   begin
4983    FRefreshSQL.Assign(Value);
4984   end;
4985  
4986 + procedure TIBDataSetUpdateObject.InternalSetParams(Params: ISQLParams;
4987 +  buff: PChar);
4988 + begin
4989 +  if not Assigned(DataSet) then Exit;
4990 +  DataSet.SetInternalSQLParams(Params, buff);
4991 + end;
4992 +
4993   procedure TIBDataSetUpdateObject.InternalSetParams(Query: TIBSQL; buff: PChar);
4994   begin
4995 +  InternalSetParams(Query.Params,buff);
4996 + end;
4997 +
4998 + procedure TIBDataSetUpdateObject.UpdateRecordFromQuery(QryResults: IResults;
4999 +  Buffer: PChar);
5000 + begin
5001    if not Assigned(DataSet) then Exit;
5002 <  DataSet.SetInternalSQLParams(Query, buff);
5002 >  DataSet.UpdateRecordFromQuery(QryResults, Buffer);
5003   end;
5004  
5005   function TIBDSBlobStream.GetSize: Int64;
# Line 4884 | Line 5062 | end;
5062  
5063   procedure TIBGenerator.SetIncrement(const AValue: integer);
5064   begin
5065 +  if FIncrement = AValue then Exit;
5066    if AValue < 0 then
5067 <     raise Exception.Create('A Generator Increment cannot be negative');
5068 <  FIncrement := AValue
5067 >    IBError(ibxeNegativeGenerator,[]);
5068 >  FIncrement := AValue;
5069 >  SetQuerySQL;
5070   end;
5071  
5072 < function TIBGenerator.GetNextValue(ADatabase: TIBDatabase;
4893 <  ATransaction: TIBTransaction): integer;
5072 > procedure TIBGenerator.SetTransaction(AValue: TIBTransaction);
5073   begin
5074 <  with TIBSQL.Create(nil) do
5075 <  try
5076 <    Database := ADatabase;
5077 <    Transaction := ATransaction;
5078 <    if not assigned(Database) then
5079 <       IBError(ibxeCannotSetDatabase,[]);
5080 <    if not assigned(Transaction) then
5081 <       IBError(ibxeCannotSetTransaction,[]);
5082 <    with Transaction do
5083 <      if not InTransaction then StartTransaction;
5084 <    SQL.Text := Format('Select Gen_ID(%s,%d) as ID From RDB$Database',[FGeneratorName,Increment]);
5085 <    Prepare;
5074 >  FQuery.Transaction := AValue;
5075 > end;
5076 >
5077 > procedure TIBGenerator.SetQuerySQL;
5078 > begin
5079 >  FQuery.SQL.Text := Format('Select Gen_ID(%s,%d) From RDB$Database',[FGeneratorName,Increment]);
5080 > end;
5081 >
5082 > function TIBGenerator.GetDatabase: TIBDatabase;
5083 > begin
5084 >  Result := FQuery.Database;
5085 > end;
5086 >
5087 > function TIBGenerator.GetTransaction: TIBTransaction;
5088 > begin
5089 >  Result := FQuery.Transaction;
5090 > end;
5091 >
5092 > procedure TIBGenerator.SetDatabase(AValue: TIBDatabase);
5093 > begin
5094 >  FQuery.Database := AValue;
5095 > end;
5096 >
5097 > procedure TIBGenerator.SetGeneratorName(AValue: string);
5098 > begin
5099 >  if FGeneratorName = AValue then Exit;
5100 >  FGeneratorName := AValue;
5101 >  SetQuerySQL;
5102 > end;
5103 >
5104 > function TIBGenerator.GetNextValue: integer;
5105 > begin
5106 >  with FQuery do
5107 >  begin
5108 >    Transaction.Active := true;
5109      ExecQuery;
5110      try
5111 <      Result := FieldByName('ID').AsInteger
5111 >      Result := Fields[0].AsInteger
5112      finally
5113        Close
5114      end;
4913  finally
4914    Free
5115    end;
5116   end;
5117  
# Line 4919 | Line 5119 | constructor TIBGenerator.Create(Owner: T
5119   begin
5120    FOwner := Owner;
5121    FIncrement := 1;
5122 +  FQuery := TIBSQL.Create(nil);
5123 + end;
5124 +
5125 + destructor TIBGenerator.Destroy;
5126 + begin
5127 +  if assigned(FQuery) then FQuery.Free;
5128 +  inherited Destroy;
5129   end;
5130  
5131  
5132   procedure TIBGenerator.Apply;
5133   begin
5134 <  if (FGeneratorName <> '') and (FFieldName <> '') and Owner.FieldByName(FFieldName).IsNull then
5135 <    Owner.FieldByName(FFieldName).AsInteger := GetNextValue(Owner.Database,Owner.Transaction);
5134 >  if assigned(Database) and assigned(Transaction) and
5135 >       (FGeneratorName <> '') and (FFieldName <> '') and Owner.FieldByName(FFieldName).IsNull then
5136 >    Owner.FieldByName(FFieldName).AsInteger := GetNextValue;
5137   end;
5138  
5139  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines