delegate db actions to label class

This commit is contained in:
yggverse 2024-09-11 05:16:37 +03:00
parent 6b6ae753bc
commit b67b9561fb
10 changed files with 291 additions and 59 deletions

View file

@ -3,8 +3,14 @@
using namespace app::browser::main::tab;
Label::Label(
sqlite3 * db,
const Glib::RefPtr<Gio::SimpleAction> & ACTION__CLOSE_ACTIVE
) {
// Init database
DB::SESSION::init(
this->db = db
);
// Init actions
action__close_active = ACTION__CLOSE_ACTIVE;
@ -29,4 +35,173 @@ Label::Label(
add_controller(
GtkGestureClick
);
}
// Actions
int Label::restore(
const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID
) {
sqlite3_stmt * statement;
const int PREPARE_STATUS = sqlite3_prepare_v3(
db,
Glib::ustring::sprintf(
R"SQL(
SELECT * FROM `app_browser_main_tab_label__session`
WHERE `app_browser_main_tab__session__id` = %d
ORDER BY `id` DESC LIMIT 1
)SQL",
APP_BROWSER_MAIN_TAB__SESSION__ID
).c_str(),
-1,
SQLITE_PREPARE_NORMALIZE,
&statement,
nullptr
);
if (PREPARE_STATUS == SQLITE_OK)
{
// Use latest record as order
while (sqlite3_step(statement) == SQLITE_ROW)
{
// Restore widget data
set_text(
reinterpret_cast<const char*>(
sqlite3_column_text(
statement,
DB::SESSION::TEXT
)
)
);
// Restore children components here (on available)
}
}
return sqlite3_finalize(
statement
);
}
int Label::save(
const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID
) {
// Delegate save action to child components (on available)
// Save label session
return DB::SESSION::add(
db,
APP_BROWSER_MAIN_TAB__SESSION__ID,
get_text()
);
}
// Database model
int Label::DB::SESSION::init(
sqlite3 * db
) {
char * error;
return sqlite3_exec(
db,
R"SQL(
CREATE TABLE IF NOT EXISTS `app_browser_main_tab_label__session`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `app_browser_main_tab__session__id` INTEGER NOT NULL,
`time` INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP,
`text` VARCHAR (1024) NOT NULL
)
)SQL",
nullptr,
nullptr,
&error
);
}
int Label::DB::SESSION::clean(
sqlite3 * db,
const sqlite3_int64 & APP_BROWSER_MAIN_TAB__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_tab_label__session`
WHERE `app_browser_main_tab__session__id` = %d
)SQL",
APP_BROWSER_MAIN_TAB__SESSION__ID
).c_str(),
-1,
SQLITE_PREPARE_NORMALIZE,
&statement,
nullptr
);
if (PREPARE_STATUS == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
// Delete record
const int EXEC_STATUS = sqlite3_exec(
db,
Glib::ustring::sprintf(
R"SQL(
DELETE FROM `app_browser_main_tab_label__session` WHERE `id` = %d
)SQL",
sqlite3_column_int64(
statement,
DB::SESSION::ID
)
).c_str(),
nullptr,
nullptr,
&error
);
// Delegate children dependencies cleanup
if (EXEC_STATUS == SQLITE_OK)
{
// nothing here.
}
}
}
return sqlite3_finalize(
statement
);
}
sqlite3_int64 Label::DB::SESSION::add(
sqlite3 * db,
const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID,
const Glib::ustring & TEXT
) {
char * error; // @TODO
sqlite3_exec(
db,
Glib::ustring::sprintf(
R"SQL(
INSERT INTO `app_browser_main_tab_label__session` (
`app_browser_main_tab__session__id`,
`text`
) VALUES (
'%d',
'%s'
)
)SQL",
APP_BROWSER_MAIN_TAB__SESSION__ID,
TEXT
).c_str(),
nullptr,
nullptr,
&error
);
return sqlite3_last_insert_rowid(
db
);
}

View file

@ -7,18 +7,78 @@
#include <glibmm/ustring.h>
#include <gtkmm/gestureclick.h>
#include <gtkmm/label.h>
#include <sqlite3.h>
namespace app::browser::main::tab
{
class Label : public Gtk::Label
{
Glib::RefPtr<Gio::SimpleAction> action__close_active;
public:
/*
* Class database
*
* Allowed parental access to enums and relationship methods
*/
struct DB
{
// APP_BROWSER_MAIN_TAB_LABEL__*
struct SESSION
{
enum
{
ID,
APP_BROWSER_MAIN_TAB__SESSION__ID,
TIME,
TEXT
}; // table fields index
static int init(
sqlite3 * db
); // return sqlite3_exec status code
static int clean(
sqlite3 * db,
const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID
); // return sqlite3_finalize status code
static sqlite3_int64 add(
sqlite3 * db,
const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID,
const Glib::ustring & TEXT
); // return sqlite3_last_insert_rowid
};
};
/*
* Internal members
*/
private:
// Database
sqlite3 * db;
// Actions
Glib::RefPtr<Gio::SimpleAction> action__close_active;
/*
* Class API
*/
public:
Label(
sqlite3 * db,
const Glib::RefPtr<Gio::SimpleAction> & ACTION__CLOSE_ACTIVE
);
// Actions
int restore(
const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID
); // return sqlite3_finalize status code
int save(
const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID
); // return sqlite3_finalize status code
};
}

View file

@ -63,13 +63,6 @@ Page::Page(
}
// Actions
void Page::update()
{
pageNavigation->update(
progress_fraction
);
}
int Page::restore(
const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID
) {
@ -106,11 +99,9 @@ int Page::restore(
}
}
sqlite3_finalize(
return sqlite3_finalize(
statement
);
return PREPARE_STATUS;
}
int Page::save(
@ -140,6 +131,11 @@ void Page::update(
description = DESCRIPTION;
progress_fraction = PROGRESS_FRACTION;
// Refresh children components
pageNavigation->update(
progress_fraction
);
// Refresh parent window
action__refresh->activate();
}

View file

@ -118,11 +118,9 @@ namespace app::browser::main::tab
);
// Actions
void update();
int restore(
const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID
);
); // return sqlite3_finalize status code
int save(
const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID

View file

@ -144,11 +144,9 @@ int Navigation::restore(
}
}
sqlite3_finalize(
return sqlite3_finalize(
statement
);
return PREPARE_STATUS;
}
int Navigation::save(

View file

@ -95,7 +95,7 @@ namespace app::browser::main::tab::page
int restore(
const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID
);
); // return sqlite3_finalize status code
int save(
const sqlite3_int64 & APP_BROWSER_MAIN_TAB__SESSION__ID

View file

@ -124,11 +124,9 @@ int Request::restore(
}
}
sqlite3_finalize(
return sqlite3_finalize(
statement
);
return PREPARE_STATUS;
}
int Request::save(

View file

@ -98,7 +98,7 @@ namespace app::browser::main::tab::page::navigation
int restore(
const sqlite3_int64 & APP_BROWSER_MAIN_TAB_PAGE_NAVIGATION__SESSION__ID
);
); // return sqlite3_finalize status code
int save(
const sqlite3_int64 & APP_BROWSER_MAIN_TAB_PAGE_NAVIGATION__SESSION__ID