comparison 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
comparison
equal deleted inserted replaced
105:0a4a246d765b 106:f8c6d2244795
1 /*
2 * sqlite_spell_dao.cpp -- spell_dao for SQLite
3 *
4 * Copyright (c) 2013-2017 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 <malikania/server/db/spell_dao.hpp>
20 #include <malikania/server/db/character.hpp>
21 #include <malikania/server/db/spell.hpp>
22
23 #include "common.hpp"
24
25 namespace mlk {
26
27 namespace server {
28
29 class sqlite_spell_dao : public spell_dao {
30 public:
31 void save(spell&, character& parent) override;
32 void set_level(spell&s, std::uint8_t level) override;
33 void remove(spell&) override;
34 };
35
36 void sqlite_spell_dao::save(spell& s, character& parent)
37 {
38 const std::string sql(
39 "INSERT INTO spell(account_id, level) VALUES(?, ?)"
40 );
41
42 mlk::server::sqlite::exec(sql, {parent.id(), static_cast<std::uint64_t>(s.level())});
43
44 s.set_character(parent.shared_from_this());
45 s.set_id(sqlite3_last_insert_rowid(mlk::server::sqlite::handle));
46 }
47
48 void sqlite_spell_dao::set_level(spell& s, std::uint8_t level)
49 {
50 const std::string sql(
51 "UPDATE spell"
52 " SET level = ?"
53 " WHERE id = ?"
54 );
55
56 mlk::server::sqlite::exec(sql, {static_cast<std::uint64_t>(level), s.id()});
57 }
58
59 void sqlite_spell_dao::remove(spell& s)
60 {
61 const std::string sql(
62 "DELETE"
63 " FROM spell"
64 " WHERE id = ?"
65 );
66
67 mlk::server::sqlite::exec(sql, {s.id()});
68 }
69
70 } // !server
71
72 } // !mlk
73
74 extern "C" {
75
76 BOOST_SYMBOL_EXPORT
77 std::unique_ptr<mlk::server::spell_dao> mlk_spell_dao()
78 {
79 return std::make_unique<mlk::server::sqlite_spell_dao>();
80 }
81
82 } // !C