Browse Source

create database struct methods

CPP-GTK4
yggverse 3 months ago
parent
commit
ae85b3bc71
  1. 233
      src/app/browser/main/tab.cpp
  2. 76
      src/app/browser/main/tab.hpp
  3. 176
      src/app/browser/main/tab/page.cpp
  4. 41
      src/app/browser/main/tab/page.hpp

233
src/app/browser/main/tab.cpp

@ -14,26 +14,9 @@ Tab::Tab(
const Glib::RefPtr<Gio::SimpleAction> & ACTION__TAB_PAGE_NAVIGATION_UPDATE const Glib::RefPtr<Gio::SimpleAction> & ACTION__TAB_PAGE_NAVIGATION_UPDATE
) { ) {
// Init database // Init database
this->db = db; DB::init(
this->db = db
char * error; );
::sqlite3_exec(
db,
R"SQL(
CREATE TABLE IF NOT EXISTS `app_browser_main_tab__session`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`time` INTEGER NOT NULL,
`page_number` INTEGER NOT NULL,
`is_current` INTEGER NOT NULL,
`label_text` VARCHAR(1024)
)
)SQL",
nullptr,
nullptr,
&error
);
// Init actions // Init actions
action__refresh = ACTION__REFRESH; action__refresh = ACTION__REFRESH;
@ -68,7 +51,7 @@ Tab::Tab(
int Tab::restore() int Tab::restore()
{ {
sqlite3_stmt* statement; sqlite3_stmt* statement; // @TODO move to the DB model namespace
const int PREPARE_STATUS = ::sqlite3_prepare_v3( const int PREPARE_STATUS = ::sqlite3_prepare_v3(
db, db,
@ -109,51 +92,10 @@ int Tab::restore()
return PREPARE_STATUS; return PREPARE_STATUS;
} }
void Tab::clean() void Tab::clear() // @TODO menu action?
{ {
char * error; // @TODO DB::clear(
sqlite3_stmt * statement; db
const int PREPARE_STATUS = ::sqlite3_prepare_v3(
db,
R"SQL(
SELECT * FROM `app_browser_main_tab__session`
)SQL",
-1,
SQLITE_PREPARE_NORMALIZE,
&statement,
nullptr
);
if (PREPARE_STATUS == SQLITE_OK)
{
while (::sqlite3_step(statement) == SQLITE_ROW)
{
const int APP_BROWSER_MAIN_TAB__SESSION_ID = ::sqlite3_column_int(
statement,
DB::APP_BROWSER_MAIN_TAB__SESSION::ID
);
// @TODO Delegate cleanup to the child components
// Delete record
::sqlite3_exec(
db,
Glib::ustring::sprintf(
R"SQL(
DELETE FROM `app_browser_main_tab__session` WHERE `id` = %d
)SQL",
APP_BROWSER_MAIN_TAB__SESSION_ID
).c_str(),
nullptr,
nullptr,
&error
);
}
}
::sqlite3_finalize(
statement
); );
close_all(); close_all();
@ -164,46 +106,24 @@ void Tab::save()
char * error; // @TODO char * error; // @TODO
// Delete previous data // Delete previous data
clean(); DB::clear(
db
);
// Save current tab session // Save current tab session
for (int page_number = 0; page_number < get_n_pages(); page_number++) for (int page_number = 0; page_number < get_n_pages(); page_number++)
{ {
const auto TAB_LABEL = get_tabLabel( // Delegate save actions to child page component
page_number
);
::sqlite3_exec(
db,
Glib::ustring::sprintf(
R"SQL(
INSERT INTO `app_browser_main_tab__session` (
`time`,
`page_number`,
`is_current`,
`label_text`
) VALUES (
CURRENT_TIMESTAMP,
'%d',
'%d',
'%s'
)
)SQL",
page_number,
page_number == get_current_page() ? 1 : 0,
TAB_LABEL->get_text()
).c_str(),
nullptr,
nullptr,
&error
);
// Delegate save action to the page component
get_tabPage( get_tabPage(
page_number page_number
)->save( )->save(
::sqlite3_last_insert_rowid( DB::add(
db db,
page_number,
page_number == get_current_page() ? 1 : 0,
get_tabLabel(
page_number
)->get_text()
) )
); );
} }
@ -349,7 +269,6 @@ Glib::ustring Tab::get_page_description(
)->get_description(); )->get_description();
}; };
// Private helpers
tab::Label * Tab::get_tabLabel( tab::Label * Tab::get_tabLabel(
const int & PAGE_NUMBER const int & PAGE_NUMBER
) { ) {
@ -410,4 +329,120 @@ tab::Page * Tab::get_tabPage(
} }
return TAB_PAGE; return TAB_PAGE;
}
// Database model
int Tab::DB::init(
sqlite3 * db
) {
char * error;
return ::sqlite3_exec(
db,
R"SQL(
CREATE TABLE IF NOT EXISTS `app_browser_main_tab__session`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`time` INTEGER NOT NULL,
`page_number` INTEGER NOT NULL,
`is_current` INTEGER NOT NULL,
`label_text` VARCHAR(1024)
)
)SQL",
nullptr,
nullptr,
&error
);
}
int Tab::DB::clear(
sqlite3 * db
) {
char * error; // @TODO
sqlite3_stmt * statement;
const int PREPARE_STATUS = ::sqlite3_prepare_v3(
db,
R"SQL(
SELECT * FROM `app_browser_main_tab__session`
)SQL",
-1,
SQLITE_PREPARE_NORMALIZE,
&statement,
nullptr
);
if (PREPARE_STATUS == SQLITE_OK)
{
while (::sqlite3_step(statement) == SQLITE_ROW)
{
const int APP_BROWSER_MAIN_TAB__SESSION_ID = ::sqlite3_column_int(
statement,
DB::APP_BROWSER_MAIN_TAB__SESSION::ID
);
// Delete record
::sqlite3_exec(
db,
Glib::ustring::sprintf(
R"SQL(
DELETE FROM `app_browser_main_tab__session` WHERE `id` = %d
)SQL",
APP_BROWSER_MAIN_TAB__SESSION_ID
).c_str(),
nullptr,
nullptr,
&error
);
// Delegate cleanup childs
tab::Page::DB::clear(
db,
APP_BROWSER_MAIN_TAB__SESSION_ID
);
}
}
return ::sqlite3_finalize(
statement
);
}
sqlite3_int64 Tab::DB::add(
sqlite3 * db,
const int & PAGE_NUMBER,
const bool & IS_CURRENT,
const Glib::ustring & LABEL_TEXT
) {
char * error; // @TODO
::sqlite3_exec(
db,
Glib::ustring::sprintf(
R"SQL(
INSERT INTO `app_browser_main_tab__session` (
`time`,
`page_number`,
`is_current`,
`label_text`
) VALUES (
CURRENT_TIMESTAMP,
'%d',
'%d',
'%s'
)
)SQL",
PAGE_NUMBER,
IS_CURRENT,
LABEL_TEXT
).c_str(),
nullptr,
nullptr,
&error
);
return ::sqlite3_last_insert_rowid(
db
);
} }

