add SqliteJdbcMailbox
[org.ibex.mail.git] / src / org / ibex / mail / target / SqliteJdbcMailbox.java
1 package org.ibex.mail.target;
2
3 import org.ibex.mail.*;
4 import org.ibex.io.Fountain;
5 import org.ibex.io.Stream;
6 import java.sql.Timestamp;
7 import java.sql.*;
8 import java.net.*;
9 import java.io.*;
10 import java.util.*;
11
12 public class SqliteJdbcMailbox extends Mailbox.Default {
13
14     private Connection conn;
15
16     public SqliteJdbcMailbox(String filename) {
17         try {
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();
22         }
23         catch (SQLException e) { throw new RuntimeException(e); }
24         catch (ClassNotFoundException e) { throw new RuntimeException(e); }
25     }
26
27     public void add(Message m) {
28         try {
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()));
37             add.executeUpdate();
38         } catch (Exception e) { throw new RuntimeException(e); }
39     }
40
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"); }
44
45     private class SqliteJdbcIterator extends Mailbox.Default.Iterator {
46         private ResultSet rs;
47         private int count=1;
48         private Message m = null;
49         public SqliteJdbcIterator() {
50             try {
51                 PreparedStatement query = conn.prepareStatement("select messageid_ from 'mail'");
52                 rs = query.executeQuery();
53                 rs.next();
54             } catch (Exception e) { throw new RuntimeException(e); }
55         }
56         public Message cur()    {
57             try {
58                 if (m!=null) return m;
59                 rs.next();
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)));
65                 return m;
66             } catch (Exception e) { throw new RuntimeException(e); }
67         }
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"); }
73     }
74
75     public static String streamToString(Stream stream) throws Exception {
76         StringBuffer b = new StringBuffer();
77         for(String s = stream.readln(); s!=null; s=stream.readln())
78             b.append(s+"\n");
79         return b.toString();
80     }
81
82 }