Selaa lähdekoodia

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

Iñigo Valentin 4 vuotta sitten
vanhempi
sitoutus
e530dea065

+ 81 - 34
src/RuneOptimizer/db/db.c

@@ -1,3 +1,19 @@
+/*
+ * This file is part of RuneOptimizer.
+ *
+ * RuneOptimizer is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * RuneOptimizer is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * RuneOptimizer. If not, see <https://www.gnu.org/licenses/>.
+ */
 
 /**
  * Opens the database connection.
@@ -7,7 +23,6 @@
  * @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];
@@ -21,18 +36,12 @@ int db_open(sqlite3 **db, char *path){
         strcat(db_location, path);
     }
     if (
-      SUCCESS != sqlite3_open_v2(db_location, db, SQLITE_OPEN_READWRITE, NULL)
+        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;
 }
 
@@ -42,50 +51,88 @@ int db_open(sqlite3 **db, char *path){
  * @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[]){
+int db_close(sqlite3 **db){
+    sqlite3_close_v2(*db);
+}
 
+/**
+ * Executes a database query for a result.
+ *
+ * @param[in|out] db Connection to the database.
+ * @param[out] stmt Statement to load
+ * @param[in] query Query to execute.
+ * @param[in] parameters List of string parameters to bind to the query.
+ * @return SUCCESS or an error code.
+ */
+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);
+    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)
+          stderr, "ERROR executing query '%s': %s\n", query, sqlite3_errmsg(*db)
         );
-        sqlite3_finalize(stmt);
+        sqlite3_finalize(*stmt);
         sqlite3_close(*db);
         return ERROR_DB_DELETE_UNITS_TEAMS;
     }
+
+    // Loop and bind parameters
     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;
+        sqlite3_bind_text(*stmt, i + 1, parameters[i], strlen(parameters[i]), NULL);
     }
-    printf(" QUERY team_name: %s\n", sqlite3_column_text(stmt, 0));*/
 
+    //printf("PREP QUERY: %s\n", sqlite3_expanded_sql(*stmt));
 
+    return SUCCESS;
+}
 
+/**
+ * Executes a database statement.
+ *
+ * @param[in|out] db Connection to the database.
+ * @param[in] query Query to execute.
+ * @param[in] parameters List of string parameters to bind to the query.
+ * @return SUCCESS or an error code.
+ */
+int db_execute(sqlite3 **db, char query[], char *parameters[]){
 
-    //sqlite3_bind_text(stmt_verify, 1, argv[0], strlen(argv[0]), NULL);
-    //sqlite3_bind_text(stmt_verify, 2, argv[1], strlen(argv[1]), NULL);
+    // If the connection has not been initialized, do it now
+    if (NULL == *db){
+        db_open(db, NULL);
+    }
 
+    sqlite3_stmt *stmt;
+    if (SQLITE_OK != sqlite3_prepare_v2(*db, query, -1, &stmt, 0)) {
+        fprintf(
+          stderr, "ERROR executing statement '%s': %s\n", query, sqlite3_errmsg(*db)
+        );
+        sqlite3_finalize(stmt);
+        return ERROR_DB_QUERY;
+    }
+
+    // Loop and bind parameters
+    char total_parameters = sqlite3_bind_parameter_count(stmt);
+    for (int i = 0; i < total_parameters; i ++){
+        sqlite3_bind_text(stmt, i + 1, parameters[i], strlen(parameters[i]), NULL);
+    }
+    //printf("PREP QUERY: %s\n", sqlite3_expanded_sql(stmt));
+    if (SQLITE_DONE != sqlite3_step(stmt)){
+        fprintf(
+          stderr,
+          "ERROR Executing statement '%s': %s\n",
+          query, sqlite3_errmsg(*db)
+        );
+        sqlite3_finalize(stmt);
+        return ERROR_DB_EXECUTE;
+    }
 
+    sqlite3_finalize(stmt);
+    return SUCCESS;
 }

+ 53 - 4
src/RuneOptimizer/db/db.h

@@ -1,9 +1,58 @@
+/*
+ * This file is part of RuneOptimizer.
+ *
+ * RuneOptimizer is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * RuneOptimizer is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * RuneOptimizer. If not, see <https://www.gnu.org/licenses/>.
+ */
+
 #define DEFAULT_DB_PATH "./data.sqlite"
 
-//int db_open(sqlite3 *db, char *path);
+/**
+ * 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);
 
-//int db_close(sqlite3 *db);
+/**
+ * Finalizes a database connection.
+ *
+ * @param[in|out] db Connection to close.
+ * @return SUCCESS or an error code.
+ */
+int db_close(sqlite3 **db);
 
