commit 1c5d537e98f90c9b479d772c65c39bcbebe14685
parent 5c557330970b6adee42c154bb8a2179a38e5fd48
Author: olikru <olikru@tkruger.se>
Date: Thu, 2 May 2024 23:44:53 +0200
refactored dbtool
Diffstat:
| M | fmpzio.c | | | 5 | +++++ |
| M | tools/dbtool.c | | | 105 | +++++++++++++++++++++++++++++++++++-------------------------------------------- |
2 files changed, 52 insertions(+), 58 deletions(-)
diff --git a/fmpzio.c b/fmpzio.c
@@ -125,6 +125,11 @@ read_hex_lines(fmpz **v)
}
}
+ // ignore last if it is the empty string
+ if(tokens[ntokens][0] == '\0') {
+ ntokens--;
+ }
+
*v = _fmpz_vec_init(ntokens);
if (*v == NULL) {
diff --git a/tools/dbtool.c b/tools/dbtool.c
@@ -16,6 +16,15 @@
enum command { INSERT, CREATE, NONE };
+const char* layout[][2] = {
+ { "factor", "modulus TEXT, factor TEXT, callback INTEGER" },
+ { "same", "id INTEGER PRIMARY KEY, row INTEGER" },
+ { "products", "id INTEGER PRIMARY KEY, product TEXT" },
+ { "tree", "node_id INTEGER, left_row INTEGER, right_row INTEGER" },
+ { "forest", "root_row INTEGER" },
+ { NULL, NULL }
+};
+
static void
usage(char *c)
{
@@ -31,65 +40,45 @@ static void
create_tables(sqlite3 *db)
{
int rc;
- // create a moduli table
sqlite3_stmt *ppStmt;
- rc = sqlite3_prepare(
- db, "CREATE TABLE moduli (product_id INTEGER, modulus BLOB);", -1,
- &ppStmt, NULL);
-
- if (rc != SQLITE_OK) {
- errx(EXIT_FAILURE, "prepare statement error: %s\n",
- sqlite3_errmsg(db));
- } else {
- fprintf(stderr, "Prepared statement successfully\n");
- }
-
- while ((rc = sqlite3_step(ppStmt)) == SQLITE_BUSY) {
- fprintf(stderr, "Busy, sleeping...\n");
- sleep(1);
- }
-
- if (rc != SQLITE_DONE) {
- errx(EXIT_FAILURE, "step error: %s\n", sqlite3_errmsg(db));
- } else {
- fprintf(stderr, "Created moduli table\n");
- }
-
- rc = sqlite3_finalize(ppStmt);
-
- if (rc != SQLITE_OK) {
- errx(EXIT_FAILURE, "finalize prepared statement error: %s\n",
- sqlite3_errmsg(db));
- }
-
- // create a product_trees table
- rc = sqlite3_prepare(
- db, "CREATE TABLE product_trees (n_factors INTEGER, tree BLOB);",
- -1, &ppStmt, NULL);
-
- if (rc != SQLITE_OK) {
- errx(EXIT_FAILURE, "prepare statement error: %s\n",
- sqlite3_errmsg(db));
- } else {
- fprintf(stderr, "Prepared statement successfully\n");
- }
-
- while ((rc = sqlite3_step(ppStmt)) == SQLITE_BUSY) {
- fprintf(stderr, "Busy, sleeping...\n");
- sleep(1);
- }
-
- if (rc != SQLITE_DONE) {
- errx(EXIT_FAILURE, "step error: %s\n", sqlite3_errmsg(db));
- } else {
- fprintf(stderr, "Created product_tree table\n");
- }
-
- rc = sqlite3_finalize(ppStmt);
-
- if (rc != SQLITE_OK) {
- errx(EXIT_FAILURE, "finalize prepared statement error: %s\n",
- sqlite3_errmsg(db));
+ char buffer[4096];
+
+ size_t i = 0;
+ while (layout[i][0] != NULL) {
+ fprintf(stderr, "[+] Creating the \"%s\" table...\n", layout[i][0]);
+
+ snprintf(buffer, 4096, "CREATE TABLE %s (%s);", layout[i][0], layout[i][1]);
+
+ rc = sqlite3_prepare(db, buffer, -1, &ppStmt, NULL);
+
+ if (rc != SQLITE_OK) {
+ errx(EXIT_FAILURE, "prepare statement error: %s\n",
+ sqlite3_errmsg(db));
+ } else {
+ fprintf(stderr, " [-] Prepared statement successfully\n");
+ }
+
+ while ((rc = sqlite3_step(ppStmt)) == SQLITE_BUSY) {
+ fprintf(stderr, " [-] Busy, sleeping...\n");
+ sleep(1);
+ }
+
+ if (rc != SQLITE_DONE) {
+ errx(EXIT_FAILURE, "step error: %s\n", sqlite3_errmsg(db));
+ } else {
+ fprintf(stderr, " [-] Table creation returned okay\n");
+ }
+
+ rc = sqlite3_finalize(ppStmt);
+
+ if (rc != SQLITE_OK) {
+ errx(EXIT_FAILURE, "finalize prepared statement error: %s\n",
+ sqlite3_errmsg(db));
+ }
+
+ fprintf(stderr, "[+] Done creating the factor table");
+
+ i++;
}
}