diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2019-09-29 13:45:14 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2019-09-29 13:45:14 (GMT) |
commit | 522298cf50e67d92b0aa85a85177ed7c16b00e08 (patch) | |
tree | 134a48dfc67584fb8e147d326135f2bd109bdfac /src/analysis | |
parent | 034fda4f2b0eb1c2093b24eb749733ce1ea8791e (diff) |
Provided a generic way to update DB values.
Diffstat (limited to 'src/analysis')
-rw-r--r-- | src/analysis/db/collection.c | 142 |
1 files changed, 19 insertions, 123 deletions
diff --git a/src/analysis/db/collection.c b/src/analysis/db/collection.c index 9ae7b9c..7b2082a 100644 --- a/src/analysis/db/collection.c +++ b/src/analysis/db/collection.c @@ -1219,147 +1219,43 @@ static bool g_db_collection_store_updated_item(const GDbCollection *collec, cons bool result; /* Conclusion à faire remonter */ bound_value *values; /* Champs de table à inclure */ size_t count; /* Nombre de ces champs */ - char *sql; /* Requête SQL à construire */ - bool first; /* Première valeur ? */ + bound_value *updates; /* Champs à mettre à jour */ + size_t ucount; /* Nombre de ces champs */ + bound_value *conds; /* Champs de condition */ + size_t ccount; /* Nombre de ces champs */ + const bound_value *flags_ptr; /* Emplacement des fanions */ size_t i; /* Boucle de parcours */ - sqlite3_stmt *stmt; /* Déclaration mise en place */ - int ret; /* Bilan d'un appel à SQLite */ - int index; /* Indice de valeur attachée */ - const bound_value *timestamp; /* Valeur de l'horodatage */ result = false; if (!g_db_item_store(item, &values, &count)) - goto gdcsui_building_values; + goto building_values; - /* Préparation de la requête */ + updates = malloc(1 * sizeof(bound_value)); + ucount = 0; - sql = strdup("UPDATE "); - sql = stradd(sql, collec->name); + conds = malloc((count - 1) * sizeof(bound_value)); + ccount = 0; - sql = stradd(sql, " SET timestamp = ? "); - - sql = stradd(sql, "WHERE "); - - first = true; + flags_ptr = find_bound_value(values, count, "flags"); for (i = 0; i < count; i++) { - if (strcmp(values[i].name, "timestamp") == 0) - continue; - - if (first) - first = false; - else - sql = stradd(sql, " AND "); - - sql = stradd(sql, values[i].name); - - if (values[i].type == SQLITE_NULL) - sql = stradd(sql, " IS "); - else - sql = stradd(sql, " = "); - - if (values[i].type == SQLITE_RAW) - sql = stradd(sql, values[i].cstring); - else - sql = stradd(sql, "?"); - - } - - sql = stradd(sql, ";"); - - ret = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL); - if (ret != SQLITE_OK) - { - fprintf(stderr, "Can't prepare UPDATE statment '%s' (ret=%d): %s\n", sql, ret, sqlite3_errmsg(db)); - goto gdcsui_exit; - } - - /* Attribution des valeurs */ - - index = 1; - - timestamp = find_bound_value(values, count, "timestamp"); - assert(timestamp->type == SQLITE_INT64); - - ret = sqlite3_bind_int64(stmt, index, timestamp->integer64); - index++; - - if (ret != SQLITE_OK) - { - fprintf(stderr, "Can't bind value for parameter nb %d in '%s' (ret=%d): %s\n", - index - 1, sql, ret, sqlite3_errmsg(db)); - goto gdcsui_exit; - } - - for (i = 0; i < count; i++) - { - if (strcmp(values[i].name, "timestamp") == 0) - continue; - - switch (values[i].type) + if (&values[i] == flags_ptr) { - case SQLITE_BOOLEAN: - ret = sqlite3_bind_int(stmt, index, values[i].boolean); - index++; - break; - - case SQLITE_INTEGER: - ret = sqlite3_bind_int(stmt, index, values[i].integer); - index++; - break; - - case SQLITE_INT64: - ret = sqlite3_bind_int64(stmt, index, values[i].integer64); - index++; - break; - - case SQLITE_TEXT: - ret = sqlite3_bind_text(stmt, index, values[i].string, -1, values[i].delete); - index++; - break; - - case SQLITE_NULL: - ret = sqlite3_bind_null(stmt, index); - index++; - break; - - default: - assert(false); - ret = SQLITE_ERROR; - break; - + assert(ucount < 1); + memcpy(&updates[ucount++], &values[i], sizeof(bound_value)); } - - if (ret != SQLITE_OK) + else { - fprintf(stderr, "Can't bind value for parameter nb %d in '%s' (ret=%d): %s\n", - index - 1, sql, ret, sqlite3_errmsg(db)); - goto gdcsui_exit; + assert(ccount < (count - 1)); + memcpy(&conds[ccount++], &values[i], sizeof(bound_value)); } - - } - - /* Exécution finale */ - - ret = sqlite3_step(stmt); - - if (ret != SQLITE_DONE) - { - fprintf(stderr, "UPDATE statement '%s' didn't return DONE (ret=%d): %s\n", sql, ret, sqlite3_errmsg(db)); - goto gdcsui_exit; } - sqlite3_finalize(stmt); - - result = true; - - gdcsui_exit: - - free(sql); + result = update_db_values(db, collec->name, updates, ucount, conds, ccount); - gdcsui_building_values: + building_values: free_all_bound_values(values, count); |