-//int db_query(sqlite3 *db, sqlite3_stmt *stmt, char *query, char **parameters);
+/**
+ * Executes a database query for a result.
+ *
+ * @param[in|out] db Connection to the database.
+ * @param[out] stmt Statement to load
+ * @param[in] query Query to execute.
+ * @param[in] parameters List of string parameters to bind to the query.
+ * @return SUCCESS or an error code.
+ */
+int db_query(
+  sqlite3 **db, sqlite3_stmt **stmt, char query[], char *parameters[]
+);
 
-int db_execute(sqlite3 *db, sqlite3_stmt *stmt, char *query, ...);
+/**
+ * Executes a database statement.
+ *
+ * @param[in|out] db Connection to the database.
+ * @param[in] query Query to execute.
+ * @param[in] parameters List of string parameters to bind to the query.
+ * @return SUCCESS or an error code.
+ */
+int db_execute(sqlite3 **db, char query[], char *parameters[]);

+ 2 - 0
src/RuneOptimizer/error/error.h

@@ -78,3 +78,5 @@
 #define ERROR_DB_VERIFY_TEAM_UNIT 216
 #define ERROR_DB_DELETE_UNITS_TEAMS 217
 #define ERROR_DB_DELETE_TEAMS 217
+#define ERROR_DB_QUERY 218
+#define ERROR_DB_EXECUTE 218

+ 101 - 106
src/RuneOptimizer/optimize/optimize.c

