Browse Source

Database functions separated in their own module. All calls are now simplified.

Iñigo Valentin 4 years ago
parent
commit
d72af5c327

+ 5 - 0
src/RuneOptimizer/RuneOptimizer.c

@@ -17,6 +17,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <string.h>
 #include <sqlite3.h>
 #include <math.h>
@@ -25,6 +26,8 @@
 #include <json-c/json.h>
 #include "RuneOptimizer.h"
 #include "error/error.h"
+#include "db/db.h"
+#include "db/db.c"
 #include "help/help.h"
 #include "help/help.c"
 #include "optimize/optimize.h"
@@ -61,9 +64,11 @@ int main(int argc, char *argv[]){
     }
     if (last_path_separator != -1){
         strncpy(db_location, argv[0], last_path_separator);
+        strncpy(executable_location, argv[0], last_path_separator);
     }
     else{
         strcpy(db_location, "");
+        strcpy(executable_location, "");
     }
     strcat(db_location, DB_NAME);
 

+ 1 - 0
src/RuneOptimizer/RuneOptimizer.h

@@ -25,6 +25,7 @@
 #define FALSE 0
 
 char db_location[256];
+char executable_location[512];
 
 struct Rune_Set_Count {
     unsigned short energy;

+ 91 - 0
src/RuneOptimizer/db/db.c

@@ -0,0 +1,91 @@
+
+/**
+ * Opens the database connection.
+ *
+ * @param[out] db Database object.
+ * @param[in] path Database path, relative to the execution path. Optional.
+ * @return SUCCESS or an error code.
+ */
+int db_open(sqlite3 **db, char *path){
+    printf("IN_DB_OPEN\n");
+
+    // Get the path, relative to the program
+    char db_location[512];
+    strcpy(db_location, executable_location);
+
+    // If db specified as argument, use it, else use the default location.
+    if (path == NULL){
+        strcat(db_location, DEFAULT_DB_PATH);
+    }
+    else{
+        strcat(db_location, path);
+    }
+    if (
+      SUCCESS != sqlite3_open_v2(db_location, db, SQLITE_OPEN_READWRITE, NULL)
+    ){
+        fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(*db));
+        sqlite3_close(*db);
+        return ERROR_DB_CANT_OPEN;
+    }
+
+
+
+
+
+
+    return SUCCESS;
+}
+
+/**
+ * Finalizes a database connection.
+ *
+ * @param[in|out] db Connection to close.
+ * @return SUCCESS or an error code.
+ */
+//int db_close(sqlite3 *db){
+//    sqlite3_close_v2(db);
+//}
+
+int db_query(sqlite3 **db, sqlite3_stmt **stmt, char query[], char *parameters[]){
+
+
+    // If the connection has not been initialized, do it now
+    if (NULL != *db){
+        db_open(*db, NULL);
+    }
+
+    sqlite3_stmt *stmt2;
+
+    if (SQLITE_OK != sqlite3_prepare_v2(*db, query, -1, stmt, 0)) {
+        fprintf(
+          stderr,
+          "Failed IN QUERY: %s\n",
+          sqlite3_errmsg(db)
+        );
+        sqlite3_finalize(stmt);
+        sqlite3_close(*db);
+        return ERROR_DB_DELETE_UNITS_TEAMS;
+    }
+    char total_parameters = sqlite3_bind_parameter_count(*stmt);
+    for (int i = 0; i < total_parameters; i ++){
+        sqlite3_bind_text(*stmt, 1, parameters[i], strlen(parameters[i]), NULL);
+    }
+    printf("END QUERY\n");
+    return 0;
+    /*int step = sqlite3_step(stmt);
+    if (step != SQLITE_ROW) {
+        fprintf(stderr, "Unit not found: %s\n", sqlite3_errmsg(db));
+        //sqlite3_finalize(stmt);
+        //sqlite3_close(db);
+        //return ERROR_DB_UNIT_NOF_FOUND;
+    }
+    printf(" QUERY team_name: %s\n", sqlite3_column_text(stmt, 0));*/
+
+
+
+
+    //sqlite3_bind_text(stmt_verify, 1, argv[0], strlen(argv[0]), NULL);
+    //sqlite3_bind_text(stmt_verify, 2, argv[1], strlen(argv[1]), NULL);
+
+
+}

+ 9 - 0
src/RuneOptimizer/db/db.h

@@ -0,0 +1,9 @@
+#define DEFAULT_DB_PATH "./data.sqlite"
+
+//int db_open(sqlite3 *db, char *path);
+
+//int db_close(sqlite3 *db);
+
+//int db_query(sqlite3 *db, sqlite3_stmt *stmt, char *query, char **parameters);
+
+int db_execute(sqlite3 *db, sqlite3_stmt *stmt, char *query, ...);