commit 22e1b88675c8d9b3186b24625f981df72b620396
parent 63f2128090e0906f6e77df62e1cc6f207f2ada6d
Author: olikru <olikru@tkruger.se>
Date: Tue, 16 Apr 2024 14:32:26 +0200
working on sqlite3 dbtool
Diffstat:
3 files changed, 146 insertions(+), 46 deletions(-)
diff --git a/Makefile b/Makefile
@@ -28,7 +28,7 @@ wiener.o
TOOLS=\
hnpsolve\
lcgfloyd\
-dbcreate\
+dbtool\
pierre\
varmkorv
PRECOMPUTERS=\
@@ -71,8 +71,8 @@ pierre: build $(OBJS)
varmkorv: build $(OBJS)
$(CC) -I. $(CFLAGS) -o $(BUILD)/tools/varmkorv $(TOOLS_DIR)/varmkorv.c $(BUILD_OBJS) $(LDFLAGS)
-dbcreate: build $(OBJS)
- $(CC) -I. $(CFLAGS) -o $(BUILD)/tools/dbcreate $(TOOLS_DIR)/dbcreate.c $(BUILD_OBJS) $(LDFLAGS) -lsqlite3
+dbtool: build $(OBJS)
+ $(CC) -I. $(CFLAGS) -o $(BUILD)/tools/dbtool $(TOOLS_DIR)/dbtool.c $(BUILD_OBJS) $(LDFLAGS) -lsqlite3
# -- precomputers
diff --git a/tools/dbcreate.c b/tools/dbcreate.c
@@ -1,43 +0,0 @@
-/**
- * Create an SQLite database, which uses serialized mode
- * to prevent horrible file corruption issues.
- */
-
-#include <err.h>
-#include <sqlite3.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-static void
-usage(char *c)
-{
- printf("Usage: %s <db>\n"
- " where\n"
- "<db> path to database to create\n");
- exit(1);
-}
-
-int
-main(int argc, char *argv[])
-{
- sqlite3 *db;
- int rc;
-
- if (argc != 2) {
- usage(argv[0]);
- }
-
- rc = sqlite3_open(argv[1], &db);
-
- if (rc) {
- errx(EXIT_FAILURE, "database opening error: %s\n",
- sqlite3_errmsg(db));
- } else {
- fprintf(stderr, "Opened database successfully\n");
- }
-
- while (sqlite3_close(db) != SQLITE_OK) {
- }
-
- return 0;
-}
diff --git a/tools/dbtool.c b/tools/dbtool.c
@@ -0,0 +1,143 @@
+/**
+ * Tool for interacting with the database.
+ */
+
+#include <err.h>
+#include <sqlite3.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+enum command { INSERT, CREATE, NONE };
+
+static void
+usage(char *c)
+{
+ printf("Usage: %s <db> <command> [..]\n"
+ " where\n"
+ "<db> path to databasee\n"
+ "<command> command to use\n",
+ c);
+ exit(1);
+}
+
+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));
+ }
+
+
+}
+
+static enum command
+get_command(char *str)
+{
+ if (strcmp(str, "insert") == 0) {
+ return INSERT;
+ } else if (strcmp(str, "create") == 0) {
+ return CREATE;
+ } else {
+ return NONE;
+ }
+}
+
+int
+main(int argc, char *argv[])
+{
+ sqlite3 *db;
+ int rc;
+
+ if (argc < 3) {
+ usage(argv[0]);
+ }
+
+ enum command com = get_command(argv[2]);
+ if (com == NONE) {
+ fprintf(stderr, "Unknown command: %s\n\n", argv[2]);
+ usage(argv[0]);
+ }
+
+ rc = sqlite3_open(argv[1], &db);
+
+ if (rc) {
+ errx(EXIT_FAILURE, "database opening error: %s\n",
+ sqlite3_errmsg(db));
+ } else {
+ fprintf(stderr, "Opened database successfully\n");
+ }
+
+ switch (com) {
+ case CREATE:
+ create_tables(db);
+ break;
+ case INSERT:
+ fprintf(stderr, "insert not implemented\n");
+ break;
+ default:
+ break;
+ }
+
+ while (sqlite3_close(db) != SQLITE_OK) {
+ }
+
+ return 0;
+}