Page: add id field

This commit is contained in:
Albert S. 2018-11-04 19:31:58 +01:00
parent b6cdf9a088
commit 099f6533f8
4 changed files with 11 additions and 8 deletions

View File

@ -11,9 +11,9 @@ class PageDao
public: public:
PageDao(); PageDao();
virtual bool exists(std::string page) const = 0; virtual bool exists(std::string page) const = 0;
virtual bool exists(int id) const = 0; virtual bool exists(unsigned int id) const = 0;
virtual std::optional<Page> find(std::string name) = 0; virtual std::optional<Page> find(std::string name) = 0;
virtual std::optional<Page> find(int id) = 0; virtual std::optional<Page> find(unsigned int id) = 0;
virtual std::vector<std::string> getPageList(QueryOption option) = 0; virtual std::vector<std::string> getPageList(QueryOption option) = 0;
virtual std::vector<std::string> fetchCategories(std::string pagename, QueryOption option) = 0; virtual std::vector<std::string> fetchCategories(std::string pagename, QueryOption option) = 0;
virtual void deletePage(std::string page) = 0; virtual void deletePage(std::string page) = 0;

View File

@ -26,7 +26,7 @@ SOFTWARE.
/* TODO: copied from C version mostly, review whether access to table other than page is ok */ /* TODO: copied from C version mostly, review whether access to table other than page is ok */
bool PageDaoSqlite::exists(int id) const bool PageDaoSqlite::exists(unsigned int id) const
{ {
auto binder = *db << "SELECT 1 from page WHERE id = ?" << id; auto binder = *db << "SELECT 1 from page WHERE id = ?" << id;
return execBool(binder); return execBool(binder);
@ -44,9 +44,10 @@ std::optional<Page> PageDaoSqlite::find(std::string name)
return find(pageid); return find(pageid);
} }
std::optional<Page> PageDaoSqlite::find(int id) std::optional<Page> PageDaoSqlite::find(unsigned int id)
{ {
Page result; Page result;
result.pageid = id;
try try
{ {
auto ps = *db << "SELECT name, lastrevision, visible FROM page WHERE id = ?"; auto ps = *db << "SELECT name, lastrevision, visible FROM page WHERE id = ?";
@ -88,8 +89,9 @@ void PageDaoSqlite::save(const Page &page)
{ {
try try
{ {
*db << "INSERT INTO page (name, lastrevision, visible) VALUES(?, ?, ?)" << page.name << page.current_revision *db << "INSERT OR REPLACE INTO page (id, name, lastrevision, visible) VALUES((SELECT id FROM page WHERE name = "
<< page.listed; "? OR id = ?), ?, ?, ?)"
<< page.name << page.pageid << page.name << page.current_revision << page.listed;
} }
catch(sqlite::sqlite_exception &e) catch(sqlite::sqlite_exception &e)
{ {

View File

@ -13,11 +13,11 @@ class PageDaoSqlite : public PageDao, protected SqliteDao
{ {
} }
void deletePage(std::string page) override; void deletePage(std::string page) override;
bool exists(int id) const override; bool exists(unsigned int id) const override;
bool exists(std::string name) const override; bool exists(std::string name) const override;
void save(const Page &page) override; void save(const Page &page) override;
std::optional<Page> find(std::string name) override; std::optional<Page> find(std::string name) override;
std::optional<Page> find(int id) override; std::optional<Page> find(unsigned int id) override;
std::vector<std::string> getPageList(QueryOption option) override; std::vector<std::string> getPageList(QueryOption option) override;
std::vector<std::string> fetchCategories(std::string pagename, QueryOption option) override; std::vector<std::string> fetchCategories(std::string pagename, QueryOption option) override;
using SqliteDao::SqliteDao; using SqliteDao::SqliteDao;

1
page.h
View File

@ -9,6 +9,7 @@ class Page
std::string name; std::string name;
bool listed; bool listed;
unsigned int current_revision; unsigned int current_revision;
unsigned int pageid;
}; };
#endif // PAGE_H #endif // PAGE_H