-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailDatabase.cs
More file actions
248 lines (213 loc) · 9.43 KB
/
EmailDatabase.cs
File metadata and controls
248 lines (213 loc) · 9.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
using SQLitePCL;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Windows.Forms;
namespace EmailClient
{
public sealed class EmailDatabase : IDisposable
{
private readonly sqlite3 db;
public bool IsValidDatabase { get; private set; } = false;
public EmailDatabase(string dbName, string password = "", bool hideErrors = false)
{
if (string.IsNullOrEmpty(dbName)) return;
bool useEncryption = !string.IsNullOrEmpty(password);
string path = Form1.GetFilePath("db");
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
path = Form1.GetFilePath("db\\" + dbName + ".db");
Batteries_V2.Init();
if (!File.Exists(path))
{ // Check if the database file exists, create it if not
File.Create(path).Dispose();
Console.WriteLine("Created New Db");
}
Console.WriteLine("Path:" + path + " UseEncryption:" + useEncryption);
// Open the database connection
int rc = raw.sqlite3_open(path, out db);
if (rc != raw.SQLITE_OK)
{
if (!hideErrors) MessageBox.Show("Failed to open database. " + rc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (useEncryption && !string.IsNullOrEmpty(password))
{
rc = raw.sqlite3_exec(db, $"PRAGMA key = '{password}';", null, null, out _);
if (rc != raw.SQLITE_OK)
{
if(!hideErrors) MessageBox.Show("Incorrect or missing password for encrypted database.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
}
// Try to read from the database as a test to validate password correctness
rc = raw.sqlite3_exec(db, "SELECT count(*) FROM sqlite_master;", null, null, out _);
if (rc != raw.SQLITE_OK)
{
if (!hideErrors) MessageBox.Show("Incorrect password or corrupted database.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
const string sql = @"
CREATE TABLE IF NOT EXISTS Emails (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Key TEXT UNIQUE,
Sender TEXT,
Receiver TEXT,
Subject TEXT,
Body TEXT,
Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- Create an index on the 'Key' column to improve lookup performance
CREATE INDEX IF NOT EXISTS idx_Emails_Key ON Emails (Key);
";
rc = raw.sqlite3_exec(db, sql, null, null, out _);
if (rc != raw.SQLITE_OK)
{
if (!hideErrors) MessageBox.Show("Failed to create table or index in the database.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
IsValidDatabase = true;
}
public bool EmailExists(string key)
{
try
{
string sql = "SELECT 1 FROM Emails WHERE Key = ?;";
sqlite3_stmt stmt;
raw.sqlite3_prepare_v2(db, sql, out stmt);
raw.sqlite3_bind_text(stmt, 1, key); // Bind the key to the query
bool exists = raw.sqlite3_step(stmt) == raw.SQLITE_ROW;
raw.sqlite3_finalize(stmt);
return exists;
}
catch (Exception ex)
{
Console.WriteLine($"Error adding emails: {ex.Message}");
return true; // Returning true since true means it wont be added
}
}
/// <summary>
/// This db will preserve its entry order meaning the newest emails will be stored at the end. Becuase of this we will need to retreive
/// emails from the end which is why we use `ORDER BY Id DESC` to reverse the order such that we return emails from newest to oldest.
/// </summary>
/// <param name="limit"> The amount of emails we should return </param>
/// <param name="offset"> How many emails we should skip </param>
/// <returns> The list of emails queried from newest to oldest. </returns>
public List<Email> GetEmails(int limit, int offset)
{
try
{
List<Email> emails = new List<Email>();
// Modified SQL to order by Id in descending order
string sql = $"SELECT Id, Key, Sender, Receiver, Subject, Body, Timestamp FROM Emails ORDER BY Id DESC LIMIT {limit} OFFSET {offset};";
sqlite3_stmt stmt;
raw.sqlite3_prepare_v2(db, sql, out stmt);
while (raw.sqlite3_step(stmt) == raw.SQLITE_ROW)
{
emails.Add(new Email
{
Id = raw.sqlite3_column_int(stmt, 0),
Key = raw.sqlite3_column_text(stmt, 1).utf8_to_string(),
Sender = raw.sqlite3_column_text(stmt, 2).utf8_to_string(),
Receiver = raw.sqlite3_column_text(stmt, 3).utf8_to_string(),
Subject = raw.sqlite3_column_text(stmt, 4).utf8_to_string(),
Body = raw.sqlite3_column_text(stmt, 5).utf8_to_string(),
Date = DateTime.ParseExact(raw.sqlite3_column_text(stmt, 6).utf8_to_string(), "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)
});
}
raw.sqlite3_finalize(stmt);
return emails;
}
catch (Exception ex)
{
Console.WriteLine($" GetEmails Error: {ex.Message}");
return new List<Email>();
}
}
public int GetEmailCount()
{
string sql = "SELECT COUNT(*) FROM Emails;";
sqlite3_stmt stmt;
int rowCount = 0;
// Prepare the statement
int rc = raw.sqlite3_prepare_v2(db, sql, out stmt);
if (rc == raw.SQLITE_OK)
{
// Execute the query
if (raw.sqlite3_step(stmt) == raw.SQLITE_ROW)
{
rowCount = raw.sqlite3_column_int(stmt, 0);
}
// Finalize to release the statement
raw.sqlite3_finalize(stmt);
}
return rowCount;
}
public bool AddEmail(Email email)
{
string sql = @"
INSERT OR IGNORE INTO Emails (Key, Sender, Receiver, Subject, Body, Timestamp)
VALUES (?, ?, ?, ?, ?, ?);";
sqlite3_stmt stmt;
raw.sqlite3_prepare_v2(db, sql, out stmt);
raw.sqlite3_bind_text(stmt, 1, email.Key);
raw.sqlite3_bind_text(stmt, 2, email.Sender);
raw.sqlite3_bind_text(stmt, 3, email.Receiver);
raw.sqlite3_bind_text(stmt, 4, email.Subject);
raw.sqlite3_bind_text(stmt, 5, email.Body);
raw.sqlite3_bind_text(stmt, 6, email.Date.ToString("dd-MM-yyyy HH:mm:ss"));
bool completed = raw.sqlite3_step(stmt) == raw.SQLITE_DONE;
raw.sqlite3_finalize(stmt);
return completed;
}
public void AddEmails(List<Email> emails)
{
try
{
string sql = @"
INSERT OR IGNORE INTO Emails (Key, Sender, Receiver, Subject, Body, Timestamp)
VALUES (?, ?, ?, ?, ?, ?);";
// Begin transaction
raw.sqlite3_exec(db, "BEGIN TRANSACTION;", null, null, out _);
foreach (Email email in emails)
{
sqlite3_stmt stmt;
raw.sqlite3_prepare_v2(db, sql, out stmt);
raw.sqlite3_bind_text(stmt, 1, email.Key);
raw.sqlite3_bind_text(stmt, 2, email.Sender);
raw.sqlite3_bind_text(stmt, 3, email.Receiver);
raw.sqlite3_bind_text(stmt, 4, email.Subject);
raw.sqlite3_bind_text(stmt, 5, email.Body);
raw.sqlite3_bind_text(stmt, 6, email.Date.ToString("yyyy-MM-dd HH:mm:ss"));
raw.sqlite3_step(stmt);
raw.sqlite3_finalize(stmt);
}
// Commit transaction
raw.sqlite3_exec(db, "COMMIT;", null, null, out _);
}
catch (Exception) { }
}
public bool DeleteEmailFromDatabase(string emailKey)
{
try
{
string sql = "DELETE FROM Emails WHERE Key = ?;";
sqlite3_stmt stmt;
raw.sqlite3_prepare_v2(db, sql, out stmt);
raw.sqlite3_bind_text(stmt, 1, emailKey); // Bind the key to the SQL command
bool completed = raw.sqlite3_step(stmt) == raw.SQLITE_DONE;
raw.sqlite3_finalize(stmt);
return completed;
} catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
}
public void Dispose()
{
if(db != null) raw.sqlite3_close(db);
IsValidDatabase = false;
}
}
}