From 109379f51430ea057d810791b43ba02c22a30e46 Mon Sep 17 00:00:00 2001 From: Jedidiah Barber Date: Wed, 3 Nov 2021 12:30:31 +1300 Subject: SQL binding now automatically closes databases and finishes statements --- src/sqlite3.ads | 92 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 55 insertions(+), 37 deletions(-) (limited to 'src/sqlite3.ads') diff --git a/src/sqlite3.ads b/src/sqlite3.ads index 667fdd6..df49e2c 100644 --- a/src/sqlite3.ads +++ b/src/sqlite3.ads @@ -2,16 +2,18 @@ with - Ada.Strings.Unbounded, - System, - Interfaces.C; + Ada.Strings.Unbounded; + +private with + + Ada.Finalization; package SQLite3 is - type SQLite3_DB is private; - type SQLite3_Statement is private; + type SQLite3_DB is tagged limited private; + type SQLite3_Statement is tagged limited private; type Open_Flag is mod 2**32; type Prepare_Flag is mod 2**32; @@ -19,7 +21,6 @@ package SQLite3 is subtype SQL_Parameter_Index is Integer range 1 .. Integer'Last; subtype SQL_Column_Index is Integer; - type Int is new Interfaces.C.int; OPEN_READONLY : constant Open_Flag := 1; @@ -56,62 +57,66 @@ package SQLite3 is procedure Open - (Filename : in String; - Handle : out SQLite3_DB); + (Handle : out SQLite3_DB; + Filename : in String); procedure Open - (Filename : in String; - Handle : out SQLite3_DB; + (Handle : out SQLite3_DB; + Filename : in String; Flags : in Open_Flag); procedure Close (Handle : in out SQLite3_DB); + procedure Prepare (Handle : in SQLite3_DB; SQL : in String; - SQL_Handle : out SQLite3_Statement); + SQL_Handle : out SQLite3_Statement'Class); procedure Prepare (Handle : in SQLite3_DB; SQL : in String; Flags : in Prepare_Flag; - SQL_Handle : out SQLite3_Statement); + SQL_Handle : out SQLite3_Statement'Class); procedure Step - (SQL_Handle : in SQLite3_Statement; - Status : out Status_Code); + (SQL_Handle : in out SQLite3_Statement); + + function Status + (SQL_Handle : in SQLite3_Statement) + return Status_Code; procedure Finish - (SQL_Handle : in SQLite3_Statement); + (SQL_Handle : in out SQLite3_Statement); procedure Reset - (SQL_Handle : in SQLite3_Statement); + (SQL_Handle : in out SQLite3_Statement); procedure Bind - (SQL_Handle : in SQLite3_Statement; - Index : in SQL_Parameter_Index; - Value : in Integer); + (SQL_Handle : in out SQLite3_Statement; + Index : in SQL_Parameter_Index; + Value : in Integer); 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); 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); - 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; - 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; private @@ -132,16 +137,29 @@ private type DB_Private is null record; type DB_Private_Access is access all DB_Private; - type SQLite3_DB is record - Ptr : aliased DB_Private_Access; + type SQLite3_DB is new Ada.Finalization.Limited_Controlled with record + Ptr : aliased DB_Private_Access; + Prep : Boolean := False; end record; + overriding + procedure Finalize + (This : in out SQLite3_DB); + + type Statement_Private is null record; type Statement_Private_Access is access all Statement_Private; - type SQLite3_Statement is record - Ptr : aliased Statement_Private_Access; + + type SQLite3_Statement is new Ada.Finalization.Limited_Controlled with record + Ptr : aliased Statement_Private_Access; + Status : Status_Code := SQLITE_OK; + Prep : Boolean := False; end record; + overriding + procedure Finalize + (This : in out SQLite3_Statement); + end SQLite3; -- cgit