a0afc8f74972262a62cef0f7821326e3c60f7eab
[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 Mailbox.Iterator iterator()                      { return new SqliteJdbcIterator(); }
28     public void             insert(Message m, int flags) {
29         // FIXME: flags
30         try {
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()));
39             add.executeUpdate();
40         } catch (Exception e) { throw new RuntimeException(e); }
41     }
42     public int              uidNext()                       { throw new RuntimeException("not supported"); }
43
44     private class SqliteJdbcIterator extends Mailbox.Default.Iterator {
45         private ResultSet rs;
46         private int count=1;
47         private Message m = null;
48         public SqliteJdbcIterator() {
49             try {
50                 PreparedStatement query = conn.prepareStatement("select messageid_ from 'mail'");
51                 rs = query.executeQuery();
52                 rs.next();
53             } catch (Exception e) { throw new RuntimeException(e); }
54         }
55         public Message cur()    {
56             try {
57                 if (m!=null) return m;
58                 rs.next();
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)));
64                 return m;
65             } catch (Exception e) { throw new RuntimeException(e); }
66         }
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"); }
73     }
74
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())
78             b.append(s+"\n");
79         return b.toString();
80     }
81
82 }