reshuffling of file locations to make package structure flatter
[org.ibex.mail.git] / src / org / ibex / mail / SqliteJdbcMailbox.java
similarity index 65%
rename from src/org/ibex/mail/target/SqliteJdbcMailbox.java
rename to src/org/ibex/mail/SqliteJdbcMailbox.java
index a0afc8f..af8ee5c 100644 (file)
@@ -1,6 +1,5 @@
-package org.ibex.mail.target;
+package org.ibex.mail;
 
-import org.ibex.mail.*;
 import org.ibex.io.Fountain;
 import org.ibex.io.Stream;
 import java.sql.Timestamp;
@@ -9,26 +8,32 @@ import java.net.*;
 import java.io.*;
 import java.util.*;
 
+// nntpNumber (column)
+// uid (column)
+// uidvalidity
 public class SqliteJdbcMailbox extends Mailbox.Default {
 
     private Connection conn;
+    private static final String columns = "messageid_,from_,to_,date_,subject_,headers_,body_,flags_";
 
     public SqliteJdbcMailbox(String filename) {
         try {
             Class.forName("org.sqlite.JDBC");
             conn = DriverManager.getConnection("jdbc:sqlite:"+filename);
-            conn.prepareStatement("create table if not exists "+
-                                  "'mail' (messageid_,from_,to_,date_,subject_,headers_,body_,PRIMARY KEY(messageid_))").executeUpdate();
+            conn.prepareStatement("create virtual table if not exists "+
+                                  "'mail' using FTS1("+columns+",PRIMARY KEY(messageid_))").executeUpdate();
         }
         catch (SQLException e) { throw new RuntimeException(e); }
         catch (ClassNotFoundException e) { throw new RuntimeException(e); }
     }
 
-    public Mailbox.Iterator iterator()                      { return new SqliteJdbcIterator(); }
+    public int              uidNext()                    { throw new RuntimeException("not supported"); }
+    public Mailbox.Iterator iterator()                   { return new SqliteJdbcIterator(); }
     public void             insert(Message m, int flags) {
         // FIXME: flags
         try {
-            PreparedStatement add = conn.prepareStatement("insert into 'mail' values (?,?,?,?,?,?,?)");
+            PreparedStatement add =
+                conn.prepareStatement("insert into 'mail' ("+columns+") values (?,?,?,?,?,?,?,?)");
             add.setString(1, m.messageid+"");
             add.setString(2, m.from+"");
             add.setString(3, m.to+"");
@@ -36,15 +41,17 @@ public class SqliteJdbcMailbox extends Mailbox.Default {
             add.setString(5, m.subject+"");
             add.setString(6, streamToString(m.headers.getStream()));
             add.setString(7, streamToString(m.getBody().getStream()));
+            add.setInt   (8, flags);
             add.executeUpdate();
         } catch (Exception e) { throw new RuntimeException(e); }
     }
-    public int              uidNext()                       { throw new RuntimeException("not supported"); }
 
     private class SqliteJdbcIterator extends Mailbox.Default.Iterator {
+        // could be more efficient in a ton of ways
         private ResultSet rs;
-        private int count=1;
-        private Message m = null;
+        private int count  = 1;
+        private int flags;
+        private Message m  = null;
         public SqliteJdbcIterator() {
             try {
                 PreparedStatement query = conn.prepareStatement("select messageid_ from 'mail'");
@@ -56,19 +63,21 @@ public class SqliteJdbcMailbox extends Mailbox.Default {
             try {
                 if (m!=null) return m;
                 rs.next();
-                PreparedStatement query = conn.prepareStatement("select headers_,body_ from 'mail' where messageid_=?");
+                PreparedStatement query = conn.prepareStatement("select headers_,body_,flags_ from 'mail' where messageid_=?");
                 query.setString(1, rs.getString(1));
                 ResultSet rs2 = query.executeQuery();
                 if (!rs.next()) return null;
                 m = Message.newMessage(new Fountain.StringFountain(rs.getString(1) + "\r\n\r\n" + rs.getString(2)));
+                flags = rs.getInt(3);
                 return m;
             } catch (Exception e) { throw new RuntimeException(e); }
         }
-        public Headers head()   { return cur().headers; }
-        public boolean next()   { try { m = null; count++; return rs.next(); } catch (Exception e) { throw new RuntimeException(e); } }
-        public int     uid()    { throw new RuntimeException("not supported"); }
-        public int     nntpNumber() { throw new RuntimeException("not supported"); }
+        public int     getFlags()   { if (m==null) /* could be more efficient */ cur(); return flags; }
+        public Headers head()       { return cur().headers; }
+        public boolean next()       { try { m = null; count++; return rs.next(); } catch (Exception e) { throw new RuntimeException(e); } }
+        public int     uid()        { throw new RuntimeException("not supported"); }
         public int     imapNumber() { return count; }
+        public int     nntpNumber() { throw new RuntimeException("not supported"); }
         public void    delete()     { throw new RuntimeException("not supported"); }
     }
 
@@ -79,4 +88,4 @@ public class SqliteJdbcMailbox extends Mailbox.Default {
         return b.toString();
     }
 
-}
\ No newline at end of file
+}