Bläddra i källkod

* Patch from Luiz Américo:
Sqlite interface:
- Added missing constant
- Remapps sqlite_version from cvar to function call, the former was not working in win32
- Clean up the code
SQLite Dataset :
- Reduce memory usage
- Replaces TList with TFPList
- Add auxiliary QuickQuery functions
- Misc fixes

git-svn-id: trunk@827 -

michael 20 år sedan
förälder
incheckning
e050f7ddb1

+ 40 - 26
fcl/db/sqlite/customsqliteds.pas

@@ -37,7 +37,6 @@ type
   PPDataRecord = ^PDataRecord;
   DataRecord = record
     Row: PPchar;
-    BookmarkData: Pointer;
     BookmarkFlag: TBookmarkFlag;
     Next: PDataRecord;
     Previous: PDataRecord;
@@ -78,10 +77,10 @@ type
     FSaveOnClose: Boolean;
     FSaveOnRefetch: Boolean;
     FComplexSql: Boolean;
-    FUpdatedItems: TList;
-    FAddedItems: TList;
-    FDeletedItems: TList;
-    FOrphanItems: TList;
+    FUpdatedItems: TFPList;
+    FAddedItems: TFPList;
+    FDeletedItems: TFPList;
+    FOrphanItems: TFPList;
     FMasterLink: TMasterDataLink;
     FIndexFieldNames: String;
     FIndexFieldList: TList;
@@ -105,6 +104,7 @@ type
     function SqliteExec(AHandle: Pointer; Sql:PChar):Integer;virtual; abstract;
     procedure SqliteClose(AHandle: Pointer);virtual;abstract;
     function GetSqliteHandle: Pointer; virtual; abstract;
+    function GetSqliteVersion: String; virtual; abstract;
     procedure BuildLinkedList; virtual; abstract;
     function SqliteReturnString: String; virtual; abstract;
     function TableExists: Boolean;virtual;abstract;
@@ -153,6 +153,9 @@ type
     function CreateTable: Boolean; virtual;
     function ExecSQL:Integer;
     function ExecSQL(const ASql:String):Integer;
+    function QuickQuery(const ASql:String):String;overload;
+    function QuickQuery(const ASql:String;const AStrList: TStrings):String;overload;
+    function QuickQuery(const ASql:String;const AStrList: TStrings;FillObjects:Boolean):String;virtual;abstract;overload;
     procedure RefetchData;
     function UpdatesPending: Boolean;
     {$ifdef DEBUGACTIVEBUFFER}
@@ -162,9 +165,9 @@ type
     {$ifdef USE_SQLITEDS_INTERNALS}
     property BeginItem: PDataRecord read FBeginItem;
     property EndItem: PDataRecord read FEndItem;
-    property UpdatedItems: TList read FUpdatedItems;
-    property AddedItems: TList read FAddedItems;
-    property DeletedItems: TList read FDeletedItems;
+    property UpdatedItems: TFPList read FUpdatedItems;
+    property AddedItems: TFPList read FAddedItems;
+    property DeletedItems: TFPList read FDeletedItems;
     {$endif}
     property ComplexSql: Boolean read FComplexSql write FComplexSql;
     property ExpectedAppends: Integer read FExpectedAppends write SetExpectedAppends;
@@ -172,6 +175,8 @@ type
     property ExpectedDeletes: Integer read FExpectedDeletes write SetExpectedDeletes;
     property IndexFields[Value: Integer]: TField read GetIndexFields;
     property SqliteReturnId: Integer read FSqliteReturnId;
+    property SqliteHandle: Pointer read FSqliteHandle;
+    property SqliteVersion: String read GetSqliteVersion;
    published
     property IndexFieldNames: string read FIndexFieldNames write FIndexFieldNames;
     property FileName: String read FFileName write FFileName;
@@ -325,13 +330,13 @@ begin
   FIndexFieldList:=TList.Create;
   BookmarkSize := SizeOf(Pointer);
   FBufferSize := SizeOf(PPDataRecord);
-  FUpdatedItems:= TList.Create;
+  FUpdatedItems:= TFPList.Create;
   FUpdatedItems.Capacity:=20;
-  FAddedItems:= TList.Create;
+  FAddedItems:= TFPList.Create;
   FAddedItems.Capacity:=20;
-  FOrphanItems:= TList.Create;
+  FOrphanItems:= TFPList.Create;
   FOrphanItems.Capacity:=20;
-  FDeletedItems:= TList.Create;
+  FDeletedItems:= TFPList.Create;
   FDeletedItems.Capacity:=20;
   inherited Create(AOwner);
 end;
@@ -359,7 +364,7 @@ end;
 function TCustomSqliteDataset.GetIndexFields(Value: Integer): TField;
 begin
   if (Value < 0) or (Value > FIndexFieldList.Count - 1) then
-    DatabaseError('Error acessing IndexFields: Index out of bonds');
+    DatabaseError('Error acessing IndexFields: Index out of bonds',Self);
   Result:= TField(FIndexFieldList[Value]);
 end;
 
@@ -407,7 +412,7 @@ end;
 
 procedure TCustomSqliteDataset.GetBookmarkData(Buffer: PChar; Data: Pointer);
 begin
-  Pointer(Data^) := PPDataRecord(Buffer)^^.BookmarkData;
+  Pointer(Data^) := PPDataRecord(Buffer)^;
 end;
 
 function TCustomSqliteDataset.GetBookmarkFlag(Buffer: PChar): TBookmarkFlag;
@@ -467,11 +472,7 @@ begin
   if Result = grOk then
   begin
     PDataRecord(Pointer(Buffer)^):=FCurrentItem;
