1 | #include "DataAccess.h"
|
---|
2 |
|
---|
3 | #include <iostream>
|
---|
4 | #include <sstream>
|
---|
5 | #include <cstdlib>
|
---|
6 |
|
---|
7 | using namespace std;
|
---|
8 |
|
---|
9 | DataAccess::DataAccess()
|
---|
10 | {
|
---|
11 | mysql_init(&mysql);
|
---|
12 | connection = mysql_real_connect(&mysql, "localhost", "pythonAdmin", "pyMaster09*", "pythondb", 0, 0, 0);
|
---|
13 |
|
---|
14 | if (connection == NULL) {
|
---|
15 | cout << mysql_error(&mysql) << endl;
|
---|
16 | }else
|
---|
17 | cout << "Connection successful" << endl;
|
---|
18 | }
|
---|
19 |
|
---|
20 | DataAccess::~DataAccess()
|
---|
21 | {
|
---|
22 | mysql_close(connection);
|
---|
23 | mysql_close(&mysql);
|
---|
24 | }
|
---|
25 |
|
---|
26 | int DataAccess::insertPlayer(string username, string password)
|
---|
27 | {
|
---|
28 | ostringstream oss;
|
---|
29 |
|
---|
30 | string salt = "$1$";
|
---|
31 | int random;
|
---|
32 | char chr;
|
---|
33 | for(int i=0; i<8; i++)
|
---|
34 | {
|
---|
35 | random = rand() % 62;
|
---|
36 | if (random < 26)
|
---|
37 | chr = (char)('a'+random);
|
---|
38 | else if (random < 52)
|
---|
39 | chr = (char)('A'+random-26);
|
---|
40 | else
|
---|
41 | chr = (char)('0'+random-52);
|
---|
42 | salt += chr;
|
---|
43 | }
|
---|
44 | salt += '$';
|
---|
45 |
|
---|
46 | string encrypted(crypt(password.c_str(), salt.c_str()));
|
---|
47 |
|
---|
48 | oss << "'" << username << "', '" << encrypted << "'";
|
---|
49 |
|
---|
50 | return insert("users", "name, password", oss.str());
|
---|
51 | }
|
---|
52 |
|
---|
53 | int DataAccess::updatePlayer(string username, string password)
|
---|
54 | {
|
---|
55 | ostringstream values, where;
|
---|
56 |
|
---|
57 | values << "password='" << password << "'";
|
---|
58 |
|
---|
59 | where << "name='" << username << "'";
|
---|
60 |
|
---|
61 | return update("users", values.str(), where.str());
|
---|
62 | }
|
---|
63 |
|
---|
64 | Player *DataAccess::getPlayer(string username)
|
---|
65 | {
|
---|
66 | MYSQL_RES *result;
|
---|
67 | MYSQL_ROW row;
|
---|
68 | Player *p;
|
---|
69 | ostringstream oss;
|
---|
70 |
|
---|
71 | oss << "name='" << username << "'";
|
---|
72 |
|
---|
73 | result = select("users", oss.str().c_str());
|
---|
74 |
|
---|
75 | cout << "Got result" << endl;
|
---|
76 |
|
---|
77 | if (result == NULL) {
|
---|
78 | cout << "Error occured" << endl;
|
---|
79 | cout << mysql_error(connection) << endl;
|
---|
80 | return NULL;
|
---|
81 | }
|
---|
82 |
|
---|
83 | if ( ( row = mysql_fetch_row(result)) != NULL )
|
---|
84 | p = new Player(string(row[1]), string(row[2]));
|
---|
85 | else {
|
---|
86 | cout << "Returned no results for some reason" << endl;
|
---|
87 | p = NULL;
|
---|
88 | }
|
---|
89 |
|
---|
90 | mysql_free_result(result);
|
---|
91 |
|
---|
92 | return p;
|
---|
93 | }
|
---|
94 |
|
---|
95 | // need to make sure this list is freed
|
---|
96 | // since we need to create a DataAccess class
|
---|
97 | // when calling these functions,
|
---|
98 | // we could free this list in the destructor
|
---|
99 | list<Player*>* DataAccess::getPlayers()
|
---|
100 | {
|
---|
101 | MYSQL_RES *result;
|
---|
102 | MYSQL_ROW row;
|
---|
103 | ostringstream oss;
|
---|
104 |
|
---|
105 | result = select("users", "");
|
---|
106 |
|
---|
107 | if (result == NULL) {
|
---|
108 | cout << mysql_error(connection) << endl;
|
---|
109 | return NULL;
|
---|
110 | }
|
---|
111 |
|
---|
112 | list<Player*>* lstPlayers = new list<Player*>();
|
---|
113 | while ( ( row = mysql_fetch_row(result)) != NULL ) {
|
---|
114 | cout << row[0] << ", " << row[1] << ", " << row[2] << endl;
|
---|
115 | lstPlayers->push_back(new Player(row[1], row[2]));
|
---|
116 | }
|
---|
117 |
|
---|
118 | mysql_free_result(result);
|
---|
119 |
|
---|
120 | return lstPlayers;
|
---|
121 | }
|
---|
122 |
|
---|
123 | bool DataAccess::verifyPassword(string password, string encrypted)
|
---|
124 | {
|
---|
125 | string test(crypt(password.c_str(), encrypted.c_str()));
|
---|
126 |
|
---|
127 | return encrypted.compare(test) == 0;
|
---|
128 | }
|
---|
129 |
|
---|
130 | int DataAccess::insert(string table, string columns, string values)
|
---|
131 | {
|
---|
132 | int query_state;
|
---|
133 | ostringstream oss;
|
---|
134 |
|
---|
135 | oss << "INSERT into " << table << " (" << columns << ") VALUES (" << values << ")";
|
---|
136 | cout << "query: " << oss.str() << endl;
|
---|
137 |
|
---|
138 | query_state = mysql_query(connection, oss.str().c_str());
|
---|
139 |
|
---|
140 | if (query_state != 0) {
|
---|
141 | cout << mysql_error(connection) << endl;
|
---|
142 | return 1;
|
---|
143 | }
|
---|
144 |
|
---|
145 | return 0;
|
---|
146 | }
|
---|
147 |
|
---|
148 | int DataAccess::update(string table, string values, string where)
|
---|
149 | {
|
---|
150 | int query_state;
|
---|
151 | ostringstream oss;
|
---|
152 |
|
---|
153 | oss << "UPDATE " << table << " SET " << values << " WHERE " << where;
|
---|
154 | cout << "query: " << oss.str() << endl;
|
---|
155 |
|
---|
156 | query_state = mysql_query(connection, oss.str().c_str());
|
---|
157 |
|
---|
158 | if (query_state != 0) {
|
---|
159 | cout << mysql_error(connection) << endl;
|
---|
160 | return 1;
|
---|
161 | }
|
---|
162 |
|
---|
163 | return 0;
|
---|
164 | }
|
---|
165 |
|
---|
166 | MYSQL_RES *DataAccess::select(string table, string filter)
|
---|
167 | {
|
---|
168 | MYSQL_RES *result;
|
---|
169 | int query_state;
|
---|
170 | ostringstream oss;
|
---|
171 |
|
---|
172 | oss << "SELECT * FROM " << table;
|
---|
173 | if (!filter.empty())
|
---|
174 | oss << " WHERE " << filter;
|
---|
175 |
|
---|
176 | query_state = mysql_query(connection, oss.str().c_str());
|
---|
177 |
|
---|
178 | if (query_state != 0) {
|
---|
179 | cout << mysql_error(connection) << endl;
|
---|
180 | return NULL;
|
---|
181 | }
|
---|
182 |
|
---|
183 | return mysql_store_result(connection);
|
---|
184 | }
|
---|