📄 test1.c
字号:
/*** Usage: sqlite3_rekey DB KEY**** Change the codec key.*/static int test_rekey( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ char **argv /* Text of each argument */){ sqlite3 *db; const char *zKey; int nKey; if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " FILENAME\"", 0); return TCL_ERROR; } if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; zKey = argv[2]; nKey = strlen(zKey);#ifdef SQLITE_HAS_CODEC sqlite3_rekey(db, zKey, nKey);#endif return TCL_OK;}/*** Usage: sqlite3_close DB**** Closes the database opened by sqlite3_open.*/static int sqlite_test_close( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ char **argv /* Text of each argument */){ sqlite3 *db; int rc; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " FILENAME\"", 0); return TCL_ERROR; } if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; rc = sqlite3_close(db); Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC); return TCL_OK;}/*** Implementation of the x_coalesce() function.** Return the first argument 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_text(context, (char*)sqlite3_value_text(argv[i]), sqlite3_value_bytes(argv[i]), SQLITE_TRANSIENT); break; } }}/*** These are test functions. hex8() interprets its argument as** UTF8 and returns a hex encoding. hex16le() interprets its argument** as UTF16le and returns a hex encoding.*/static void hex8Func(sqlite3_context *p, int argc, sqlite3_value **argv){ const unsigned char *z; int i; char zBuf[200]; z = sqlite3_value_text(argv[0]); for(i=0; i<sizeof(zBuf)/2 - 2 && z[i]; i++){ sprintf(&zBuf[i*2], "%02x", z[i]&0xff); } zBuf[i*2] = 0; sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);}static void hex16Func(sqlite3_context *p, int argc, sqlite3_value **argv){ const unsigned short int *z; int i; char zBuf[400]; z = sqlite3_value_text16(argv[0]); for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){ sprintf(&zBuf[i*4], "%04x", z[i]&0xff); } zBuf[i*4] = 0; sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);}/*** A structure into which to accumulate text.*/struct dstr { int nAlloc; /* Space allocated */ int nUsed; /* Space used */ char *z; /* The space */};/*** Append text to a dstr*/static void dstrAppend(struct dstr *p, const char *z, int divider){ int n = strlen(z); if( p->nUsed + n + 2 > p->nAlloc ){ char *zNew; p->nAlloc = p->nAlloc*2 + n + 200; zNew = sqliteRealloc(p->z, p->nAlloc); if( zNew==0 ){ sqliteFree(p->z); memset(p, 0, sizeof(*p)); return; } p->z = zNew; } if( divider && p->nUsed>0 ){ p->z[p->nUsed++] = divider; } memcpy(&p->z[p->nUsed], z, n+1); p->nUsed += n;}/*** Invoked for each callback from sqlite3ExecFunc*/static int execFuncCallback(void *pData, int argc, char **argv, char **NotUsed){ struct dstr *p = (struct dstr*)pData; int i; for(i=0; i<argc; i++){ if( argv[i]==0 ){ dstrAppend(p, "NULL", ' '); }else{ dstrAppend(p, argv[i], ' '); } } return 0;}/*** Implementation of the x_sqlite_exec() function. This function takes** a single argument and attempts to execute that argument as SQL code.** This is illegal and should set the SQLITE_MISUSE flag on the database.**** 2004-Jan-07: We have changed this to make it legal to call sqlite3_exec()** from within a function call. ** ** This routine simulates the effect of having two threads attempt to** use the same database at the same time.*/static void sqlite3ExecFunc( sqlite3_context *context, int argc, sqlite3_value **argv){ struct dstr x; memset(&x, 0, sizeof(x)); (void)sqlite3_exec((sqlite3*)sqlite3_user_data(context), (char*)sqlite3_value_text(argv[0]), execFuncCallback, &x, 0); sqlite3_result_text(context, x.z, x.nUsed, SQLITE_TRANSIENT); sqliteFree(x.z);}/*** Usage: sqlite_test_create_function DB**** Call the sqlite3_create_function API on the given database in order** to create a function named "x_coalesce". This function does the same thing** as the "coalesce" function. This function also registers an SQL function** named "x_sqlite_exec" that invokes sqlite3_exec(). Invoking sqlite3_exec()** in this way is illegal recursion and should raise an SQLITE_MISUSE error.** The effect is similar to trying to use the same database connection from** two threads at the same time.**** The original motivation for this routine was to be able to call the** sqlite3_create_function function while a query is in progress in order** to test the SQLITE_MISUSE detection logic.*/static int test_create_function( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ char **argv /* Text of each argument */){ int rc; sqlite3 *db; extern void Md5_Register(sqlite3*); if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", 0); return TCL_ERROR; } if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_ANY, 0, ifnullFunc, 0, 0); if( rc==SQLITE_OK ){ rc = sqlite3_create_function(db, "hex8", 1, SQLITE_ANY, 0, hex8Func, 0, 0); } if( rc==SQLITE_OK ){ rc = sqlite3_create_function(db, "hex16", 1, SQLITE_ANY, 0, hex16Func, 0, 0); }#ifndef SQLITE_OMIT_UTF16 /* Use the sqlite3_create_function16() API here. Mainly for fun, but also ** because it is not tested anywhere else. */ if( rc==SQLITE_OK ){ sqlite3_value *pVal;#ifdef SQLITE_MEMDEBUG if( sqlite3_iMallocFail>0 ){ sqlite3_iMallocFail++; }#endif pVal = sqlite3ValueNew(); sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC); rc = sqlite3_create_function16(db, sqlite3ValueText(pVal, SQLITE_UTF16NATIVE), 1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0); sqlite3ValueFree(pVal); }#endif if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; Tcl_SetResult(interp, (char *)errorName(rc), 0); return TCL_OK;}/*** Routines to implement the x_count() aggregate function.**** x_count() counts the number of non-null arguments. But there are** some twists for testing purposes.**** If the argument to x_count() is 40 then a UTF-8 error is reported** on the step function. If x_count(41) is seen, then a UTF-16 error** is reported on the step function. If the total count is 42, then** a UTF-8 error is reported on the finalize function.*/typedef struct CountCtx CountCtx;struct CountCtx { int n;};static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ CountCtx *p; p = sqlite3_aggregate_context(context, sizeof(*p)); if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){ p->n++; } if( argc>0 ){ int v = sqlite3_value_int(argv[0]); if( v==40 ){ sqlite3_result_error(context, "value of 40 handed to x_count", -1);#ifndef SQLITE_OMIT_UTF16 }else if( v==41 ){ const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0}; sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1);#endif } }} static void countFinalize(sqlite3_context *context){ CountCtx *p; p = sqlite3_aggregate_context(context, sizeof(*p)); if( p ){ if( p->n==42 ){ sqlite3_result_error(context, "x_count totals to 42", -1); }else{ sqlite3_result_int(context, p ? p->n : 0); } }}/*** Usage: sqlite_test_create_aggregate DB**** Call the sqlite3_create_function API on the given database in order** to create a function named "x_count". This function does the same thing** as the "md5sum" function.**** The original motivation for this routine was to be able to call the** sqlite3_create_aggregate function while a query is in progress in order** to test the SQLITE_MISUSE detection logic. See misuse.test.**** This routine was later extended to test the use of sqlite3_result_error()** within aggregate functions.*/static int test_create_aggregate( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ char **argv /* Text of each argument */){ sqlite3 *db; int rc; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " FILENAME\"", 0); return TCL_ERROR; } if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0, countStep,countFinalize); if( rc==SQLITE_OK ){ sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0, countStep,countFinalize); } if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; return TCL_OK;}/*** Usage: printf TEXT**** Send output to printf. Use this rather than puts to merge the output** in the correct sequence with debugging printfs inserted into C code.** Puts uses a separate buffer and debugging statements will be out of** sequence if it is used.*/static int test_printf( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ char **argv /* Text of each argument */){ if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " TEXT\"", 0); return TCL_ERROR; } printf("%s\n", argv[1]); return TCL_OK;}/*** Usage: sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER**** Call mprintf with three integer arguments*/static int sqlite3_mprintf_int( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ char **argv /* Text of each argument */){ int a[3], i; char *z; if( argc!=5 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " FORMAT INT INT INT\"", 0); return TCL_ERROR; } for(i=2; i<5; i++){ if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; } z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]); Tcl_AppendResult(interp, z, 0); sqlite3_free(z); return TCL_OK;}/*** If zNum represents an integer that will fit in 64-bits, then set** *pValue to that integer and return true. Otherwise return false.*/static int sqlite3GetInt64(const char *zNum, i64 *pValue){ if( sqlite3FitsIn64Bits(zNum) ){ sqlite3atoi64(zNum, pValue); return 1; } return 0;}/*** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER**** Call mprintf with three 64-bit integer arguments*/static int sqlite3_mprintf_int64( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ char **argv /* Text of each argument */){ int i; sqlite_int64 a[3]; char *z; if( argc!=5 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " FORMAT INT INT INT\"", 0); return TCL_ERROR; } for(i=2; i<5; i++){ if( !sqlite3GetInt64(argv[i], &a[i-2]) ){ Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", 0); return TCL_ERROR; } } z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]); Tcl_AppendResult(interp, z, 0); sqlite3_free(z); return TCL_OK;}/*** Usage: sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING**** Call mprintf with two integer arguments and one string argument*/static int sqlite3_mprintf_str( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ char **argv /* Text of each argument */){ int a[3], i; char *z; if( argc<4 || argc>5 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " FORMAT INT INT ?STRING?\"", 0); return TCL_ERROR; } for(i=2; i<4; i++){ if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; } z = sqlite3_mprintf(argv[1], a[0], a[1], argc>4 ? argv[4] : NULL); Tcl_AppendResult(interp, z, 0); sqlite3_free(z); return TCL_OK;}/*** Usage: sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE**** Call mprintf with two integer arguments and one double argument
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -