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 37 by tony, Mon Feb 15 14:44:25 2016 UTC vs.
Revision 67 by tony, Tue Oct 3 14:08:11 2017 UTC

# Line 35 | Line 35 | unit IBCustomDataSet;
35  
36   {$R-}
37  
38 + {$IFDEF FPC}
39   {$Mode Delphi}
40 + {$codepage UTF8}
41 + {$ENDIF}
42  
43   {$IFDEF DELPHI}
44   {$DEFINE TDBDFIELD_IS_BCD}
# Line 49 | Line 52 | uses
52   {$ELSE}
53    unix,
54   {$ENDIF}
55 <  SysUtils, Classes, IBDatabase, IBExternals, IB, IBHeader,  IBSQL, Db,
55 >  SysUtils, Classes, IBDatabase, IBExternals, IB,  IBSQL, Db,
56    IBUtils, IBBlob, IBSQLParser;
57  
58   const
# Line 82 | Line 85 | type
85  
86    TBlobDataArray = array[0..0] of TIBBlobStream;
87    PBlobDataArray = ^TBlobDataArray;
88 +  TIBArrayField = class;
89  
90 <  { TIBCustomDataSet }
91 <  TFieldData = record
92 <    fdDataType: Short;
93 <    fdDataScale: Short;
94 <    fdNullable: Boolean;
95 <    fdIsNull: Boolean;
96 <    fdDataSize: Short;
97 <    fdDataLength: Short;
98 <    fdDataOfs: Integer;
90 >  { TIBArray }
91 >
92 >  {Wrapper class to support array cache in TIBCustomDataset and event handling}
93 >
94 >  TIBArray = class
95 >  private
96 >    FArray: IArray;
97 >    FRecNo: integer;
98 >    FField: TIBArrayField;
99 >    procedure EventHandler(Sender: IArray; Reason: TArrayEventReason);
100 >  public
101 >    constructor Create(aField: TIBArrayField; anArray: IArray);
102 >    destructor Destroy; override;
103 >    property ArrayIntf: IArray read FArray;
104    end;
105 <  PFieldData = ^TFieldData;
105 >
106 >  TArrayDataArray = array [0..0] of TIBArray;
107 >  PArrayDataArray = ^TArrayDataArray;
108 >
109 >  { TIBCustomDataSet }
110  
111    TCachedUpdateStatus = (
112                           cusUnmodified, cusModified, cusInserted,
# Line 104 | Line 117 | type
117    end;
118    PIBDBKey = ^TIBDBKey;
119  
120 +  PFieldData = ^TFieldData;
121 +  TFieldData = record
122 +   fdIsNull: Boolean;
123 +   fdDataLength: Short;
124 + end;
125 +
126 + PColumnData = ^TColumnData;
127 + TColumnData = record
128 +  fdDataType: Short;
129 +  fdDataScale: Short;
130 +  fdNullable: Boolean;
131 +  fdDataSize: Short;
132 +  fdDataOfs: Integer;
133 +  fdCodePage: TSystemCodePage;
134 + end;
135 +
136 + PFieldColumns = ^TFieldColumns;
137 + TFieldColumns =  array[1..1] of TColumnData;
138 +
139    TRecordData = record
140      rdBookmarkFlag: TBookmarkFlag;
141      rdFieldCount: Short;
# Line 116 | Line 148 | type
148    end;
149    PRecordData = ^TRecordData;
150  
151 +  { TIBArrayField }
152 +
153 +  TIBArrayField = class(TField)
154 +  private
155 +    FArrayBounds: TArrayBounds;
156 +    FArrayDimensions: integer;
157 +    FRelationName: string;
158 +    FCacheOffset: word;
159 +    function GetArrayID: TISC_QUAD;
160 +    function GetArrayIntf: IArray;
161 +    procedure SetArrayIntf(AValue: IArray);
162 +  protected
163 +    class procedure CheckTypeSize(AValue: Longint); override;
164 +    function GetAsString: string; override;
165 +    function GetDataSize: Integer; override;
166 +    procedure Bind(Binding: Boolean); override;
167 +  public
168 +    constructor Create(AOwner: TComponent); override;
169 +    function CreateArray: IArray;
170 +    property ArrayID: TISC_QUAD read GetArrayID;
171 +    property ArrayIntf: IArray read GetArrayIntf write SetArrayIntf;
172 +    property ArrayDimensions: integer read FArrayDimensions write FArrayDimensions;
173 +    property ArrayBounds: TArrayBounds read FArrayBounds write FArrayBounds;
174 +  end;
175 +
176    { TIBStringField allows us to have strings longer than 8196 }
177  
178    TIBStringField = class(TStringField)
179    private
180 <    FCharacterSetName: string;
180 >    FCharacterSetName: RawByteString;
181      FCharacterSetSize: integer;
182 +    FAutoFieldSize: boolean;
183 +    FCodePage: TSystemCodePage;
184 +    FDataSize: integer;
185    protected
186 <    function GetDefaultWidth: Longint; override;
186 >    procedure Bind(Binding: Boolean); override;
187 >    function GetDataSize: Integer; override;
188    public
189      constructor Create(aOwner: TComponent); override;
190      class procedure CheckTypeSize(Value: Integer); override;
# Line 131 | Line 192 | type
192      function GetAsVariant: Variant; override;
193      function GetValue(var Value: string): Boolean;
194      procedure SetAsString(const Value: string); override;
195 <    property CharacterSetName: string read FCharacterSetName write FCharacterSetName;
135 <    property CharacterSetSize: integer read FCharacterSetSize write FCharacterSetSize;
136 <  end;
137 <
138 <  { TIBWideStringField }
139 <
140 <  TIBWideStringField = class(TWideStringField)
141 <  private
142 <    FCharacterSetName: string;
143 <    FCharacterSetSize: integer;
144 <  public
145 <    property CharacterSetName: string read FCharacterSetName write FCharacterSetName;
195 >    property CharacterSetName: RawByteString read FCharacterSetName write FCharacterSetName;
196      property CharacterSetSize: integer read FCharacterSetSize write FCharacterSetSize;
197 +    property CodePage: TSystemCodePage read FCodePage write FCodePage;
198 +  published
199 +    property AutoFieldSize: boolean read FAutoFieldSize write FAutoFieldSize default true;
200    end;
201  
202    { TIBBCDField }
# Line 172 | Line 225 | type
225  
226     TIBMemoField = class(TMemoField)
227     private
228 <     FCharacterSetName: string;
228 >     FCharacterSetName: RawByteString;
229       FCharacterSetSize: integer;
230       FDisplayTextAsClassName: boolean;
231       function GetTruncatedText: string;
232     protected
233 +     procedure Bind(Binding: Boolean); override;
234 +     function GetAsString: string; override;
235       function GetDefaultWidth: Longint; override;
236       procedure GetText(var AText: string; ADisplayText: Boolean); override;
237 +     procedure SetAsString(const AValue: string); override;
238     public
239       constructor Create(AOwner: TComponent); override;
240 <     property CharacterSetName: string read FCharacterSetName write FCharacterSetName;
240 >     property CharacterSetName: RawByteString read FCharacterSetName write FCharacterSetName;
241       property CharacterSetSize: integer read FCharacterSetSize write FCharacterSetSize;
242 <  published
242 >   published
243       property DisplayTextAsClassName: boolean read FDisplayTextAsClassName
244                                              write FDisplayTextAsClassName;
189   end;
190
191   { TIBWideMemoField }
192
193   TIBWideMemoField = class(TWideMemoField)
245     private
246 <     FCharacterSetName: string;
247 <     FCharacterSetSize: integer;
197 <     FDisplayTextAsClassName: boolean;
198 <     function GetTruncatedText: string;
199 <   protected
200 <     function GetDefaultWidth: Longint; override;
201 <     procedure GetText(var AText: string; ADisplayText: Boolean); override;
246 >     FCodePage: TSystemCodePage;
247 >     FFCodePage: TSystemCodePage;
248     public
249 <     constructor Create(AOwner: TComponent); override;
204 <     property CharacterSetName: string read FCharacterSetName write FCharacterSetName;
205 <     property CharacterSetSize: integer read FCharacterSetSize write FCharacterSetSize;
206 <   published
207 <      property DisplayTextAsClassName: boolean read FDisplayTextAsClassName
208 <                                             write FDisplayTextAsClassName;
249 >     property CodePage: TSystemCodePage read FFCodePage write FFCodePage;
250     end;
251  
252    TIBDataLink = class(TDetailDataLink)
# Line 263 | Line 304 | type
304    TIBAutoCommit = (acDisabled, acCommitRetaining);
305  
306    { TIBCustomDataSet }
307 +
308    TIBUpdateAction = (uaFail, uaAbort, uaSkip, uaRetry, uaApply, uaApplied);
309  
310    TIBUpdateErrorEvent = procedure(DataSet: TDataSet; E: EDatabaseError;
# Line 279 | Line 321 | type
321  
322    TIBCustomDataSet = class(TDataset)
323    private
324 +    FAllowAutoActivateTransaction: Boolean;
325 +    FArrayFieldCount: integer;
326 +    FArrayCacheOffset: integer;
327      FAutoCommit: TIBAutoCommit;
328      FGenerateParamNames: Boolean;
329      FGeneratorField: TIBGenerator;
330      FNeedsRefresh: Boolean;
331      FForcedRefresh: Boolean;
332      FDidActivate: Boolean;
288    FIBLoaded: Boolean;
333      FBase: TIBBase;
334      FBlobCacheOffset: Integer;
335      FBlobStreamList: TList;
336 +    FArrayList: TList;
337      FBufferChunks: Integer;
338      FBufferCache,
339      FOldBufferCache: PChar;
# Line 342 | Line 387 | type
387      FCloseAction: TTransactionAction;
388      FInTransactionEnd: boolean;
389      FIBLinks: TList;
390 <    function GetSelectStmtHandle: TISC_STMT_HANDLE;
390 >    FFieldColumns: PFieldColumns;
391 >    procedure InitModelBuffer(Qry: TIBSQL; Buffer: PChar);
392 >    function GetSelectStmtIntf: IStatement;
393      procedure SetUpdateMode(const Value: TUpdateMode);
394      procedure SetUpdateObject(Value: TIBDataSetUpdateObject);
395  
# Line 354 | Line 401 | type
401      function CanRefresh: Boolean;
402      procedure CheckEditState;
403      procedure ClearBlobCache;
404 +    procedure ClearArrayCache;
405      procedure ClearIBLinks;
406      procedure CopyRecordBuffer(Source, Dest: Pointer);
407      procedure DoBeforeDatabaseDisconnect(Sender: TObject);
# Line 365 | Line 413 | type
413      procedure FetchCurrentRecordToBuffer(Qry: TIBSQL; RecordNumber: Integer;
414                                           Buffer: PChar);
415      function GetDatabase: TIBDatabase;
368    function GetDBHandle: PISC_DB_HANDLE;
416      function GetDeleteSQL: TStrings;
417      function GetInsertSQL: TStrings;
418 <    function GetSQLParams: TIBXSQLDA;
418 >    function GetSQLParams: ISQLParams;
419      function GetRefreshSQL: TStrings;
420      function GetSelectSQL: TStrings;
421 <    function GetStatementType: TIBSQLTypes;
421 >    function GetStatementType: TIBSQLStatementTypes;
422      function GetModifySQL: TStrings;
423      function GetTransaction: TIBTransaction;
377    function GetTRHandle: PISC_TR_HANDLE;
424      function GetParser: TSelectSQLParser;
425      procedure InternalDeleteRecord(Qry: TIBSQL; Buff: Pointer); virtual;
426      function InternalLocate(const KeyFields: string; const KeyValues: Variant;
# Line 429 | Line 475 | type
475      procedure SQLChanging(Sender: TObject); virtual;
476      procedure SQLChanged(Sender: TObject); virtual;
477  
478 < (*    { IProviderSupport }
478 >    { IProviderSupport }
479      procedure PSEndTransaction(Commit: Boolean); override;
480      function PSExecuteStatement(const ASQL: string; AParams: TParams;
481        ResultSet: Pointer = nil): Integer; override;
# Line 442 | Line 488 | type
488      procedure PSStartTransaction; override;
489      procedure PSReset; override;
490      function PSUpdateRecord(UpdateKind: TUpdateKind; Delta: TDataSet): Boolean; override;
491 < *)
491 >
492      { TDataSet support }
493      procedure InternalInsert; override;
494      procedure InitRecord(Buffer: PChar); override;
# Line 506 | Line 552 | type
552    protected
553      {Likely to be made public by descendant classes}
554      property AutoCommit: TIBAutoCommit read FAutoCommit write FAutoCommit default acDisabled;
555 <    property SQLParams: TIBXSQLDA read GetSQLParams;
556 <    property Params: TIBXSQLDA read GetSQLParams;
555 >    property SQLParams: ISQLParams read GetSQLParams;
556 >    property Params: ISQLParams read GetSQLParams;
557      property InternalPrepared: Boolean read FInternalPrepared;
558      property QDelete: TIBSQL read FQDelete;
559      property QInsert: TIBSQL read FQInsert;
560      property QRefresh: TIBSQL read FQRefresh;
561      property QSelect: TIBSQL read FQSelect;
562      property QModify: TIBSQL read FQModify;
563 <    property StatementType: TIBSQLTypes read GetStatementType;
564 <    property SelectStmtHandle: TISC_STMT_HANDLE read GetSelectStmtHandle;
563 >    property StatementType: TIBSQLStatementTypes read GetStatementType;
564 >    property SelectStmtHandle: IStatement read GetSelectStmtIntf;
565  
566      {Likely to be made published by descendant classes}
567      property BufferChunks: Integer read FBufferChunks write SetBufferChunks;
# Line 566 | Line 612 | type
612      function BookmarkValid(Bookmark: TBookmark): Boolean; override;
613      function CompareBookmarks(Bookmark1, Bookmark2: TBookmark): Integer; override;
614      function CreateBlobStream(Field: TField; Mode: TBlobStreamMode): TStream; override;
615 +    function GetArray(Field: TIBArrayField): IArray;
616 +    procedure SetArrayIntf(AnArray: IArray; Field: TIBArrayField);
617      function GetCurrentRecord(Buffer: PChar): Boolean; override;
618      function GetFieldData(Field : TField; Buffer : Pointer) : Boolean; overload; override;
619      function GetFieldData(FieldNo: Integer; Buffer: Pointer): Boolean; overload; (*override;*)
# Line 579 | Line 627 | type
627      function UpdateStatus: TUpdateStatus; override;
628      function IsSequenced: Boolean; override;
629      procedure Post; override;
630 <    function ParamByName(ParamName: String): TIBXSQLVAR;
631 <    property DBHandle: PISC_DB_HANDLE read GetDBHandle;
584 <    property TRHandle: PISC_TR_HANDLE read GetTRHandle;
630 >    function ParamByName(ParamName: String): ISQLParam;
631 >    property ArrayFieldCount: integer read FArrayFieldCount;
632      property UpdateObject: TIBDataSetUpdateObject read FUpdateObject write SetUpdateObject;
633      property UpdatesPending: Boolean read FUpdatesPending;
634      property UpdateRecordTypes: TIBUpdateRecordTypes read FUpdateRecordTypes
# Line 590 | Line 637 | type
637                 read FDataSetCloseAction write FDataSetCloseAction;
638  
639    published
640 +    property AllowAutoActivateTransaction: Boolean read FAllowAutoActivateTransaction
641 +                 write FAllowAutoActivateTransaction;
642      property Database: TIBDatabase read GetDatabase write SetDatabase;
643      property Transaction: TIBTransaction read GetTransaction
644                                            write SetTransaction;
# Line 719 | Line 768 | type
768    protected
769      FField: TField;
770      FBlobStream: TIBBlobStream;
771 +    function  GetSize: Int64; override;
772    public
773      constructor Create(AField: TField; ABlobStream: TIBBlobStream;
774                         Mode: TBlobStreamMode);
# Line 729 | Line 779 | type
779      function Write(const Buffer; Count: Longint): Longint; override;
780    end;
781  
782 +  {Extended Field Def for character set info}
783 +
784 +  { TIBFieldDef }
785 +
786 +  TIBFieldDef = class(TFieldDef)
787 +  private
788 +    FArrayBounds: TArrayBounds;
789 +    FArrayDimensions: integer;
790 +    FCharacterSetName: RawByteString;
791 +    FCharacterSetSize: integer;
792 +    FCodePage: TSystemCodePage;
793 +    FRelationName: string;
794 +    FDataSize: integer;
795 +  published
796 +    property CharacterSetName: RawByteString read FCharacterSetName write FCharacterSetName;
797 +    property CharacterSetSize: integer read FCharacterSetSize write FCharacterSetSize;
798 +    property CodePage: TSystemCodePage read FCodePage write FCodePage;
799 +    property DataSize: integer read FDataSize write FDataSize;
800 +    property RelationName: string read FRelationName write FRelationName;
801 +    property ArrayDimensions: integer read FArrayDimensions write FArrayDimensions;
802 +    property ArrayBounds: TArrayBounds read FArrayBounds write FArrayBounds;
803 +  end;
804 +
805   const
806   DefaultFieldClasses: array[TFieldType] of TFieldClass = (
807      nil,                { ftUnknown }
# Line 755 | Line 828 | DefaultFieldClasses: array[TFieldType] o
828      TBlobField,         { ftTypedBinary }
829      nil,                { ftCursor }
830      TStringField,       { ftFixedChar }
831 <    TIBWideStringField,    { ftWideString }
831 >    nil,    { ftWideString }
832      TLargeIntField,     { ftLargeInt }
833      nil,          { ftADT }
834 <    nil,        { ftArray }
834 >    TIBArrayField,        { ftArray }
835      nil,    { ftReference }
836      nil,     { ftDataSet }
837      TBlobField,         { ftOraBlob }
# Line 770 | Line 843 | DefaultFieldClasses: array[TFieldType] o
843      TDateTimeField,    {ftTimestamp}
844      TIBBCDField,       {ftFMTBcd}
845      nil,  {ftFixedWideChar}
846 <    TIBWideMemoField);   {ftWideMemo}
846 >    nil);   {ftWideMemo}
847   (*
848      TADTField,          { ftADT }
849      TArrayField,        { ftArray }
# Line 787 | Line 860 | DefaultFieldClasses: array[TFieldType] o
860  
861   implementation
862  
863 < uses IBIntf, Variants, FmtBCD, LazUTF8;
863 > uses Variants, FmtBCD, LazUTF8, FBMessages, IBQuery;
864  
865   const FILE_BEGIN = 0;
866        FILE_CURRENT = 1;
# Line 810 | Line 883 | type
883      NextRelation : TRelationNode;
884    end;
885  
813  {Extended Field Def for character set info}
814
815  { TIBFieldDef }
816
817  TIBFieldDef = class(TFieldDef)
818  private
819    FCharacterSetName: string;
820    FCharacterSetSize: integer;
821  published
822    property CharacterSetName: string read FCharacterSetName write FCharacterSetName;
823    property CharacterSetSize: integer read FCharacterSetSize write FCharacterSetSize;
824  end;
825
886  
887    {  Copied from LCLProc in order to avoid LCL dependency
888  
# Line 865 | Line 925 | type
925      Result := str;
926    end;
927  
928 < { TIBWideMemoField }
928 > { TIBArray }
929  
930 < function TIBWideMemoField.GetTruncatedText: string;
930 > procedure TIBArray.EventHandler(Sender: IArray; Reason: TArrayEventReason);
931   begin
932 <  Result := GetAsString;
932 >  case Reason of
933 >  arChanging:
934 >    if FRecNo <> FField.Dataset.RecNo then
935 >      IBError(ibxeNotCurrentArray,[nil]);
936  
937 <  if Result <> '' then
938 <    if DisplayWidth = 0 then
939 <      Result := TextToSingleLine(Result)
940 <    else
941 <    if Length(Result) > DisplayWidth then {Show truncation with elipses}
942 <      Result := TextToSingleLine(system.copy(Result,1,DisplayWidth-3)) + '...';
937 >  arChanged:
938 >    FField.DataChanged;
939 >  end;
940 > end;
941 >
942 > constructor TIBArray.Create(aField: TIBArrayField; anArray: IArray);
943 > begin
944 >  inherited Create;
945 >  FField := aField;
946 >  FArray := anArray;
947 >  FRecNo := FField.Dataset.RecNo;
948 >  FArray.AddEventHandler(EventHandler);
949   end;
950  
951 < function TIBWideMemoField.GetDefaultWidth: Longint;
951 > destructor TIBArray.Destroy;
952   begin
953 <  Result := 128;
953 >  FArray.RemoveEventHandler(EventHandler);
954 >  inherited Destroy;
955   end;
956  
957 < procedure TIBWideMemoField.GetText(var AText: string; ADisplayText: Boolean);
957 > { TIBArrayField }
958 >
959 > function TIBArrayField.GetArrayIntf: IArray;
960   begin
961 <  if ADisplayText then
961 >  Result := TIBCustomDataSet(DataSet).GetArray(self);
962 > end;
963 >
964 > function TIBArrayField.GetArrayID: TISC_QUAD;
965 > begin
966 >  GetData(@Result);
967 > end;
968 >
969 > procedure TIBArrayField.SetArrayIntf(AValue: IArray);
970 > begin
971 >  TIBCustomDataSet(DataSet).SetArrayIntf(AValue,self);
972 >  DataChanged;
973 > end;
974 >
975 > class procedure TIBArrayField.CheckTypeSize(AValue: Longint);
976 > begin
977 >  //Ignore
978 > end;
979 >
980 > function TIBArrayField.GetAsString: string;
981 > begin
982 >  Result := '(Array)';
983 > end;
984 >
985 > function TIBArrayField.GetDataSize: Integer;
986 > begin
987 >  Result := sizeof(TISC_QUAD);
988 > end;
989 >
990 > procedure TIBArrayField.Bind(Binding: Boolean);
991 > begin
992 >  inherited Bind(Binding);
993 >  if Binding then
994    begin
995 <    if not DisplayTextAsClassName then
996 <      AText := GetTruncatedText
997 <    else
998 <      inherited GetText(AText, ADisplayText);
999 <  end
1000 <  else
1001 <    AText := GetAsString;
995 >    FCacheOffset := TIBCustomDataSet(DataSet).ArrayFieldCount;
996 >    Inc(TIBCustomDataSet(DataSet).FArrayFieldCount);
997 >    if FieldDef <> nil then
998 >    begin
999 >      FRelationName := TIBFieldDef(FieldDef).FRelationName;
1000 >      FArrayDimensions := TIBFieldDef(FieldDef).ArrayDimensions;
1001 >      FArrayBounds :=  TIBFieldDef(FieldDef).ArrayBounds;
1002 >    end;
1003 >  end;
1004   end;
1005  
1006 < constructor TIBWideMemoField.Create(AOwner: TComponent);
1006 > constructor TIBArrayField.Create(AOwner: TComponent);
1007   begin
1008    inherited Create(AOwner);
1009 <  BlobType := ftWideMemo;
1009 >  SetDataType(ftArray);
1010 > end;
1011 >
1012 > function TIBArrayField.CreateArray: IArray;
1013 > begin
1014 > with DataSet as TIBCustomDataSet do
1015 >  Result := Database.Attachment.CreateArray(Transaction.TransactionIntf,FRelationName,FieldName);
1016   end;
1017  
1018   { TIBMemoField }
# Line 932 | Line 1044 | begin
1044     end
1045   end;
1046  
1047 + procedure TIBMemoField.Bind(Binding: Boolean);
1048 + var IBFieldDef: TIBFieldDef;
1049 + begin
1050 +  inherited Bind(Binding);
1051 +  if Binding and (FieldDef <> nil) then
1052 +  begin
1053 +    IBFieldDef := FieldDef as TIBFieldDef;
1054 +    CharacterSetSize := IBFieldDef.CharacterSetSize;
1055 +    CharacterSetName := IBFieldDef.CharacterSetName;
1056 +    CodePage := IBFieldDef.CodePage;
1057 +  end;
1058 + end;
1059 +
1060 + function TIBMemoField.GetAsString: string;
1061 + var s: RawByteString;
1062 + begin
1063 +  s := inherited GetAsString;
1064 +  SetCodePage(s,CodePage,false);
1065 +  if (CodePage <> CP_NONE) and (CodePage <> CP_UTF8) then
1066 +    SetCodePage(s,CP_UTF8,true);  {LCL only accepts UTF8}
1067 +  Result := s;
1068 + end;
1069 +
1070   function TIBMemoField.GetDefaultWidth: Longint;
1071   begin
1072    if DisplayTextAsClassName then
# Line 944 | Line 1079 | procedure TIBMemoField.GetText(var AText
1079   begin
1080    if ADisplayText then
1081    begin
1082 <    if not DisplayTextAsClassName then
1082 >    if not DisplayTextAsClassName and (CharacterSetName <> '') then
1083        AText := GetTruncatedText
1084      else
1085        inherited GetText(AText, ADisplayText);
# Line 953 | Line 1088 | begin
1088      AText := GetAsString;
1089   end;
1090  
1091 + procedure TIBMemoField.SetAsString(const AValue: string);
1092 + var s: RawByteString;
1093 + begin
1094 +  s := AValue;
1095 +  if StringCodePage(s) <> CodePage then
1096 +    SetCodePage(s,CodePage,CodePage<>CP_NONE);
1097 +  inherited SetAsString(s);
1098 + end;
1099 +
1100   constructor TIBMemoField.Create(AOwner: TComponent);
1101   begin
1102    inherited Create(AOwner);
1103    BlobType := ftMemo;
1104 +  FCodePage := CP_NONE;
1105   end;
1106  
1107   { TIBControlLink }
# Line 990 | Line 1135 | end;
1135  
1136   { TIBStringField}
1137  
1138 < function TIBStringField.GetDefaultWidth: Longint;
1138 > procedure TIBStringField.Bind(Binding: Boolean);
1139 > var IBFieldDef: TIBFieldDef;
1140   begin
1141 <  Result := Size div CharacterSetSize;
1141 >  inherited Bind(Binding);
1142 >  if Binding and (FieldDef <> nil) then
1143 >  begin
1144 >    IBFieldDef := FieldDef as TIBFieldDef;
1145 >    CharacterSetSize := IBFieldDef.CharacterSetSize;
1146 >    CharacterSetName := IBFieldDef.CharacterSetName;
1147 >    FDataSize := IBFieldDef.DataSize + 1;
1148 >    if AutoFieldSize then
1149 >      Size := IBFieldDef.Size;
1150 >    CodePage := IBFieldDef.CodePage;
1151 >  end;
1152 > end;
1153 >
1154 > function TIBStringField.GetDataSize: Integer;
1155 > begin
1156 >  Result := FDataSize;
1157   end;
1158  
1159   constructor TIBStringField.Create(aOwner: TComponent);
1160   begin
1161    inherited Create(aOwner);
1162    FCharacterSetSize := 1;
1163 +  FCodePage := CP_NONE;
1164 +  FAutoFieldSize := true;
1165   end;
1166  
1167   class procedure TIBStringField.CheckTypeSize(Value: Integer);
# Line 1021 | Line 1184 | end;
1184   function TIBStringField.GetValue(var Value: string): Boolean;
1185   var
1186    Buffer: PChar;
1187 +  s: RawByteString;
1188   begin
1189    Buffer := nil;
1190 <  IBAlloc(Buffer, 0, Size + 1);
1190 >  IBAlloc(Buffer, 0, DataSize);
1191    try
1192      Result := GetData(Buffer);
1193      if Result then
1194      begin
1195 <      Value := string(Buffer);
1195 >      s := strpas(Buffer);
1196 >      SetCodePage(s,CodePage,false);
1197 >      if (CodePage <> CP_NONE) and (CodePage <> CP_UTF8) then
1198 >        SetCodePage(s,CP_UTF8,true);  {LCL only accepts UTF8}
1199 >      Value := s;
1200 > //      writeln(FieldName,': ', StringCodePage(Value),', ',Value);
1201        if Transliterate and (Value <> '') then
1202          DataSet.Translate(PChar(Value), PChar(Value), False);
1203      end
# Line 1040 | Line 1209 | end;
1209   procedure TIBStringField.SetAsString(const Value: string);
1210   var
1211    Buffer: PChar;
1212 +  s: RawByteString;
1213   begin
1214    Buffer := nil;
1215 <  IBAlloc(Buffer, 0, Size + 1);
1215 >  IBAlloc(Buffer, 0, DataSize);
1216    try
1217 <    StrLCopy(Buffer, PChar(Value), Size);
1217 >    s := Value;
1218 >    if StringCodePage(s) <> CodePage then
1219 >      SetCodePage(s,CodePage,CodePage<>CP_NONE);
1220 >    StrLCopy(Buffer, PChar(s), DataSize-1);
1221      if Transliterate then
1222        DataSet.Translate(Buffer, Buffer, True);
1223      SetData(Buffer);
# Line 1147 | Line 1320 | end;
1320   constructor TIBCustomDataSet.Create(AOwner: TComponent);
1321   begin
1322    inherited Create(AOwner);
1150  FIBLoaded := False;
1151  CheckIBLoaded;
1152  FIBLoaded := True;
1323    FBase := TIBBase.Create(Self);
1324    FIBLinks := TList.Create;
1325    FCurrentRecord := -1;
# Line 1157 | Line 1327 | begin
1327    FUniDirectional := False;
1328    FBufferChunks := BufferCacheSize;
1329    FBlobStreamList := TList.Create;
1330 +  FArrayList := TList.Create;
1331    FGeneratorField := TIBGenerator.Create(self);
1332    FDataLink := TIBDataLink.Create(Self);
1333    FQDelete := TIBSQL.Create(Self);
# Line 1200 | Line 1371 | end;
1371   destructor TIBCustomDataSet.Destroy;
1372   begin
1373    if Active then Active := false;
1374 <  if FIBLoaded then
1375 <  begin
1376 <    if assigned(FGeneratorField) then FGeneratorField.Free;
1377 <    FDataLink.Free;
1378 <    FBase.Free;
1379 <    ClearBlobCache;
1380 <    ClearIBLinks;
1381 <    FIBLinks.Free;
1382 <    FBlobStreamList.Free;
1383 <    FreeMem(FBufferCache);
1384 <    FBufferCache := nil;
1385 <    FreeMem(FOldBufferCache);
1386 <    FOldBufferCache := nil;
1387 <    FCacheSize := 0;
1388 <    FOldCacheSize := 0;
1218 <    FMappedFieldPosition := nil;
1219 <  end;
1374 >  if assigned(FGeneratorField) then FGeneratorField.Free;
1375 >  FDataLink.Free;
1376 >  FBase.Free;
1377 >  ClearBlobCache;
1378 >  ClearIBLinks;
1379 >  FIBLinks.Free;
1380 >  FBlobStreamList.Free;
1381 >  FArrayList.Free;
1382 >  FreeMem(FBufferCache);
1383 >  FBufferCache := nil;
1384 >  FreeMem(FOldBufferCache);
1385 >  FOldBufferCache := nil;
1386 >  FCacheSize := 0;
1387 >  FOldCacheSize := 0;
1388 >  FMappedFieldPosition := nil;
1389    if assigned(FBaseSQLSelect) then FBaseSQLSelect.Free;
1390    if assigned(FParser) then FParser.Free;
1391    inherited Destroy;
# Line 1241 | Line 1410 | begin
1410        Inc(FCurrentRecord);
1411        if (FCurrentRecord = FRecordCount) then
1412        begin
1413 <        if (not FQSelect.EOF) and (FQSelect.Next <> nil) then
1413 >        if (not FQSelect.EOF) and FQSelect.Next  then
1414          begin
1415            FetchCurrentRecordToBuffer(FQSelect, FCurrentRecord, Buffer);
1416            Inc(FRecordCount);
# Line 1260 | Line 1429 | end;
1429  
1430   procedure TIBCustomDataSet.ApplyUpdates;
1431   var
1263  {$IF FPC_FULLVERSION >= 20700 }
1432    CurBookmark: TBookmark;
1265  {$ELSE}
1266  CurBookmark: string;
1267  {$ENDIF}
1433    Buffer: PRecordData;
1434    CurUpdateTypes: TIBUpdateRecordTypes;
1435    UpdateAction: TIBUpdateAction;
# Line 1495 | Line 1660 | end;
1660   function TIBCustomDataSet.ActivateTransaction: Boolean;
1661   begin
1662    Result := False;
1663 <  if not Assigned(Transaction) then
1499 <    IBError(ibxeTransactionNotAssigned, [nil]);
1500 <  if not Transaction.Active then
1663 >  if AllowAutoActivateTransaction or (csDesigning in ComponentState) then
1664    begin
1665 <    Result := True;
1666 <    Transaction.StartTransaction;
1667 <    FDidActivate := True;
1665 >    if not Assigned(Transaction) then
1666 >      IBError(ibxeTransactionNotAssigned, [nil]);
1667 >    if not Transaction.Active then
1668 >    begin
1669 >      Result := True;
1670 >      Transaction.StartTransaction;
1671 >      FDidActivate := True;
1672 >    end;
1673    end;
1674   end;
1675  
# Line 1626 | Line 1794 | begin
1794    FBlobStreamList.Pack;
1795   end;
1796  
1797 + procedure TIBCustomDataSet.ClearArrayCache;
1798 + var
1799 +  i: Integer;
1800 + begin
1801 +  for i := 0 to FArrayList.Count - 1 do
1802 +  begin
1803 +    TIBArray(FArrayList[i]).Free;
1804 +    FArrayList[i] := nil;
1805 +  end;
1806 +  FArrayList.Pack;
1807 + end;
1808 +
1809   procedure TIBCustomDataSet.CopyRecordBuffer(Source, Dest: Pointer);
1810   begin
1811    Move(Source^, Dest^, FRecordBufferSize);
# Line 1635 | Line 1815 | procedure TIBCustomDataSet.DoBeforeDatab
1815   begin
1816    if Active then
1817      Active := False;
1818 <  FInternalPrepared := False;
1818 >  InternalUnPrepare;
1819    if Assigned(FBeforeDatabaseDisconnect) then
1820      FBeforeDatabaseDisconnect(Sender);
1821   end;
# Line 1673 | Line 1853 | begin
1853      FQModify.FreeHandle;
1854    if FQRefresh <> nil then
1855      FQRefresh.FreeHandle;
1856 +  InternalUnPrepare;
1857    if Assigned(FBeforeTransactionEnd) then
1858      FBeforeTransactionEnd(Sender);
1859   end;
# Line 1689 | Line 1870 | begin
1870      FTransactionFree(Sender);
1871   end;
1872  
1873 + procedure TIBCustomDataSet.InitModelBuffer(Qry: TIBSQL; Buffer: PChar);
1874 + var i, j: Integer;
1875 +    FieldsLoaded: integer;
1876 +    p: PRecordData;
1877 +    colMetadata: IColumnMetaData;
1878 + begin
1879 +  p := PRecordData(Buffer);
1880 +  { Get record information }
1881 +  p^.rdBookmarkFlag := bfCurrent;
1882 +  p^.rdFieldCount := Qry.FieldCount;
1883 +  p^.rdRecordNumber := -1;
1884 +  p^.rdUpdateStatus := usUnmodified;
1885 +  p^.rdCachedUpdateStatus := cusUnmodified;
1886 +  p^.rdSavedOffset := $FFFFFFFF;
1887 +
1888 +  { Load up the fields }
1889 +  FieldsLoaded := FQSelect.MetaData.Count;
1890 +  j := 1;
1891 +  for i := 0 to Qry.MetaData.Count - 1 do
1892 +  begin
1893 +    if (Qry = FQSelect) then
1894 +      j := i + 1
1895 +    else
1896 +    begin
1897 +      if FieldsLoaded = 0 then
1898 +        break;
1899 +      j := FQSelect.FieldIndex[Qry[i].Name] + 1;
1900 +      if j < 1 then
1901 +        continue
1902 +      else
1903 +        Dec(FieldsLoaded);
1904 +    end;
1905 +    if j > 0 then
1906 +    begin
1907 +      colMetadata := Qry.MetaData[i];
1908 +      with p^.rdFields[j], FFieldColumns^[j] do
1909 +      begin
1910 +        fdDataType := colMetadata.GetSQLType;
1911 +        if fdDataType = SQL_BLOB then
1912 +          fdDataScale := 0
1913 +        else
1914 +          fdDataScale := colMetadata.getScale;
1915 +        fdNullable := colMetadata.getIsNullable;
1916 +        fdIsNull := true;
1917 +        fdDataSize := colMetadata.GetSize;
1918 +        fdDataLength := 0;
1919 +        fdCodePage := CP_NONE;
1920 +
1921 +        case fdDataType of
1922 +        SQL_TIMESTAMP,
1923 +        SQL_TYPE_DATE,
1924 +        SQL_TYPE_TIME:
1925 +          fdDataSize := SizeOf(TDateTime);
1926 +        SQL_SHORT, SQL_LONG:
1927 +        begin
1928 +          if (fdDataScale = 0) then
1929 +            fdDataSize := SizeOf(Integer)
1930 +          else
1931 +          if (fdDataScale >= (-4)) then
1932 +            fdDataSize := SizeOf(Currency)
1933 +          else
1934 +            fdDataSize := SizeOf(Double);
1935 +        end;
1936 +        SQL_INT64:
1937 +        begin
1938 +          if (fdDataScale = 0) then
1939 +            fdDataSize := SizeOf(Int64)
1940 +          else
1941 +          if (fdDataScale >= (-4)) then
1942 +            fdDataSize := SizeOf(Currency)
1943 +          else
1944 +            fdDataSize := SizeOf(Double);
1945 +        end;
1946 +        SQL_DOUBLE, SQL_FLOAT, SQL_D_FLOAT:
1947 +          fdDataSize := SizeOf(Double);
1948 +        SQL_BOOLEAN:
1949 +          fdDataSize := SizeOf(wordBool);
1950 +        SQL_VARYING,
1951 +        SQL_TEXT,
1952 +        SQL_BLOB:
1953 +          fdCodePage := Qry.Metadata[i].getCodePage;
1954 +        end;
1955 +        fdDataOfs := FRecordSize;
1956 +        Inc(FRecordSize, fdDataSize);
1957 +      end;
1958 +    end;
1959 +  end;
1960 + end;
1961 +
1962   { Read the record from FQSelect.Current into the record buffer
1963    Then write the buffer to in memory cache }
1964   procedure TIBCustomDataSet.FetchCurrentRecordToBuffer(Qry: TIBSQL;
1965    RecordNumber: Integer; Buffer: PChar);
1966   var
1697  p: PRecordData;
1967    pbd: PBlobDataArray;
1968 +  pda: PArrayDataArray;
1969    i, j: Integer;
1970 <  LocalData: Pointer;
1970 >  LocalData: PByte;
1971    LocalDate, LocalDouble: Double;
1972    LocalInt: Integer;
1973    LocalBool: wordBool;
1974    LocalInt64: Int64;
1975    LocalCurrency: Currency;
1976    FieldsLoaded: Integer;
1977 <  temp: TIBXSQLVAR;
1977 >  p: PRecordData;
1978   begin
1979 +  if RecordNumber = -1 then
1980 +  begin
1981 +    InitModelBuffer(Qry,Buffer);
1982 +    Exit;
1983 +  end;
1984    p := PRecordData(Buffer);
1985    { Make sure blob cache is empty }
1986    pbd := PBlobDataArray(Buffer + FBlobCacheOffset);
1987 <  if RecordNumber > -1 then
1988 <    for i := 0 to BlobFieldCount - 1 do
1989 <      pbd^[i] := nil;
1987 >  pda := PArrayDataArray(Buffer + FArrayCacheOffset);
1988 >  for i := 0 to BlobFieldCount - 1 do
1989 >    pbd^[i] := nil;
1990 >  for i := 0 to ArrayFieldCount - 1 do
1991 >    pda^[i] := nil;
1992 >
1993    { Get record information }
1994    p^.rdBookmarkFlag := bfCurrent;
1995 <  p^.rdFieldCount := Qry.Current.Count;
1995 >  p^.rdFieldCount := Qry.FieldCount;
1996    p^.rdRecordNumber := RecordNumber;
1997    p^.rdUpdateStatus := usUnmodified;
1998    p^.rdCachedUpdateStatus := cusUnmodified;
1999    p^.rdSavedOffset := $FFFFFFFF;
2000  
2001    { Load up the fields }
2002 <  FieldsLoaded := FQSelect.Current.Count;
2002 >  FieldsLoaded := FQSelect.MetaData.Count;
2003    j := 1;
2004 <  for i := 0 to Qry.Current.Count - 1 do
2004 >  for i := 0 to Qry.FieldCount - 1 do
2005    begin
2006      if (Qry = FQSelect) then
2007        j := i + 1
2008 <    else begin
2008 >    else
2009 >    begin
2010        if FieldsLoaded = 0 then
2011          break;
2012 <      j := FQSelect.FieldIndex[Qry.Current[i].Name] + 1;
2012 >      j := FQSelect.FieldIndex[Qry[i].Name] + 1;
2013        if j < 1 then
2014          continue
2015        else
2016          Dec(FieldsLoaded);
2017      end;
2018 <    with FQSelect.Current[j - 1].Data^ do
2019 <      if aliasname = 'IBX_INTERNAL_DBKEY' then {do not localize}
2018 >    with FQSelect.MetaData[j - 1] do
2019 >      if GetAliasname = 'IBX_INTERNAL_DBKEY' then {do not localize}
2020        begin
2021 <        if sqllen <= 8 then
2022 <          p^.rdDBKey := PIBDBKEY(Qry.Current[i].AsPointer)^;
2021 >        if (GetSize <= 8) then
2022 >          p^.rdDBKey := PIBDBKEY(Qry[i].AsPointer)^;
2023          continue;
2024        end;
2025 <    if j > 0 then with p^ do
2025 >    if j > 0 then
2026      begin
2027 <      rdFields[j].fdDataType :=
2028 <        Qry.Current[i].Data^.sqltype and (not 1);
2029 <      rdFields[j].fdDataScale :=
2030 <        Qry.Current[i].Data^.sqlscale;
2031 <      rdFields[j].fdNullable :=
1753 <        (Qry.Current[i].Data^.sqltype and 1 = 1);
1754 <      rdFields[j].fdIsNull :=
1755 <        (rdFields[j].fdNullable and (Qry.Current[i].Data^.sqlind^ = -1));
1756 <      LocalData := Qry.Current[i].Data^.sqldata;
1757 <      case rdFields[j].fdDataType of
1758 <        SQL_TIMESTAMP:
1759 <        begin
1760 <          rdFields[j].fdDataSize := SizeOf(TDateTime);
1761 <          if RecordNumber >= 0 then
1762 <            LocalDate := TimeStampToMSecs(DateTimeToTimeStamp(Qry.Current[i].AsDateTime));
1763 <          LocalData := PChar(@LocalDate);
1764 <        end;
1765 <        SQL_TYPE_DATE:
1766 <        begin
1767 <          rdFields[j].fdDataSize := SizeOf(TDateTime);
1768 <          if RecordNumber >= 0 then
1769 <            LocalInt := DateTimeToTimeStamp(Qry.Current[i].AsDateTime).Date;
1770 <          LocalData := PChar(@LocalInt);
1771 <        end;
1772 <        SQL_TYPE_TIME:
1773 <        begin
1774 <          rdFields[j].fdDataSize := SizeOf(TDateTime);
1775 <          if RecordNumber >= 0 then
1776 <            LocalInt := DateTimeToTimeStamp(Qry.Current[i].AsDateTime).Time;
1777 <          LocalData := PChar(@LocalInt);
1778 <        end;
1779 <        SQL_SHORT, SQL_LONG:
2027 >      LocalData := nil;
2028 >      with p^.rdFields[j], FFieldColumns^[j] do
2029 >      begin
2030 >        Qry.Current.GetData(i,fdIsNull,fdDataLength,LocalData);
2031 >        if not fdIsNull then
2032          begin
2033 <          if (rdFields[j].fdDataScale = 0) then
2034 <          begin
2035 <            rdFields[j].fdDataSize := SizeOf(Integer);
2036 <            if RecordNumber >= 0 then
2037 <              LocalInt := Qry.Current[i].AsLong;
2038 <            LocalData := PChar(@LocalInt);
2039 <          end
2040 <          else if (rdFields[j].fdDataScale >= (-4)) then
2041 <               begin
2042 <                 rdFields[j].fdDataSize := SizeOf(Currency);
2043 <                 if RecordNumber >= 0 then
2044 <                   LocalCurrency := Qry.Current[i].AsCurrency;
2045 <                 LocalData := PChar(@LocalCurrency);
2046 <               end
2047 <               else begin
2048 <                 rdFields[j].fdDataSize := SizeOf(Double);
2049 <                 if RecordNumber >= 0 then
2050 <                   LocalDouble := Qry.Current[i].AsDouble;
2051 <                LocalData := PChar(@LocalDouble);
2033 >          case fdDataType of  {Get Formatted data for column types that need formatting}
2034 >            SQL_TIMESTAMP:
2035 >            begin
2036 >              LocalDate := TimeStampToMSecs(DateTimeToTimeStamp(Qry[i].AsDateTime));
2037 >              LocalData := PByte(@LocalDate);
2038 >            end;
2039 >            SQL_TYPE_DATE:
2040 >            begin
2041 >              LocalInt := DateTimeToTimeStamp(Qry[i].AsDateTime).Date;
2042 >              LocalData := PByte(@LocalInt);
2043 >            end;
2044 >            SQL_TYPE_TIME:
2045 >            begin
2046 >              LocalInt := DateTimeToTimeStamp(Qry[i].AsDateTime).Time;
2047 >              LocalData := PByte(@LocalInt);
2048 >            end;
2049 >            SQL_SHORT, SQL_LONG:
2050 >            begin
2051 >              if (fdDataScale = 0) then
2052 >              begin
2053 >                LocalInt := Qry[i].AsLong;
2054 >                LocalData := PByte(@LocalInt);
2055 >              end
2056 >              else
2057 >              if (fdDataScale >= (-4)) then
2058 >              begin
2059 >                LocalCurrency := Qry[i].AsCurrency;
2060 >                LocalData := PByte(@LocalCurrency);
2061 >              end
2062 >              else
2063 >              begin
2064 >               LocalDouble := Qry[i].AsDouble;
2065 >               LocalData := PByte(@LocalDouble);
2066                end;
2067 <        end;
2068 <        SQL_INT64:
2069 <        begin
2070 <          if (rdFields[j].fdDataScale = 0) then
2071 <          begin
2072 <            rdFields[j].fdDataSize := SizeOf(Int64);
2073 <            if RecordNumber >= 0 then
2074 <              LocalInt64 := Qry.Current[i].AsInt64;
2075 <            LocalData := PChar(@LocalInt64);
2076 <          end
2077 <          else if (rdFields[j].fdDataScale >= (-4)) then
2078 <               begin
2079 <                 rdFields[j].fdDataSize := SizeOf(Currency);
2080 <                 if RecordNumber >= 0 then
2081 <                   LocalCurrency := Qry.Current[i].AsCurrency;
2082 <                   LocalData := PChar(@LocalCurrency);
2083 <               end
2084 <               else begin
2085 <                  rdFields[j].fdDataSize := SizeOf(Double);
2086 <                  if RecordNumber >= 0 then
2087 <                    LocalDouble := Qry.Current[i].AsDouble;
2088 <                  LocalData := PChar(@LocalDouble);
2089 <               end
2090 <        end;
2091 <        SQL_DOUBLE, SQL_FLOAT, SQL_D_FLOAT:
2092 <        begin
1827 <          rdFields[j].fdDataSize := SizeOf(Double);
1828 <          if RecordNumber >= 0 then
1829 <            LocalDouble := Qry.Current[i].AsDouble;
1830 <          LocalData := PChar(@LocalDouble);
1831 <        end;
1832 <        SQL_VARYING:
1833 <        begin
1834 <          rdFields[j].fdDataSize := Qry.Current[i].Data^.sqllen;
1835 <          rdFields[j].fdDataLength := isc_vax_integer(Qry.Current[i].Data^.sqldata, 2);
1836 <          if RecordNumber >= 0 then
1837 <          begin
1838 <            if (rdFields[j].fdDataLength = 0) then
1839 <              LocalData := nil
1840 <            else
2067 >            end;
2068 >            SQL_INT64:
2069 >            begin
2070 >              if (fdDataScale = 0) then
2071 >              begin
2072 >                LocalInt64 := Qry[i].AsInt64;
2073 >                LocalData := PByte(@LocalInt64);
2074 >              end
2075 >              else
2076 >              if (fdDataScale >= (-4)) then
2077 >              begin
2078 >                LocalCurrency := Qry[i].AsCurrency;
2079 >                LocalData := PByte(@LocalCurrency);
2080 >                end
2081 >                else
2082 >                begin
2083 >                  LocalDouble := Qry[i].AsDouble;
2084 >                  LocalData := PByte(@LocalDouble);
2085 >                end
2086 >            end;
2087 >            SQL_DOUBLE, SQL_FLOAT, SQL_D_FLOAT:
2088 >            begin
2089 >              LocalDouble := Qry[i].AsDouble;
2090 >              LocalData := PByte(@LocalDouble);
2091 >            end;
2092 >            SQL_BOOLEAN:
2093              begin
2094 <              temp :=  Qry.Current[i];
2095 <              LocalData := @temp.Data^.sqldata[2];
1844 < (*              LocalData := @Qry.Current[i].Data^.sqldata[2];*)
2094 >              LocalBool := Qry[i].AsBoolean;
2095 >              LocalData := PByte(@LocalBool);
2096              end;
2097            end;
2098 <        end;
2099 <        SQL_BOOLEAN:
2100 <        begin
2101 <          LocalBool:= false;
2102 <          rdFields[j].fdDataSize := SizeOf(wordBool);
1852 <          if RecordNumber >= 0 then
1853 <            LocalBool := Qry.Current[i].AsBoolean;
1854 <          LocalData := PChar(@LocalBool);
1855 <        end;
1856 <        else { SQL_TEXT, SQL_BLOB, SQL_ARRAY, SQL_QUAD }
1857 <        begin
1858 <          rdFields[j].fdDataSize := Qry.Current[i].Data^.sqllen;
1859 <          if (rdFields[j].fdDataType = SQL_TEXT) then
1860 <            rdFields[j].fdDataLength := rdFields[j].fdDataSize;
1861 <        end;
1862 <      end;
1863 <      if RecordNumber < 0 then
1864 <      begin
1865 <        rdFields[j].fdIsNull := True;
1866 <        rdFields[j].fdDataOfs := FRecordSize;
1867 <        Inc(FRecordSize, rdFields[j].fdDataSize);
1868 <      end
1869 <      else begin
1870 <        if rdFields[j].fdDataType = SQL_VARYING then
1871 <        begin
1872 <          if LocalData <> nil then
1873 <            Move(LocalData^, Buffer[rdFields[j].fdDataOfs], rdFields[j].fdDataLength)
2098 >
2099 >          if fdDataType = SQL_VARYING then
2100 >            Move(LocalData^, Buffer[fdDataOfs], fdDataLength)
2101 >          else
2102 >            Move(LocalData^, Buffer[fdDataOfs], fdDataSize)
2103          end
2104 +        else {Null column}
2105 +        if fdDataType = SQL_VARYING then
2106 +          FillChar(Buffer[fdDataOfs],fdDataLength,0)
2107          else
2108 <          Move(LocalData^, Buffer[rdFields[j].fdDataOfs], rdFields[j].fdDataSize)
2108 >          FillChar(Buffer[fdDataOfs],fdDataSize,0);
2109        end;
2110      end;
2111    end;
2112 <  WriteRecordCache(RecordNumber, PChar(p));
2112 >  WriteRecordCache(RecordNumber, Buffer);
2113   end;
2114  
2115   function TIBCustomDataSet.GetActiveBuf: PChar;
# Line 1922 | Line 2154 | begin
2154    result := FBase.Database;
2155   end;
2156  
1925 function TIBCustomDataSet.GetDBHandle: PISC_DB_HANDLE;
1926 begin
1927  result := FBase.DBHandle;
1928 end;
1929
2157   function TIBCustomDataSet.GetDeleteSQL: TStrings;
2158   begin
2159    result := FQDelete.SQL;
# Line 1937 | Line 2164 | begin
2164    result := FQInsert.SQL;
2165   end;
2166  
2167 < function TIBCustomDataSet.GetSQLParams: TIBXSQLDA;
2167 > function TIBCustomDataSet.GetSQLParams: ISQLParams;
2168   begin
2169    if not FInternalPrepared then
2170      InternalPrepare;
# Line 1954 | Line 2181 | begin
2181    result := FQSelect.SQL;
2182   end;
2183  
2184 < function TIBCustomDataSet.GetStatementType: TIBSQLTypes;
2184 > function TIBCustomDataSet.GetStatementType: TIBSQLStatementTypes;
2185   begin
2186 <  result := FQSelect.SQLType;
2186 >  result := FQSelect.SQLStatementType;
2187   end;
2188  
2189   function TIBCustomDataSet.GetModifySQL: TStrings;
# Line 1969 | Line 2196 | begin
2196    result := FBase.Transaction;
2197   end;
2198  
1972 function TIBCustomDataSet.GetTRHandle: PISC_TR_HANDLE;
1973 begin
1974  result := FBase.TRHandle;
1975 end;
1976
2199   procedure TIBCustomDataSet.InternalDeleteRecord(Qry: TIBSQL; Buff: Pointer);
2200   begin
2201    if (Assigned(FUpdateObject) and (FUpdateObject.GetSQL(ukDelete).Text <> '')) then
# Line 1995 | Line 2217 | function TIBCustomDataSet.InternalLocate
2217    const KeyValues: Variant; Options: TLocateOptions): Boolean;
2218   var
2219    keyFieldList: TList;
1998  {$IF FPC_FULLVERSION >= 20700 }
2220    CurBookmark: TBookmark;
2000  {$ELSE}
2001  CurBookmark: string;
2002  {$ENDIF}
2221    fieldValue: Variant;
2222    lookupValues: array of variant;
2223    i, fieldCount: Integer;
# Line 2081 | Line 2299 | end;
2299  
2300   procedure TIBCustomDataSet.InternalPostRecord(Qry: TIBSQL; Buff: Pointer);
2301   var
2302 <  i, j, k: Integer;
2302 >  i, j, k, arr: Integer;
2303    pbd: PBlobDataArray;
2304 +  pda: PArrayDataArray;
2305   begin
2306    pbd := PBlobDataArray(PChar(Buff) + FBlobCacheOffset);
2307 <  j := 0;
2307 >  pda := PArrayDataArray(PChar(Buff) + FArrayCacheOffset);
2308 >  j := 0; arr := 0;
2309    for i := 0 to FieldCount - 1 do
2310      if Fields[i].IsBlob then
2311      begin
# Line 2094 | Line 2314 | begin
2314        begin
2315          pbd^[j].Finalize;
2316          PISC_QUAD(
2317 <          PChar(Buff) + PRecordData(Buff)^.rdFields[k].fdDataOfs)^ :=
2317 >          PChar(Buff) + FFieldColumns^[k].fdDataOfs)^ :=
2318            pbd^[j].BlobID;
2319          PRecordData(Buff)^.rdFields[k].fdIsNull := pbd^[j].Size = 0;
2320 +      end
2321 +      else
2322 +      begin
2323 +        PRecordData(Buff)^.rdFields[k].fdIsNull := true;
2324 +        with PISC_QUAD(PChar(Buff) + FFieldColumns^[k].fdDataOfs)^ do
2325 +        begin
2326 +          gds_quad_high := 0;
2327 +          gds_quad_low := 0;
2328 +        end;
2329        end;
2330        Inc(j);
2331 +    end
2332 +    else
2333 +    if Fields[i] is TIBArrayField then
2334 +    begin
2335 +      if pda^[arr] <> nil then
2336 +      begin
2337 +        k := FMappedFieldPosition[Fields[i].FieldNo -1];
2338 +        PISC_QUAD(
2339 +          PChar(Buff) + FFieldColumns^[k].fdDataOfs)^ :=  pda^[arr].ArrayIntf.GetArrayID;
2340 +        PRecordData(Buff)^.rdFields[k].fdIsNull := pda^[arr].ArrayIntf.IsEmpty;
2341 +      end;
2342 +      Inc(arr);
2343      end;
2344    if Assigned(FUpdateObject) then
2345    begin
# Line 2147 | Line 2388 | begin
2388          SetInternalSQLParams(Qry, Buff);
2389          Qry.ExecQuery;
2390          try
2391 <          if (Qry.SQLType = SQLExecProcedure) or
2151 <             (Qry.Next <> nil) then
2391 >          if (Qry.SQLStatementType = SQLExecProcedure) or Qry.Next then
2392            begin
2393              ofs := PRecordData(Buff)^.rdSavedOffset;
2394              FetchCurrentRecordToBuffer(Qry,
# Line 2241 | Line 2481 | begin
2481   end;
2482  
2483   procedure TIBCustomDataSet.InternalPrepare;
2244 var
2245  DidActivate: Boolean;
2484   begin
2485    if FInternalPrepared then
2486      Exit;
2249  DidActivate := False;
2487    FBase.SetCursor;
2488    try
2489      ActivateConnection;
2490 <    DidActivate := ActivateTransaction;
2490 >    ActivateTransaction;
2491      FBase.CheckDatabase;
2492      FBase.CheckTransaction;
2493      if HasParser and (FParser.SQLText <> FQSelect.SQL.Text) then
# Line 2288 | Line 2525 | begin
2525      end else
2526        IBError(ibxeEmptyQuery, [nil]);
2527    finally
2291    if DidActivate then
2292      DeactivateTransaction;
2528      FBase.RestoreCursor;
2529    end;
2530   end;
# Line 2319 | Line 2554 | var
2554    begin
2555      CopyRecordBuffer(Buffer, OldBuffer);
2556      if BlobFieldCount > 0 then
2557 <      FillChar(PChar(OldBuffer)[FBlobCacheOffset], BlobFieldCount * SizeOf(TIBBlobStream),
2557 >      FillChar(PChar(OldBuffer)[FBlobCacheOffset],
2558 >               BlobFieldCount * SizeOf(TIBBlobStream) + ArrayFieldCount * SizeOf(IArray),
2559                 0);
2560    end;
2561  
# Line 2392 | Line 2628 | procedure TIBCustomDataSet.SetInternalSQ
2628   var
2629    i, j: Integer;
2630    cr, data: PChar;
2631 <  fn, st: string;
2631 >  fn: string;
2632 >  st: RawByteString;
2633    OldBuffer: Pointer;
2634    ts: TTimeStamp;
2635 +  Param: ISQLParam;
2636   begin
2637    if (Buffer = nil) then
2638      IBError(ibxeBufferNotSet, [nil]);
# Line 2402 | Line 2640 | begin
2640      InternalPrepare;
2641    OldBuffer := nil;
2642    try
2643 <    for i := 0 to Qry.Params.Count - 1 do
2643 >    for i := 0 to Qry.Params.GetCount - 1 do
2644      begin
2645 <      fn := Qry.Params[i].Name;
2645 >      Param := Qry.Params[i];
2646 >      fn := Param.Name;
2647        if (Pos('OLD_', fn) = 1) then {mbcs ok}
2648        begin
2649          fn := Copy(fn, 5, Length(fn));
# Line 2424 | Line 2663 | begin
2663               cr := Buffer;
2664        j := FQSelect.FieldIndex[fn] + 1;
2665        if (j > 0) then
2666 <        with PRecordData(cr)^ do
2666 >        with PRecordData(cr)^,rdFields[j], FFieldColumns^[j] do
2667          begin
2668 <          if Qry.Params[i].name = 'IBX_INTERNAL_DBKEY' then {do not localize}
2668 >          if Param.name = 'IBX_INTERNAL_DBKEY' then {do not localize}
2669            begin
2670 <            PIBDBKey(Qry.Params[i].AsPointer)^ := rdDBKey;
2670 >            PIBDBKey(Param.AsPointer)^ := rdDBKey;
2671              continue;
2672            end;
2673 <          if rdFields[j].fdIsNull then
2674 <            Qry.Params[i].IsNull := True
2673 >          if fdIsNull then
2674 >            Param.IsNull := True
2675            else begin
2676 <            Qry.Params[i].IsNull := False;
2677 <            data := cr + rdFields[j].fdDataOfs;
2678 <            case rdFields[j].fdDataType of
2676 >            Param.IsNull := False;
2677 >            data := cr + fdDataOfs;
2678 >            case fdDataType of
2679                SQL_TEXT, SQL_VARYING:
2680                begin
2681 <                SetString(st, data, rdFields[j].fdDataLength);
2682 <                Qry.Params[i].AsString := st;
2681 >                SetString(st, data, fdDataLength);
2682 >                SetCodePage(st,fdCodePage,false);
2683 >                Param.AsString := st;
2684                end;
2685              SQL_FLOAT, SQL_DOUBLE, SQL_D_FLOAT:
2686 <              Qry.Params[i].AsDouble := PDouble(data)^;
2686 >              Param.AsDouble := PDouble(data)^;
2687              SQL_SHORT, SQL_LONG:
2688              begin
2689 <              if rdFields[j].fdDataScale = 0 then
2690 <                Qry.Params[i].AsLong := PLong(data)^
2691 <              else if rdFields[j].fdDataScale >= (-4) then
2692 <                Qry.Params[i].AsCurrency := PCurrency(data)^
2689 >              if fdDataScale = 0 then
2690 >                Param.AsLong := PLong(data)^
2691 >              else
2692 >              if fdDataScale >= (-4) then
2693 >                Param.AsCurrency := PCurrency(data)^
2694                else
2695 <                Qry.Params[i].AsDouble := PDouble(data)^;
2695 >                Param.AsDouble := PDouble(data)^;
2696              end;
2697              SQL_INT64:
2698              begin
2699 <              if rdFields[j].fdDataScale = 0 then
2700 <                Qry.Params[i].AsInt64 := PInt64(data)^
2460 <              else if rdFields[j].fdDataScale >= (-4) then
2461 <                Qry.Params[i].AsCurrency := PCurrency(data)^
2699 >              if fdDataScale = 0 then
2700 >                Param.AsInt64 := PInt64(data)^
2701                else
2702 <                Qry.Params[i].AsDouble := PDouble(data)^;
2702 >              if fdDataScale >= (-4) then
2703 >                Param.AsCurrency := PCurrency(data)^
2704 >              else
2705 >                Param.AsDouble := PDouble(data)^;
2706              end;
2707              SQL_BLOB, SQL_ARRAY, SQL_QUAD:
2708 <              Qry.Params[i].AsQuad := PISC_QUAD(data)^;
2708 >              Param.AsQuad := PISC_QUAD(data)^;
2709              SQL_TYPE_DATE:
2710              begin
2711                ts.Date := PInt(data)^;
2712                ts.Time := 0;
2713 <              Qry.Params[i].AsDate :=
2472 <                TimeStampToDateTime(ts);
2713 >              Param.AsDate := TimeStampToDateTime(ts);
2714              end;
2715              SQL_TYPE_TIME:
2716              begin
2717                ts.Date := 0;
2718                ts.Time := PInt(data)^;
2719 <              Qry.Params[i].AsTime :=
2479 <                TimeStampToDateTime(ts);
2719 >              Param.AsTime := TimeStampToDateTime(ts);
2720              end;
2721              SQL_TIMESTAMP:
2722 <              Qry.Params[i].AsDateTime :=
2722 >              Param.AsDateTime :=
2723                         TimeStampToDateTime(MSecsToTimeStamp(trunc(PDouble(data)^)));
2724              SQL_BOOLEAN:
2725 <              Qry.Params[i].AsBoolean := PWordBool(data)^;
2725 >              Param.AsBoolean := PWordBool(data)^;
2726            end;
2727          end;
2728        end;
# Line 2638 | Line 2878 | begin
2878    Result := Assigned( FQSelect ) and FQSelect.EOF;
2879   end;
2880  
2881 < function TIBCustomDataSet.ParamByName(ParamName: String): TIBXSQLVAR;
2881 > function TIBCustomDataSet.ParamByName(ParamName: String): ISQLParam;
2882   begin
2883    ActivateConnection;
2884    ActivateTransaction;
# Line 2775 | Line 3015 | var
3015    Buff: PChar;
3016    bTr, bDB: Boolean;
3017   begin
3018 +  if (Field = nil) or (Field.DataSet <> self) then
3019 +    IBError(ibxFieldNotinDataSet,[Field.Name,Name]);
3020    Buff := GetActiveBuf;
3021    if Buff = nil then
3022    begin
3023      fs := TIBBlobStream.Create;
3024      fs.Mode := bmReadWrite;
3025 +    fs.Database := Database;
3026 +    fs.Transaction := Transaction;
3027 +    fs.SetField(Field);
3028      FBlobStreamList.Add(Pointer(fs));
3029      result := TIBDSBlobStream.Create(Field, fs, Mode);
3030      exit;
# Line 2794 | Line 3039 | begin
3039      fs.Mode := bmReadWrite;
3040      fs.Database := Database;
3041      fs.Transaction := Transaction;
3042 +    fs.SetField(Field);
3043      fs.BlobID :=
3044 <      PISC_QUAD(@Buff[PRecordData(Buff)^.rdFields[FMappedFieldPosition[Field.FieldNo - 1]].fdDataOfs])^;
3044 >      PISC_QUAD(@Buff[FFieldColumns^[FMappedFieldPosition[Field.FieldNo - 1]].fdDataOfs])^;
3045      if (CachedUpdates) then
3046      begin
3047        bTr := not Transaction.InTransaction;
# Line 2816 | Line 3062 | begin
3062    result := TIBDSBlobStream.Create(Field, fs, Mode);
3063   end;
3064  
3065 + function TIBCustomDataSet.GetArray(Field: TIBArrayField): IArray;
3066 + var Buff: PChar;
3067 +    pda: PArrayDataArray;
3068 +    bTr, bDB: Boolean;
3069 + begin
3070 +  if (Field = nil) or (Field.DataSet <> self) then
3071 +    IBError(ibxFieldNotinDataSet,[Field.Name,Name]);
3072 +  Buff := GetActiveBuf;
3073 +  if Buff = nil then
3074 +    Result := Database.Attachment.CreateArray(Transaction.TransactionIntf,
3075 +                              Field.FRelationName,Field.FieldName)
3076 +  else
3077 +  begin
3078 +    pda := PArrayDataArray(Buff + FArrayCacheOffset);
3079 +    if pda^[Field.FCacheOffset] = nil then
3080 +    begin
3081 +      AdjustRecordOnInsert(Buff);
3082 +      if Field.IsNull then
3083 +        Result := Database.Attachment.CreateArray(Transaction.TransactionIntf,
3084 +                                Field.FRelationName,Field.FieldName)
3085 +      else
3086 +        Result := Database.Attachment.OpenArray(Transaction.TransactionIntf,
3087 +                            Field.FRelationName,Field.FieldName,Field.ArrayID);
3088 +      pda^[Field.FCacheOffset] := TIBArray.Create(Field,Result);
3089 +      FArrayList.Add(pda^[Field.FCacheOffset]);
3090 +      if (CachedUpdates) then
3091 +      begin
3092 +        bTr := not Transaction.InTransaction;
3093 +        bDB := not Database.Connected;
3094 +        if bDB then
3095 +          Database.Open;
3096 +        if bTr then
3097 +          Transaction.StartTransaction;
3098 +         pda^[Field.FCacheOffset].ArrayIntf.PreLoad;
3099 +        if bTr then
3100 +          Transaction.Commit;
3101 +        if bDB then
3102 +          Database.Close;
3103 +      end;
3104 +      WriteRecordCache(PRecordData(Buff)^.rdRecordNumber, Pointer(Buff));
3105 +    end
3106 +    else
3107 +      Result := pda^[Field.FCacheOffset].ArrayIntf;
3108 +  end;
3109 + end;
3110 +
3111 + procedure TIBCustomDataSet.SetArrayIntf(AnArray: IArray; Field: TIBArrayField);
3112 + var Buff: PChar;
3113 +    pda: PArrayDataArray;
3114 + begin
3115 +  if (Field = nil) or (Field.DataSet <> self) then
3116 +    IBError(ibxFieldNotinDataSet,[Field.Name,Name]);
3117 +  Buff := GetActiveBuf;
3118 +  if Buff <> nil then
3119 +  begin
3120 +    AdjustRecordOnInsert(Buff);
3121 +    pda := PArrayDataArray(Buff + FArrayCacheOffset);
3122 +    pda^[Field.FCacheOffset].FArray := AnArray;
3123 +    WriteRecordCache(PRecordData(Buff)^.rdRecordNumber, Pointer(Buff));
3124 +  end;
3125 + end;
3126 +
3127   function TIBCustomDataSet.CompareBookmarks(Bookmark1, Bookmark2: TBookmark): Integer;
3128   const
3129    CMPLess = -1;
# Line 2896 | Line 3204 | end;
3204   procedure TIBCustomDataSet.DoBeforeClose;
3205   begin
3206    inherited DoBeforeClose;
3207 +  if FInTransactionEnd and (FCloseAction = TARollback) then
3208 +     Exit;
3209    if State in [dsInsert,dsEdit] then
3210    begin
2901    if FInTransactionEnd and (FCloseAction = TARollback) then
2902       Exit;
2903
3211      if DataSetCloseAction = dcSaveChanges then
3212        Post;
3213        {Note this can fail with an exception e.g. due to
3214         database validation error. In which case the dataset remains open }
3215    end;
3216 +  if FCachedUpdates and FUpdatesPending and (DataSetCloseAction = dcSaveChanges) then
3217 +    ApplyUpdates;
3218   end;
3219  
3220   procedure TIBCustomDataSet.DoBeforeOpen;
# Line 2937 | Line 3246 | end;
3246  
3247   procedure TIBCustomDataSet.FetchAll;
3248   var
2940  {$IF FPC_FULLVERSION >= 20700 }
3249    CurBookmark: TBookmark;
2942  {$ELSE}
2943  CurBookmark: string;
2944  {$ENDIF}
3250   begin
3251    FBase.SetCursor;
3252   try
# Line 3052 | Line 3357 | begin
3357      if result and (Buffer <> nil) then
3358        Move(Buff[1], Buffer^, Field.DataSize);
3359    end
3360 <  else if (FMappedFieldPosition[Field.FieldNo - 1] > 0) and
3360 >  else
3361 >  if (FMappedFieldPosition[Field.FieldNo - 1] > 0) and
3362       (FMappedFieldPosition[Field.FieldNo - 1] <= CurrentRecord^.rdFieldCount) then
3363 +  with CurrentRecord^.rdFields[FMappedFieldPosition[Field.FieldNo - 1]],
3364 +                         FFieldColumns^[FMappedFieldPosition[Field.FieldNo - 1]] do
3365    begin
3366 <    result := not CurrentRecord^.rdFields[FMappedFieldPosition[Field.FieldNo - 1]].fdIsNull;
3366 >    result := not fdIsNull;
3367      if result and (Buffer <> nil) then
3060      with CurrentRecord^.rdFields[FMappedFieldPosition[Field.FieldNo - 1]] do
3368        begin
3369 <        Data := Buff + CurrentRecord^.rdFields[FMappedFieldPosition[Field.FieldNo - 1]].fdDataOfs;
3369 >        Data := Buff + fdDataOfs;
3370          if (fdDataType = SQL_VARYING) or (fdDataType = SQL_TEXT) then
3371          begin
3372            if fdDataLength < Field.DataSize then
# Line 3130 | Line 3437 | begin
3437          if FCurrentRecord < FRecordCount then
3438            ReadRecordCache(FCurrentRecord, Buffer, False)
3439          else begin
3440 <          while (not FQSelect.EOF) and
3134 <                (FQSelect.Next <> nil) and
3440 >          while (not FQSelect.EOF) and FQSelect.Next  and
3441                  (FCurrentRecord >= FRecordCount) do begin
3442              FetchCurrentRecordToBuffer(FQSelect, FRecordCount, Buffer);
3443              Inc(FRecordCount);
# Line 3242 | Line 3548 | procedure TIBCustomDataSet.InternalCance
3548   var
3549    Buff: PChar;
3550    CurRec: Integer;
3551 +  pda: PArrayDataArray;
3552 +  i: integer;
3553   begin
3554    inherited InternalCancel;
3555    Buff := GetActiveBuf;
3556 <  if Buff <> nil then begin
3556 >  if Buff <> nil then
3557 >  begin
3558 >    pda := PArrayDataArray(Buff + FArrayCacheOffset);
3559 >    for i := 0 to ArrayFieldCount - 1 do
3560 >      pda^[i].ArrayIntf.CancelChanges;
3561      CurRec := FCurrentRecord;
3562      AdjustRecordOnInsert(Buff);
3563      if (State = dsEdit) then begin
# Line 3268 | Line 3580 | begin
3580      DeactivateTransaction;
3581    FQSelect.Close;
3582    ClearBlobCache;
3583 +  ClearArrayCache;
3584    FreeRecordBuffer(FModelBuffer);
3585    FreeRecordBuffer(FOldBuffer);
3586    FCurrentRecord := -1;
# Line 3283 | Line 3596 | begin
3596    FOBEnd := 0;
3597    FreeMem(FBufferCache);
3598    FBufferCache := nil;
3599 +  FreeMem(FFieldColumns);
3600 +  FFieldColumns := nil;
3601    FreeMem(FOldBufferCache);
3602    FOldBufferCache := nil;
3603    BindFields(False);
# Line 3360 | Line 3675 | const
3675   var
3676    FieldType: TFieldType;
3677    FieldSize: Word;
3678 +  FieldDataSize: integer;
3679    charSetID: short;
3680    CharSetSize: integer;
3681 <  CharSetName: string;
3681 >  CharSetName: RawByteString;
3682 >  FieldCodePage: TSystemCodePage;
3683    FieldNullable : Boolean;
3684    i, FieldPosition, FieldPrecision: Integer;
3685    FieldAliasName, DBAliasName: string;
3686 <  RelationName, FieldName: string;
3686 >  aRelationName, FieldName: string;
3687    Query : TIBSQL;
3688    FieldIndex: Integer;
3689    FRelationNodes : TRelationNode;
3690 +  aArrayDimensions: integer;
3691 +  aArrayBounds: TArrayBounds;
3692 +  ArrayMetaData: IArrayMetaData;
3693  
3694    function Add_Node(Relation, Field : String) : TRelationNode;
3695    var
# Line 3467 | Line 3787 | var
3787   begin
3788    FRelationNodes := TRelationNode.Create;
3789    FNeedsRefresh := False;
3790 <  Database.InternalTransaction.StartTransaction;
3790 >  if not Database.InternalTransaction.InTransaction then
3791 >    Database.InternalTransaction.StartTransaction;
3792    Query := TIBSQL.Create(self);
3793    try
3794      Query.Database := DataBase;
# Line 3475 | Line 3796 | begin
3796      FieldDefs.BeginUpdate;
3797      FieldDefs.Clear;
3798      FieldIndex := 0;
3799 <    if (Length(FMappedFieldPosition) < SourceQuery.Current.Count) then
3800 <      SetLength(FMappedFieldPosition, SourceQuery.Current.Count);
3799 >    if (Length(FMappedFieldPosition) < SourceQuery.MetaData.Count) then
3800 >      SetLength(FMappedFieldPosition, SourceQuery.MetaData.Count);
3801      Query.SQL.Text := DefaultSQL;
3802      Query.Prepare;
3803 <    SetLength(FAliasNameMap, SourceQuery.Current.Count);
3804 <    SetLength(FAliasNameList, SourceQuery.Current.Count);
3805 <    for i := 0 to SourceQuery.Current.Count - 1 do
3806 <      with SourceQuery.Current[i].Data^ do
3803 >    SetLength(FAliasNameMap, SourceQuery.MetaData.Count);
3804 >    SetLength(FAliasNameList, SourceQuery.MetaData.Count);
3805 >    for i := 0 to SourceQuery.MetaData.GetCount - 1 do
3806 >      with SourceQuery.MetaData[i] do
3807        begin
3808          { Get the field name }
3809 <        FieldAliasName := SourceQuery.Current[i].Name;
3810 <        SetString(DBAliasName, aliasname, aliasname_length);
3811 <        SetString(RelationName, relname, relname_length);
3812 <        SetString(FieldName, sqlname, sqlname_length);
3809 >        FieldAliasName := GetName;
3810 >        DBAliasName := GetAliasname;
3811 >        aRelationName := getRelationName;
3812 >        FieldName := getSQLName;
3813          FAliasNameList[i] := DBAliasName;
3814          FieldSize := 0;
3815 +        FieldDataSize := GetSize;
3816          FieldPrecision := 0;
3817 <        FieldNullable := SourceQuery.Current[i].IsNullable;
3817 >        FieldNullable := IsNullable;
3818          CharSetSize := 0;
3819          CharSetName := '';
3820 <        case sqltype and not 1 of
3820 >        FieldCodePage := CP_NONE;
3821 >        aArrayDimensions := 0;
3822 >        SetLength(aArrayBounds,0);
3823 >        case SQLType of
3824            { All VARCHAR's must be converted to strings before recording
3825             their values }
3826            SQL_VARYING, SQL_TEXT:
3827            begin
3828 <            CharSetSize := FBase.GetCharSetSize(sqlsubtype and $FF);
3829 <            CharSetName := FBase.GetCharSetName(sqlsubtype and $FF);
3830 <            {FieldSize is encoded for strings - see TIBStringField.SetSize for decode}
3831 <            FieldSize := sqllen;
3832 <            if CharSetSize = 2 then
3833 <              FieldType := ftWideString
3509 <            else
3510 <              FieldType := ftString;
3828 >            if not Database.Attachment.CharSetWidth(getCharSetID,CharSetSize) then
3829 >              CharSetSize := 1;
3830 >            CharSetName := Database.Attachment.GetCharsetName(getCharSetID);
3831 >            Database.Attachment.CharSetID2CodePage(getCharSetID,FieldCodePage);
3832 >            FieldSize := FieldDataSize div CharSetSize;
3833 >            FieldType := ftString;
3834            end;
3835            { All Doubles/Floats should be cast to doubles }
3836            SQL_DOUBLE, SQL_FLOAT:
3837              FieldType := ftFloat;
3838            SQL_SHORT:
3839            begin
3840 <            if (sqlscale = 0) then
3840 >            if (getScale = 0) then
3841                FieldType := ftSmallInt
3842              else begin
3843                FieldType := ftBCD;
3844                FieldPrecision := 4;
3845 <              FieldSize := -sqlscale;
3845 >              FieldSize := -getScale;
3846              end;
3847            end;
3848            SQL_LONG:
3849            begin
3850 <            if (sqlscale = 0) then
3850 >            if (getScale = 0) then
3851                FieldType := ftInteger
3852 <            else if (sqlscale >= (-4)) then
3852 >            else if (getScale >= (-4)) then
3853              begin
3854                FieldType := ftBCD;
3855                FieldPrecision := 9;
3856 <              FieldSize := -sqlscale;
3856 >              FieldSize := -getScale;
3857              end
3858              else
3859              if Database.SQLDialect = 1 then
# Line 3542 | Line 3865 | begin
3865              begin
3866                FieldType := ftFMTBCD;
3867                FieldPrecision := 9;
3868 <              FieldSize := -sqlscale;
3868 >              FieldSize := -getScale;
3869              end;
3870            end;
3871  
3872            SQL_INT64:
3873            begin
3874 <            if (sqlscale = 0) then
3874 >            if (getScale = 0) then
3875                FieldType := ftLargeInt
3876 <            else if (sqlscale >= (-4)) then
3876 >            else if (getScale >= (-4)) then
3877              begin
3878                FieldType := ftBCD;
3879                FieldPrecision := 18;
3880 <              FieldSize := -sqlscale;
3880 >              FieldSize := -getScale;
3881              end
3882              else
3883 <              FieldType := ftFloat
3883 >              FieldType := ftFloat;
3884            end;
3885            SQL_TIMESTAMP: FieldType := ftDateTime;
3886            SQL_TYPE_TIME: FieldType := ftTime;
# Line 3565 | Line 3888 | begin
3888            SQL_BLOB:
3889            begin
3890              FieldSize := sizeof (TISC_QUAD);
3891 <            if (sqlsubtype = 1) then
3891 >            if (getSubtype = 1) then
3892              begin
3893 <              if strpas(sqlname) = '' then {Complex SQL with no identifiable column - use connection default}
3894 <              begin
3895 <                CharSetSize := FBase.GetDefaultCharSetSize;
3896 <                CharSetName := FBase.GetDefaultCharSetName;
3897 <              end
3575 <              else
3576 <              begin
3577 <                charSetID := GetBlobCharSetID(Database.Handle,Database.InternalTransaction.Handle,
3578 <                        @relname,@sqlname);
3579 <                CharSetSize := FBase.GetCharSetSize(charSetID);
3580 <                CharSetName := FBase.GetCharSetName(charSetID);
3581 <              end;
3582 <              if CharSetSize = 2 then
3583 <                FieldType := ftWideMemo
3584 <              else
3585 <                FieldType := ftMemo;
3893 >              if not Database.Attachment.CharSetWidth(getCharSetID,CharSetSize) then
3894 >                CharSetSize := 1;
3895 >              CharSetName := Database.Attachment.GetCharsetName(getCharSetID);
3896 >              Database.Attachment.CharSetID2CodePage(getCharSetID,FieldCodePage);
3897 >              FieldType := ftMemo;
3898              end
3899              else
3900                FieldType := ftBlob;
# Line 3590 | Line 3902 | begin
3902            SQL_ARRAY:
3903            begin
3904              FieldSize := sizeof (TISC_QUAD);
3905 <            FieldType := ftUnknown;
3905 >            FieldType := ftArray;
3906 >            ArrayMetaData := GetArrayMetaData;
3907 >            if ArrayMetaData <> nil then
3908 >            begin
3909 >              aArrayDimensions := ArrayMetaData.GetDimensions;
3910 >              aArrayBounds := ArrayMetaData.GetBounds;
3911 >            end;
3912            end;
3913            SQL_BOOLEAN:
3914               FieldType:= ftBoolean;
# Line 3607 | Line 3925 | begin
3925              Name := FieldAliasName;
3926              FAliasNameMap[FieldNo-1] := DBAliasName;
3927              Size := FieldSize;
3928 +            DataSize := FieldDataSize;
3929              Precision := FieldPrecision;
3930              Required := not FieldNullable;
3931 +            RelationName := aRelationName;
3932              InternalCalcField := False;
3933              CharacterSetSize := CharSetSize;
3934              CharacterSetName := CharSetName;
3935 +            CodePage := FieldCodePage;
3936 +            ArrayDimensions := aArrayDimensions;
3937 +            ArrayBounds := aArrayBounds;
3938              if (FieldName <> '') and (RelationName <> '') then
3939              begin
3940                if Has_COMPUTED_BLR(RelationName, FieldName) then
# Line 3639 | Line 3962 | begin
3962      FreeNodes;
3963      Database.InternalTransaction.Commit;
3964      FieldDefs.EndUpdate;
3965 +    FieldDefs.Updated := true;
3966    end;
3967   end;
3968  
# Line 3656 | Line 3980 | begin
3980    else begin
3981      Buffer := AllocRecordBuffer;
3982      try
3983 <      while FQSelect.Next <> nil do
3983 >      while FQSelect.Next do
3984        begin
3985          FetchCurrentRecordToBuffer(FQSelect, FRecordCount, Buffer);
3986          Inc(FRecordCount);
# Line 3671 | Line 3995 | end;
3995   procedure TIBCustomDataSet.InternalSetParamsFromCursor;
3996   var
3997    i: Integer;
3998 <  cur_param: TIBXSQLVAR;
3998 >  cur_param: ISQLParam;
3999    cur_field: TField;
4000    s: TStream;
4001   begin
# Line 3679 | Line 4003 | begin
4003      IBError(ibxeEmptyQuery, [nil]);
4004    if not FInternalPrepared then
4005      InternalPrepare;
4006 <  if (SQLParams.Count > 0) and (DataSource <> nil) and (DataSource.DataSet <> nil) then
4006 >  if (SQLParams.GetCount > 0) and (DataSource <> nil) and (DataSource.DataSet <> nil) then
4007    begin
4008 <    for i := 0 to SQLParams.Count - 1 do
4008 >    for i := 0 to SQLParams.GetCount - 1 do
4009      begin
4010        cur_field := DataSource.DataSet.FindField(SQLParams[i].Name);
4011        cur_param := SQLParams[i];
# Line 3715 | Line 4039 | begin
4039              try
4040                s := DataSource.DataSet.
4041                       CreateBlobStream(cur_field, bmRead);
4042 <              cur_param.LoadFromStream(s);
4042 >              cur_param.AsBlob := TIBDSBlobStream(s).FBlobStream.Blob;
4043              finally
4044                s.free;
4045              end;
4046            end;
4047 +          ftArray:
4048 +            cur_param.AsArray := TIBArrayField(cur_field).ArrayIntf;
4049            else
4050              IBError(ibxeNotSupported, [nil]);
4051          end;
# Line 3752 | Line 4078 | procedure TIBCustomDataSet.InternalOpen;
4078      result := SizeOf(TRecordData) + ((n - 1) * SizeOf(TFieldData));
4079    end;
4080  
3755  function GetFieldDef(aFieldNo: integer): TIBFieldDef;
3756  var i: integer;
3757  begin
3758    Result := nil;
3759    for i := 0 to FieldDefs.Count - 1 do
3760      if FieldDefs[i].FieldNo = aFieldNo then
3761      begin
3762        Result := TIBFieldDef(FieldDefs[i]);
3763        break;
3764      end;
3765  end;
3766
3767  procedure SetExtendedProperties;
3768  var i: integer;
3769      IBFieldDef: TIBFieldDef;
3770  begin
3771    for i := 0 to Fields.Count - 1 do
3772      if Fields[i].FieldNo > 0 then
3773      begin
3774        if(Fields[i] is TIBStringField) then
3775        with TIBStringField(Fields[i]) do
3776        begin
3777          IBFieldDef := GetFieldDef(FieldNo);
3778          if IBFieldDef <> nil then
3779          begin
3780            CharacterSetSize := IBFieldDef.CharacterSetSize;
3781            CharacterSetName := IBFieldDef.CharacterSetName;
3782          end;
3783        end
3784        else
3785        if(Fields[i] is TIBWideStringField) then
3786        with TIBWideStringField(Fields[i]) do
3787        begin
3788          IBFieldDef := GetFieldDef(FieldNo);
3789          if IBFieldDef <> nil then
3790          begin
3791            CharacterSetSize := IBFieldDef.CharacterSetSize;
3792            CharacterSetName := IBFieldDef.CharacterSetName;
3793          end;
3794        end
3795        else
3796        if(Fields[i] is TIBMemoField) then
3797        with TIBMemoField(Fields[i]) do
3798        begin
3799          IBFieldDef := GetFieldDef(FieldNo);
3800          if IBFieldDef <> nil then
3801          begin
3802            CharacterSetSize := IBFieldDef.CharacterSetSize;
3803            CharacterSetName := IBFieldDef.CharacterSetName;
3804          end;
3805        end
3806        else
3807        if(Fields[i] is TIBWideMemoField) then
3808        with TIBWideMemoField(Fields[i]) do
3809        begin
3810          IBFieldDef := GetFieldDef(FieldNo);
3811          if IBFieldDef <> nil then
3812          begin
3813            CharacterSetSize := IBFieldDef.CharacterSetSize;
3814            CharacterSetName := IBFieldDef.CharacterSetName;
3815          end;
3816        end
3817      end
3818  end;
3819
4081   begin
4082    FBase.SetCursor;
4083    try
# Line 3826 | Line 4087 | begin
4087        IBError(ibxeEmptyQuery, [nil]);
4088      if not FInternalPrepared then
4089        InternalPrepare;
4090 <   if FQSelect.SQLType = SQLSelect then
4090 >   if FQSelect.SQLStatementType = SQLSelect then
4091     begin
4092        if DefaultFields then
4093          CreateFields;
4094 +      FArrayFieldCount := 0;
4095        BindFields(True);
3834      SetExtendedProperties;
4096        FCurrentRecord := -1;
4097        FQSelect.ExecQuery;
4098        FOpen := FQSelect.Open;
# Line 3842 | Line 4103 | begin
4103          3. After the dummy fetch, FRecordSize will be appropriately
4104             adjusted to reflect the additional "weight" of the field
4105             data.
4106 <        4. Set up the FCalcFieldsOffset, FBlobCacheOffset and FRecordBufferSize.
4106 >        4. Set up the FCalcFieldsOffset, FBlobCacheOffset, FArrayCacheOffset and FRecordBufferSize.
4107          5. Now, with the BufferSize available, allocate memory for chunks of records
4108          6. Re-allocate the model buffer, accounting for the new
4109             FRecordBufferSize.
4110          7. Finally, calls to AllocRecordBuffer will work!.
4111         }
4112        {Step 1}
4113 <      FRecordSize := RecordDataLength(FQSelect.Current.Count);
4113 >      FRecordSize := RecordDataLength(FQSelect.FieldCount);
4114        {Step 2, 3}
4115 +      GetMem(FFieldColumns,sizeof(TFieldColumns) * (FQSelect.FieldCount));
4116        IBAlloc(FModelBuffer, 0, FRecordSize);
4117 <      FetchCurrentRecordToBuffer(FQSelect, -1, FModelBuffer);
4117 >      InitModelBuffer(FQSelect, FModelBuffer);
4118        {Step 4}
4119        FCalcFieldsOffset := FRecordSize;
4120        FBlobCacheOffset := FCalcFieldsOffset + CalcFieldsSize;
4121 <      FRecordBufferSize := (FBlobCacheOffset + (BlobFieldCount * SizeOf(TIBBlobStream)));
4121 >      FArrayCacheOffset := (FBlobCacheOffset + (BlobFieldCount * SizeOf(TIBBlobStream)));
4122 >      FRecordBufferSize := FArrayCacheOffset + (ArrayFieldCount * sizeof(IArray));
4123        {Step 5}
4124        if UniDirectional then
4125          FBufferChunkSize := FRecordBufferSize * UniCache
# Line 3872 | Line 4135 | begin
4135        FCacheSize := FBufferChunkSize;
4136        FOldCacheSize := FBufferChunkSize;
4137        {Step 6}
4138 <      IBAlloc(FModelBuffer, RecordDataLength(FQSelect.Current.Count),
4138 >      IBAlloc(FModelBuffer, RecordDataLength(FQSelect.FieldCount),
4139                               FRecordBufferSize);
4140        {Step 7}
4141        FOldBuffer := AllocRecordBuffer;
# Line 3970 | Line 4233 | end;
4233   function TIBCustomDataSet.Locate(const KeyFields: string; const KeyValues: Variant;
4234                                   Options: TLocateOptions): Boolean;
4235   var
3973  {$IF FPC_FULLVERSION >= 20700 }
4236    CurBookmark: TBookmark;
3975  {$ELSE}
3976  CurBookmark: string;
3977  {$ENDIF}
4237   begin
4238    DisableControls;
4239    try
# Line 3992 | Line 4251 | function TIBCustomDataSet.Lookup(const K
4251                                   const ResultFields: string): Variant;
4252   var
4253    fl: TList;
3995  {$IF FPC_FULLVERSION >= 20700 }
4254    CurBookmark: TBookmark;
3997  {$ELSE}
3998  CurBookmark: string;
3999  {$ENDIF}
4255   begin
4256    DisableControls;
4257    fl := TList.Create;
# Line 4069 | Line 4324 | begin
4324        MappedFieldPos := FMappedFieldPosition[Field.FieldNo - 1];
4325        if (MappedFieldPos > 0) and
4326           (MappedFieldPos <= rdFieldCount) then
4327 +      with rdFields[MappedFieldPos], FFieldColumns^[MappedFieldPos] do
4328        begin
4329          Field.Validate(Buffer);
4330          if (Buffer = nil) or
4331             (Field is TIBStringField) and (PChar(Buffer)[0] = #0) then
4332 <          rdFields[MappedFieldPos].fdIsNull := True
4333 <        else begin
4334 <          Move(Buffer^, Buff[rdFields[MappedFieldPos].fdDataOfs],
4335 <                 rdFields[MappedFieldPos].fdDataSize);
4336 <          if (rdFields[MappedFieldPos].fdDataType = SQL_TEXT) or
4337 <             (rdFields[MappedFieldPos].fdDataType = SQL_VARYING) then
4338 <            rdFields[MappedFieldPos].fdDataLength := StrLen(PChar(Buffer));
4083 <          rdFields[MappedFieldPos].fdIsNull := False;
4332 >          fdIsNull := True
4333 >        else
4334 >        begin
4335 >          Move(Buffer^, Buff[fdDataOfs],fdDataSize);
4336 >          if (fdDataType = SQL_TEXT) or (fdDataType = SQL_VARYING) then
4337 >            fdDataLength := StrLen(PChar(Buffer));
4338 >          fdIsNull := False;
4339            if rdUpdateStatus = usUnmodified then
4340            begin
4341              if CachedUpdates then
# Line 4181 | Line 4436 | begin
4436    if FInternalPrepared then
4437    begin
4438      CheckDatasetClosed;
4439 +    if FDidActivate then
4440 +      DeactivateTransaction;
4441      FieldDefs.Clear;
4442      FieldDefs.Updated := false;
4443      FInternalPrepared := False;
# Line 4201 | Line 4458 | begin
4458        IBError(ibxeEmptyQuery, [nil]);
4459      if not FInternalPrepared then
4460        InternalPrepare;
4461 <    if FQSelect.SQLType = SQLSelect then
4461 >    if FQSelect.SQLStatementType = SQLSelect then
4462      begin
4463        IBError(ibxeIsASelectStatement, [nil]);
4464      end
# Line 4214 | Line 4471 | begin
4471    end;
4472   end;
4473  
4474 < function TIBCustomDataSet.GetSelectStmtHandle: TISC_STMT_HANDLE;
4474 > function TIBCustomDataSet.GetSelectStmtIntf: IStatement;
4475   begin
4476 <  Result := FQSelect.Handle;
4476 >  Result := FQSelect.Statement;
4477   end;
4478  
4479   function TIBCustomDataSet.GetParser: TSelectSQLParser;
# Line 4271 | Line 4528 | end;
4528  
4529   { TIBDataSet IProviderSupport }
4530  
4531 < (*procedure TIBCustomDataSet.PSEndTransaction(Commit: Boolean);
4531 > procedure TIBCustomDataSet.PSEndTransaction(Commit: Boolean);
4532   begin
4533    if Commit then
4534      Transaction.Commit else
# Line 4434 | Line 4691 | begin
4691    if not FQSelect.Prepared then
4692      FQSelect.Prepare;
4693    Result := FQSelect.UniqueRelationName;
4694 < end;*)
4694 > end;
4695  
4696   procedure TIBDataSet.BatchInput(InputObject: TIBBatchInput);
4697   begin
# Line 4576 | Line 4833 | begin
4833    DataSet.SetInternalSQLParams(Query, buff);
4834   end;
4835  
4836 + function TIBDSBlobStream.GetSize: Int64;
4837 + begin
4838 +  Result := FBlobStream.BlobSize;
4839 + end;
4840 +
4841   { TIBDSBlobStream }
4842   constructor TIBDSBlobStream.Create(AField: TField; ABlobStream: TIBBlobStream;
4843                                      Mode: TBlobStreamMode);
# Line 4584 | Line 4846 | begin
4846    FBlobStream := ABlobStream;
4847    FBlobStream.Seek(0, soFromBeginning);
4848    if (Mode = bmWrite) then
4849 +  begin
4850      FBlobStream.Truncate;
4851 +    TIBCustomDataSet(FField.DataSet).RecordModified(True);
4852 +    TBlobField(FField).Modified := true;
4853 +    FHasWritten := true;
4854 +  end;
4855   end;
4856  
4857   destructor TIBDSBlobStream.Destroy;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines