-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.cpp
242 lines (193 loc) · 5.44 KB
/
mysql.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
#include "header.h"
string str2hex(const string& input){
static const char* const lut = "0123456789ABCDEF";
size_t len = input.length();
string output;
output.reserve(2 * len);
for (size_t i = 0; i < len; ++i)
{
const char c = input[i];
output.push_back(lut[c >> 4]);
output.push_back(lut[c & 15]);
}
return output;
}
string hex2str(const string& input){
static const char* const lut = "0123456789ABCDEF";
size_t len = input.length();
if (len & 1) throw invalid_argument("odd length");
string output;
output.reserve(len / 2);
for (size_t i = 0; i < len; i += 2)
{
char a = input[i];
const char* p = lower_bound(lut, lut + 16, a);
if (*p != a) throw invalid_argument("not a hex digit");
char b = input[i + 1];
const char* q = lower_bound(lut, lut + 16, b);
if (*q != b) throw invalid_argument("not a hex digit");
output.push_back(((p - lut) << 4) | (q - lut));
}
return output;
}
string encrypt(string input){
string encryptThis = input;
/*string encryptThis;
char pad;
srand ((unsigned) time(NULL));
pad = rand()%26 + 'a';
encryptThis = pad;
encryptThis.append(input);
pad = rand()%26 + 'a';
encryptThis += pad;
pad = rand()%26 + 'a';
encryptThis += pad;
for( int i=0; encryptThis[i] != '\0'; ++i ){
++encryptThis[i];
encryptThis[i] = encryptThis[i]^(int(key)+i)%255;
}*/
return str2hex(encryptThis);
}
string decrypt(string input){
string tmp;
tmp = hex2str(input);
input = tmp;
string decryptThis = input;
/*
string decryptThis = input.substr(0,input.size()-2);
for( int i=0; decryptThis[i] != '\0'; ++i ){
decryptThis[i] = decryptThis[i]^(int(key)+i)%255;
--decryptThis[i];
}
decryptThis.erase(0,1);
*/
return decryptThis;
}
void getDatabaseDetails(string *DBHOST, string *USER, string *PASSWORD, string *DATABASE){
FILE *dbFile;
char buf[128];
string crypt;
dbFile = fopen("database.conf","r");
if (dbFile == NULL){
perror ("Couldn't open database.conf");
return;
}
fgets(buf, 128, dbFile);
crypt = buf;
crypt.pop_back();
fgets(buf, 128, dbFile);
*DBHOST = buf;
DBHOST->pop_back();
fgets(buf, 128, dbFile);
*USER = buf;
USER->pop_back();
fgets(buf, 128, dbFile);
*PASSWORD = buf;
PASSWORD->pop_back();
fgets(buf, 128, dbFile);
*DATABASE = buf;
DATABASE->pop_back();
fclose(dbFile);
if (crypt == "decrypted"){
dbFile = fopen("database.conf","w");
strncpy(buf, "encrypted\n", 128);
fputs(buf, dbFile);
strncpy(buf, encrypt(*DBHOST).c_str(), 128);
fputs(buf, dbFile); fputc('\n', dbFile);
strncpy(buf, encrypt(*USER).c_str(), 128);
fputs(buf, dbFile); fputc('\n', dbFile);
strncpy(buf, encrypt(*PASSWORD).c_str(), 128);
fputs(buf, dbFile); fputc('\n', dbFile);
strncpy(buf, encrypt(*DATABASE).c_str(), 128);
fputs(buf, dbFile);
fclose(dbFile);
}else if (crypt == "encrypted"){
*DBHOST = decrypt(*DBHOST);
*USER = decrypt(*USER);
*PASSWORD = decrypt(*PASSWORD);
*DATABASE = decrypt(*DATABASE);
}else{
printf("ERROR: This is not the database.conf you are looking for!");
}
}
int retrieveSQL(map<const int,Entry> &entries, hd44780 &lcd){
Entry person;
try {
Driver *driver;
Connection *con;
PreparedStatement *pstmt;
ResultSet *res;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect(DBHOST, USER, PASSWORD);
con->setSchema(DATABASE);
pstmt = con->prepareStatement("SELECT * FROM dreg_persons");
res = pstmt->executeQuery();
string test;
while (res->next()){
Entry person( res->getInt("id"),
res->getString("last_name").c_str(),
res->getString("first_name").c_str(),
res->getInt("tab"),
res->getInt("cash"),
res->getInt("spent"),
res->getString("comment").c_str());
entries[res->getInt("id")] = person;
}
delete res;
delete pstmt;
delete con;
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line "
<< __LINE__ << endl;
cout << "# ERR: " << e.what() << endl;
lcd.clear();
lcd.write("Error retrieving SQL\ndatabase!\nAre you connected to\nthe internet?",50);
}
return EXIT_SUCCESS;
}
int updateSQL(int id, string field, int int_value, hd44780 &lcd){
int n = -1;
enum FIELDS{
cash,
spent};
try {
Driver *driver;
Connection *con;
PreparedStatement *pstmt;
map<const int,Entry>::iterator it;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect(DBHOST, USER, PASSWORD);
con->setSchema(DATABASE);
if (field == "cash") {n = cash;}
else if (field == "spent") {n = spent;}
switch (FIELDS(n)){
case cash:
pstmt = con->prepareStatement("UPDATE dreg_persons SET cash = ? WHERE id = ? LIMIT 1");
pstmt->setInt(1,int_value);
break;
case spent:
pstmt = con->prepareStatement("UPDATE dreg_persons SET spent = ? WHERE id = ? LIMIT 1");
pstmt->setInt(1,int_value);
break;
default:
pstmt = con->prepareStatement("");
printf("SQL identifier not recognized!\n");
break;
}
pstmt->setInt(2,id);
pstmt->executeUpdate();
delete pstmt;
delete con;
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line "
<< __LINE__ << endl;
cout << "# ERR: " << e.what() << endl;
lcd.clear();
lcd.write("Error updating SQL\ndatabase!\nAre you connected to\nthe internet?",50);
}
return EXIT_SUCCESS;
}