@@ -29,7 +29,7 @@
  * @return SUCCESS or an error code.
  */
 int parse_optimization_arguments(
-  int argc,  char argv[][128], char id[64],             char *level,
+  int argc,  char argv[][128], char id[64], char *level,
   int *sets, int *stats,   struct Stats *min_stats, char *gui,
   char *storage, char excluded_teams[64][64], char excluded_units[64][64]
 ){
@@ -525,70 +525,58 @@ int parse_optimization_arguments(
  * @return SUCCESS or an error code.
  */
 int retrieveUnit(char id[64], struct Unit *unit){
-    sqlite3 *db;
-    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;
-    }
-    char *sql = "SELECT "
+    sqlite3 *db = NULL;
+    sqlite3_stmt *stmt_unit;
+    char *parameters[2];
+    parameters[0] = id;
+    parameters[1] = id;
+    db_query(
+      &db, &stmt_unit,
+      "SELECT "
       "id, name, base_hp, base_atk, base_def, base_spd, base_crr, base_crd, "
       "base_res, base_acc, current_hp, current_atk, current_def, current_spd, "
       "current_crr, current_crd, current_res, current_acc "
-      "FROM units WHERE id = ? OR name = ?";
-    sqlite3_stmt *res;
-    if (SQLITE_OK != sqlite3_prepare_v2(db, sql, -1, &res, 0)) {
-        fprintf(
-          stderr,
-          "Failed to execute statement to select unit:%s\n",
-          sqlite3_errmsg(db)
-        );
-        sqlite3_finalize(res);
-        sqlite3_close(db);
-        return ERROR_DB_UNIT;
-    }
-    sqlite3_bind_text(res, 1, id, strlen(id), NULL);
-    sqlite3_bind_text(res, 2, id, strlen(id), NULL);
+      "FROM units WHERE id = ? OR name = ?",
+      parameters
+    );
 
     // Fetch just one line
-    int step = sqlite3_step(res);
+    int step = sqlite3_step(stmt_unit);
 
     // Populate the unit structure with the red data.
     if (step != SQLITE_ROW) {
         fprintf(stderr, "Unit not found: %s\n", sqlite3_errmsg(db));
-        sqlite3_finalize(res);
+        sqlite3_finalize(stmt_unit);
         sqlite3_close(db);
         return ERROR_DB_UNIT_NOF_FOUND;
     }
-    strcpy(unit->id, sqlite3_column_text(res, 0));
-    strcpy(unit->name, sqlite3_column_text(res, 1));
-    unit->base_hp = sqlite3_column_int(res, 2);
-    unit->base_atk = sqlite3_column_int(res, 3);
-    unit->base_def = sqlite3_column_int(res, 4);
-    unit->base_spd = sqlite3_column_int(res, 5);
-    unit->base_crr = sqlite3_column_int(res, 6);
-    unit->base_crd = sqlite3_column_int(res, 7);
-    unit->base_res = sqlite3_column_int(res, 8);
-    unit->base_acc = sqlite3_column_int(res, 9);
+    strcpy(unit->id, sqlite3_column_text(stmt_unit, 0));
+    strcpy(unit->name, sqlite3_column_text(stmt_unit, 1));
+    unit->base_hp = sqlite3_column_int(stmt_unit, 2);
+    unit->base_atk = sqlite3_column_int(stmt_unit, 3);
+    unit->base_def = sqlite3_column_int(stmt_unit, 4);
+    unit->base_spd = sqlite3_column_int(stmt_unit, 5);
+    unit->base_crr = sqlite3_column_int(stmt_unit, 6);
+    unit->base_crd = sqlite3_column_int(stmt_unit, 7);
+    unit->base_res = sqlite3_column_int(stmt_unit, 8);
+    unit->base_acc = sqlite3_column_int(stmt_unit, 9);
     unit->base_ehp = calculate_ehp(unit->base_hp, unit->base_def);
     unit->base_dmg =
       calculate_dmg(unit->base_atk, unit->base_crr, unit->base_crd);
-    unit->current_hp = sqlite3_column_int(res, 10);
-    unit->current_atk = sqlite3_column_int(res, 11);
-    unit->current_def = sqlite3_column_int(res, 12);
-    unit->current_spd = sqlite3_column_int(res, 13);
-    unit->current_crr = sqlite3_column_int(res, 14);
-    unit->current_crd = sqlite3_column_int(res, 15);
-    unit->current_res = sqlite3_column_int(res, 16);
-    unit->current_acc = sqlite3_column_int(res, 17);
+    unit->current_hp = sqlite3_column_int(stmt_unit, 10);
+    unit->current_atk = sqlite3_column_int(stmt_unit, 11);
+    unit->current_def = sqlite3_column_int(stmt_unit, 12);
+    unit->current_spd = sqlite3_column_int(stmt_unit, 13);
+    unit->current_crr = sqlite3_column_int(stmt_unit, 14);
+    unit->current_crd = sqlite3_column_int(stmt_unit, 15);
+    unit->current_res = sqlite3_column_int(stmt_unit, 16);
+    unit->current_acc = sqlite3_column_int(stmt_unit, 17);
     unit->current_ehp = calculate_ehp(unit->current_hp, unit->current_def);
     unit->current_dmg =
       calculate_dmg(unit->current_atk, unit->current_crr, unit->current_crd);
 
     // Close db connection
-    sqlite3_finalize(res);
+    sqlite3_finalize(stmt_unit);
     sqlite3_close(db);
 }
 
@@ -933,15 +921,9 @@ int get_runes(
   struct Rune runes[7][600], int rune_count[7]
 ){
     char **query;
-    sqlite3 *db;
+    sqlite3 *db = NULL;
     sqlite3_stmt *stmt_runes[7];
-    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;
-    }
+
     for (char i = 1; i < 7; i ++){
         if (i % 2 == 0){
             query = &query_even;
@@ -949,15 +931,12 @@ int get_runes(
         else{
             query = &query_odd;
         }
-        if (SQLITE_OK != sqlite3_prepare_v2(db, *query, -1, &stmt_runes[i], 0)){
-            fprintf(
-              stderr,
-              "Error getting runes for slot %d: %s\n", i, sqlite3_errmsg(db)
-            );
-            sqlite3_close(db);
-            return ERROR_DB_RUNES_SLOT;
-        }
-        sqlite3_bind_int(stmt_runes[i], 1, i);
+        char *parameters[1];
+        parameters[0] = malloc(1);
+        sprintf(parameters[0], "%d", i);
+        db_query(&db, &stmt_runes[i], *query, parameters);
+
+        printf("PREP QUERY: %s\n", sqlite3_expanded_sql(stmt_runes[i]));
     }
 
     // Get and populate all the runes
@@ -991,6 +970,45 @@ int get_runes(
     }
     for (int r = 1; r < 7; r ++) sqlite3_finalize(stmt_runes[r]);
     sqlite3_close(db);
+
+
+    for (int i = 0; i < rune_count[1]; i++){
+        if (1 != runes[1][i].slot){
+            printf("Rune in reel 1 with slot %d: %s\n", runes[1][i].slot, runes[1][i].id);
+        }
+    }
+
+    for (int i = 0; i < rune_count[2]; i++){
+        if (2 != runes[2][i].slot){
+            printf("Rune in reel 2 with slot %d: %s\n", runes[2][i].slot, runes[2][i].id);
+        }
+    }
+
+    for (int i = 0; i < rune_count[3]; i++){
+        if (3 != runes[3][i].slot){
+            printf("Rune in reel 3 with slot %d: %s\n", runes[3][i].slot, runes[3][i].id);
+        }
+    }
+
+    for (int i = 0; i < rune_count[4]; i++){
+        if (4 != runes[4][i].slot){
+            printf("Rune in reel 4 with slot %d: %s\n", runes[4][i].slot, runes[4][i].id);
+        }
+    }
+
+    for (int i = 0; i < rune_count[5]; i++){
+        if (5 != runes[5][i].slot){
+            printf("Rune in reel 5 with slot %d: %s\n", runes[5][i].slot, runes[5][i].id);
+        }
+    }
+
+    for (int i = 0; i < rune_count[6]; i++){
+        if (6 != runes[6][i].slot){
+            printf("Rune in reel 6 with slot %d: %s\n", runes[6][i].slot, runes[6][i].id);
+        }
+    }
+
+
     return SUCCESS;
 }
 
@@ -1148,31 +1166,22 @@ int present_option(struct Unit *unit, Result *result){
     strcpy(present[6], "");
     strcpy(present[7], "");
 
-    // Retrieve the rune stats from the database
-    sqlite3 *db;
-    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;
-    }
+    // Retrieve the runes from the database
+    sqlite3 *db = NULL;
     printf("\n");
+    char *parameters[1];
     for (int i = 6; i != 0;){
-        char *rune_id = result->rune_ids[i - 1];
+        parameters[0] = result->rune_ids[i - 1];
         sqlite3_stmt *rune_res;
-        char *sql =
-            "SELECT runes.id, runes.slot, runes.type, units.id, units.name, runes.level "
-            "FROM runes LEFT JOIN units ON runes.unit = units.id "
-            "WHERE runes.id = ?";
-        if (SQLITE_OK == sqlite3_prepare_v2(db, sql, -1, &rune_res, 0)) {
-            sqlite3_bind_text(rune_res, 1, result->rune_ids[i - 1], strlen(result->rune_ids[i - 1]), NULL);
-        }
-        else {
-            fprintf(stderr, "Failed to execute statement to select rune: %s\n", sqlite3_errmsg(db));
-            return ERROR_DB_RUNES_RESULT;
-        }
 
-        int step = sqlite3_step(rune_res);
-        if (step == SQLITE_ROW) {
+        db_query(
+          &db, &rune_res,
+          "SELECT runes.id, runes.slot, runes.type, units.id, units.name, runes.level "
+          "FROM runes LEFT JOIN units ON runes.unit = units.id "
+          "WHERE runes.id = ?",
+          parameters
+        );
+        if (SQLITE_ROW == sqlite3_step(rune_res)) {
             char tmp[130];
             strcpy(tmp, "");
             strcat(present[0], "|");
@@ -1183,7 +1192,6 @@ int present_option(struct Unit *unit, Result *result){
             strcat(present[0], tmp);
             sprintf(tmp, "%*s|  ", 12, sqlite3_column_text(rune_res, 0));
             strcat(present[0], tmp);
-
             strcat(present[1], "|");
             if (sqlite3_column_type(rune_res, 3) == SQLITE_NULL){
                 sprintf(tmp, " %*s", -14, "Storage");
@@ -1195,24 +1203,19 @@ int present_option(struct Unit *unit, Result *result){
                 strcat(present[1], tmp);
                 strcat(present[1], "|");
             }
-
             // Rune level
             sprintf(tmp, "        +%*s |  ", 2, sqlite3_column_text(rune_res, 5));
             strcat(present[1], tmp);
 
+            // Get stats
             sqlite3_stmt *stats_res;
-            char *sql_stats =
-                "SELECT rune, slot, stat, value, enchant, grind "
-                "FROM rune_stats "
-                "WHERE rune = ?";
-            if (SQLITE_OK == sqlite3_prepare_v2(db, sql_stats, -1, &stats_res, 0)) {
-                sqlite3_bind_text(stats_res, 1, sqlite3_column_text(rune_res, 0), strlen(sqlite3_column_text(rune_res, 0)), NULL);
-            }
-            else {
-                fprintf(stderr, "Failed to execute statement to select rune stats: %s\n", sqlite3_errmsg(db));
-                return ERROR_DB_STATS_RESULT;
-            }
-
+            db_query(
+              &db, &stats_res,
+              "SELECT rune, slot, stat, value, enchant, grind "
+              "FROM rune_stats "
+              "WHERE rune = ?",
+              parameters
+            );
             int curr_slot = -1;
             while (1 == 1){
                 int status = sqlite3_step(stats_res);
@@ -1242,7 +1245,7 @@ int present_option(struct Unit *unit, Result *result){
                     }
 
                     curr_slot ++;
-                }
+                    }
                 else{
                     break;
                 }
@@ -1385,18 +1388,10 @@ int gui_output(Result results[5000], int result_count){
         }
 
         printf(json);
-        printf("");
     }
-    // Remove last comma
-    //json[strlen(json) - 1] = '\0';
-    //free(*tmp);
 
     // End and print
     printf("]}\n");
-    //printf("STRLEN JSON %d", strlen(json));
-    //printf("%s", json);
-    //free(*json);
-    //printf("END");
     return SUCCESS;
 }
 

+ 88 - 197
src/RuneOptimizer/team/team.c

@@ -64,26 +64,14 @@ int remove_team_unit(int argc, char *argv[]){
         );
         return ERROR_INPUT_TEAM_NO_ACTION;
     }
-    sqlite3 *db;
-    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;
-    }
-    char query[200] =
-      "SELECT count(unit) AS inc FROM units_teams WHERE team = ? AND unit = ?";
-
+    sqlite3 *db = NULL;
     sqlite3_stmt *stmt_verify;
-    if (SQLITE_OK != sqlite3_prepare_v2(db, query, -1, &stmt_verify, 0)) {
-        fprintf(
-          stderr, "Error verifiying units and teams: %s\n", sqlite3_errmsg(db)
-        );
-        return ERROR_DB_RUNES_SLOT;
-    }
-    sqlite3_bind_text(stmt_verify, 1, argv[0], strlen(argv[0]), NULL);
-    sqlite3_bind_text(stmt_verify, 2, argv[1], strlen(argv[1]), NULL);
+    char *parameters[2] = {argv[0], argv[1]};
+    db_query(
+      &db, &stmt_verify,
+      "SELECT count(unit) AS inc FROM units_teams WHERE team = ? AND unit = ?",
+      parameters
+    );
     if (SQLITE_ROW != sqlite3_step(stmt_verify)){
         fprintf(
           stderr, "Unable to verify unit and team: %s\n", sqlite3_errmsg(db)
@@ -105,37 +93,26 @@ int remove_team_unit(int argc, char *argv[]){
     }
     sqlite3_finalize(stmt_verify);
 
-    // Delete from units_teams
-    sqlite3_stmt *stmt_insert;
-    strcpy(query, "DELETE FROM units_teams WHERE team = ? AND unit = ?");
-    if (SQLITE_OK != sqlite3_prepare_v2(db, query, -1, &stmt_insert, 0)) {
-        fprintf(
-          stderr,
-          "Failed to prepare statement to delete from units_teams: %s\n",
-          sqlite3_errmsg(db)
-        );
-        sqlite3_finalize(stmt_insert);
-        sqlite3_close(db);
-        return ERROR_DB_DELETE_UNITS_TEAMS;
-    }
-    sqlite3_bind_text(stmt_verify, 1, argv[0], strlen(argv[0]), NULL);
-    sqlite3_bind_text(stmt_verify, 2, argv[1], strlen(argv[1]), NULL);
-    if (SQLITE_DONE != sqlite3_step(stmt_insert)){
+    // Delete from units_teams.
+    if (
+      SUCCESS !=
+      db_execute(
+        &db, "DELETE FROM units_teams WHERE team = ? AND unit = ?", parameters
+      )
+    ){
         fprintf(
           stderr,
-          "Unable to to insert into units_teams: %s\n", sqlite3_errmsg(db)
+          "Unable to remove unit from team: %s\n", sqlite3_errmsg(db)
         );
-        sqlite3_finalize(stmt_insert);
         sqlite3_close(db);
         return ERROR_DB_DELETE_UNITS_TEAMS;
     }
 
-    // Inform about the newly created team
+    // Inform about the new team ocmposition
     printf(
       "Unit with ID '%s' succesfully removed from team with id '%s'\n",
       argv[1], argv[0]
     );
-    sqlite3_finalize(stmt_insert);
     sqlite3_close(db);
 
     // Show the team composition and end.
@@ -159,31 +136,17 @@ int add_team_unit(int argc, char *argv[]){
         );
         return ERROR_INPUT_TEAM_NO_ACTION;
     }
-    sqlite3 *db;
-    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;
-    }
-    char query[200] =
+    sqlite3 *db = NULL;
+    sqlite3_stmt *stmt_verify;
+    char *parameters[4] = {argv[0], argv[1], argv[0], argv[1]};
+    db_query(
+      &db, &stmt_verify,
       "SELECT "
       "(SELECT count(id) FROM teams WHERE id = ?) AS team, "
       "(SELECT count(id) FROM units WHERE id = ?) AS unit, "
-      "(SELECT count(*) FROM units_teams WHERE team = ? AND unit = ?) AS inc";
-
-    sqlite3_stmt *stmt_verify;
-    if (SQLITE_OK != sqlite3_prepare_v2(db, query, -1, &stmt_verify, 0)) {
-        fprintf(
-          stderr, "Error verifiying units and teams: %s\n", sqlite3_errmsg(db)
-        );
-        return ERROR_DB_RUNES_SLOT;
-    }
-    sqlite3_bind_text(stmt_verify, 1, argv[0], strlen(argv[0]), NULL);
-    sqlite3_bind_text(stmt_verify, 2, argv[1], strlen(argv[1]), NULL);
-    sqlite3_bind_text(stmt_verify, 3, argv[0], strlen(argv[0]), NULL);
-    sqlite3_bind_text(stmt_verify, 4, argv[1], strlen(argv[1]), NULL);
+      "(SELECT count(*) FROM units_teams WHERE team = ? AND unit = ?) AS inc",
+      parameters
+    );
     if (SQLITE_ROW != sqlite3_step(stmt_verify)){
         fprintf(
           stderr, "Unable to verify unit and team: %s\n", sqlite3_errmsg(db)
@@ -216,26 +179,16 @@ int add_team_unit(int argc, char *argv[]){
     sqlite3_finalize(stmt_verify);
 
     // Insert into units_teams
-    sqlite3_stmt *stmt_insert;
-    strcpy(query, "INSERT INTO units_teams (team, unit) VALUES (?, ?)");
-    if (SQLITE_OK != sqlite3_prepare_v2(db, query, -1, &stmt_insert, 0)) {
-        fprintf(
-          stderr,
-          "Failed to prepare statement to insert into units_teams: %s\n",
-          sqlite3_errmsg(db)
-        );
-        sqlite3_finalize(stmt_insert);
-        sqlite3_close(db);
-        return ERROR_DB_INSERT_UNITS_TEAMS;
-    }
-    sqlite3_bind_text(stmt_verify, 1, argv[0], strlen(argv[0]), NULL);
-    sqlite3_bind_text(stmt_verify, 2, argv[1], strlen(argv[1]), NULL);
-    if (SQLITE_DONE != sqlite3_step(stmt_insert)){
+    if (
+      SUCCESS !=
+      db_execute(
+        &db, "INSERT INTO units_teams (team, unit) VALUES (?, ?)", parameters
+      )
+    ){
         fprintf(
           stderr,
-          "Unable to to insert into units_teams: %s\n", sqlite3_errmsg(db)
+          "Unable to add unit to team: %s\n", sqlite3_errmsg(db)
         );
-        sqlite3_finalize(stmt_insert);
         sqlite3_close(db);
         return ERROR_DB_INSERT_UNITS_TEAMS;
     }
@@ -245,7 +198,6 @@ int add_team_unit(int argc, char *argv[]){
       "Unit with ID '%s' succesfully added to team with id '%s'\n",
       argv[1], argv[0]
     );
-    sqlite3_finalize(stmt_insert);
     sqlite3_close(db);
 
     // Show the team composition and end.
@@ -271,29 +223,18 @@ int list_team(int argc, char *argv[]){
             show_units = TRUE;
         }
     }
-    char id[8] = "\0";
+
     char query[300] = "SELECT id, name, priority FROM teams ";
-    char tmp[64];
+    char *parameters[] = {"", ""};
     if (argc > number_of_options){
-        strcpy(id, argv[0]);
-        strcpy(tmp, "");
-        sprintf(tmp, "WHERE id = '%s' OR name = '%s' ", id, id);
-        strcat(query, tmp);
+        strcat(query, "WHERE id = ? OR name = ?");
+        parameters[0] = argv[0];
+        parameters[1] = argv[0];
     }
     strcat(query, " ORDER BY priority DESC");
-    sqlite3 *db;
-    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;
-    }
+    sqlite3 *db = NULL;
     sqlite3_stmt *stmt_teams;
-    if (SQLITE_OK != sqlite3_prepare_v2(db, query, -1, &stmt_teams, 0)) {
-        fprintf(stderr, "Error getting teams: %s\n", sqlite3_errmsg(db));
-        return ERROR_DB_TEAM_ID;
-    }
+    db_query(&db, &stmt_teams, query, parameters);
 
     printf(" --------------------------------------------------\n");
     printf(" | ID   | Name                             | Prio |\n");
@@ -331,6 +272,7 @@ int list_team(int argc, char *argv[]){
                   strlen(sqlite3_column_text(stmt_teams, 0)), NULL
                 );
                 printf(" |      |----------------------------------|      |\n");
+                char tmp[64];
                 while (SQLITE_ROW == sqlite3_step(stmt_units)){
                     strcpy(tmp, sqlite3_column_text(stmt_units, 1));
                     if (strlen(tmp) > 9){
@@ -394,69 +336,54 @@ int create_team(int argc, char *argv[]){
             return ERROR_INPUT_TEAM_CREATE_PRIORITY;
         }
     }
-
     // Read name
     char name[128];
     strcpy(name, argv[0]);
-
     int new_id = -1;
-
-    // Connect to the db.
-    sqlite3 *db;
-    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;
-    }
+    sqlite3 *db = NULL;
+    sqlite3_stmt *stmt_id;
 
     // Get the new id for the team
-    char sql[512];
-    strcpy(sql, "SELECT max(CAST(id AS INTEGER)) + 1 AS id FROM teams;");
-    sqlite3_stmt *res;
-    if (SQLITE_OK != sqlite3_prepare_v2(db, sql, -1, &res, 0)) {
-        fprintf(
-          stderr,
-          "Failed to execute statement to select new team ID: %s\n",
-          sqlite3_errmsg(db)
-        );
-        sqlite3_finalize(res);
-        sqlite3_close(db);
-        return ERROR_DB_TEAM_ID;
-    }
-    if (SQLITE_ROW != sqlite3_step(res)){
+    db_query(
+      &db, &stmt_id,
+      "SELECT max(CAST(id AS INTEGER)) + 1 AS id FROM teams;",
+      NULL
+    );
+    if (SQLITE_ROW != sqlite3_step(stmt_id)){
         fprintf(stderr, "Unable to read teams: %s\n", sqlite3_errmsg(db));
-        sqlite3_finalize(res);
+        sqlite3_finalize(stmt_id);
         sqlite3_close(db);
         return ERROR_DB_NEW_TEAM_ID;
     }
-    new_id = sqlite3_column_int(res, 0);
 
-    // Insert the row.
-    strcpy(sql, "INSERT INTO teams (id, name, priority) VALUES (?, ?, ?)");
-    if (SQLITE_OK != sqlite3_prepare_v2(db, sql, -1, &res, 0)) {
+    // Get all parameters and convert to char.
+    new_id = sqlite3_column_int(stmt_id, 0);
+    sqlite3_finalize(stmt_id);
+    char priority_c[2];
+    char id_c[4];
+    sprintf(id_c, "%d", new_id);
+    sprintf(priority_c, "%d", priority);
+    char *parameters[3] = {id_c, argv[0], priority_c};
+
+    // Insert into teams
+    if (
+      SUCCESS !=
+      db_execute(
+        &db,
+        "INSERT INTO teams (id, name, priority) VALUES (?, ?, ?)", parameters
+      )
+    ){
         fprintf(
           stderr,
-          "Failed to execute statement to create team: %s\n",
-          sqlite3_errmsg(db)
+          "Unable to create team: %s\n", sqlite3_errmsg(db)
         );
-        sqlite3_finalize(res);
-        sqlite3_close(db);
-        return ERROR_DB_TEAM_ID;
-    }
-    sqlite3_bind_int(res, 1, new_id);
-    sqlite3_bind_text(res, 2, name, strlen(name), NULL);
-    sqlite3_bind_int(res, 3, priority);
-    if (SQLITE_DONE != sqlite3_step(res)){
-        fprintf(stderr, "Unable to create team: %s\n", sqlite3_errmsg(db));
-        sqlite3_finalize(res);
         sqlite3_close(db);
-        return ERROR_DB_NEW_TEAM_ID;
+        return ERROR_DB_INSERT_TEAMS;
     }
 
     // Inform about the newly created team
-    printf("Team ID %d '%s' created with priority %d\n", new_id, name, priority);
-    sqlite3_finalize(res);
     sqlite3_close(db);
+    printf("Team ID %d '%s' created with priority %d\n", new_id, name, priority);
     return SUCCESS;
 }
 
@@ -475,25 +402,15 @@ int delete_team(int argc, char *argv[]){
         );
         return ERROR_INPUT_TEAM_DELETE_ARGUMENT_COUNT;
     }
+    sqlite3 *db = NULL;
 
-    // Open database
-    sqlite3 *db;
-    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;
-    }
-    char query[200] = "SELECT count(id) AS c FROM teams WHERE id = ?";
+    // Verify that the team exists.
     sqlite3_stmt *stmt_verify;
-    if (SQLITE_OK != sqlite3_prepare_v2(db, query, -1, &stmt_verify, 0)) {
-        fprintf(
-          stderr, "Error verifiying team: %s\n", sqlite3_errmsg(db)
-        );
-        return ERROR_DB_TEAM_ID;
-    }
-    sqlite3_bind_text(stmt_verify, 1, argv[0], strlen(argv[0]), NULL);
+    char *parameters[1] = {argv[0]};
+    db_query(
+      &db, &stmt_verify,
+      "SELECT count(id) AS c FROM teams WHERE id = ?", parameters
+    );
     if (SQLITE_ROW != sqlite3_step(stmt_verify)){
         fprintf(
           stderr, "Unable to verify team: %s\n", sqlite3_errmsg(db)
@@ -510,57 +427,31 @@ int delete_team(int argc, char *argv[]){
     }
     sqlite3_finalize(stmt_verify);
 
-    // Delete from teams
-    sqlite3_stmt *stmt_delete;
-    strcpy(query, "DELETE FROM teams WHERE id = ?");
-    if (SQLITE_OK != sqlite3_prepare_v2(db, query, -1, &stmt_delete, 0)) {
-        fprintf(
-          stderr,
-          "Failed to prepare statement to delete from teams: %s\n",
-          sqlite3_errmsg(db)
-        );
-        sqlite3_finalize(stmt_delete);
-        sqlite3_close(db);
-        return ERROR_DB_DELETE_TEAMS;
-    }
-    sqlite3_bind_text(stmt_delete, 1, argv[0], strlen(argv[0]), NULL);
-    if (SQLITE_DONE != sqlite3_step(stmt_delete)){
+    // Delete from table teams.
+    if (
+      SUCCESS !=
+      db_execute(&db, "DELETE FROM teams WHERE id = ?", parameters)
+    ){
         fprintf(
           stderr,
-          "Unable to to delete from units_teams: %s\n", sqlite3_errmsg(db)
+          "Unable to delete team: %s\n", sqlite3_errmsg(db)
         );
-        sqlite3_finalize(stmt_delete);
         sqlite3_close(db);
         return ERROR_DB_DELETE_TEAMS;
     }
-    sqlite3_finalize(stmt_delete);
 
-    // Delete from unit_teams
-    strcpy(query, "DELETE FROM units_teams WHERE team = ?");
-    if (SQLITE_OK != sqlite3_prepare_v2(db, query, -1, &stmt_delete, 0)) {
-        fprintf(
-          stderr,
-          "Failed to prepare statement to delete from units_teams: %s\n",
-          sqlite3_errmsg(db)
-        );
-        sqlite3_finalize(stmt_delete);
-        sqlite3_close(db);
-        return ERROR_DB_DELETE_TEAMS;
-    }
-    sqlite3_bind_text(stmt_delete, 1, argv[0], strlen(argv[0]), NULL);
-    if (SQLITE_DONE != sqlite3_step(stmt_delete)){
+    // Delete from table units_teams.
+    if (
+      SUCCESS !=
+      db_execute(&db, "DELETE FROM units_teams WHERE id = ?", parameters)
+    ){
         fprintf(
           stderr,
-          "Unable to to delete from units_teams: %s\n", sqlite3_errmsg(db)
+          "Unable to delete units in team: %s\n", sqlite3_errmsg(db)
         );
-        sqlite3_finalize(stmt_delete);
         sqlite3_close(db);
-        return ERROR_DB_DELETE_TEAMS;
+        return ERROR_DB_DELETE_UNITS_TEAMS;
     }
-
-    // Inform about the deleted team
-    printf("Team with ID '%s' deleted\n", argv[0]);
-    sqlite3_finalize(stmt_delete);
     sqlite3_close(db);
     return SUCCESS;
 }

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 410 - 502
src/RuneOptimizer/update/update.c


Kaikkia tiedostoja ei voida näyttää, sillä liian monta tiedostoa muuttui tässä diffissä