summary refs log tree commit diff homepage
path: root/src/sqlite.cr
diff options
context:
space:
mode:
Diffstat (limited to 'src/sqlite.cr')
-rw-r--r--src/sqlite.cr13
1 files changed, 11 insertions, 2 deletions
diff --git a/src/sqlite.cr b/src/sqlite.cr
index 9ae1c51..60f2e19 100644
--- a/src/sqlite.cr
+++ b/src/sqlite.cr
@@ -19,6 +19,7 @@
 @[Link("sqlite3")]
 lib SQLite
   OK = 0
+  BUSY = 5
   ROW = 100
   DONE = 101
 
@@ -36,7 +37,7 @@ lib SQLite
   fun free = sqlite3_free(format : Void*, ...)
 
   fun open = sqlite3_open(filename : LibC::Char*, db : Database*) : LibC::Int
-  fun close = sqlite3_close(db : Database) : LibC::Int
+  fun close = sqlite3_close_v2(db : Database) : LibC::Int
   fun update_hook = sqlite3_update_hook(db : Database,
                                         f : (Void*, UpdateAction, LibC::Char*,
                                              LibC::Char*, Int64 ->),
@@ -119,6 +120,12 @@ class Database
     end
   end
 
+  def initialize(path)
+    Dir.mkdir_p path.parent
+    Database.check SQLite.open path.to_s, out @ref
+    self.exec "PRAGMA foreign_keys = ON" do end
+  end
+
   def initialize(path, opennic, icann)
     Dir.mkdir_p path.parent
     Database.check SQLite.open path.to_s, out @ref
@@ -162,6 +169,8 @@ class Database
         yield stmt.row
       when SQLite::DONE
         break
+      when SQLite::BUSY
+        next # FIXME: rollback transaction
       else
         Database.check rc
       end
@@ -169,7 +178,7 @@ class Database
   end
 
   def transact
-    self.exec "BEGIN TRANSACTION" do end
+    self.exec "BEGIN" do end
     yield
     self.exec "COMMIT" do end
   end