summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNeels Hofmeyr <neels@hofmeyr.de>2017-10-06 03:44:57 +0200
committerNeels Hofmeyr <neels@hofmeyr.de>2017-10-11 22:28:09 +0200
commit28da26ec191b176028c6bad6a43749a8c8926cb2 (patch)
tree36e9f12c8cb813af6f3a3dcee1ce6482cf60dad5 /src
parentcd83b8a44c2824cfc9dfdca685fea28f9221f60e (diff)
add db_bind_int() and db_bind_int64()
Diffstat (limited to 'src')
-rw-r--r--src/db.c44
-rw-r--r--src/db.h2
2 files changed, 46 insertions, 0 deletions
diff --git a/src/db.c b/src/db.c
index cc45db3..463eeb4 100644
--- a/src/db.c
+++ b/src/db.c
@@ -105,6 +105,50 @@ bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text)
return true;
}
+/** bind int arg and do proper cleanup in case of failure. If param_name is
+ * NULL, bind to the first parameter (useful for SQL statements that have only
+ * one parameter). */
+bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr)
+{
+ int rc;
+ int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
+ if (idx < 1) {
+ LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
+ param_name);
+ return false;
+ }
+ rc = sqlite3_bind_int(stmt, idx, nr);
+ if (rc != SQLITE_OK) {
+ LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
+ param_name ? param_name : "#1", rc);
+ db_remove_reset(stmt);
+ return false;
+ }
+ return true;
+}
+
+/** bind int64 arg and do proper cleanup in case of failure. If param_name is
+ * NULL, bind to the first parameter (useful for SQL statements that have only
+ * one parameter). */
+bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr)
+{
+ int rc;
+ int idx = param_name ? sqlite3_bind_parameter_index(stmt, param_name) : 1;
+ if (idx < 1) {
+ LOGP(DDB, LOGL_ERROR, "Error composing SQL, cannot bind parameter '%s'\n",
+ param_name);
+ return false;
+ }
+ rc = sqlite3_bind_int64(stmt, idx, nr);
+ if (rc != SQLITE_OK) {
+ LOGP(DDB, LOGL_ERROR, "Error binding int64 to SQL parameter %s: %d\n",
+ param_name ? param_name : "#1", rc);
+ db_remove_reset(stmt);
+ return false;
+ }
+ return true;
+}
+
void db_close(struct db_context *dbc)
{
unsigned int i;
diff --git a/src/db.h b/src/db.h
index 6d6723a..533c4d2 100644
--- a/src/db.h
+++ b/src/db.h
@@ -24,6 +24,8 @@ struct db_context {
bool db_remove_reset(sqlite3_stmt *stmt);
bool db_bind_text(sqlite3_stmt *stmt, const char *param_name, const char *text);
+bool db_bind_int(sqlite3_stmt *stmt, const char *param_name, int nr);
+bool db_bind_int64(sqlite3_stmt *stmt, const char *param_name, int64_t nr);
void db_close(struct db_context *dbc);
struct db_context *db_open(void *ctx, const char *fname);