fcl-db: base: introduce TParam.AsBytes + test

(FDataType is set to ftVarBytes intentionally due to Delphi compatibility)

git-svn-id: trunk@27844 -
This commit is contained in:
lacak 2014-06-03 08:33:09 +00:00
parent 62782f1904
commit ae3f0f3752
4 changed files with 45 additions and 9 deletions

View File

@ -1149,6 +1149,7 @@ type
Procedure AssignParam(Param: TParam);
Procedure AssignTo(Dest: TPersistent); override;
Function GetAsBoolean: Boolean;
Function GetAsBytes: TBytes;
Function GetAsCurrency: Currency;
Function GetAsDateTime: TDateTime;
Function GetAsFloat: Double;
@ -1164,6 +1165,7 @@ type
Procedure SetAsBCD(const AValue: Currency);
Procedure SetAsBlob(const AValue: TBlobData);
Procedure SetAsBoolean(AValue: Boolean);
Procedure SetAsBytes(const AValue: TBytes);
Procedure SetAsCurrency(const AValue: Currency);
Procedure SetAsDate(const AValue: TDateTime);
Procedure SetAsDateTime(const AValue: TDateTime);
@ -1196,9 +1198,10 @@ type
Procedure LoadFromStream(Stream: TStream; BlobType: TBlobType);
Procedure SetBlobData(Buffer: Pointer; ASize: Integer);
Procedure SetData(Buffer: Pointer);
Property AsBCD : Currency read GetAsCurrency write SetAsBCD;
Property AsBlob : TBlobData read GetAsString write SetAsBlob;
Property AsBoolean : Boolean read GetAsBoolean write SetAsBoolean;
Property AsBCD : Currency read GetAsCurrency write SetAsBCD;
Property AsBytes : TBytes read GetAsBytes write SetAsBytes;
Property AsCurrency : Currency read GetAsCurrency write SetAsCurrency;
Property AsDate : TDateTime read GetAsDateTime write SetAsDate;
Property AsDateTime : TDateTime read GetAsDateTime write SetAsDateTime;

View File

@ -500,6 +500,17 @@ begin
Result:=FValue;
end;
function TParam.GetAsBytes: TBytes;
begin
if IsNull then
Result:=nil
else if VarIsArray(FValue) then
Result:=FValue
else
// todo: conversion from other variant types to TBytes
Result:=FValue;
end;
Function TParam.GetAsCurrency: Currency;
begin
If IsNull then
@ -576,7 +587,6 @@ begin
Result := FValue;
end;
Function TParam.GetAsVariant: Variant;
begin
if IsNull then
@ -635,16 +645,22 @@ begin
Value:=AValue;
end;
procedure TParam.SetAsBytes(const AValue: TBytes);
begin
FDataType:=ftVarBytes;
Value:=AValue;
end;
Procedure TParam.SetAsCurrency(const AValue: Currency);
begin
FDataType:=ftCurrency;
Value:=Avalue;
Value:=AValue;
end;
Procedure TParam.SetAsDate(const AValue: TDateTime);
begin
FDataType:=ftDate;
Value:=Avalue;
Value:=AValue;
end;
Procedure TParam.SetAsDateTime(const AValue: TDateTime);
@ -730,7 +746,7 @@ begin
if VarIsFmtBCD(Value) then
FDataType:=ftFmtBCD
else if VarIsArray(AValue) and (VarType(AValue) and varTypeMask = varByte) then
FDataType:=ftBytes
FDataType:=ftVarBytes
else
FDataType:=ftUnknown;
end;

View File

@ -89,6 +89,7 @@ type
procedure TestBytesParamQuery;
procedure TestVarBytesParamQuery;
procedure TestBooleanParamQuery;
procedure TestBlobParamQuery;
procedure TestSetBlobAsMemoParam;
procedure TestSetBlobAsBlobParam;
@ -1534,6 +1535,11 @@ begin
TestXXParamQuery(ftBoolean, FieldtypeDefinitions[ftBoolean], testValuesCount);
end;
procedure TTestFieldTypes.TestBlobParamQuery;
begin
TestXXParamQuery(ftBlob, FieldtypeDefinitions[ftBlob], testBlobValuesCount);
end;
procedure TTestFieldTypes.TestStringParamQuery;
begin
@ -1588,14 +1594,15 @@ begin
Params.ParamByName('field1').AsDate := StrToDate(testDateValues[i],'yyyy/mm/dd','-');
ftDateTime: Params.ParamByName('field1').AsDateTime := StrToDateTime(testValues[ADataType,i], DBConnector.FormatSettings);
ftFMTBcd : Params.ParamByName('field1').AsFMTBCD := StrToBCD(testFmtBCDValues[i], DBConnector.FormatSettings);
ftBlob : Params.ParamByName('field1').AsBlob := testBlobValues[i];
ftBytes : if cross then
Params.ParamByName('field1').Value := StringToByteArray(testBytesValues[i])
else
Params.ParamByName('field1').AsBlob := testBytesValues[i];
Params.ParamByName('field1').AsBytes := StringToBytes(testBytesValues[i]);
ftVarBytes: if cross then
Params.ParamByName('field1').AsString := testBytesValues[i]
else
Params.ParamByName('field1').AsBlob := testBytesValues[i];
Params.ParamByName('field1').AsBytes := StringToBytes(testBytesValues[i]);
else
AssertTrue('no test for paramtype available',False);
end;
@ -1629,6 +1636,7 @@ begin
ftDate : AssertEquals(testDateValues[i],DateTimeToStr(FieldByName('FIELD1').AsDateTime, DBConnector.FormatSettings));
ftDateTime : AssertEquals(testValues[ADataType,i], DateTimeToStr(FieldByName('FIELD1').AsDateTime, DBConnector.FormatSettings));
ftFMTBcd : AssertEquals(testFmtBCDValues[i], BCDToStr(FieldByName('FIELD1').AsBCD, DBConnector.FormatSettings));
ftBlob : AssertEquals(testBlobValues[i], FieldByName('FIELD1').AsString);
ftVarBytes,
ftBytes : AssertEquals(testBytesValues[i], shortstring(FieldByName('FIELD1').AsString));
else

View File

@ -237,7 +237,8 @@ procedure FreeDBConnector;
function DateTimeToTimeString(d: tdatetime) : string;
function TimeStringToDateTime(d: String): TDateTime;
function StringToByteArray(s: ansistring): Variant;
function StringToByteArray(const s: ansistring): Variant;
function StringToBytes(const s: ansistring): TBytes;
implementation
@ -610,7 +611,7 @@ begin
result := ComposeDateTime(days,EncodeTime(hour,minute,second,millisecond));
end;
function StringToByteArray(s: ansistring): Variant;
function StringToByteArray(const s: ansistring): Variant;
var P: Pointer;
Len: integer;
begin
@ -624,6 +625,14 @@ begin
end;
end;
function StringToBytes(const s: ansistring): TBytes;
var Len: integer;
begin
Len := Length(s) * SizeOf(AnsiChar);
SetLength(Result, Len);
Move(s[1], Result[0], Len);
end;
initialization
ReadIniFile;