-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataStore.cs
207 lines (187 loc) · 6.42 KB
/
DataStore.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;
using System.Data;
using System.Windows.Forms;
namespace Hack_in_the_north_hand_mouse
{
class DataStore
{
private SQLiteConnection _sqLiteConnection;
public void DataStoreface(String databasePath = "face.sqlite")
{
_sqLiteConnection = new SQLiteConnection(String.Format("Data Source={0};Version=3;", databasePath));
}
public void SaveFace(string username, Byte[] faceBlob)
{
try
{
var exisitingUserId = GetUserId(username);
if (exisitingUserId == 0) exisitingUserId = GenerateUserId();
_sqLiteConnection.Open();
var insertQuery = "INSERT INTO faces(username, faceSample, userId) VALUES(@username,@faceSample,@userId)";
var cmd = new SQLiteCommand(insertQuery, _sqLiteConnection);
cmd.Parameters.AddWithValue("username", username);
cmd.Parameters.AddWithValue("userId", exisitingUserId);
cmd.Parameters.Add("faceSample", DbType.Binary, faceBlob.Length).Value = faceBlob;
var result = cmd.ExecuteNonQuery();
MessageBox.Show(String.Format("{0} face(s) saved successfully", result));
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString() + "So FUCK!");
}
finally
{
_sqLiteConnection.Close();
}
}
public List<face> CallFaces(string username)
{
var faces = new List<face>();
try
{
_sqLiteConnection.Open();
var query = username.ToLower().Equals("ALL_USERS".ToLower()) ? "SELECT * FROM faces" : "SELECT * FROM faces WHERE username=@username";
var cmd = new SQLiteCommand(query, _sqLiteConnection);
if (!username.ToLower().Equals("ALL_USERS".ToLower())) cmd.Parameters.AddWithValue("username", username);
var result = cmd.ExecuteReader();
if (!result.HasRows) return null;
while (result.Read())
{
var face = new face
{
Image = (byte[])result["faceSample"],
Id = Convert.ToInt32(result["id"]),
Label = (String)result["username"],
UserId = Convert.ToInt32(result["userId"])
};
faces.Add(face);
}
faces = faces.OrderBy(f => f.Id).ToList();
}
catch (Exception ex)
{
return null;
}
finally
{
_sqLiteConnection.Close();
}
return faces;
}
public int GetUserId(string username)
{
var userId = 0;
try
{
_sqLiteConnection.Open();
var selectQuery = "SELECT userId FROM faces WHERE username=@username LIMIT 1";
var cmd = new SQLiteCommand(selectQuery, _sqLiteConnection);
cmd.Parameters.AddWithValue("username", username);
var result = cmd.ExecuteReader();
if (!result.HasRows) return 0;
while (result.Read())
{
userId = Convert.ToInt32(result["userId"]);
}
}
catch
{
return userId;
}
finally
{
_sqLiteConnection.Close();
}
return userId; ;
}
public string GetUsername(int userId)
{
var username = "";
try
{
_sqLiteConnection.Open();
var selectQuery = "SELECT name FROM c_people_data WHERE userID=@userId LIMIT 1";
var cmd = new SQLiteCommand(selectQuery, _sqLiteConnection);
cmd.Parameters.AddWithValue("userId", userId);
var result = cmd.ExecuteReader();
if (!result.HasRows) return username;
result.Read();
username = (String)result["name"];
}
catch
{
return username;
}
finally
{
_sqLiteConnection.Close();
}
return username; ;
}
public List<string> GetAllUsernames()
{
var usernames = new List<string>();
try
{
_sqLiteConnection.Open();
var query = "SELECT DISTINCT username FROM faces";
var cmd = new SQLiteCommand(query, _sqLiteConnection);
var result = cmd.ExecuteReader();
while (result.Read())
{
usernames.Add((String)result["username"]);
}
usernames.Sort();
}
catch (Exception ex)
{
return null;
}
finally
{
_sqLiteConnection.Close();
}
return usernames;
}
public bool DeleteUser(string username)
{
var toReturn = false;
try
{
_sqLiteConnection.Open();
var query = "DELETE FROM faces WHERE username=@username";
var cmd = new SQLiteCommand(query, _sqLiteConnection);
cmd.Parameters.AddWithValue("username", username);
var result = cmd.ExecuteNonQuery();
if (result > 0) toReturn = true;
}
catch (Exception ex)
{
return toReturn;
}
finally
{
_sqLiteConnection.Close();
}
return toReturn;
}
public int GenerateUserId()
{
var date = DateTime.Now.ToString("MMddHHmmss");
return Convert.ToInt32(date);
}
public bool IsUsernameValid(string username)
{
throw new NotImplementedException();
}
public string SaveAdmin(string username, string password)
{
throw new NotImplementedException();
}
}
}