76
src/app/browser/main/tab.hpp

@ -18,41 +18,51 @@ namespace app::browser::main
class Tab : public Gtk::Notebook class Tab : public Gtk::Notebook
{ {
// Database public:
sqlite3 * db;
struct DB struct DB
{
enum APP_BROWSER_MAIN_TAB__SESSION
{ {
ID, enum APP_BROWSER_MAIN_TAB__SESSION
TIME, {
PAGE_NUMBER, ID,
IS_CURRENT, TIME,
LABEL_TEXT PAGE_NUMBER,
IS_CURRENT,
LABEL_TEXT
};
static int init(
sqlite3 * db
);
static int clear(
sqlite3 * db
);
static sqlite3_int64 add(
sqlite3 * db,
const int & PAGE_NUMBER,
const bool & IS_CURRENT,
const Glib::ustring & LABEL_TEXT
);
}; };
};
// Actions private:
Glib::RefPtr<Gio::SimpleAction> action__refresh,
action__tab_close_active,
action__tab_close_all,
action__tab_page_navigation_history_back,
action__tab_page_navigation_history_forward,
action__tab_page_navigation_update;
// Components // Database
tab::Label * get_tabLabel( sqlite3 * db;
const int & PAGE_NUMBER
);
tab::Page * get_tabPage( // Actions
const int & PAGE_NUMBER Glib::RefPtr<Gio::SimpleAction> action__refresh,
); action__tab_close_active,
action__tab_close_all,
action__tab_page_navigation_history_back,
action__tab_page_navigation_history_forward,
action__tab_page_navigation_update;
// Defaults // Defaults
const bool REORDERABLE = true; const bool REORDERABLE = true;
const bool SCROLLABLE = true; const bool SCROLLABLE = true;
public: public:
@ -99,7 +109,7 @@ namespace app::browser::main
int restore(); int restore();
void clean(); void clear();
void save(); void save();
@ -111,6 +121,14 @@ namespace app::browser::main
Glib::ustring get_page_description( Glib::ustring get_page_description(
const int & PAGE_NUMBER const int & PAGE_NUMBER
); );
tab::Label * get_tabLabel(
const int & PAGE_NUMBER
);
tab::Page * get_tabPage(
const int & PAGE_NUMBER
);
}; };
} }

176
src/app/browser/main/tab/page.cpp

