view database/sqlite/src/account.cpp @ 43:fabbe1759cec

Misc: switch to mlk namespace, closes #589
author David Demelier <markand@malikania.fr>
date Tue, 29 Nov 2016 22:25:17 +0100
parents a47a4477f347
children b0593a3e2ca8
line wrap: on
line source

/*
 * 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 <boost/dll.hpp>

#include <vector>

#include <malikania/account.hpp>

#include "driver.hpp"

using namespace mlk;

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.id = sqlite3_column_int64(stmt.get(), 0);
    ac.name = reinterpret_cast<const char *>(sqlite3_column_text(stmt.get(), 1));
    ac.email = reinterpret_cast<const char *>(sqlite3_column_text(stmt.get(), 2));
    ac.first_name = reinterpret_cast<const char *>(sqlite3_column_text(stmt.get(), 3));
    ac.last_name = reinterpret_cast<const char *>(sqlite3_column_text(stmt.get(), 4));
    ac.password = reinterpret_cast<const char *>(sqlite3_column_text(stmt.get(), 5));

    return ac;
}

} // !namespace

#include <iostream>

extern "C" {

BOOST_SYMBOL_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.first_name.c_str(), -1, nullptr);
    sqlite3_bind_text(stmt.get(), 4, account.last_name.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.id = sqlite3_last_insert_rowid(sqlite::database.get());
}

BOOST_SYMBOL_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.first_name.c_str(), -1, nullptr);
    sqlite3_bind_text(stmt.get(), 4, account.last_name.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()));
    }
}

BOOST_SYMBOL_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()));
    }
}

BOOST_SYMBOL_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);
}

BOOST_SYMBOL_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;
}

BOOST_SYMBOL_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()));
    }
}

BOOST_SYMBOL_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