Browse Source

+ Caching of blob-fields which can only be fetched for the current record is now handled by TSQLConnection
+ postgresql text-fields are now considered to be blob-fields

git-svn-id: trunk@4584 -

joost 19 years ago
parent
commit
d0a367ef1e
3 changed files with 57 additions and 53 deletions
  1. 2 40
      fcl/db/sqldb/mysql/mysqlconn.inc
  2. 16 11
      fcl/db/sqldb/postgres/pqconnection.pp
  3. 39 2
      fcl/db/sqldb/sqldb.pp

+ 2 - 40
fcl/db/sqldb/mysql/mysqlconn.inc

@@ -51,10 +51,6 @@ Type
     ParamBinding : TParamBinding;
     ParamReplaceString : String;
     MapDSRowToMSQLRow  : array of integer;
-    FBlobStrings:TStringList;   // list of strings in which the blob-fields are stored
-  public
-    constructor Create;
-    destructor Destroy; override;
   end;
 
   TConnectionName = class (TSQLConnection)
@@ -99,7 +95,6 @@ Type
     procedure CommitRetaining(trans : TSQLHandle); override;
     procedure RollBackRetaining(trans : TSQLHandle); override;
     procedure UpdateIndexDefs(var IndexDefs : TIndexDefs;TableName : string); override;
-    procedure LoadBlobIntoStream(Field: TField;AStream: TMemoryStream;cursor: TSQLCursor;ATransaction : TSQLTransaction); override;
   Public
     Property ServerInfo : String Read FServerInfo;
     Property HostInfo : String Read FHostInfo;
@@ -216,7 +211,7 @@ var esc_str : pchar;
 
 begin
   if (not assigned(param)) or param.IsNull then Result := 'Null'
-  else if param.DataType =  ftString then
+  else if param.DataType in [ftString,ftBlob] then
     begin
     Getmem(esc_str,length(param.asstring)*2+1);
     mysql_real_escape_string(FMySQL,esc_str,pchar(param.asstring),length(param.asstring));
@@ -347,7 +342,7 @@ begin
     C.FRes:=Nil;
     end;
   SetLength(c.MapDSRowToMSQLRow,0);
-  c.FBlobStrings.Clear;
+  inherited;
 end;
 
 procedure TConnectionName.Execute(cursor: TSQLCursor;
@@ -764,25 +759,6 @@ begin
   qry.free;
 end;
 
-procedure TConnectionName.LoadBlobIntoStream(Field: TField;AStream: TMemoryStream;cursor: TSQLCursor;ATransaction : TSQLTransaction);
-
-var blobId : pinteger;
-    BlobBuf : TBufBlobField;
-    s      : string;
-
-begin
-  if not field.getData(@BlobBuf) then
-    exit;
-  blobId := @BlobBuf;
-
-  s := (cursor as TCursorName).FBlobStrings.Strings[blobid^];
-
-  AStream.WriteBuffer(s[1],length(s));
-
-  AStream.seek(0,soFromBeginning);
-end;
-
-
 function TConnectionName.GetTransactionHandle(trans: TSQLHandle): pointer;
 begin
   Result:=Nil;
@@ -813,18 +789,4 @@ begin
   // Do nothing
 end;
 
-{ TCursorName }
-
-constructor TCursorName.Create;
-begin
-  FBlobStrings := TStringList.Create;
-  inherited;
-end;
-
-destructor TCursorName.Destroy;
-begin
-  FBlobStrings.Free;
-  inherited Destroy;
-end;
-
 end.

+ 16 - 11
fcl/db/sqldb/postgres/pqconnection.pp

@@ -49,7 +49,6 @@ type
     Function AllocateTransactionHandle : TSQLHandle; override;
 
     procedure PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams); override;
-    procedure FreeFldBuffers(cursor : TSQLCursor); override;
     procedure Execute(cursor: TSQLCursor;atransaction:tSQLtransaction; AParams : TParams); override;
     procedure AddFieldDefs(cursor: TSQLCursor; FieldDefs : TfieldDefs); override;
     function Fetch(cursor : TSQLCursor) : boolean; override;
@@ -307,7 +306,8 @@ begin
   case Type_Oid of
     Oid_varchar,Oid_bpchar,
     Oid_name               : Result := ftstring;
-    Oid_text               : Result := ftstring;
+//    Oid_text               : Result := ftstring;
+    Oid_text               : Result := ftBlob;
     Oid_oid                : Result := ftInteger;
     Oid_int8               : Result := ftLargeInt;
     Oid_int4               : Result := ftInteger;
