1 Star 0 Fork 0

Ny/mORMot

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
SynVirtualDataSet.pas 31.90 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
/// DB VCL read-only virtual dataset
// - this unit is a part of the freeware Synopse framework,
// licensed under a MPL/GPL/LGPL tri-license; version 1.18
unit SynVirtualDataSet;
{
This file is part of Synopse framework.
Synopse framework. Copyright (c) Arnaud Bouchez
Synopse Informatique - https://synopse.info
*** BEGIN LICENSE BLOCK *****
Version: MPL 1.1/GPL 2.0/LGPL 2.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the License.
The Original Code is Synopse mORMot framework.
The Initial Developer of the Original Code is Arnaud Bouchez.
Portions created by the Initial Developer are Copyright (c)
the Initial Developer. All Rights Reserved.
Contributor(s):
- Alfred Glaenzer (alf)
- Esteban Martin (EMartin)
- mingda
- Murat Ak
- Valentin (StxLog)
Alternatively, the contents of this file may be used under the terms of
either the GNU General Public License Version 2 or later (the "GPL"), or
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
in which case the provisions of the GPL or the LGPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of either the GPL or the LGPL, and not to allow others to
use your version of this file under the terms of the MPL, indicate your
decision by deleting the provisions above and replace them with the notice
and other provisions required by the GPL or the LGPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the MPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****
}
{$I Synopse.inc} // define HASINLINE CPU32 CPU64 OWNNORMTOUPPER
interface
uses
SysUtils,
Classes,
{$ifndef FPC}
Contnrs,
{$endif}
{$ifndef NOVARIANTS}
Variants,
{$endif}
SynCommons,
SynTable,
{$ifdef ISDELPHIXE2}
System.Generics.Collections,
Data.DB, Data.FMTBcd;
{$else}
DB, FMTBcd;
{$endif}
type
{$ifndef UNICODE} // defined as TRecordBuffer = PByte in newer DB.pas
TRecordBuffer = PChar;
{$endif UNICODE}
PDateTimeRec = ^TDateTimeRec;
/// read-only virtual TDataSet able to access any content
TSynVirtualDataSet = class(TDataSet)
protected
fCurrentRow: integer;
fIsCursorOpen: boolean;
// TDataSet overridden methods
function AllocRecordBuffer: TRecordBuffer; override;
procedure FreeRecordBuffer(var Buffer: TRecordBuffer); override;
procedure InternalInitRecord(Buffer: TRecordBuffer); override;
function GetCanModify: Boolean; override;
procedure GetBookmarkData(Buffer: TRecordBuffer; Data: Pointer); override;
function GetBookmarkFlag(Buffer: TRecordBuffer): TBookmarkFlag; override;
function GetRecord(Buffer: TRecordBuffer; GetMode: TGetMode; DoCheck: Boolean): TGetResult; override;
function GetRecordSize: Word; override;
procedure InternalClose; override;
procedure InternalFirst; override;
procedure InternalGotoBookmark(Bookmark: Pointer); override;
procedure InternalHandleException; override;
procedure InternalLast; override;
procedure InternalSetToRecord(Buffer: TRecordBuffer); override;
function IsCursorOpen: Boolean; override;
procedure SetBookmarkFlag(Buffer: TRecordBuffer; Value: TBookmarkFlag); override;
procedure SetBookmarkData(Buffer: TRecordBuffer; Data: Pointer); override;
procedure SetRecNo(Value: Integer); override;
function GetRecNo: Integer; override;
// classses should override all those following methods:
// - to read the data e.g. into memory:
procedure InternalOpen; override;
// - to initialize FieldDefs:
// procedure InternalInitFieldDefs; override;
// - to return row count:
// function GetRecordCount: Integer; override;
// - result should point to Int64,Double,Blob,UTF8 data (if ResultLen<>nil)
function GetRowFieldData(Field: TField; RowIndex: integer; out ResultLen: Integer;
OnlyCheckNull: boolean): Pointer; virtual; abstract;
// - to search for a field, returning RecNo (0 = not found by default)
function SearchForField(const aLookupFieldName: RawUTF8; const aLookupValue: variant;
aOptions: TLocateOptions): integer; virtual;
{$ifndef NOVARIANTS}
// used to serialize TBCDVariant as JSON - BcdRead will always fail
class procedure BcdWrite(const aWriter: TTextWriter; const aValue);
//class function BcdRead(P: PUTF8Char; var aValue; out aValid: Boolean): PUTF8Char;
{$endif}
public
/// this overridden constructor will compute an unique Name property
constructor Create(Owner: TComponent); override;
/// get BLOB column data for the current active row
// - handle ftBlob,ftMemo,ftWideMemo via GetRowFieldData()
function CreateBlobStream(Field: TField; Mode: TBlobStreamMode): TStream; override;
/// get BLOB column data for a given row (may not the active row)
// - handle ftBlob,ftMemo,ftWideMemo via GetRowFieldData()
function GetBlobStream(Field: TField; RowIndex: integer): TStream;
/// get column data for the current active row
// - handle ftBoolean,ftInteger,ftLargeint,ftFloat,ftCurrency,ftDate,ftTime,
// ftDateTime,ftString,ftWideString kind of fields via GetRowFieldData()
{$ifdef ISDELPHIXE3}
{$ifdef ISDELPHIXE4}
function GetFieldData(Field: TField; var Buffer: TValueBuffer): Boolean; override;
{$else}
function GetFieldData(Field: TField; Buffer: TValueBuffer): Boolean; override;
{$endif}
{$else}
function GetFieldData(Field: TField; Buffer: pointer): Boolean; override;
{$endif}
{$ifndef UNICODE}
function GetFieldData(Field: TField; Buffer: pointer; NativeFormat: Boolean): Boolean; override;
{$endif}
/// searching a dataset for a specified record and making it the active record
// - will call SearchForField protected virtual method for actual lookup
function Locate(const KeyFields: string; const KeyValues: Variant;
Options: TLocateOptions) : boolean; override;
published
property Active;
property BeforeOpen;
property AfterOpen;
property BeforeClose;
property AfterClose;
property BeforeInsert;
property AfterInsert;
property BeforeEdit;
property AfterEdit;
property BeforePost;
property AfterPost;
property BeforeCancel;
property AfterCancel;
property BeforeDelete;
property AfterDelete;
property BeforeScroll;
property AfterScroll;
property OnCalcFields;
property OnDeleteError;
property OnEditError;
property OnFilterRecord;
property OnNewRecord;
property OnPostError;
end;
{$ifndef NOVARIANTS}
/// read-only virtual TDataSet able to access a dynamic array of TDocVariant
// - could be used e.g. from the result of TMongoCollection.FindDocs() to
// avoid most temporary conversion into JSON or TClientDataSet buffers
TDocVariantArrayDataSet = class(TSynVirtualDataSet)
protected
fValues: TVariantDynArray;
fColumns: array of record
Name: RawUTF8;
FieldType: TSQLDBFieldType;
end;
fTemp64: Int64;
fTempUTF8: RawUTF8;
fTempBlob: RawByteString;
procedure InternalInitFieldDefs; override;
function GetRecordCount: Integer; override;
function GetRowFieldData(Field: TField; RowIndex: integer;
out ResultLen: Integer; OnlyCheckNull: boolean): Pointer; override;
function SearchForField(const aLookupFieldName: RawUTF8; const aLookupValue: variant;
aOptions: TLocateOptions): integer; override;
public
/// initialize the virtual TDataSet from a dynamic array of TDocVariant
// - you can set the expected column names and types matching the results
// document layout - if no column information is specified, the first
// TDocVariant will be used as reference
constructor Create(Owner: TComponent; const Data: TVariantDynArray;
const ColumnNames: array of RawUTF8; const ColumnTypes: array of TSQLDBFieldType); reintroduce;
end;
{$endif}
const
/// map the VCL string type, depending on the Delphi compiler version
{$ifdef UNICODE}
ftDefaultVCLString = ftWideString;
{$else}
ftDefaultVCLString = ftString;
{$endif}
/// map the best ft*Memo type available, depending on the Delphi compiler version
{$ifdef ISDELPHI2007ANDUP}
ftDefaultMemo = ftWideMemo;
{$else}
ftDefaultMemo = ftMemo;
{$endif}
/// append a TBcd value as text to the output buffer
// - very optimized for speed
procedure AddBcd(WR: TTextWriter; const AValue: TBcd);
type
/// a string buffer, used by InternalBCDToBuffer to store its output text
TBCDBuffer = array[0..66] of AnsiChar;
/// convert a TBcd value as text to the output buffer
// - buffer is to be array[0..66] of AnsiChar
// - returns the resulting text start in PBeg, and the length as function result
// - does not handle negative sign and 0 value - see AddBcd() function use case
// - very optimized for speed
function InternalBCDToBuffer(const AValue: TBcd; out ADest: TBCDBuffer; var PBeg: PAnsiChar): integer;
/// convert a TBcd value into a currency
// - purepascal version included in latest Delphi versions is slower than this
function BCDToCurr(const AValue: TBcd; var Curr: Currency): boolean;
/// convert a TBcd value into a RawUTF8 text
// - will call fast InternalBCDToBuffer function
procedure BCDToUTF8(const AValue: TBcd; var result: RawUTF8); overload;
/// convert a TBcd value into a RawUTF8 text
// - will call fast InternalBCDToBuffer function
function BCDToUTF8(const AValue: TBcd): RawUTF8; overload;
{$ifdef HASINLINE}inline;{$endif}
/// convert a TBcd value into a VCL string text
// - will call fast InternalBCDToBuffer function
function BCDToString(const AValue: TBcd): string;
/// export all rows of a TDataSet into JSON
// - will work for any kind of TDataSet
function DataSetToJSON(Data: TDataSet): RawUTF8;
{$ifndef NOVARIANTS}
/// convert a dynamic array of TDocVariant result into a VCL DataSet
// - this function is just a wrapper around TDocVariantArrayDataSet.Create()
// - the TDataSet will be opened once created
function ToDataSet(aOwner: TComponent; const Data: TVariantDynArray;
const ColumnNames: array of RawUTF8; const ColumnTypes: array of TSQLDBFieldType): TDocVariantArrayDataSet; overload;
{$endif}
implementation
function InternalBCDToBuffer(const AValue: TBcd; out ADest: TBCDBuffer; var PBeg: PAnsiChar): integer;
var i,DecimalPos: integer;
P,Frac: PByte;
PEnd: PAnsiChar;
begin
result := 0;
if AValue.Precision=0 then
exit;
DecimalPos := AValue.Precision-(AValue.SignSpecialPlaces and $3F);
P := @ADest;
Frac := @Avalue.Fraction;
for i := 0 to AValue.Precision-1 do begin
if i=DecimalPos then
if i=0 then begin
PWord(P)^ := ord('0')+ord('.')shl 8;
inc(P,2);
end else begin
P^ := ord('.');
inc(P);
end;
if (i and 1)=0 then
P^ := ((Frac^ and $F0) shr 4)+ord('0') else begin
P^ := ((Frac^ and $0F))+ord('0');
inc(Frac);
end;
inc(P);
end;
// remove trailing 0 after decimal
if AValue.Precision>DecimalPos then begin
repeat dec(P) until (P^<>ord('0')) or (P=@ADest);
PEnd := pointer(P);
if PEnd^<>'.' then
inc(PEnd);
end else
PEnd := pointer(P);
PEnd^ := #0;
// remove leading 0
PBeg := @ADest;
while (PBeg[0]='0') and (PBeg[1] in ['0'..'9']) do inc(PBeg);
result := PEnd-PBeg;
end;
procedure AddBcd(WR: TTextWriter; const AValue: TBcd);
var len: integer;
PBeg: PAnsiChar;
tmp: TBCDBuffer;
begin
len := InternalBCDToBuffer(AValue,tmp,PBeg);
if len<=0 then
WR.Add('0') else begin
if AValue.SignSpecialPlaces and $80=$80 then
WR.Add('-');
WR.AddNoJSONEscape(PBeg,len);
end;
end;
function BCDToCurr(const AValue: TBcd; var Curr: Currency): boolean;
var len: integer;
PBeg: PAnsiChar;
tmp: TBCDBuffer;
begin
len := InternalBCDToBuffer(AValue,tmp,PBeg);
if len<=0 then
Curr := 0 else begin
PInt64(@Curr)^ := StrToCurr64(pointer(PBeg));
if AValue.SignSpecialPlaces and $80=$80 then
Curr := -Curr;
end;
result := true;
end;
procedure BCDToUTF8(const AValue: TBcd; var result: RawUTF8);
var len: integer;
PBeg: PAnsiChar;
tmp: TBCDBuffer;
begin
len := InternalBCDToBuffer(AValue,tmp,PBeg);
SetString(result,PBeg,len);
end;
function BCDToUTF8(const AValue: TBcd): RawUTF8;
begin
BCDToUTF8(AValue,result);
end;
function BCDToString(const AValue: TBcd): string;
var len: integer;
PBeg: PAnsiChar;
tmp: TBCDBuffer;
begin
len := InternalBCDToBuffer(AValue,tmp,PBeg);
Ansi7ToString(PWinAnsiChar(PBeg),len,result);
end;
var
GlobalDataSetCount: integer;
type
/// define how a single row is identified
// - for TSynVirtualDataSet, it is just the row index (starting at 0)
TRecInfoIdentifier = integer;
PRecInfoIdentifier = ^TRecInfoIdentifier;
/// pointer to an internal structure used to identify a row position
PRecInfo = ^TRecInfo;
/// internal structure used to identify a row position
TRecInfo = record
/// define how a single row is identified
RowIndentifier: TRecInfoIdentifier;
/// any associated bookmark
Bookmark: TRecInfoIdentifier;
/// any associated bookmark flag
BookmarkFlag: TBookmarkFlag;
end;
{ TSynVirtualDataSet }
function TSynVirtualDataSet.AllocRecordBuffer: TRecordBuffer;
begin
result := AllocMem(sizeof(TRecInfo));
end;
procedure TSynVirtualDataSet.FreeRecordBuffer(var Buffer: TRecordBuffer);
begin
FreeMem(Buffer);
Buffer := nil;
end;
procedure TSynVirtualDataSet.GetBookmarkData(Buffer: TRecordBuffer; Data: Pointer);
begin
PRecInfoIdentifier(Data)^ := PRecInfo(Buffer)^.Bookmark;
end;
function TSynVirtualDataSet.GetBookmarkFlag(Buffer: TRecordBuffer): TBookmarkFlag;
begin
result := PRecInfo(Buffer)^.BookmarkFlag;
end;
function TSynVirtualDataSet.GetCanModify: Boolean;
begin
result := false; // we define a READ-ONLY TDataSet
end;
{$ifndef UNICODE}
function TSynVirtualDataSet.GetFieldData(Field: TField; Buffer: Pointer;
NativeFormat: Boolean): Boolean;
begin
if Field.DataType in [ftWideString] then
NativeFormat := true; // to force Buffer as PWideString
Result := inherited GetFieldData(Field, Buffer, NativeFormat);
end;
{$endif}
{$ifdef ISDELPHIXE3}
{$ifdef ISDELPHIXE4}
function TSynVirtualDataSet.GetFieldData(Field: TField; var Buffer: TValueBuffer): Boolean;
{$else}
function TSynVirtualDataSet.GetFieldData(Field: TField; Buffer: TValueBuffer): Boolean;
{$endif}
{$else}
function TSynVirtualDataSet.GetFieldData(Field: TField; Buffer: Pointer): Boolean;
{$endif}
var Data, Dest: pointer;
RowIndex, DataLen, MaxLen: integer;
Temp: RawByteString;
OnlyTestForNull: boolean;
TS: TTimeStamp;
begin
OnlyTestForNull := (Buffer=nil);
RowIndex := PRecInfo(ActiveBuffer).RowIndentifier;
Data := GetRowFieldData(Field,RowIndex,DataLen,OnlyTestForNull);
result := Data<>nil; // null field or out-of-range RowIndex/Field
if OnlyTestForNull or not result then
exit;
Dest := pointer(Buffer); // works also if Buffer is [var] TValueBuffer
case Field.DataType of // Data^ points to Int64,Double,Blob,UTF8
ftBoolean:
PWORDBOOL(Dest)^ := PBoolean(Data)^;
ftInteger:
PInteger(Dest)^ := PInteger(Data)^;
ftLargeint, ftFloat, ftCurrency:
PInt64(Dest)^ := PInt64(Data)^;
ftDate, ftTime, ftDateTime:
if PDateTime(Data)^=0 then // handle 30/12/1899 date as NULL
result := false else begin // inlined DataConvert(Field,Data,Dest,true)
TS := DateTimeToTimeStamp(PDateTime(Data)^);
case Field.DataType of
ftDate: PDateTimeRec(Dest)^.Date := TS.Date;
ftTime: PDateTimeRec(Dest)^.Time := TS.Time;
ftDateTime:
if (TS.Time<0) or (TS.Date<=0) then
result := false else // matches ValidateTimeStamp() expectations
PDateTimeRec(Dest)^.DateTime := TimeStampToMSecs(TS);
end; // see NativeToDateTime/DateTimeToNative in TDataSet.DataConvert
end;
ftString: begin
if DataLen<>0 then begin
CurrentAnsiConvert.UTF8BufferToAnsi(Data,DataLen,Temp);
DataLen := length(Temp);
MaxLen := Field.DataSize-1; // without trailing #0
if DataLen>MaxLen then
DataLen := MaxLen;
move(pointer(Temp)^,Dest^,DataLen);
end;
PAnsiChar(Dest)[DataLen] := #0;
end;
ftWideString: begin
{$ifdef ISDELPHI2007ANDUP} // here Dest = PWideChar[] of DataSize bytes
if DataLen=0 then
PWideChar(Dest)^ := #0 else
UTF8ToWideChar(Dest,Data,(Field.DataSize-2)shr 1,DataLen);
{$else} // here Dest is PWideString
UTF8ToWideString(Data,DataLen,WideString(Dest^));
{$endif}
end;
// ftBlob,ftMemo,ftWideMemo should be retrieved by CreateBlobStream()
else raise EDatabaseError.CreateFmt('%s.GetFieldData unhandled DataType=%s (%d)',
[ClassName,GetEnumName(TypeInfo(TFieldType),ord(Field.DataType))^,ord(Field.DataType)]);
end;
end;
function TSynVirtualDataSet.GetBlobStream(Field: TField; RowIndex: integer): TStream;
var Data: pointer;
DataLen: integer;
begin
Data := GetRowFieldData(Field,RowIndex,DataLen,false);
if Data=nil then // should point to Blob or UTF8 data
result := nil else
case Field.DataType of
ftBlob:
result := TSynMemoryStream.Create(Data,DataLen);
ftMemo, ftString:
result := TRawByteStringStream.Create(CurrentAnsiConvert.UTF8BufferToAnsi(Data,DataLen));
{$ifdef ISDELPHI2007ANDUP} ftWideMemo, {$endif} ftWideString:
result := TRawByteStringStream.Create(Utf8DecodeToRawUnicode(Data,DataLen));
else raise EDatabaseError.CreateFmt('%s.CreateBlobStream DataType=%d',
[ClassName,ord(Field.DataType)]);
end;
end;
function TSynVirtualDataSet.CreateBlobStream(Field: TField; Mode: TBlobStreamMode): TStream;
begin
if Mode<>bmRead then
raise EDatabaseError.CreateFmt('%s BLOB should be ReadOnly',[ClassName]);
result := GetBlobStream(Field,PRecInfo(ActiveBuffer).RowIndentifier);
if result=nil then
result := TSynMemoryStream.Create; // null BLOB returns a void TStream
end;
function TSynVirtualDataSet.GetRecNo: Integer;
begin
result := fCurrentRow+1;
end;
function TSynVirtualDataSet.GetRecord(Buffer: TRecordBuffer; GetMode: TGetMode;
DoCheck: Boolean): TGetResult;
begin
result := grOK;
case GetMode of
gmPrior:
if fCurrentRow>0 then
dec(fCurrentRow) else
result := grBOF;
gmCurrent:
if fCurrentRow<0 then
result := grBOF else
if fCurrentRow>=GetRecordCount then
result := grEOF;
gmNext:
if fCurrentRow<GetRecordCount-1 then
inc(fCurrentRow) else
result := grEOF;
end;
if result=grOK then
with PRecInfo(Buffer)^ do begin
RowIndentifier := fCurrentRow;
BookmarkFlag := bfCurrent;
Bookmark := fCurrentRow;
end;
end;
function TSynVirtualDataSet.GetRecordSize: Word;
begin
result := SizeOf(TRecInfoIdentifier); // excluding Bookmark information
end;
procedure TSynVirtualDataSet.InternalClose;
begin
BindFields(false);
{$ifdef ISDELPHIXE6}
if not(lcPersistent in Fields.LifeCycles) then
{$else}
if DefaultFields then
{$endif}
DestroyFields;
fIsCursorOpen := False;
end;
procedure TSynVirtualDataSet.InternalFirst;
begin
fCurrentRow := -1;
end;
procedure TSynVirtualDataSet.InternalGotoBookmark(Bookmark: Pointer);
begin
fCurrentRow := PRecInfoIdentifier(Bookmark)^;
end;
procedure TSynVirtualDataSet.InternalHandleException;
begin
if Assigned(Classes.ApplicationHandleException) then
Classes.ApplicationHandleException(ExceptObject) else
SysUtils.ShowException(ExceptObject,ExceptAddr);
end;
procedure TSynVirtualDataSet.InternalInitRecord(Buffer: TRecordBuffer);
begin
FillcharFast(Buffer^,sizeof(TRecInfo),0);
end;
procedure TSynVirtualDataSet.InternalLast;
begin
fCurrentRow := GetRecordCount;
end;
procedure TSynVirtualDataSet.InternalOpen;
begin
BookmarkSize := SizeOf(TRecInfo)-sizeof(TRecInfoIdentifier);
InternalInitFieldDefs;
{$ifdef ISDELPHIXE6}
if not(lcPersistent in Fields.LifeCycles) then
{$else}
if DefaultFields then
{$endif}
CreateFields;
BindFields(true);
fCurrentRow := -1;
fIsCursorOpen := True;
end;
procedure TSynVirtualDataSet.InternalSetToRecord(Buffer: TRecordBuffer);
begin
fCurrentRow := PRecInfo(Buffer).RowIndentifier;
end;
function TSynVirtualDataSet.IsCursorOpen: Boolean;
begin
result := fIsCursorOpen;
end;
procedure TSynVirtualDataSet.SetBookmarkData(Buffer: TRecordBuffer; Data: Pointer);
begin
PRecInfo(Buffer)^.Bookmark := PRecInfoIdentifier(Data)^;
end;
procedure TSynVirtualDataSet.SetBookmarkFlag(Buffer: TRecordBuffer; Value: TBookmarkFlag);
begin
PRecInfo(Buffer)^.BookmarkFlag := Value;
end;
procedure TSynVirtualDataSet.SetRecNo(Value: Integer);
begin
CheckBrowseMode;
if Value<>RecNo then begin
dec(Value);
if cardinal(Value)>=cardinal(GetRecordCount) then
raise ERangeError.CreateFmt('%s.SetRecNo(%d) with Count=%d',
[ClassName,Value+1,GetRecordCount]);
DoBeforeScroll;
fCurrentRow := Value;
Resync([rmCenter]);
DoAfterScroll;
end;
end;
constructor TSynVirtualDataSet.Create(Owner: TComponent);
begin
inherited Create(Owner);
inc(GlobalDataSetCount);
Name := ClassName+IntToStr(GlobalDataSetCount); // force unique name
end;
function TSynVirtualDataSet.SearchForField(const aLookupFieldName: RawUTF8;
const aLookupValue: variant; aOptions: TLocateOptions): integer;
begin
result := 0; // nothing found
end;
function TSynVirtualDataSet.Locate(const KeyFields: string;
const KeyValues: Variant; Options: TLocateOptions) : boolean;
var i, l, h, found: Integer;
{$ifdef ISDELPHIXE4}
FieldList: TList<TField>;
{$else}
FieldList: TList;
{$endif}
begin
CheckActive;
result := true;
if not IsEmpty then
if VarIsArray(KeyValues) then begin
{$ifdef ISDELPHIXE4}
FieldList := TList<TField>.Create;
{$else}
FieldList := TList.Create;
{$endif}
try
GetFieldList(FieldList,KeyFields);
l := VarArrayLowBound(KeyValues,1);
h := VarArrayHighBound(KeyValues,1);
if (FieldList.Count = 1) and (l < h) then begin
found := SearchForField(StringToUTF8(KeyFields),KeyValues,Options);
if found>0 then begin
RecNo := found;
exit;
end;
end
else for i := 0 to FieldList.Count - 1 do begin
found := SearchForField(StringToUTF8(TField(FieldList[i]).FieldName),
KeyValues[l+i],Options);
if found>0 then begin
RecNo := found;
exit;
end;
end;
finally
FieldList.Free;
end;
end else begin
found := SearchForField(StringToUTF8(KeyFields),KeyValues,Options);
if found>0 then begin
RecNo := found;
exit;
end;
end;
result := false;
end;
{$ifndef NOVARIANTS}
type // as in FMTBcd.pas
TFMTBcdData = class(TPersistent)
private
FBcd: TBcd;
end;
TFMTBcdVarData = packed record
VType: TVarType;
Reserved1, Reserved2, Reserved3: Word;
VBcd: TFMTBcdData;
Reserved4: Cardinal;
end;
class procedure TSynVirtualDataSet.BcdWrite(const aWriter: TTextWriter; const aValue);
begin
AddBCD(aWriter,TFMTBcdVarData(aValue).VBcd.FBcd);
end;
{$endif NOVARIANTS}
function DataSetToJSON(Data: TDataSet): RawUTF8;
var W: TJSONWriter;
f: integer;
blob: TRawByteStringStream;
begin
result := 'null';
if Data=nil then
exit;
Data.First;
if Data.Eof then
exit;
W := TJSONWriter.Create(nil,true,false);
try
// get col names and types
SetLength(W.ColNames,Data.FieldCount);
for f := 0 to high(W.ColNames) do
StringToUTF8(Data.FieldDefs[f].Name,W.ColNames[f]);
W.AddColumns;
W.Add('[');
repeat
W.Add('{');
for f := 0 to Data.FieldCount-1 do begin
W.AddString(W.ColNames[f]);
with Data.Fields[f] do
if IsNull then
W.AddShort('null') else
case DataType of
ftBoolean:
W.Add(AsBoolean);
ftSmallint, ftInteger, ftWord, ftAutoInc:
W.Add(AsInteger);
ftLargeint:
W.Add(TLargeintField(Data.Fields[f]).AsLargeInt);
ftFloat, ftCurrency: // TCurrencyField is sadly a TFloatField
W.Add(AsFloat,TFloatField(Data.Fields[f]).Precision);
ftBCD:
W.AddCurr64(AsCurrency);
ftFMTBcd:
AddBcd(W,AsBCD);
ftTimeStamp, ftDate, ftTime, ftDateTime: begin
W.Add('"');
W.AddDateTime(AsDateTime);
W.Add('"');
end;
ftString, ftFixedChar, ftMemo, ftGuid: begin
W.Add('"');
W.AddAnsiString({$ifdef UNICODE}AsAnsiString{$else}AsString{$endif},
twJSONEscape);
W.Add('"');
end;
ftWideString: begin
W.Add('"');
W.AddJSONEscapeW(pointer(TWideStringField(Data.Fields[f]).Value));
W.Add('"');
end;
ftVariant:
W.AddVariant(AsVariant);
ftBytes, ftVarBytes, ftBlob, ftGraphic, ftOraBlob, ftOraClob: begin
blob := TRawByteStringStream.Create;
try
(Data.Fields[f] as TBlobField).SaveToStream(blob);
W.WrBase64(pointer(blob.DataString),length(blob.DataString),true);
finally
blob.Free;
end;
end;
{$ifdef ISDELPHI2007ANDUP}
ftWideMemo, ftFixedWideChar: begin
W.Add('"');
W.AddJSONEscapeW(pointer(AsWideString));
W.Add('"');
end;
{$endif}
{$ifdef UNICODE}
ftShortint, ftByte:
W.Add(AsInteger);
ftLongWord:
W.AddU(TLongWordField(Data.Fields[f]).Value);
ftExtended:
W.AddDouble(AsFloat);
ftSingle:
W.Add(AsFloat,SINGLE_PRECISION);
{$endif}
else W.AddShort('null'); // unhandled field type
end;
W.Add(',');
end;
W.CancelLastComma;
W.Add('}',',');
Data.Next;
until Data.Eof;
W.CancelLastComma;
W.Add(']');
W.SetText(result);
finally
W.Free;
end;
end;
{ TDocVariantArrayDataSet }
constructor TDocVariantArrayDataSet.Create(Owner: TComponent;
const Data: TVariantDynArray; const ColumnNames: array of RawUTF8;
const ColumnTypes: array of TSQLDBFieldType);
var n,ndx,j: PtrInt;
first: PDocVariantData;
begin
fValues := Data;
n := Length(ColumnNames);
if n>0 then begin
if n<>length(ColumnTypes) then
raise ESynException.CreateUTF8('%.Create(ColumnNames<>ColumnTypes)',[self]);
SetLength(fColumns,n);
for ndx := 0 to n-1 do begin
fColumns[ndx].Name := ColumnNames[ndx];
fColumns[ndx].FieldType := ColumnTypes[ndx];
end;
end else
if fValues<>nil then begin
first := _Safe(fValues[0],dvObject);
SetLength(fColumns,first^.Count);
for ndx := 0 to first^.Count-1 do begin
fColumns[ndx].Name := first^.Names[ndx];
fColumns[ndx].FieldType := VariantTypeToSQLDBFieldType(first^.Values[ndx]);
case fColumns[ndx].FieldType of
SynTable.ftNull:
fColumns[ndx].FieldType := SynTable.ftBlob;
SynTable.ftCurrency:
fColumns[ndx].FieldType := SynTable.ftDouble;
SynTable.ftInt64: // ensure type coherency of whole column
for j := 1 to first^.Count-1 do
if j>=Length(fValues) then // check objects are consistent
break else
with _Safe(fValues[j],dvObject)^ do
if (ndx<Length(Names)) and IdemPropNameU(Names[ndx],fColumns[ndx].Name) then
if VariantTypeToSQLDBFieldType(Values[ndx]) in
[SynTable.ftNull,SynTable.ftDouble,SynTable.ftCurrency] then begin
fColumns[ndx].FieldType := SynTable.ftDouble;
break;
end;
end;
end;
end;
inherited Create(Owner);
end;
function TDocVariantArrayDataSet.GetRecordCount: Integer;
begin
result := length(fValues);
end;
function TDocVariantArrayDataSet.GetRowFieldData(Field: TField;
RowIndex: integer; out ResultLen: Integer; OnlyCheckNull: boolean): Pointer;
var F,ndx: integer;
wasString: Boolean;
begin
result := nil;
F := Field.Index;
if (cardinal(RowIndex)<cardinal(length(fValues))) and
(cardinal(F)<cardinal(length(fColumns))) and
not (fColumns[F].FieldType in [ftNull,SynTable.ftUnknown,SynTable.ftCurrency]) then
with _Safe(fValues[RowIndex])^ do
if (Kind=dvObject) and (Count>0) then begin
if IdemPropNameU(fColumns[F].Name,Names[F]) then
ndx := F else // optimistic match
ndx := GetValueIndex(fColumns[F].Name);
if ndx>=0 then
if VarIsEmptyOrNull(Values[ndx]) then
exit else begin
result := @fTemp64;
if not OnlyCheckNull then
case fColumns[F].FieldType of
ftInt64:
VariantToInt64(Values[ndx],fTemp64);
ftDouble,SynTable.ftDate:
VariantToDouble(Values[ndx],unaligned(PDouble(@fTemp64)^));
ftUTF8: begin
VariantToUTF8(Values[ndx],fTempUTF8,wasString);
result := pointer(fTempUTF8);
ResultLen := length(fTempUTF8);
end;
SynTable.ftBlob: begin
VariantToUTF8(Values[ndx],fTempUTF8,wasString);
if Base64MagicCheckAndDecode(pointer(fTempUTF8),length(fTempUTF8),fTempBlob) then begin
result := pointer(fTempBlob);
ResultLen := length(fTempBlob);
end;
end;
end;
end;
end;
end;
procedure TDocVariantArrayDataSet.InternalInitFieldDefs;
const TYPES: array[TSQLDBFieldType] of TFieldType = (
// ftUnknown, ftNull, ftInt64, ftDouble, ftCurrency, ftDate, ftUTF8, ftBlob
ftWideString,ftWideString,ftLargeint,ftFloat,ftFloat,ftDate,ftWideString,ftBlob);
var F,siz: integer;
begin
FieldDefs.Clear;
for F := 0 to high(fColumns) do begin
if fColumns[F].FieldType=ftUTF8 then
siz := 16 else
siz := 0;
FieldDefs.Add(UTF8ToString(fColumns[F].Name),TYPES[fColumns[F].FieldType],siz);
end;
end;
function TDocVariantArrayDataSet.SearchForField(const aLookupFieldName: RawUTF8;
const aLookupValue: variant; aOptions: TLocateOptions): integer;
var f: integer;
begin
f := -1; // allows O(1) field lookup for invariant object columns
for result := 1 to length(fValues) do
with _Safe(fValues[result-1])^ do
if (Kind=dvObject) and (Count>0) then begin
if (cardinal(f)>=cardinal(Count)) or
not IdemPropNameU(aLookupFieldName,Names[f]) then
f := GetValueIndex(aLookupFieldName);
if (f>=0) and (SortDynArrayVariantComp(TVarData(Values[f]),
TVarData(aLookupValue),loCaseInsensitive in aOptions)=0) then
exit;
end;
result := 0;
end;
function ToDataSet(aOwner: TComponent; const Data: TVariantDynArray;
const ColumnNames: array of RawUTF8; const ColumnTypes: array of TSQLDBFieldType): TDocVariantArrayDataSet; overload;
begin
result := TDocVariantArrayDataSet.Create(aOwner,Data,ColumnNames,ColumnTypes);
result.Open;
end;
initialization
{$ifndef NOVARIANTS}
TTextWriter.RegisterCustomJSONSerializerForVariantByType(
VarFMTBcd,nil,TSynVirtualDataSet.BcdWrite);
{$endif}
end.
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Delphi
1
https://gitee.com/Xdu2050/mORMot.git
git@gitee.com:Xdu2050/mORMot.git
Xdu2050
mORMot
mORMot
master

搜索帮助