This repository has been 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
/
Database.cpp
122 lines (103 loc) · 3.2 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
/*
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"
#ifdef __MARIADB__
#include <mysql.h>
#endif
#ifdef __SQLITE__
#include <sqlite3.h>
#endif
#if !defined(__MARIADB__) && !defined(__SQLITE__)
#error "Please change your configuration and choose at least one database engine!"
#endif
MDKDLLAPI CDatabase::CDatabase()
{
#ifdef __MARIADB__
m_eDatabasetype = DATABASE_TYPE_MARIADB;
#elif defined(__SQLITE__)
m_eDatabasetype = DATABASE_TYPE_SQLITE;
#endif
m_Pointed_db = NULL;
m_uiProtocol = 0;
}
MDKDLLAPI CDatabase::~CDatabase()
{
Disconnect();
}
MDKDLLAPI bool CDatabase::Connect(EDatabaseType type, const char *host, int port, const char *username, const char *database_name, const char *password)
{
m_eDatabasetype = type;
#ifdef __MARIADB__
if (m_eDatabasetype == DATABASE_TYPE_MARIADB)
{
if (!m_Pointed_db)
{
MYSQL* connection1 = NULL, *connection2 = NULL;
connection2 = mysql_init(connection1);
if (!connection1)
m_Pointed_db = (mdk_database)connection2;
else
m_Pointed_db = (mdk_database)connection1;
}
if (port > 0)
m_uiProtocol = MYSQL_PROTOCOL_TCP;
else
m_uiProtocol = MYSQL_PROTOCOL_SOCKET;
mysql_options((MYSQL*)m_Pointed_db, MYSQL_OPT_PROTOCOL, &m_uiProtocol);
if (!mysql_real_connect((MYSQL*)m_Pointed_db, port > 0 ? host : NULL, username, password, database_name, port, port > 0 ? NULL : host, 0))
{
LOG_ERROR("Database", "Cannot connect to Database Server. Error: %s\n", mysql_error((MYSQL*)m_Pointed_db));
return false;
}
}
#endif
#ifdef __SQLITE__
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()
{
#ifdef __MARIADB__
if ((m_Pointed_db) && m_eDatabasetype == DATABASE_TYPE_MARIADB)
mysql_close((MYSQL*)m_Pointed_db);
#endif
#ifdef __SQLITE__
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 __MARIADB__
if (m_eDatabasetype == DATABASE_TYPE_MARIADB)
{
if (mysql_stat((MYSQL*)m_Pointed_db) == NULL)
return false;
}
#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;
}