Browse Source

fcl-db: base: TBinaryField.AsVariant should return variant Null when Field.IsNull

git-svn-id: trunk@24387 -
lacak 12 years ago
parent
commit
e280a8d97e

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

@@ -741,6 +741,7 @@ type
     function GetAsString: string; override;
     function GetAsVariant: Variant; override;
     procedure GetText(var TheText: string; ADisplayText: Boolean); override;
+    function GetValue(var AValue: TBytes): Boolean;
     procedure SetAsBytes(const AValue: TBytes); override;
     procedure SetAsString(const AValue: string); override;
     procedure SetText(const AValue: string); override;

+ 30 - 20
packages/fcl-db/src/base/fields.inc

@@ -2206,26 +2206,16 @@ begin
 end;
 
 function TBinaryField.GetAsBytes: TBytes;
-var B: TBytes;
 begin
-  SetLength(B, DataSize);
-  if not assigned(B) or not GetData(Pointer(B), True) then
-    SetLength(Result, 0)
-  else if DataType = ftVarBytes then
-  begin
-    SetLength(Result, PWord(B)^);
-    Move(B[sizeof(Word)], Result[0], Length(Result));
-  end
-  else // ftBytes
-    Result := B;
+  if not GetValue(Result) then
+    SetLength(Result, 0);
 end;
 
 
 function TBinaryField.GetAsString: string;
 var B: TBytes;
 begin
-  B := GetAsBytes;
-  if length(B) = 0 then
+  if not GetValue(B) then
     Result := ''
   else
     SetString(Result, @B[0], length(B) div SizeOf(Char));
@@ -2236,13 +2226,17 @@ function TBinaryField.GetAsVariant: Variant;
 var B: TBytes;
     P: Pointer;
 begin
-  B := GetAsBytes;
-  Result := VarArrayCreate([0, length(B)-1], varByte);
-  P := VarArrayLock(Result);
-  try
-    Move(B[0], P^, length(B));
-  finally
-    VarArrayUnlock(Result);
+  if not GetValue(B) then
+    Result := Null
+  else
+  begin
+    Result := VarArrayCreate([0, length(B)-1], varByte);
+    P := VarArrayLock(Result);
+    try
+      Move(B[0], P^, length(B));
+    finally
+      VarArrayUnlock(Result);
+    end;
   end;
 end;
 
@@ -2254,6 +2248,22 @@ begin
 end;
 
 
+function TBinaryField.GetValue(var AValue: TBytes): Boolean;
+var B: TBytes;
+begin
+  SetLength(B, DataSize);
+  Result := assigned(B) and GetData(Pointer(B), True);
+  if Result then
+    if DataType = ftVarBytes then
+      begin
+      SetLength(AValue, PWord(B)^);
+      Move(B[sizeof(Word)], AValue[0], Length(AValue));
+      end
+    else // ftBytes
+      AValue := B;
+end;
+
+
 procedure TBinaryField.SetAsBytes(const AValue: TBytes);
 var Buf: array[0..dsMaxStringSize] of byte;
     DynBuf: TBytes;

+ 7 - 0
packages/fcl-db/tests/testfieldtypes.pas

@@ -933,6 +933,11 @@ begin
       end;
       ExecSQL;
       end;
+    // test NULL parameter value
+    Params.ParamByName('id').AsInteger := testValuesCount;
+    Params.ParamByName('field1').Clear;
+    ExecSQL;
+
     TSQLDBConnector(DBConnector).Transaction.CommitRetaining;
 
     sql.clear;
@@ -962,6 +967,8 @@ begin
       end;
       Next;
       end;
+    AssertTrue('Expected IsNull', FieldByName('FIELD1').IsNull);
+    AssertTrue('Expected Null Variant', VarIsNull(FieldByName('FIELD1').AsVariant));
     close;
     end;
   TSQLDBConnector(DBConnector).Transaction.CommitRetaining;