فهرست منبع

* Removed unused BlobsCached function
* Implemented GetSchemaInfoSQL
* Add support for pragma-statements
* Map empty field-types to ftString types
* Implemented BLOB-support

git-svn-id: trunk@10845 -

joost 17 سال پیش
والد
کامیت
545e44ff11
1فایلهای تغییر یافته به همراه47 افزوده شده و 33 حذف شده
  1. 47 33
      packages/fcl-db/src/sqldb/sqlite/sqlite3conn.pp

+ 47 - 33
packages/fcl-db/src/sqldb/sqlite/sqlite3conn.pp

@@ -48,7 +48,6 @@ type
   private
   private
     fhandle: psqlite3;
     fhandle: psqlite3;
     foptions: TSQLiteOptions;
     foptions: TSQLiteOptions;
-    function blobscached: boolean;
     procedure setoptions(const avalue: tsqliteoptions);
     procedure setoptions(const avalue: tsqliteoptions);
   protected
   protected
     function stringquery(const asql: string): TStringArray;
     function stringquery(const asql: string): TStringArray;
@@ -86,8 +85,11 @@ type
     procedure UpdateIndexDefs(var IndexDefs : TIndexDefs; const TableName : string); // Differs from SQLDB.
     procedure UpdateIndexDefs(var IndexDefs : TIndexDefs; const TableName : string); // Differs from SQLDB.
     function  getprimarykeyfield(const atablename: string; const acursor: tsqlcursor): string; 
     function  getprimarykeyfield(const atablename: string; const acursor: tsqlcursor): string; 
     function RowsAffected(cursor: TSQLCursor): TRowsCount; override;
     function RowsAffected(cursor: TSQLCursor): TRowsCount; override;
+    function GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string; override;
+    function StrToStatementType(s : string) : TStatementType; override;
   public
   public
     function GetInsertID: int64; 
     function GetInsertID: int64; 
+    procedure GetFieldNames(const TableName : string; List :  TStrings); override;
   published
   published
     property Options: TSqliteOptions read FOptions write SetOptions;
     property Options: TSqliteOptions read FOptions write SetOptions;
   end;
   end;
@@ -251,25 +253,20 @@ end;
 procedure TSQLite3Connection.LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField; cursor: TSQLCursor; ATransaction : TSQLTransaction); 
 procedure TSQLite3Connection.LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField; cursor: TSQLCursor; ATransaction : TSQLTransaction); 
 
 
 var
 var
- blobid: integer;
- int1,int2: integer;
- str1: string;
- bo1: boolean;
-begin
-{$WARNING TSQLite3Connection.LoadBlobIntoBuffer not implemented !}
-{ if (mode = bmwrite) and (field.dataset is tmsesqlquery) then begin
-  result:= tmsebufdataset(field.dataset).createblobbuffer(field);
- end
- else begin
-  result:= nil;
-  if mode = bmread then begin
-   if field.getData(@blobId) then begin
-    result:= acursor.getcachedblob(blobid);
-   end;
-  end;
- end;
- }
- 
+ int1: integer;
+ st: psqlite3_stmt;
+ fnum: integer;
+
+begin
+  st:=TSQLite3Cursor(cursor).fstatement;
+  fnum:= FieldDef.fieldno - 1;
+
+  int1:= sqlite3_column_bytes(st,fnum);
+
+  ReAllocMem(ABlobBuf^.BlobBuffer^.Buffer,int1);
+  if int1 > 0 then
+    move(sqlite3_column_text(st,fnum)^,ABlobBuf^.BlobBuffer^.Buffer^,int1);
+  ABlobBuf^.BlobBuffer^.Size := int1;
 end;
 end;
 
 
 function TSQLite3Connection.AllocateTransactionHandle: TSQLHandle;
 function TSQLite3Connection.AllocateTransactionHandle: TSQLHandle;
