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

📄 test1.c

📁 最新的sqlite3.6.2源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
    p2 = (const void*)sqlite3_value_blob(argv[0]);  }else{    return;  }  sqlite3_result_int(context, p1!=p2);}/*** 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;  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,         t1_ifnullFunc, 0, 0);  if( rc==SQLITE_OK ){    rc = sqlite3_create_function(db, "hex8", 1, SQLITE_ANY, 0,           hex8Func, 0, 0);  }#ifndef SQLITE_OMIT_UTF16  if( rc==SQLITE_OK ){    rc = sqlite3_create_function(db, "hex16", 1, SQLITE_ANY, 0,           hex16Func, 0, 0);  }#endif  if( rc==SQLITE_OK ){    rc = sqlite3_create_function(db, "tkt2213func", 1, SQLITE_ANY, 0,           tkt2213Function, 0, 0);  }  if( rc==SQLITE_OK ){    rc = sqlite3_create_function(db, "pointer_change", 4, SQLITE_ANY, 0,           ptrChngFunction, 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 ){    const void *zUtf16;    sqlite3_value *pVal;    sqlite3_mutex_enter(db->mutex);    pVal = sqlite3ValueNew(db);    sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC);    zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);    if( db->mallocFailed ){      rc = SQLITE_NOMEM;    }else{      rc = sqlite3_create_function16(db, zUtf16,                 1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0);    }    sqlite3ValueFree(pVal);    sqlite3_mutex_leave(db->mutex);  }#endif  if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;  Tcl_SetResult(interp, (char *)t1ErrorName(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 t1CountCtx t1CountCtx;struct t1CountCtx {  int n;};static void t1CountStep(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  t1CountCtx *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 t1CountFinalize(sqlite3_context *context){  t1CountCtx *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);    }  }}static void legacyCountStep(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  /* no-op */}static void legacyCountFinalize(sqlite3_context *context){  sqlite3_result_int(context, sqlite3_aggregate_count(context));}/*** Usage:  sqlite3_create_aggregate DB**** Call the sqlite3_create_function API on the given database in order** to create a function named "x_count".  This function is similar** to the built-in count() function, with a few special quirks** for testing the sqlite3_result_error() APIs.**** 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.**** Later: It is now also extended to register the aggregate function** "legacy_count()" with the supplied database handle. This is used** to test the deprecated sqlite3_aggregate_count() API.*/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,      t1CountStep,t1CountFinalize);  if( rc==SQLITE_OK ){    rc = sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0,        t1CountStep,t1CountFinalize);  }  if( rc==SQLITE_OK ){    rc = sqlite3_create_function(db, "legacy_count", 0, SQLITE_ANY, 0, 0,        legacyCountStep, legacyCountFinalize    );  }  if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;  Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);  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, 0) ){    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_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING**** Call mprintf with two integer arguments and one string argument*/static int sqlite3_snprintf_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;  int n;  char *z;  if( argc<5 || argc>6 ){    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],       " INT FORMAT INT INT ?STRING?\"", 0);    return TCL_ERROR;  }  if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;  if( n<0 ){    Tcl_AppendResult(interp, "N must be non-negative", 0);    return TCL_ERROR;  }  for(i=3; i<5; i++){    if( Tcl_GetInt(interp, argv[i], &a[i-3]) ) return TCL_ERROR;  }  z = sqlite3_malloc( n+1 );  sqlite3_snprintf(n, z, argv[2], a[0], a[1], argc>4 ? argv[5] : 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*/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 DOUBLE\"", 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_scaled 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:  sqlite3_mprintf_hexdouble FORMAT HEX**** Call mprintf with a single double argument which is derived from the** hexadecimal encoding of an IEEE double.*/static int sqlite3_mprintf_hexdouble(  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;  double r;  unsigned int x1, x2;

⌨️ 快捷键说明

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