⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test1.c

📁 一个小型的嵌入式数据库
💻 C
📖 第 1 页 / 共 5 页
字号:
** 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);#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;    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.*/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++;  }}   static void countFinalize(sqlite3_context *context){  CountCtx *p;  p = sqlite3_aggregate_context(context, sizeof(*p));  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.*/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:  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_str FORMAT INTEGER INTEGER DOUBLE**** Call mprintf with two integer arguments and one double argument*/static int sqlite3_mprintf_double(  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;  double r;  char *z;  if( 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;  }  if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR;  z = sqlite3_mprintf(argv[1], a[0], a[1], r);  Tcl_AppendResult(interp, z, 0);  sqlite3_free(z);  return TCL_OK;}/*** Usage:  sqlite3_mprintf_str FORMAT DOUBLE DOUBLE**** Call mprintf with a single double argument which is the product of the** two arguments given above.  This is used to generate overflow and underflow** doubles to test that they are converted properly.*/static int sqlite3_mprintf_scaled(  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;  double r[2];  char *z;  if( argc!=4 ){    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],       " FORMAT DOUBLE DOUBLE\"", 0);    return TCL_ERROR;  }  for(i=2; i<4; i++){    if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR;  }  z = sqlite3_mprintf(argv[1], r[0]*r[1]);  Tcl_AppendResult(interp, z, 0);  sqlite3_free(z);  return TCL_OK;}/*** Usage:  sqlite3_mprintf_stronly FORMAT STRING**** Call mprintf with a single double argument which is the product of the** two arguments given above.  This is used to generate overflow and underflow** doubles to test that they are converted properly.*/static int sqlite3_mprintf_stronly(  void *NotUsed,  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */  int argc,              /* Number of arguments */  char **argv            /* Text of each argument */){  char *z;  if( argc!=3 ){    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],       " FORMAT STRING\"", 0);    return TCL_ERROR;  }  z = sqlite3_mprintf(argv[1], argv[2]);  Tcl_AppendResult(interp, z, 0);  sqlite3_free(z);  return TCL_OK;}/*** Usage: sqlite_malloc_fail N  ?REPEAT-INTERVAL?**** Rig sqliteMalloc() to fail on the N-th call and every REPEAT-INTERVAL call** after that.  If REPEAT-INTERVAL is 0 or is omitted, then only a single** malloc will fail.  If REPEAT-INTERVAL is 1 then all mallocs after the** first failure will continue to fail on every call.  If REPEAT-INTERVAL is** 2 then every other malloc will fail.  And so forth.**** Turn off this mechanism and reset the sqlite3_malloc_failed variable is N==0.*/#ifdef SQLITE_MEMDEBUGstatic int sqlite_malloc_fail(  void *NotUsed,  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */  int argc,              /* Number of arguments */  char **argv            /* Text of each argument */){  int n;  int rep;  if( argc!=2 && argc!=3 ){    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " N\"", 0);    return TCL_ERROR;  }  if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;  if( argc==3 ){    if( Tcl_GetInt(interp, argv[2], &rep) ) return TCL_ERROR;  }else{    rep = 0;  }  sqlite3_iMallocFail = n;  sqlite3_iMallocReset = rep;  sqlite3_malloc_failed = 0;  return TCL_OK;}#endif/*** Usage: sqlite_malloc_stat**** Return the number of prior calls to sqliteMalloc() and sqliteFree().*/#ifdef SQLITE_MEMDEBUGstatic int sqlite_malloc_stat(  void *NotUsed,  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */  int argc,              /* Number of arguments */  char **argv            /* Text of each argument */){  char zBuf[200];  sprintf(zBuf, "%d %d %d", sqlite3_nMalloc, sqlite3_nFree, sqlite3_iMallocFail);  Tcl_AppendResult(interp, zBuf, 0);  return TCL_OK;}#endif/*** Usage:  sqlite_abort**** Shutdown the process immediately.  This is not a clean shutdown.** This command is used to test the recoverability of a database in** the event of a program crash.*/static int sqlite_abort(  void *NotUsed,  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */  int argc,              /* Number of arguments */  char **argv            /* Text of each argument */){  assert( interp==0 );   /* This will always fail */  return TCL_OK;}/*** The following routine is a user-defined SQL function whose purpose** is to test the sqlite_set_result() API.*/static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){  while( argc>=2 ){    const char *zArg0 = sqlite3_value_text(argv[0]);    if( zArg0 ){      if( 0==sqlite3StrICmp(zArg0, "int") ){        sqlite3_result_int(context, sqlite3_value_int(argv[1]));      }else if( sqlite3StrICmp(zArg0,"int64")==0 ){        sqlite3_result_int64(context, sqlite3_value_int64(argv[1]));      }else if( sqlite3StrICmp(zArg0,"string")==0 ){        sqlite3_result_text(context, sqlite3_value_text(argv[1]), -1,            SQLITE_TRANSIENT);      }else if( sqlite3StrICmp(zArg0,"double")==0 ){        sqlite3_result_double(context, sqlite3_value_double(argv[1]));      }else if( sqlite3StrICmp(zArg0,"null")==0 ){        sqlite3_result_null(context);      }else if( sqlite3StrICmp(zArg0,"value")==0 ){        sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]);      }else{        goto error_out;      }    }else{      goto error_out;    }    argc -= 2;    argv += 2;  }  return;error_out:  sqlite3_result_error(context,"first argument should be one of: "      "int int64 string double null value", -1);}/*** Usage:   sqlite_register_test_function  DB  NAME**** Register the test SQL function on the database DB under the name NAME.*/static int test_register_func(  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!=3 ){    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],        " DB FUNCTION-NAME", 0);    return TCL_ERROR;  }  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;  rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0,       testFunc, 0, 0);  if( rc!=0 ){    Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);    return TCL_ERROR;  }  if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;  return TCL_OK;}/*** Usage:  sqlite3_finalize  STMT **** Finalize a statement handle.*/static int test_finalize(  void * clientData,  Tcl_Interp *interp,  int objc,  Tcl_Obj *CONST objv[]){  sqlite3_stmt *pStmt;  int rc;  sqlite3 *db;  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;  if( pStmt ){    db = StmtToDb(pStmt);  }  rc = sqlite3_finalize(pStmt);  Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);  if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;  return TCL_OK;}/*** Usage:  sqlite3_reset  STMT **** Finalize a statement handle.*/static int test_reset(  void * clientData,  Tcl_Interp *interp,  int objc,  Tcl_Obj *CONST objv[]){  sqlite3_stmt *pStmt;  int rc;  if( objc!=2 ){    Tcl_AppendResult(interp, "wrong # args: should be \"",        Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -