diff db.c @ 5:566bc028cdcb

tests: initial tests coming - Add tests for database access. - While here transform common code into libsci.a
author David Demelier <markand@malikania.fr>
date Wed, 16 Jun 2021 13:25:42 +0200
parents 9c4fea43803c
children 8c408176d2b1
line wrap: on
line diff
--- a/db.c	Tue Jun 15 20:42:55 2021 +0200
+++ b/db.c	Wed Jun 16 13:25:42 2021 +0200
@@ -1,3 +1,21 @@
+/*
+ * db.c -- scid database access
+ *
+ * Copyright (c) 2021 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
 #include <sys/queue.h>
 #include <assert.h>
 #include <stdlib.h>
@@ -78,7 +96,7 @@
 		free(s);
 	}
 
-	SLIST_INIT(l);
+	free(l);
 }
 
 static void
@@ -135,6 +153,7 @@
 
 	sqlite3_stmt *stmt = NULL;
 	va_list ap;
+	int ret = -1;
 
 	if (sqlite3_prepare(db, sql, -1, &stmt, NULL) != SQLITE_OK)
 		return log_warn("db: %s", sqlite3_errmsg(db)), -1;
@@ -143,13 +162,14 @@
 	vbind(stmt, fmt, ap);
 	va_end(ap);
 
-	if (sqlite3_step(stmt) != SQLITE_DONE) {
+	if (sqlite3_step(stmt) != SQLITE_DONE)
 		log_warn("db: %s", sqlite3_errmsg(db));
-		sqlite3_finalize(stmt);
-		return -1;
-	}
+	else
+		ret = sqlite3_last_insert_rowid(db);
 
-	return sqlite3_last_insert_rowid(db);
+	sqlite3_finalize(stmt);
+
+	return ret;
 }
 
 static ssize_t
@@ -194,18 +214,14 @@
 {
 	assert(path);
 
-	if (sqlite3_open(path, &db) != SQLITE_OK) {
-		log_warn("db: open error: %s", sqlite3_errmsg(db));
-		return -1;
-	}
+	if (sqlite3_open(path, &db) != SQLITE_OK)
+		return log_warn("db: open error: %s", sqlite3_errmsg(db)), -1;
 
 	/* Wait for 30 seconds to lock the database. */
 	sqlite3_busy_timeout(db, 30000);
 
-	if (sqlite3_exec(db, CHAR(sql_init), NULL, NULL, NULL) != SQLITE_OK) {
-		log_warn("db: initialization error: %s", sqlite3_errmsg(db));
-		return -1;
-	}
+	if (sqlite3_exec(db, CHAR(sql_init), NULL, NULL, NULL) != SQLITE_OK)
+		return log_warn("db: initialization error: %s", sqlite3_errmsg(db)), -1;
 
 	return 0;
 }
@@ -295,7 +311,7 @@
 		.ctx = ctx
 	};
 
-	return list(&sel, CHAR(sql_worker_find), "s", wk->name);
+	return list(&sel, CHAR(sql_worker_find), "s", wk->name) == 1 ? 0 : -1;
 }
 
 int
@@ -309,7 +325,7 @@
 		.ctx = ctx
 	};
 
-	return list(&sel, CHAR(sql_worker_find_id), "i", wk->id);
+	return list(&sel, CHAR(sql_worker_find_id), "i", wk->id) == 1 ? 0 : -1;
 }
 
 int