changeset 138:532f259557dd

Server: switch to std::uint64_t in model, closes #706 Database objects now have model id starting from 1 and 0 is considered as draft.
author David Demelier <markand@malikania.fr>
date Fri, 06 Oct 2017 16:30:24 +0200
parents 0f9a8ddee022
children b80d37e71b87
files libdb-sqlite/malikania/server/db/sqlite_character.cpp libdb-sqlite/malikania/server/db/sqlite_character.hpp libserver-test/malikania/server/db/broken_character.cpp libserver-test/malikania/server/db/broken_character.hpp libserver-test/malikania/server/db/test_account.hpp libserver-test/malikania/server/db/test_character.cpp libserver-test/malikania/server/db/test_character.hpp libserver-test/malikania/server/db/test_database.hpp libserver-test/malikania/server/db/test_spell.cpp libserver-test/malikania/server/db/test_spell.hpp libserver/malikania/server/db/character.hpp libserver/malikania/server/db/model.hpp libserver/malikania/server/db/spell.hpp
diffstat 13 files changed, 25 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/libdb-sqlite/malikania/server/db/sqlite_character.cpp	Thu Oct 05 08:38:47 2017 +0200
+++ b/libdb-sqlite/malikania/server/db/sqlite_character.cpp	Fri Oct 06 16:30:24 2017 +0200
@@ -35,7 +35,7 @@
     exec(db_.instance(), sql, {level, id_});
 }
 
