view database/postgresql/src/account.cpp @ 29:99792c6c8b06

Server: add initial postgresql account management, #475
author David Demelier <markand@malikania.fr>
date Thu, 26 May 2016 07:32:05 +0200
parents
children a1e80d991968
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 <cassert>
#include <sstream>
#include <stdexcept>
#include <vector>

#include <malikania/dynlib.h>
#include <malikania/account.h>

#include "driver.h"

using namespace malikania;

extern "C" {

namespace {

Account toAccount(PGresult *result, int i)
{
	assert(i < PQntuples(result));

	Account account;

	account.setId(static_cast<std::uint64_t>(std::stoll(PQgetvalue(result, i, 0))));
	account.setName(PQgetvalue(result, i, 1));
	account.setEmail(PQgetvalue(result, i, 2));
	account.setFirstName(PQgetvalue(result, i, 3));
	account.setLastName(PQgetvalue(result, i, 4));
	account.setPassword(PQgetvalue(result, i, 6));

	return account;
}

} // !namespace

DYNLIB_EXPORT void malikania_account_create(Account &account)
{
	std::ostringstream oss;

	oss << "insert into mk_account(ac_name, ac_email, ac_firstname, ac_lastname, ac_password) values (";
	oss << pgsql::escape(account.name()) << ", ";
	oss << pgsql::escape(account.email()) << ", ";
	oss << pgsql::escape(account.firstName()) << ", ";
	oss << pgsql::escape(account.lastName()) << ", ";
	oss << pgsql::escape(account.password()) << ") returning ac_id";

	std::shared_ptr<PGresult> result = pgsql::exec(oss.str());

	account.setId(static_cast<std::uint64_t>(std::stoll(PQgetvalue(result.get(), 0, 0))));
}

DYNLIB_EXPORT void malikania_account_update(Account &account)
{
	std::ostringstream oss;

	oss << "update mk_account set ";
	oss << "ac_name = " << pgsql::escape(account.name()) << ", ";
	oss << "ac_email = " << pgsql::escape(account.email()) << ", ";
	oss << "ac_firstname = " << pgsql::escape(account.firstName()) << ", ";
	oss << "ac_lastname = " << pgsql::escape(account.lastName()) << ", ";
	oss << "ac_password = " << pgsql::escape(account.password()) << " ";
	oss << "where ac_id = " << account.id();

	pgsql::exec(oss.str());
}

DYNLIB_EXPORT void malikania_account_remove(const Account &account)
{
	std::ostringstream oss;

	oss << "delete from mk_account where ac_id = " << account.id();

	pgsql::exec(oss.str());
}

DYNLIB_EXPORT Account malikania_account_get(std::uint64_t id)
{
	std::ostringstream oss;

	oss << "select * from mk_account where ac_id = " << id;

	std::shared_ptr<PGresult> result = pgsql::exec(oss.str());

	if (PQntuples(result.get()) != 1)
		throw std::out_of_range("account not found");

	return toAccount(result.get(), 0);
}

DYNLIB_EXPORT std::vector<malikania::Account> malikania_account_list()
{
	std::vector<Account> accounts;
	std::shared_ptr<PGresult> result = pgsql::exec("select * from mk_account");

	for (int i = 0; i < PQntuples(result.get()); ++i)
		accounts.push_back(toAccount(result.get(), i));

	return accounts;
}

DYNLIB_EXPORT std::uint64_t malikania_account_count()
{
	std::shared_ptr<PGresult> result = pgsql::exec("select count(*) from mk_account");

	return static_cast<std::uint64_t>(std::stoll(PQgetvalue(result.get(), 0, 0)));
}

DYNLIB_EXPORT void malikania_account_clear()
{
	pgsql::exec("delete from mk_account");
}

}