-    with FCurrentItem^ do
-    begin
-      BookmarkData := FCurrentItem;
-      BookmarkFlag := bfCurrent;
-    end;
+    FCurrentItem^.BookmarkFlag := bfCurrent;
   end
     else if (Result = grError) and DoCheck then
       DatabaseError('SqliteDs - No records',Self);
@@ -634,9 +635,9 @@ procedure TCustomSqliteDataset.InternalOpen;
 begin
   FAutoIncFieldNo:=-1;
   if not FileExists(FFileName) then
-    DatabaseError('TCustomSqliteDataset - File '+FFileName+' not found');
+    DatabaseError('TCustomSqliteDataset - File "'+FFileName+'" not found',Self);
   if (FTablename = '') and not (FComplexSql) then
-    DatabaseError('TCustomSqliteDataset - Tablename not set');
+    DatabaseError('TCustomSqliteDataset - Tablename not set',Self);
 
   if MasterSource <> nil then
   begin
@@ -660,7 +661,7 @@ begin
 
   UpdateIndexFields;
   if FMasterLink.Active and (FIndexFieldList.Count <> FMasterLink.Fields.Count) then
-    DatabaseError('MasterFields count doesnt match IndexFields count');
+    DatabaseError('MasterFields count doesnt match IndexFields count',Self);
 
   // Get PrimaryKeyNo if available
   if Fields.FindField(FPrimaryKey) <> nil then
@@ -690,7 +691,7 @@ end;
 
 procedure TCustomSqliteDataset.SetBookmarkData(Buffer: PChar; Data: Pointer);
 begin
-  PPDataRecord(Buffer)^^.BookmarkData := Pointer(Data^);
+  //The BookMarkData is the Buffer itsef;
 end;
 
 procedure TCustomSqliteDataset.SetBookmarkFlag(Buffer: PChar; Value: TBookmarkFlag);
@@ -714,6 +715,8 @@ procedure TCustomSqliteDataset.SetExpectedDeletes(AValue:Integer);
 begin
   if Assigned(FDeletedItems) then
     FDeletedItems.Capacity:=AValue;
+  if Assigned(FOrphanItems) then
+    FOrphanItems.Capacity:=AValue;  
 end;  
 
 procedure TCustomSqliteDataset.SetFieldData(Field: TField; Buffer: Pointer);
@@ -757,7 +760,7 @@ var
   TempItem:PDataRecord;
 begin
   if (Value >= FRecordCount) or (Value < 0) then
-    DatabaseError('SqliteDs - Record Number Out Of Range');
+    DatabaseError('SqliteDs - Record Number Out Of Range',Self);
   TempItem:=FBeginItem;
   for Counter := 0 to Value do
     TempItem:=TempItem^.Next;
@@ -805,7 +808,7 @@ end;
 procedure TCustomSqliteDataset.SetMasterFields(Value: String);
 begin
   if Active then
-    DatabaseError('It''s not allowed to set MasterFields property in a open dataset');
+    DatabaseError('It''s not allowed to set MasterFields property in a open dataset',Self);
   FMasterLink.FieldNames:=Value;
 end;
 
@@ -852,7 +855,7 @@ begin
     if FFileName <> '' then  
       AHandle := GetSqliteHandle
     else
-      DatabaseError ('ExecSql - FileName not set');    
+      DatabaseError ('ExecSql - FileName not set',Self);    
   FSqliteReturnId:= SqliteExec(AHandle,PChar(ASql));
   //todo: add a way to get the num of changes
   //Result:=sqlite_changes(AHandle);
@@ -1079,6 +1082,17 @@ begin
     (FAddedItems.Count > 0) or (FUpdatedItems.Count > 0);
 end;
 
+function TCustomSqliteDataset.QuickQuery(const ASql:String):String;
+begin
+  Result:=QuickQuery(ASql,nil,False); 
+end;
+
+function TCustomSqliteDataset.QuickQuery(const ASql:String;const AStrList: TStrings):String;
+begin
+  Result:=QuickQuery(ASql,AStrList,False)
+end;  
+
+
 {$ifdef DEBUGACTIVEBUFFER}
 procedure TCustomSqliteDataset.SetCurrentItem(Value:PDataRecord);
 var

+ 59 - 0
fcl/db/sqlite/sqlite3ds.pas

@@ -37,6 +37,7 @@ type
   private
     function SqliteExec(AHandle: Pointer; ASql:PChar):Integer;override;
     function GetSqliteHandle: Pointer; override;
+    function GetSqliteVersion: String; override;
     procedure SqliteClose(AHandle: Pointer);override;
     procedure BuildLinkedList; override;
   protected
@@ -44,6 +45,7 @@ type
   public
     function SqliteReturnString: String; override;
     function TableExists: Boolean;override;
+    function QuickQuery(const ASql:String;const AStrList: TStrings;FillObjects:Boolean):String;override;
   end;
 
 implementation
@@ -285,6 +287,63 @@ begin
  end;
 end;
 
