-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.cpp
166 lines (132 loc) · 4.55 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
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
// Copyright (c) 2022 Perets Dmytro
// Author: Perets Dmytro <[email protected]>
//
// Personal usage is allowed only if this comment was not changed or deleted.
// Commercial usage must be agreed with the author of this comment.
#include "database.h"
void mariadb_create_db(const char* db_user_password)
{
MG_INFO(("Creating database " DB_NAME " ..."));
try
{
sql::Driver* driver = sql::mariadb::get_driver_instance();
sql::SQLString url("jdbc:mariadb://localhost:3306/");
sql::Properties properties(
{ { "user", DB_USER_NAME },
{ "password", db_user_password } }
);
std::unique_ptr<sql::Connection> connection(driver->connect(url, properties));
std::unique_ptr<sql::Statement> stmnt(connection->createStatement());
stmnt->executeQuery("CREATE DATABASE " DB_NAME);
}
catch (sql::SQLException& e) { MG_ERROR(("Database creation failed: %s", e.what())); }
}
void mariadb_drop_db(const char* db_user_password)
{
MG_INFO(("Dropping database " DB_NAME " ..."));
try
{
sql::Driver* driver = sql::mariadb::get_driver_instance();
sql::SQLString url("jdbc:mariadb://localhost:3306/");
sql::Properties properties(
{ { "user", DB_USER_NAME },
{ "password", db_user_password } }
);
std::unique_ptr<sql::Connection> connection(driver->connect(url, properties));
std::unique_ptr<sql::Statement> stmnt(connection->createStatement());
stmnt->executeQuery("DROP DATABASE " DB_NAME);
}
catch (sql::SQLException& e) { MG_ERROR(("Database dropping failed: %s", e.what())); }
}
std::unique_ptr<sql::Connection> mariadb_connect_to_db(const char* db_user_password)
{
MG_INFO(("Connecting to database " DB_NAME " ..."));
try
{
sql::Driver* driver = sql::mariadb::get_driver_instance();
sql::SQLString url("jdbc:mariadb://localhost:3306/" DB_NAME);
sql::Properties properties(
{ { "user", DB_USER_NAME },
{ "password", db_user_password } }
);
auto connection = driver->connect(url, properties);
return std::unique_ptr<sql::Connection>(connection);
}
catch (sql::SQLException& e) { MG_ERROR(("Table creation failed: %s", e.what())); }
return nullptr;
}
void mariadb_create_table(sql::Connection* connection)
{
if (!connection)
{
MG_ERROR(("Connection is nullptr."));
return;
}
MG_INFO(("Creating table " TABLE_NAME " in database " DB_NAME " ..."));
try
{
std::unique_ptr<sql::Statement> stmnt(connection->createStatement());
stmnt->executeQuery(
"CREATE TABLE " TABLE_NAME " "
"( login varchar(" MACRO_STR(MAX_LOGIN) ") NOT NULL PRIMARY KEY,"
"password varchar(" MACRO_STR(MAX_PASSWORD) ") NOT NULL );"
);
}
catch (sql::SQLException& e) { MG_ERROR(("Table creation failed: %s", e.what())); }
}
int mariadb_user_insert(sql::Connection* connection, const char* login, const char* password)
{
if (!connection)
{
MG_ERROR(("Connection is nullptr."));
return 0;
}
MG_INFO(("Updating user '%s' in table " TABLE_NAME " of database " DB_NAME " ...", login));
try
{
std::unique_ptr<sql::PreparedStatement> stmnt(
connection->prepareStatement("INSERT INTO " TABLE_NAME " ( login, password ) values( ?,? );")
);
stmnt->setString(1, login);
stmnt->setString(2, password);
stmnt->executeQuery();
return 1;
}
catch (sql::SQLException& e) { MG_ERROR(("User update failed: %s", e.what())); }
return 0;
}
void mariadb_user_update(sql::Connection* connection, const char* login, const char* password)
{
if (!connection)
{
MG_ERROR(("Connection is nullptr."));
return;
}
MG_INFO(("Saving user '%s' into table " TABLE_NAME " of database " DB_NAME " ...", login));
try
{
std::unique_ptr<sql::PreparedStatement> stmnt(connection->prepareStatement("UPDATE " TABLE_NAME " SET password=? WHERE login=?;"));
stmnt->setString(1, password);
stmnt->setString(2, login);
stmnt->executeQuery();
}
catch (sql::SQLException& e) { MG_ERROR(("User creation failed: %s", e.what())); }
}
const char* mariadb_user_get_password(sql::Connection* connection, const char* login)
{
if (!connection)
{
MG_ERROR(("Connection is nullptr."));
return nullptr;
}
MG_INFO(("Querying %s's password from table " TABLE_NAME " of database " DB_NAME " ...", login));
try
{
std::unique_ptr<sql::PreparedStatement> stmnt(connection->prepareStatement("SELECT password FROM " TABLE_NAME " WHERE login=?;"));
stmnt->setString(1, login);
auto res = std::unique_ptr<sql::ResultSet>(stmnt->executeQuery());
if (res->next()) return res->getString("password").c_str();
}
catch (sql::SQLException& e) { MG_ERROR(("Failed to get user password: %s.", e.what())); }
return nullptr;
}