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 Mailbox.Iterator iterator() { return new SqliteJdbcIterator(); }
28 public void insert(Message m, int flags) {
31 PreparedStatement add = conn.prepareStatement("insert into 'mail' values (?,?,?,?,?,?,?)");
32 add.setString(1, m.messageid+"");
33 add.setString(2, m.from+"");
34 add.setString(3, m.to+"");
35 add.setString(4, m.date+"");
36 add.setString(5, m.subject+"");
37 add.setString(6, streamToString(m.headers.getStream()));
38 add.setString(7, streamToString(m.getBody().getStream()));
40 } catch (Exception e) { throw new RuntimeException(e); }
42 public int uidNext() { throw new RuntimeException("not supported"); }
44 private class SqliteJdbcIterator extends Mailbox.Default.Iterator {
47 private Message m = null;
48 public SqliteJdbcIterator() {
50 PreparedStatement query = conn.prepareStatement("select messageid_ from 'mail'");
51 rs = query.executeQuery();
53 } catch (Exception e) { throw new RuntimeException(e); }
55 public Message cur() {
57 if (m!=null) return m;
59 PreparedStatement query = conn.prepareStatement("select headers_,body_ from 'mail' where messageid_=?");
60 query.setString(1, rs.getString(1));
61 ResultSet rs2 = query.executeQuery();
62 if (!rs.next()) return null;
63 m = Message.newMessage(new Fountain.StringFountain(rs.getString(1) + "\r\n\r\n" + rs.getString(2)));
65 } catch (Exception e) { throw new RuntimeException(e); }
67 public Headers head() { return cur().headers; }
68 public boolean next() { try { m = null; count++; return rs.next(); } catch (Exception e) { throw new RuntimeException(e); } }
69 public int uid() { throw new RuntimeException("not supported"); }
70 public int nntpNumber() { throw new RuntimeException("not supported"); }
71 public int imapNumber() { return count; }
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())