📄 test1.c
字号:
**** Return TRUE if a recompilation of the statement is recommended.*/static int test_expired( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]){ sqlite3_stmt *pStmt; if( objc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); return TCL_ERROR; } if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt))); return TCL_OK;}/*** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT**** Transfer all bindings from FROMSTMT over to TOSTMT*/static int test_transfer_bind( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]){ sqlite3_stmt *pStmt1, *pStmt2; if( objc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0); return TCL_ERROR; } if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR; if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR; Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2))); return TCL_OK;}/*** Usage: sqlite3_changes DB**** Return the number of changes made to the database by the last SQL** execution.*/static int test_changes( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]){ sqlite3 *db; if( objc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", Tcl_GetString(objv[0]), " DB", 0); return TCL_ERROR; } if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db))); return TCL_OK;}/*** This is the "static_bind_value" that variables are bound to when** the FLAG option of sqlite3_bind is "static"*/static char *sqlite_static_bind_value = 0;/*** Usage: sqlite3_bind VM IDX VALUE FLAGS**** Sets the value of the IDX-th occurance of "?" in the original SQL** string. VALUE is the new value. If FLAGS=="null" then VALUE is** ignored and the value is set to NULL. If FLAGS=="static" then** the value is set to the value of a static variable named** "sqlite_static_bind_value". If FLAGS=="normal" then a copy** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored** an a 10-byte blob "abc\000xyz\000pq" is inserted.*/static int test_bind( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ char **argv /* Text of each argument */){ sqlite3_stmt *pStmt; int rc; int idx; if( argc!=5 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " VM IDX VALUE (null|static|normal)\"", 0); return TCL_ERROR; } if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR; if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR; if( strcmp(argv[4],"null")==0 ){ rc = sqlite3_bind_null(pStmt, idx); }else if( strcmp(argv[4],"static")==0 ){ rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0); }else if( strcmp(argv[4],"normal")==0 ){ rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT); }else if( strcmp(argv[4],"blob10")==0 ){ rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC); }else{ Tcl_AppendResult(interp, "4th argument should be " "\"null\" or \"static\" or \"normal\"", 0); return TCL_ERROR; } if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; if( rc ){ char zBuf[50]; sprintf(zBuf, "(%d) ", rc); Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0); return TCL_ERROR; } return TCL_OK;}#ifndef SQLITE_OMIT_UTF16/*** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be>**** This function is used to test that SQLite selects the correct collation** sequence callback when multiple versions (for different text encodings)** are available.**** Calling this routine registers the collation sequence "test_collate"** with database handle <db>. The second argument must be a list of three** boolean values. If the first is true, then a version of test_collate is** registered for UTF-8, if the second is true, a version is registered for** UTF-16le, if the third is true, a UTF-16be version is available.** Previous versions of test_collate are deleted.**** The collation sequence test_collate is implemented by calling the** following TCL script:**** "test_collate <enc> <lhs> <rhs>"**** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8.** The <enc> parameter is the encoding of the collation function that** SQLite selected to call. The TCL test script implements the** "test_collate" proc.**** Note that this will only work with one intepreter at a time, as the** interp pointer to use when evaluating the TCL script is stored in** pTestCollateInterp.*/static Tcl_Interp* pTestCollateInterp;static int test_collate_func( void *pCtx, int nA, const void *zA, int nB, const void *zB){ Tcl_Interp *i = pTestCollateInterp; int encin = (int)pCtx; int res; int n; sqlite3_value *pVal; Tcl_Obj *pX; pX = Tcl_NewStringObj("test_collate", -1); Tcl_IncrRefCount(pX); switch( encin ){ case SQLITE_UTF8: Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1)); break; case SQLITE_UTF16LE: Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1)); break; case SQLITE_UTF16BE: Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1)); break; default: assert(0); } pVal = sqlite3ValueNew(); sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC); n = sqlite3_value_bytes(pVal); Tcl_ListObjAppendElement(i,pX, Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n)); sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC); n = sqlite3_value_bytes(pVal); Tcl_ListObjAppendElement(i,pX, Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n)); sqlite3ValueFree(pVal); Tcl_EvalObjEx(i, pX, 0); Tcl_DecrRefCount(pX); Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res); return res;}static int test_collate( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]){ sqlite3 *db; int val; sqlite3_value *pVal; int rc; if( objc!=5 ) goto bad_args; pTestCollateInterp = interp; if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8, (void *)SQLITE_UTF8, val?test_collate_func:0); if( rc==SQLITE_OK ){ if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR; rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE, (void *)SQLITE_UTF16LE, val?test_collate_func:0); if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;#ifdef SQLITE_MEMDEBUG if( sqlite3_iMallocFail>0 ){ sqlite3_iMallocFail++; }#endif pVal = sqlite3ValueNew(); sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC); rc = sqlite3_create_collation16(db, sqlite3ValueText(pVal, SQLITE_UTF16NATIVE), SQLITE_UTF16BE, (void *)SQLITE_UTF16BE, val?test_collate_func:0); sqlite3ValueFree(pVal); } if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; if( rc!=SQLITE_OK ){ Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0); return TCL_ERROR; } return TCL_OK;bad_args: Tcl_AppendResult(interp, "wrong # args: should be \"", Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0); return TCL_ERROR;}/*** When the collation needed callback is invoked, record the name of ** the requested collating function here. The recorded name is linked** to a TCL variable and used to make sure that the requested collation** name is correct.*/static char zNeededCollation[200];static char *pzNeededCollation = zNeededCollation;/*** Called when a collating sequence is needed. Registered using** sqlite3_collation_needed16().*/static void test_collate_needed_cb( void *pCtx, sqlite3 *db, int eTextRep, const void *pName){ int enc = ENC(db); int i; char *z; for(z = (char*)pName, i=0; *z || z[1]; z++){ if( *z ) zNeededCollation[i++] = *z; } zNeededCollation[i] = 0; sqlite3_create_collation( db, "test_collate", ENC(db), (void *)enc, test_collate_func);}/*** Usage: add_test_collate_needed DB*/static int test_collate_needed( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]){ sqlite3 *db; int rc; if( objc!=2 ) goto bad_args; if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb); zNeededCollation[0] = 0; if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; return TCL_OK;bad_args: Tcl_WrongNumArgs(interp, 1, objv, "DB"); return TCL_ERROR;}/*** tclcmd: add_alignment_test_collations DB**** Add two new collating sequences to the database DB**** utf16_aligned** utf16_unaligned**** Both collating sequences use the same sort order as BINARY.** The only difference is that the utf16_aligned collating** sequence is declared with the SQLITE_UTF16_ALIGNED flag.** Both collating functions increment the unaligned utf16 counter** whenever they see a string that begins on an odd byte boundary.*/static int unaligned_string_counter = 0;static int alignmentCollFunc( void *NotUsed, int nKey1, const void *pKey1, int nKey2, const void *pKey2){ int rc, n; n = nKey1<nKey2 ? nKey1 : nKey2; if( nKey1>0 && 1==(1&(int)pKey1) ) unaligned_string_counter++; if( nKey2>0 && 1==(1&(int)pKey2) ) unaligned_string_counter++; rc = memcmp(pKey1, pKey2, n); if( rc==0 ){ rc = nKey1 - nKey2; } return rc;}static int add_alignment_test_collations( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]){ sqlite3 *db; if( objc>=2 ){ if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; sqlite3_create_collation(db, "utf16_unaligned", SQLITE_UTF16, 0, alignmentCollFunc); sqlite3_create_collation(db, "utf16_aligned", SQLITE_UTF16 | SQLITE_UTF16_ALIGNED, 0, alignmentCollFunc); } return SQLITE_OK;}#endif /* !defined(SQLITE_OMIT_UTF16) *//*** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be>**** This function is used to test that SQLite selects the correct user** function callback when multiple versions (for different text encodings)** are available.**** Calling this routine registers up to three versions of the user function** "test_function" with database handle <db>. If the second argument is** true, then a version of test_function is registered for UTF-8, if the** third is true, a version is registered for UTF-16le, if the fourth is** true, a UTF-16be version is available. Previous versions of** test_function are deleted.**** The user function is implemented by calling the following TCL script:**** "test_function <enc> <arg>"**** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the** single argument passed to the SQL function. The value returned by** the TCL script is used as the return value of the SQL function. It** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8** for a UTF-16LE test_function(), and UTF-16LE for an implementation that** prefers UTF-16BE.*/#ifndef SQLITE_OMIT_UTF16static void test_function_utf8( sqlite3_context *pCtx, int nArg, sqlite3_value **argv){ Tcl_Interp *interp; Tcl_Obj *pX; sqlite3_value *pVal; interp = (Tcl_Interp *)sqlite3_user_data(pCtx); pX = Tcl_NewStringObj("test_function", -1); Tcl_IncrRefCount(pX); Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1)); Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); Tcl_EvalObjEx(interp, pX, 0); Tcl_DecrRefCount(pX); sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT); pVal = sqlite3ValueNew(); sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), SQLITE_UTF8, SQLITE_STATIC); sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal), -1, SQLITE_TRANSIENT); sqlite3ValueFree(pVal);}static void test_function_utf16le( sqlite3_context *pCtx, int nArg, sqlite3_value **argv){ Tcl_Interp *interp; Tcl_Obj *pX; sqlite3_value *pVal; interp = (Tcl_Interp *)sqlite3_user_data(pCtx); pX = Tcl_NewStringObj("test_function", -1); Tcl_IncrRefCount(pX); Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1)); Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); Tcl_EvalObjEx(interp, pX, 0); Tcl_DecrRefCount(pX); pVal = sqlite3ValueNew(); sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), SQLITE_UTF8, SQLITE_STATIC); sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT); sqlite3ValueFree(pVal);}static void test_function_utf16be( sqlite3_context *pCtx, int nArg, sqlite3_value **argv){ Tcl_Interp *interp; Tcl_Obj *pX; sqlite3_value *pVal; interp = (Tcl_Interp *)sqlite3_user_data(pCtx); pX = Tcl_NewStringObj("test_function", -1); Tcl_IncrRefCount(pX); Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1)); Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); Tcl_EvalObjEx(interp, pX, 0); Tcl_DecrRefCount(pX); pVal = sqlite3ValueNew(); sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), SQLITE_UTF8, SQLITE_STATIC); sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal), -1, SQLITE_TRANSIENT); sqlite3ValueFree(pVal);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -