-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContactController.cpp
255 lines (246 loc) · 7.71 KB
/
ContactController.cpp
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
249
250
251
252
253
254
255
/* Copyright Navid Mafi Ranji
This source code is provided only for reference purposes. This is not a public domain source code.
All rights reserved.
*/
#include <iostream>
#include "ContactController.h"
using std::cout;
using std::endl;
using std::stoi;
using std::string;
using std::to_string;
static int contactListCB(void *NotUsed, int argc, char **argv, char **azColName)
{
cout << argv[0] << " - " << argv[1] << " " << argv[2] << endl;
return 0;
}
static int singleContactCB(void *NotUsed, int argc, char **argv, char **azColName)
{
for (int i = 0; i < argc; i++)
{
cout << azColName[i] << " : " << argv[i] << endl;
}
printf("\n");
return 0;
}
static int contactCountCB(void *NotUsed, int argc, char **argv, char **azColName)
{
logger("info", "Contact count: " + std::to_string(stoi(argv[0])));
ContactController::dbSize = stoi(argv[0]);
return 0;
}
void ContactController::openDatabase()
{
int rc = sqlite3_open("contacts.db", &db);
if (rc)
{
logger("error", "Error occured while opening DB :: " + string(sqlite3_errmsg(db)));
}
else
{
logger("info", "Opened DB successfully");
}
}
void ContactController::initDatabase()
{
string sqlQuery = "CREATE TABLE IF NOT EXISTS CONTACTS("
"ID INTEGER PRIMARY KEY,"
"FIRSTNAME TEXT NOT NULL,"
"LASTNAME TEXT NOT NULL,"
"AGE INTEGER NOT NULL,"
"ADDRESS CHAR(50),"
"EMAIL CHAR(50),"
"PHONENUMBER TEXT NOT NULL);";
char *cErrMsg = 0;
int rc = sqlite3_exec(db, sqlQuery.c_str(), contactListCB, 0, &cErrMsg);
logger("debug", "Ran sql init query");
if (rc != SQLITE_OK)
{
string errMsg = cErrMsg;
logger("error", "Error while init db :: " + errMsg);
sqlite3_free(cErrMsg);
}
else
{
logger("info", "DB Init succesfully");
}
// set dbSize
}
void ContactController::readDBSize()
{
char *cErrMsg = 0;
string sqlQuery2 = "SELECT COUNT(*) FROM CONTACTS;";
int rc = sqlite3_exec(db, sqlQuery2.c_str(), contactCountCB, 0, &cErrMsg);
logger("debug", "Ran sql count query");
if (rc != SQLITE_OK)
{
string errMsg = cErrMsg;
logger("error", "Error while counting db :: " + errMsg);
sqlite3_free(cErrMsg);
}
else
{
logger("info", "DB Count succesfully");
}
}
void ContactController::addContact(ContactType contact)
{
string sqlQuery = "INSERT INTO CONTACTS (FIRSTNAME,LASTNAME,AGE,ADDRESS,EMAIL,PHONENUMBER) "
"VALUES (\"" +
contact.firstName +
"\",\"" + contact.lastName +
"\",\"" + to_string(contact.age) +
"\",\"" + contact.address +
"\",\"" + contact.email +
"\",\"" + contact.phoneNumber + "\");";
char *cErrMsg = 0;
int rc = sqlite3_exec(db, sqlQuery.c_str(), contactListCB, 0, &cErrMsg);
if (rc != SQLITE_OK)
{
string errMsg = cErrMsg;
logger("error", "Error while inserting new contact :: " + errMsg);
sqlite3_free(cErrMsg);
}
else
{
logger("info", "New Contact inserted succesfully");
readDBSize();
}
}
void ContactController::editContactField(int id, string field, string newValue)
{
string sqlQuery = "UPDATE CONTACTS SET " + field + " = \"" + newValue + "\" WHERE ID = " + to_string(id) + ";";
char *cErrMsg = 0;
int rc = sqlite3_exec(db, sqlQuery.c_str(), 0, 0, &cErrMsg);
if (rc != SQLITE_OK)
{
string errMsg = cErrMsg;
logger("error", "Error while editing contact :: " + errMsg);
sqlite3_free(cErrMsg);
}
else
{
logger("info", "Field edited succesfully");
}
}
void ContactController::deleteContact(int id)
{
string sqlQuery = "DELETE FROM CONTACTS WHERE ID = " + to_string(id) + ";";
char *cErrMsg = 0;
int rc = sqlite3_exec(db, sqlQuery.c_str(), 0, 0, &cErrMsg);
if (rc != SQLITE_OK)
{
string errMsg = cErrMsg;
logger("error", "Error while deleting contact :: " + errMsg);
sqlite3_free(cErrMsg);
}
else
{
logger("info", "Contact deleted succesfully");
readDBSize();
}
}
void ContactController::updateContact(int id, ContactType newContent)
{
// update contact with id = id to newContent
string sqlQuery = "UPDATE CONTACTS SET FIRSTNAME = \"" + newContent.firstName +
"\", LASTNAME = \"" + newContent.lastName +
"\", AGE = \"" + to_string(newContent.age) +
"\", ADDRESS = \"" + to_string(newContent.age) +
"\", EMAIL = \"" + to_string(newContent.age) +
"\", PHONENUMBER = \"" + to_string(newContent.age) + "\" WHERE ID = " + to_string(id) + ";";
char *cErrMsg = 0;
int rc = sqlite3_exec(db, sqlQuery.c_str(), contactListCB, 0, &cErrMsg);
if (rc != SQLITE_OK)
{
string errMsg = cErrMsg;
logger("error", "Error while updating contact :: " + errMsg);
sqlite3_free(cErrMsg);
}
else
{
logger("info", "Contact updated succesfully");
}
}
void ContactController::listContacts()
{
char *cErrMsg = 0;
// string sqlQuery = "SELECT * from CONTACTS";
// read id and firstname and lastname from db
string sqlQuery = "SELECT ID,FIRSTNAME,LASTNAME from CONTACTS";
int rc = sqlite3_exec(db, sqlQuery.c_str(), contactListCB, 0, &cErrMsg);
if (rc != SQLITE_OK)
{
string errMsg = cErrMsg;
logger("error", "Error while reading all contacts :: " + errMsg);
sqlite3_free(cErrMsg);
}
else
{
logger("info", "Read Contacts succesfully");
}
}
void ContactController::showSingleContact(int contactID)
{
char *cErrMsg = 0;
string sqlQuery = "SELECT * from CONTACTS WHERE ID = " + to_string(contactID) + ";";
int rc = sqlite3_exec(db, sqlQuery.c_str(), singleContactCB, 0, &cErrMsg);
if (rc != SQLITE_OK)
{
string errMsg = cErrMsg;
logger("error", "Error while reading single contact :: " + errMsg);
sqlite3_free(cErrMsg);
}
else
{
logger("info", "Read single contact succesfully");
}
}
void ContactController::findContact(string searchString)
{
// search for contact with firstname or lastname containing searchString
// return array of contacts
// if no contact found return empty array
// if more than one contact found return array of contacts
char *cErrMsg = 0;
string sqlQuery = "SELECT * from CONTACTS WHERE FIRSTNAME LIKE \"%" + searchString + "%\" OR LASTNAME LIKE \"%" + searchString + "%\"";
int rc = sqlite3_exec(db, sqlQuery.c_str(), contactListCB, 0, &cErrMsg);
if (rc != SQLITE_OK)
{
string errMsg = cErrMsg;
logger("error", "Error while searching contacts :: " + errMsg);
sqlite3_free(cErrMsg);
}
else
{
logger("info", "Search Contacts succesfully");
}
}
void ContactController::clearDatabase()
{
string sqlQuery = "DELETE from CONTACTS";
char *cErrMsg = 0;
int rc = sqlite3_exec(db, sqlQuery.c_str(), contactListCB, 0, &cErrMsg);
if (rc != SQLITE_OK)
{
string errMsg = cErrMsg;
logger("error", "Error while clearing database :: " + errMsg);
sqlite3_free(cErrMsg);
}
else
{
logger("info", "Database cleared succesfully");
}
}
void ContactController::closeDatabase()
{
int rc = sqlite3_close(db);
if (rc)
{
logger("error", "Error while closing DB :: " + string(sqlite3_errmsg(db)));
}
else
{
logger("info", "Closed DB. Bye.");
}
}