This repository was archived by the owner on May 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabase.cpp
More file actions
147 lines (116 loc) · 3.78 KB
/
Copy pathDatabase.cpp
File metadata and controls
147 lines (116 loc) · 3.78 KB
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
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define MDK_EXPORT_FUNCTIONS 1 /*Export the methods*/
#include "Database.h"
#include "Utility.h"
static bool bIsConnecting = false;
static bool bIsConnected = false;
#ifdef ENABLE_MYSQL_SUPPORT
#include "centosworkaround.h"
#include <amy.hpp>
static void amy_handle_connect(AMY_SYSTEM_NS::error_code const& ec)
{
bIsConnecting = false;
if (!ec)
bIsConnected = true;
}
#endif
#ifdef ENABLE_SQLITE3_SUPPORT
#include <sqlite3.h>
#endif
#if !defined(ENABLE_MYSQL_SUPPORT) && !defined(ENABLE_SQLITE3_SUPPORT)
#error "Please change your configuration and choose at least one database engine!"
#endif
MDKDLLAPI CDatabase::CDatabase()
{
#ifdef ENABLE_MYSQL_SUPPORT
m_eDatabasetype = DATABASE_TYPE_MYSQL;
#elif defined(ENABLE_SQLITE3_SUPPORT)
m_eDatabasetype = DATABASE_TYPE_SQLITE;
#endif
bIsConnecting = false;
m_Pointed_db = NULL;
bIsConnected = false;
}
MDKDLLAPI CDatabase::~CDatabase()
{
Disconnect();
}
MDKDLLAPI bool CDatabase::Connect(void* io_service, EDatabaseType type, const char *host, int port, const char *username, const char *database_name, const char *password)
{
m_eDatabasetype = type;
bIsConnecting = false;
if (bIsConnected)
Disconnect();
#ifdef ENABLE_MYSQL_SUPPORT
if (m_eDatabasetype == DATABASE_TYPE_MYSQL)
{
AMY_ASIO_NS::io_service* io = (AMY_ASIO_NS::io_service*)io_service;
AMY_ASIO_NS::ip::tcp::endpoint ep;
amy::connector* connector = new amy::connector(*io);
ep = AMY_ASIO_NS::ip::tcp::endpoint(AMY_ASIO_NS::ip::address::from_string(host), port);
connector->set_option(amy::options::reconnect(true));
connector->set_option(amy::options::connect_timeout(600));
connector->set_option(amy::options::read_timeout(10000));
connector->set_option(amy::options::write_timeout(10000));
connector->async_connect(ep, amy::auth_info(username, password), database_name, amy::default_flags, std::bind(amy_handle_connect, amy::placeholders::error));
bIsConnecting = true;
m_Pointed_db = connector;
}
#endif
#ifdef ENABLE_SQLITE3_SUPPORT
if (m_eDatabasetype == DATABASE_TYPE_SQLITE)
{
if (sqlite3_open_v2(database_name, (sqlite3**)&m_Pointed_db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL)) {
LOG_ERROR("Database", "Cannot open Database. Error: %s\n", sqlite3_errmsg((sqlite3*)m_Pointed_db));
return false;
}
}
#endif
return true;
}
MDKDLLAPI void CDatabase::Disconnect()
{
bIsConnecting = false;
bIsConnected = false;
#ifdef ENABLE_MYSQL_SUPPORT
if ((m_Pointed_db) && m_eDatabasetype == DATABASE_TYPE_MYSQL)
{
amy::connector* connector = (amy::connector*)m_Pointed_db;
delete connector;
}
#endif
#ifdef ENABLE_SQLITE3_SUPPORT
if ((m_Pointed_db) && m_eDatabasetype == DATABASE_TYPE_SQLITE)
sqlite3_close((sqlite3*)m_Pointed_db);
#endif
m_Pointed_db = NULL;
}
MDKDLLAPI bool CDatabase::IsConnected()
{
if (!m_Pointed_db)
return false;
#ifdef ENABLE_MYSQL_SUPPORT
if (m_eDatabasetype == DATABASE_TYPE_MYSQL)
{
return bIsConnected;
}
#endif
// SQLite dosen't need this code because the database is always "connected" thanks to the nature
// of SQLite, the file is always opened until you close it
return true;
}
MDKDLLAPI bool CDatabase::IsConnecting()
{
return bIsConnecting;
}