1 | #include "DataAccess.h"
|
---|
2 |
|
---|
3 | #include <iostream>
|
---|
4 | #include <sstream>
|
---|
5 |
|
---|
6 | using namespace std;
|
---|
7 |
|
---|
8 | DataAccess::DataAccess()
|
---|
9 | {
|
---|
10 | mysql_init(&mysql);
|
---|
11 | connection = mysql_real_connect(&mysql, "localhost", "pythonAdmin", "pyMaster09*", "pythondb", 0, 0, 0);
|
---|
12 |
|
---|
13 | if (connection == NULL) {
|
---|
14 | cout << mysql_error(&mysql) << endl;
|
---|
15 | }else
|
---|
16 | cout << "Connection successful" << endl;
|
---|
17 | }
|
---|
18 |
|
---|
19 | DataAccess::~DataAccess()
|
---|
20 | {
|
---|
21 | mysql_close(connection);
|
---|
22 | mysql_close(&mysql);
|
---|
23 | }
|
---|
24 |
|
---|
25 | int DataAccess::insertPlayer(string username, string password)
|
---|
26 | {
|
---|
27 | ostringstream oss;
|
---|
28 |
|
---|
29 | oss << "'" << username << "', '" << password << "'";
|
---|
30 |
|
---|
31 | return insert("users", "name, password", oss.str());
|
---|
32 | }
|
---|
33 |
|
---|
34 | Player *DataAccess::getPlayer(string username)
|
---|
35 | {
|
---|
36 | MYSQL_RES *result;
|
---|
37 | MYSQL_ROW row;
|
---|
38 | Player *p;
|
---|
39 | ostringstream oss;
|
---|
40 |
|
---|
41 | oss << "name='" << username << "'";
|
---|
42 |
|
---|
43 | result = select("users", oss.str().c_str());
|
---|
44 |
|
---|
45 | cout << "Got result" << endl;
|
---|
46 |
|
---|
47 | if (result == NULL) {
|
---|
48 | cout << "Error occured" << endl;
|
---|
49 | cout << mysql_error(connection) << endl;
|
---|
50 | return NULL;
|
---|
51 | }
|
---|
52 |
|
---|
53 | if ( ( row = mysql_fetch_row(result)) != NULL )
|
---|
54 | p = new Player(string(row[1]), string(row[2]));
|
---|
55 | else {
|
---|
56 | cout << "Returned no results for some reason" << endl;
|
---|
57 | p = NULL;
|
---|
58 | }
|
---|
59 |
|
---|
60 | mysql_free_result(result);
|
---|
61 |
|
---|
62 | return p;
|
---|
63 | }
|
---|
64 |
|
---|
65 | int DataAccess::printPlayers()
|
---|
66 | {
|
---|
67 | MYSQL_RES *result;
|
---|
68 | MYSQL_ROW row;
|
---|
69 | ostringstream oss;
|
---|
70 |
|
---|
71 | result = select("users", "");
|
---|
72 |
|
---|
73 | if (result == NULL) {
|
---|
74 | cout << mysql_error(connection) << endl;
|
---|
75 | return 1;
|
---|
76 | }
|
---|
77 |
|
---|
78 | while ( ( row = mysql_fetch_row(result)) != NULL ) {
|
---|
79 | cout << row[0] << ", " << row[1] << ", " << row[2] << endl;
|
---|
80 | }
|
---|
81 |
|
---|
82 | mysql_free_result(result);
|
---|
83 |
|
---|
84 | return 0;
|
---|
85 | }
|
---|
86 |
|
---|
87 | int DataAccess::insert(string table, string rows, string values)
|
---|
88 | {
|
---|
89 | int query_state;
|
---|
90 | ostringstream oss;
|
---|
91 |
|
---|
92 | oss << "INSERT into " << table << " (" << rows << ") VALUES (" << values << ")";
|
---|
93 | cout << "query: " << oss.str() << endl;
|
---|
94 |
|
---|
95 | query_state = mysql_query(connection, oss.str().c_str());
|
---|
96 |
|
---|
97 | if (query_state != 0) {
|
---|
98 | cout << mysql_error(connection) << endl;
|
---|
99 | return 1;
|
---|
100 | }
|
---|
101 |
|
---|
102 | return 0;
|
---|
103 | }
|
---|
104 |
|
---|
105 | MYSQL_RES *DataAccess::select(string table, string filter)
|
---|
106 | {
|
---|
107 | MYSQL_RES *result;
|
---|
108 | int query_state;
|
---|
109 | ostringstream oss;
|
---|
110 |
|
---|
111 | oss << "SELECT * FROM " << table;
|
---|
112 | if (!filter.empty())
|
---|
113 | oss << " WHERE " << filter;
|
---|
114 |
|
---|
115 | query_state = mysql_query(connection, oss.str().c_str());
|
---|
116 |
|
---|
117 | if (query_state != 0) {
|
---|
118 | cout << mysql_error(connection) << endl;
|
---|
119 | return NULL;
|
---|
120 | }
|
---|
121 |
|
---|
122 | return mysql_store_result(connection);
|
---|
123 | }
|
---|