+function TSqlite3Dataset.GetSqliteVersion: String;
+begin
+  Result:=StrPas(sqlite3_version);
+end;
+
+function TSqlite3Dataset.QuickQuery(const ASql:String;const AStrList: TStrings;FillObjects:Boolean):String;
+var
+  vm,AHandle:Pointer;
+    
+  procedure FillStrings;
+  begin
+    while FSqliteReturnId = SQLITE_ROW do
+    begin
+      AStrList.Add(StrPas(sqlite3_column_text(vm,0)));
+      FSqliteReturnId:=sqlite3_step(vm);  
+    end;
+  end;
+  procedure FillStringsAndObjects;
+  begin
+    while FSqliteReturnId = SQLITE_ROW do
+    begin
+      AStrList.AddObject(StrPas(sqlite3_column_text(vm,0)),TObject(sqlite3_column_int(vm,1)));
+      FSqliteReturnId:=sqlite3_step(vm);  
+    end;
+  end;    
+begin
+  if FSqliteHandle <> nil then
+    AHandle:=FSqliteHandle
+  else
+    if FileExists(FFileName) then
+      AHandle:=GetSqliteHandle
+    else
+      DatabaseError('File '+FFileName+' not Exists',Self);    
+  Result:='';
+  if AStrList <> nil then
+    AStrList.Clear;
+  FSqliteReturnId:=sqlite3_prepare(AHandle,Pchar(ASql),-1,@vm,nil);
+  //FSqliteReturnId:=sqlite_compile(AHandle,Pchar(ASql),nil,@vm,nil);
+  if FSqliteReturnId <> SQLITE_OK then
+    DatabaseError('Error returned by sqlite in QuickQuery: '+SqliteReturnString,Self);
+    
+  FSqliteReturnId:=sqlite3_step(vm);
+  if (FSqliteReturnId = SQLITE_ROW) and (sqlite3_column_count(vm) > 0) then
+  begin
+    Result:=StrPas(sqlite3_column_text(vm,0));
+    if AStrList <> nil then
+    begin   
+      if FillObjects and (sqlite3_column_count(vm) > 1) then
+        FillStringsAndObjects
+      else
+        FillStrings;
+    end;          
+  end;  
+  sqlite3_finalize(vm); 
+  if FSqliteHandle = nil then
+    sqlite3_close(AHandle);
+end;
 
 end.
 

+ 70 - 2
fcl/db/sqlite/sqliteds.pas

@@ -37,6 +37,8 @@ type
   private
     function SqliteExec(AHandle: Pointer; ASql:PChar):Integer;override;
     function GetSqliteHandle: Pointer; override;
+    function GetSqliteEncoding: String;
+    function GetSqliteVersion: String; override;
     procedure SqliteClose(AHandle: Pointer);override;
     procedure BuildLinkedList; override;
   protected
@@ -44,6 +46,8 @@ type
   public
     function SqliteReturnString: String; override;
     function TableExists: Boolean;override;
+    function QuickQuery(const ASql:String;const AStrList: TStrings;FillObjects:Boolean):String;override;
+    property SqliteEncoding: String read GetSqliteEncoding;
   end;
 
 implementation
@@ -254,7 +258,7 @@ begin
     {$endif}
     Result:=FSqliteReturnId = SQLITE_ROW;
     sqlite_finalize(vm, nil);
-    if (FSqliteHandle = nil) then
+    if FSqliteHandle = nil then
       SqliteClose(AHandle);
   end;
   {$ifdef DEBUG}
@@ -290,7 +294,7 @@ begin
       SQLITE_NOLFS        : Result := 'SQLITE_NOLFS       ';
       SQLITE_AUTH         : Result := 'SQLITE_AUTH        ';
       SQLITE_FORMAT       : Result := 'SQLITE_FORMAT      ';
-     // SQLITE_RANGE        : Result := 'SQLITE_RANGE       ';
+      SQLITE_RANGE        : Result := 'SQLITE_RANGE       ';
       SQLITE_ROW          : Result := 'SQLITE_ROW         ';
       SQLITE_DONE         : Result := 'SQLITE_DONE        ';
   else
@@ -298,6 +302,70 @@ begin
  end;
 end;
 
+function TSqliteDataset.GetSqliteEncoding: String;
+begin
+  Result:=StrPas(sqlite_encoding);
+end;
+  
+function TSqliteDataset.GetSqliteVersion: String;
+begin
+  Result:=StrPas(sqlite_version);
+end;
 
+function TSqliteDataset.QuickQuery(const ASql:String;const AStrList: TStrings;FillObjects:Boolean):String;
+var
+  vm,AHandle:Pointer;
+  ColumnNames,ColumnValues:PPChar;
+  ColCount:Integer;
+  
+  procedure FillStrings;
+  begin
+    while FSqliteReturnId = SQLITE_ROW do
+    begin
+      AStrList.Add(StrPas(ColumnValues[0]));
+      FSqliteReturnId:=sqlite_step(vm,@ColCount,@ColumnValues,@ColumnNames);  
+    end;
+  end;
+  procedure FillStringsAndObjects;
+  begin
+    while FSqliteReturnId = SQLITE_ROW do
+    begin
+      // I know, this code is really dirty!!
+      AStrList.AddObject(StrPas(ColumnValues[0]),TObject(StrToInt(StrPas(ColumnValues[1]))));
+      FSqliteReturnId:=sqlite_step(vm,@ColCount,@ColumnValues,@ColumnNames);  
+    end;
+  end;    
+begin
+  if FSqliteHandle <> nil then
+    AHandle:=FSqliteHandle
+  else
+    if FileExists(FFileName) then
+      AHandle:=GetSqliteHandle
+    else
+      DatabaseError('File '+FFileName+' not Exists',Self);    
+  Result:='';
+  if AStrList <> nil then
+    AStrList.Clear;
+  FSqliteReturnId:=sqlite_compile(AHandle,Pchar(ASql),nil,@vm,nil);
+  if FSqliteReturnId <> SQLITE_OK then
+    DatabaseError('Error returned by sqlite in QuickQuery: '+SqliteReturnString,Self);
+    
+  FSqliteReturnId:=sqlite_step(vm,@ColCount,@ColumnValues,@ColumnNames);
+  if (FSqliteReturnId = SQLITE_ROW) and (ColCount > 0) then
+  begin
+    Result:=StrPas(ColumnValues[0]);
+    if AStrList <> nil then
+    begin   
+      if FillObjects and (ColCount > 1) then
+        FillStringsAndObjects
+      else
+        FillStrings;
+    end;          
+  end;  
+  sqlite_finalize(vm, nil); 
+  if FSqliteHandle = nil then
+    sqlite_close(AHandle);
+end;
+    
 end.
 

