Beispiel #1
0
void registerEncodingExtensions(sqlite3* db) {
  sqlite3_create_function(db,
                          "conditional_to_base64",
                          1,
                          SQLITE_UTF8 | SQLITE_DETERMINISTIC,
                          nullptr,
                          sqliteB64ConditionalEncFunc,
                          nullptr,
                          nullptr);
  sqlite3_create_function(db,
                          "to_base64",
                          1,
                          SQLITE_UTF8 | SQLITE_DETERMINISTIC,
                          nullptr,
                          sqliteB64EncFunc,
                          nullptr,
                          nullptr);
  sqlite3_create_function(db,
                          "from_base64",
                          1,
                          SQLITE_UTF8 | SQLITE_DETERMINISTIC,
                          nullptr,
                          sqliteB64DecFunc,
                          nullptr,
                          nullptr);
}
/*
** Set up SQL objects in database db used to access the contents of
** the hash table pointed to by argument pHash. The hash table must
** been initialised to use string keys, and to take a private copy 
** of the key when a value is inserted. i.e. by a call similar to:
**
**    sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
**
** This function adds a scalar function (see header comment above
** scalarFunc() in this file for details) and, if ENABLE_TABLE is
** defined at compilation time, a temporary virtual table (see header 
** comment above struct HashTableVtab) to the database schema. Both 
** provide read/write access to the contents of *pHash.
**
** The third argument to this function, zName, is used as the name
** of both the scalar and, if created, the virtual table.
*/
int sqlite3Fts3InitHashTable(
  sqlite3 *db, 
  fts3Hash *pHash, 
  const char *zName
){
  int rc = SQLITE_OK;
  void *p = (void *)pHash;
  const int any = SQLITE_ANY;
  char *zTest = 0;
  char *zTest2 = 0;

#ifdef SQLITE_TEST
  void *pdb = (void *)db;
  zTest = sqlite3_mprintf("%s_test", zName);
  zTest2 = sqlite3_mprintf("%s_internal_test", zName);
  if( !zTest || !zTest2 ){
    rc = SQLITE_NOMEM;
  }
#endif

  if( rc!=SQLITE_OK
   || (rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0))
   || (rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0))
#ifdef SQLITE_TEST
   || (rc = sqlite3_create_function(db, zTest, 2, any, p, testFunc, 0, 0))
   || (rc = sqlite3_create_function(db, zTest, 3, any, p, testFunc, 0, 0))
   || (rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0))
