diff options
Diffstat (limited to 'src/sqlite3.adb')
-rw-r--r-- | src/sqlite3.adb | 146 |
1 files changed, 92 insertions, 54 deletions
diff --git a/src/sqlite3.adb b/src/sqlite3.adb index 081d076..d39fd6c 100644 --- a/src/sqlite3.adb +++ b/src/sqlite3.adb @@ -8,9 +8,25 @@ with package body SQLite3 is + procedure Finalize + (This : in out SQLite3_DB) is + begin + This.Close; + end Finalize; + + + procedure Finalize + (This : in out SQLite3_Statement) is + begin + This.Finish; + end Finalize; + + + + procedure Open - (Filename : in String; - Handle : out SQLite3_DB) + (Handle : out SQLite3_DB; + Filename : in String) is function sqlite3_open (Filename : in Interfaces.C.char_array; @@ -21,6 +37,7 @@ package body SQLite3 is Code : Interfaces.C.int; begin Code := sqlite3_open (Interfaces.C.To_C (Filename), Handle.Ptr'Access); + Handle.Prep := True; if Status_Code (Code) /= SQLITE_OK then raise Program_Error with Error_Message (Handle); end if; @@ -28,8 +45,8 @@ package body SQLite3 is procedure Open - (Filename : in String; - Handle : out SQLite3_DB; + (Handle : out SQLite3_DB; + Filename : in String; Flags : in Open_Flag) is function sqlite3_open_v2 @@ -47,6 +64,7 @@ package body SQLite3 is Handle.Ptr'Access, Interfaces.C.int (Flags), Interfaces.C.To_C ("")); + Handle.Prep := True; if Status_Code (Code) /= SQLITE_OK then raise Program_Error with Error_Message (Handle); end if; @@ -62,20 +80,24 @@ package body SQLite3 is with Import => True, Convention => C; Code : Interfaces.C.int; - Status : Status_Code; begin + if Handle.Prep = False then + return; + end if; Code := sqlite3_close (Handle.Ptr); - Status := Status_Code (Code); - if Status /= SQLITE_OK and Status /= SQLITE_ROW and Status /= SQLITE_DONE then + Handle.Prep := False; + if Status_Code (Code) /= SQLITE_OK then raise Program_Error with Error_Message (Handle); end if; end Close; + + procedure Prepare (Handle : in SQLite3_DB; SQL : in String; - SQL_Handle : out SQLite3_Statement) + SQL_Handle : out SQLite3_Statement'Class) is type Chars_Ptr_Ptr is access all Interfaces.C.Strings.chars_ptr; @@ -104,7 +126,9 @@ package body SQLite3 is nByte => SQL_Str'Length, ppStmt => SQL_Handle.Ptr'Access, pzTail => null); - if Status_Code (Code) /= SQLITE_OK then + SQL_Handle.Prep := True; + SQL_Handle.Status := Status_Code (Code); + if SQL_Handle.Status /= SQLITE_OK then raise Program_Error with Error_Message (Handle); end if; end Prepare; @@ -114,7 +138,7 @@ package body SQLite3 is (Handle : in SQLite3_DB; SQL : in String; Flags : in Prepare_Flag; - SQL_Handle : out SQLite3_Statement) + SQL_Handle : out SQLite3_Statement'Class) is type Chars_Ptr_Ptr is access all Interfaces.C.Strings.chars_ptr; @@ -146,15 +170,16 @@ package body SQLite3 is prepFlags => Interfaces.C.unsigned (Flags), ppStmt => SQL_Handle.Ptr'Access, pzTail => null); - if Status_Code (Code) /= SQLITE_OK then + SQL_Handle.Prep := True; + SQL_Handle.Status := Status_Code (Code); + if SQL_Handle.Status /= SQLITE_OK then raise Program_Error with Error_Message (Handle); end if; end Prepare; procedure Step - (SQL_Handle : in SQLite3_Statement; - Status : out Status_Code) + (SQL_Handle : in out SQLite3_Statement) is -- int sqlite3_step(sqlite3_stmt*); function sqlite3_step @@ -165,15 +190,26 @@ package body SQLite3 is Code : Interfaces.C.int; begin Code := sqlite3_step (SQL_Handle.Ptr); - Status := Status_Code (Code); - if Status /= SQLITE_OK and Status /= SQLITE_ROW and Status /= SQLITE_DONE then - raise Program_Error with Error_String (Status); + SQL_Handle.Status := Status_Code (Code); + if SQL_Handle.Status /= SQLITE_OK and + SQL_Handle.Status /= SQLITE_ROW and + SQL_Handle.Status /= SQLITE_DONE + then + raise Program_Error with Error_String (SQL_Handle.Status); end if; end Step; - procedure Finish + function Status (SQL_Handle : in SQLite3_Statement) + return Status_Code is + begin + return SQL_Handle.Status; + end Status; + + + procedure Finish + (SQL_Handle : in out SQLite3_Statement) is -- int sqlite3_finalize(sqlite3_stmt*); function sqlite3_finalize @@ -182,18 +218,21 @@ package body SQLite3 is with Import => True, Convention => C; Code : Interfaces.C.int; - Status : Status_Code; begin + if SQL_Handle.Prep = False then + return; + end if; Code := sqlite3_finalize (SQL_Handle.Ptr); - Status := Status_Code (Code); - if Status /= SQLITE_OK then - raise Program_Error with Error_String (Status); + SQL_Handle.Prep := False; + SQL_Handle.Status := Status_Code (Code); + if SQL_Handle.Status /= SQLITE_OK then + raise Program_Error with Error_String (SQL_Handle.Status); end if; end Finish; procedure Reset - (SQL_Handle : in SQLite3_Statement) + (SQL_Handle : in out SQLite3_Statement) is -- int sqlite3_reset(sqlite3_stmt*); function sqlite3_reset @@ -202,29 +241,28 @@ package body SQLite3 is with Import => True, Convention => C; Code : Interfaces.C.int; - Status : Status_Code; begin Code := sqlite3_reset (SQL_Handle.Ptr); - Status := Status_Code (Code); - if Status /= SQLITE_OK then - raise Program_Error with Error_String (Status); + SQL_Handle.Status := Status_Code (Code); + if SQL_Handle.Status /= SQLITE_OK then + raise Program_Error with Error_String (SQL_Handle.Status); end if; end Reset; procedure Bind - (SQL_Handle : in SQLite3_Statement; - Index : in SQL_Parameter_Index; - Value : in Integer) is + (SQL_Handle : in out SQLite3_Statement; + Index : in SQL_Parameter_Index; + Value : in Integer) is begin Bind (SQL_Handle, Index, Long_Integer (Value)); end Bind; procedure Bind - (SQL_Handle : in SQLite3_Statement; - Index : in SQL_Parameter_Index; - Value : in Long_Integer) + (SQL_Handle : in out SQLite3_Statement; + Index : in SQL_Parameter_Index; + Value : in Long_Integer) is -- int sqlite3_bind_int(sqlite3_stmt*, int, int); function sqlite3_bind_int @@ -235,23 +273,22 @@ package body SQLite3 is with Import => True, Convention => C; Code : Interfaces.C.int; - Status : Status_Code; begin Code := sqlite3_bind_int (Stmt => SQL_Handle.Ptr, Index => Interfaces.C.int (Index), Value => Interfaces.C.int (Value)); - Status := Status_Code (Code); - if Status /= SQLITE_OK then - raise Program_Error with Error_String (Status); + SQL_Handle.Status := Status_Code (Code); + if SQL_Handle.Status /= SQLITE_OK then + raise Program_Error with Error_String (SQL_Handle.Status); end if; end Bind; procedure Bind - (SQL_Handle : in SQLite3_Statement; - Index : in SQL_Parameter_Index; - Value : in Ada.Strings.Unbounded.Unbounded_String) + (SQL_Handle : in out SQLite3_Statement; + Index : in SQL_Parameter_Index; + Value : in Ada.Strings.Unbounded.Unbounded_String) is use Ada.Strings.Unbounded; @@ -271,7 +308,6 @@ package body SQLite3 is with Import => True, Convention => C; Code : Interfaces.C.int; - Status : Status_Code; begin Code := sqlite3_bind_text (Stmt => SQL_Handle.Ptr, @@ -279,17 +315,17 @@ package body SQLite3 is Value => Interfaces.C.To_C (To_String (Value)), Bytes => Interfaces.C.int (Length (Value)), Opt => SQLITE_TRANSIENT); - Status := Status_Code (Code); - if Status /= SQLITE_OK then - raise Program_Error with Error_String (Status); + SQL_Handle.Status := Status_Code (Code); + if SQL_Handle.Status /= SQLITE_OK then + raise Program_Error with Error_String (SQL_Handle.Status); end if; end Bind; - procedure Column - (SQL_Handle : in SQLite3_Statement; - Index : in SQL_Column_Index; - Value : out Int) + function Column + (SQL_Handle : in SQLite3_Statement; + Index : in SQL_Column_Index) + return Integer is -- int sqlite3_column_int(sqlite3_stmt*, int iCol); function sqlite3_column_int @@ -303,14 +339,14 @@ package body SQLite3 is Ret_Val := sqlite3_column_int (Stmt => SQL_Handle.Ptr, Index => Interfaces.C.int (Index)); - Value := Int (Ret_Val); + return Integer (Ret_Val); end Column; - procedure Column - (SQL_Handle : in SQLite3_Statement; - Index : in SQL_Column_Index; - Value : out Ada.Strings.Unbounded.Unbounded_String) + function Column + (SQL_Handle : in SQLite3_Statement; + Index : in SQL_Column_Index) + return Ada.Strings.Unbounded.Unbounded_String is use Ada.Strings.Unbounded; use Interfaces; @@ -329,13 +365,15 @@ package body SQLite3 is (Stmt => SQL_Handle.Ptr, Index => Interfaces.C.int (Index)); if Ret_Val = C.Strings.Null_Ptr then - Value := Null_Unbounded_String; + return Null_Unbounded_String; else - Value := To_Unbounded_String (C.To_Ada (C.Strings.Value (Ret_Val))); + return To_Unbounded_String (C.To_Ada (C.Strings.Value (Ret_Val))); end if; end Column; + + function Error_Message (Handle : in SQLite3_DB) return String |