comparison database/sqlite/src/account.cpp @ 39:56ab2f0c90dc

Server: add sqlite basic backend, closes #558
author David Demelier <markand@malikania.fr>
date Sun, 27 Nov 2016 18:00:49 +0100
parents
children 1e206fdc7021
comparison
equal deleted inserted replaced
38:ecf316d52f5d 39:56ab2f0c90dc
1 /*
2 * account.cpp -- account management
3 *
4 * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <vector>
20
21 #include <malikania/account.hpp>
22 #include <malikania/dynlib.hpp>
23
24 #include "driver.hpp"
25
26 using namespace malikania;
27
28 namespace {
29
30 const std::string create_query(
31 "insert into mk_account("
32 " ac_name, ac_email, ac_firstname, ac_lastname, ac_password"
33 ") values (?, ?, ?, ?, ?)"
34 );
35
36 const std::string update_query(
37 "update mk_account set"
38 " ac_name = ?, " // 0
39 " ac_email = ?, " // 1
40 " ac_firstname = ?, " // 2
41 " ac_lastname = ?, " // 3
42 " ac_password = ? " // 4
43 "where ac_id = ?" // 5
44 );
45
46 const std::string delete_query(
47 "delete from mk_account where ac_id = ?"
48 );
49
50 const std::string get_query(
51 "select ac_id, ac_name, ac_email, ac_firstname, ac_lastname, ac_password "
52 "from mk_account "
53 "where ac_id = ?"
54 );
55
56 const std::string list_query(
57 "select ac_id, ac_name, ac_email, ac_firstname, ac_lastname, ac_password "
58 "from mk_account"
59 );
60
61 const std::string clear_query(
62 "delete from mk_account"
63 );
64
65 const std::string count_query(
66 "select count(*) from mk_account"
67 );
68
69 Account to_account(sqlite::statement& stmt)
70 {
71 Account ac;
72
73 ac.setId(sqlite3_column_int64(stmt.get(), 0));
74 ac.setName(reinterpret_cast<const char *>(sqlite3_column_text(stmt.get(), 1)));
75 ac.setEmail(reinterpret_cast<const char *>(sqlite3_column_text(stmt.get(), 2)));
76 ac.setFirstName(reinterpret_cast<const char *>(sqlite3_column_text(stmt.get(), 3)));
77 ac.setLastName(reinterpret_cast<const char *>(sqlite3_column_text(stmt.get(), 4)));
78 ac.setPassword(reinterpret_cast<const char *>(sqlite3_column_text(stmt.get(), 5)));
79
80 return ac;
81 }
82
83 } // !namespace
84
85 #include <iostream>
86
87 extern "C" {
88
89 DYNLIB_EXPORT void malikania_account_create(Account& account)
90 {
91 auto stmt = sqlite::prepare(create_query);
92
93 sqlite3_bind_text(stmt.get(), 1, account.name().c_str(), -1, nullptr);
94 sqlite3_bind_text(stmt.get(), 2, account.email().c_str(), -1, nullptr);
95 sqlite3_bind_text(stmt.get(), 3, account.firstName().c_str(), -1, nullptr);
96 sqlite3_bind_text(stmt.get(), 4, account.lastName().c_str(), -1, nullptr);
97 sqlite3_bind_text(stmt.get(), 5, account.password().c_str(), -1, nullptr);
98
99 if (sqlite3_step(stmt.get()) != SQLITE_DONE) {
100 throw std::runtime_error(sqlite3_errmsg(sqlite::database.get()));
101 }
102
103 account.setId(sqlite3_last_insert_rowid(sqlite::database.get()));
104 }
105
106 DYNLIB_EXPORT void malikania_account_update(Account& account)
107 {
108 auto stmt = sqlite::prepare(update_query);
109
110 sqlite3_bind_text(stmt.get(), 1, account.name().c_str(), -1, nullptr);
111 sqlite3_bind_text(stmt.get(), 2, account.email().c_str(), -1, nullptr);
112 sqlite3_bind_text(stmt.get(), 3, account.firstName().c_str(), -1, nullptr);
113 sqlite3_bind_text(stmt.get(), 4, account.lastName().c_str(), -1, nullptr);
114 sqlite3_bind_text(stmt.get(), 5, account.password().c_str(), -1, nullptr);
115 sqlite3_bind_int64(stmt.get(), 6, account.id());
116
117 if (sqlite3_step(stmt.get()) != SQLITE_DONE) {
118 throw std::runtime_error(sqlite3_errmsg(sqlite::database.get()));
119 }
120 }
121
122 DYNLIB_EXPORT void malikania_account_remove(const Account& account)
123 {
124 auto stmt = sqlite::prepare(delete_query);
125
126 sqlite3_bind_int64(stmt.get(), 1, account.id());
127
128 if (sqlite3_step(stmt.get()) != SQLITE_OK) {
129 throw std::runtime_error(sqlite3_errmsg(sqlite::database.get()));
130 }
131 }
132
133 DYNLIB_EXPORT Account malikania_account_get(std::uint64_t id)
134 {
135 auto stmt = sqlite::prepare(get_query);
136
137 sqlite3_bind_int64(stmt.get(), 1, id);
138
139 if (sqlite3_step(stmt.get()) != SQLITE_ROW) {
140 throw std::runtime_error(sqlite3_errmsg(sqlite::database.get()));
141 }
142
143 return to_account(stmt);
144 }
145
146 DYNLIB_EXPORT std::vector<Account> malikania_account_list()
147 {
148 auto stmt = sqlite::prepare(list_query);
149
150 std::vector<Account> list;
151
152 while (sqlite3_step(stmt.get()) == SQLITE_ROW) {
153 list.push_back(to_account(stmt));
154 }
155
156 return list;
157 }
158
159 DYNLIB_EXPORT void malikania_account_clear()
160 {
161 if (sqlite3_exec(sqlite::database.get(), clear_query.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) {
162 throw std::runtime_error(sqlite3_errmsg(sqlite::database.get()));
163 }
164 }
165
166 DYNLIB_EXPORT std::uint64_t malikania_account_count()
167 {
168 auto stmt = sqlite::prepare(count_query);
169
170 if (sqlite3_step(stmt.get()) != SQLITE_ROW) {
171 throw std::runtime_error(sqlite3_errmsg(sqlite::database.get()));
172 }
173
174 return sqlite3_column_int64(stmt.get(), 0);
175 }
176
177 } // !C