summaryrefslogtreecommitdiffstats
path: root/src/server/database.cpp
blob: 4438cc22dc38ae4fb0bf4e233a58f9c268d62192 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "database.hpp"

DB_ROW::~DB_ROW()
{
	row = 0;
	/* mariadb documentation:
	Memory associated to MYSQL_ROW will be freed when calling
	mysql_free_result() function. */
}

char *DB_ROW::operator[](const int index)
{
 	return row[index];
}
//////////////////////////////////////////////////////////////

AnswerDB::~AnswerDB()
{
	mysql_free_result(result);
}

DB_ROW *AnswerDB::GetNextRow()
{
	if(current_row)
		delete current_row;

	MYSQL_ROW row = mysql_fetch_row(result);
	if(!row)
		return 0;

	current_row = new DB_ROW(&row, fields);
	return current_row;
}

//////////////////////////////////////////////////////////////

DatabaseManager::~DatabaseManager()
{
	mysql_close(connection);
}

DatabaseManager *DatabaseManager::Connect(const char *host, const char *user,
	const char *pass, const char *db_name, unsigned int port,
    const char *unix_socket, unsigned long flags)
{
	MYSQL *mysql = 0;
	mysql = mysql_init(mysql);

	mysql = mysql_real_connect(mysql, host, user, pass,
		db_name, port, unix_socket, flags);
	if(!mysql)
		return 0;

	return new DatabaseManager(mysql);
}

AnswerDB *DatabaseManager::QuerySelect(const char *sql)
{
	int state = mysql_query(connection, sql);
	if(state != 0)
		return 0;
		// #TODO: error msg

	MYSQL_RES *result = mysql_store_result(connection);
	if(!result) 
		return 0;

	unsigned int fields = mysql_num_fields(result);
	unsigned long rows = mysql_num_rows(result);
	return new AnswerDB(result, fields, rows);
}