diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/database/sqlite/src/account.cpp	Sun Nov 27 18:00:49 2016 +0100
@@ -0,0 +1,177 @@
+/*
+ * account.cpp -- account management
+ *
+ * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <vector>
+
+#include <malikania/account.hpp>
+#include <malikania/dynlib.hpp>
+
+#include "driver.hpp"
+
+using namespace malikania;
+
+namespace {
+
+const std::string create_query(
+    "insert into mk_account("
+    "  ac_name, ac_email, ac_firstname, ac_lastname, ac_password"
+    ") values (?, ?, ?, ?, ?)"
+);
+
+const std::string update_query(
+    "update mk_account set"
+    "  ac_name = ?, "           // 0
+    "  ac_email = ?, "          // 1
+    "  ac_firstname = ?, "      // 2
+    "  ac_lastname = ?, "       // 3
+    "  ac_password = ? "        // 4
+    "where ac_id = ?"           // 5
+);
+
+const std::string delete_query(
+    "delete from mk_account where ac_id = ?"
+);
+
+const std::string get_query(
+    "select ac_id, ac_name, ac_email, ac_firstname, ac_lastname, ac_password "
+    "from mk_account "
+    "where ac_id = ?"
+);
+
+const std::string list_query(
+    "select ac_id, ac_name, ac_email, ac_firstname, ac_lastname, ac_password "
+    "from mk_account"
+);
+
+const std::string clear_query(
+    "delete from mk_account"
+);
+
+const std::string count_query(
+    "select count(*) from mk_account"
+);
+
+Account to_account(sqlite::statement& stmt)
+{
+    Account ac;
+
+    ac.setId(sqlite3_column_int64(stmt.get(), 0));
+    ac.setName(reinterpret_cast<const char *>(sqlite3_column_text(stmt.get(), 1)));
+    ac.setEmail(reinterpret_cast<const char *>(sqlite3_column_text(stmt.get(), 2)));
+    ac.setFirstName(reinterpret_cast<const char *>(sqlite3_column_text(stmt.get(), 3)));
+    ac.setLastName(reinterpret_cast<const char *>(sqlite3_column_text(stmt.get(), 4)));
+    ac.setPassword(reinterpret_cast<const char *>(sqlite3_column_text(stmt.get(), 5)));
+
+    return ac;
+}
+
+} // !namespace
+
+#include <iostream>
+
+extern "C" {
+
+DYNLIB_EXPORT void malikania_account_create(Account& account)
+{
+    auto stmt = sqlite::prepare(create_query);
+
+    sqlite3_bind_text(stmt.get(), 1, account.name().c_str(), -1, nullptr);
+    sqlite3_bind_text(stmt.get(), 2, account.email().c_str(), -1, nullptr);
+    sqlite3_bind_text(stmt.get(), 3, account.firstName().c_str(), -1, nullptr);
+    sqlite3_bind_text(stmt.get(), 4, account.lastName().c_str(), -1, nullptr);
+    sqlite3_bind_text(stmt.get(), 5, account.password().c_str(), -1, nullptr);
+
+    if (sqlite3_step(stmt.get()) != SQLITE_DONE) {
+        throw std::runtime_error(sqlite3_errmsg(sqlite::database.get()));
+    }
+
+    account.setId(sqlite3_last_insert_rowid(sqlite::database.get()));
+}
+
+DYNLIB_EXPORT void malikania_account_update(Account& account)
+{
+    auto stmt = sqlite::prepare(update_query);
+
+    sqlite3_bind_text(stmt.get(), 1, account.name().c_str(), -1, nullptr);
+    sqlite3_bind_text(stmt.get(), 2, account.email().c_str(), -1, nullptr);
+    sqlite3_bind_text(stmt.get(), 3, account.firstName().c_str(), -1, nullptr);
+    sqlite3_bind_text(stmt.get(), 4, account.lastName().c_str(), -1, nullptr);
+    sqlite3_bind_text(stmt.get(), 5, account.password().c_str(), -1, nullptr);
+    sqlite3_bind_int64(stmt.get(), 6, account.id());
+
+    if (sqlite3_step(stmt.get()) != SQLITE_DONE) {
+        throw std::runtime_error(sqlite3_errmsg(sqlite::database.get()));
+    }
+}
+
+DYNLIB_EXPORT void malikania_account_remove(const Account& account)
+{
+    auto stmt = sqlite::prepare(delete_query);
+
+    sqlite3_bind_int64(stmt.get(), 1, account.id());
+
+    if (sqlite3_step(stmt.get()) != SQLITE_OK) {
+        throw std::runtime_error(sqlite3_errmsg(sqlite::database.get()));
+    }
+}
+
+DYNLIB_EXPORT Account malikania_account_get(std::uint64_t id)
+{
+    auto stmt = sqlite::prepare(get_query);
+
+    sqlite3_bind_int64(stmt.get(), 1, id);
+
+    if (sqlite3_step(stmt.get()) != SQLITE_ROW) {
+        throw std::runtime_error(sqlite3_errmsg(sqlite::database.get()));
+    }
+
+    return to_account(stmt);
+}
+
+DYNLIB_EXPORT std::vector<Account> malikania_account_list()
+{
+    auto stmt = sqlite::prepare(list_query);
+
+    std::vector<Account> list;
+
+    while (sqlite3_step(stmt.get()) == SQLITE_ROW) {
+        list.push_back(to_account(stmt));
+    }
+
+    return list;
+}
+
+DYNLIB_EXPORT void malikania_account_clear()
+{
+    if (sqlite3_exec(sqlite::database.get(), clear_query.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) {
+        throw std::runtime_error(sqlite3_errmsg(sqlite::database.get()));
+    }
+}
+
+DYNLIB_EXPORT std::uint64_t malikania_account_count()
+{
+    auto stmt = sqlite::prepare(count_query);
+
+    if (sqlite3_step(stmt.get()) != SQLITE_ROW) {
+        throw std::runtime_error(sqlite3_errmsg(sqlite::database.get()));
+    }
+
+    return sqlite3_column_int64(stmt.get(), 0);
+}
+
+} // !C