diff database.c @ 44:f939d7864de4

pasterd: fix unique key constraint
author David Demelier <markand@malikania.fr>
date Fri, 14 Feb 2020 14:55:59 +0100
parents a2789cb8ebe6
children 520f57836ff3
line wrap: on
line diff
--- a/database.c	Fri Feb 14 14:19:17 2020 +0100
+++ b/database.c	Fri Feb 14 14:55:59 2020 +0100
@@ -135,16 +135,14 @@
 	assert(id);
 
 	sqlite3_stmt *stmt = NULL;
-	bool ret = false;
+	bool ret = true;
 
-	if (sqlite3_prepare(db, sql_get, -1, &stmt, NULL) != SQLITE_OK) {
-		log_warn("database: error (exists): %s", sqlite3_errmsg(db));
-		return false;
+	if (sqlite3_prepare(db, sql_get, -1, &stmt, NULL) == SQLITE_OK) {
+		sqlite3_bind_text(stmt, 1, id, -1, NULL);
+		ret = sqlite3_step(stmt) == SQLITE_ROW;
+		sqlite3_finalize(stmt);
 	}
 
-	ret = sqlite3_step(stmt) == SQLITE_ROW;
-	sqlite3_finalize(stmt);
-
 	return ret;
 }
 
@@ -169,6 +167,9 @@
 
 	/*
 	 * Avoid infinite loop, we only try to create a new id in 30 steps.
+	 *
+	 * On error, the function `exist` returns true to indicate we should
+	 * not try to save with that id.
 	 */
 	int tries = 0;