From b0a4d544d0118aed6f631a9b2cbedc8254f3dbcc Mon Sep 17 00:00:00 2001 From: Olivier Ramonat Date: Thu, 13 Mar 2008 17:59:12 +0100 Subject: [PATCH] Add support for in-memory database --- src/db-sqlite.adb | 28 ++++++++++++++++++++++++---- src/db-sqlite.ads | 5 ++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/db-sqlite.adb b/src/db-sqlite.adb index d044ce2..d752d26 100755 --- a/src/db-sqlite.adb +++ b/src/db-sqlite.adb @@ -30,6 +30,9 @@ package body DB.SQLite is Module : constant Logs.Module_Name := "DB_SQLITE"; + Unique_Handle : SQLite3.Handle := null; + -- Unique handle to use when we want to use in memory connection + procedure Unchecked_Free is new Unchecked_Deallocation (Object => SQLite3.Object, Name => SQLite3.Handle); @@ -133,11 +136,28 @@ package body DB.SQLite is begin Logs.Write (Module, "connect " & Logs.NV ("Name", Name)); - if DB.H = null then - DB.H := new GNU.DB.SQLite3.Object; + if Name = In_Memory_Database then + if Unique_Handle = null then + + -- Open only one database connection ! + + Unique_Handle := new GNU.DB.SQLite3.Object; + DB.H := Unique_Handle; + SQLite_Safe.Open (DB, Name, Result); + Check_Result ("connect", Result); + + elsif DB.H = null then + -- Get the open database connection + DB.H := Unique_Handle; + end if; + else + if DB.H = null then + DB.H := new GNU.DB.SQLite3.Object; + end if; + + SQLite_Safe.Open (DB, Name, Result); + Check_Result ("connect", Result); end if; - SQLite_Safe.Open (DB, Name, Result); - Check_Result ("connect", Result); end Connect; ---------------- diff --git a/src/db-sqlite.ads b/src/db-sqlite.ads index 9f5a989..7cdc1d8 100644 --- a/src/db-sqlite.ads +++ b/src/db-sqlite.ads @@ -27,7 +27,10 @@ package DB.SQLite is type Handle is new DB.Handle with private; - -- Open / Close + In_Memory_Database : constant String := ":memory:"; + -- When the database name is ":memory:", a private in-memory + -- database is created for the connection. This in-memory + -- database will vanish when the database connection is closed. overriding procedure Connect (DB : in out Handle; -- 2.11.4.GIT