📄 test1.c
字号:
*/static int rememberDataTypes(void *pArg, int nCol, char **argv, char **colv){ int i; Tcl_Interp *interp = (Tcl_Interp*)pArg; Tcl_Obj *pList, *pElem; if( colv[nCol+1]==0 ){ return 1; } pList = Tcl_NewObj(); for(i=0; i<nCol; i++){ pElem = Tcl_NewStringObj(colv[i+nCol] ? colv[i+nCol] : "NULL", -1); Tcl_ListObjAppendElement(interp, pList, pElem); } Tcl_SetObjResult(interp, pList); return 1;}/*** Invoke an SQL statement but ignore all the data in the result. Instead,** return a list that consists of the datatypes of the various columns.**** This only works if "PRAGMA show_datatypes=on" has been executed against** the database connection.*/static int sqlite_datatypes( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ char **argv /* Text of each argument */){ sqlite *db; int rc; if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB SQL", 0); return TCL_ERROR; } if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; rc = sqlite_exec(db, argv[2], rememberDataTypes, interp, 0); if( rc!=0 && rc!=SQLITE_ABORT ){ Tcl_AppendResult(interp, sqlite_error_string(rc), 0); return TCL_ERROR; } return TCL_OK;}/*** Usage: sqlite_compile DB SQL ?TAILVAR?**** Attempt to compile an SQL statement. Return a pointer to the virtual** machine used to execute that statement. Unprocessed SQL is written** into TAILVAR.*/static int test_compile( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ char **argv /* Text of each argument */){ sqlite *db; sqlite_vm *vm; int rc; char *zErr = 0; const char *zTail; char zBuf[50]; if( argc!=3 && argc!=4 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB SQL TAILVAR", 0); return TCL_ERROR; } if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; rc = sqlite_compile(db, argv[2], argc==4 ? &zTail : 0, &vm, &zErr); if( argc==4 ) Tcl_SetVar(interp, argv[3], zTail, 0); if( rc ){ assert( vm==0 ); sprintf(zBuf, "(%d) ", rc); Tcl_AppendResult(interp, zBuf, zErr, 0); sqlite_freemem(zErr); return TCL_ERROR; } if( vm ){ if( makePointerStr(interp, zBuf, vm) ) return TCL_ERROR; Tcl_AppendResult(interp, zBuf, 0); } return TCL_OK;}/*** Usage: sqlite_step VM ?NVAR? ?VALUEVAR? ?COLNAMEVAR?**** Step a virtual machine. Return a the result code as a string.** Column results are written into three variables.*/static int test_step( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ char **argv /* Text of each argument */){ sqlite_vm *vm; int rc, i; const char **azValue = 0; const char **azColName = 0; int N = 0; char *zRc; char zBuf[50]; if( argc<2 || argc>5 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " VM NVAR VALUEVAR COLNAMEVAR", 0); return TCL_ERROR; } if( getVmPointer(interp, argv[1], &vm) ) return TCL_ERROR; rc = sqlite_step(vm, argc>=3?&N:0, argc>=4?&azValue:0, argc==5?&azColName:0); if( argc>=3 ){ sprintf(zBuf, "%d", N); Tcl_SetVar(interp, argv[2], zBuf, 0); } if( argc>=4 ){ Tcl_SetVar(interp, argv[3], "", 0); if( azValue ){ for(i=0; i<N; i++){ Tcl_SetVar(interp, argv[3], azValue[i] ? azValue[i] : "", TCL_APPEND_VALUE | TCL_LIST_ELEMENT); } } } if( argc==5 ){ Tcl_SetVar(interp, argv[4], "", 0); if( azColName ){ for(i=0; i<N*2; i++){ Tcl_SetVar(interp, argv[4], azColName[i] ? azColName[i] : "", TCL_APPEND_VALUE | TCL_LIST_ELEMENT); } } } switch( rc ){ case SQLITE_DONE: zRc = "SQLITE_DONE"; break; case SQLITE_BUSY: zRc = "SQLITE_BUSY"; break; case SQLITE_ROW: zRc = "SQLITE_ROW"; break; case SQLITE_ERROR: zRc = "SQLITE_ERROR"; break; case SQLITE_MISUSE: zRc = "SQLITE_MISUSE"; break; default: zRc = "unknown"; break; } Tcl_AppendResult(interp, zRc, 0); return TCL_OK;}/*** Usage: sqlite_finalize VM **** Shutdown a virtual machine.*/static int test_finalize( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ char **argv /* Text of each argument */){ sqlite_vm *vm; int rc; char *zErrMsg = 0; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " VM\"", 0); return TCL_ERROR; } if( getVmPointer(interp, argv[1], &vm) ) return TCL_ERROR; rc = sqlite_finalize(vm, &zErrMsg); if( rc ){ char zBuf[50]; sprintf(zBuf, "(%d) ", rc); Tcl_AppendResult(interp, zBuf, zErrMsg, 0); sqlite_freemem(zErrMsg); return TCL_ERROR; } return TCL_OK;}/*** Usage: sqlite_reset VM **** Reset a virtual machine and prepare it to be run again.*/static int test_reset( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ char **argv /* Text of each argument */){ sqlite_vm *vm; int rc; char *zErrMsg = 0; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " VM\"", 0); return TCL_ERROR; } if( getVmPointer(interp, argv[1], &vm) ) return TCL_ERROR; rc = sqlite_reset(vm, &zErrMsg); if( rc ){ char zBuf[50]; sprintf(zBuf, "(%d) ", rc); Tcl_AppendResult(interp, zBuf, zErrMsg, 0); sqlite_freemem(zErrMsg); return TCL_ERROR; } return TCL_OK;}/*** This is the "static_bind_value" that variables are bound to when** the FLAG option of sqlite_bind is "static"*/static char *sqlite_static_bind_value = 0;/*** Usage: sqlite_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.*/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 */){ sqlite_vm *vm; 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( getVmPointer(interp, argv[1], &vm) ) return TCL_ERROR; if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR; if( strcmp(argv[4],"null")==0 ){ rc = sqlite_bind(vm, idx, 0, 0, 0); }else if( strcmp(argv[4],"static")==0 ){ rc = sqlite_bind(vm, idx, sqlite_static_bind_value, -1, 0); }else if( strcmp(argv[4],"normal")==0 ){ rc = sqlite_bind(vm, idx, argv[3], -1, 1); }else{ Tcl_AppendResult(interp, "4th argument should be " "\"null\" or \"static\" or \"normal\"", 0); return TCL_ERROR; } if( rc ){ char zBuf[50]; sprintf(zBuf, "(%d) ", rc); Tcl_AppendResult(interp, zBuf, sqlite_error_string(rc), 0); return TCL_ERROR; } return TCL_OK;}/*** Usage: breakpoint**** This routine exists for one purpose - to provide a place to put a** breakpoint with GDB that can be triggered using TCL code. The use** for this is when a particular test fails on (say) the 1485th iteration.** In the TCL test script, we can add code like this:**** if {$i==1485} breakpoint**** Then run testfixture in the debugger and wait for the breakpoint to** fire. Then additional breakpoints can be set to trace down the bug.*/static int test_breakpoint( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ char **argv /* Text of each argument */){ return TCL_OK; /* Do nothing */}/*** Register commands with the TCL interpreter.*/int Sqlitetest1_Init(Tcl_Interp *interp){ extern int sqlite_search_count; extern int sqlite_interrupt_count; extern int sqlite_open_file_count; extern int sqlite_current_time; extern int sqlite_temp_directory; static struct { char *zName; Tcl_CmdProc *xProc; } aCmd[] = { { "sqlite_mprintf_int", (Tcl_CmdProc*)sqlite_mprintf_int }, { "sqlite_mprintf_str", (Tcl_CmdProc*)sqlite_mprintf_str }, { "sqlite_mprintf_double", (Tcl_CmdProc*)sqlite_mprintf_double }, { "sqlite_mprintf_scaled", (Tcl_CmdProc*)sqlite_mprintf_scaled }, { "sqlite_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z }, { "sqlite_open", (Tcl_CmdProc*)sqlite_test_open }, { "sqlite_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid }, { "sqlite_exec_printf", (Tcl_CmdProc*)test_exec_printf }, { "sqlite_get_table_printf", (Tcl_CmdProc*)test_get_table_printf }, { "sqlite_close", (Tcl_CmdProc*)sqlite_test_close }, { "sqlite_create_function", (Tcl_CmdProc*)test_create_function }, { "sqlite_create_aggregate", (Tcl_CmdProc*)test_create_aggregate }, { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func }, { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort }, { "sqlite_datatypes", (Tcl_CmdProc*)sqlite_datatypes },#ifdef MEMORY_DEBUG { "sqlite_malloc_fail", (Tcl_CmdProc*)sqlite_malloc_fail }, { "sqlite_malloc_stat", (Tcl_CmdProc*)sqlite_malloc_stat },#endif { "sqlite_compile", (Tcl_CmdProc*)test_compile }, { "sqlite_step", (Tcl_CmdProc*)test_step }, { "sqlite_finalize", (Tcl_CmdProc*)test_finalize }, { "sqlite_bind", (Tcl_CmdProc*)test_bind }, { "sqlite_reset", (Tcl_CmdProc*)test_reset }, { "breakpoint", (Tcl_CmdProc*)test_breakpoint }, }; int i; for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); } Tcl_LinkVar(interp, "sqlite_search_count", (char*)&sqlite_search_count, TCL_LINK_INT); Tcl_LinkVar(interp, "sqlite_interrupt_count", (char*)&sqlite_interrupt_count, TCL_LINK_INT); Tcl_LinkVar(interp, "sqlite_open_file_count", (char*)&sqlite_open_file_count, TCL_LINK_INT); Tcl_LinkVar(interp, "sqlite_current_time", (char*)&sqlite_current_time, TCL_LINK_INT); Tcl_LinkVar(interp, "sqlite_static_bind_value", (char*)&sqlite_static_bind_value, TCL_LINK_STRING); Tcl_LinkVar(interp, "sqlite_temp_directory", (char*)&sqlite_temp_directory, TCL_LINK_STRING); return TCL_OK;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -