/* * Returns value for the column at position iCol (starting from 0). * Reads column data from ref-values table, filtered by ObjectID and sorted by PropertyID * For the sake of better performance, fetches required columns on demand, sequentially. * */ static int _column(sqlite3_vtab_cursor *pCursor, sqlite3_context *pContext, int iCol) { int result = SQLITE_OK; struct flexi_VTabCursor *cur = (void *) pCursor; if (iCol == -1) { sqlite3_result_int64(pContext, cur->lObjectID); goto EXIT; } struct flexi_ClassDef_t *vtab = (void *) cur->base.pVtab; // First, check if column has been already loaded while (cur->iReadCol < iCol) { int colResult = sqlite3_step(cur->pPropertyIterator); if (colResult == SQLITE_DONE) break; if (colResult != SQLITE_ROW) { result = colResult; goto ONERROR; } sqlite3_int64 lPropID = sqlite3_column_int64(cur->pPropertyIterator, 1); if (lPropID < vtab->pProps[cur->iReadCol + 1].iPropID) continue; cur->iReadCol++; if (lPropID == vtab->pProps[cur->iReadCol].iPropID) { sqlite3_int64 lPropIdx = sqlite3_column_int64(cur->pPropertyIterator, 2); /* * No need in any special verification as we expect columns are sorted by property IDs, so * we just assume that once column index is OK, we can process this property data */ cur->pCols[cur->iReadCol] = sqlite3_value_dup(sqlite3_column_value(cur->pPropertyIterator, 4)); } } if (cur->pCols[iCol] == NULL || sqlite3_value_type(cur->pCols[iCol]) == SQLITE_NULL) { sqlite3_result_value(pContext, vtab->pProps[iCol].defaultValue); } else { sqlite3_result_value(pContext, cur->pCols[iCol]); } result = SQLITE_OK; goto EXIT; ONERROR: EXIT: // Map column number to property ID return result; }
/* ** Retrieve a column of data. */ static int schemaColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ schema_cursor *pCur = (schema_cursor *)cur; switch( i ){ case 0: sqlite3_result_value(ctx, sqlite3_column_value(pCur->pDbList, 1)); break; case 1: sqlite3_result_value(ctx, sqlite3_column_value(pCur->pTableList, 0)); break; default: sqlite3_result_value(ctx, sqlite3_column_value(pCur->pColumnList, i-2)); break; } return SQLITE_OK; }
/* ** EXPERIMENTAL - This is not an official function. The interface may ** change. This function may disappear. Do not write code that depends ** on this function. ** ** Implementation of the QUOTE() function. This function takes a single ** argument. If the argument is numeric, the return value is the same as ** the argument. If the argument is NULL, the return value is the string ** "NULL". Otherwise, the argument is enclosed in single quotes with ** single-quote escapes. */ static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ assert( argc==1 ); UNUSED_PARAMETER(argc); switch( sqlite3_value_type(argv[0]) ){ case SQLITE_INTEGER: case SQLITE_FLOAT: { sqlite3_result_value(context, argv[0]); break; } case SQLITE_BLOB: { char *zText = 0; char const *zBlob = sqlite3_value_blob(argv[0]); int nBlob = sqlite3_value_bytes(argv[0]); assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); if( zText ){ int i; for(i=0; i<nBlob; i++){ zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; } zText[(nBlob*2)+2] = '\''; zText[(nBlob*2)+3] = '\0'; zText[0] = 'X'; zText[1] = '\''; sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); sqlite3_free(zText); } break; } case SQLITE_TEXT: { int i,j; u64 n; const unsigned char *zArg = sqlite3_value_text(argv[0]); char *z; if( zArg==0 ) return; for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } z = contextMalloc(context, ((i64)i)+((i64)n)+3); if( z ){ z[0] = '\''; for(i=0, j=1; zArg[i]; i++){ z[j++] = zArg[i]; if( zArg[i]=='\'' ){ z[j++] = '\''; } } z[j++] = '\''; z[j] = 0; sqlite3_result_text(context, z, j, sqlite3_free); } break; } default: { assert( sqlite3_value_type(argv[0])==SQLITE_NULL ); sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); break; } } }
static int sesqlite_column(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int cidx) { sesqlite_cursor *c = (sesqlite_cursor*) cur; sqlite3_result_value(ctx, sqlite3_column_value(c->stmt, cidx)); return SQLITE_OK; }
/* ** Invoke an SQL statement recursively. The function result is the ** first column of the first row of the result set. */ static void test_eval( sqlite3_context *pCtx, int nArg, sqlite3_value **argv ){ sqlite3_stmt *pStmt; int rc; sqlite3 *db = sqlite3_context_db_handle(pCtx); const char *zSql; zSql = (char*)sqlite3_value_text(argv[0]); rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); if( rc==SQLITE_OK ){ rc = sqlite3_step(pStmt); if( rc==SQLITE_ROW ){ sqlite3_result_value(pCtx, sqlite3_column_value(pStmt, 0)); } rc = sqlite3_finalize(pStmt); } if( rc ){ char *zErr; assert( pStmt==0 ); zErr = sqlite3_mprintf("sqlite3_prepare_v2() error: %s",sqlite3_errmsg(db)); sqlite3_result_text(pCtx, zErr, -1, sqlite3_free); sqlite3_result_error_code(pCtx, rc); } }
/* ** Implementation of the non-aggregate min() and max() functions */ static void minmaxFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ int i; int mask; /* 0 for min() or 0xffffffff for max() */ int iBest; CollSeq *pColl; if( argc==0 ) return; mask = sqlite3_user_data(context)==0 ? 0 : -1; pColl = sqlite3GetFuncCollSeq(context); assert( pColl ); assert( mask==-1 || mask==0 ); iBest = 0; if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; for(i=1; i<argc; i++){ if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return; if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){ iBest = i; } } sqlite3_result_value(context, argv[iBest]); }
void __SQLite3ExtLogicFunctionIf(sqlite3_context *context, int argc, sqlite3_value **argv) { if (argc == 3) { sqlite3_result_value(context, sqlite3_value_int(argv[0]) ? argv[1] : argv[2]); } else { sqlite3_result_null(context); } }
/* ** EXPERIMENTAL - This is not an official function. The interface may ** change. This function may disappear. Do not write code that depends ** on this function. ** ** Implementation of the QUOTE() function. This function takes a single ** argument. If the argument is numeric, the return value is the same as ** the argument. If the argument is NULL, the return value is the string ** "NULL". Otherwise, the argument is enclosed in single quotes with ** single-quote escapes. */ static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv) { if( argc<1 ) return; switch( sqlite3_value_type(argv[0]) ) { case SQLITE_NULL: { sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); break; } case SQLITE_INTEGER: case SQLITE_FLOAT: { sqlite3_result_value(context, argv[0]); break; } case SQLITE_BLOB: { char *zText = 0; int nBlob = sqlite3_value_bytes(argv[0]); char const *zBlob = sqlite3_value_blob(argv[0]); zText = (char *)sqliteMalloc((2*nBlob)+4); if( !zText ) { sqlite3_result_error(context, "out of memory", -1); } else { int i; for(i=0; i<nBlob; i++) { zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; } zText[(nBlob*2)+2] = '\''; zText[(nBlob*2)+3] = '\0'; zText[0] = 'X'; zText[1] = '\''; sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); sqliteFree(zText); } break; } case SQLITE_TEXT: { int i,j,n; const unsigned char *zArg = sqlite3_value_text(argv[0]); char *z; for(i=n=0; zArg[i]; i++) { if( zArg[i]=='\'' ) n++; } z = sqliteMalloc( i+n+3 ); if( z==0 ) return; z[0] = '\''; for(i=0, j=1; zArg[i]; i++) { z[j++] = zArg[i]; if( zArg[i]=='\'' ) { z[j++] = '\''; } } z[j++] = '\''; z[j] = 0; sqlite3_result_text(context, z, j, SQLITE_TRANSIENT); sqliteFree(z); } } }
static void minMaxFinalize(sqlite3_context *context){ sqlite3_value *pRes; pRes = (sqlite3_value *)sqlite3_aggregate_context(context, sizeof(Mem)); if( pRes->flags ){ sqlite3_result_value(context, pRes); } sqlite3VdbeMemRelease(pRes); }
/* ** Implementation of the NULLIF(x,y) function. The result is the first ** argument if the arguments are different. The result is NULL if the ** arguments are equal to each other. */ static void nullifFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ CollSeq *pColl = sqlite3GetFuncCollSeq(context); if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){ sqlite3_result_value(context, argv[0]); } }
/* ** Implementation of the NULLIF(x,y) function. The result is the first ** argument if the arguments are different. The result is NULL if the ** arguments are equal to each other. */ static void nullifFunc( sqlite3_context *context, int NotUsed, sqlite3_value **argv ){ CollSeq *pColl = sqlite3GetFuncCollSeq(context); UNUSED_PARAMETER(NotUsed); if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){ sqlite3_result_value(context, argv[0]); } }
ikptr ik_sqlite3_result_value (ikptr s_context, ikptr s_retval, ikpcb * pcb) { #ifdef HAVE_SQLITE3_RESULT_VALUE sqlite3_context * context = IK_SQLITE_CONTEXT(s_context); sqlite3_value * retval = IK_SQLITE_VALUE(s_retval); sqlite3_result_value(context, retval); return IK_VOID_OBJECT; #else feature_failure(__func__); #endif }
/* ** Implementation of the IFNULL(), NVL(), and COALESCE() functions. ** All three do the same thing. They return the first non-NULL ** argument. */ static void ifnullFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ int i; for(i=0; i<argc; i++){ if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){ sqlite3_result_value(context, argv[i]); break; } } }
/* ** This function takes two arguments. It performance UTF-8/16 type ** conversions on the first argument then returns a copy of the second ** argument. ** ** This function is used in cases such as the following: ** ** SELECT test_isolation(x,x) FROM t1; ** ** We want to verify that the type conversions that occur on the ** first argument do not invalidate the second argument. */ static void test_isolation( sqlite3_context *pCtx, int nArg, sqlite3_value **argv ){ #ifndef SQLITE_OMIT_UTF16 sqlite3_value_text16(argv[0]); sqlite3_value_text(argv[0]); sqlite3_value_text16(argv[0]); sqlite3_value_text(argv[0]); #endif sqlite3_result_value(pCtx, argv[1]); }
/* ** Return values of columns for the row at which the explain_cursor ** is currently pointing. */ static int explainColumn( sqlite3_vtab_cursor *cur, /* The cursor */ sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ int i /* Which column to return */ ){ explain_cursor *pCur = (explain_cursor*)cur; if( i==EXPLN_COLUMN_SQL ){ sqlite3_result_text(ctx, pCur->zSql, -1, SQLITE_TRANSIENT); }else{ sqlite3_result_value(ctx, sqlite3_column_value(pCur->pExplain, i)); } return SQLITE_OK; }
static void xmms_sqlite_stringify (sqlite3_context *context, int args, sqlite3_value **val) { gint i; gchar buffer[32]; if (sqlite3_value_type (val[0]) == SQLITE_INTEGER) { i = sqlite3_value_int (val[0]); sprintf (buffer, "%d", i); sqlite3_result_text (context, buffer, -1, SQLITE_TRANSIENT); } else { sqlite3_result_value (context, val[0]); } }
static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ int iCol = i + 1; sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt; if( simulateVtabError((echo_vtab *)(cur->pVtab), "xColumn") ){ return SQLITE_ERROR; } if( !pStmt ){ sqlite3_result_null(ctx); }else{ assert( sqlite3_data_count(pStmt)>iCol ); sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol)); } return SQLITE_OK; }
static void mmenc_func(sqlite3_context *db, int argc, sqlite3_value **argv) { mm_cipher_context_t *ctx; const UChar *src; int32_t src_len; char buf[1024]; char *dst = buf; int32_t dst_len; UErrorCode status = U_ZERO_ERROR; int arg_type; // only accept 1 argument. if (argc != 1) goto error_misuse; // encoding BLOB data type is not supported. arg_type = sqlite3_value_type(argv[0]); if (arg_type == SQLITE_BLOB) goto error_misuse; // for data types other than TEXT, just return them. if (arg_type != SQLITE_TEXT) { sqlite3_result_value(db, argv[0]); return; } ctx = (mm_cipher_context_t *) sqlite3_user_data(db); src_len = sqlite3_value_bytes16(argv[0]) / 2; src = (const UChar *) sqlite3_value_text16(argv[0]); // transform input string to BOCU-1 encoding. // try stack buffer first, if it doesn't fit, malloc a new buffer. dst_len = ucnv_fromUChars(ctx->cnv, dst, sizeof(buf), src, src_len, &status); if (status == U_BUFFER_OVERFLOW_ERROR) { status = U_ZERO_ERROR; dst = (char *) sqlite3_malloc(dst_len); dst_len = ucnv_fromUChars(ctx->cnv, dst, dst_len, src, src_len, &status); } if (U_FAILURE(status) && status != U_STRING_NOT_TERMINATED_WARNING) { sqlite3_mm_set_last_error( "Failed transforming text to internal encoding."); goto error_error; } // encrypt transformed BOCU-1 string. do_rc4(ctx, dst, dst_len); // return sqlite3_result_blob(db, dst, dst_len, SQLITE_TRANSIENT); if (dst != buf) sqlite3_free(dst); return; error_error: if (dst != buf) sqlite3_free(dst); sqlite3_result_error_code(db, SQLITE_ERROR); return; error_misuse: if (dst != buf) sqlite3_free(dst); sqlite3_result_error_code(db, SQLITE_MISUSE); return; }
/* ** The replace() function. Three arguments are all strings: call ** them A, B, and C. The result is also a string which is derived ** from A by replacing every occurance of B with C. The match ** must be exact. Collating sequences are not used. */ static void replaceFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ const unsigned char *zStr; /* The input string A */ const unsigned char *zPattern; /* The pattern string B */ const unsigned char *zRep; /* The replacement string C */ unsigned char *zOut; /* The output */ int nStr; /* Size of zStr */ int nPattern; /* Size of zPattern */ int nRep; /* Size of zRep */ i64 nOut; /* Maximum size of zOut */ int loopLimit; /* Last zStr[] that might match zPattern[] */ int i, j; /* Loop counters */ assert( argc==3 ); UNUSED_PARAMETER(argc); zStr = sqlite3_value_text(argv[0]); if( zStr==0 ) return; nStr = sqlite3_value_bytes(argv[0]); assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */ zPattern = sqlite3_value_text(argv[1]); if( zPattern==0 ){ assert( sqlite3_value_type(argv[1])==SQLITE_NULL || sqlite3_context_db_handle(context)->mallocFailed ); return; } if( zPattern[0]==0 ){ assert( sqlite3_value_type(argv[1])!=SQLITE_NULL ); sqlite3_result_value(context, argv[0]); return; } nPattern = sqlite3_value_bytes(argv[1]); assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */ zRep = sqlite3_value_text(argv[2]); if( zRep==0 ) return; nRep = sqlite3_value_bytes(argv[2]); assert( zRep==sqlite3_value_text(argv[2]) ); nOut = nStr + 1; assert( nOut<SQLITE_MAX_LENGTH ); zOut = contextMalloc(context, (i64)nOut); if( zOut==0 ){ return; } loopLimit = nStr - nPattern; for(i=j=0; i<=loopLimit; i++){ if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){ zOut[j++] = zStr[i]; }else{ u8 *zOld; sqlite3 *db = sqlite3_context_db_handle(context); nOut += nRep - nPattern; testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] ); testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] ); if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ sqlite3_result_error_toobig(context); sqlite3DbFree(db, zOut); return; } zOld = zOut; zOut = sqlite3_realloc(zOut, (int)nOut); if( zOut==0 ){ sqlite3_result_error_nomem(context); sqlite3DbFree(db, zOld); return; } memcpy(&zOut[j], zRep, nRep); j += nRep; i += nPattern-1; } } assert( j+nStr-i+1==nOut ); memcpy(&zOut[j], &zStr[i], nStr-i); j += nStr - i; assert( j<=nOut ); zOut[j] = 0; sqlite3_result_text(context, (char*)zOut, j, sqlite3_free); }
GEOPACKAGE_DECLARE void fnct_gpkgPointToTile (sqlite3_context * context, int argc UNUSED, sqlite3_value ** argv) { /* SQL function: / gpkgPointToTile (table, srid, x, y, zoom) / / Returns tile from tile matrix for specified srid, point and zoom level */ const unsigned char *table; int srid = 0; int target_srid = -1; double x_coord, y_coord; int int_value; int zoom; char *sql_stmt = NULL; sqlite3 *sqlite = NULL; sqlite3_stmt *stmt; int ret; if (sqlite3_value_type (argv[0]) != SQLITE_TEXT) { sqlite3_result_error(context, "gpkgPointToTile() error: argument 1 [table] is not of the String type", -1); return; } table = sqlite3_value_text (argv[0]); if (sqlite3_value_type (argv[1]) != SQLITE_INTEGER) { sqlite3_result_error(context, "gpkgPointToTile() error: argument 2 [srid] is not of the integer type", -1); return; } srid = sqlite3_value_int (argv[1]); if (sqlite3_value_type (argv[2]) == SQLITE_FLOAT) { x_coord = sqlite3_value_double (argv[2]); } else if (sqlite3_value_type (argv[2]) == SQLITE_INTEGER) { int_value = sqlite3_value_int (argv[2]); x_coord = int_value; } else { sqlite3_result_error(context, "gpkgPointToTile() error: argument 3 [x coordinate] is not of a numerical type", -1); return; } if (sqlite3_value_type (argv[3]) == SQLITE_FLOAT) { y_coord = sqlite3_value_double (argv[3]); } else if (sqlite3_value_type (argv[3]) == SQLITE_INTEGER) { int_value = sqlite3_value_int (argv[3]); y_coord = int_value; } else { sqlite3_result_error(context, "gpkgPointToTile() error: argument 4 [y coordinate] is not of a numerical type", -1); return; } if (sqlite3_value_type (argv[4]) != SQLITE_INTEGER) { sqlite3_result_error(context, "gpkgPointToTile() error: argument 5 [zoom level] is not of the integer type", -1); return; } zoom = sqlite3_value_int (argv[4]); /* project into right coordinate basis if the input isn't already there */ /* Get the target table SRID */ sql_stmt = sqlite3_mprintf("SELECT srid FROM raster_columns WHERE r_table_name=%Q AND r_raster_column='tile_data'", table); sqlite = sqlite3_context_db_handle (context); ret = sqlite3_prepare_v2 (sqlite, sql_stmt, strlen(sql_stmt), &stmt, NULL); sqlite3_free(sql_stmt); if (ret != SQLITE_OK) { sqlite3_result_error(context, "gpkgPointToTile() error: failed to prepare SQL SRID select statement", -1); return; } ret = sqlite3_step (stmt); if (ret != SQLITE_ROW) { sqlite3_finalize (stmt); sqlite3_result_error(context, "gpkgPointToTile() error: Could not find SRID for specified table", -1); return; } if (sqlite3_column_type (stmt, 0) != SQLITE_INTEGER) { sqlite3_finalize (stmt); sqlite3_result_error(context, "gpkgPointToTile() error: SRID for table is not an integer. Corrupt GeoPackage?", -1); return; } target_srid = sqlite3_column_int(stmt, 0); sqlite3_finalize (stmt); if (srid != target_srid) { /* project input coordinates */ sql_stmt = sqlite3_mprintf("SELECT ST_X(projected),ST_Y(projected) FROM (SELECT Transform(MakePoint(%f, %f, %i), %i) AS projected)", x_coord, y_coord, srid, target_srid); sqlite = sqlite3_context_db_handle (context); ret = sqlite3_prepare_v2 (sqlite, sql_stmt, strlen(sql_stmt), &stmt, NULL); sqlite3_free(sql_stmt); if (ret != SQLITE_OK) { sqlite3_result_error(context, "gpkgPointToTile() error: failed to prepare SQL Transform statement", -1); return; } ret = sqlite3_step (stmt); if (ret == SQLITE_ROW) { if ((sqlite3_column_type (stmt, 0) == SQLITE_FLOAT) && (sqlite3_column_type (stmt, 1) == SQLITE_FLOAT)) { x_coord = sqlite3_column_double(stmt, 0); y_coord = sqlite3_column_double(stmt, 1); } } ret = sqlite3_finalize (stmt); } /* now we can get the tile blob */ sql_stmt = sqlite3_mprintf("SELECT tile_data FROM \"%q\",\"%s_rt_metadata\" WHERE %q.id=%s_rt_metadata.id AND zoom_level=%i AND min_x <= %g AND max_x >=%g AND min_y <= %g AND max_y >= %g", table, table, table, table, zoom, x_coord, x_coord, y_coord, y_coord); sqlite = sqlite3_context_db_handle (context); ret = sqlite3_prepare_v2 (sqlite, sql_stmt, strlen(sql_stmt), &stmt, NULL); sqlite3_free(sql_stmt); if (ret != SQLITE_OK) { sqlite3_result_error(context, "gpkgPointToTile() error: failed to prepare SQL statement", -1); return; } ret = sqlite3_step (stmt); if (ret == SQLITE_ROW) { if (sqlite3_column_type (stmt, 0) == SQLITE_BLOB) { sqlite3_result_value (context, sqlite3_column_value(stmt, 0)); } } ret = sqlite3_finalize (stmt); }
void context::result_copy(int idx) { sqlite3_result_value(ctx_, values_[idx]); }