#endif
  );

  sqlite3_free(zTest);
  sqlite3_free(zTest2);
  return rc;
}
Beispiel #3
0
static int registerTestFunctions(sqlite3 *db){
  static const struct {
     char *zName;
     signed char nArg;
     unsigned char eTextRep; /* 1: UTF-16.  0: UTF-8 */
     void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
  } aFuncs[] = {
    { "randstr",               2, SQLITE_UTF8, randStr    },
    { "test_destructor",       1, SQLITE_UTF8, test_destructor},
#ifndef SQLITE_OMIT_UTF16
    { "test_destructor16",     1, SQLITE_UTF8, test_destructor16},
    { "hex_to_utf16be",        1, SQLITE_UTF8, testHexToUtf16be},
    { "hex_to_utf16le",        1, SQLITE_UTF8, testHexToUtf16le},
#endif
    { "hex_to_utf8",           1, SQLITE_UTF8, testHexToUtf8},
    { "test_destructor_count", 0, SQLITE_UTF8, test_destructor_count},
    { "test_auxdata",         -1, SQLITE_UTF8, test_auxdata},
    { "test_error",            1, SQLITE_UTF8, test_error},
    { "test_error",            2, SQLITE_UTF8, test_error},
    { "test_eval",             1, SQLITE_UTF8, test_eval},
    { "test_isolation",        2, SQLITE_UTF8, test_isolation},
    { "test_counter",          1, SQLITE_UTF8, counterFunc},
  };
  int i;

  for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
    sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg,
        aFuncs[i].eTextRep, 0, aFuncs[i].xFunc, 0, 0);
  }

  sqlite3_create_function(db, "test_agg_errmsg16", 0, SQLITE_ANY, 0, 0, 
      test_agg_errmsg16_step, test_agg_errmsg16_final);
      
  return SQLITE_OK;
}
jint Java_com_tencent_mobileqq_persistence_FTSDatatbaseDao_initFTS(JNIEnv* env, jobject thiz)
{
    int errCode = sqlite3_open_v2(DB_FILE, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    if (SQLITE_OK != errCode)
    {
        logError("Can't open database IndexQQMsg.db, ", sqlite3_errmsg(db));

        sqlite3_close(db);
        return errCode;
    }

    errCode = sqlite3_exec(db, "PRAGMA cache_size=4000;", NULL, NULL, NULL);
    if (SQLITE_OK != errCode)
    {
        logError("Can't set PRAGMA cache_size = 4000, ", sqlite3_errmsg(db));

        sqlite3_close(db);
        return errCode;
    }

    errCode = sqlite3_create_function(db, "qqcompress", 1, SQLITE_UTF8, NULL, qqcompress, NULL, NULL);
    if (SQLITE_OK != errCode)
    {
        logError("Can't create function, ", sqlite3_errmsg(db));

        sqlite3_close(db);
        return errCode;
    }

    errCode = sqlite3_create_function(db, "qquncompress", 1, SQLITE_UTF8, NULL, qquncompress, NULL, NULL);
    if (SQLITE_OK != errCode)
    {
        logError("Can't create function, ", sqlite3_errmsg(db));

        sqlite3_close(db);
        return errCode;
    }

    char* sql = "CREATE VIRTUAL TABLE IF NOT EXISTS IndexMsg USING fts4(type, content, contentindex, oid, ext1, ext2,ext3,exts, compress=qqcompress, uncompress=qquncompress);";
    // char* sql = "CREATE VIRTUAL TABLE IF NOT EXISTS IndexMsg USING fts4(type, content, contentindex, oid, ext1, ext2,ext3,exts);";
    errCode = sqlite3_exec(db, sql, NULL, NULL, NULL);
    if (SQLITE_OK != errCode)
    {
        logError("Can't create virtual table IndexMsg, ", sqlite3_errmsg(db));

        sqlite3_close(db);
        return errCode;
    }

#ifdef ZIP
    const char* version = zlibVersion();
    logInfo("FTS init zlib version = ", version);
#endif

    logInfo("FTS init...", NULL);
    return 0;
}
Beispiel #5
0
/*
** Register the SQL functions.
*/
static int cflRegister(sqlite3 *db){
  int rc = sqlite3_create_function(
      db, "sqlite_readint32", -1, SQLITE_UTF8, 0, readint_function, 0, 0
  );
  if( rc!=SQLITE_OK ) return rc;
  rc = sqlite3_create_function(
      db, "checkfreelist", 1, SQLITE_UTF8, 0, checkfreelist_function, 0, 0
  );
  return rc;
}
Beispiel #6
0
int db_get_table(char *command)
{
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;

#if (GTKVER == 3)
    G_LOCK(table_mutex);
#else
    g_static_mutex_lock(&table_mutex);
#endif
    if (tablep) {
        g_print("ERROR: TABLE NOT CLOSED BEFORE OPENING!\n");
        sqlite3_free_table(tablep);
    }
    tablep = NULL;

    G_LOCK(db);

    rc = sqlite3_open(db_name, &db);
    if (rc) {
        fprintf(stderr, "Can't open database: %s\n", 
                sqlite3_errmsg(db));
        G_UNLOCK(db);
#if (GTKVER == 3)
        G_UNLOCK(table_mutex);
#else
        g_static_mutex_unlock(&table_mutex);
#endif
        return -1;
    }

    sqlite3_create_function(db, "utf8error", 1, SQLITE_UTF8, NULL, utf8error, NULL, NULL);
    sqlite3_create_function(db, "getweight", 1, SQLITE_UTF8, NULL, getweight, NULL, NULL);

    //g_print("\nSQL: %s:\n  %s\n", db_name, cmd);
    rc = sqlite3_get_table(db, command, &tablep, &tablerows, &tablecols, &zErrMsg);
    if (rc != SQLITE_OK && zErrMsg) {
        g_print("SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
        sqlite3_close(db);
        G_UNLOCK(db);
#if (GTKVER == 3)
        G_UNLOCK(table_mutex);
#else
        g_static_mutex_unlock(&table_mutex);
#endif
        return -1;
    }

    sqlite3_close(db);
    G_UNLOCK(db);

    return tablerows;
}
Beispiel #7
0
/*
** Register the query expression parser test function fts3_exprtest() 
** with database connection db. 
*/
int sqlite3Fts3ExprInitTestInterface(sqlite3* db){
  int rc = sqlite3_create_function(
      db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0
  );
  if( rc==SQLITE_OK ){
    rc = sqlite3_create_function(db, "fts3_exprtest_rebalance", 
        -1, SQLITE_UTF8, (void *)1, fts3ExprTest, 0, 0
    );
  }
  return rc;
}
int register_sqlite_funcs(sqlite3 *db, sqlite_registration_func_t *reg_funcs)
{
    int lpc;

    assert(db != NULL);
    assert(reg_funcs != NULL);
    
    for (lpc = 0; reg_funcs[lpc]; lpc++) {
        const struct FuncDef *basic_funcs = NULL;
        const struct FuncDefAgg *agg_funcs = NULL;
        int i;

        reg_funcs[lpc](&basic_funcs, &agg_funcs);

        for (i = 0; basic_funcs && basic_funcs[i].zName; i++) {
            void *pArg = 0;

            switch (basic_funcs[i].argType) {
                case 1: pArg = db; break;
                case 2: pArg = (void *)(-1); break;
            }
            //sqlite3CreateFunc
            /* LMH no error checking */
            sqlite3_create_function(db,
                                    basic_funcs[i].zName,
                                    basic_funcs[i].nArg,
                                    basic_funcs[i].eTextRep,
                                    pArg,
                                    basic_funcs[i].xFunc,
                                    0,
                                    0);
        }

        for (i = 0; agg_funcs && agg_funcs[i].zName; i++) {
            void *pArg = 0;

            switch (agg_funcs[i].argType) {
                case 1: pArg = db; break;
                case 2: pArg = (void *)(-1); break;
            }
            //sqlite3CreateFunc
            sqlite3_create_function(db,
                                    agg_funcs[i].zName,
                                    agg_funcs[i].nArg,
                                    SQLITE_UTF8,
                                    pArg,
                                    0,
                                    agg_funcs[i].xStep,
                                    agg_funcs[i].xFinalize);
        }
    }

    return 0;
}
Beispiel #9
0
/*
** This is the "automatic extensionn" initializer that runs right after
** the connection to the repository database is opened.  Set up the
** database connection to be more useful to the human operator.
*/
static int sqlcmd_autoinit(
  sqlite3 *db,
  const char **pzErrMsg,
  const void *notUsed
){
  sqlite3_create_function(db, "content", 1, SQLITE_ANY, 0,
                          sqlcmd_content, 0, 0);
  sqlite3_create_function(db, "compress", 1, SQLITE_ANY, 0,
                          sqlcmd_compress, 0, 0);
  sqlite3_create_function(db, "decompress", 1, SQLITE_ANY, 0,
                          sqlcmd_decompress, 0, 0);
  return SQLITE_OK;
}
Beispiel #10
0
SQLITE_EXTENSION_INIT1

extern int sqlite3_extension_init(sqlite3                     *db,
                                  char                       **pzErrMsg,
                                  const sqlite3_api_routines  *pApi) {
  SQLITE_EXTENSION_INIT2(pApi)
  (void)setlocale(LC_ALL, "en_US.UTF-8");
  sqlite3_create_function(db, "tan", 1, SQLITE_UTF8,
                            0, ora_tan, 0, 0);
  // -1 means variable number of arguments
  sqlite3_create_function(db, "monthname", 1, SQLITE_UTF8,
                            0, my_monthname, 0, 0);
  return 0;
}
Beispiel #11
0
/*
** Extension load function.
*/
int testloadext_init(
  sqlite3 *db, 
  char **pzErrMsg, 
  const sqlite3_api_routines *pApi
){
  int nErr = 0;
  SQLITE_EXTENSION_INIT2(pApi);
  nErr |= sqlite3_create_function(db, "half", 1, SQLITE_ANY, 0, halfFunc, 0, 0);
  nErr |= sqlite3_create_function(db, "sqlite3_status", 1, SQLITE_ANY, 0,
                          statusFunc, 0, 0);
  nErr |= sqlite3_create_function(db, "sqlite3_status", 2, SQLITE_ANY, 0,
                          statusFunc, 0, 0);
  return nErr ? SQLITE_ERROR : SQLITE_OK;
}
Beispiel #12
0
/*
** Register the built-in LIKE and GLOB functions.  The caseSensitive
** parameter determines whether or not the LIKE operator is case
** sensitive.  GLOB is always case sensitive.
*/
void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
  struct compareInfo *pInfo;
  if( caseSensitive ){
    pInfo = (struct compareInfo*)&likeInfoAlt;
  }else{
    pInfo = (struct compareInfo*)&likeInfoNorm;
  }
  sqlite3_create_function(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
  sqlite3_create_function(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
  sqlite3_create_function(db, "glob", 2, SQLITE_UTF8, 
      (struct compareInfo*)&globInfo, likeFunc, 0,0);
  setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
  setLikeOptFlag(db, "like", 
      caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
}
Beispiel #13
0
int sqlite3_create_function16(
  sqlite3 *db,
  const void *zFunctionName,
  int nArg,
  int eTextRep,
  void *pUserData,
  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
  void (*xFinal)(sqlite3_context*)
){
  int rc;
  char const *zFunc8;
  sqlite3_value *pTmp;

  if( sqlite3SafetyCheck(db) ){
    return SQLITE_MISUSE;
  }
  pTmp = sqlite3GetTransientValue(db);
  sqlite3ValueSetStr(pTmp, -1, zFunctionName, SQLITE_UTF16NATIVE,SQLITE_STATIC);
  zFunc8 = sqlite3ValueText(pTmp, SQLITE_UTF8);

  if( !zFunc8 ){
    return SQLITE_NOMEM;
  }
  rc = sqlite3_create_function(db, zFunc8, nArg, eTextRep,
      pUserData, xFunc, xStep, xFinal);
  return rc;
}
Beispiel #14
0
/* call-seq: define_aggregator(name, aggregator)
 *
 * Define an aggregate function named +name+ using the object +aggregator+.
 * +aggregator+ must respond to +step+ and +finalize+.  +step+ will be called
 * with row information and +finalize+ must return the return value for the
 * aggregator function.
 */
static VALUE define_aggregator(VALUE self, VALUE name, VALUE aggregator)
{
  sqlite3RubyPtr ctx;
  int arity, status;

  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  arity = sqlite3_obj_method_arity(aggregator, rb_intern("step"));

  status = sqlite3_create_function(
    ctx->db,
    StringValuePtr(name),
    arity,
    SQLITE_UTF8,
    (void *)aggregator,
    NULL,
    rb_sqlite3_step,
    rb_sqlite3_final
  );

  rb_iv_set(self, "@agregator", aggregator);

  CHECK(ctx->db, status);

  return self;
}
Beispiel #15
0
bool HHVM_METHOD(SQLite3, createfunction,
                 const String& name,
                 const Variant& callback,
                 int64_t argcount /* = -1 */) {
  auto *data = Native::data<SQLite3>(this_);
  data->validate();
  if (name.empty()) {
    return false;
  }
  if (!is_callable(callback)) {
    raise_warning("Not a valid callback function %s",
                  callback.toString().data());
    return false;
  }

  auto udf = std::make_shared<SQLite3::UserDefinedFunc>();
  if (sqlite3_create_function(data->m_raw_db, name.data(), argcount,
                              SQLITE_UTF8, udf.get(), php_sqlite3_callback_func,
                              nullptr, nullptr) == SQLITE_OK) {
    udf->func = callback;
    udf->argc = argcount;
    data->m_udfs.push_back(udf);
    return true;
  }
  return false;
}
Beispiel #16
0
int sqlite3_eval_init(
  sqlite3 *db, 
  char **pzErrMsg, 
  const sqlite3_api_routines *pApi
){
  int rc = SQLITE_OK;
  SQLITE_EXTENSION_INIT2(pApi);
  (void)pzErrMsg;  /* Unused parameter */
  rc = sqlite3_create_function(db, "eval", 1, SQLITE_UTF8, 0,
                               sqlEvalFunc, 0, 0);
  if( rc==SQLITE_OK ){
    rc = sqlite3_create_function(db, "eval", 2, SQLITE_UTF8, 0,
                                 sqlEvalFunc, 0, 0);
  }
  return rc;
}
Beispiel #17
0
/* call-seq: define_function(name) { |args,...| }
 *
 * Define a function named +name+ with +args+.  The arity of the block
 * will be used as the arity for the function defined.
 */
static VALUE define_function(VALUE self, VALUE name)
{
  sqlite3RubyPtr ctx;
  VALUE block;
  int status;

  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  block = rb_block_proc();

  status = sqlite3_create_function(
    ctx->db,
    StringValuePtr(name),
    rb_proc_arity(block),
    SQLITE_UTF8,
    (void *)block,
    rb_sqlite3_func,
    NULL,
    NULL
  );

  CHECK(ctx->db, status);

  return self;
}
Beispiel #18
0
static int sfuserdata_mtgc(lua_State *l) {
	lua_settop(l, 1);	//[u]
	assert(lua_type(l, -1) == LUA_TUSERDATA);

	//clear the function
	lua_getuservalue(l, 1);	//[ut]
	lua_getfield(l, -1, "dbud");	//[utu]

	struct dbuserdata *uddb =
	 (struct dbuserdata *)lua_touserdata(l, 3);
	assert(uddb != NULL);
	assert(uddb->db != NULL);
	lua_pop(l, 1);	//[ut]

	lua_getfield(l, -1, "name");	//[uts]
	sqlite3_create_function(uddb->db, lua_tostring(l, -1),
	 -1, SQLITE_ANY, NULL, NULL, NULL, NULL);

	struct sfuserdata *ud =
	 (struct sfuserdata *)lua_touserdata(l, 1);
	if(ud->funcref != -1) {
		luaL_unref(l, LUA_REGISTRYINDEX, ud->funcref);
	}

	return 0;
}
Beispiel #19
0
static void
tracker_fts_register_functions (sqlite3 *db)
{
	sqlite3_create_function (db, "tracker_rank", 2, SQLITE_ANY,
	                         NULL, &function_rank,
	                         NULL, NULL);
	sqlite3_create_function (db, "tracker_offsets", 2, SQLITE_ANY,
	                         NULL, &function_offsets,
	                         NULL, NULL);
	sqlite3_create_function (db, "fts_column_weights", 0, SQLITE_ANY,
	                         NULL, &function_weights,
	                         NULL, NULL);
	sqlite3_create_function (db, "fts_property_names", 0, SQLITE_ANY,
	                         NULL, &function_property_names,
	                         NULL, NULL);
}
Beispiel #20
0
/* create function if pos is non-negative, aggregate if agg is true */
int create_function_helper(sqlite3 *db, const char *name, int pos, int agg)
{
    return sqlite3_create_function(db, name, -1, SQLITE_ANY, (void*)pos,
            pos>=0 && !agg ? &xFunc_helper : 0,
            pos>=0 &&  agg ? &xStep_helper : 0,
            pos>=0 &&  agg ? &xFinal_helper : 0);
}
Beispiel #21
0
bool PDOSqliteResource::createFunction(const String& name,
                                       const Variant& callback,
                                       int argcount) {
  if (!is_callable(callback)) {
    raise_warning("function '%s' is not callable", callback.toString().data());
    return false;
  }

  auto udf = req::make_unique<UDF>();

  udf->func = callback;
  udf->argc = argcount;
  udf->name = name.toCppString();

  auto stat = sqlite3_create_function(
    conn()->m_db,
    udf->name.c_str(),
    argcount,
    SQLITE_UTF8,
    static_cast<SQLite3::UserDefinedFunc*>(udf.get()),
    php_sqlite3_callback_func,
    nullptr,
    nullptr
  );

  if (stat != SQLITE_OK) {
    return false;
  }
  m_udfs.emplace_back(std::move(udf));

  return true;
}
Beispiel #22
0
bool PDOSqliteConnection::createFunction(const String& name,
                                         const Variant& callback,
                                         int argcount) {
  if (!is_callable(callback)) {
    raise_warning("function '%s' is not callable", callback.toString().data());
    return false;
  }

  auto udf = std::make_shared<UDF>();

  udf->func = callback;
  udf->argc = argcount;
  udf->name = name.toCppString();

  auto stat = sqlite3_create_function(m_db, udf->name.c_str(), argcount,
                                      SQLITE_UTF8, udf.get(),
                                      php_sqlite3_callback_func,
                                      nullptr, nullptr);
  if (stat != SQLITE_OK) {
    return false;
  }
  m_udfs.push_back(udf);

  return true;
}
Beispiel #23
0
void PDOSqliteConnection::clearFunctions() {
  for (auto& udf : m_udfs) {
    sqlite3_create_function(m_db, udf->name.data(), 0, SQLITE_UTF8,
                            nullptr, nullptr, nullptr, nullptr);
  }
  m_udfs.clear();
}
Beispiel #24
0
int main(int argc, char **argv)
{
    int rc;
    sqlite3 *db;
    const char* sql;

    sqlite3_open("test.db", &db);
    sqlite3_create_function( db, "function", -1, SQLITE_UTF8, NULL,
                             function, NULL, NULL);

    /* Turn on SQL logging */
    log_sql(db, 1);

    /* Call function with one text argument. */
    execute(db, "select function(1)");

    /* Call function with several arguments of various types. */
    execute(db, "select function(1, 2.71828)");

    /* Call function with variable arguments, the first argument’s value
    ** being 'fail'. This will trigger the function to call 
    ** sqlite3_result_error(). */
    execute(db, "select function('fail', 1, 2.71828, 'three', X'0004', NULL)");

    /* Done */
    sqlite3_close(db);

    return 0;    
}
Beispiel #25
0
SEXP
R_registerSQLFunc(SEXP rdb, SEXP r_func, SEXP rname, SEXP rnargs, SEXP userData)
{
    sqlite3 *db = GET_SQLITE_DB(rdb);
    void *udata = NULL;
    SQLiteFunc fun;
    int nargs = INTEGER(rnargs)[0];

    if(TYPEOF(r_func) == EXTPTRSXP) {
	fun = (SQLiteFunc) R_ExternalPtrAddr(r_func);
	if(Rf_length(userData))
	    udata = R_ExternalPtrAddr(userData);
	sqlite3_create_function(db, CHAR(STRING_ELT(rname, 0)), nargs, SQLITE_UTF8, udata, fun, NULL, NULL);
    } else {
	SEXP expr;
	if(nargs > -1) {
	    expr = allocVector(LANGSXP, nargs + 1);
	    R_PreserveObject(expr);
	    SETCAR(expr, r_func);
	    udata = expr;
	    fun = R_callFunc;
	} else {
	    R_PreserveObject(r_func);
	    udata = r_func;
	    fun = R_mkCallFunc;
	}
	sqlite3_create_function_v2(db, CHAR(STRING_ELT(rname, 0)), nargs, SQLITE_UTF8, udata, fun, NULL, NULL, Rsqlite_Release);
    }

    return(R_NilValue); // return a ticket to be able to release the fun.
}
static
void* OGRSQLiteRegisterRegExpFunction(
#ifndef HAVE_PCRE
CPL_UNUSED
#endif
                                      sqlite3* hDB)
{
#ifdef HAVE_PCRE

    /* For debugging purposes mostly */
    if( !CSLTestBoolean(CPLGetConfigOption("OGR_SQLITE_REGEXP", "YES")) )
        return NULL;

    /* Check if we really need to define our own REGEXP function */
    int rc = sqlite3_exec(hDB, "SELECT 'a' REGEXP 'a'", NULL, NULL, NULL);
    if( rc == SQLITE_OK )
    {
        CPLDebug("SQLITE", "REGEXP already available");
        return NULL;
    }

    cache_entry *cache = (cache_entry*) CPLCalloc(CACHE_SIZE, sizeof(cache_entry));
    sqlite3_create_function(hDB, "REGEXP", 2, SQLITE_UTF8, cache,
                            OGRSQLiteREGEXPFunction, NULL, NULL);

    /* To clear the error flag */
    sqlite3_exec(hDB, "SELECT 1", NULL, NULL, NULL);

    return cache;
#else // HAVE_PCRE
    return NULL;
#endif // HAVE_PCRE
}
Beispiel #27
0
/* SQLite invokes this routine once when it loads the extension.
** Create new functions, collating sequences, and virtual table
** modules here.  This is usually the only exported symbol in
** the shared library.
*/
int sqlite3_extension_init(sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi) {
  SQLITE_EXTENSION_INIT2(pApi)

  // rank call
  sqlite3_create_function(db, "rank", 3, SQLITE_ANY, 0, &rank, 0, 0);
  return 0;
}
Beispiel #28
0
static PHP_METHOD(SQLite, sqliteCreateAggregate)
{
	struct pdo_sqlite_func *func;
	zval *step_callback, *fini_callback;
	char *func_name;
	size_t func_name_len;
	zend_long argc = -1;
	zend_string *cbname = NULL;
	pdo_dbh_t *dbh;
	pdo_sqlite_db_handle *H;
	int ret;

	ZEND_PARSE_PARAMETERS_START(3, 4)
		Z_PARAM_STRING(func_name, func_name_len)
		Z_PARAM_ZVAL_DEREF(step_callback)
		Z_PARAM_ZVAL_DEREF(fini_callback)
		Z_PARAM_OPTIONAL
		Z_PARAM_LONG(argc)
	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

	dbh = Z_PDO_DBH_P(getThis());
	PDO_CONSTRUCT_CHECK;

	if (!zend_is_callable(step_callback, 0, &cbname)) {
		php_error_docref(NULL, E_WARNING, "function '%s' is not callable", ZSTR_VAL(cbname));
		zend_string_release(cbname);
		RETURN_FALSE;
	}
	zend_string_release(cbname);
	if (!zend_is_callable(fini_callback, 0, &cbname)) {
		php_error_docref(NULL, E_WARNING, "function '%s' is not callable", ZSTR_VAL(cbname));
		zend_string_release(cbname);
		RETURN_FALSE;
	}
	zend_string_release(cbname);

	H = (pdo_sqlite_db_handle *)dbh->driver_data;

	func = (struct pdo_sqlite_func*)ecalloc(1, sizeof(*func));

	ret = sqlite3_create_function(H->db, func_name, argc, SQLITE_UTF8,
			func, NULL, php_sqlite3_func_step_callback, php_sqlite3_func_final_callback);
	if (ret == SQLITE_OK) {
		func->funcname = estrdup(func_name);

		ZVAL_COPY(&func->step, step_callback);

		ZVAL_COPY(&func->fini, fini_callback);

		func->argc = argc;

		func->next = H->funcs;
		H->funcs = func;

		RETURN_TRUE;
	}

	efree(func);
	RETURN_FALSE;
}
Beispiel #29
0
void KVStore::createFunctions()
{
    int ret = sqlite3_create_function(mConn.get(), "escapeStr", 1, SQLITE_ANY, 0, &sqliteEscapeStr, 0, 0);
    if (ret != SQLITE_OK) {
        throw ConfigException("Error during creating functions: " + mConn.getErrorMessage());
    }
}
Beispiel #30
0
Datei: init.c Projekt: dpl0/pkg
int
pkg_repo_binary_init(struct pkg_repo *repo)
{
	int retcode = EPKG_OK;
	sqlite3 *sqlite = PRIV_GET(repo);

	sqlite3_create_function(sqlite, "file_exists", 2, SQLITE_ANY, NULL,
		    sqlite_file_exists, NULL, NULL);
	retcode = sql_exec(sqlite, "PRAGMA synchronous=default");
	if (retcode != EPKG_OK)
		return (retcode);

	retcode = sql_exec(sqlite, "PRAGMA foreign_keys=on");
	if (retcode != EPKG_OK)
		return (retcode);

	pkgdb_sqlcmd_init(sqlite, NULL, NULL);

	retcode = pkg_repo_binary_init_prstatements(sqlite);
	if (retcode != EPKG_OK)
		return (retcode);

	repo->priv = sqlite;

	return (EPKG_OK);
}