Browse Source

create database struct methods

CPP-GTK4
yggverse 2 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( @@ -14,26 +14,9 @@ Tab::Tab(
const Glib::RefPtr<Gio::SimpleAction> & ACTION__TAB_PAGE_NAVIGATION_UPDATE
) {
// Init database
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
);
DB::init(
this->db = db
);
// Init actions
action__refresh = ACTION__REFRESH;
@ -68,7 +51,7 @@ Tab::Tab( @@ -68,7 +51,7 @@ Tab::Tab(
int Tab::restore()
{
sqlite3_stmt* statement;
sqlite3_stmt* statement; // @TODO move to the DB model namespace
const int PREPARE_STATUS = ::sqlite3_prepare_v3(
db,
@ -109,51 +92,10 @@ int Tab::restore() @@ -109,51 +92,10 @@ int Tab::restore()
return PREPARE_STATUS;
}
void Tab::clean()
void Tab::clear() // @TODO menu action?
{
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
);
// @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
DB::clear(
db
);
close_all();
@ -164,46 +106,24 @@ void Tab::save() @@ -164,46 +106,24 @@ void Tab::save()
char * error; // @TODO
// Delete previous data
clean();
DB::clear(
db
);
// Save current tab session
for (int page_number = 0; page_number < get_n_pages(); page_number++)
{
const auto TAB_LABEL = get_tabLabel(
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
// Delegate save actions to child page component
get_tabPage(
page_number
)->save(
::sqlite3_last_insert_rowid(
db
DB::add(
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( @@ -349,7 +269,6 @@ Glib::ustring Tab::get_page_description(
)->get_description();
};
// Private helpers
tab::Label * Tab::get_tabLabel(
const int & PAGE_NUMBER
) {
@ -410,4 +329,120 @@ tab::Page * Tab::get_tabPage( @@ -410,4 +329,120 @@ tab::Page * Tab::get_tabPage(
}
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 @@ -18,41 +18,51 @@ namespace app::browser::main
class Tab : public Gtk::Notebook
{
// Database
sqlite3 * db;
public:
struct DB
{
enum APP_BROWSER_MAIN_TAB__SESSION
struct DB
{
ID,
TIME,
PAGE_NUMBER,
IS_CURRENT,
LABEL_TEXT
enum APP_BROWSER_MAIN_TAB__SESSION
{
ID,
TIME,
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
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;
private:
// Components
tab::Label * get_tabLabel(
const int & PAGE_NUMBER
);
// Database
sqlite3 * db;
tab::Page * get_tabPage(
const int & PAGE_NUMBER
);
// Actions
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
const bool REORDERABLE = true;
const bool SCROLLABLE = true;
// Defaults
const bool REORDERABLE = true;
const bool SCROLLABLE = true;
public:
@ -99,7 +109,7 @@ namespace app::browser::main @@ -99,7 +109,7 @@ namespace app::browser::main
int restore();
void clean();
void clear();
void save();
@ -111,6 +121,14 @@ namespace app::browser::main @@ -111,6 +121,14 @@ namespace app::browser::main
Glib::ustring get_page_description(
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( @@ -24,25 +24,8 @@ Page::Page(
action__refresh = ACTION__REFRESH;
// Init database
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
DB::init(
this->db = db
);
// Init components
@ -89,44 +72,16 @@ void Page::refresh() @@ -89,44 +72,16 @@ void Page::refresh()
int Page::save(
const sqlite3_int64 & DB__APP_BROWSER_MAIN_TAB__SESSION_ID
) {
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",
// Delegate save action to child components
return pageNavigation->save(
Page::DB::add(
db,
DB__APP_BROWSER_MAIN_TAB__SESSION_ID,
mime,
title,
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(
@ -406,4 +361,123 @@ void Page::set_navbar_request_text( @@ -406,4 +361,123 @@ void Page::set_navbar_request_text(
pageNavigation->set_request_text(
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 @@ -35,6 +35,35 @@ namespace app::browser::main::tab
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:
// Meta
@ -49,18 +78,6 @@ namespace app::browser::main::tab @@ -49,18 +78,6 @@ namespace app::browser::main::tab
// Database
sqlite3 * db;
struct DB
{
enum APP_BROWSER_MAIN_TAB_PAGE__SESSION
{
ID,
TIME,
MIME,
TITLE,
DESCRIPTION
};
};
// Socket
char buffer[0xfffff]; // 1Mb

Loading…
Cancel
Save