summaryrefslogtreecommitdiffstats
path: root/src/server/database.hpp
blob: b4b731d911c097e5f7b6fca3fa448a611bea31b3 (plain)
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
#ifndef WC_DATABASE_H
#define WC_DATABASE_H

#include <mysql.h>

class DatabaseManager;

class DB_ROW {
	 MYSQL_ROW row;
	 const unsigned int fields;
public:
	 DB_ROW(MYSQL_ROW *r, const unsigned int flds) :
	 	row(*r), fields(flds) {}
	 ~DB_ROW();
	 char *operator[](const int index);

	 const unsigned int GetFieldCount() const { return fields; }
};

class AnswerDB {
	friend class DatabaseManager; // ?

	MYSQL_RES *result;
	DB_ROW *current_row;
	const unsigned int fields;
	unsigned long rows;

	AnswerDB(MYSQL_RES *res, unsigned int flds, unsigned long rws) :
		result(res), current_row(0), fields(flds), rows(rws) {}
public:
	~AnswerDB();

	DB_ROW *GetNextRow();
	const unsigned int GetFieldCount() const { return fields; }
	unsigned long GetRowCount() const { return rows; }
};

class DatabaseManager {
	MYSQL *connection;

	DatabaseManager(MYSQL *db) : connection(db) {}
public:
	~DatabaseManager();

	static DatabaseManager *Connect(const char *host, const char *user,
		const char *pass, const char *db_name, unsigned int port,
    	const char *unix_socket = 0, unsigned long flags = 0);

	AnswerDB *QuerySelect(const char *sql);
	int QueryInsert(const char *sql);
};

#endif