int convertRResult(SEXP ans, sqlite3_context *context) { switch(TYPEOF(ans)) { case INTSXP: sqlite3_result_int(context, INTEGER(ans)[0]); break; case REALSXP: sqlite3_result_double(context, REAL(ans)[0]); break; case STRSXP: { const char *str = CHAR(STRING_ELT(ans, 0)); sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT); break; } // Add more default: PROBLEM "Unhandled conversion of result of UDF from R to SQLite" WARN; break; } return(0); }
static void getweight(sqlite3_context *context, int argc, sqlite3_value **argv) { switch (sqlite3_value_type(argv[0]) ) { case SQLITE_TEXT: { const unsigned char *tVal = sqlite3_value_text(argv[0]); const guchar *p = tVal; gint weight = 0; while (*p) { if (*p >= '0' && *p <= '9') { weight = 10*weight + 1000*(*p - '0'); } else if (*p == '.' || *p == ',') { p++; if (*p >= '0' && *p <= '9') { weight += 100*(*p - '0'); p++; if (*p >= '0' && *p <= '9') { weight += 10*(*p - '0'); } } break; } else weight = 0; p++; } sqlite3_result_int(context, weight); break; } default: sqlite3_result_null(context); break; } }
static void pkgdb_regex(sqlite3_context *ctx, int argc, sqlite3_value **argv, int reg_type) { const unsigned char *regex = NULL; const unsigned char *str; regex_t *re; int ret; if (argc != 2 || (regex = sqlite3_value_text(argv[0])) == NULL || (str = sqlite3_value_text(argv[1])) == NULL) { sqlite3_result_error(ctx, "SQL function regex() called with invalid arguments.\n", -1); return; } re = (regex_t *)sqlite3_get_auxdata(ctx, 0); if (re == NULL) { re = malloc(sizeof(regex_t)); if (regcomp(re, regex, reg_type | REG_NOSUB) != 0) { sqlite3_result_error(ctx, "Invalid regex\n", -1); free(re); return; } sqlite3_set_auxdata(ctx, 0, re, pkgdb_regex_delete); } ret = regexec(re, str, 0, NULL, 0); sqlite3_result_int(ctx, (ret != REG_NOMATCH)); }
static void utf8error(sqlite3_context *context, int argc, sqlite3_value **argv) { switch (sqlite3_value_type(argv[0]) ) { case SQLITE_TEXT: { const unsigned char *tVal = sqlite3_value_text(argv[0]); if (g_utf8_validate((gchar *)tVal, -1, NULL)) sqlite3_result_int(context, 0); else sqlite3_result_int(context, 1); break; } default: sqlite3_result_null(context); break; } }
/** * REGEXP function for sqlite3. Takes two arguments; the first is the value and * the second the pattern. If the pattern is invalid, errors out. Otherwise, * returns true if the value matches the pattern and false otherwise. * * This function is made available in sqlite3 as the REGEXP operator. * * @param [in] context sqlite3-defined structure * @param [in] argc number of arguments - always 2 and hence unused * @param [in] argv 0: value to match; 1: pattern to match against */ static void sql_regexp(sqlite3_context* context, int argc UNUSED, sqlite3_value** argv) { const char* value = (const char*)sqlite3_value_text(argv[0]); const char* pattern = (const char*)sqlite3_value_text(argv[1]); switch (Tcl_RegExpMatch(NULL, value, pattern)) { case 0: sqlite3_result_int(context, 0); break; case 1: sqlite3_result_int(context, 1); break; case -1: sqlite3_result_error(context, "invalid pattern", -1); break; } }
static void sqlite_checksum_int4( sqlite3_context * ctx, int argc, sqlite3_value ** argv) { assert(argc==1); const unsigned char * txt; size_t len; switch (sqlite3_value_type(argv[0])) { case SQLITE_NULL: txt = NULL; len = 0; break; case SQLITE_TEXT: txt = sqlite3_value_text(argv[0]); len = sqlite3_value_bytes(argv[0]); break; // hmmm... should I do something else? case SQLITE_INTEGER: case SQLITE_FLOAT: case SQLITE_BLOB: default: sqlite3_result_error(ctx, "expecting TEXT or NULL", -1); return; } sqlite3_result_int(ctx, checksum_int4(txt, len)); }
static void UdfInsertFunc(sqlite3_context* aCtx, int aCnt, sqlite3_value** aValues) { int err; const char* tail = 0; sqlite3* db = 0; TEST2(aCnt, 1); db = sqlite3_context_db_handle(aCtx);/* to test that sqlite3_context_db_handle() can be called */ TEST(db != 0); TEST(!TheStmt); err = sqlite3_prepare(TheDb, "INSERT INTO t1(x) VALUES(:Val)", -1, &TheStmt, &tail); if(err == SQLITE_OK) { err = sqlite3_bind_value(TheStmt, 1, aValues[0]); if(err == SQLITE_OK) { err = sqlite3_step(TheStmt); } } (void)sqlite3_finalize(TheStmt); TheStmt = 0; sqlite3_result_int(aCtx, err); }
static void test_destructor_count( sqlite3_context *pCtx, int nArg, sqlite3_value **argv ){ sqlite3_result_int(pCtx, test_destructor_count_var); }
/* ** Implementation of the like() SQL function. This function implements ** the build-in LIKE operator. The first argument to the function is the ** pattern and the second argument is the string. So, the SQL statements: ** ** A LIKE B ** ** is implemented as like(B,A). ** ** This same function (with a different compareInfo structure) computes ** the GLOB operator. */ static void likeFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ const unsigned char *zA = sqlite3_value_text(argv[0]); const unsigned char *zB = sqlite3_value_text(argv[1]); int escape = 0; if( argc==3 ){ /* The escape character string must consist of a single UTF-8 character. ** Otherwise, return an error. */ const unsigned char *zEsc = sqlite3_value_text(argv[2]); if( sqlite3utf8CharLen((char*)zEsc, -1)!=1 ){ sqlite3_result_error(context, "ESCAPE expression must be a single character", -1); return; } escape = sqlite3ReadUtf8(zEsc); } if( zA && zB ){ struct compareInfo *pInfo = sqlite3_user_data(context); #ifdef SQLITE_TEST sqlite3_like_count++; #endif sqlite3_result_int(context, patternCompare(zA, zB, pInfo, escape)); } }
int cmdline_sqlite3_pid(sqlite3_vtab_cursor* pCursor, sqlite3_context *ctx) { cmdline_cursor_t *cursor = (cmdline_cursor_t*) pCursor; cmdline_table_t *table = (cmdline_table_t*) pCursor->pVtab; cmdline_t* cmdline = (cmdline_t*) vec_get(table->content,cursor->row); sqlite3_result_int(ctx, cmdline->pid); return SQLITE_OK; }
static void sqlite3_do_callback(sqlite3_context *context, const Variant& callback, int argc, sqlite3_value **argv, bool is_agg) { Array params = Array::Create(); php_sqlite3_agg_context *agg_context = NULL; if (is_agg) { agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context (context, sizeof(php_sqlite3_agg_context)); params.appendRef(agg_context->context); params.append(agg_context->row_count); } for (int i = 0; i < argc; i++) { params.append(get_value(argv[i])); } Variant ret = vm_call_user_func(callback, params); if (!is_agg || !argv) { /* only set the sqlite return value if we are a scalar function, * or if we are finalizing an aggregate */ if (ret.isInteger()) { sqlite3_result_int(context, ret.toInt64()); } else if (ret.isNull()) { sqlite3_result_null(context); } else if (ret.isDouble()) { sqlite3_result_double(context, ret.toDouble()); } else { String sret = ret.toString(); sqlite3_result_text(context, sret.data(), sret.size(), SQLITE_TRANSIENT); } } else { /* we're stepping in an aggregate; the return value goes into * the context */ agg_context->context = ret; } }
int xColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int col) { BaseCursor *pCur = (BaseCursor *)cur; const auto *pVtab = (VirtualTable *)cur->pVtab; if (col >= static_cast<int>(pVtab->content->columns.size())) { // Requested column index greater than column set size. return SQLITE_ERROR; } const auto &column_name = pVtab->content->columns[col].first; const auto &type = pVtab->content->columns[col].second; if (pCur->row >= pCur->data.size()) { // Request row index greater than row set size. return SQLITE_ERROR; } // Attempt to cast each xFilter-populated row/column to the SQLite type. const auto &value = pCur->data[pCur->row][column_name]; if (pCur->data[pCur->row].count(column_name) == 0) { // Missing content. VLOG(1) << "Error " << column_name << " is empty"; sqlite3_result_null(ctx); } else if (type == TEXT_TYPE) { sqlite3_result_text(ctx, value.c_str(), value.size(), SQLITE_STATIC); } else if (type == INTEGER_TYPE) { long afinite; if (!safeStrtol(value, 10, afinite) || afinite < INT_MIN || afinite > INT_MAX) { VLOG(1) << "Error casting " << column_name << " (" << value << ") to INTEGER"; sqlite3_result_null(ctx); } else { sqlite3_result_int(ctx, (int)afinite); } } else if (type == BIGINT_TYPE || type == UNSIGNED_BIGINT_TYPE) { long long afinite; if (!safeStrtoll(value, 10, afinite)) { VLOG(1) << "Error casting " << column_name << " (" << value << ") to BIGINT"; sqlite3_result_null(ctx); } else { sqlite3_result_int64(ctx, afinite); } } else if (type == DOUBLE_TYPE) { char *end = nullptr; double afinite = strtod(value.c_str(), &end); if (end == nullptr || end == value.c_str() || *end != '\0') { afinite = 0; VLOG(1) << "Error casting " << column_name << " (" << value << ") to DOUBLE"; sqlite3_result_null(ctx); } else { sqlite3_result_double(ctx, afinite); } } else { LOG(ERROR) << "Error unknown column type " << column_name; } return SQLITE_OK; }
/* ** Return values of columns for the row at which the templatevtab_cursor ** is currently pointing. */ static int templatevtabColumn( sqlite3_vtab_cursor *cur, /* The cursor */ sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ int i /* Which column to return */ ){ templatevtab_cursor *pCur = (templatevtab_cursor*)cur; switch( i ){ case TEMPLATEVTAB_A: sqlite3_result_int(ctx, 1000 + pCur->iRowid); break; default: assert( i==TEMPLATEVTAB_B ); sqlite3_result_int(ctx, 2000 + pCur->iRowid); break; } return SQLITE_OK; }
void collisionFunctionForSqlite(sqlite3_context* context, int dunno, sqlite3_value** values) { Number x(sqlite3_value_double(values[0])), z(sqlite3_value_double(values[1])); Number distance_sq = FixedPoint::square(collider_x - x) + FixedPoint::square(collider_z - z); sqlite3_result_int(context, distance_sq < collider_rsq ? 1 : 0); ++collider_comparisons; }
/* ** Implementation of the total_changes() SQL function. The return value is ** the same as the sqlite3_total_changes() API function. */ static void total_changes( sqlite3_context *context, int arg, sqlite3_value **argv ){ sqlite3 *db = sqlite3_user_data(context); sqlite3_result_int(context, sqlite3_total_changes(db)); }
static int statColumn( sqlite3_vtab_cursor *pCursor, sqlite3_context *ctx, int i ){ StatCursor *pCsr = (StatCursor *)pCursor; switch( i ){ case 0: /* name */ sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT); break; case 1: /* path */ sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT); break; case 2: /* pageno */ sqlite3_result_int64(ctx, pCsr->iPageno); break; case 3: /* pagetype */ sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC); break; case 4: /* ncell */ sqlite3_result_int(ctx, pCsr->nCell); break; case 5: /* payload */ sqlite3_result_int(ctx, pCsr->nPayload); break; case 6: /* unused */ sqlite3_result_int(ctx, pCsr->nUnused); break; case 7: /* mx_payload */ sqlite3_result_int(ctx, pCsr->nMxPayload); break; case 8: /* pgoffset */ sqlite3_result_int64(ctx, pCsr->iOffset); break; case 9: /* pgsize */ sqlite3_result_int(ctx, pCsr->szPage); break; default: { /* schema */ sqlite3 *db = sqlite3_context_db_handle(ctx); int iDb = pCsr->iDb; sqlite3_result_text(ctx, db->aDb[iDb].zName, -1, SQLITE_STATIC); break; } } return SQLITE_OK; }
void sql_verify_passwd(sqlite3_context* context, int argc, sqlite3_value** values) { char* passwd = (char*)sqlite3_value_text(values[0]); if (argc != 1 || passwd == 0) { fprintf(stderr,"SQL function VERIFY_PASSWD called with invalid arguments"); return; } sqlite3_result_int(context,verify_passwd(passwd)); }
extern void pg_bit_length(sqlite3_context * context, int argc, sqlite3_value ** argv) { int byte_count; _ksu_null_if_null_param(argc, argv); byte_count = sqlite3_value_bytes(argv[0]); sqlite3_result_int(context, byte_count * 8); }
/* ** Implementation of the total_changes() SQL function. The return value is ** the same as the sqlite3_total_changes() API function. */ static void total_changes( sqlite3_context *context, int NotUsed, sqlite3_value **NotUsed2 ){ sqlite3 *db = sqlite3_context_db_handle(context); UNUSED_PARAMETER2(NotUsed, NotUsed2); sqlite3_result_int(context, sqlite3_total_changes(db)); }
/* ** Implementation of random(). Return a random integer. */ static void randomFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ int r; sqlite3Randomness(sizeof(r), &r); sqlite3_result_int(context, r); }
extern void ora_vsize(sqlite3_context * context, int argc, sqlite3_value ** argv) { int sz; _ksu_null_if_null_param(argc, argv); sz = sqlite3_value_bytes(argv[0]); sqlite3_result_int(context, sz); }
/* * Implement substring match as an application-defined SQL function. * Using the SQL LIKE or GLOB operators instead would be a bad idea * because that would require escaping metacharacters in the string * being searched for. */ static void sql_match(sqlite3_context *context, int argc, sqlite3_value **argv) { assert(2 == argc); sqlite3_result_int(context, NULL != strcasestr( (const char *)sqlite3_value_text(argv[1]), (const char *)sqlite3_value_text(argv[0]))); }
static void OGR2SQLITE_ogr_geocode_set_result(sqlite3_context* pContext, OGRLayerH hLayer, const char* pszField) { if( hLayer == NULL ) sqlite3_result_null (pContext); else { OGRLayer* poLayer = (OGRLayer*)hLayer; OGRFeatureDefn* poFDefn = poLayer->GetLayerDefn(); OGRFeature* poFeature = poLayer->GetNextFeature(); int nIdx = -1; if( poFeature == NULL ) sqlite3_result_null (pContext); else if( strcmp(pszField, "geometry") == 0 && poFeature->GetGeometryRef() != NULL ) { GByte* pabyGeomBLOB = NULL; int nGeomBLOBLen = 0; if( OGRSQLiteLayer::ExportSpatiaLiteGeometry( poFeature->GetGeometryRef(), 4326, wkbNDR, FALSE, FALSE, FALSE, &pabyGeomBLOB, &nGeomBLOBLen ) != CE_None ) { sqlite3_result_null (pContext); } else { sqlite3_result_blob (pContext, pabyGeomBLOB, nGeomBLOBLen, CPLFree); } } else if( (nIdx = poFDefn->GetFieldIndex(pszField)) >= 0 && poFeature->IsFieldSet(nIdx) ) { OGRFieldType eType = poFDefn->GetFieldDefn(nIdx)->GetType(); if( eType == OFTInteger ) sqlite3_result_int(pContext, poFeature->GetFieldAsInteger(nIdx)); else if( eType == OFTInteger64 ) sqlite3_result_int64(pContext, poFeature->GetFieldAsInteger64(nIdx)); else if( eType == OFTReal ) sqlite3_result_double(pContext, poFeature->GetFieldAsDouble(nIdx)); else sqlite3_result_text(pContext, poFeature->GetFieldAsString(nIdx), -1, SQLITE_TRANSIENT); } else sqlite3_result_null (pContext); delete poFeature; OGRGeocodeFreeResult(hLayer); } }
/* ** Implementation of SQLite REGEXP operator. This scalar function takes ** two arguments. The first is a regular expression pattern to compile ** the second is a string to match against that pattern. If either ** argument is an SQL NULL, then NULL Is returned. Otherwise, the result ** is 1 if the string matches the pattern, or 0 otherwise. ** ** SQLite maps the regexp() function to the regexp() operator such ** that the following two are equivalent: ** ** zString REGEXP zPattern ** regexp(zPattern, zString) ** ** Uses the following ICU regexp APIs: ** ** uregex_open() ** uregex_matches() ** uregex_close() */ static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){ UErrorCode status = U_ZERO_ERROR; URegularExpression *pExpr; UBool res; const UChar *zString = sqlite3_value_text16(apArg[1]); (void)nArg; /* Unused parameter */ /* If the left hand side of the regexp operator is NULL, ** then the result is also NULL. */ if( !zString ){ return; } pExpr = sqlite3_get_auxdata(p, 0); if( !pExpr ){ const UChar *zPattern = sqlite3_value_text16(apArg[0]); if( !zPattern ){ return; } pExpr = uregex_open(zPattern, -1, 0, 0, &status); if( U_SUCCESS(status) ){ sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete); }else{ assert(!pExpr); icuFunctionError(p, "uregex_open", status); return; } } /* Configure the text that the regular expression operates on. */ uregex_setText(pExpr, zString, -1, &status); if( !U_SUCCESS(status) ){ icuFunctionError(p, "uregex_setText", status); return; } /* Attempt the match */ res = uregex_matches(pExpr, 0, &status); if( !U_SUCCESS(status) ){ icuFunctionError(p, "uregex_matches", status); return; } /* Set the text that the regular expression operates on to a NULL ** pointer. This is not really necessary, but it is tidier than ** leaving the regular expression object configured with an invalid ** pointer after this function returns. */ uregex_setText(pExpr, 0, 0, &status); /* Return 1 or 0. */ sqlite3_result_int(p, res ? 1 : 0); }
static int vtxt_column (sqlite3_vtab_cursor * pCursor, sqlite3_context * pContext, int column) { /* fetching value for the Nth column */ int nCol = 1; int i; char buf[4096]; int type; const char *value; VirtualTextCursorPtr cursor = (VirtualTextCursorPtr) pCursor; gaiaTextReaderPtr text = cursor->pVtab->reader; if (column == 0) { /* the ROWNO column */ sqlite3_result_int (pContext, cursor->current_row); return SQLITE_OK; } if (text->current_line_ready == 0) return SQLITE_ERROR; for (i = 0; i < text->max_fields; i++) { if (nCol == column) { if (!gaiaTextReaderFetchField (text, i, &type, &value)) sqlite3_result_null (pContext); else { if (type == VRTTXT_INTEGER) { strcpy (buf, value); text_clean_integer (buf); #if defined(_WIN32) || defined(__MINGW32__) /* CAVEAT - M$ runtime has non-standard functions for 64 bits */ sqlite3_result_int64 (pContext, _atoi64 (buf)); #else sqlite3_result_int64 (pContext, atoll (buf)); #endif } else if (type == VRTTXT_DOUBLE) { strcpy (buf, value); text_clean_double (buf); sqlite3_result_double (pContext, atof (buf)); } else if (type == VRTTXT_TEXT) sqlite3_result_text (pContext, value, strlen (value), free); else sqlite3_result_null (pContext); } } nCol++; } return SQLITE_OK; }
extern void my_strcmp(sqlite3_context * context, int argc, sqlite3_value ** argv) { int cmp; _ksu_null_if_null_param(argc, argv); cmp = strcmp((const char *)sqlite3_value_text(argv[0]), (const char *)sqlite3_value_text(argv[1])); sqlite3_result_int(context, (cmp>0?1:(cmp<0?-1:0))); }
/** * sqlite3 custom function for comparison of uint64_t values * since it is not supported by default */ void sqlite3_lessthan (sqlite3_context * ctx, int dummy, sqlite3_value ** values) { uint64_t v1; uint64_t v2; v1 = (uint64_t) sqlite3_value_int64 (values[0]); v2 = (uint64_t) sqlite3_value_int64 (values[1]); sqlite3_result_int (ctx, v1 < v2); }
/* * Get File size of a BFILE */ static void BFileSizeFunc( sqlite3_context *context, int argc, sqlite3_value **argv) { int rc; sqlite3 *db; int loc_size; off_t size; char *pLoc, *full_path; assert(context != NULL && argc == 1 && argv != NULL); full_path = NULL; loc_size = sqlite3_value_bytes(argv[0]); if (loc_size <= strlen(BFILE_PREFIX)) { sqlite3_result_int(context, -1); return; } db = (sqlite3 *)sqlite3_user_data(context); pLoc = (char *)sqlite3_value_text(argv[0]); assert(db != NULL && pLoc != NULL); rc = get_full_path(db, pLoc, loc_size, &full_path); if (rc) { if (rc == SQLITE_NOMEM) sqlite3_result_error_nomem(context); else sqlite3_result_error(context, "internal error", -1); return; } /* check existence, if not exits at at set size as -1 */ if (access(full_path, F_OK)) sqlite3_result_int(context, -1); else if (__bfile_get_size(full_path, &size) == SQLITE_OK) sqlite3_result_int(context, size); else sqlite3_result_error(context, "internal error", -1); sqlite3_free(full_path); }
/* * Implement regular expression match * as an application-defined SQL function. */ static void sql_regexp(sqlite3_context *context, int argc, sqlite3_value **argv) { assert(2 == argc); sqlite3_result_int(context, !regexec( (regex_t *)sqlite3_value_blob(argv[0]), (const char *)sqlite3_value_text(argv[1]), 0, NULL, 0)); }
/* ** Implementation of the total_changes() SQL function. The return value is ** the same as the sqlite3_total_changes() API function. */ static void total_changes( sqlite3_context *context, int NotUsed, sqlite3_value **NotUsed2 ){ sqlite3 *db = sqlite3_context_db_handle(context); UNUSED_PARAMETER2(NotUsed, NotUsed2); /* IMP: R-52756-41993 This function is a wrapper around the ** sqlite3_total_changes() C/C++ interface. */ sqlite3_result_int(context, sqlite3_total_changes(db)); }