aboutsummaryrefslogtreecommitdiffstats
path: root/src/data.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/data.c')
-rw-r--r--src/data.c131
1 files changed, 113 insertions, 18 deletions
diff --git a/src/data.c b/src/data.c
index 82b5570..e3c0703 100644
--- a/src/data.c
+++ b/src/data.c
@@ -8,8 +8,9 @@
#include <errno.h>
#include "collections.h"
-static gboolean data_check_and_create_database(gchar *data_file);
-static gint data_create_new_database(const gchar *filename);
+static gboolean data_check_and_create_database(gchar *data_file);
+static gint data_create_new_database(const gchar *filename);
+static struct collection *data_get_collection_from_stmt(sqlite3_stmt *stmt);
GList *data_get_series(void)
{
@@ -38,14 +39,7 @@ GList *data_get_series(void)
&statement, NULL);
if (res == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
- struct collection *col =
- (struct collection *)malloc(sizeof(struct collection));
-
- col->id = sqlite3_column_int(statement, 0);
- col->name = g_strdup(sqlite3_column_text(statement, 1));
- col->current_qty = sqlite3_column_int(statement, 2);
- col->total_qty = sqlite3_column_int(statement, 3);
-
+ struct collection *col = data_get_collection_from_stmt(statement);
list = g_list_append(list, (gpointer)col);
}
}
@@ -53,19 +47,103 @@ GList *data_get_series(void)
g_print("error %d: %s\n", res, sqlite3_errmsg(database));
/* Release the compiled statement from memory */
sqlite3_finalize(statement);
- }
- sqlite3_close(database);
+ }
+ sqlite3_close(database);
}
return list;
}
+struct collection *data_get_series_by_id(gint collection_id)
+{
+ sqlite3 *database;
+ sqlite3_stmt *statement;
+ gchar *data_file;
+ struct collection *col = NULL;
+
+ data_file = collections_get_data_file();
+
+ if (data_check_and_create_database(data_file)) {
+ if (sqlite3_open(data_file, &database) == SQLITE_OK) {
+ int res;
+ const char *sql = g_strdup_printf(
+ " SELECT id, "
+ " name, "
+ " current_qty, "
+ " total_qty "
+ " FROM collection "
+ " WHERE id = %d ", collection_id);
+
+ res = sqlite3_prepare_v2(database, sql, strlen(sql), &statement, NULL);
+ if (res == SQLITE_OK) {
+ if (sqlite3_step(statement) == SQLITE_ROW) {
+ col = data_get_collection_from_stmt(statement);
+ }
+ }
+ sqlite3_finalize(statement);
+ }
+ sqlite3_close(database);
+ }
+
+ return col;
+}
+
+gint *data_get_items_by_collection_id(gint collection_id)
+{
+ gint count;
+ gint *volumes;
+ sqlite3 *database;
+ sqlite3_stmt *statement;
+ gchar *data_file;
+
+ data_file = collections_get_data_file();
+ count = 0;
+
+ if (data_check_and_create_database(data_file)) {
+ if (sqlite3_open(data_file, &database) == SQLITE_OK) {
+ int res;
+ const char *sql = g_strdup_printf(
+ " SELECT COUNT(id) "
+ " FROM items "
+ " WHERE collection_id = %d ", collection_id);
+
+ res = sqlite3_prepare_v2(database, sql, strlen(sql), &statement, NULL);
+ if (res == SQLITE_OK) {
+ if (sqlite3_step(statement) == SQLITE_ROW) {
+ count = sqlite3_column_int(statement, 0);
+ }
+ }
+ sqlite3_finalize(statement);
+
+ if (count > 0) {
+ sql = g_strdup_printf(
+ " SELECT id "
+ " FROM items "
+ " WHERE collection_id = %d ", collection_id);
+
+ res = sqlite3_prepare_v2(database, sql, strlen(sql), &statement, NULL);
+ if (res == SQLITE_OK) {
+ gint i = 0;
+ while (sqlite3_step(statement) == SQLITE_ROW) {
+ volumes[i++] = sqlite3_column_int(statement, 0);
+ }
+ }
+ }
+ }
+ sqlite3_close(database);
+ }
+
+ return volumes;
+}
+
gboolean data_add_series(gchar *name, gint total_qty)
{
sqlite3 *database;
sqlite3_stmt *statement;
gchar *data_file;
+ gboolean result;
+ result = FALSE;
data_file = collections_get_data_file();
if (data_check_and_create_database(data_file)) {
@@ -81,12 +159,14 @@ gboolean data_add_series(gchar *name, gint total_qty)
&statement, NULL);
if (res == SQLITE_OK) {
if (sqlite3_step(statement) == SQLITE_DONE)
- return TRUE;
+ result = TRUE;
}
+ sqlite3_finalize(statement);
}
+ sqlite3_close(database);
}
- return FALSE;
+ return result;
}
gboolean data_add_to_series(gint collection_id, gint count)
@@ -94,10 +174,10 @@ gboolean data_add_to_series(gint collection_id, gint count)
sqlite3 *database;
sqlite3_stmt *statement;
gchar *data_file;
-
- g_print("collection_id: %d, count: %d\n", collection_id, count);
+ gboolean result;
data_file = collections_get_data_file();
+ result = FALSE;
if (data_check_and_create_database(data_file)) {
if (sqlite3_open(data_file, &database) == SQLITE_OK) {
@@ -113,12 +193,14 @@ gboolean data_add_to_series(gint collection_id, gint count)
&statement, NULL);
if (res == SQLITE_OK) {
if (sqlite3_step(statement) == SQLITE_DONE)
- return TRUE;
+ result = TRUE;
}
+ sqlite3_finalize(statement);
}
+ sqlite3_close(database);
}
- return FALSE;
+ return result;
}
static gboolean data_check_and_create_database(gchar *data_file)
@@ -185,3 +267,16 @@ static gint data_create_new_database(const gchar *filename)
sqlite3_close(db);
return 0;
}
+
+static struct collection *data_get_collection_from_stmt(sqlite3_stmt *stmt)
+{
+ struct collection *collection =
+ (struct collection *)malloc(sizeof(struct collection));
+
+ collection->id = sqlite3_column_int(stmt, 0);
+ collection->name = g_strdup(sqlite3_column_text(stmt, 1));
+ collection->current_qty = sqlite3_column_int(stmt, 2);
+ collection->total_qty = sqlite3_column_int(stmt, 3);
+
+ return collection;
+}