view database/sqlite/src/sqlite_spell_dao.cpp @ 106:f8c6d2244795

Server: rework model and dao
author David Demelier <markand@malikania.fr>
date Mon, 04 Sep 2017 19:41:18 +0200
parents
children
line wrap: on
line source

/*
 * sqlite_spell_dao.cpp -- spell_dao for SQLite
 *
 * Copyright (c) 2013-2017 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 <malikania/server/db/spell_dao.hpp>
#include <malikania/server/db/character.hpp>
#include <malikania/server/db/spell.hpp>

#include "common.hpp"

namespace mlk {

namespace server {

class sqlite_spell_dao : public spell_dao {
public:
    void save(spell&, character& parent) override;
    void set_level(spell&s, std::uint8_t level) override;
    void remove(spell&) override;
};

void sqlite_spell_dao::save(spell& s, character& parent)
{
    const std::string sql(
        "INSERT INTO spell(account_id, level) VALUES(?, ?)"
    );

    mlk::server::sqlite::exec(sql, {parent.id(), static_cast<std::uint64_t>(s.level())});

    s.set_character(parent.shared_from_this());
    s.set_id(sqlite3_last_insert_rowid(mlk::server::sqlite::handle));
}

void sqlite_spell_dao::set_level(spell& s, std::uint8_t level)
{
    const std::string sql(
        "UPDATE spell"
        "   SET level = ?"
        " WHERE id = ?"
    );

    mlk::server::sqlite::exec(sql, {static_cast<std::uint64_t>(level), s.id()});
}

void sqlite_spell_dao::remove(spell& s)
{
    const std::string sql(
        "DELETE"
        "  FROM spell"
        " WHERE id = ?"
    );

    mlk::server::sqlite::exec(sql, {s.id()});
}

} // !server

} // !mlk

extern "C" {

BOOST_SYMBOL_EXPORT
std::unique_ptr<mlk::server::spell_dao> mlk_spell_dao()
{
    return std::make_unique<mlk::server::sqlite_spell_dao>();
}

} // !C