@@ -334,7 +331,7 @@ Const
    (n:'DECIMAL'; t: ftBCD),
    (n:'DECIMAL'; t: ftBCD),
    (n:'TEXT'; t: ftmemo),
    (n:'TEXT'; t: ftmemo),
    (n:'BLOB'; t: ftBlob)
    (n:'BLOB'; t: ftBlob)
-{ Template:   
+{ Template:
   (n:''; t: ft)
   (n:''; t: ft)
 }
 }
   );
   );
@@ -363,6 +360,9 @@ begin
       ft1:=FieldMap[fi].t;
       ft1:=FieldMap[fi].t;
       break;
       break;
       end;
       end;
+    // Empty field types are allowed and used in calculated columns (aggregates)
+    // and by pragma-statements
+    if FD='' then ft1 := ftString;
     // handle some specials.
     // handle some specials.
     size1:=0;
     size1:=0;
     case ft1 of
     case ft1 of
@@ -515,13 +515,7 @@ begin
                  move(sqlite3_column_text(st,fnum)^,buffer^,int1);
                  move(sqlite3_column_text(st,fnum)^,buffer^,int1);
               end;
               end;
     ftMemo,
     ftMemo,
-    ftBlob: begin
-            CreateBlob:=True;
-            int2:= sqlite3_column_bytes(st,fnum);
-            {$WARNING Blob data not handled correctly }
-            // int1:= addblobdata(sqlite3_column_text(st,fnum),int2);
-            move(int1,buffer^,sizeof(int1)); //save id
-            end;
+    ftBlob: CreateBlob:=True;
   else { Case }
   else { Case }
    result:= false; // unknown
    result:= false; // unknown
   end; { Case }
   end; { Case }
@@ -631,11 +625,6 @@ begin
    databaseerror(str1);
    databaseerror(str1);
 end;
 end;
 
 
-function TSQLite3Connection.blobscached: boolean;
-begin
-  result:= true;
-end;
-
 function execcallback(adata: pointer; ncols: longint; //adata = PStringArray
 function execcallback(adata: pointer; ncols: longint; //adata = PStringArray
                 avalues: PPchar; anames: PPchar):longint; cdecl;
                 avalues: PPchar; anames: PPchar):longint; cdecl;
 var
 var
@@ -711,6 +700,25 @@ begin
     Result := -1;
     Result := -1;
 end;
 end;
 
 
+function TSQLite3Connection.GetSchemaInfoSQL(SchemaType: TSchemaType;
+  SchemaObjectName, SchemaPattern: string): string;
+  
+begin
+  case SchemaType of
+    stTables     : result := 'select name as table_name from sqlite_master where type = ''table''';
+    stColumns    : result := 'pragma table_info(''' + (SchemaObjectName) + ''')';
+  else
+    DatabaseError(SMetadataUnavailable)
+  end; {case}
+end;
+
+function TSQLite3Connection.StrToStatementType(s: string): TStatementType;
+begin
+  S:=Lowercase(s);
+  if s = 'pragma' then exit(stSelect);
+  result := inherited StrToStatementType(s);
+end;
+
 procedure TSQLite3Connection.UpdateIndexDefs(var IndexDefs: TIndexDefs;
 procedure TSQLite3Connection.UpdateIndexDefs(var IndexDefs: TIndexDefs;
                               const TableName: string);
                               const TableName: string);
 var
 var
@@ -746,6 +754,12 @@ begin
  result:= sqlite3_last_insert_rowid(fhandle);
  result:= sqlite3_last_insert_rowid(fhandle);
 end;
 end;
 
 
+procedure TSQLite3Connection.GetFieldNames(const TableName: string;
+  List: TStrings);
+begin
+  GetDBInfo(stColumns,TableName,'name',List);
+end;
+
 procedure TSQLite3Connection.setoptions(const avalue: tsqliteoptions);
 procedure TSQLite3Connection.setoptions(const avalue: tsqliteoptions);
 begin
 begin
  if avalue <> foptions then 
  if avalue <> foptions then