source: network-game/server/DataAccess.cpp@ 1106210

Last change on this file since 1106210 was 41ad8ed, checked in by dportnoy <dmp1488@…>, 12 years ago

The server properly handles registration messages

  • Property mode set to 100644
File size: 2.5 KB
RevLine 
[36082e8]1#include "DataAccess.h"
2
3#include <iostream>
[59061f6]4#include <sstream>
[36082e8]5
6using namespace std;
7
8DataAccess::DataAccess()
9{
[59061f6]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;
[36082e8]17}
18
19DataAccess::~DataAccess()
20{
[59061f6]21 mysql_close(connection);
22 mysql_close(&mysql);
[36082e8]23}
24
[59061f6]25int 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
34Player *DataAccess::getPlayer(string username)
[36082e8]35{
36 MYSQL_RES *result;
37 MYSQL_ROW row;
[59061f6]38 Player *p;
39 ostringstream oss;
[36082e8]40
[59061f6]41 oss << "name='" << username << "'";
[36082e8]42
[59061f6]43 result = select("users", oss.str().c_str());
[36082e8]44
[41ad8ed]45 cout << "Got result" << endl;
46
[59061f6]47 if (result == NULL) {
[41ad8ed]48 cout << "Error occured" << endl;
[59061f6]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]));
[41ad8ed]55 else {
56 cout << "Returned no results for some reason" << endl;
[59061f6]57 p = NULL;
[41ad8ed]58 }
[36082e8]59
[59061f6]60 mysql_free_result(result);
[36082e8]61
[59061f6]62 return p;
63}
64
65int DataAccess::printPlayers()
66{
67 MYSQL_RES *result;
68 MYSQL_ROW row;
69 ostringstream oss;
70
71 result = select("users", "");
72
73 if (result == NULL) {
[36082e8]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
[59061f6]84 return 0;
85}
86
87int 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 }
[36082e8]101
102 return 0;
103}
[59061f6]104
105MYSQL_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}
Note: See TracBrowser for help on using the repository browser.