+ 61 - 73
packages/base/sqlite/sqlite.pp

@@ -13,32 +13,14 @@ interface
     sqlite.h
 }
 
-  const
-    External_library='sqlite'; {Setup as you need}
-
-  { Pointers to basic pascal types, inserted by h2pas conversion program.}
-  Type
-    PLongint  = ^Longint;
-    PSmallInt = ^SmallInt;
-    PByte     = ^Byte;
-    PWord     = ^Word;
-    PDWord    = ^DWord;
-    PDouble   = ^Double;
-    PPPchar   = ^ppchar;
-
 {$PACKRECORDS C}
 
 const
-  _SQLITE_VERSION = '2.8.3';
-  SQLITE_ISO8859 = 1;
-
-{$ifndef win32}
-var
-  sqlite_version  : pchar;cvar;external;
-  sqlite_encoding : pchar;cvar;external;
-{$endif}
-
-const
+  External_library='sqlite';
+ 
+  SQLITE_ISO8859 = 1;//?
+  
+  //sqlite_exec and sqlite_step return values
   SQLITE_OK         = 0;
   SQLITE_ERROR      = 1;
   SQLITE_INTERNAL   = 2;
@@ -64,10 +46,11 @@ const
   SQLITE_NOLFS      = 22;
   SQLITE_AUTH       = 23;
   SQLITE_FORMAT     = 24;
+  SQLITE_RANGE      = 25;
   SQLITE_ROW        = 100;
   SQLITE_DONE       = 101;
 
-
+  // values used in sqlite_set_authorizer to define what operations authorize
   SQLITE_COPY                = 0;
   SQLITE_CREATE_INDEX        = 1;
   SQLITE_CREATE_TABLE        = 2;
@@ -92,6 +75,8 @@ const
   SQLITE_SELECT              = 21;
   SQLITE_TRANSACTION         = 22;
   SQLITE_UPDATE              =  23;
+  
+  //Return values of the authorizer function
   SQLITE_DENY                = 1;
   SQLITE_IGNORE              = 2;
 
@@ -99,8 +84,8 @@ const
   SQLITE_TEXT    = -2;
   SQLITE_ARGS    = -3;
 
-
 Type
+  PPPchar   = ^ppchar;
   Psqlite = Pointer;
   Psqlite_vm = Pointer;
   PPsqlite_vm = ^Psqlite_vm;
@@ -108,61 +93,64 @@ Type
 
   // Procedural types used in functions.
 
