-
Notifications
You must be signed in to change notification settings - Fork 21
/
database.cpp
140 lines (118 loc) · 3.02 KB
/
database.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
//////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////
#include "otpch.h"
#include "database.h"
#include <string>
#ifdef __USE_MYSQL__
#include "databasemysql.h"
#endif
#ifdef __USE_SQLITE__
#include "databasesqlite.h"
#endif
#if defined MULTI_SQL_DRIVERS
#include "tools.h"
#include "configmanager.h"
extern ConfigManager g_config;
#endif
boost::recursive_mutex DBQuery::database_lock;
Database* _Database::_instance = NULL;
Database* _Database::getInstance()
{
if(!_instance)
{
#if defined MULTI_SQL_DRIVERS
std::string sqlType = asLowerCaseString(g_config.getString(ConfigManager::SQL_TYPE));
if(sqlType == "mysql")
_instance = new DatabaseMySQL;
else if(sqlType == "sqlite")
_instance = new DatabaseSQLite;
else
_instance = new Database;
#else
_instance = new Database;
#endif
}
return _instance;
}
DBResult* _Database::verifyResult(DBResult* result)
{
if(!result->next())
{
_instance->freeResult(result);
return NULL;
}
return result;
}
DBQuery::DBQuery()
{
database_lock.lock();
}
DBQuery::~DBQuery()
{
database_lock.unlock();
}
DBInsert::DBInsert(Database* db)
{
m_db = db;
m_rows = 0;
// checks if current database engine supports multiline INSERTs
m_multiLine = m_db->getParam(DBPARAM_MULTIINSERT) != 0;
}
void DBInsert::setQuery(const std::string& query)
{
m_query = query;
m_buf = "";
m_rows = 0;
}
bool DBInsert::addRow(const std::string& row)
{
if(!m_multiLine)
return m_db->executeQuery(m_query + "(" + row + ")" ); // executes INSERT for current row
m_rows++;
// adds new row to buffer
size_t size = m_buf.length();
if(size == 0)
m_buf = "(" + row + ")";
else if(size > 8192)
{
if(!execute())
return false;
m_buf = "(" + row + ")";
}
else
m_buf += ",(" + row + ")";
return true;
}
bool DBInsert::addRow(std::ostringstream& row)
{
bool ret = addRow(row.str());
row.str("");
return ret;
}
bool DBInsert::execute()
{
if(!m_multiLine || m_buf.length() == 0 || m_rows == 0)
return true;
m_rows = 0;
// executes buffer
bool res = m_db->executeQuery(m_query + m_buf);
m_buf = "";
return res;
}