From: adam Date: Sun, 22 Jul 2007 02:28:56 +0000 (+0000) Subject: SqliteMailbox: massive overhauld X-Git-Url: http://git.megacz.com/?p=org.ibex.mail.git;a=commitdiff_plain;h=1bd96ef617f2749c253c722d51890fcfc8b29ac4 SqliteMailbox: massive overhauld darcs-hash:20070722022856-5007d-1f080c1fc194728431f83cc867f274934da8706f.gz --- diff --git a/src/org/ibex/mail/SqliteMailbox.java b/src/org/ibex/mail/SqliteMailbox.java index 66ed674..b28ef7f 100644 --- a/src/org/ibex/mail/SqliteMailbox.java +++ b/src/org/ibex/mail/SqliteMailbox.java @@ -1,5 +1,6 @@ package org.ibex.mail; +import org.ibex.util.*; import org.ibex.io.Fountain; import org.ibex.io.Stream; import java.sql.Timestamp; @@ -8,10 +9,15 @@ import java.net.*; import java.io.*; import java.util.*; -// nntpNumber (column) -// uid (column) -// uidvalidity -public class SqliteMailbox extends Mailbox.Default { + +public class SqliteMailbox extends Mailbox.Default implements MailTree { + + public MailTree slash(String name, boolean create) { return null; } + public String[] children() { return new String[0]; } + public void rmdir(String subdir) { throw new RuntimeException("invalid"); } + public void rename(String subdir, MailTree newParent, String newName) { throw new RuntimeException("invalid"); } + public Mailbox getMailbox() { return this; } + private Connection conn; private static final String columns = @@ -32,23 +38,129 @@ public class SqliteMailbox extends Mailbox.Default { private static final String columns_ = "uid_ INTEGER PRIMARY KEY AUTOINCREMENT, messageid_ unique,from_,to_,date_,subject_,headers_,body_,flags_"; - public SqliteMailbox(String filename) { + private final int uidValidity; + private final File file; + public int uidValidity() { return uidValidity; } + + public SqliteMailbox(String filename) throws SQLException { try { + this.file = new File(filename); Class.forName("org.sqlite.JDBC"); conn = DriverManager.getConnection("jdbc:sqlite:"+filename); - conn.prepareStatement("create virtual table if not exists 'mail' using FTS2("+columns_+")").executeUpdate(); + conn.prepareStatement("create table if not exists uidvalidity (uidvalidity)").executeUpdate(); + ResultSet rs = conn.prepareStatement("select uidvalidity from uidvalidity").executeQuery(); + if (!rs.next()) { + this.uidValidity = new Random().nextInt(); + PreparedStatement ps = conn.prepareStatement("insert into uidvalidity (uidvalidity) values (?)"); + ps.setInt(1, uidValidity); + ps.executeUpdate(); + } else { + this.uidValidity = rs.getInt(1); + } + try { + //conn.prepareStatement("create virtual table 'mail' using FTS2("+columns_+")").executeUpdate(); + conn.prepareStatement("create table 'mail' ("+columns_+")").executeUpdate(); + } catch (SQLException e) { + /* FIXME */ + } + conn.prepareStatement("create index if not exists uid_index on mail(uid_);").executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } - public int uidNext() { throw new RuntimeException("not supported"); } + public int uidNext() { + try { + PreparedStatement q = conn.prepareStatement("select max(uid_) from mail"); + ResultSet rs = q.executeQuery(); + if (!rs.next()) return -1; + return rs.getInt(1)+1; + } catch (Exception e) { throw new RuntimeException(e); } + } + public Mailbox.Iterator iterator() { return new SqliteJdbcIterator(); } + private static String set(int[] set, String arg) { + String whereClause = ""; + boolean needsOr = false; + for(int i=0; i=" + set[i]; + whereClause += " and "; + while(i+2 < set.length && set[i+2] == (set[i+1]+1)) i += 2; + whereClause += arg+"<=" + set[i+1]; + whereClause += ")"; + needsOr = true; + } + return whereClause; + } + private static String joinWith(String op, Query[] q) throws UnsupportedQueryException { + boolean add = false; + StringBuffer sb = new StringBuffer(); + for(int i=0; i