@@ -451,12 +451,6 @@ begin
     end;
 end;
 
-procedure TPQConnection.FreeFldBuffers(cursor : TSQLCursor);
-
-begin
-// Do nothing
-end;
-
 procedure TPQConnection.Execute(cursor: TSQLCursor;atransaction:tSQLtransaction;AParams : TParams);
 
 var ar  : array of pointer;
@@ -537,9 +531,11 @@ begin
         begin
         size := pqfmod(res,i)-4;
         if size = -5 then size := dsMaxStringSize;
-        end;
-      if fieldtype = ftdate  then
-        size := sizeof(double);
+        end
+      else if fieldtype = ftdate  then
+        size := sizeof(double)
+      else if fieldtype = ftblob then
+        size := 0;
 
       TFieldDef.Create(FieldDefs, PQfname(Res, i), fieldtype,size, False, (i + 1));
       end;
@@ -581,6 +577,7 @@ var
   dbl           : pdouble;
   cur           : currency;
   NumericRecord : ^TNumericRecord;
+  s             : string;
 
 begin
   with cursor as TPQCursor do
@@ -619,6 +616,14 @@ begin
           pchar(Buffer + li)^ := #0;
           i := pqfmod(res,x)-3;
           end;
+        ftBlob  :
+          begin
+          li := pqgetlength(res,curtuple,x);
+          setlength(s,li);
+          Move(CurrBuff^, s[1], li);
+          i := fBlobStrings.Add(S);
+          Move(I, Buffer^, SizeOf(Integer));
+          end;
         ftdate :
           begin
           dbl := pointer(buffer);

+ 39 - 2
fcl/db/sqldb/sqldb.pp

@@ -38,11 +38,17 @@ type
   TSQLHandle = Class(TObject)
   end;
 
+  { TSQLCursor }
+
   TSQLCursor = Class(TSQLHandle)
   public
     FPrepared      : Boolean;
     FInitFieldDef  : Boolean;
     FStatementType : TStatementType;
+    FBlobStrings   : TStringList;   // list of strings in which the blob-fields are stored
+  public
+    constructor Create;
+    destructor Destroy; override;
   end;
 
 
@@ -90,7 +96,7 @@ type
     procedure AddFieldDefs(cursor: TSQLCursor; FieldDefs : TfieldDefs); virtual; abstract;
     procedure UnPrepareStatement(cursor : TSQLCursor); virtual; abstract;
 
-    procedure FreeFldBuffers(cursor : TSQLCursor); virtual; abstract;
+    procedure FreeFldBuffers(cursor : TSQLCursor); virtual;
     function LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer) : boolean; virtual; abstract;
     function GetTransactionHandle(trans : TSQLHandle): pointer; virtual; abstract;
     function Commit(trans : TSQLHandle) : boolean; virtual; abstract;
@@ -455,6 +461,11 @@ begin
   Result := nil;
 end;
 
+procedure TSQLConnection.FreeFldBuffers(cursor: TSQLCursor);
+begin
+  cursor.FBlobStrings.Clear;
+end;
+
 function TSQLConnection.GetSchemaInfoSQL( SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string;
 
 begin
@@ -463,8 +474,20 @@ end;
 
 procedure TSQLConnection.LoadBlobIntoStream(Field: TField;AStream: TMemoryStream; cursor: TSQLCursor;ATransaction : TSQLTransaction);
 
+var blobId  : pinteger;
+    BlobBuf : TBufBlobField;
+    s       : string;
+
 begin
-  DatabaseErrorFmt(SUnsupportedFieldType,['Blob']);
+  if not field.getData(@BlobBuf) then
+    exit;
+  blobId := @BlobBuf;
+
+  s := cursor.FBlobStrings.Strings[blobid^];
+
+  AStream.WriteBuffer(s[1],length(s));
+
+  AStream.seek(0,soFromBeginning);
 end;
 
 { TSQLTransaction }
@@ -1260,4 +1283,18 @@ begin
     DataSource:=Nil;
 end;
 
+{ TSQLCursor }
+
+constructor TSQLCursor.Create;
+begin
+  FBlobStrings := TStringList.Create;
+  inherited;
+end;
+
+destructor TSQLCursor.Destroy;
+begin
+  FBlobStrings.Free;
+  inherited Destroy;
+end;
+
 end.