From bb01fef4bbee79ac0a1a2acb2303bde46a23d7a4 Mon Sep 17 00:00:00 2001 From: adam Date: Mon, 15 Jan 2007 08:08:31 +0000 Subject: [PATCH] add SqliteJdbcMailbox darcs-hash:20070115080831-5007d-9c01095c8de30ff4f5e3e3c206cf4837b2b349b2.gz --- src/org/ibex/mail/target/SqliteJdbcMailbox.java | 82 +++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/org/ibex/mail/target/SqliteJdbcMailbox.java diff --git a/src/org/ibex/mail/target/SqliteJdbcMailbox.java b/src/org/ibex/mail/target/SqliteJdbcMailbox.java new file mode 100644 index 0000000..587cf95 --- /dev/null +++ b/src/org/ibex/mail/target/SqliteJdbcMailbox.java @@ -0,0 +1,82 @@ +package org.ibex.mail.target; + +import org.ibex.mail.*; +import org.ibex.io.Fountain; +import org.ibex.io.Stream; +import java.sql.Timestamp; +import java.sql.*; +import java.net.*; +import java.io.*; +import java.util.*; + +public class SqliteJdbcMailbox extends Mailbox.Default { + + private Connection conn; + + 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(); + } + catch (SQLException e) { throw new RuntimeException(e); } + catch (ClassNotFoundException e) { throw new RuntimeException(e); } + } + + public void add(Message m) { + try { + PreparedStatement add = conn.prepareStatement("insert into 'mail' values (?,?,?,?,?,?,?)"); + add.setString(1, m.messageid+""); + add.setString(2, m.from+""); + add.setString(3, m.to+""); + add.setString(4, m.date+""); + add.setString(5, m.subject+""); + add.setString(6, streamToString(m.headers.getStream())); + add.setString(7, streamToString(m.getBody().getStream())); + add.executeUpdate(); + } catch (Exception e) { throw new RuntimeException(e); } + } + + public Mailbox.Iterator iterator() { return new SqliteJdbcIterator(); } + public void add(Message message, int flags) { add(message); } + public int uidNext() { throw new RuntimeException("not supported"); } + + private class SqliteJdbcIterator extends Mailbox.Default.Iterator { + private ResultSet rs; + private int count=1; + private Message m = null; + public SqliteJdbcIterator() { + try { + PreparedStatement query = conn.prepareStatement("select messageid_ from 'mail'"); + rs = query.executeQuery(); + rs.next(); + } catch (Exception e) { throw new RuntimeException(e); } + } + public Message cur() { + try { + if (m!=null) return m; + rs.next(); + PreparedStatement query = conn.prepareStatement("select headers_,body_ 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))); + 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 num() { return count; } // FIXME FIXME + public void delete() { throw new RuntimeException("not supported"); } + } + + public static String streamToString(Stream stream) throws Exception { + StringBuffer b = new StringBuffer(); + for(String s = stream.readln(); s!=null; s=stream.readln()) + b.append(s+"\n"); + return b.toString(); + } + +} \ No newline at end of file -- 1.7.10.4