From de04b3c2e8c2018d5d951515f89df82de7bb76ae Mon Sep 17 00:00:00 2001 From: AlexZ Date: Thu, 6 Feb 2014 09:53:08 -0600 Subject: [PATCH] added wrapper for sqlite3_create_function_v2 --- src/Database.cpp | 18 ++++++++++++++++++ src/Database.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/Database.cpp b/src/Database.cpp index a13dbbf..9d1e6df 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -14,6 +14,9 @@ #include "Assertion.h" #include "Exception.h" +#ifndef SQLITE_DETERMINISTIC +#define SQLITE_DETERMINISTIC 0x800 +#endif //SQLITE_DETERMINISTIC namespace SQLite { @@ -96,6 +99,21 @@ void Database::check(const int aRet) const // throw(SQLite::Exception) throw SQLite::Exception(sqlite3_errmsg(mpSQLite)); } } + +// Attach a custom function to your sqlite database. +// assumes UTF8 text representation. +// Parameter details can be found here: http://www.sqlite.org/c3ref/create_function.html +void Database::createFunction(const char *funcName, int nArg, bool deterministic, void *pApp, void (*xFunc)(sqlite3_context *, int, sqlite3_value **), void (*xStep)(sqlite3_context *, int, sqlite3_value **), void (*xFinal)(sqlite3_context *), void (*xDestroy)(void *)) +{ + int eTextRep = SQLITE_UTF8; + // optimization if deterministic function... e.g. of non deterministic function (random()) + if (deterministic) { + eTextRep = eTextRep|SQLITE_DETERMINISTIC; + } + int ret = sqlite3_create_function_v2(mpSQLite, funcName, nArg, eTextRep, pApp, xFunc, xStep, xFinal, xDestroy); + + check(ret); +} } // namespace SQLite diff --git a/src/Database.h b/src/Database.h index a7cfca7..22aed69 100644 --- a/src/Database.h +++ b/src/Database.h @@ -229,6 +229,50 @@ public: { return sqlite3_errmsg(mpSQLite); } + + /** + * @brief Create a function in the sqlite database. + * + * This is the equivalent of the sqlite3_create_function_v2 command. + * NOTE: UTF-8 text encoding assumed. + * + * + * @param[in] funcName Name of the SQL function to be created or redefined + * @param[in] nArg Number of arguments in the function + * @param[in] determininistic optimize for deterministic functions. Most sqlite functions are deterministic. + * a random number generator is an example of an indeterministic function. + * @param[in] pApp See: http://www.sqlite.org/c3ref/create_function.html + * @param[in] xFunc See: http://www.sqlite.org/c3ref/create_function.html + * @param[in] xStep See: http://www.sqlite.org/c3ref/create_function.html + * @param[in] xFinal See: http://www.sqlite.org/c3ref/create_function.html + * @param[in] xDestroy See: http://www.sqlite.org/c3ref/create_function.html + * + * @throw SQLite::Exception in case of error + */ + void createFunction(const char *funcName, int nArg, bool deterministic, void *pApp, void (*xFunc)(sqlite3_context *, int, sqlite3_value **), void (*xStep)(sqlite3_context *, int, sqlite3_value **), void (*xFinal)(sqlite3_context *), void (*xDestroy)(void *)); + /** + * @brief Create a function in the sqlite database. + * + * This is the equivalent of the sqlite3_create_function_v2 command. + * NOTE: UTF-8 text encoding assumed. + * + * + * @param[in] funcName Name of the SQL function to be created or redefined + * @param[in] nArg Number of arguments in the function + * @param[in] determininistic optimize for deterministic functions. Most sqlite functions are deterministic. + * a random number generator is an example of an indeterministic function. + * @param[in] pApp See: http://www.sqlite.org/c3ref/create_function.html + * @param[in] xFunc See: http://www.sqlite.org/c3ref/create_function.html + * @param[in] xStep See: http://www.sqlite.org/c3ref/create_function.html + * @param[in] xFinal See: http://www.sqlite.org/c3ref/create_function.html + * @param[in] xDestroy See: http://www.sqlite.org/c3ref/create_function.html + * + * @throw SQLite::Exception in case of error + */ + inline void createFunction(const std::string &funcName, int nArg, bool deterministic, void *pApp, void (*xFunc)(sqlite3_context *, int, sqlite3_value **), void (*xStep)(sqlite3_context *, int, sqlite3_value **), void (*xFinal)(sqlite3_context *), void (*xDestroy)(void *)) + { + return createFunction(funcName.c_str(), nArg, deterministic, pApp, xFunc, xStep, xFinal, xDestroy); + } private: /// @{ Database must be non-copyable