break out SqliteTable
[org.ibex.mail.git] / src / org / ibex / mail / SqliteDB.java
index 39acded..e5fbc68 100644 (file)
@@ -15,8 +15,18 @@ public class SqliteDB {
 
     protected Connection conn;
     private String filename;
-    private String reapTable;
-    private String reapColumn;
+
+    private HashMap<String,SqliteTable> tables = new HashMap<String,SqliteTable>();
+
+    public static final int REAPER_INTERVAL_SECONDS = 60 * 60;
+
+    public Connection getConnection() { return conn; }
+
+    public synchronized SqliteTable getTable(String name) {
+        SqliteTable ret = tables.get(name);
+        if (ret==null) ret = new SqliteTable(name);
+        return ret;
+    }
 
     // check upstream: PRAGMA encoding = "UTF-8"; 
     // create indices
@@ -47,18 +57,28 @@ public class SqliteDB {
         catch (ClassNotFoundException e) { throw new RuntimeException(e); }
     }
 
-    protected void reap(String reapTable, String reapColumn) {
-        if (this.reapTable != null || this.reapColumn != null)
-            throw new RuntimeException("reapTable/reapColumn already set");
-        this.reapTable = reapTable;
-        this.reapColumn = reapColumn;
-        if (reapTable != null && reapColumn != null)
-            Main.cron.executeLater(1000 * REAPER_INTERVAL_SECONDS, new Reaper());
+    public class SqliteTable {
+        public final String name;
+        private String reapColumn = null;
+        private SqliteTable(String name) {
+            this.name = name;
+            tables.put(name, this);
+        }
+        protected void reap(String reapColumn) {
+            if (this.reapColumn != null) throw new RuntimeException("reapColumn already set");
+            this.reapColumn = reapColumn;
+            Main.cron.executeLater(1000 * REAPER_INTERVAL_SECONDS, new Reaper(name, reapColumn));
+        }
     }
 
-    public static final int REAPER_INTERVAL_SECONDS = 60 * 60;
-
+    // FIXME: desynchronized access to the conn?
     private class Reaper implements Runnable {
+        public Reaper(String reapTable, String reapColumn) {
+            this.reapTable = reapTable;
+            this.reapColumn = reapColumn;
+        }
+        private String reapTable;
+        private String reapColumn;
         public void run() {
             try {
                 Log.warn(Reaper.class, filename + " reaping...");