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 60 by tony, Mon Mar 27 15:21:02 2017 UTC vs.
Revision 139 by tony, Wed Jan 24 16:16:29 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(UpdateKind: TUpdateKind; 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 318 | Line 371 | type
371  
372    TOnValidatePost = procedure (Sender: TObject; var CancelPost: boolean) of object;
373  
374 +  TOnDeleteReturning = procedure (Sender: TObject; QryResults: IResults) of object;
375 +
376    TIBCustomDataSet = class(TDataset)
377    private
378      FAllowAutoActivateTransaction: Boolean;
# Line 350 | Line 405 | type
405      FDeletedRecords: Long;
406      FModelBuffer,
407      FOldBuffer: PChar;
408 +    FOnDeleteReturning: TOnDeleteReturning;
409      FOnValidatePost: TOnValidatePost;
410      FOpen: Boolean;
411      FInternalPrepared: Boolean;
# Line 358 | Line 414 | type
414      FQRefresh,
415      FQSelect,
416      FQModify: TIBSQL;
417 +    FDatabaseInfo: TIBDatabaseInfo;
418      FRecordBufferSize: Integer;
419      FRecordCount: Integer;
420      FRecordSize: Integer;
# Line 387 | Line 444 | type
444      FInTransactionEnd: boolean;
445      FIBLinks: TList;
446      FFieldColumns: PFieldColumns;
447 +    FBufferUpdatedOnQryReturn: boolean;
448 +    procedure ColumnDataToBuffer(QryResults: IResults; ColumnIndex,
449 +      FieldIndex: integer; Buffer: PChar);
450      procedure InitModelBuffer(Qry: TIBSQL; Buffer: PChar);
451      function GetSelectStmtIntf: IStatement;
452      procedure SetUpdateMode(const Value: TUpdateMode);
# Line 409 | Line 469 | type
469      procedure DoBeforeTransactionEnd(Sender: TObject; Action: TTransactionAction);
470      procedure DoAfterTransactionEnd(Sender: TObject);
471      procedure DoTransactionFree(Sender: TObject);
472 +    procedure DoDeleteReturning(QryResults: IResults);
473      procedure FetchCurrentRecordToBuffer(Qry: TIBSQL; RecordNumber: Integer;
474                                           Buffer: PChar);
475      function GetDatabase: TIBDatabase;
# Line 434 | Line 495 | type
495      procedure SetDatabase(Value: TIBDatabase);
496      procedure SetDeleteSQL(Value: TStrings);
497      procedure SetInsertSQL(Value: TStrings);
498 <    procedure SetInternalSQLParams(Qry: TIBSQL; Buffer: Pointer);
498 >    procedure SetInternalSQLParams(Params: ISQLParams; Buffer: Pointer);
499      procedure SetRefreshSQL(Value: TStrings);
500      procedure SetSelectSQL(Value: TStrings);
501      procedure SetModifySQL(Value: TStrings);
502      procedure SetTransaction(Value: TIBTransaction);
503      procedure SetUpdateRecordTypes(Value: TIBUpdateRecordTypes);
504      procedure SetUniDirectional(Value: Boolean);
505 +    procedure UpdateRecordFromQuery(QryResults: IResults; Buffer: PChar);
506      procedure RefreshParams;
507      function AdjustPosition(FCache: PChar; Offset: DWORD;
508                              Origin: Integer): DWORD;
# Line 628 | Line 690 | type
690      procedure Post; override;
691      function ParamByName(ParamName: String): ISQLParam;
692      property ArrayFieldCount: integer read FArrayFieldCount;
693 +    property DatabaseInfo: TIBDatabaseInfo read FDatabaseInfo;
694      property UpdateObject: TIBDataSetUpdateObject read FUpdateObject write SetUpdateObject;
695      property UpdatesPending: Boolean read FUpdatesPending;
696      property UpdateRecordTypes: TIBUpdateRecordTypes read FUpdateRecordTypes
# Line 672 | Line 735 | type
735                                                   write FOnUpdateError;
736      property OnUpdateRecord: TIBUpdateRecordEvent read FOnUpdateRecord
737                                                     write FOnUpdateRecord;
738 +    property OnDeleteReturning: TOnDeleteReturning read FOnDeleteReturning
739 +                                                   write FOnDeleteReturning;
740    end;
741  
742    TIBParserDataSet = class(TIBCustomDataSet)
# Line 758 | Line 823 | type
823      property OnNewRecord;
824      property OnPostError;
825      property OnValidatePost;
826 +    property OnDeleteReturning;
827    end;
828  
829    { TIBDSBlobStream }
# Line 789 | Line 855 | type
855      FCharacterSetName: RawByteString;
856      FCharacterSetSize: integer;
857      FCodePage: TSystemCodePage;
858 +    FIdentityColumn: boolean;
859      FRelationName: string;
860 +    FDataSize: integer;
861    published
862      property CharacterSetName: RawByteString read FCharacterSetName write FCharacterSetName;
863      property CharacterSetSize: integer read FCharacterSetSize write FCharacterSetSize;
864      property CodePage: TSystemCodePage read FCodePage write FCodePage;
865 +    property DataSize: integer read FDataSize write FDataSize;
866      property RelationName: string read FRelationName write FRelationName;
867      property ArrayDimensions: integer read FArrayDimensions write FArrayDimensions;
868      property ArrayBounds: TArrayBounds read FArrayBounds write FArrayBounds;
869 +    property IdentityColumn: boolean read FIdentityColumn write FIdentityColumn default false;
870    end;
871  
872   const
873   DefaultFieldClasses: array[TFieldType] of TFieldClass = (
874      nil,                { ftUnknown }
875      TIBStringField,     { ftString }
876 <    TSmallintField,     { ftSmallint }
877 <    TIntegerField,      { ftInteger }
876 >    TIBSmallintField,   { ftSmallint }
877 >    TIBIntegerField,      { ftInteger }
878      TWordField,         { ftWord }
879      TBooleanField,      { ftBoolean }
880      TFloatField,        { ftFloat }
# Line 826 | Line 896 | DefaultFieldClasses: array[TFieldType] o
896      nil,                { ftCursor }
897      TStringField,       { ftFixedChar }
898      nil,    { ftWideString }
899 <    TLargeIntField,     { ftLargeInt }
899 >    TIBLargeIntField,     { ftLargeInt }
900      nil,          { ftADT }
901      TIBArrayField,        { ftArray }
902      nil,    { ftReference }
# Line 870 | Line 940 | type
940      FieldName : String;
941      COMPUTED_BLR : Boolean;
942      DEFAULT_VALUE : boolean;
943 +    IDENTITY_COLUMN : boolean;
944      NextField : TFieldNode;
945    end;
946  
# Line 922 | Line 993 | type
993      Result := str;
994    end;
995  
996 + { TIBLargeIntField }
997 +
998 + procedure TIBLargeIntField.Bind(Binding: Boolean);
999 + begin
1000 +  inherited Bind(Binding);
1001 +  if Binding and (FieldDef <> nil) then
1002 +     FIdentityColumn := (FieldDef as TIBFieldDef).IdentityColumn;
1003 + end;
1004 +
1005 + { TIBIntegerField }
1006 +
1007 + procedure TIBIntegerField.Bind(Binding: Boolean);
1008 + begin
1009 +  inherited Bind(Binding);
1010 +  if Binding and (FieldDef <> nil) then
1011 +     FIdentityColumn := (FieldDef as TIBFieldDef).IdentityColumn;
1012 + end;
1013 +
1014 + { TIBSmallintField }
1015 +
1016 + procedure TIBSmallintField.Bind(Binding: Boolean);
1017 + begin
1018 +  inherited Bind(Binding);
1019 +  if Binding and (FieldDef <> nil) then
1020 +     FIdentityColumn := (FieldDef as TIBFieldDef).IdentityColumn;
1021 + end;
1022 +
1023   { TIBArray }
1024  
1025   procedure TIBArray.EventHandler(Sender: IArray; Reason: TArrayEventReason);
# Line 1031 | Line 1129 | begin
1129         {2: case 2 ignored. This should be handled by TIBWideMemo}
1130  
1131         3, {Assume UNICODE_FSS is really UTF8}
1132 <       4: {Include GB18030 - assuming UTF8 routine work for this codeset}
1132 >       4: {Include GB18030 - assuming UTF8 routines work for this codeset}
1133           if DisplayWidth = 0 then
1134 <           Result := ValidUTF8String(TextToSingleLine(Result))
1134 >           Result := Utf8EscapeControlChars(TextToSingleLine(Result))
1135           else
1136           if UTF8Length(Result) > DisplayWidth then {Show truncation with elipses}
1137 <           Result := ValidUTF8String(TextToSingleLine(UTF8Copy(Result,1,DisplayWidth-3))) + '...';
1137 >           Result := Utf8EscapeControlChars(TextToSingleLine(UTF8Copy(Result,1,DisplayWidth-3))) + '...';
1138         end;
1139     end
1140   end;
# Line 1141 | Line 1239 | begin
1239      IBFieldDef := FieldDef as TIBFieldDef;
1240      CharacterSetSize := IBFieldDef.CharacterSetSize;
1241      CharacterSetName := IBFieldDef.CharacterSetName;
1242 +    FDataSize := IBFieldDef.DataSize + 1;
1243      if AutoFieldSize then
1244        Size := IBFieldDef.Size;
1245      CodePage := IBFieldDef.CodePage;
# Line 1149 | Line 1248 | end;
1248  
1249   function TIBStringField.GetDataSize: Integer;
1250   begin
1251 <  Result := Size * CharacterSetSize + 1;
1251 >  Result := FDataSize;
1252   end;
1253  
1254   constructor TIBStringField.Create(aOwner: TComponent);
# Line 1232 | Line 1331 | begin
1331    Size := 8;
1332   end;
1333  
1334 + procedure TIBBCDField.Bind(Binding: Boolean);
1335 + begin
1336 +  inherited Bind(Binding);
1337 +  if Binding and (FieldDef <> nil) then
1338 +     FIdentityColumn := (FieldDef as TIBFieldDef).IdentityColumn;
1339 + end;
1340 +
1341   class procedure TIBBCDField.CheckTypeSize(Value: Integer);
1342   begin
1343   { No need to check as the base type is currency, not BCD }
# Line 1317 | Line 1423 | constructor TIBCustomDataSet.Create(AOwn
1423   begin
1424    inherited Create(AOwner);
1425    FBase := TIBBase.Create(Self);
1426 +  FDatabaseInfo := TIBDatabaseInfo.Create(self);
1427    FIBLinks := TList.Create;
1428    FCurrentRecord := -1;
1429    FDeletedRecords := 0;
# Line 1866 | Line 1973 | begin
1973      FTransactionFree(Sender);
1974   end;
1975  
1976 + procedure TIBCustomDataSet.DoDeleteReturning(QryResults: IResults);
1977 + begin
1978 +  if assigned(FOnDeleteReturning) then
1979 +     OnDeleteReturning(self,QryResults);
1980 + end;
1981 +
1982   procedure TIBCustomDataSet.InitModelBuffer(Qry: TIBSQL; Buffer: PChar);
1983   var i, j: Integer;
1984      FieldsLoaded: integer;
# Line 1955 | Line 2068 | begin
2068    end;
2069   end;
2070  
2071 + {Update Buffer Fields from Query Results}
2072 +
2073 + procedure TIBCustomDataSet.UpdateRecordFromQuery(QryResults: IResults;
2074 +  Buffer: PChar);
2075 + var i, j: integer;
2076 + begin
2077 +  for i := 0 to QryResults.Count - 1 do
2078 +  begin
2079 +    j := GetFieldPosition(QryResults[i].GetAliasName);
2080 +    if j > 0 then
2081 +    begin
2082 +      ColumnDataToBuffer(QryResults,i,j,Buffer);
2083 +      FBufferUpdatedOnQryReturn := true;
2084 +    end;
2085 +  end;
2086 + end;
2087 +
2088 +
2089 + {Move column data returned from query to row buffer}
2090 +
2091 + procedure TIBCustomDataSet.ColumnDataToBuffer(QryResults: IResults;
2092 +               ColumnIndex, FieldIndex: integer; Buffer: PChar);
2093 + var
2094 +  LocalData: PByte;
2095 +  LocalDate: TDateTime;
2096 +  LocalDouble: Double;
2097 +  LocalInt: Integer;
2098 +  LocalBool: wordBool;
2099 +  LocalInt64: Int64;
2100 +  LocalCurrency: Currency;
2101 +  ColData: ISQLData;
2102 + begin
2103 +  LocalData := nil;
2104 +  with PRecordData(Buffer)^.rdFields[FieldIndex], FFieldColumns^[FieldIndex] do
2105 +  begin
2106 +    QryResults.GetData(ColumnIndex,fdIsNull,fdDataLength,LocalData);
2107 +    if not fdIsNull then
2108 +    begin
2109 +      ColData := QryResults[ColumnIndex];
2110 +      case fdDataType of  {Get Formatted data for column types that need formatting}
2111 +        SQL_TYPE_DATE,
2112 +        SQL_TYPE_TIME,
2113 +        SQL_TIMESTAMP:
2114 +        begin
2115 +          {This is an IBX native format and not the TDataset approach. See also GetFieldData}
2116 +          LocalDate := ColData.AsDateTime;
2117 +          LocalData := PByte(@LocalDate);
2118 +        end;
2119 +        SQL_SHORT, SQL_LONG:
2120 +        begin
2121 +          if (fdDataScale = 0) then
2122 +          begin
2123 +            LocalInt := ColData.AsLong;
2124 +            LocalData := PByte(@LocalInt);
2125 +          end
2126 +          else
2127 +          if (fdDataScale >= (-4)) then
2128 +          begin
2129 +            LocalCurrency := ColData.AsCurrency;
2130 +            LocalData := PByte(@LocalCurrency);
2131 +          end
2132 +          else
2133 +          begin
2134 +           LocalDouble := ColData.AsDouble;
2135 +           LocalData := PByte(@LocalDouble);
2136 +          end;
2137 +        end;
2138 +        SQL_INT64:
2139 +        begin
2140 +          if (fdDataScale = 0) then
2141 +          begin
2142 +            LocalInt64 := ColData.AsInt64;
2143 +            LocalData := PByte(@LocalInt64);
2144 +          end
2145 +          else
2146 +          if (fdDataScale >= (-4)) then
2147 +          begin
2148 +            LocalCurrency := ColData.AsCurrency;
2149 +            LocalData := PByte(@LocalCurrency);
2150 +            end
2151 +            else
2152 +            begin
2153 +              LocalDouble := ColData.AsDouble;
2154 +              LocalData := PByte(@LocalDouble);
2155 +            end
2156 +        end;
2157 +        SQL_DOUBLE, SQL_FLOAT, SQL_D_FLOAT:
2158 +        begin
2159 +          LocalDouble := ColData.AsDouble;
2160 +          LocalData := PByte(@LocalDouble);
2161 +        end;
2162 +        SQL_BOOLEAN:
2163 +        begin
2164 +          LocalBool := ColData.AsBoolean;
2165 +          LocalData := PByte(@LocalBool);
2166 +        end;
2167 +      end;
2168 +
2169 +      if fdDataType = SQL_VARYING then
2170 +        Move(LocalData^, Buffer[fdDataOfs], fdDataLength)
2171 +      else
2172 +        Move(LocalData^, Buffer[fdDataOfs], fdDataSize)
2173 +    end
2174 +    else {Null column}
2175 +    if fdDataType = SQL_VARYING then
2176 +      FillChar(Buffer[fdDataOfs],fdDataLength,0)
2177 +    else
2178 +      FillChar(Buffer[fdDataOfs],fdDataSize,0);
2179 +  end;
2180 + end;
2181 +
2182   { Read the record from FQSelect.Current into the record buffer
2183    Then write the buffer to in memory cache }
2184   procedure TIBCustomDataSet.FetchCurrentRecordToBuffer(Qry: TIBSQL;
# Line 1963 | Line 2187 | var
2187    pbd: PBlobDataArray;
2188    pda: PArrayDataArray;
2189    i, j: Integer;
1966  LocalData: PByte;
1967  LocalDate, LocalDouble: Double;
1968  LocalInt: Integer;
1969  LocalBool: wordBool;
1970  LocalInt64: Int64;
1971  LocalCurrency: Currency;
2190    FieldsLoaded: Integer;
2191    p: PRecordData;
2192   begin
# Line 2019 | Line 2237 | begin
2237          continue;
2238        end;
2239      if j > 0 then
2240 <    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 := PByte(@LocalDate);
2034 <            end;
2035 <            SQL_TYPE_DATE:
2036 <            begin
2037 <              LocalInt := DateTimeToTimeStamp(Qry[i].AsDateTime).Date;
2038 <              LocalData := PByte(@LocalInt);
2039 <            end;
2040 <            SQL_TYPE_TIME:
2041 <            begin
2042 <              LocalInt := DateTimeToTimeStamp(Qry[i].AsDateTime).Time;
2043 <              LocalData := PByte(@LocalInt);
2044 <            end;
2045 <            SQL_SHORT, SQL_LONG:
2046 <            begin
2047 <              if (fdDataScale = 0) then
2048 <              begin
2049 <                LocalInt := Qry[i].AsLong;
2050 <                LocalData := PByte(@LocalInt);
2051 <              end
2052 <              else
2053 <              if (fdDataScale >= (-4)) then
2054 <              begin
2055 <                LocalCurrency := Qry[i].AsCurrency;
2056 <                LocalData := PByte(@LocalCurrency);
2057 <              end
2058 <              else
2059 <              begin
2060 <               LocalDouble := Qry[i].AsDouble;
2061 <               LocalData := PByte(@LocalDouble);
2062 <              end;
2063 <            end;
2064 <            SQL_INT64:
2065 <            begin
2066 <              if (fdDataScale = 0) then
2067 <              begin
2068 <                LocalInt64 := Qry[i].AsInt64;
2069 <                LocalData := PByte(@LocalInt64);
2070 <              end
2071 <              else
2072 <              if (fdDataScale >= (-4)) then
2073 <              begin
2074 <                LocalCurrency := Qry[i].AsCurrency;
2075 <                LocalData := PByte(@LocalCurrency);
2076 <                end
2077 <                else
2078 <                begin
2079 <                  LocalDouble := Qry[i].AsDouble;
2080 <                  LocalData := PByte(@LocalDouble);
2081 <                end
2082 <            end;
2083 <            SQL_DOUBLE, SQL_FLOAT, SQL_D_FLOAT:
2084 <            begin
2085 <              LocalDouble := Qry[i].AsDouble;
2086 <              LocalData := PByte(@LocalDouble);
2087 <            end;
2088 <            SQL_BOOLEAN:
2089 <            begin
2090 <              LocalBool := Qry[i].AsBoolean;
2091 <              LocalData := PByte(@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;
2240 >      ColumnDataToBuffer(Qry.Current,i,j,Buffer);
2241    end;
2242    WriteRecordCache(RecordNumber, Buffer);
2243   end;
# Line 2198 | Line 2332 | begin
2332      FUpdateObject.Apply(ukDelete,Buff)
2333    else
2334    begin
2335 <    SetInternalSQLParams(FQDelete, Buff);
2335 >    SetInternalSQLParams(FQDelete.Params, Buff);
2336      FQDelete.ExecQuery;
2337 +    if (FQDelete.FieldCount > 0)  then
2338 +      DoDeleteReturning(FQDelete.Current);
2339    end;
2340    with PRecordData(Buff)^ do
2341    begin
# Line 2337 | Line 2473 | begin
2473        end;
2474        Inc(arr);
2475      end;
2476 +  FBufferUpdatedOnQryReturn := false;
2477    if Assigned(FUpdateObject) then
2478    begin
2479      if (Qry = FQDelete) then
# Line 2347 | Line 2484 | begin
2484        FUpdateObject.Apply(ukModify,Buff);
2485    end
2486    else begin
2487 <    SetInternalSQLParams(Qry, Buff);
2487 >    SetInternalSQLParams(Qry.Params, Buff);
2488      Qry.ExecQuery;
2489 +    if Qry.FieldCount > 0 then {Has RETURNING Clause}
2490 +      UpdateRecordFromQuery(Qry.Current,Buff);
2491    end;
2492    PRecordData(Buff)^.rdUpdateStatus := usUnmodified;
2493    PRecordData(Buff)^.rdCachedUpdateStatus := cusUnmodified;
2494    SetModified(False);
2495    WriteRecordCache(PRecordData(Buff)^.rdRecordNumber, Buff);
2496 <  if (FForcedRefresh or FNeedsRefresh) and CanRefresh then
2496 >  if (FForcedRefresh or (FNeedsRefresh and not FBufferUpdatedOnQryReturn)) and CanRefresh then
2497      InternalRefreshRow;
2498   end;
2499  
# Line 2381 | Line 2520 | begin
2520          end
2521          else
2522            Qry := FQRefresh;
2523 <        SetInternalSQLParams(Qry, Buff);
2523 >        SetInternalSQLParams(Qry.Params, Buff);
2524          Qry.ExecQuery;
2525          try
2526            if (Qry.SQLStatementType = SQLExecProcedure) or Qry.Next then
# Line 2590 | Line 2729 | end;
2729  
2730   procedure TIBCustomDataSet.SetDatabase(Value: TIBDatabase);
2731   begin
2732 <  if (FBase.Database <> Value) then
2732 >  if (csLoading in ComponentState) or (FBase.Database <> Value) then
2733    begin
2734      CheckDatasetClosed;
2735 +    InternalUnPrepare;
2736      FBase.Database := Value;
2737      FQDelete.Database := Value;
2738      FQInsert.Database := Value;
2739      FQRefresh.Database := Value;
2740      FQSelect.Database := Value;
2741      FQModify.Database := Value;
2742 +    FDatabaseInfo.Database := Value;
2743 +    FGeneratorField.Database := Value;
2744    end;
2745   end;
2746  
# Line 2620 | Line 2762 | begin
2762    end;
2763   end;
2764  
2765 < procedure TIBCustomDataSet.SetInternalSQLParams(Qry: TIBSQL; Buffer: Pointer);
2765 > procedure TIBCustomDataSet.SetInternalSQLParams(Params: ISQLParams; Buffer: Pointer);
2766   var
2767    i, j: Integer;
2768    cr, data: PChar;
2769    fn: string;
2770    st: RawByteString;
2771    OldBuffer: Pointer;
2630  ts: TTimeStamp;
2772    Param: ISQLParam;
2773   begin
2774    if (Buffer = nil) then
# Line 2636 | Line 2777 | begin
2777      InternalPrepare;
2778    OldBuffer := nil;
2779    try
2780 <    for i := 0 to Qry.Params.GetCount - 1 do
2780 >    for i := 0 to Params.GetCount - 1 do
2781      begin
2782 <      Param := Qry.Params[i];
2782 >      Param := Params[i];
2783        fn := Param.Name;
2784        if (Pos('OLD_', fn) = 1) then {mbcs ok}
2785        begin
# Line 2702 | Line 2843 | begin
2843              end;
2844              SQL_BLOB, SQL_ARRAY, SQL_QUAD:
2845                Param.AsQuad := PISC_QUAD(data)^;
2846 <            SQL_TYPE_DATE:
2847 <            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;
2846 >            SQL_TYPE_DATE,
2847 >            SQL_TYPE_TIME,
2848              SQL_TIMESTAMP:
2849 <              Param.AsDateTime :=
2850 <                       TimeStampToDateTime(MSecsToTimeStamp(trunc(PDouble(data)^)));
2849 >            {This is an IBX native format and not the TDataset approach. See also SetFieldData}
2850 >              Param.AsDateTime := PDateTime(data)^;
2851              SQL_BOOLEAN:
2852                Param.AsBoolean := PWordBool(data)^;
2853            end;
# Line 2767 | Line 2898 | begin
2898      FQRefresh.Transaction := Value;
2899      FQSelect.Transaction := Value;
2900      FQModify.Transaction := Value;
2901 +    FGeneratorField.Transaction := Value;
2902    end;
2903   end;
2904  
# Line 3668 | Line 3800 | const
3800                 'and R.RDB$FIELD_SOURCE = F.RDB$FIELD_NAME '+ {do not localize}
3801                 'and ((not F.RDB$COMPUTED_BLR is NULL) or ' + {do not localize}
3802                 '     (not F.RDB$DEFAULT_VALUE is NULL)) '; {do not localize}
3803 +
3804 +  DefaultSQLODS12 = 'Select F.RDB$COMPUTED_BLR, ' + {do not localize}
3805 +               'F.RDB$DEFAULT_VALUE, R.RDB$FIELD_NAME, R.RDB$IDENTITY_TYPE ' + {do not localize}
3806 +               'from RDB$RELATION_FIELDS R, RDB$FIELDS F ' + {do not localize}
3807 +               'where R.RDB$RELATION_NAME = :RELATION ' +  {do not localize}
3808 +               'and R.RDB$FIELD_SOURCE = F.RDB$FIELD_NAME '+ {do not localize}
3809 +               'and ((not F.RDB$COMPUTED_BLR is NULL) or ' + {do not localize}
3810 +               '     (not F.RDB$DEFAULT_VALUE is NULL) or ' + {do not localize}
3811 +               '     ( not R.RDB$IDENTITY_TYPE is NULL))' ; {do not localize}
3812 +
3813   var
3814    FieldType: TFieldType;
3815    FieldSize: Word;
3816 <  charSetID: short;
3816 >  FieldDataSize: integer;
3817    CharSetSize: integer;
3818    CharSetName: RawByteString;
3819    FieldCodePage: TSystemCodePage;
# Line 3707 | Line 3849 | var
3849        FField.FieldName := Query.Fields[2].AsString;
3850        FField.DEFAULT_VALUE := not Query.Fields[1].IsNull;
3851        FField.COMPUTED_BLR := not Query.Fields[0].IsNull;
3852 +      FField.IDENTITY_COLUMN := (Query.FieldCount > 3) and not Query.Fields[3].IsNull;
3853        FField.NextField := Result.FieldNodes;
3854        Result.FieldNodes := FField;
3855        Query.Next;
# Line 3760 | Line 3903 | var
3903          FField := Ffield.NextField;
3904    end;
3905  
3906 +  function Is_IDENTITY_COLUMN(Relation, Field : String) : Boolean;
3907 +  var
3908 +    FRelation : TRelationNode;
3909 +    FField : TFieldNode;
3910 +  begin
3911 +    FRelation := FRelationNodes;
3912 +    while Assigned(FRelation) and
3913 +         (FRelation.RelationName <> Relation) do
3914 +      FRelation := FRelation.NextRelation;
3915 +    if not Assigned(FRelation) then
3916 +      FRelation := Add_Node(Relation, Field);
3917 +    Result := false;
3918 +    FField := FRelation.FieldNodes;
3919 +    while Assigned(FField) do
3920 +      if FField.FieldName = Field then
3921 +      begin
3922 +        Result := Ffield.IDENTITY_COLUMN;
3923 +        Exit;
3924 +      end
3925 +      else
3926 +        FField := Ffield.NextField;
3927 +  end;
3928 +
3929    Procedure FreeNodes;
3930    var
3931      FRelation : TRelationNode;
# Line 3793 | Line 3959 | begin
3959      FieldIndex := 0;
3960      if (Length(FMappedFieldPosition) < SourceQuery.MetaData.Count) then
3961        SetLength(FMappedFieldPosition, SourceQuery.MetaData.Count);
3962 <    Query.SQL.Text := DefaultSQL;
3962 >    if FDatabaseInfo.ODSMajorVersion >= 12 then
3963 >      Query.SQL.Text := DefaultSQLODS12
3964 >    else
3965 >      Query.SQL.Text := DefaultSQL;
3966      Query.Prepare;
3967      SetLength(FAliasNameMap, SourceQuery.MetaData.Count);
3968      SetLength(FAliasNameList, SourceQuery.MetaData.Count);
# Line 3807 | Line 3976 | begin
3976          FieldName := getSQLName;
3977          FAliasNameList[i] := DBAliasName;
3978          FieldSize := 0;
3979 +        FieldDataSize := GetSize;
3980          FieldPrecision := 0;
3981          FieldNullable := IsNullable;
3982          CharSetSize := 0;
# Line 3823 | Line 3993 | begin
3993                CharSetSize := 1;
3994              CharSetName := Database.Attachment.GetCharsetName(getCharSetID);
3995              Database.Attachment.CharSetID2CodePage(getCharSetID,FieldCodePage);
3996 <            FieldSize := GetSize div CharSetSize;
3996 >            FieldSize := FieldDataSize div CharSetSize;
3997              FieldType := ftString;
3998            end;
3999            { All Doubles/Floats should be cast to doubles }
# Line 3874 | Line 4044 | begin
4044                FieldSize := -getScale;
4045              end
4046              else
4047 <              FieldType := ftFloat
4047 >              FieldType := ftFloat;
4048            end;
4049            SQL_TIMESTAMP: FieldType := ftDateTime;
4050            SQL_TYPE_TIME: FieldType := ftTime;
# Line 3919 | Line 4089 | begin
4089              Name := FieldAliasName;
4090              FAliasNameMap[FieldNo-1] := DBAliasName;
4091              Size := FieldSize;
4092 +            DataSize := FieldDataSize;
4093              Precision := FieldPrecision;
4094              Required := not FieldNullable;
4095              RelationName := aRelationName;
# Line 3930 | Line 4101 | begin
4101              ArrayBounds := aArrayBounds;
4102              if (FieldName <> '') and (RelationName <> '') then
4103              begin
4104 +              IdentityColumn := Is_IDENTITY_COLUMN(RelationName, FieldName);
4105                if Has_COMPUTED_BLR(RelationName, FieldName) then
4106                begin
4107                  Attributes := [faReadOnly];
# Line 4001 | Line 4173 | begin
4173      for i := 0 to SQLParams.GetCount - 1 do
4174      begin
4175        cur_field := DataSource.DataSet.FindField(SQLParams[i].Name);
4176 <      cur_param := SQLParams[i];
4177 <      if (cur_field <> nil) then begin
4176 >      if (cur_field <> nil) then
4177 >      begin
4178 >        cur_param := SQLParams[i];
4179          if (cur_field.IsNull) then
4180            cur_param.IsNull := True
4181 <        else case cur_field.DataType of
4181 >        else
4182 >        case cur_field.DataType of
4183            ftString:
4184              cur_param.AsString := cur_field.AsString;
4185            ftBoolean:
# Line 4015 | Line 4189 | begin
4189            ftInteger:
4190              cur_param.AsLong := cur_field.AsInteger;
4191            ftLargeInt:
4192 <            cur_param.AsInt64 := TLargeIntField(cur_field).AsLargeInt;
4192 >            cur_param.AsInt64 := cur_field.AsLargeInt;
4193            ftFloat, ftCurrency:
4194             cur_param.AsDouble := cur_field.AsFloat;
4195            ftBCD:
# Line 4674 | Line 4848 | begin
4848    Transaction.StartTransaction;
4849   end;
4850  
4851 < function TIBCustomDataSet.PSGetTableName: string;
4851 > function TIBCustomDataSet.PsGetTableName: string;
4852   begin
4853   //  if not FInternalPrepared then
4854   //    InternalPrepare;
# Line 4769 | Line 4943 | end;
4943   function TIBCustomDataSet.GetFieldData(Field: TField; Buffer: Pointer;
4944    NativeFormat: Boolean): Boolean;
4945   begin
4946 <  if (Field.DataType = ftBCD) and not NativeFormat then
4946 >  {These datatypes use IBX conventions and not TDataset conventions}
4947 >  if (Field.DataType in [ftBCD,ftDateTime,ftDate,ftTime]) and not NativeFormat then
4948      Result := InternalGetFieldData(Field, Buffer)
4949    else
4950      Result := inherited GetFieldData(Field, Buffer, NativeFormat);
# Line 4795 | Line 4970 | end;
4970   procedure TIBCustomDataSet.SetFieldData(Field: TField; Buffer: Pointer;
4971    NativeFormat: Boolean);
4972   begin
4973 <  if (not NativeFormat) and (Field.DataType = ftBCD) then
4973 >  {These datatypes use IBX conventions and not TDataset conventions}
4974 >  if (not NativeFormat) and (Field.DataType in [ftBCD,ftDateTime,ftDate,ftTime]) then
4975      InternalSetfieldData(Field, Buffer)
4976    else
4977      inherited SetFieldData(Field, buffer, NativeFormat);
# Line 4815 | Line 4991 | begin
4991    inherited Destroy;
4992   end;
4993  
4994 < procedure TIBDataSetUpdateObject.SetRefreshSQL(Value: TStrings);
4994 > procedure TIBDataSetUpdateObject.SetRefreshSQL(value: TStrings);
4995   begin
4996    FRefreshSQL.Assign(Value);
4997   end;
4998  
4999 + procedure TIBDataSetUpdateObject.InternalSetParams(Params: ISQLParams;
5000 +  buff: PChar);
5001 + begin
5002 +  if not Assigned(DataSet) then Exit;
5003 +  DataSet.SetInternalSQLParams(Params, buff);
5004 + end;
5005 +
5006   procedure TIBDataSetUpdateObject.InternalSetParams(Query: TIBSQL; buff: PChar);
5007   begin
5008 +  InternalSetParams(Query.Params,buff);
5009 + end;
5010 +
5011 + procedure TIBDataSetUpdateObject.UpdateRecordFromQuery(UpdateKind: TUpdateKind;
5012 +  QryResults: IResults; Buffer: PChar);
5013 + begin
5014    if not Assigned(DataSet) then Exit;
5015 <  DataSet.SetInternalSQLParams(Query, buff);
5015 >  case UpdateKind of
5016 >  ukModify, ukInsert:
5017 >    DataSet.UpdateRecordFromQuery(QryResults, Buffer);
5018 >  ukDelete:
5019 >    DataSet.DoDeleteReturning(QryResults);
5020 >  end;
5021   end;
5022  
5023   function TIBDSBlobStream.GetSize: Int64;
# Line 4886 | Line 5080 | end;
5080  
5081   procedure TIBGenerator.SetIncrement(const AValue: integer);
5082   begin
5083 +  if FIncrement = AValue then Exit;
5084    if AValue < 0 then
5085 <     raise Exception.Create('A Generator Increment cannot be negative');
5086 <  FIncrement := AValue
5085 >    IBError(ibxeNegativeGenerator,[]);
5086 >  FIncrement := AValue;
5087 >  SetQuerySQL;
5088   end;
5089  
5090 < function TIBGenerator.GetNextValue(ADatabase: TIBDatabase;
4895 <  ATransaction: TIBTransaction): integer;
5090 > procedure TIBGenerator.SetTransaction(AValue: TIBTransaction);
5091   begin
5092 <  with TIBSQL.Create(nil) do
5093 <  try
5094 <    Database := ADatabase;
5095 <    Transaction := ATransaction;
5096 <    if not assigned(Database) then
5097 <       IBError(ibxeCannotSetDatabase,[]);
5098 <    if not assigned(Transaction) then
5099 <       IBError(ibxeCannotSetTransaction,[]);
5100 <    with Transaction do
5101 <      if not InTransaction then StartTransaction;
5102 <    SQL.Text := Format('Select Gen_ID(%s,%d) as ID From RDB$Database',[FGeneratorName,Increment]);
5103 <    Prepare;
5092 >  FQuery.Transaction := AValue;
5093 > end;
5094 >
5095 > procedure TIBGenerator.SetQuerySQL;
5096 > begin
5097 >  FQuery.SQL.Text := Format('Select Gen_ID(%s,%d) From RDB$Database',[FGeneratorName,Increment]);
5098 > end;
5099 >
5100 > function TIBGenerator.GetDatabase: TIBDatabase;
5101 > begin
5102 >  Result := FQuery.Database;
5103 > end;
5104 >
5105 > function TIBGenerator.GetTransaction: TIBTransaction;
5106 > begin
5107 >  Result := FQuery.Transaction;
5108 > end;
5109 >
5110 > procedure TIBGenerator.SetDatabase(AValue: TIBDatabase);
5111 > begin
5112 >  FQuery.Database := AValue;
5113 > end;
5114 >
5115 > procedure TIBGenerator.SetGeneratorName(AValue: string);
5116 > begin
5117 >  if FGeneratorName = AValue then Exit;
5118 >  FGeneratorName := AValue;
5119 >  SetQuerySQL;
5120 > end;
5121 >
5122 > function TIBGenerator.GetNextValue: integer;
5123 > begin
5124 >  with FQuery do
5125 >  begin
5126 >    Transaction.Active := true;
5127      ExecQuery;
5128      try
5129 <      Result := FieldByName('ID').AsInteger
5129 >      Result := Fields[0].AsInteger
5130      finally
5131        Close
5132      end;
4915  finally
4916    Free
5133    end;
5134   end;
5135  
# Line 4921 | Line 5137 | constructor TIBGenerator.Create(Owner: T
5137   begin
5138    FOwner := Owner;
5139    FIncrement := 1;
5140 +  FQuery := TIBSQL.Create(nil);
5141 + end;
5142 +
5143 + destructor TIBGenerator.Destroy;
5144 + begin
5145 +  if assigned(FQuery) then FQuery.Free;
5146 +  inherited Destroy;
5147   end;
5148  
5149  
5150   procedure TIBGenerator.Apply;
5151   begin
5152 <  if (FGeneratorName <> '') and (FFieldName <> '') and Owner.FieldByName(FFieldName).IsNull then
5153 <    Owner.FieldByName(FFieldName).AsInteger := GetNextValue(Owner.Database,Owner.Transaction);
5152 >  if assigned(Database) and assigned(Transaction) and
5153 >       (FGeneratorName <> '') and (FFieldName <> '') and Owner.FieldByName(FFieldName).IsNull then
5154 >    Owner.FieldByName(FFieldName).AsInteger := GetNextValue;
5155   end;
5156  
5157  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines