This repository has been archived by the owner on Mar 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
/
database_driver.cpp
185 lines (159 loc) · 4.11 KB
/
database_driver.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//////////////////////////////////////////////////////////////////////
// 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_driver.h"
#ifdef __USE_MYSQL__
#include "database_driver_mysql.h"
#endif
#ifdef __USE_SQLITE__
#include "database_driver_sqlite.h"
#endif
#ifdef __USE_ODBC__
#include "database_driver_odbc.h"
#endif
#ifdef __USE_PGSQL__
#include "database_driver_pgsql.h"
#endif
#include "configmanager.h"
extern ConfigManager g_config;
boost::recursive_mutex DBQuery::database_lock;
DatabaseDriver* DatabaseDriver::_instance = NULL;
DatabaseDriver* DatabaseDriver::instance(){
if(!_instance){
#ifdef __USE_MYSQL__
if(g_config.getString(ConfigManager::SQL_TYPE) == "mysql")
_instance = new DatabaseMySQL;
#endif
#ifdef __USE_ODBC__
if(g_config.getString(ConfigManager::SQL_TYPE) == "odbc")
_instance = new DatabaseODBC;
#endif
#ifdef __USE_SQLITE__
if(g_config.getString(ConfigManager::SQL_TYPE) == "sqlite")
_instance = new DatabaseSQLite;
#endif
#ifdef __USE_PGSQL__
if(g_config.getString(ConfigManager::SQL_TYPE) == "pgsql")
_instance = new DatabasePgSQL;
#endif
}
return _instance;
}
bool DatabaseDriver::executeQuery(DBQuery &query)
{
return internalQuery(query.str());
}
bool DatabaseDriver::executeQuery(const std::string &query)
{
return internalQuery(query);
}
DBResult_ptr DatabaseDriver::storeQuery(const std::string &query)
{
return internalSelectQuery(query);
}
DBResult_ptr DatabaseDriver::storeQuery(DBQuery &query)
{
return storeQuery(query.str());
}
void DatabaseDriver::freeResult(DBResult *res)
{
throw std::runtime_error("No database driver loaded, yet a DBResult was freed.");
}
DBResult_ptr DatabaseDriver::verifyResult(DBResult_ptr result)
{
if(!result->advance()){
return DBResult_ptr();
}
else{
return result;
}
}
// DBQuery
DBQuery::DBQuery()
{
database_lock.lock();
}
DBQuery::~DBQuery()
{
database_lock.unlock();
}
// DBInsert
DBInsert::DBInsert(DatabaseDriver* db)
{
m_db = db;
m_rows = 0;
// checks if current database engine supports multi line INSERTs
m_multiLine = m_db->getParam(DBPARAM_MULTIINSERT) != 0;
}
void DBInsert::setQuery(const std::string& query)
{
m_query = query;
m_buf.str("");
m_rows = 0;
}
bool DBInsert::addRow(const std::string& row)
{
if(m_multiLine){
m_rows++;
size_t size = m_buf.tellp();
// adds new row to buffer
if(size == 0){
m_buf << "(" << row << ")";
}
else if(size > 8192){
if(!execute())
return false;
m_buf << "(" << row << ")";
}
else{
m_buf << ",(" + row + ")";
}
return true;
}
else{
// executes INSERT for current row
return m_db->executeQuery(m_query + "(" + row + ")" );
}
}
bool DBInsert::addRowAndReset(std::ostringstream& row)
{
bool ret = addRow(row.str());
row.str("");
return ret;
}
bool DBInsert::execute()
{
if(m_multiLine && m_buf.tellp() > 0){
if(m_rows == 0){
//no rows to execute
return true;
}
// executes buffer
bool res = m_db->executeQuery(m_query + m_buf.str());
// Reset counters
m_rows = 0;
m_buf.str("");
return res;
}
else{
// INSERTs were executed on-fly
return true;
}
}