forked from Flatlander57/TheImaginedServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.cpp
122 lines (107 loc) · 2.78 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
////////////////////////////////////////////////////////////////////////
// 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_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::m_instance = NULL;
Database* _Database::getInstance()
{
if(!m_instance)
{
#if defined MULTI_SQL_DRIVERS
#ifdef __USE_MYSQL__
if(g_config.getString(ConfigManager::SQL_TYPE) == "mysql")
m_instance = new DatabaseMySQL;
#endif
#ifdef __USE_MYSQLPP__
else if(g_config.getString(ConfigManager::SQL_TYPE) == "mysql++")
m_instance = new DatabaseMySQLpp;
#endif
#ifdef __USE_SQLITE__
else if(g_config.getString(ConfigManager::SQL_TYPE) == "sqlite")
m_instance = new DatabaseSQLite;
#endif
#ifdef __USE_PGSQL__
else if(g_config.getString(ConfigManager::SQL_TYPE) == "pgsql")
m_instance = new DatabasePgSQL;
#endif
#else
m_instance = new Database;
#endif
}
m_instance->use();
return m_instance;
}
DBResult* _Database::verifyResult(DBResult* result)
{
if(result->next())
return result;
result->free();
return NULL;
}
void DBInsert::setQuery(std::string query)
{
m_query = query;
m_buf = "";
m_rows = 0;
}
bool DBInsert::addRow(std::string row)
{
if(!m_db->multiLine())
return m_db->query(m_query + "(" + row + ")");
++m_rows;
if(m_buf.empty())
m_buf = "(" + row + ")";
else if(m_buf.length() > 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_db->multiLine() || m_buf.empty() || !m_rows)
return true;
m_rows = 0;
bool ret = m_db->query(m_query + m_buf);
m_buf = "";
return ret;
}