/** * 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); }
void ActionLog::apply_action_xFun(sqlite3_context* context, int argc, sqlite3_value** argv) { ActionLog* the = reinterpret_cast<ActionLog*>(sqlite3_user_data(context)); if (argc != 11) { sqlite3_result_error(context, "``apply_action'' expects 10 arguments", -1); return; } Buffer device_name(sqlite3_value_blob(argv[0]), sqlite3_value_bytes(argv[0])); sqlite3_int64 seq_no = sqlite3_value_int64(argv[1]); int action = sqlite3_value_int(argv[2]); std::string filename = reinterpret_cast<const char*>(sqlite3_value_text(argv[3])); sqlite3_int64 version = sqlite3_value_int64(argv[4]); _LOG_TRACE("apply_function called with " << argc); _LOG_TRACE("device_name: " << Name(Block(reinterpret_cast<const char*>(device_name.buf()), device_name.size())) << ", action: " << action << ", file: " << filename); if (action == 0) // update { Buffer hash(sqlite3_value_blob(argv[5]), sqlite3_value_bytes(argv[5])); time_t atime = static_cast<time_t>(sqlite3_value_int64(argv[6])); time_t mtime = static_cast<time_t>(sqlite3_value_int64(argv[7])); time_t ctime = static_cast<time_t>(sqlite3_value_int64(argv[8])); int mode = sqlite3_value_int(argv[9]); int seg_num = sqlite3_value_int(argv[10]); _LOG_DEBUG("Update " << filename << " " << atime << " " << mtime << " " << ctime << " " << toHex(hash)); the->m_fileState->UpdateFile(filename, version, hash, device_name, seq_no, atime, mtime, ctime, mode, seg_num); // no callback here } else if (action == 1) // delete { the->m_fileState->DeleteFile(filename); the->m_onFileRemoved(filename); } sqlite3_result_null(context); }
/* ** Implementation of the abs() function */ static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ assert( argc==1 ); switch( sqlite3_value_type(argv[0]) ){ case SQLITE_INTEGER: { i64 iVal = sqlite3_value_int64(argv[0]); if( iVal<0 ){ if( (iVal<<1)==0 ){ sqlite3_result_error(context, "integer overflow", -1); return; } iVal = -iVal; } sqlite3_result_int64(context, iVal); break; } case SQLITE_NULL: { sqlite3_result_null(context); break; } default: { double rVal = sqlite3_value_double(argv[0]); if( rVal<0 ) rVal = -rVal; sqlite3_result_double(context, rVal); break; } } }
extern void pg_to_hex(sqlite3_context * context, int argc, sqlite3_value ** argv) { unsigned long long n; long long signedn; char *result; if (ksu_prm_ok(context, argc, argv, "to_hex", KSU_PRM_INT)) { signedn = (long long)sqlite3_value_int64(argv[0]); n = (unsigned long long)signedn; if (signedn < 0) { signedn *= -1; } result = (char *)sqlite3_malloc(1 + 2 * sizeof(n)); if (result == (char *)NULL) { sqlite3_result_error_nomem(context); return; } if (signedn > INT_MAX) { (void)sprintf(result, "%llx", n); } else { (void)sprintf(result, "%x", (unsigned int)n); } sqlite3_result_text(context, result, -1, sqlite3_free); } }
/* ** Insert a new row into the FTS content table. */ int sqlite3Fts5StorageContentInsert( Fts5Storage *p, sqlite3_value **apVal, i64 *piRowid ){ Fts5Config *pConfig = p->pConfig; int rc = SQLITE_OK; /* Insert the new row into the %_content table. */ if( pConfig->eContent!=FTS5_CONTENT_NORMAL ){ if( sqlite3_value_type(apVal[1])==SQLITE_INTEGER ){ *piRowid = sqlite3_value_int64(apVal[1]); }else{ rc = fts5StorageNewRowid(p, piRowid); } }else{ sqlite3_stmt *pInsert = 0; /* Statement to write %_content table */ int i; /* Counter variable */ rc = fts5StorageGetStmt(p, FTS5_STMT_INSERT_CONTENT, &pInsert, 0); for(i=1; rc==SQLITE_OK && i<=pConfig->nCol+1; i++){ rc = sqlite3_bind_value(pInsert, i, apVal[i]); } if( rc==SQLITE_OK ){ sqlite3_step(pInsert); rc = sqlite3_reset(pInsert); } *piRowid = sqlite3_last_insert_rowid(pConfig->db); } return rc; }
static void sqlite_callback_args(int argc, sqlite3_value **argv) { int i; for (i = 0; i < argc; i++) { sqlite3_value *sv = argv[i]; Value *vp = fg->stk_top; switch (sqlite3_value_type(sv)) { case SQLITE_INTEGER: *vp = fs->int64_Value(sqlite3_value_int64(sv)); break; case SQLITE_FLOAT: *vp = fs->float_Value(fs->cls_float, sqlite3_value_double(sv)); break; case SQLITE_TEXT: { const char *p = (const char*)sqlite3_value_text(sv); int len = sqlite3_value_bytes(sv); *vp = fs->cstr_Value(NULL, p, len); break; } case SQLITE_BLOB: { const char *p = (const char*)sqlite3_value_blob(sv); int len = sqlite3_value_bytes(sv); *vp = fs->cstr_Value(fs->cls_bytes, p, len); break; } default: *vp = VALUE_NULL; break; } fg->stk_top++; } }
/* ** Called to "rewind" a cursor back to the beginning so that ** it starts its output over again. Always called at least once ** prior to any closureColumn, closureRowid, or closureEof call. ** ** This routine actually computes the closure. ** ** See the comment at the beginning of closureBestIndex() for a ** description of the meaning of idxNum. The idxStr parameter is ** not used. */ static int closureFilter( sqlite3_vtab_cursor *pVtabCursor, int idxNum, const char *idxStr, int argc, sqlite3_value **argv ){ closure_cursor *pCur = (closure_cursor *)pVtabCursor; closure_vtab *pVtab = pCur->pVtab; sqlite3_int64 iRoot; int mxGen = 999999999; char *zSql; sqlite3_stmt *pStmt; closure_avl *pAvl; int rc = SQLITE_OK; const char *zTableName = pVtab->zTableName; const char *zIdColumn = pVtab->zIdColumn; const char *zParentColumn = pVtab->zParentColumn; closure_queue sQueue; (void)idxStr; /* Unused parameter */ (void)argc; /* Unused parameter */ closureClearCursor(pCur); memset(&sQueue, 0, sizeof(sQueue)); if( (idxNum & 1)==0 ){ /* No root=$root in the WHERE clause. Return an empty set */ return SQLITE_OK; } iRoot = sqlite3_value_int64(argv[0]); if( (idxNum & 0x000f0)!=0 ){ mxGen = sqlite3_value_int(argv[(idxNum>>4)&0x0f]); if( (idxNum & 0x00002)!=0 ) mxGen--; }
static int intarrayFilter(sqlite3_vtab_cursor *pVtabCursor, int idxNum, const char *idxStr, int argc, sqlite3_value **argv) { intarray_cursor *pCur = (intarray_cursor *)pVtabCursor; intarray_vtab *table = (intarray_vtab*)pCur->base.pVtab; sqlite3_intarray *arr = table->intarray; int op1 = (idxNum >> 2) & 7, op2 = (idxNum >> 5) & 7; sqlite3_int64 v = 0; int startIndex = 0; pCur->mode = (idxNum & 3); pCur->hasMin = 0, pCur->hasMax = 0; pCur->min = 0; pCur->max = 0; pCur->uniqueLeft = -1; if (argc > 0 && op1) { v = sqlite3_value_int64(argv[0]); intarrayOpVal(v, op1, &pCur->max, &pCur->min, &pCur->hasMax, &pCur->hasMin); } if (argc > 1 && op2) { v = sqlite3_value_int64(argv[1]); intarrayOpVal(v, op2, &pCur->max, &pCur->min, &pCur->hasMax, &pCur->hasMin); } if (pCur->hasMin && pCur->hasMax && pCur->min > pCur->max) { /* constraint is never true */ pCur->i = arr->n; return SQLITE_OK; } if (pCur->hasMin && pCur->mode == 1 ) { startIndex = (int)pCur->min; if (startIndex < 0) startIndex = 0; } else if (pCur->hasMin && pCur->mode == 2 && arr->ordered && arr->n > INTARRAY_BSEARCH_THRESHOLD) { startIndex = intarray_bsearch(pCur->min, arr->a, 0, arr->n, !arr->unique); } if (arr->unique && pCur->mode == 2 && pCur->hasMin && pCur->hasMax) { v = pCur->max - pCur->min + 1; if (v > 0 && v < 0x7FFFFFFF) { pCur->uniqueLeft = (int)v; } } pCur->i = intarrayNextMatch(pCur, startIndex); return SQLITE_OK; }
/* ** Output an sqlite3_value object's value as an SQL literal. */ static void vtablogQuote(sqlite3_value *p){ char z[50]; switch( sqlite3_value_type(p) ){ case SQLITE_NULL: { printf("NULL"); break; } case SQLITE_INTEGER: { sqlite3_snprintf(50,z,"%lld", sqlite3_value_int64(p)); printf("%s", z); break; } case SQLITE_FLOAT: { sqlite3_snprintf(50,z,"%!.20g", sqlite3_value_double(p)); printf("%s", z); break; } case SQLITE_BLOB: { int n = sqlite3_value_bytes(p); const unsigned char *z = (const unsigned char*)sqlite3_value_blob(p); int i; printf("x'"); for(i=0; i<n; i++) printf("%02x", z[i]); printf("'"); break; } case SQLITE_TEXT: { const char *z = (const char*)sqlite3_value_text(p); int i; char c; for(i=0; (c = z[i])!=0 && c!='\''; i++){} if( c==0 ){ printf("'%s'",z); }else{ printf("'"); while( *z ){ for(i=0; (c = z[i])!=0 && c!='\''; i++){} if( c=='\'' ) i++; if( i ){ printf("%.*s", i, z); z += i; } if( c=='\'' ){ printf("'"); continue; } if( c==0 ){ break; } z++; } printf("'"); } break; } } }
static void SFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ std::stringstream s; std::string d; double sum=0; char *buf=NULL; int i; s.str(""); s << "("; d=""; for(i=0; i < argc; i++) { switch( sqlite3_value_type(argv[i]) ){ case SQLITE_INTEGER: { sum+=(double) sqlite3_value_int64(argv[i]); s << d << sum; d=","; break; } case SQLITE_NULL: { s << d << "()"; d=","; break; } default: { sum+=sqlite3_value_int64(argv[i]); s << d << sum; d=","; break; } } } s << ")"; buf = (char *) malloc (sizeof(char)*(s.str().size()+2)); if (buf == NULL) fprintf(stderr,"malloc error in SNFunc, buf\n"); snprintf(buf,s.str().size()+1,"%s",s.str().c_str()); sqlite3_result_text(context,buf,s.str().size()+1,free ); }
// integer SUM without fancy overflow handling static void isum_step( sqlite3_context * ctx, int argc, sqlite3_value ** argv) { assert(argc==1); int64_t * val = sqlite3_aggregate_context(ctx, sizeof(int64_t)); if (sqlite3_value_type(argv[0])==SQLITE_INTEGER) *val += sqlite3_value_int64(argv[0]); // else just ignore... }
void TiVoRandomSeedFunc(sqlite3_context *context, int argc, sqlite3_value **argv) { int64_t r, seed; if( argc != 1 || sqlite3_value_type(argv[0]) != SQLITE_INTEGER ) return; seed = sqlite3_value_int64(argv[0]); seedRandomness(sizeof(r), &r, seed); sqlite3_result_int64(context, r); }
ikptr ik_sqlite3_value_int64 (ikptr s_value, ikpcb * pcb) { #ifdef HAVE_SQLITE3_VALUE_INT64 sqlite3_value * value = IK_SQLITE_VALUE(s_value); sqlite3_int64 rv; rv = sqlite3_value_int64(value); return ika_integer_from_sint64(pcb, rv); #else feature_failure(__func__); #endif }
static unsigned __int64 getUnsignedResult(const RtlFieldInfo *field, sqlite3_value *val) { assertex(val); if (isNull(val)) { NullFieldProcessor p(field); return p.uintResult; } if (sqlite3_value_type(val) != SQLITE_INTEGER) typeError("integer", field); return (unsigned __int64) sqlite3_value_int64(val); }
static bool getBooleanResult(const RtlFieldInfo *field, sqlite3_value *val) { assertex(val); if (isNull(val)) { NullFieldProcessor p(field); return p.boolResult; } if (sqlite3_value_type(val) != SQLITE_INTEGER) typeError("boolean", field); return sqlite3_value_int64(val) != 0; }
/* ** The zeroblob(N) function returns a zero-filled blob of size N bytes. */ static void zeroblobFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ i64 n; assert( argc==1 ); n = sqlite3_value_int64(argv[0]); if( n>SQLITE_MAX_LENGTH ){ sqlite3_result_error_toobig(context); }else{ sqlite3_result_zeroblob(context, n); } }
extern void pg_bool_or_step(sqlite3_context *context, int argc, sqlite3_value **argv) { CONTEXT_T *ctx; int typ; int val; typ = sqlite3_value_type(argv[0]); if (typ != SQLITE_NULL) { if (typ == SQLITE_INTEGER) { val = sqlite3_value_int(argv[0]); if ((val != 0) && (val != 1)) { ksu_err_msg(context, KSU_ERR_INV_DATATYPE, "bool_or"); return; } } else { char *p = (char *)sqlite3_value_text(argv[0]); int len = strlen(p); if (len == 1) { if (toupper(*p) == 'T') { val = 1; } else if (toupper(*p) == 'F') { val = 0; } else { ksu_err_msg(context, KSU_ERR_INV_DATATYPE, "bool_or"); return; } } else if (strcasecmp(p, "true") == 0) { val = 1; } else if (strcasecmp(p, "false") == 0) { val = 0; } else { ksu_err_msg(context, KSU_ERR_INV_DATATYPE, "bool_or"); return; } } val = sqlite3_value_int64(argv[0]); ctx = (CONTEXT_T *)sqlite3_aggregate_context(context, sizeof(CONTEXT_T)); if (ctx) { if (ctx->n == 0) { ctx->boolval = val; } else { ctx->boolval |= val; } (ctx->n)++; } } }
MHStoreEntry* MHLabelsStore::readEntry(sqlite3_stmt *stmt) const { if (!stmt) { LOG.error("%s: null sql statement", __FUNCTION__); return NULL; } sqlite3_value* oid = sqlite3_column_value(stmt, 0); unsigned long luid = static_cast<unsigned long>(sqlite3_value_int64(oid)); const char* name = (const char*)sqlite3_column_text(stmt, 1); uint32_t guid = (uint32_t)sqlite3_column_int(stmt, 2); MHLabelInfo* itemInfo = new MHLabelInfo(guid, luid, name); return itemInfo; }
static int fulltextUpdate(sqlite3_vtab *pVtab, int nArg, sqlite3_value **ppArg, sqlite_int64 *pRowid){ fulltext_vtab *v = (fulltext_vtab *) pVtab; if( nArg<2 ){ return index_delete(v, sqlite3_value_int64(ppArg[0])); } if( sqlite3_value_type(ppArg[0]) != SQLITE_NULL ){ return SQLITE_ERROR; /* an update; not yet supported */ } assert( nArg==3 ); /* ppArg[1] = rowid, ppArg[2] = content */ return index_insert(v, ppArg[1], (const char *)sqlite3_value_text(ppArg[2]), pRowid); }
static double OGR2SQLITE_GetValAsDouble(sqlite3_value* val, int* pbGotVal) { switch(sqlite3_value_type(val)) { case SQLITE_FLOAT: if( pbGotVal ) *pbGotVal = TRUE; return sqlite3_value_double(val); case SQLITE_INTEGER: if( pbGotVal ) *pbGotVal = TRUE; return (double) sqlite3_value_int64(val); default: if( pbGotVal ) *pbGotVal = FALSE; return 0.0; } }
/* ** The zeroblob(N) function returns a zero-filled blob of size N bytes. */ static void zeroblobFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ i64 n; sqlite3 *db = sqlite3_context_db_handle(context); assert( argc==1 ); UNUSED_PARAMETER(argc); n = sqlite3_value_int64(argv[0]); testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH] ); testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){ sqlite3_result_error_toobig(context); }else{ sqlite3_result_zeroblob(context, (int)n); } }
/* ** Routines used to compute the sum, average, and total. ** ** The SUM() function follows the (broken) SQL standard which means ** that it returns NULL if it sums over no inputs. TOTAL returns ** 0.0 in that case. In addition, TOTAL always returns a float where ** SUM might return an integer if it never encounters a floating point ** value. ** ** I am told that SUM() should raise an exception if it encounters ** a integer overflow. But after pondering this, I decided that ** behavior leads to brittle programs. So instead, I have coded ** SUM() to revert to using floating point if it encounters an ** integer overflow. The answer may not be exact, but it will be ** close. If the SUM() function returns an integer, the value is ** exact. If SUM() returns a floating point value, it means the ** value might be approximated. */ static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ SumCtx *p; int type; assert( argc==1 ); p = sqlite3_aggregate_context(context, sizeof(*p)); type = sqlite3_value_numeric_type(argv[0]); if( p && type!=SQLITE_NULL ){ p->cnt++; if( type==SQLITE_INTEGER ){ p->sum += sqlite3_value_int64(argv[0]); if( !p->approx ){ i64 iVal; p->approx = p->sum!=(LONGDOUBLE_TYPE)(iVal = (i64)p->sum); } }else{ p->sum += sqlite3_value_double(argv[0]); p->approx = 1; } } }
/* * Close a BFILE */ static void BFileCloseFunc( sqlite3_context *context, int argc, sqlite3_value **argv) { BfileHdl *pHdl; assert(context != NULL && argc == 1 && argv != NULL); pHdl = (BfileHdl*)sqlite3_value_int64(argv[0]); if (pHdl != NULL) { if (pHdl->fd >= 0) close(pHdl->fd); if (pHdl->full_path != NULL) sqlite3_free(pHdl->full_path); if (pHdl->zBuf != NULL) sqlite3_free(pHdl->zBuf); sqlite3_free(pHdl); } }
/* ** largest integer value not greater than argument */ void myfloorFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ double rVal=0.0; switch( sqlite3_value_type(argv[0]) ){ case SQLITE_INTEGER: { i64 iVal = sqlite3_value_int64(argv[0]); sqlite3_result_int64(context, iVal); break; } case SQLITE_NULL: { sqlite3_result_null(context); break; } default: { rVal = sqlite3_value_double(argv[0]); sqlite3_result_int64(context, (i64) floor(rVal)); break; } } }
static void IFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ assert( argc==2 ); char *buf=NULL; std::stringstream s; std::string ts; extract e; e.setdelims(", "); s.str(""); s << sqlite3_value_text(argv[0]); e.strip(s.str()); s.str(e.I(sqlite3_value_int64(argv[1]))); buf = (char *) malloc (sizeof(char)*(s.str().size()+2)); if (buf == NULL) fprintf(stderr,"malloc error in SNFunc, buf\n"); snprintf(buf,s.str().size()+1,"%s",s.str().c_str()); sqlite3_result_text(context,buf,s.str().size()+1,free ); }
/* ** Implementation of the abs() function */ static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ assert( argc==1 ); switch( sqlite3_value_type(argv[0]) ){ case SQLITE_INTEGER: { i64 iVal = sqlite3_value_int64(argv[0]); if( iVal<0 ) iVal = iVal * -1; sqlite3_result_int64(context, iVal); break; } case SQLITE_NULL: { sqlite3_result_null(context); break; } default: { double rVal = sqlite3_value_double(argv[0]); if( rVal<0 ) rVal = rVal * -1.0; sqlite3_result_double(context, rVal); break; } } }
static void callCastedDoubleFunc(sqlite3_context* context, int argc, sqlite3_value** argv, DoubleDoubleFunction f) { double rVal = 0.0; assert(argc == 1); switch (sqlite3_value_type(argv[0])) { case SQLITE_INTEGER: { int64_t iVal = sqlite3_value_int64(argv[0]); sqlite3_result_int64(context, iVal); break; } case SQLITE_NULL: sqlite3_result_null(context); break; default: rVal = sqlite3_value_double(argv[0]); sqlite3_result_int64(context, (int64_t)f(rVal)); break; } }
/* ** Routines used to compute the sum, average, and total. ** ** The SUM() function follows the (broken) SQL standard which means ** that it returns NULL if it sums over no inputs. TOTAL returns ** 0.0 in that case. In addition, TOTAL always returns a float where ** SUM might return an integer if it never encounters a floating point ** value. TOTAL never fails, but SUM might through an exception if ** it overflows an integer. */ static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ SumCtx *p; int type; assert( argc==1 ); UNUSED_PARAMETER(argc); p = sqlite3_aggregate_context(context, sizeof(*p)); type = sqlite3_value_numeric_type(argv[0]); if( p && type!=SQLITE_NULL ){ p->cnt++; if( type==SQLITE_INTEGER ){ i64 v = sqlite3_value_int64(argv[0]); p->rSum += v; if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){ p->overflow = 1; } }else{ p->rSum += sqlite3_value_double(argv[0]); p->approx = 1; } } }
SQLITE_EXTENSION_INIT1 static void hamming_distance(sqlite3_context * ctx, int agc, sqlite3_value **argv) { sqlite3_int64 hashes[4]; ulong64 left, right; int i, result; for(i = 0; i < 4; i++) { if (SQLITE_INTEGER == sqlite3_value_type(argv[i])) { hashes[i] = sqlite3_value_int64(argv[i]); } else { hashes[i] = 0; } } left = (hashes[0] << 32) + hashes[1]; right = (hashes[2] << 32) + hashes[3]; result = ph_hamming_distance(left, right); sqlite3_result_int(ctx, result); }
/* ** Implementation of the sign() function */ static void msignFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ assert( argc==1 ); switch( sqlite3_value_type(argv[0]) ){ case SQLITE_INTEGER: { long long int iVal = sqlite3_value_int64(argv[0]); iVal = ( iVal > 0) ? 1 : ( iVal < 0 ) ? -1 : 0; sqlite3_result_int64(context, iVal); break; } case SQLITE_NULL: { sqlite3_result_null(context); break; } default: { double rVal = sqlite3_value_double(argv[0]); rVal = ( rVal > 0) ? 1 : ( rVal < 0 ) ? -1 : 0; sqlite3_result_double(context, rVal); break; } } }