forked from GarethWright/WinApp.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageStore.cs
More file actions
138 lines (132 loc) · 4.57 KB
/
Copy pathMessageStore.cs
File metadata and controls
138 lines (132 loc) · 4.57 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data.SQLite;
namespace WinAppNET.AppCode
{
class MessageStore
{
public const string ConnectionString = "Data Source=data/sqlite/messages.db3";
public static void CheckTable()
{
DbProviderFactory fact = DbProviderFactories.GetFactory("System.Data.SQLite");
using (DbConnection cnn = fact.CreateConnection())
{
cnn.ConnectionString = MessageStore.ConnectionString;
cnn.Open();
DbCommand cmd = cnn.CreateCommand();
cmd.CommandText = @"CREATE TABLE IF NOT EXISTS
'Messages' (
'id' INTEGER PRIMARY KEY,
'jid' VARCHAR(64),
'author' VARCHAR(64),
'from_me' INTEGER,
'read' INTEGER,
'data' TEXT,
'type' VARCHAR(64),
'preview' TEXT,
'timestamp' VARCHAR(64)
)";
cmd.ExecuteNonQuery();
}
}
public static WappMessage[] GetAllMessagesForContact(string jid)
{
List<WappMessage> messages = new List<WappMessage>();
DbProviderFactory fact = DbProviderFactories.GetFactory("System.Data.SQLite");
using (DbConnection cnn = fact.CreateConnection())
{
cnn.ConnectionString = MessageStore.ConnectionString;
cnn.Open();
DbCommand cmd = cnn.CreateCommand();
cmd = cnn.CreateCommand();
cmd.CommandText = "SELECT * FROM Messages where jid = @jid";
cmd.Parameters.Add(new SQLiteParameter("@jid", jid));
DbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
int id = Int32.Parse(reader["id"].ToString());
string author = string.Empty;
try
{
if (reader["author"] != null)
{
author = reader["author"].ToString();
}
}
catch (Exception e)
{
throw e;
}
bool from_me = (Int32.Parse(reader["from_me"].ToString()) == 1 ? true : false);
string data = (string)reader["data"];
string type;
try
{
type = (string)reader["type"];
}catch(Exception)
{
type = string.Empty;
}
string preview;
try
{
preview = (string)reader["preview"];
}
catch (Exception)
{
preview = string.Empty;
}
DateTime timestamp = DateTime.Parse(reader["timestamp"].ToString());
WappMessage message = new WappMessage(id, data, from_me, jid, timestamp, type, preview);
if (!String.IsNullOrEmpty(author))
{
message.author = author;
}
messages.Add(message);
}
}
return messages.ToArray();
}
public static void AddMessage(WappMessage message)
{
DbProviderFactory fact = DbProviderFactories.GetFactory("System.Data.SQLite");
using (DbConnection cnn = fact.CreateConnection())
{
cnn.ConnectionString = MessageStore.ConnectionString;
cnn.Open();
DbCommand cmd = cnn.CreateCommand();
cmd.CommandText = @"INSERT INTO
'Messages'
(
'jid',
'author',
'from_me',
'data',
'timestamp',
'type',
'preview'
)
VALUES (
@jid,
@author,
@from_me,
@data,
@timestamp,
@type,
@preview
)";
cmd.Parameters.Add(new SQLiteParameter("@jid", message.jid));
cmd.Parameters.Add(new SQLiteParameter("@author", message.author));
cmd.Parameters.Add(new SQLiteParameter("@from_me", (message.from_me ? "1" : "0")));
cmd.Parameters.Add(new SQLiteParameter("@data", message.data));
cmd.Parameters.Add(new SQLiteParameter("@timestamp", message.timestamp.ToString()));
cmd.Parameters.Add(new SQLiteParameter("@type", message.type));
cmd.Parameters.Add(new SQLiteParameter("@preview", message.preview));
cmd.ExecuteNonQuery();
}
}
}
}