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.cr33
1 files changed, 16 insertions, 17 deletions
diff --git a/src/sqlite.cr b/src/sqlite.cr
index 1d89dc4..2975031 100644
--- a/src/sqlite.cr
+++ b/src/sqlite.cr
@@ -19,12 +19,15 @@
 @[Link("sqlite3")]
 lib SQLite
   OK = 0
-  DELETE = 9
-  INSERT = 18
-  UPDATE = 23
   ROW = 100
   DONE = 101
 
+  enum UpdateAction : LibC::Int
+    Delete = 9
+    Insert = 18
+    Update = 23
+  end
+
   type Database = Void*
   type Statement = Void*
 
@@ -35,7 +38,7 @@ lib SQLite
   fun open = sqlite3_open(filename : LibC::Char*, db : Database*) : LibC::Int
   fun close = sqlite3_close(db : Database) : LibC::Int
   fun update_hook = sqlite3_update_hook(db : Database,
-                                        f : (Void*, LibC::Int, LibC::Char*,
+                                        f : (Void*, UpdateAction, LibC::Char*,
                                              LibC::Char*, Int64 ->),
                                         arg : Void*)
 
@@ -120,8 +123,9 @@ class Database
     end
   end
 
-  def initialize(path : String, opennic, icann)
-    Database.check SQLite.open path, out @ref
+  def initialize(path, opennic, icann)
+    Dir.mkdir_p path.parent
+    Database.check SQLite.open path.to_s, out @ref
     @members = {} of Int64 => {String, String, String}
     @applicants = {} of Int64 => {String, String, String}
 
@@ -162,18 +166,18 @@ class Database
       case table
       when "member"
         case action
-        when SQLite::DELETE
+        in .delete?
           obj.members.delete rowid
-        when SQLite::INSERT, SQLite::UPDATE
+        in .insert?, .update?
           obj.exec SELECT_MEMBER_ROW, rowid do |row|
             obj.members[rowid] = {row[0].text, row[1].text, row[2].text}
           end
         end
       when "applicant"
         case action
-        when SQLite::DELETE
+        in .delete?
           obj.applicants.delete rowid
-        when SQLite::INSERT, SQLite::UPDATE
+        in .insert?, .update?
           obj.exec SELECT_APPLICANT_ROW, rowid do |row|
             obj.applicants[rowid] = {row[0].text, row[1].text, row[2].text}
           end
@@ -205,13 +209,8 @@ class Database
     self.exec "COMMIT" do end
   end
 
-  def members
-    @members
-  end
-
-  def applicants
-    @applicants
-  end
+  getter members
+  getter applicants
 
   def add_applicant(nick, opennic, icann)
     self.exec INSERT_APPLICANT, nick, opennic, icann do end