database: pagedao: Add-support for subpages

This commit is contained in:
Albert S. 2023-11-23 18:24:40 +01:00
parent f08e235d03
commit 2d5d483790
4 changed files with 31 additions and 10 deletions

View File

@ -23,6 +23,8 @@ class PageDao
virtual void setCategories(std::string pagename, const std::vector<std::string> &catnames) = 0; virtual void setCategories(std::string pagename, const std::vector<std::string> &catnames) = 0;
virtual std::vector<SearchResult> search(std::string query, QueryOption option) = 0; virtual std::vector<SearchResult> search(std::string query, QueryOption option) = 0;
virtual std::vector<std::string> getChildren(std::string pagename) = 0;
virtual ~PageDao() virtual ~PageDao()
{ {
} }

View File

@ -57,8 +57,10 @@ std::optional<Page> PageDaoSqlite::findByTitle(std::string title)
Page result; Page result;
try try
{ {
auto ps = *db << "SELECT id, name, title, lastrevision, visible FROM page WHERE title = ?"; auto ps = *db << "SELECT id, name, title, lastrevision, visible, (SELECT name FROM page WHERE id = parent) "
ps << title >> std::tie(result.pageid, result.name, result.title, result.current_revision, result.listed); "FROM page WHERE title = ?";
ps << title >> std::tie(result.pageid, result.name, result.title, result.current_revision, result.listed,
result.parentpage);
} }
catch(const sqlite::errors::no_rows &e) catch(const sqlite::errors::no_rows &e)
{ {
@ -78,9 +80,10 @@ std::optional<Page> PageDaoSqlite::find(unsigned int id)
result.pageid = id; result.pageid = id;
try try
{ {
auto ps = *db << "SELECT name, title, lastrevision, visible FROM page WHERE id = ?"; auto ps = *db << "SELECT name, title, lastrevision, visible, (SELECT name FROM page WHERE id = parent) FROM "
"page WHERE id = ?";
ps << id >> std::tie(result.name, result.title, result.current_revision, result.listed); ps << id >> std::tie(result.name, result.title, result.current_revision, result.listed, result.parentpage);
} }
catch(const sqlite::errors::no_rows &e) catch(const sqlite::errors::no_rows &e)
{ {
@ -118,10 +121,12 @@ void PageDaoSqlite::save(const Page &page)
{ {
try try
{ {
*db << "INSERT OR REPLACE INTO page (id, name, title, lastrevision, visible) VALUES((SELECT id FROM page WHERE " *db << "INSERT OR REPLACE INTO page (id, name, title, lastrevision, visible, parent) VALUES((SELECT id FROM "
"page WHERE "
"name = " "name = "
"? OR id = ?), ?, ?, ?, ?)" "? OR id = ?), ?, ?, ?, ?, (SELECT id FROM page WHERE name = ?))"
<< page.name << page.pageid << page.name << page.title << page.current_revision << page.listed; << page.name << page.pageid << page.name << page.title << page.current_revision << page.listed
<< page.parentpage;
} }
catch(sqlite::sqlite_exception &e) catch(sqlite::sqlite_exception &e)
{ {
@ -140,9 +145,11 @@ std::vector<Page> PageDaoSqlite::getPageList(QueryOption option)
.setVisibleColumnName("visible") .setVisibleColumnName("visible")
.setPrependWhere(true) .setPrependWhere(true)
.build(); .build();
std::string query = "SELECT id, name, title, lastrevision, visible FROM page " + queryOption; std::string query =
*db << query >> "SELECT id, name, title, lastrevision, visible, (SELECT name FROM page WHERE id = parent) FROM page " +
[&](unsigned int pageid, std::string name, std::string title, unsigned int current_revision, bool visible) queryOption;
*db << query >> [&](unsigned int pageid, std::string name, std::string title, unsigned int current_revision,
bool visible, std::string parent)
{ {
Page tmp; Page tmp;
tmp.pageid = pageid; tmp.pageid = pageid;
@ -150,6 +157,7 @@ std::vector<Page> PageDaoSqlite::getPageList(QueryOption option)
tmp.title = title; tmp.title = title;
tmp.current_revision = current_revision; tmp.current_revision = current_revision;
tmp.listed = visible; tmp.listed = visible;
tmp.parentpage = parent;
result.push_back(tmp); result.push_back(tmp);
}; };
} }
@ -262,3 +270,11 @@ int PageDaoSqlite::fetchPageId(std::string pagename)
auto binder = *db << "SELECT id FROM page WHERE name = ?" << pagename; auto binder = *db << "SELECT id FROM page WHERE name = ?" << pagename;
return execInt(binder); return execInt(binder);
} }
std::vector<std::string> PageDaoSqlite::getChildren(std::string pagename)
{
std::vector<std::string> result;
auto query = *db << "SELECT name FROM page WHERE parent = (SELECT id FROM page WHERE name = ?)" << pagename;
query >> [&](std::string page) { result.push_back(page); };
return result;
}

View File

@ -28,6 +28,8 @@ class PageDaoSqlite : public PageDao, protected SqliteDao
int fetchPageId(std::string pagename); int fetchPageId(std::string pagename);
std::vector<SearchResult> search(std::string query, QueryOption option) override; std::vector<SearchResult> search(std::string query, QueryOption option) override;
void setCategories(std::string pagename, const std::vector<std::string> &catnames) override; void setCategories(std::string pagename, const std::vector<std::string> &catnames) override;
std::vector<std::string> getChildren(std::string pagename) override;
}; };
#endif // PAGEDAOSQLITE_H #endif // PAGEDAOSQLITE_H

1
page.h
View File

@ -8,6 +8,7 @@ class Page
Page(); Page();
std::string name; std::string name;
std::string title; std::string title;
std::string parentpage;
bool listed; bool listed;
unsigned int current_revision; unsigned int current_revision;
unsigned int pageid; unsigned int pageid;