@ -24,25 +24,8 @@ Page::Page(
action__refresh = ACTION__REFRESH; action__refresh = ACTION__REFRESH;
// Init database // Init database
this->db = db; DB::init(
this->db = db
char * error;
::sqlite3_exec(
db,
R"SQL(
CREATE TABLE IF NOT EXISTS `app_browser_main_tab_page__session`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `app_browser_main_tab__session_id` INTEGER NOT NULL,
`time` INTEGER NOT NULL,
`mime` INTEGER NOT NULL,
`title` VARCHAR(1024) NOT NULL,
`description` VARCHAR(1024) NOT NULL
)
)SQL",
nullptr,
nullptr,
&error
); );
// Init components // Init components
@ -89,44 +72,16 @@ void Page::refresh()
int Page::save( int Page::save(
const sqlite3_int64 & DB__APP_BROWSER_MAIN_TAB__SESSION_ID const sqlite3_int64 & DB__APP_BROWSER_MAIN_TAB__SESSION_ID
) { ) {
char * error; // @TODO // Delegate save action to child components
return pageNavigation->save(
::sqlite3_exec( Page::DB::add(
db, db,
Glib::ustring::sprintf(
R"SQL(
INSERT INTO `app_browser_main_tab_page__session` (
`app_browser_main_tab__session_id`,
`time`,
`mime`,
`title`,
`description`
) VALUES (
'%d',
CURRENT_TIMESTAMP,
'%d',
'%s',
'%s'
)
)SQL",
DB__APP_BROWSER_MAIN_TAB__SESSION_ID, DB__APP_BROWSER_MAIN_TAB__SESSION_ID,
mime, mime,
title, title,
description description
).c_str(),
nullptr,
nullptr,
&error
); // @TODO auto-clean old records somewhere
// Delegate save action to child components
pageNavigation->save(
::sqlite3_last_insert_rowid(
db
) )
); );
return 1; // @TODO SQL
} }
void Page::update( void Page::update(
@ -406,4 +361,123 @@ void Page::set_navbar_request_text(
pageNavigation->set_request_text( pageNavigation->set_request_text(
VALUE VALUE
); );
}
// Database model
int Page::DB::init(
sqlite3 * db
) {
char * error;
return ::sqlite3_exec(
db,
R"SQL(
CREATE TABLE IF NOT EXISTS `app_browser_main_tab_page__session`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `app_browser_main_tab__session_id` INTEGER NOT NULL,
`time` INTEGER NOT NULL,
`mime` INTEGER NOT NULL,
`title` VARCHAR(1024) NOT NULL,
`description` VARCHAR(1024) NOT NULL
)
)SQL",
nullptr,
nullptr,
&error
);
}
int Page::DB::clear(
sqlite3 * db,
const int & DB__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_page__session` WHERE `app_browser_main_tab__session_id` = %d
)SQL",
DB__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)
{
const int APP_BROWSER_MAIN_TAB_PAGE__SESSION_ID = ::sqlite3_column_int(
statement,
DB::APP_BROWSER_MAIN_TAB_PAGE__SESSION::ID
);
// @TODO Delegate cleanup to the child components
// Delete record
::sqlite3_exec(
db,
Glib::ustring::sprintf(
R"SQL(
DELETE FROM `app_browser_main_tab_page__session` WHERE `id` = %d
)SQL",
APP_BROWSER_MAIN_TAB_PAGE__SESSION_ID
).c_str(),
nullptr,
nullptr,
&error
);
}
}
return ::sqlite3_finalize(
statement
);
}
sqlite3_int64 Page::DB::add(
sqlite3 * db,
const sqlite3_int64 & DB__APP_BROWSER_MAIN_TAB__SESSION_ID,
const Page::MIME & MIME,
const Glib::ustring & TITLE,
const Glib::ustring & DESCRIPTION
) {
char * error; // @TODO
::sqlite3_exec(
db,
Glib::ustring::sprintf(
R"SQL(
INSERT INTO `app_browser_main_tab_page__session` (
`app_browser_main_tab__session_id`,
`time`,
`mime`,
`title`,
`description`
) VALUES (
'%d',
CURRENT_TIMESTAMP,
'%d',
'%s',
'%s'
)
)SQL",
DB__APP_BROWSER_MAIN_TAB__SESSION_ID,
MIME,
TITLE,
DESCRIPTION
).c_str(),
nullptr,
nullptr,
&error
);
return ::sqlite3_last_insert_rowid(
db
);
} }

41
src/app/browser/main/tab/page.hpp

@ -35,6 +35,35 @@ namespace app::browser::main::tab
UNDEFINED UNDEFINED
}; };
struct DB
{
enum APP_BROWSER_MAIN_TAB_PAGE__SESSION
{
ID,
TIME,
MIME,
TITLE,
DESCRIPTION
};
static int init(
sqlite3 * db
);
static int clear(
sqlite3 * db,
const int & DB__APP_BROWSER_MAIN_TAB__SESSION_ID
);
static sqlite3_int64 add(
sqlite3 * db,
const sqlite3_int64 & DB__APP_BROWSER_MAIN_TAB__SESSION_ID,
const Page::MIME & MIME,
const Glib::ustring & TITLE,
const Glib::ustring & DESCRIPTION
);
};
private: private:
// Meta // Meta
@ -49,18 +78,6 @@ namespace app::browser::main::tab
// Database // Database
sqlite3 * db; sqlite3 * db;
struct DB
{
enum APP_BROWSER_MAIN_TAB_PAGE__SESSION
{
ID,
TIME,
MIME,
TITLE,
DESCRIPTION
};
};
// Socket // Socket
char buffer[0xfffff]; // 1Mb char buffer[0xfffff]; // 1Mb

Loading…
Cancel
Save