2 // EvolutionSummaryTracker.cs
4 // Copyright (C) 2006 Novell, Inc.
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
28 using System
.Collections
;
31 using Mono
.Data
.SqliteClient
;
35 namespace Beagle
.Daemon
.EvolutionMailDriver
{
37 public class EvolutionSummaryTracker
: IDisposable
{
39 private SqliteConnection connection
;
40 private string tmp_filename
;
42 public EvolutionSummaryTracker (string directory
, string account_name
, string folder_name
)
44 string filename
= Path
.Combine (directory
, String
.Format ("SummaryTracker-{0}-{1}.db", account_name
, folder_name
.Replace ('/', '-')));
45 bool create_new_db
= ! File
.Exists (filename
);
47 tmp_filename
= filename
;
49 connection
= GetConnection (filename
);
52 } catch (ApplicationException
) {
54 connection
.Dispose ();
56 // Purge the old database and create a new one
57 File
.Delete (filename
);
58 connection
= GetConnection (filename
);
65 // Start a transaction for any updates
66 SqliteUtils
.DoNonQuery (connection
, "BEGIN");
69 private static SqliteConnection
GetConnection (string filename
)
71 SqliteConnection connection
= new SqliteConnection ();
72 connection
.ConnectionString
= String
.Format ("version={0},encoding=UTF-8,URI=file:{1}", ExternalStringsHack
.SqliteVersion
, filename
);
77 private void CreateDatabase ()
79 SqliteUtils
.DoNonQuery (connection
,
80 "CREATE TABLE mapping ( " +
81 " uid STRING UNIQUE, " +
82 " flags INTEGER NOT NULL, " +
83 " last_seen STRING NOT NULL " +
86 SqliteUtils
.DoNonQuery (connection
,
87 "CREATE INDEX mapping_uid on mapping (uid)");
90 public void Checkpoint ()
93 // Commit any outstanding changes and open a new transaction
94 SqliteUtils
.DoNonQuery (connection
, "COMMIT");
95 SqliteUtils
.DoNonQuery (connection
, "BEGIN");
102 // Commit any outstanding changes
103 SqliteUtils
.DoNonQuery (connection
, "COMMIT");
110 public void Dispose ()
113 GC
.SuppressFinalize (this);
116 public void Update (string uid
, uint flags
)
119 SqliteUtils
.DoNonQuery (connection
,
120 "INSERT OR REPLACE INTO mapping " +
121 " (uid, flags, last_seen) " +
122 " VALUES ('{0}', {1}, {2})",
123 uid
.Replace ("'", "''"),
125 StringFu
.DateTimeToString (DateTime
.UtcNow
));
129 public bool Get (string uid
, out uint flags
)
131 SqliteCommand command
;
132 SqliteDataReader reader
;
135 command
= new SqliteCommand ();
136 command
.Connection
= connection
;
137 command
.CommandText
= String
.Format (
138 "SELECT flags FROM mapping WHERE uid='{0}'",
139 uid
.Replace ("'", "''"));
141 reader
= SqliteUtils
.ExecuteReaderOrWait (command
);
144 if (SqliteUtils
.ReadOrWait (reader
)) {
145 flags
= (uint) reader
.GetInt32 (0);
158 public void Remove (string uid
)
161 SqliteUtils
.DoNonQuery (connection
,
162 "DELETE FROM mapping WHERE uid='{0}'",
163 uid
.Replace ("'", "''"));
167 // FIXME: I don't really like this, the collection could be huge
168 public ArrayList
GetOlderThan (DateTime dt
)
170 SqliteCommand command
;
171 SqliteDataReader reader
;
174 command
= new SqliteCommand ();
175 command
.Connection
= connection
;
176 command
.CommandText
=
177 "SELECT uid FROM mapping WHERE last_seen < " +
178 StringFu
.DateTimeToString (dt
);
180 reader
= SqliteUtils
.ExecuteReaderOrWait (command
);
182 ArrayList uids
= new ArrayList ();
184 while (SqliteUtils
.ReadOrWait (reader
))
185 uids
.Add (reader
.GetString (0));