implement browser, main widget db session

This commit is contained in:
yggverse 2024-09-12 23:28:58 +03:00
parent c240c9faee
commit c13cc44a59
6 changed files with 552 additions and 62 deletions

View file

@ -12,6 +12,11 @@ Main::Main(
const Glib::RefPtr<Gio::SimpleAction> & ACTION__MAIN_TAB_PAGE_NAVIGATION_HISTORY_FORWARD,
const Glib::RefPtr<Gio::SimpleAction> & ACTION__MAIN_TAB_PAGE_NAVIGATION_RELOAD
) {
// Init database
DB::SESSION::init(
this->db = db
);
// Init widget
set_orientation(
Gtk::Orientation::VERTICAL
@ -40,19 +45,73 @@ Main::Main(
// Actions
/// Session
void Main::clean()
{
mainTab->clean();
void Main::clean(
const sqlite3_int64 & APP_BROWSER__SESSION__ID
) {
DB::SESSION::clean(
db,
APP_BROWSER__SESSION__ID
);
mainTab->close_all();
};
void Main::restore()
{
mainTab->restore();
int Main::restore(
const sqlite3_int64 & APP_BROWSER__SESSION__ID
) {
sqlite3_stmt* statement; // @TODO move to the DB model namespace
const int PREPARE_STATUS = sqlite3_prepare_v3(
db,
Glib::ustring::sprintf(
R"SQL(
SELECT * FROM `app_browser_main__session`
WHERE `app_browser__session_id` = %d
)SQL",
APP_BROWSER__SESSION__ID
).c_str(),
-1,
SQLITE_PREPARE_NORMALIZE,
&statement,
nullptr
);
if (PREPARE_STATUS == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
mainTab->restore(
DB::SESSION::ID
);
}
}
return sqlite3_finalize(
statement
);
};
void Main::save()
{
mainTab->save();
void Main::save(
const sqlite3_int64 & APP_BROWSER__SESSION__ID
) {
char * error; // @TODO
// Delete previous data
DB::SESSION::clean(
db,
APP_BROWSER__SESSION__ID
); // @TODO run on background
// Create new session
const sqlite3_int64 APP_BROWSER_MAIN__SESSION__ID = DB::SESSION::add(
db,
APP_BROWSER__SESSION__ID
);
// Delegate save actions to children components
mainTab->save(
APP_BROWSER_MAIN__SESSION__ID
);
};
/// Tab actions
@ -130,4 +189,116 @@ Glib::ustring Main::get_tab_page_description()
return mainTab->get_page_description(
mainTab->get_current_page()
);
};
};
// Database
int Main::DB::SESSION::init(
sqlite3 * db
) {
char * error;
return sqlite3_exec(
db,
R"SQL(
CREATE TABLE IF NOT EXISTS `app_browser_main__session`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`time` INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP,
`app_browser__session__id` INTEGER NOT NULL
)
)SQL",
nullptr,
nullptr,
&error
);
}
int Main::DB::SESSION::clean(
sqlite3 * db,
const sqlite3_int64 & APP_BROWSER__SESSION__ID
) {
char * error; // @TODO
sqlite3_stmt * statement;
const int PREPARE_STATUS = sqlite3_prepare_v3(
db,
Glib::ustring::sprintf(
R"SQL(
SELECT * FROM `app_browser_main__session`
WHERE `app_browser__session__id` = %d
)SQL",
APP_BROWSER__SESSION__ID
).c_str(),
-1,
SQLITE_PREPARE_NORMALIZE,
&statement,
nullptr
);
if (PREPARE_STATUS == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
const sqlite3_int64 APP_BROWSER_MAIN__SESSION__ID = sqlite3_column_int64(
statement,
DB::SESSION::ID
);
// Delete record
const int EXEC_STATUS = sqlite3_exec(
db,
Glib::ustring::sprintf(
R"SQL(
DELETE FROM `app_browser_main__session` WHERE `id` = %d
)SQL",
APP_BROWSER_MAIN__SESSION__ID
).c_str(),
nullptr,
nullptr,
&error
);
// Delegate children dependencies cleanup
if (EXEC_STATUS == SQLITE_OK)
{
main::Tab::DB::SESSION::clean(
db,
APP_BROWSER_MAIN__SESSION__ID
);
}
}
}
return sqlite3_finalize(
statement
);
}
sqlite3_int64 Main::DB::SESSION::add(
sqlite3 * db,
const sqlite3_int64 & APP_BROWSER__SESSION__ID
) {
char * error; // @TODO
sqlite3_exec(
db,
Glib::ustring::sprintf(
R"SQL(
INSERT INTO `app_browser_main__session` (
`app_browser_main__session__id`
) VALUES (
%d
)
)SQL",
APP_BROWSER__SESSION__ID
).c_str(),
nullptr,
nullptr,
&error
);
return sqlite3_last_insert_rowid(
db
);
}