1 package org.ibex.mail.target;
3 import org.ibex.mail.*;
4 import org.ibex.io.Fountain;
5 import org.ibex.io.Stream;
6 import java.sql.Timestamp;
12 public class SqliteJdbcMailbox extends Mailbox.Default {
14 private Connection conn;
16 public SqliteJdbcMailbox(String filename) {
18 Class.forName("org.sqlite.JDBC");
19 conn = DriverManager.getConnection("jdbc:sqlite:"+filename);
20 conn.prepareStatement("create table if not exists "+
21 "'mail' (messageid_,from_,to_,date_,subject_,headers_,body_,PRIMARY KEY(messageid_))").executeUpdate();
23 catch (SQLException e) { throw new RuntimeException(e); }
24 catch (ClassNotFoundException e) { throw new RuntimeException(e); }
27 public void add(Message m) {
29 PreparedStatement add = conn.prepareStatement("insert into 'mail' values (?,?,?,?,?,?,?)");
30 add.setString(1, m.messageid+"");
31 add.setString(2, m.from+"");
32 add.setString(3, m.to+"");
33 add.setString(4, m.date+"");
34 add.setString(5, m.subject+"");
35 add.setString(6, streamToString(m.headers.getStream()));
36 add.setString(7, streamToString(m.getBody().getStream()));
38 } catch (Exception e) { throw new RuntimeException(e); }
41 public Mailbox.Iterator iterator() { return new SqliteJdbcIterator(); }
42 public void add(Message message, int flags) { add(message); }
43 public int uidNext() { throw new RuntimeException("not supported"); }
45 private class SqliteJdbcIterator extends Mailbox.Default.Iterator {
48 private Message m = null;
49 public SqliteJdbcIterator() {
51 PreparedStatement query = conn.prepareStatement("select messageid_ from 'mail'");
52 rs = query.executeQuery();
54 } catch (Exception e) { throw new RuntimeException(e); }
56 public Message cur() {
58 if (m!=null) return m;
60 PreparedStatement query = conn.prepareStatement("select headers_,body_ from 'mail' where messageid_=?");
61 query.setString(1, rs.getString(1));
62 ResultSet rs2 = query.executeQuery();
63 if (!rs.next()) return null;
64 m = Message.newMessage(new Fountain.StringFountain(rs.getString(1) + "\r\n\r\n" + rs.getString(2)));
66 } catch (Exception e) { throw new RuntimeException(e); }
68 public Headers head() { return cur().headers; }
69 public boolean next() { try { m = null; count++; return rs.next(); } catch (Exception e) { throw new RuntimeException(e); } }
70 public int uid() { throw new RuntimeException("not supported"); }
71 public int num() { return count; } // FIXME FIXME
72 public void delete() { throw new RuntimeException("not supported"); }
75 private static String streamToString(Stream stream) throws Exception {
76 StringBuffer b = new StringBuffer();
77 for(String s = stream.readln(); s!=null; s=stream.readln())