Merge pull request #15 from saaqibz/master

Added wrapper for sqlite3_create_function_v2
This commit is contained in:
Sébastien Rombauts 2014-02-06 18:02:37 +01:00
commit 5095b086e1
2 changed files with 62 additions and 0 deletions

View File

@ -14,6 +14,9 @@
#include "Assertion.h"
#include "Exception.h"
#ifndef SQLITE_DETERMINISTIC
#define SQLITE_DETERMINISTIC 0x800
#endif //SQLITE_DETERMINISTIC
namespace SQLite
{
@ -97,5 +100,20 @@ void Database::check(const int aRet) const // throw(SQLite::Exception)
}
}
// 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

View File

@ -230,6 +230,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
Database(const Database&);