summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouie S <louie@example.com>2024-04-01 21:34:20 -0400
committerLouie S <louie@example.com>2024-04-04 18:25:51 -0400
commita754fced75565f4c8d0f94f41b576415f3f9dcf0 (patch)
tree5889e0b1777b198b13805bda4df84a108a9b5836
parent50d93e4a110533be2c45efbd853e2257d9962978 (diff)
Keep static variables based on backend information to reduce number of database calls
-rw-r--r--src/assignmentList.cpp34
-rw-r--r--src/assignmentList.h1
-rw-r--r--src/backend/db_sqlite.cpp68
-rw-r--r--src/entry.cpp4
-rw-r--r--src/entry.h2
-rw-r--r--src/group.cpp4
-rw-r--r--src/group.h2
-rw-r--r--src/rule.cpp4
-rw-r--r--src/rule.h2
9 files changed, 108 insertions, 13 deletions
diff --git a/src/assignmentList.cpp b/src/assignmentList.cpp
index 136af04..8c1fab1 100644
--- a/src/assignmentList.cpp
+++ b/src/assignmentList.cpp
@@ -31,6 +31,9 @@ AssignmentList::AssignmentList() {
// ensure QSettings location exists
this->initializeSettings();
+ // load everything from database into static global variables
+ this->initializeGlobals();
+
// load uic
ui.setupUi(this);
this->initializeUI();
@@ -48,6 +51,15 @@ void AssignmentList::initializeSettings() {
settings.endGroup();
}
+// load from database into static QList variables
+void AssignmentList::initializeGlobals() {
+ BackendDB database;
+
+ Group::groups = database.loadGroups();
+ Entry::entries = database.loadEntries();
+ Rule::rules = database.loadRules();
+}
+
void AssignmentList::initializeUI() {
// create menu connections
QObject::connect(ui.actionPreferences, &QAction::triggered, this, &AssignmentList::preferences);
@@ -74,18 +86,17 @@ void AssignmentList::displayWidgets() {
QVBoxLayout *column_left = new QVBoxLayout();
QVBoxLayout *column_right = new QVBoxLayout();
BackendDB database;
- QList<Group> groups = database.loadGroups();
GroupLayout *new_group_layout;
int i;
// clear out old layouts if they exist
recursiveClear(ui.groups_layout);
- for(i = 0; i < groups.size(); ++i) {
- if(groups[i].hidden) continue;
- new_group_layout = new GroupLayout(groups[i]);
- new_group_layout->addLayout(this->drawEntries(groups[i].id)); // add entries to layout
- if(groups[i].column == Group::left) column_left->addLayout(new_group_layout);
+ for(i = 0; i < Group::groups.size(); ++i) {
+ if(Group::groups[i].hidden) continue;
+ new_group_layout = new GroupLayout(Group::groups[i]);
+ new_group_layout->addLayout(this->drawEntries(Group::groups[i].id)); // add entries to layout
+ if(Group::groups[i].column == Group::left) column_left->addLayout(new_group_layout);
else column_right->addLayout(new_group_layout);
}
@@ -98,7 +109,6 @@ void AssignmentList::displayWidgets() {
QVBoxLayout *AssignmentList::drawEntries(int parent_id) {
BackendDB database;
- QList<Entry> entries = database.loadEntries(parent_id);
QVBoxLayout *output = new QVBoxLayout;
int i;
@@ -106,12 +116,13 @@ QVBoxLayout *AssignmentList::drawEntries(int parent_id) {
output->setContentsMargins(5, 0, 0, 0);
// sort entries
- std::sort(entries.begin(), entries.end(), Entry::compare);
+ std::sort(Entry::entries.begin(), Entry::entries.end(), Entry::compare);
- for(i = 0; i < entries.size(); ++i) {
+ for(i = 0; i < Entry::entries.size(); ++i) {
+ if(Entry::entries[i].parent_id != parent_id) continue;
// skip if this entry is set to hidden
- if(entries[i].hidden) continue;
- output->addLayout(new EntryLayout(entries[i]));
+ if(Entry::entries[i].hidden) continue;
+ output->addLayout(new EntryLayout(Entry::entries[i]));
}
return output;
@@ -131,6 +142,7 @@ void AssignmentList::preferences() {
}
void AssignmentList::reload() {
+ this->initializeGlobals();
this->displayWidgets();
}
diff --git a/src/assignmentList.h b/src/assignmentList.h
index 8b028d7..4b792fa 100644
--- a/src/assignmentList.h
+++ b/src/assignmentList.h
@@ -14,6 +14,7 @@ class AssignmentList : public QMainWindow {
QSettings configuration;
AssignmentList();
+ void initializeGlobals();
void displayWidgets();
private:
diff --git a/src/backend/db_sqlite.cpp b/src/backend/db_sqlite.cpp
index f9b9a8e..847b706 100644
--- a/src/backend/db_sqlite.cpp
+++ b/src/backend/db_sqlite.cpp
@@ -126,7 +126,7 @@ QList<Entry> BackendDB::loadEntries(int parent_id) {
return output;
}
-// load entries
+// load rules
QList<Rule> BackendDB::loadRules() {
QList<Rule> output;
@@ -150,7 +150,7 @@ QList<Rule> BackendDB::loadRules() {
return output;
}
-// load entries
+// load rules
QList<Rule> BackendDB::loadRules(int entry_id) {
QList<Rule> output;
@@ -194,6 +194,9 @@ int BackendDB::insertGroup(const Group &new_group) {
}
QSqlDatabase::removeDatabase("qt_sql_default_connection");
+ // update static variable
+ if(output > 0)
+ Group::groups.append(Group(output, new_group.name, new_group.column, new_group.link));
return output;
}
@@ -219,6 +222,9 @@ int BackendDB::insertEntry(const Entry &new_entry) {
}
QSqlDatabase::removeDatabase("qt_sql_default_connection");
+ // update static variable
+ if(output > 0)
+ Entry::entries.append(Entry(output, new_entry.parent_id, new_entry.desc, new_entry.due, new_entry.due_alt, new_entry.link, new_entry.color, new_entry.highlight));
return output;
}
@@ -242,10 +248,15 @@ int BackendDB::insertRule(const Rule &new_rule) {
}
QSqlDatabase::removeDatabase("qt_sql_default_connection");
+ // update static variable
+ if(output > 0)
+ Rule::rules.append(Rule(output, new_rule.entry_id, new_rule.when, new_rule.date, new_rule.color, new_rule.highlight));
return output;
}
void BackendDB::updateGroup(const Group &group) {
+ int i;
+
{
QSqlDatabase database(this->openDB());
QSqlQuery query;
@@ -265,9 +276,18 @@ void BackendDB::updateGroup(const Group &group) {
}
QSqlDatabase::removeDatabase("qt_sql_default_connection");
+ // update static variable
+ for(i = 0; i < Group::groups.size(); ++i) {
+ if(Group::groups[i].id == group.id) {
+ Group::groups.replace(i, group);
+ break;
+ }
+ }
}
void BackendDB::updateEntry(const Entry &entry) {
+ int i;
+
{
QSqlDatabase database(this->openDB());
QSqlQuery query;
@@ -295,9 +315,18 @@ void BackendDB::updateEntry(const Entry &entry) {
}
QSqlDatabase::removeDatabase("qt_sql_default_connection");
+ // update static variable
+ for(i = 0; i < Entry::entries.size(); ++i) {
+ if(Entry::entries[i].id == entry.id) {
+ Entry::entries.replace(i, entry);
+ break;
+ }
+ }
}
void BackendDB::updateRule(const Rule &rule) {
+ int i;
+
{
QSqlDatabase database(this->openDB());
QSqlQuery query;
@@ -317,12 +346,20 @@ void BackendDB::updateRule(const Rule &rule) {
}
QSqlDatabase::removeDatabase("qt_sql_default_connection");
+ // update static variable
+ for(i = 0; i < Rule::rules.size(); ++i) {
+ if(Rule::rules[i].id == rule.id) {
+ Rule::rules.replace(i, rule);
+ break;
+ }
+ }
}
// hide group and entries belonging to group
// return value: number of affected rows
int BackendDB::removeGroup(const Group &group) {
int output;
+ int i;
{
QSqlDatabase database(this->openDB());
@@ -343,12 +380,24 @@ int BackendDB::removeGroup(const Group &group) {
}
QSqlDatabase::removeDatabase("qt_sql_default_connection");
+ // update static variable
+ for(i = 0; i < Entry::entries.size(); ++i) {
+ if(Entry::entries[i].parent_id == group.id) {
+ Entry::entries.removeAt(i);
+ }
+ }
+ for(i = 0; i < Group::groups.size(); ++i) {
+ if(Group::groups[i].id == group.id) {
+ Group::groups.removeAt(i);
+ }
+ }
return output;
}
// return value: number of affected rows
int BackendDB::removeEntry(const Entry &entry) {
int output;
+ int i;
{
QSqlDatabase database(this->openDB());
@@ -362,12 +411,20 @@ int BackendDB::removeEntry(const Entry &entry) {
}
QSqlDatabase::removeDatabase("qt_sql_default_connection");
+ // update static variable
+ for(i = 0; i < Entry::entries.size(); ++i) {
+ if(Entry::entries[i].id == entry.id) {
+ Entry::entries.removeAt(i);
+ break;
+ }
+ }
return output;
}
// return value: number of affected rows
int BackendDB::removeRule(const Rule &rule) {
int output;
+ int i;
{
QSqlDatabase database(this->openDB());
@@ -381,6 +438,13 @@ int BackendDB::removeRule(const Rule &rule) {
}
QSqlDatabase::removeDatabase("qt_sql_default_connection");
+ // update static variable
+ for(i = 0; i < Rule::rules.size(); ++i) {
+ if(Rule::rules[i].id == rule.id) {
+ Rule::rules.removeAt(i);
+ break;
+ }
+ }
return output;
}
diff --git a/src/entry.cpp b/src/entry.cpp
index 1082c1e..c95bbb0 100644
--- a/src/entry.cpp
+++ b/src/entry.cpp
@@ -1,3 +1,5 @@
+#include <QList>
+
#include "entry.h"
Entry::Entry(int id, int parent_id, QString desc, QDateTime due, QString due_alt, QUrl link, QString color, QString highlight, bool done, bool hidden) :
@@ -14,6 +16,8 @@ Entry::Entry(int id, int parent_id, QString desc, QDateTime due, QString due_alt
{
}
+QList<Entry> Entry::entries;
+
bool Entry::compare(Entry a, Entry b) {
// 1st level: sort not done before done
if(a.done != b.done)
diff --git a/src/entry.h b/src/entry.h
index 61450f7..294df7b 100644
--- a/src/entry.h
+++ b/src/entry.h
@@ -30,6 +30,8 @@ struct Entry {
bool hidden = false
);
+ static QList<Entry> entries;
+
// function for sorting algorithm
static bool compare(Entry a, Entry b);
};
diff --git a/src/group.cpp b/src/group.cpp
index 61ecf2a..ddb3b13 100644
--- a/src/group.cpp
+++ b/src/group.cpp
@@ -1,3 +1,5 @@
+#include <QList>
+
#include "group.h"
Group::Group(int id, QString name, Group::Column column, QString link, bool hidden) :
@@ -8,3 +10,5 @@ Group::Group(int id, QString name, Group::Column column, QString link, bool hidd
hidden(hidden)
{
}
+
+QList<Group> Group::groups;
diff --git a/src/group.h b/src/group.h
index fe9158f..9c7bfa4 100644
--- a/src/group.h
+++ b/src/group.h
@@ -18,6 +18,8 @@ struct Group {
QString link = "",
bool hidden = false
);
+
+ static QList<Group> groups;
};
#endif
diff --git a/src/rule.cpp b/src/rule.cpp
index d90a0db..85da13a 100644
--- a/src/rule.cpp
+++ b/src/rule.cpp
@@ -1,3 +1,5 @@
+#include <QList>
+
#include "rule.h"
Rule::Rule(int id, int entry_id, When when, QDateTime date, QString color, QString highlight) :
@@ -9,3 +11,5 @@ Rule::Rule(int id, int entry_id, When when, QDateTime date, QString color, QStri
highlight(highlight)
{
}
+
+QList<Rule> Rule::rules;
diff --git a/src/rule.h b/src/rule.h
index 1d7e90a..cf9abfe 100644
--- a/src/rule.h
+++ b/src/rule.h
@@ -21,6 +21,8 @@ struct Rule {
QString color = "",
QString highlight = ""
);
+
+ static QList<Rule> rules;
};
#endif