From 0247c6063c5dc9e592e48fc85c807893d8b34033 Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Tue, 17 Dec 2019 19:02:53 -0500 Subject: Separate dbBindText wrappers to allow for transient --- database.h | 52 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 10 deletions(-) (limited to 'database.h') diff --git a/database.h b/database.h index 4ca9c81..ad0d0ba 100644 --- a/database.h +++ b/database.h @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -107,30 +108,61 @@ dbPrepare(sqlite3 *db, unsigned flags, const char *sql) { return stmt; } -static inline void -dbBindText(sqlite3_stmt *stmt, const char *param, const char *text, int len) { +static inline int dbParam(sqlite3_stmt *stmt, const char *param) { int index = sqlite3_bind_parameter_index(stmt, param); - if (!index) errx(EX_SOFTWARE, "no such parameter %s", param); - int error = sqlite3_bind_text(stmt, index, text, len, NULL); - if (!error) return; + if (index) return index; errx( - EX_SOFTWARE, "sqlite3_bind_text: %s", + EX_SOFTWARE, "no such parameter %s: %s", + param, sqlite3_sql(stmt) + ); +} + +static inline void dbBindNull(sqlite3_stmt *stmt, const char *param) { + if (!sqlite3_bind_null(stmt, dbParam(stmt, param))) return; + errx( + EX_SOFTWARE, "sqlite3_bind_null: %s", sqlite3_errmsg(sqlite3_db_handle(stmt)) ); } static inline void dbBindInt(sqlite3_stmt *stmt, const char *param, int64_t value) { - int index = sqlite3_bind_parameter_index(stmt, param); - if (!index) errx(EX_SOFTWARE, "no such parameter %s", param); - int error = sqlite3_bind_int64(stmt, index, value); - if (!error) return; + if (!sqlite3_bind_int64(stmt, dbParam(stmt, param), value)) return; errx( EX_SOFTWARE, "sqlite3_bind_int64: %s", sqlite3_errmsg(sqlite3_db_handle(stmt)) ); } +static inline void dbBindText5( + sqlite3_stmt *stmt, const char *param, + const char *text, int len, bool copy +) { + int error = sqlite3_bind_text( + stmt, dbParam(stmt, param), text, len, (copy ? SQLITE_TRANSIENT : NULL) + ); + if (!error) return; + errx( + EX_SOFTWARE, "sqlite3_bind_text: %s", + sqlite3_errmsg(sqlite3_db_handle(stmt)) + ); +} + +static inline void +dbBindText(sqlite3_stmt *stmt, const char *param, const char *text) { + dbBindText5(stmt, param, text, -1, false); +} + +static inline void +dbBindTextLen(sqlite3_stmt *stmt, const char *param, const char *text, int len) { + dbBindText5(stmt, param, text, len, false); +} + +static inline void +dbBindTextCopy(sqlite3_stmt *stmt, const char *param, const char *text) { + dbBindText5(stmt, param, text, -1, true); +} + static inline int dbStep(sqlite3_stmt *stmt) { int error = sqlite3_step(stmt); if (error == SQLITE_ROW || error == SQLITE_DONE) return error; -- cgit 1.4.1