forked from Jo-Tran/cryingdamson-0.3.6-8.60-V8.2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.cpp
140 lines (123 loc) · 3.17 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 3 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, see <http://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////
#include "otpch.h"
#include <string>
#include "database.h"
#ifdef __USE_MYSQL__
#include "databasemysql.h"
#endif
#ifdef __USE_SQLITE__
#include "databasesqlite.h"
#endif
#ifdef __USE_ODBC__
#include "databaseodbc.h"
#endif
#ifdef __USE_PGSQL__
#include "databasepgsql.h"
#endif
#if defined MULTI_SQL_DRIVERS
#include "configmanager.h"
extern ConfigManager g_config;
#endif
boost::recursive_mutex DBQuery::databaseLock;
Database* _Database::_instance = NULL;
Database* _Database::getInstance()
{
if(!_instance)
{
#if defined MULTI_SQL_DRIVERS
#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
#else
_instance = new Database;
#endif
}
_instance->use();
return _instance;
}
DBResult* _Database::verifyResult(DBResult* result)
{
if(result->next())
return result;
result->free();
result = NULL;
return NULL;
}
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);
}
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) // executes INSERT for current row
return m_db->executeQuery(m_query + "(" + row + ")");
m_rows++;
int32_t size = m_buf.length();
// adds new row to buffer
if(!size)
m_buf = "(" + row + ")";
else if(size > 8192)
{
if(!execute())
return false;
m_buf = "(" + row + ")";
}
else
m_buf += ",(" + row + ")";
return true;
}
bool DBInsert::addRow(std::stringstream& row)
{
bool ret = addRow(row.str());
row.str("");
return ret;
}
bool DBInsert::execute()
{
if(!m_multiLine || m_buf.length() < 1) // INSERTs were executed on-fly
return true;
if(!m_rows) //no rows to execute
return true;
m_rows = 0;
// executes buffer
bool res = m_db->executeQuery(m_query + m_buf);
m_buf = "";
return res;
}