summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorlouie <lshprung@tutanota.com>2023-07-03 19:24:19 -0700
committerlouie <lshprung@tutanota.com>2023-07-03 19:24:19 -0700
commit89906af1a08aee8fd9f49fff30a655240d041c30 (patch)
tree0f4f18e11a5f6be4d04493a6633b8bfc90b82882 /src
parentee6714b329daaf802ab736faae40a471fccc2364 (diff)
Write load_entries implementation
Diffstat (limited to 'src')
-rw-r--r--src/db/sqlite3/db.c50
-rw-r--r--src/main.c6
2 files changed, 47 insertions, 9 deletions
diff --git a/src/db/sqlite3/db.c b/src/db/sqlite3/db.c
index e701140..91ac0eb 100644
--- a/src/db/sqlite3/db.c
+++ b/src/db/sqlite3/db.c
@@ -15,7 +15,8 @@
int create_tables(const char *path);
// callback function prototypes
-int count_callback(void *first_arg, int argc, char **argv, char **azColName);
+int group_count_callback(void *first_arg, int argc, char **argv, char **azColName);
+int entry_count_callback(void *first_arg, int argc, char **argv, char **azColName);
int load_groups_callback(void *first_arg, int argc, char **argv, char **azColName);
// globals
@@ -56,7 +57,7 @@ Group *db_load_groups() {
}
// count rows in table
- res = sqlite3_exec(db, count_query, count_callback, &output, &zErrMsg);
+ res = sqlite3_exec(db, count_query, group_count_callback, &output, &zErrMsg);
if(res != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
@@ -77,8 +78,39 @@ Group *db_load_groups() {
}
Entry *db_load_entries() {
- // TODO
- return NULL;
+ Entry *output;
+ sqlite3 *db;
+ const char *count_query = "SELECT COUNT(*) FROM 'Entry'";
+ const char *query = "SELECT ROWID, * FROM 'Entry'";
+ char *zErrMsg = NULL; // hold error from sqlite3_exec
+ int res;
+
+ res = sqlite3_open(USER_DATA_PATH, &db);
+ if(res) {
+ fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
+ sqlite3_close(db);
+ return NULL;
+ }
+
+ // count rows in table
+ res = sqlite3_exec(db, count_query, entry_count_callback, &output, &zErrMsg);
+ if(res != SQLITE_OK) {
+ fprintf(stderr, "SQL error: %s\n", zErrMsg);
+ sqlite3_free(zErrMsg);
+ return NULL;
+ }
+
+ // do select query
+ callback_index = 0;
+ res = sqlite3_exec(db, query, load_groups_callback, &output, &zErrMsg);
+ if(res != SQLITE_OK) {
+ fprintf(stderr, "SQL error: %s\n", zErrMsg);
+ sqlite3_free(zErrMsg);
+ return NULL;
+ }
+
+ sqlite3_close(db);
+ return output;
}
@@ -134,14 +166,20 @@ int create_tables(const char *path) {
}
// callback functions
-int count_callback(void *first_arg, int argc, char **argv, char **azColName) {
- printf("count: %d\n", argc);
+int group_count_callback(void *first_arg, int argc, char **argv, char **azColName) {
Group **groups = (Group **) first_arg;
*groups = malloc(sizeof(Group) * argc);
return 0;
}
+int entry_count_callback(void *first_arg, int argc, char **argv, char **azColName) {
+ Entry **entries = (Entry **) first_arg;
+ *entries = malloc(sizeof(Entry) * argc);
+
+ return 0;
+}
+
int load_groups_callback(void *first_arg, int argc, char **argv, char **azColName) {
Group **groups = (Group **) first_arg;
diff --git a/src/main.c b/src/main.c
index 71e5def..18cf5c5 100644
--- a/src/main.c
+++ b/src/main.c
@@ -2,16 +2,16 @@
#include "db.h"
#include "draw.h"
+#include "entry.h"
#include "group.h"
int main() {
Group *groups;
+ Entry *entries;
db_init();
groups = db_load_groups();
-
- // REMOVE debug
- printf("%d\t%s\t%s\t%s\n", group_get_id(groups), group_get_name(groups), group_get_desc(groups), group_get_url(groups));
+ entries = db_load_entries();
tui_init();