-  sqlite_callback = function (_para1:pointer; _para2:longint; _para3:PPchar; _para4:PPchar):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};
-  sqlite_trace_func = procedure (_para1:pointer; _para2:Pchar);{$ifdef win32}cdecl{$else}cdecl{$endif};
-  sqlite_create_func = procedure (_para1:Psqlite_func; _para2:longint; _para3:PPchar);{$ifdef win32}cdecl{$else}cdecl{$endif};
-  sqlite_handler = function (_para1:pointer; _para2:Pchar; _para3:longint):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};
-  sqlite_step_func = procedure (_para1:Psqlite_func; _para2:longint; _para3:PPchar)  ;{$ifdef win32}cdecl{$else}cdecl{$endif};
-  sqlite_finalize_func = procedure (_para1:Psqlite_func);{$ifdef win32}cdecl{$else}cdecl{$endif};
-  sqlite_authorize_func = function (_para1:pointer; _para2:longint; _para3, _para4,_para5,_para6:Pchar):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};
-
-  function sqlite_create_function(_para1:Psqlite; zName:Pchar; nArg:longint; xFunc:sqlite_create_func; pUserData:pointer):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_create_function';
-  function sqlite_open(filename:Pchar; mode:longint; errmsg:PPchar):Psqlite;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_open';
-  procedure sqlite_close(_para1:Psqlite);{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_close';
-  function sqlite_exec(_para1:Psqlite; sql:Pchar; _para3:sqlite_callback; _para4:pointer; errmsg:PPchar):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_exec';
-  function sqlite_last_insert_rowid(_para1:Psqlite):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_last_insert_rowid';
-  function sqlite_changes(_para1:Psqlite):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_changes';
-  function sqlite_error_string(_para1:longint):Pchar;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_error_string';
-  procedure do_sqlite_interrupt(_para1:Psqlite);{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_interrupt';
-  function sqlite_complete(sql:Pchar):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_complete';
-  procedure sqlite_busy_handler(_para1:Psqlite; _para2:sqlite_handler; _para3:pointer);{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_busy_handler';
-  procedure sqlite_busy_timeout(_para1:Psqlite; ms:longint);{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_busy_timeout';
+  sqlite_callback = function (_para1:pointer; _para2:longint; _para3:PPchar; _para4:PPchar):longint;cdecl;
+  sqlite_trace_func = procedure (_para1:pointer; _para2:Pchar);cdecl;
+  sqlite_create_func = procedure (_para1:Psqlite_func; _para2:longint; _para3:PPchar);cdecl;
+  sqlite_handler = function (_para1:pointer; _para2:Pchar; _para3:longint):longint;cdecl;
+  sqlite_step_func = procedure (_para1:Psqlite_func; _para2:longint; _para3:PPchar);cdecl;
+  sqlite_finalize_func = procedure (_para1:Psqlite_func);cdecl;
+  sqlite_authorize_func = function (_para1:pointer; _para2:longint; _para3, _para4,_para5,_para6:Pchar):longint;cdecl;
+
+  function sqlite_create_function(_para1:Psqlite; zName:Pchar; nArg:longint; xFunc:sqlite_create_func; pUserData:pointer):longint;cdecl;external External_library name 'sqlite_create_function';
+  function sqlite_open(filename:Pchar; mode:longint; errmsg:PPchar):Psqlite;cdecl;external External_library name 'sqlite_open';
+  procedure sqlite_close(_para1:Psqlite);cdecl;external External_library name 'sqlite_close';
+  function sqlite_exec(_para1:Psqlite; sql:Pchar; _para3:sqlite_callback; _para4:pointer; errmsg:PPchar):longint;cdecl;external External_library name 'sqlite_exec';
+  function sqlite_last_insert_rowid(_para1:Psqlite):longint;cdecl;external External_library name 'sqlite_last_insert_rowid';
+  function sqlite_changes(_para1:Psqlite):longint;cdecl;external External_library name 'sqlite_changes';
+  function sqlite_error_string(_para1:longint):Pchar;cdecl;external External_library name 'sqlite_error_string';
+  procedure do_sqlite_interrupt(_para1:Psqlite);cdecl;external External_library name 'sqlite_interrupt';
+  function sqlite_complete(sql:Pchar):longint;cdecl;external External_library name 'sqlite_complete';
+  procedure sqlite_busy_handler(_para1:Psqlite; _para2:sqlite_handler; _para3:pointer);cdecl;external External_library name 'sqlite_busy_handler';
+  procedure sqlite_busy_timeout(_para1:Psqlite; ms:longint);cdecl;external External_library name 'sqlite_busy_timeout';
   function sqlite_get_table(_para1:Psqlite; sql:Pchar; resultp:PPPchar; nrow:Plongint; ncolumn:Plongint;
-             errmsg:PPchar):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_get_table';
-  procedure sqlite_free_table(result:PPchar);{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_free_table';
+             errmsg:PPchar):longint;cdecl;external External_library name 'sqlite_get_table';
+  procedure sqlite_free_table(result:PPchar);cdecl;external External_library name 'sqlite_free_table';
   function sqlite_exec_printf(_para1:Psqlite; sqlFormat:Pchar; _para3:sqlite_callback; _para4:pointer; errmsg:PPchar;
-             args:array of const):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_exec_printf';
-  function sqlite_exec_printf(_para1:Psqlite; sqlFormat:Pchar; _para3:sqlite_callback; _para4:pointer; errmsg:PPchar):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_exec_printf';
+             args:array of const):longint;cdecl;external External_library name 'sqlite_exec_printf';
+  function sqlite_exec_printf(_para1:Psqlite; sqlFormat:Pchar; _para3:sqlite_callback; _para4:pointer; errmsg:PPchar):longint;cdecl;external External_library name 'sqlite_exec_printf';
   function sqlite_exec_vprintf(_para1:Psqlite; sqlFormat:Pchar; _para3:sqlite_callback; _para4:pointer; errmsg:PPchar;
-             ap:array of const):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_exec_vprintf';
+             ap:array of const):longint;cdecl;external External_library name 'sqlite_exec_vprintf';
   function sqlite_get_table_printf(_para1:Psqlite; sqlFormat:Pchar; resultp:PPPchar; nrow:Plongint; ncolumn:Plongint;
-             errmsg:PPchar; args:array of const):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_get_table_printf';
+             errmsg:PPchar; args:array of const):longint;cdecl;external External_library name 'sqlite_get_table_printf';
   function sqlite_get_table_printf(_para1:Psqlite; sqlFormat:Pchar; resultp:PPPchar; nrow:Plongint; ncolumn:Plongint;
-             errmsg:PPchar):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_get_table_printf';
+             errmsg:PPchar):longint;cdecl;external External_library name 'sqlite_get_table_printf';
   function sqlite_get_table_vprintf(_para1:Psqlite; sqlFormat:Pchar; resultp:PPPchar; nrow:Plongint; ncolumn:Plongint;
-             errmsg:PPchar; ap:array of const):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_get_table_vprintf';
-  function sqlite_mprintf(_para1:Pchar; args:array of const):Pchar;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_mprintf';
-  function sqlite_mprintf(_para1:Pchar):Pchar;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_mprintf';
-  procedure sqlite_freemem(p:pointer);{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_freemem';
-  function sqlite_libversion:Pchar;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_libversion';
-  function sqlite_libencoding:Pchar;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_libencoding';
+             errmsg:PPchar; ap:array of const):longint;cdecl;external External_library name 'sqlite_get_table_vprintf';
+  function sqlite_mprintf(_para1:Pchar; args:array of const):Pchar;cdecl;external External_library name 'sqlite_mprintf';
+  function sqlite_mprintf(_para1:Pchar):Pchar;cdecl;external External_library name 'sqlite_mprintf';
+  procedure sqlite_freemem(p:pointer);cdecl;external External_library name 'sqlite_freemem';
+  function sqlite_libversion:Pchar;cdecl;external External_library name 'sqlite_libversion';
+  function sqlite_libencoding:Pchar;cdecl;external External_library name 'sqlite_libencoding';
   function sqlite_create_aggregate(_para1:Psqlite; zName:Pchar; nArg:longint; xStep:sqlite_step_func ; xFinalize:sqlite_finalize_func;
-             pUserData:pointer):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_create_aggregate';
-  function sqlite_function_type(db:Psqlite; zName:Pchar; datatype:longint):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_function_type';
-  function sqlite_set_result_string(_para1:Psqlite_func; _para2:Pchar; _para3:longint):Pchar;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_set_result_string';
-  procedure sqlite_set_result_int(_para1:Psqlite_func; _para2:longint);{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_set_result_int';
-  procedure sqlite_set_result_double(_para1:Psqlite_func; _para2:double);{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_set_result_double';
-  procedure sqlite_set_result_error(_para1:Psqlite_func; _para2:Pchar; _para3:longint);{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_set_result_error';
-  function sqlite_user_data(_para1:Psqlite_func):pointer;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_user_data';
-  function sqlite_aggregate_context(_para1:Psqlite_func; nBytes:longint):pointer;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_aggregate_context';
-  function sqlite_aggregate_count(_para1:Psqlite_func):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_aggregate_count';
-  function sqlite_set_authorizer(_para1:Psqlite; xAuth:sqlite_authorize_func ; pUserData:pointer):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_set_authorizer';
-  function sqlite_trace(_para1:Psqlite; xTrace:sqlite_trace_func; _para3:pointer):pointer;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_trace';
-  function sqlite_compile(db:Psqlite; zSql:Pchar; pzTail:PPchar; ppVm:PPsqlite_vm; pzErrmsg:PPchar):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_compile';
-  function sqlite_step(pVm:Psqlite_vm; pN:Plongint; pazValue:PPPchar; pazColName:PPPchar):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_step';
-  function sqlite_finalize(_para1:Psqlite_vm; pzErrMsg:PPchar):longint;{$ifdef win32}cdecl{$else}cdecl{$endif};external External_library name 'sqlite_finalize';
-
+             pUserData:pointer):longint;cdecl;external External_library name 'sqlite_create_aggregate';
+  function sqlite_function_type(db:Psqlite; zName:Pchar; datatype:longint):longint;cdecl;external External_library name 'sqlite_function_type';
+  function sqlite_set_result_string(_para1:Psqlite_func; _para2:Pchar; _para3:longint):Pchar;cdecl;external External_library name 'sqlite_set_result_string';
+  procedure sqlite_set_result_int(_para1:Psqlite_func; _para2:longint);cdecl;external External_library name 'sqlite_set_result_int';
+  procedure sqlite_set_result_double(_para1:Psqlite_func; _para2:double);cdecl;external External_library name 'sqlite_set_result_double';
+  procedure sqlite_set_result_error(_para1:Psqlite_func; _para2:Pchar; _para3:longint);cdecl;external External_library name 'sqlite_set_result_error';
+  function sqlite_user_data(_para1:Psqlite_func):pointer;cdecl;external External_library name 'sqlite_user_data';
+  function sqlite_aggregate_context(_para1:Psqlite_func; nBytes:longint):pointer;cdecl;external External_library name 'sqlite_aggregate_context';
+  function sqlite_aggregate_count(_para1:Psqlite_func):longint;cdecl;external External_library name 'sqlite_aggregate_count';
+  function sqlite_set_authorizer(_para1:Psqlite; xAuth:sqlite_authorize_func ; pUserData:pointer):longint;cdecl;external External_library name 'sqlite_set_authorizer';
+  function sqlite_trace(_para1:Psqlite; xTrace:sqlite_trace_func; _para3:pointer):pointer;cdecl;external External_library name 'sqlite_trace';
+  function sqlite_compile(db:Psqlite; zSql:Pchar; pzTail:PPchar; ppVm:PPsqlite_vm; pzErrmsg:PPchar):longint;cdecl;external External_library name 'sqlite_compile';
+  function sqlite_step(pVm:Psqlite_vm; pN:Plongint; pazValue:PPPchar; pazColName:PPPchar):longint;cdecl;external External_library name 'sqlite_step';
+  function sqlite_finalize(_para1:Psqlite_vm; pzErrMsg:PPchar):longint;cdecl;external External_library name 'sqlite_finalize';
+  
+  //Use functions instead of external variables to retrieve version and encoding info
+  function sqlite_version: PChar external External_library name 'sqlite_libversion';
+  function sqlite_encoding: PChar external External_library name 'sqlite_libencoding';
+  
 implementation
 
-
 end.

+ 73 - 111
packages/base/sqlite/sqlite3.pp

@@ -13,29 +13,73 @@ interface
   Manual corrections made by Luiz Américo - 2005
 }
 
-    const
-      External_library='sqlite3'; {Setup as you need}
-
-    Type
-    PPPChar = ^PPChar;
-    Plongint  = ^longint;
-    Psqlite3  = Pointer;
-    PPSqlite3 = ^PSqlite3;
-    Psqlite3_context  = Pointer;
-    Psqlite3_stmt  = Pointer;
-    PPsqlite3_stmt = ^Psqlite3_stmt;
-    Psqlite3_value  = Pointer;
-    PPsqlite3_value  = ^Psqlite3_value;
-    
-{$IFDEF FPC}
 {$PACKRECORDS C}
-{$ENDIF}
+
+const
+  External_library='sqlite3';
+
+  SQLITE_INTEGER = 1;   
+  SQLITE_FLOAT = 2;   
+{ #define SQLITE_TEXT  3  // See below  }
+  SQLITE_BLOB = 4;   
+  SQLITE_NULL = 5;   
+  SQLITE_TEXT = 3;   
+  SQLITE3_TEXT = 3;   
+  SQLITE_UTF8 = 1;       
+  SQLITE_UTF16LE = 2;       
+  SQLITE_UTF16BE = 3;       
+{ Use native byte order  }
+  SQLITE_UTF16 = 4;       
+{ sqlite3_create_function only  }
+  SQLITE_ANY = 5;  
+   
+   //sqlite_exec return values
+  SQLITE_OK = 0;   
+  SQLITE_ERROR = 1;{ SQL error or missing database  }
+  SQLITE_INTERNAL = 2;{ An internal logic error in SQLite  }
+  SQLITE_PERM = 3;   { Access permission denied  }
+  SQLITE_ABORT = 4; { Callback routine requested an abort  }
+  SQLITE_BUSY = 5;  { The database file is locked  }
+  SQLITE_LOCKED = 6;{ A table in the database is locked  }
+  SQLITE_NOMEM = 7; { A malloc() failed  }
+  SQLITE_READONLY = 8;{ Attempt to write a readonly database  }
+  SQLITE_INTERRUPT = 9;{ Operation terminated by sqlite3_interrupt() }
+  SQLITE_IOERR = 10;   { Some kind of disk I/O error occurred  }
+  SQLITE_CORRUPT = 11;   { The database disk image is malformed  }
+  SQLITE_NOTFOUND = 12;   { (Internal Only) Table or record not found  }
+  SQLITE_FULL = 13;   { Insertion failed because database is full  }
+  SQLITE_CANTOPEN = 14;   { Unable to open the database file  }
+  SQLITE_PROTOCOL = 15;   { Database lock protocol error  }
+  SQLITE_EMPTY = 16;   { Database is empty  }
+  SQLITE_SCHEMA = 17;   { The database schema changed  }
+  SQLITE_TOOBIG = 18;   { Too much data for one row of a table  }
+  SQLITE_CONSTRAINT = 19;   { Abort due to contraint violation  }
+  SQLITE_MISMATCH = 20;   { Data type mismatch  }
+  SQLITE_MISUSE = 21;   { Library used incorrectly  }
+  SQLITE_NOLFS = 22;   { Uses OS features not supported on host  }
+  SQLITE_AUTH = 23;   { Authorization denied  }
+  SQLITE_FORMAT = 24;   { Auxiliary database format error  }
+  SQLITE_RANGE = 25;   { 2nd parameter to sqlite3_bind out of range  }
+  SQLITE_NOTADB = 26;   { File opened that is not a database file  }
+  SQLITE_ROW = 100;   { sqlite3_step() has another row ready  }
+  SQLITE_DONE = 101;   { sqlite3_step() has finished executing  }
+
+type
+  sqlite_int64 = int64;
+  sqlite_uint64 = qword;
+  PPPChar = ^PPChar;
+  Psqlite3  = Pointer;
+  PPSqlite3 = ^PSqlite3;
+  Psqlite3_context  = Pointer;
+  Psqlite3_stmt  = Pointer;
+  PPsqlite3_stmt = ^Psqlite3_stmt;
+  Psqlite3_value  = Pointer;
+  PPsqlite3_value  = ^Psqlite3_value;
 
 //Callback function types
 //Notice that most functions were named using as prefix the function name that uses them,
 //rather than describing their functions  
 
-type
   sqlite3_callback = function (_para1:pointer; _para2:longint; _para3:PPchar; _para4:PPchar):longint;cdecl;
   busy_handler_func = function (_para1:pointer; _para2:longint):longint;cdecl;
   sqlite3_set_authorizer_func = function (_para1:pointer; _para2:longint; _para3:Pchar; _para4:Pchar; _para5:Pchar; _para6:Pchar):longint;cdecl;
@@ -50,96 +94,15 @@ type
   sqlite3_result_func = procedure (_para1:pointer);cdecl;
   sqlite3_create_collation_func = function (_para1:pointer; _para2:longint; _para3:pointer; _para4:longint; _para5:pointer):longint;cdecl;
   sqlite3_collation_needed_func = procedure (_para1:pointer; _para2:Psqlite3; eTextRep:longint; _para4:Pchar);cdecl;
-  
-
-const
-   SQLITE_VERSION = '3.2.1';   
-   SQLITE_VERSION_NUMBER = 3002001;   
-
-
-   SQLITE_INTEGER = 1;   
-   SQLITE_FLOAT = 2;   
-{ #define SQLITE_TEXT  3  // See below  }
-   SQLITE_BLOB = 4;   
-   SQLITE_NULL = 5;   
-  
-   SQLITE_TEXT = 3;   
-
-   SQLITE3_TEXT = 3;   
-
-   SQLITE_UTF8 = 1;       
-   SQLITE_UTF16LE = 2;       
-   SQLITE_UTF16BE = 3;       
-{ Use native byte order  }
-   SQLITE_UTF16 = 4;       
-{ sqlite3_create_function only  }
-   SQLITE_ANY = 5;  
-
 
+{$ifndef win32}
 var
-  sqlite3_version : Pchar;cvar;external;
+  //This is not working under windows. Any clues?
   sqlite3_temp_directory : Pchar;cvar;external;
- 
-
-function sqlite3_libversion:PChar;cdecl;external External_library name 'sqlite3_libversion';
-//function sqlite3_libversion_number:longint;cdecl;external External_library name 'sqlite3_libversion_number';
-
-type
-   sqlite_int64 = int64;
-   sqlite_uint64 = qword;
+{$endif}
 
 function sqlite3_close(_para1:Psqlite3):longint;cdecl;external External_library name 'sqlite3_close';
-
 function sqlite3_exec(_para1:Psqlite3; sql:Pchar; _para3:sqlite3_callback; _para4:pointer; errmsg:PPchar):longint;cdecl;external External_library name 'sqlite3_exec';
-
-const
-   SQLITE_OK = 0;   { SQL error or missing database  }
-   SQLITE_ERROR = 1;{ An internal logic error in SQLite  }
-   SQLITE_INTERNAL = 2;{ Access permission denied  }
-   SQLITE_PERM = 3;   { Callback routine requested an abort  }
-   SQLITE_ABORT = 4; { The database file is locked  }
-   SQLITE_BUSY = 5;  { A table in the database is locked  }
-   SQLITE_LOCKED = 6;{ A malloc() failed  }
-   SQLITE_NOMEM = 7; { Attempt to write a readonly database  }
-   SQLITE_READONLY = 8;{ Operation terminated by sqlite3_interrupt() }
-   SQLITE_INTERRUPT = 9;{ Some kind of disk I/O error occurred  }
-   SQLITE_IOERR = 10;   { The database disk image is malformed  }
-   SQLITE_CORRUPT = 11;   
-{ (Internal Only) Table or record not found  }
-   SQLITE_NOTFOUND = 12;   
-{ Insertion failed because database is full  }
-   SQLITE_FULL = 13;   
-{ Unable to open the database file  }
-   SQLITE_CANTOPEN = 14;   
-{ Database lock protocol error  }
-   SQLITE_PROTOCOL = 15;   
-{ Database is empty  }
-   SQLITE_EMPTY = 16;   
-{ The database schema changed  }
-   SQLITE_SCHEMA = 17;   
-{ Too much data for one row of a table  }
-   SQLITE_TOOBIG = 18;   
-{ Abort due to contraint violation  }
-   SQLITE_CONSTRAINT = 19;   
-{ Data type mismatch  }
-   SQLITE_MISMATCH = 20;   
-{ Library used incorrectly  }
-   SQLITE_MISUSE = 21;   
-{ Uses OS features not supported on host  }
-   SQLITE_NOLFS = 22;   
-{ Authorization denied  }
-   SQLITE_AUTH = 23;   
-{ Auxiliary database format error  }
-   SQLITE_FORMAT = 24;   
-{ 2nd parameter to sqlite3_bind out of range  }
-   SQLITE_RANGE = 25;   
-{ File opened that is not a database file  }
-   SQLITE_NOTADB = 26;   
-{ sqlite3_step() has another row ready  }
-   SQLITE_ROW = 100;   
-{ sqlite3_step() has finished executing  }
-   SQLITE_DONE = 101;   
-
 function sqlite3_last_insert_rowid(_para1:Psqlite3):sqlite_int64;cdecl;external External_library name 'sqlite3_last_insert_rowid';
 function sqlite3_changes(_para1:Psqlite3):longint;cdecl;external External_library name 'sqlite3_changes';
 function sqlite3_total_changes(_para1:Psqlite3):longint;cdecl;external External_library name 'sqlite3_total_changes';
@@ -162,7 +125,6 @@ function sqlite3_snprintf(_para1:longint; _para2:Pchar; _para3:Pchar):Pchar;cdec
 
 function sqlite3_set_authorizer(_para1:Psqlite3; xAuth:sqlite3_set_authorizer_func; pUserData:pointer):longint;cdecl;external External_library name 'sqlite3_set_authorizer';
 
-
 const
    SQLITE_COPY = 0;   
 { Index Name      Table Name       }
@@ -222,9 +184,9 @@ const
 
 { #define SQLITE_OK  0   // Allow access (This is actually defined above)  }
 { Abort the SQL statement with an error  }
-   SQLITE_DENY = 1;   
+  SQLITE_DENY = 1;   
 { Don't allow access, but don't generate an error  }
-   SQLITE_IGNORE = 2;   
+  SQLITE_IGNORE = 2;   
 
 function sqlite3_trace(_para1:Psqlite3; xTrace:sqlite3_trace_func; _para3:pointer):pointer;cdecl;external External_library name 'sqlite3_trace';
 procedure sqlite3_progress_handler(_para1:Psqlite3; _para2:longint; _para3:sqlite3_progress_handler_func; _para4:pointer);cdecl;external External_library name 'sqlite3_progress_handler';
@@ -318,19 +280,19 @@ function sqlite3_create_collation16(_para1:Psqlite3; zName:Pchar; eTextRep:longi
  
 function sqlite3_collation_needed(_para1:Psqlite3; _para2:pointer; _para3:sqlite3_collation_needed_func):longint;cdecl;external External_library name 'sqlite3_collation_needed';
 function sqlite3_collation_needed16(_para1:Psqlite3; _para2:pointer; _para3:sqlite3_collation_needed_func):longint;cdecl;external External_library name 'sqlite3_collation_needed16';
- 
+
+function sqlite3_libversion:PChar;cdecl;external External_library name 'sqlite3_libversion';
+//Alias for allowing better code portability (win32 is not working with external variables) 
+function sqlite3_version:PChar;cdecl;external External_library name 'sqlite3_libversion';
+
+// Not published functions
+//function sqlite3_libversion_number:longint;cdecl;external External_library name 'sqlite3_libversion_number';
 //function sqlite3_key(db:Psqlite3; pKey:pointer; nKey:longint):longint;cdecl;external External_library name 'sqlite3_key';
- 
 //function sqlite3_rekey(db:Psqlite3; pKey:pointer; nKey:longint):longint;cdecl;external External_library name 'sqlite3_rekey';
- 
 //function sqlite3_sleep(_para1:longint):longint;cdecl;external External_library name 'sqlite3_sleep';
- 
 //function sqlite3_expired(_para1:Psqlite3_stmt):longint;cdecl;external External_library name 'sqlite3_expired';
-   
 //function sqlite3_global_recover:longint;cdecl;external External_library name 'sqlite3_global_recover';
 
-
 implementation
 
-
 end.