-void sqlite_character::do_save(std::int64_t account_id)
+void sqlite_character::do_save(std::uint64_t account_id)
 {
     const std::string sql(
         "INSERT INTO character("
@@ -79,7 +79,7 @@
     return list;
 }
 
-character_list sqlite_dao::characters_for_account(std::int64_t account_id)
+character_list sqlite_dao::characters_for_account(std::uint64_t account_id)
 {
     const std::string sql(
         "SELECT *"
--- a/libdb-sqlite/malikania/server/db/sqlite_character.hpp	Thu Oct 05 08:38:47 2017 +0200
+++ b/libdb-sqlite/malikania/server/db/sqlite_character.hpp	Fri Oct 06 16:30:24 2017 +0200
@@ -53,7 +53,7 @@
     /**
      * \copydoc character::do_save
      */
-    void do_save(std::int64_t account_id) override;
+    void do_save(std::uint64_t account_id) override;
 
     /**
      * \copydoc character::do_remove
@@ -101,7 +101,7 @@
     /**
      * Get the set of characters for the given account.
      */
-    character_list characters_for_account(std::int64_t account_id);
+    character_list characters_for_account(std::uint64_t account_id);
 };
 
 } // !server
--- a/libserver-test/malikania/server/db/broken_character.cpp	Thu Oct 05 08:38:47 2017 +0200
+++ b/libserver-test/malikania/server/db/broken_character.cpp	Fri Oct 06 16:30:24 2017 +0200
@@ -22,7 +22,7 @@
 
 namespace server {
 
-void broken_character::do_save(std::int64_t)
+void broken_character::do_save(std::uint64_t)
 {
     if (!bool(allow_ & allow_flags::save))
         throw std::runtime_error("broken do_save");
--- a/libserver-test/malikania/server/db/broken_character.hpp	Thu Oct 05 08:38:47 2017 +0200
+++ b/libserver-test/malikania/server/db/broken_character.hpp	Fri Oct 06 16:30:24 2017 +0200
@@ -52,7 +52,7 @@
     /**
      * Called by account::add_character.
      */
-    void do_save(std::int64_t account_id) override;
+    void do_save(std::uint64_t account_id) override;
 
     /**
      * Called by account::remove_character.
--- a/libserver-test/malikania/server/db/test_account.hpp	Thu Oct 05 08:38:47 2017 +0200
+++ b/libserver-test/malikania/server/db/test_account.hpp	Fri Oct 06 16:30:24 2017 +0200
@@ -129,7 +129,6 @@
  */
 class test_account::test_dao : public account::dao {
 private:
-    std::int64_t sequence_{0};
     test_database& db_;
 
 public:
--- a/libserver-test/malikania/server/db/test_character.cpp	Thu Oct 05 08:38:47 2017 +0200
+++ b/libserver-test/malikania/server/db/test_character.cpp	Fri Oct 06 16:30:24 2017 +0200
@@ -24,7 +24,7 @@
 
 using test_dao = test_character::test_dao;
 
-void test_character::do_save(std::int64_t account_id)
+void test_character::do_save(std::uint64_t account_id)
 {
     id_ = db_.next_character_id();
     account_id_ = account_id;
--- a/libserver-test/malikania/server/db/test_character.hpp	Thu Oct 05 08:38:47 2017 +0200
+++ b/libserver-test/malikania/server/db/test_character.hpp	Fri Oct 06 16:30:24 2017 +0200
@@ -51,7 +51,7 @@
     /**
      * \copydoc character::do_save
      */
-    void do_save(std::int64_t account_id) override;
+    void do_save(std::uint64_t account_id) override;
 
     /**
      * \copydoc character::do_remove
--- a/libserver-test/malikania/server/db/test_database.hpp	Thu Oct 05 08:38:47 2017 +0200
+++ b/libserver-test/malikania/server/db/test_database.hpp	Fri Oct 06 16:30:24 2017 +0200
@@ -59,7 +59,7 @@
      *
      * \return the next id
      */
-    inline std::int64_t next_account_id() noexcept
+    inline std::uint64_t next_account_id() noexcept
     {
         return seq_account_id_ ++;
     }
@@ -69,7 +69,7 @@
      *
      * \return the next character id
      */
-    inline std::int64_t next_character_id() noexcept
+    inline std::uint64_t next_character_id() noexcept
     {
         return seq_character_id_ ++;
     }
@@ -144,7 +144,7 @@
      *
      * \return the id
      */
-    inline std::string sid(std::int64_t id) const noexcept
+    inline std::string sid(std::uint64_t id) const noexcept
     {
         return std::to_string(id);
     }
--- a/libserver-test/malikania/server/db/test_spell.cpp	Thu Oct 05 08:38:47 2017 +0200
+++ b/libserver-test/malikania/server/db/test_spell.cpp	Fri Oct 06 16:30:24 2017 +0200
@@ -6,9 +6,8 @@
 
 using test_dao = test_spell::test_dao;
 
-void test_spell::do_save(std::int64_t character_id)
+void test_spell::do_save(std::uint64_t character_id)
 {
-    id_ = db_.test_spell_dao().next_id();
     character_id_ = character_id;
 }
 
@@ -38,7 +37,7 @@
     level_ = json["level"];
 }
 
-spell_list test_dao::spells_for_charater(std::int64_t character_id)
+spell_list test_dao::spells_for_charater(std::uint64_t character_id)
 {
 #if 0
     spell_list list;
@@ -61,7 +60,7 @@
     return list;
 }
 
-void test_dao::remove_all(std::int64_t character_id)
+void test_dao::remove_all(std::uint64_t character_id)
 {
 #if 0
     for (auto it = spells_.begin(); it != spells_.end(); ) {
--- a/libserver-test/malikania/server/db/test_spell.hpp	Thu Oct 05 08:38:47 2017 +0200
+++ b/libserver-test/malikania/server/db/test_spell.hpp	Fri Oct 06 16:30:24 2017 +0200
@@ -47,7 +47,7 @@
     test_database& db_;
 
 protected:
-    void do_save(std::int64_t character_id) override;
+    void do_save(std::uint64_t character_id) override;
     void do_remove() override;
     void do_set_level(std::uint8_t level) override;
 
@@ -76,7 +76,6 @@
 
 class test_spell::test_dao {
 private:
-    std::int64_t sequence_{0};
     test_database& db_;
 
 public:
@@ -86,26 +85,16 @@
     }
 
     /**
-     * Compute the next id from the internal sequence.
-     *
-     * \return the next id
-     */
-    inline std::int64_t next_id() noexcept
-    {
-        return ++sequence_;
-    }
-
-    /**
      * Get the set of spells for the given character.
      */
-    spell_list spells_for_charater(std::int64_t character_id);
+    spell_list spells_for_charater(std::uint64_t character_id);
 
     /**
      * Remove all characters for the specified character.
      *
      * \param character_id the character id
      */
-    void remove_all(std::int64_t character_id);
+    void remove_all(std::uint64_t character_id);
 };
 
 } // !server
--- a/libserver/malikania/server/db/character.hpp	Thu Oct 05 08:38:47 2017 +0200
+++ b/libserver/malikania/server/db/character.hpp	Fri Oct 06 16:30:24 2017 +0200
@@ -41,7 +41,7 @@
     friend class account;
 
 protected:
-    std::int64_t account_id_{-1};       //!< parent acocunt
+    std::uint64_t account_id_{0U};      //!< parent account
     std::string nickname_;              //!< nickname (non null)
     std::string classname_;             //!< class type to instanciate
     std::uint16_t level_{1};            //!< character level
@@ -59,7 +59,7 @@
      * \note called by account::add_character.
      * \throw std::exception if the operation could not succeed
      */
-    virtual void do_save(std::int64_t account_id) = 0;
+    virtual void do_save(std::uint64_t account_id) = 0;
 
     /**
      * Remove this character.
--- a/libserver/malikania/server/db/model.hpp	Thu Oct 05 08:38:47 2017 +0200
+++ b/libserver/malikania/server/db/model.hpp	Fri Oct 06 16:30:24 2017 +0200
@@ -43,7 +43,7 @@
     model& operator=(model&&) = delete;
 
 protected:
-    std::int64_t id_{-1};               //!< object id
+    std::uint64_t id_{0U};              //!< object id
 
 public:
     /**
@@ -61,7 +61,7 @@
      *
      * \return the id
      */
-    inline std::int64_t id() const noexcept
+    inline std::uint64_t id() const noexcept
     {
         return id_;
     }
@@ -73,7 +73,7 @@
      */
     inline bool is_draft() const noexcept
     {
-        return id_ == -1;
+        return id_ == 0U;
     }
 
     /**
@@ -83,7 +83,7 @@
      */
     inline bool is_published() const noexcept
     {
-        return id_ >= 0;
+        return id_ > 0U;
     }
 };
 
--- a/libserver/malikania/server/db/spell.hpp	Thu Oct 05 08:38:47 2017 +0200
+++ b/libserver/malikania/server/db/spell.hpp	Fri Oct 06 16:30:24 2017 +0200
@@ -45,7 +45,7 @@
     friend class character;
 
 protected:
-    std::int64_t character_id_{-1};     //!< parent character
+    std::uint64_t character_id_{0U};    //!< parent character
     std::string classname_;             //!< class type to instanciate
     std::uint8_t level_{1};             //!< spell level
 
@@ -59,7 +59,7 @@
      *
      * \note called by character::add_spell.
      */
-    virtual void do_save(std::int64_t character_id) = 0;
+    virtual void do_save(std::uint64_t character_id) = 0;
 
     /**
      * Remove this spell.