Преглед на файлове

fcl-db: base: introduce TParam.AsBytes + test
(FDataType is set to ftVarBytes intentionally due to Delphi compatibility)

git-svn-id: trunk@27844 -

lacak преди 11 години
родител
ревизия
ae3f0f3752
променени са 4 файла, в които са добавени 45 реда и са изтрити 9 реда
  1. 4 1
      packages/fcl-db/src/base/db.pas
  2. 20 4
      packages/fcl-db/src/base/dsparams.inc
  3. 10 2
      packages/fcl-db/tests/testfieldtypes.pas
  4. 11 2
      packages/fcl-db/tests/toolsunit.pas

+ 4 - 1
packages/fcl-db/src/base/db.pas

@@ -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;

+ 20 - 4
packages/fcl-db/src/base/dsparams.inc

@@ -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;

+ 10 - 2
packages/fcl-db/tests/testfieldtypes.pas

@@ -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

+ 11 - 2
packages/fcl-db/tests/toolsunit.pas

@@ -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;