blob: ffd6091fa36623acd4b1fecc14ae68510d2355c4 [file] [log] [blame]
/*
** 2001 September 15
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
** Code for testing all sorts of SQLite interfaces. This code
** is not included in the SQLite library. It is used for automated
** testing of the SQLite library.
*/
#include "sqliteInt.h"
#if SQLITE_OS_WIN
# include "os_win.h"
#endif
#include "vdbeInt.h"
#if defined(INCLUDE_SQLITE_TCL_H)
# include "sqlite_tcl.h"
#else
# include "tcl.h"
#endif
#include <stdlib.h>
#include <string.h>
/*
** This is a copy of the first part of the SqliteDb structure in
** tclsqlite.c. We need it here so that the get_sqlite_pointer routine
** can extract the sqlite3* pointer from an existing Tcl SQLite
** connection.
*/
struct SqliteDb {
sqlite3 *db;
};
/*
** Convert text generated by the "%p" conversion format back into
** a pointer.
*/
static int testHexToInt(int h){
if( h>='0' && h<='9' ){
return h - '0';
}else if( h>='a' && h<='f' ){
return h - 'a' + 10;
}else{
assert( h>='A' && h<='F' );
return h - 'A' + 10;
}
}
void *sqlite3TestTextToPtr(const char *z){
void *p;
u64 v;
u32 v2;
if( z[0]=='0' && z[1]=='x' ){
z += 2;
}
v = 0;
while( *z ){
v = (v<<4) + testHexToInt(*z);
z++;
}
if( sizeof(p)==sizeof(v) ){
memcpy(&p, &v, sizeof(p));
}else{
assert( sizeof(p)==sizeof(v2) );
v2 = (u32)v;
memcpy(&p, &v2, sizeof(p));
}
return p;
}
/*
** A TCL command that returns the address of the sqlite* pointer
** for an sqlite connection instance. Bad things happen if the
** input is not an sqlite connection.
*/
static int SQLITE_TCLAPI get_sqlite_pointer(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
struct SqliteDb *p;
Tcl_CmdInfo cmdInfo;
char zBuf[100];
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "SQLITE-CONNECTION");
return TCL_ERROR;
}
if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
Tcl_AppendResult(interp, "command not found: ",
Tcl_GetString(objv[1]), (char*)0);
return TCL_ERROR;
}
p = (struct SqliteDb*)cmdInfo.objClientData;
sqlite3_snprintf(sizeof(zBuf), zBuf, "%p", p->db);
Tcl_AppendResult(interp, zBuf, 0);
return TCL_OK;
}
/*
** Decode a pointer to an sqlite3 object.
*/
int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
struct SqliteDb *p;
Tcl_CmdInfo cmdInfo;
if( Tcl_GetCommandInfo(interp, zA, &cmdInfo) ){
p = (struct SqliteDb*)cmdInfo.objClientData;
*ppDb = p->db;
}else{
*ppDb = (sqlite3*)sqlite3TestTextToPtr(zA);
}
return TCL_OK;
}
#if SQLITE_OS_WIN
/*
** Decode a Win32 HANDLE object.
*/
int getWin32Handle(Tcl_Interp *interp, const char *zA, LPHANDLE phFile){
*phFile = (HANDLE)sqlite3TestTextToPtr(zA);
return TCL_OK;
}
#endif
extern const char *sqlite3ErrName(int);
#define t1ErrorName sqlite3ErrName
/*
** Convert an sqlite3_stmt* into an sqlite3*. This depends on the
** fact that the sqlite3* is the first field in the Vdbe structure.
*/
#define StmtToDb(X) sqlite3_db_handle(X)
/*
** Check a return value to make sure it agrees with the results
** from sqlite3_errcode.
*/
int sqlite3TestErrCode(Tcl_Interp *interp, sqlite3 *db, int rc){
if( sqlite3_threadsafe()==0 && rc!=SQLITE_MISUSE && rc!=SQLITE_OK
&& sqlite3_errcode(db)!=rc ){
char zBuf[200];
int r2 = sqlite3_errcode(db);
sqlite3_snprintf(sizeof(zBuf), zBuf,
"error code %s (%d) does not match sqlite3_errcode %s (%d)",
t1ErrorName(rc), rc, t1ErrorName(r2), r2);
Tcl_ResetResult(interp);
Tcl_AppendResult(interp, zBuf, 0);
return 1;
}
return 0;
}
/*
** Decode a pointer to an sqlite3_stmt object.
*/
static int getStmtPointer(
Tcl_Interp *interp,
const char *zArg,
sqlite3_stmt **ppStmt
){
*ppStmt = (sqlite3_stmt*)sqlite3TestTextToPtr(zArg);
return TCL_OK;
}
/*
** Generate a text representation of a pointer that can be understood
** by the getDbPointer and getVmPointer routines above.
**
** The problem is, on some machines (Solaris) if you do a printf with
** "%p" you cannot turn around and do a scanf with the same "%p" and
** get your pointer back. You have to prepend a "0x" before it will
** work. Or at least that is what is reported to me (drh). But this
** behavior varies from machine to machine. The solution used her is
** to test the string right after it is generated to see if it can be
** understood by scanf, and if not, try prepending an "0x" to see if
** that helps. If nothing works, a fatal error is generated.
*/
int sqlite3TestMakePointerStr(Tcl_Interp *interp, char *zPtr, void *p){
sqlite3_snprintf(100, zPtr, "%p", p);
return TCL_OK;
}
/*
** The callback routine for sqlite3_exec_printf().
*/
static int exec_printf_cb(void *pArg, int argc, char **argv, char **name){
Tcl_DString *str = (Tcl_DString*)pArg;
int i;
if( Tcl_DStringLength(str)==0 ){
for(i=0; i<argc; i++){
Tcl_DStringAppendElement(str, name[i] ? name[i] : "NULL");
}
}
for(i=0; i<argc; i++){
Tcl_DStringAppendElement(str, argv[i] ? argv[i] : "NULL");
}
return 0;
}
/*
** The I/O tracing callback.
*/
#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
static FILE *iotrace_file = 0;
static void io_trace_callback(const char *zFormat, ...){
va_list ap;
va_start(ap, zFormat);
vfprintf(iotrace_file, zFormat, ap);
va_end(ap);
fflush(iotrace_file);
}
#endif
/*
** Usage: io_trace FILENAME
**
** Turn I/O tracing on or off. If FILENAME is not an empty string,
** I/O tracing begins going into FILENAME. If FILENAME is an empty
** string, I/O tracing is turned off.
*/
static int SQLITE_TCLAPI test_io_trace(
void *NotUsed,
Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
if( argc!=2 ){
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" FILENAME\"", 0);
return TCL_ERROR;
}
if( iotrace_file ){
if( iotrace_file!=stdout && iotrace_file!=stderr ){
fclose(iotrace_file);
}
iotrace_file = 0;
sqlite3IoTrace = 0;
}
if( argv[1][0] ){
if( strcmp(argv[1],"stdout")==0 ){
iotrace_file = stdout;
}else if( strcmp(argv[1],"stderr")==0 ){
iotrace_file = stderr;
}else{
iotrace_file = fopen(argv[1], "w");
}
sqlite3IoTrace = io_trace_callback;
}
#endif
return TCL_OK;
}
/*
** Usage: clang_sanitize_address
**
** Returns true if the program was compiled using clang with the
** -fsanitize=address switch on the command line. False otherwise.
**
** Also return true if the OMIT_MISUSE environment variable exists.
*/
static int SQLITE_TCLAPI clang_sanitize_address(
void *NotUsed,
Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
int res = 0;
#if defined(__has_feature)
# if __has_feature(address_sanitizer)
res = 1;
# endif
#endif
#ifdef __SANITIZE_ADDRESS__
res = 1;
#endif
if( res==0 && getenv("OMIT_MISUSE")!=0 ) res = 1;
Tcl_SetObjResult(interp, Tcl_NewIntObj(res));
return TCL_OK;
}
/*
** Usage: sqlite3_exec_printf DB FORMAT STRING
**
** Invoke the sqlite3_exec_printf() interface using the open database
** DB. The SQL is the string FORMAT. The format string should contain
** one %s or %q. STRING is the value inserted into %s or %q.
*/
static int SQLITE_TCLAPI test_exec_printf(
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;
Tcl_DString str;
int rc;
char *zErr = 0;
char *zSql;
char zBuf[30];
if( argc!=4 ){
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" DB FORMAT STRING", 0);
return TCL_ERROR;
}
if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
Tcl_DStringInit(&str);
zSql = sqlite3_mprintf(argv[2], argv[3]);
rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
sqlite3_free(zSql);
sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc);
Tcl_AppendElement(interp, zBuf);
Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
Tcl_DStringFree(&str);
if( zErr ) sqlite3_free(zErr);
if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
return TCL_OK;
}
/*
** Usage: sqlite3_exec_hex DB HEX
**
** Invoke the sqlite3_exec() on a string that is obtained by translating
** HEX into ASCII. Most characters are translated as is. %HH becomes
** a hex character.
*/
static int SQLITE_TCLAPI test_exec_hex(
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;
Tcl_DString str;
int rc, i, j;
char *zErr = 0;
char *zHex;
char zSql[501];
char zBuf[30];
if( argc!=3 ){
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" DB HEX", 0);
return TCL_ERROR;
}
if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
zHex = argv[2];
for(i=j=0; i<(sizeof(zSql)-1) && zHex[j]; i++, j++){
if( zHex[j]=='%' && zHex[j+2] && zHex[j+2] ){
zSql[i] = (testHexToInt(zHex[j+1])<<4) + testHexToInt(zHex[j+2]);
j += 2;
}else{
zSql[i] = zHex[j];
}
}
zSql[i] = 0;
Tcl_DStringInit(&str);
rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc);
Tcl_AppendElement(interp, zBuf);
Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
Tcl_DStringFree(&str);
if( zErr ) sqlite3_free(zErr);
if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
return TCL_OK;
}
/*
** Usage: db_enter DB
** db_leave DB
**
** Enter or leave the mutex on a database connection.
*/
static int SQLITE_TCLAPI db_enter(
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;
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;
sqlite3_mutex_enter(db->mutex);
return TCL_OK;
}
static int SQLITE_TCLAPI db_leave(
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;
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;
sqlite3_mutex_leave(db->mutex);
return TCL_OK;
}
/*
** Usage: sqlite3_exec DB SQL
**
** Invoke the sqlite3_exec interface using the open database DB
*/
static int SQLITE_TCLAPI test_exec(
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;
Tcl_DString str;
int rc;
char *zErr = 0;
char *zSql;
int i, j;
char zBuf[30];
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;
Tcl_DStringInit(&str);
zSql = sqlite3_mprintf("%s", argv[2]);
for(i=j=0; zSql[i];){
if( zSql[i]=='%' ){
zSql[j++] = (testHexToInt(zSql[i+1])<<4) + testHexToInt(zSql[i+2]);
i += 3;
}else{
zSql[j++] = zSql[i++];
}
}
zSql[j] = 0;
rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
sqlite3_free(zSql);
sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc);
Tcl_AppendElement(interp, zBuf);
Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
Tcl_DStringFree(&str);
if( zErr ) sqlite3_free(zErr);
if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
return TCL_OK;
}
/*
** Usage: sqlite3_exec_nr DB SQL
**
** Invoke the sqlite3_exec interface using the open database DB. Discard
** all results
*/
static int SQLITE_TCLAPI test_exec_nr(
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;
char *zErr = 0;
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 = sqlite3_exec(db, argv[2], 0, 0, &zErr);
if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
return TCL_OK;
}
/*
** Usage: sqlite3_mprintf_z_test SEPARATOR ARG0 ARG1 ...
**
** Test the %z format of sqlite_mprintf(). Use multiple mprintf() calls to
** concatenate arg0 through argn using separator as the separator.
** Return the result.
*/
static int SQLITE_TCLAPI test_mprintf_z(
void *NotUsed,
Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
char *zResult = 0;
int i;
for(i=2; i<argc && (i==2 || zResult); i++){
zResult = sqlite3_mprintf("%z%s%s", zResult, argv[1], argv[i]);
}
Tcl_AppendResult(interp, zResult, 0);
sqlite3_free(zResult);
return TCL_OK;
}
/*
** Usage: sqlite3_mprintf_n_test STRING
**
** Test the %n format of sqlite_mprintf(). Return the length of the
** input string.
*/
static int SQLITE_TCLAPI test_mprintf_n(
void *NotUsed,
Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
char *zStr;
int n = 0;
zStr = sqlite3_mprintf("%s%n", argv[1], &n);
sqlite3_free(zStr);
Tcl_SetObjResult(interp, Tcl_NewIntObj(n));
return TCL_OK;
}
/*
** Usage: sqlite3_snprintf_int SIZE FORMAT INT
**
** Test the of sqlite3_snprintf() routine. SIZE is the size of the
** output buffer in bytes. The maximum size is 100. FORMAT is the
** format string. INT is a single integer argument. The FORMAT
** string must require no more than this one integer argument. If
** You pass in a format string that requires more than one argument,
** bad things will happen.
*/
static int SQLITE_TCLAPI test_snprintf_int(
void *NotUsed,
Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
char zStr[100];
int n = atoi(argv[1]);
const char *zFormat = argv[2];
int a1 = atoi(argv[3]);
if( n>sizeof(zStr) ) n = sizeof(zStr);
sqlite3_snprintf(sizeof(zStr), zStr, "abcdefghijklmnopqrstuvwxyz");
sqlite3_snprintf(n, zStr, zFormat, a1);
Tcl_AppendResult(interp, zStr, 0);
return TCL_OK;
}
#ifndef SQLITE_OMIT_GET_TABLE
/*
** Usage: sqlite3_get_table_printf DB FORMAT STRING ?--no-counts?
**
** Invoke the sqlite3_get_table_printf() interface using the open database
** DB. The SQL is the string FORMAT. The format string should contain
** one %s or %q. STRING is the value inserted into %s or %q.
*/
static int SQLITE_TCLAPI test_get_table_printf(
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;
Tcl_DString str;
int rc;
char *zErr = 0;
int nRow = 0, nCol = 0;
char **aResult;
int i;
char zBuf[30];
char *zSql;
int resCount = -1;
if( argc==5 ){
if( Tcl_GetInt(interp, argv[4], &resCount) ) return TCL_ERROR;
}
if( argc!=4 && argc!=5 ){
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" DB FORMAT STRING ?COUNT?", 0);
return TCL_ERROR;
}
if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
Tcl_DStringInit(&str);
zSql = sqlite3_mprintf(argv[2],argv[3]);
if( argc==5 ){
rc = sqlite3_get_table(db, zSql, &aResult, 0, 0, &zErr);
}else{
rc = sqlite3_get_table(db, zSql, &aResult, &nRow, &nCol, &zErr);
resCount = (nRow+1)*nCol;
}
sqlite3_free(zSql);
sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc);
Tcl_AppendElement(interp, zBuf);
if( rc==SQLITE_OK ){
if( argc==4 ){
sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nRow);
Tcl_AppendElement(interp, zBuf);
sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nCol);
Tcl_AppendElement(interp, zBuf);
}
for(i=0; i<resCount; i++){
Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL");
}
}else{
Tcl_AppendElement(interp, zErr);
}
sqlite3_free_table(aResult);
if( zErr ) sqlite3_free(zErr);
if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
return TCL_OK;
}
#endif /* SQLITE_OMIT_GET_TABLE */
/*
** Usage: sqlite3_last_insert_rowid DB
**
** Returns the integer ROWID of the most recent insert.
*/
static int SQLITE_TCLAPI test_last_rowid(
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;
char zBuf[30];
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;
sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", sqlite3_last_insert_rowid(db));
Tcl_AppendResult(interp, zBuf, 0);
return SQLITE_OK;
}
/*
** Usage: sqlite3_key DB KEY
**
** Set the codec key.
*/
static int SQLITE_TCLAPI test_key(
void *NotUsed,
Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL)
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);
sqlite3_key(db, zKey, nKey);
#endif
return TCL_OK;
}
/*
** Usage: sqlite3_rekey DB KEY
**
** Change the codec key.
*/
static int SQLITE_TCLAPI 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 */
){
#ifdef SQLITE_HAS_CODEC
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);
sqlite3_rekey(db, zKey, nKey);
#endif
return TCL_OK;
}
/*
** Usage: sqlite3_close DB
**
** Closes the database opened by sqlite3_open.
*/
static int SQLITE_TCLAPI 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 *)t1ErrorName(rc), TCL_STATIC);
return TCL_OK;
}
/*
** Usage: sqlite3_close_v2 DB
**
** Closes the database opened by sqlite3_open.
*/
static int SQLITE_TCLAPI sqlite_test_close_v2(
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_v2(db);
Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
return TCL_OK;
}
/*
** Implementation of the x_coalesce() function.
** Return the first argument non-NULL argument.
*/
static void t1_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]) ){
int n = sqlite3_value_bytes(argv[i]);
sqlite3_result_text(context, (char*)sqlite3_value_text(argv[i]),
n, 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++){
sqlite3_snprintf(sizeof(zBuf)-i*2, &zBuf[i*2], "%02x", z[i]);
}
zBuf[i*2] = 0;
sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
}
#ifndef SQLITE_OMIT_UTF16
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++){
sqlite3_snprintf(sizeof(zBuf)-i*4, &zBuf[i*4],"%04x", z[i]&0xff);
}
zBuf[i*4] = 0;
sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
}
#endif
/*
** 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 = (int)strlen(z);
if( p->nUsed + n + 2 > p->nAlloc ){
char *zNew;
p->nAlloc = p->nAlloc*2 + n + 200;
zNew = sqlite3_realloc(p->z, p->nAlloc);
if( zNew==0 ){
sqlite3_free(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);
sqlite3_free(x.z);
}
/*
** Implementation of tkt2213func(), a scalar function that takes exactly
** one argument. It has two interesting features:
**
** * It calls sqlite3_value_text() 3 times on the argument sqlite3_value*.
** If the three pointers returned are not the same an SQL error is raised.
**
** * Otherwise it returns a copy of the text representation of its
** argument in such a way as the VDBE representation is a Mem* cell
** with the MEM_Term flag clear.
**
** Ticket #2213 can therefore be tested by evaluating the following
** SQL expression:
**
** tkt2213func(tkt2213func('a string'));
*/
static void tkt2213Function(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
int nText;
unsigned char const *zText1;
unsigned char const *zText2;
unsigned char const *zText3;
nText = sqlite3_value_bytes(argv[0]);
zText1 = sqlite3_value_text(argv[0]);
zText2 = sqlite3_value_text(argv[0]);
zText3 = sqlite3_value_text(argv[0]);
if( zText1!=zText2 || zText2!=zText3 ){
sqlite3_result_error(context, "tkt2213 is not fixed", -1);
}else{
char *zCopy = (char *)sqlite3_malloc(nText);
memcpy(zCopy, zText1, nText);
sqlite3_result_text(context, zCopy, nText, sqlite3_free);
}
}
/*
** The following SQL function takes 4 arguments. The 2nd and
** 4th argument must be one of these strings: 'text', 'text16',
** or 'blob' corresponding to API functions
**
** sqlite3_value_text()
** sqlite3_value_text16()
** sqlite3_value_blob()
**
** The third argument is a string, either 'bytes' or 'bytes16' or 'noop',
** corresponding to APIs:
**
** sqlite3_value_bytes()
** sqlite3_value_bytes16()
** noop
**
** The APIs designated by the 2nd through 4th arguments are applied
** to the first argument in order. If the pointers returned by the
** second and fourth are different, this routine returns 1. Otherwise,
** this routine returns 0.
**
** This function is used to test to see when returned pointers from
** the _text(), _text16() and _blob() APIs become invalidated.
*/
static void ptrChngFunction(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
const void *p1, *p2;
const char *zCmd;
if( argc!=4 ) return;
zCmd = (const char*)sqlite3_value_text(argv[1]);
if( zCmd==0 ) return;
if( strcmp(zCmd,"text")==0 ){
p1 = (const void*)sqlite3_value_text(argv[0]);
#ifndef SQLITE_OMIT_UTF16
}else if( strcmp(zCmd, "text16")==0 ){
p1 = (const void*)sqlite3_value_text16(argv[0]);
#endif
}else if( strcmp(zCmd, "blob")==0 ){
p1 = (const void*)sqlite3_value_blob(argv[0]);
}else{
return;
}
zCmd = (const char*)sqlite3_value_text(argv[2]);
if( zCmd==0 ) return;
if( strcmp(zCmd,"bytes")==0 ){
sqlite3_value_bytes(argv[0]);
#ifndef SQLITE_OMIT_UTF16
}else if( strcmp(zCmd, "bytes16")==0 ){
sqlite3_value_bytes16(argv[0]);
#endif
}else if( strcmp(zCmd, "noop")==0 ){
/* do nothing */
}else{
return;
}
zCmd = (const char*)sqlite3_value_text(argv[3]);
if( zCmd==0 ) return;
if( strcmp(zCmd,"text")==0 ){
p2 = (const void*)sqlite3_value_text(argv[0]);
#ifndef SQLITE_OMIT_UTF16
}else if( strcmp(zCmd, "text16")==0 ){
p2 = (const void*)sqlite3_value_text16(argv[0]);
#endif
}else if( strcmp(zCmd, "blob")==0 ){
p2 = (const void*)sqlite3_value_blob(argv[0]);
}else{
return;
}
sqlite3_result_int(context, p1!=p2);
}
/*
** This SQL function returns a different answer each time it is called, even if
** the arguments are the same.
*/
static void nondeterministicFunction(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
static int cnt = 0;
sqlite3_result_int(context, cnt++);
}
/*
** This SQL function returns the integer value of its argument as a MEM_IntReal
** value.
*/
static void intrealFunction(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
sqlite3_int64 v = sqlite3_value_int64(argv[0]);
sqlite3_result_int64(context, v);
sqlite3_test_control(SQLITE_TESTCTRL_RESULT_INTREAL, context);
}
/*
** Usage: sqlite3_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 SQLITE_TCLAPI 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_UTF8, 0,
t1_ifnullFunc, 0, 0);
if( rc==SQLITE_OK ){
rc = sqlite3_create_function(db, "hex8", 1, SQLITE_UTF8 | SQLITE_DETERMINISTIC,
0, hex8Func, 0, 0);
}
#ifndef SQLITE_OMIT_UTF16
if( rc==SQLITE_OK ){
rc = sqlite3_create_function(db, "hex16", 1, SQLITE_UTF16 | SQLITE_DETERMINISTIC,
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);
}
/* Functions counter1() and counter2() have the same implementation - they
** both return an ascending integer with each call. But counter1() is marked
** as non-deterministic and counter2() is marked as deterministic.
*/
if( rc==SQLITE_OK ){
rc = sqlite3_create_function(db, "counter1", -1, SQLITE_UTF8,
0, nondeterministicFunction, 0, 0);
}
if( rc==SQLITE_OK ){
rc = sqlite3_create_function(db, "counter2", -1, SQLITE_UTF8|SQLITE_DETERMINISTIC,
0, nondeterministicFunction, 0, 0);
}
/* The intreal() function converts its argument to an integer and returns
** it as a MEM_IntReal.
*/
if( rc==SQLITE_OK ){
rc = sqlite3_create_function(db, "intreal", 1, SQLITE_UTF8,
0, intrealFunction, 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;
}
/*
** Usage: sqlite3_drop_modules DB ?NAME ...?
**
** Invoke the sqlite3_drop_modules(D,L) interface on database
** connection DB, in order to drop all modules except those named in
** the argument.
*/
static int SQLITE_TCLAPI test_drop_modules(
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;
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;
#ifndef SQLITE_OMIT_VIRTUALTABLE
sqlite3_drop_modules(db, argc>2 ? (const char**)(argv+2) : 0);
#endif
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);
}
}
}
#ifndef SQLITE_OMIT_DEPRECATED
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));
}
#endif
/*
** 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 SQLITE_TCLAPI 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);
}
#ifndef SQLITE_OMIT_DEPRECATED
if( rc==SQLITE_OK ){
rc = sqlite3_create_function(db, "legacy_count", 0, SQLITE_ANY, 0, 0,
legacyCountStep, legacyCountFinalize
);
}
#endif
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 SQLITE_TCLAPI 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 SQLITE_TCLAPI 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;
}
/*
** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER
**
** Call mprintf with three 64-bit integer arguments
*/
static int SQLITE_TCLAPI 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( sqlite3Atoi64(argv[i], &a[i-2], sqlite3Strlen30(argv[i]), SQLITE_UTF8) ){
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_long FORMAT INTEGER INTEGER INTEGER
**
** Call mprintf with three long integer arguments. This might be the
** same as sqlite3_mprintf_int or sqlite3_mprintf_int64, depending on
** platform.
*/
static int SQLITE_TCLAPI sqlite3_mprintf_long(
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;
long int a[3];
int b[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( Tcl_GetInt(interp, argv[i], &b[i-2]) ) return TCL_ERROR;
a[i-2] = (long int)b[i-2];
a[i-2] &= (((u64)1)<<(sizeof(int)*8))-1;
}
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 SQLITE_TCLAPI 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 SQLITE_TCLAPI 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 SQLITE_TCLAPI 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 SQLITE_TCLAPI 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 SQLITE_TCLAPI 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 SQLITE_TCLAPI 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;
sqlite_uint64 d;
if( argc!=3 ){
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" FORMAT STRING\"", 0);
return TCL_ERROR;
}
if( sscanf(argv[2], "%08x%08x", &x2, &x1)!=2 ){
Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", 0);
return TCL_ERROR;
}
d = x2;
d = (d<<32) + x1;
memcpy(&r, &d, sizeof(r));
z = sqlite3_mprintf(argv[1], r);
Tcl_AppendResult(interp, z, 0);
sqlite3_free(z);
return TCL_OK;
}
/*
** Usage: sqlite3_enable_shared_cache ?BOOLEAN?
**
*/
#if !defined(SQLITE_OMIT_SHARED_CACHE)
static int SQLITE_TCLAPI test_enable_shared(
ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
int objc, /* Number of arguments */
Tcl_Obj *CONST objv[] /* Command arguments */
){
int rc;
int enable;
int ret = 0;
if( objc!=2 && objc!=1 ){
Tcl_WrongNumArgs(interp, 1, objv, "?BOOLEAN?");
return TCL_ERROR;
}
ret = sqlite3GlobalConfig.sharedCacheEnabled;
if( objc==2 ){
if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ){
return TCL_ERROR;
}
rc = sqlite3_enable_shared_cache(enable);
if( rc!=SQLITE_OK ){
Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC);
return TCL_ERROR;
}
}
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(ret));
return TCL_OK;
}
#endif
/*
** Usage: sqlite3_extended_result_codes DB BOOLEAN
**
*/
static int SQLITE_TCLAPI test_extended_result_codes(
ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
int objc, /* Number of arguments */
Tcl_Obj *CONST objv[] /* Command arguments */
){
int enable;
sqlite3 *db;
if( objc!=3 ){
Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN");
return TCL_ERROR;
}
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
if( Tcl_GetBooleanFromObj(interp, objv[2], &enable) ) return TCL_ERROR;
sqlite3_extended_result_codes(db, enable);
return TCL_OK;
}
/*
** Usage: sqlite3_libversion_number
**
*/
static int SQLITE_TCLAPI test_libversion_number(
ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
int objc, /* Number of arguments */
Tcl_Obj *CONST objv[] /* Command arguments */
){
Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_libversion_number()));
return TCL_OK;
}
/*
** Usage: sqlite3_table_column_metadata DB dbname tblname colname
**
*/
static int SQLITE_TCLAPI test_table_column_metadata(
ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
int objc, /* Number of arguments */
Tcl_Obj *CONST objv[] /* Command arguments */
){
sqlite3 *db;
const char *zDb;
const char *zTbl;
const char *zCol;
int rc;
Tcl_Obj *pRet;
const char *zDatatype;
const char *zCollseq;
int notnull;
int primarykey;
int autoincrement;
if( objc!=5 && objc!=4 ){
Tcl_WrongNumArgs(interp, 1, objv, "DB dbname tblname colname");
return TCL_ERROR;
}
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
zDb = Tcl_GetString(objv[2]);
zTbl = Tcl_GetString(objv[3]);
zCol = objc==5 ? Tcl_GetString(objv[4]) : 0;
if( strlen(zDb)==0 ) zDb = 0;
rc = sqlite3_table_column_metadata(db, zDb, zTbl, zCol,
&zDatatype, &zCollseq, &notnull, &primarykey, &autoincrement);
if( rc!=SQLITE_OK ){
Tcl_AppendResult(interp, sqlite3_errmsg(db), 0);
return TCL_ERROR;
}
pRet = Tcl_NewObj();
Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zDatatype, -1));
Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zCollseq, -1));
Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(notnull));
Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(primarykey));
Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(autoincrement));
Tcl_SetObjResult(interp, pRet);
return TCL_OK;
}
#ifndef SQLITE_OMIT_INCRBLOB
static int SQLITE_TCLAPI blobHandleFromObj(
Tcl_Interp *interp,
Tcl_Obj *pObj,
sqlite3_blob **ppBlob
){
char *z;
int n;
z = Tcl_GetStringFromObj(pObj, &n);
if( n==0 ){
*ppBlob = 0;
}else{
int notUsed;
Tcl_Channel channel;
ClientData instanceData;
channel = Tcl_GetChannel(interp, z, &notUsed);
if( !channel ) return TCL_ERROR;
Tcl_Flush(channel);
Tcl_Seek(channel, 0, SEEK_SET);
instanceData = Tcl_GetChannelInstanceData(channel);
*ppBlob = *((sqlite3_blob **)instanceData);
}
return TCL_OK;
}
static int SQLITE_TCLAPI test_blob_reopen(
ClientData clientData, /* Not used */
Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
int objc, /* Number of arguments */
Tcl_Obj *CONST objv[] /* Command arguments */
){
Tcl_WideInt iRowid;
sqlite3_blob *pBlob;
int rc;
if( objc!=3 ){
Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL ROWID");
return TCL_ERROR;
}
if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR;
if( Tcl_GetWideIntFromObj(interp, objv[2], &iRowid) ) return TCL_ERROR;
rc = sqlite3_blob_reopen(pBlob, iRowid);
if( rc!=SQLITE_OK ){
Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
}
return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
}
#endif
/*
** Usage: sqlite3_create_collation_v2 DB-HANDLE NAME CMP-PROC DEL-PROC
**
** This Tcl proc is used for testing the experimental
** sqlite3_create_collation_v2() interface.
*/
struct TestCollationX {
Tcl_Interp *interp;
Tcl_Obj *pCmp;
Tcl_Obj *pDel;
};
typedef struct TestCollationX TestCollationX;
static void testCreateCollationDel(void *pCtx){
TestCollationX *p = (TestCollationX *)pCtx;
int rc = Tcl_EvalObjEx(p->interp, p->pDel, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL);
if( rc!=TCL_OK ){
Tcl_BackgroundError(p->interp);
}
Tcl_DecrRefCount(p->pCmp);
Tcl_DecrRefCount(p->pDel);
sqlite3_free((void *)p);
}
static int testCreateCollationCmp(
void *pCtx,
int nLeft,
const void *zLeft,
int nRight,
const void *zRight
){
TestCollationX *p = (TestCollationX *)pCtx;
Tcl_Obj *pScript = Tcl_DuplicateObj(p->pCmp);
int iRes = 0;
Tcl_IncrRefCount(pScript);
Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zLeft, nLeft));
Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zRight,nRight));
if( TCL_OK!=Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL)
|| TCL_OK!=Tcl_GetIntFromObj(p->interp, Tcl_GetObjResult(p->interp), &iRes)
){
Tcl_BackgroundError(p->interp);
}
Tcl_DecrRefCount(pScript);
return iRes;
}
static int SQLITE_TCLAPI test_create_collation_v2(
ClientData clientData, /* Not used */
Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
int objc, /* Number of arguments */
Tcl_Obj *CONST objv[] /* Command arguments */
){
TestCollationX *p;
sqlite3 *db;
int rc;
if( objc!=5 ){
Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE NAME CMP-PROC DEL-PROC");
return TCL_ERROR;
}
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
p = (TestCollationX *)sqlite3_malloc(sizeof(TestCollationX));
p->pCmp = objv[3];
p->pDel = objv[4];
p->interp = interp;
Tcl_IncrRefCount(p->pCmp);
Tcl_IncrRefCount(p->pDel);
rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), 16,
(void *)p, testCreateCollationCmp, testCreateCollationDel
);
if( rc!=SQLITE_MISUSE ){
Tcl_AppendResult(interp, "sqlite3_create_collate_v2() failed to detect "
"an invalid encoding", (char*)0);
return TCL_ERROR;
}
rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), SQLITE_UTF8,
(void *)p, testCreateCollationCmp, testCreateCollationDel
);
return TCL_OK;
}
/*
** USAGE: sqlite3_create_function_v2 DB NAME NARG ENC ?SWITCHES?
**
** Available switches are:
**
** -func SCRIPT
** -step SCRIPT
** -final SCRIPT
** -destroy SCRIPT
*/
typedef struct CreateFunctionV2 CreateFunctionV2;
struct CreateFunctionV2 {
Tcl_Interp *interp;
Tcl_Obj *pFunc; /* Script for function invocation */
Tcl_Obj *pStep; /* Script for agg. step invocation */
Tcl_Obj *pFinal; /* Script for agg. finalization invocation */
Tcl_Obj *pDestroy; /* Destructor script */
};
static void cf2Func(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
}
static void cf2Step(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
}
static void cf2Final(sqlite3_context *ctx){
}
static void cf2Destroy(void *pUser){
CreateFunctionV2 *p = (CreateFunctionV2 *)pUser;
if( p->interp && p->pDestroy ){
int rc = Tcl_EvalObjEx(p->interp, p->pDestroy, 0);
if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp);
}
if( p->pFunc ) Tcl_DecrRefCount(p->pFunc);
if( p->pStep ) Tcl_DecrRefCount(p->pStep);
if( p->pFinal ) Tcl_DecrRefCount(p->pFinal);
if( p->pDestroy ) Tcl_DecrRefCount(p->pDestroy);
sqlite3_free(p);
}
static int SQLITE_TCLAPI test_create_function_v2(
ClientData clientData, /* Not used */
Tcl_Interp *interp, /* The invoking TCL interpreter */
int objc, /* Number of arguments */
Tcl_Obj *CONST objv[] /* Command arguments */
){
sqlite3 *db;
const char *zFunc;
int nArg;
int enc;
CreateFunctionV2 *p;
int i;
int rc;
struct EncTable {
const char *zEnc;
int enc;
} aEnc[] = {
{"utf8", SQLITE_UTF8 },
{"utf16", SQLITE_UTF16 },
{"utf16le", SQLITE_UTF16LE },
{"utf16be", SQLITE_UTF16BE },
{"any", SQLITE_ANY },
{"0", 0 }
};
if( objc<5 || (objc%2)==0 ){
Tcl_WrongNumArgs(interp, 1, objv, "DB NAME NARG ENC SWITCHES...");
return TCL_ERROR;
}
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
zFunc = Tcl_GetString(objv[2]);
if( Tcl_GetIntFromObj(interp, objv[3], &nArg) ) return TCL_ERROR;
if( Tcl_GetIndexFromObjStruct(interp, objv[4], aEnc, sizeof(aEnc[0]),
"encoding", 0, &enc)
){
return TCL_ERROR;
}
enc = aEnc[enc].enc;
p = sqlite3_malloc(sizeof(CreateFunctionV2));
assert( p );
memset(p, 0, sizeof(CreateFunctionV2));
p->interp = interp;
for(i=5; i<objc; i+=2){
int iSwitch;
const char *azSwitch[] = {"-func", "-step", "-final", "-destroy", 0};
if( Tcl_GetIndexFromObj(interp, objv[i], azSwitch, "switch", 0, &iSwitch) ){
sqlite3_free(p);
return TCL_ERROR;
}
switch( iSwitch ){
case 0: p->pFunc = objv[i+1]; break;
case 1: p->pStep = objv[i+1]; break;
case 2: p->pFinal = objv[i+1]; break;
case 3: p->pDestroy = objv[i+1]; break;
}
}
if( p->pFunc ) p->pFunc = Tcl_DuplicateObj(p->pFunc);
if( p->pStep ) p->pStep = Tcl_DuplicateObj(p->pStep);
if( p->pFinal ) p->pFinal = Tcl_DuplicateObj(p->pFinal);
if( p->pDestroy ) p->pDestroy = Tcl_DuplicateObj(p->pDestroy);
if( p->pFunc ) Tcl_IncrRefCount(p->pFunc);
if( p->pStep ) Tcl_IncrRefCount(p->pStep);
if( p->pFinal ) Tcl_IncrRefCount(p->pFinal);
if( p->pDestroy ) Tcl_IncrRefCount(p->pDestroy);
rc = sqlite3_create_function_v2(db, zFunc, nArg, enc, (void *)p,
(p->pFunc ? cf2Func : 0),
(p->pStep ? cf2Step : 0),
(p->pFinal ? cf2Final : 0),
cf2Destroy
);
if( rc!=SQLITE_OK ){
Tcl_ResetResult(interp);
Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
return TCL_OK;
}
/*
** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC?
*/
static int SQLITE_TCLAPI test_load_extension(
ClientData clientData, /* Not used */
Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
int objc, /* Number of arguments */
Tcl_Obj *CONST objv[] /* Command arguments */
){
Tcl_CmdInfo cmdInfo;
sqlite3 *db;
int rc;
char *zDb;
char *zFile;
char *zProc = 0;
char *zErr = 0;
if( objc!=4 && objc!=3 ){
Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE FILE ?PROC?");
return TCL_ERROR;
}
zDb = Tcl_GetString(objv[1]);
zFile = Tcl_GetString(objv[2]);
if( objc==4 ){
zProc = Tcl_GetString(objv[3]);
}
/* Extract the C database handle from the Tcl command name */
if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
return TCL_ERROR;
}
db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
assert(db);
/* Call the underlying C function. If an error occurs, set rc to
** TCL_ERROR and load any error string into the interpreter. If no
** error occurs, set rc to TCL_OK.
*/
#ifdef SQLITE_OMIT_LOAD_EXTENSION
rc = SQLITE_ERROR;
zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()");
(void)zProc;
(void)zFile;
#else
rc = sqlite3_load_extension(db, zFile, zProc, &zErr);
#endif
if( rc!=SQLITE_OK ){
Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE);
rc = TCL_ERROR;
}else{
rc = TCL_OK;
}
sqlite3_free(zErr);
return rc;
}
/*
** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF
*/
static int SQLITE_TCLAPI test_enable_load(
ClientData clientData, /* Not used */
Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
int objc, /* Number of arguments */
Tcl_Obj *CONST objv[] /* Command arguments */
){
Tcl_CmdInfo cmdInfo;
sqlite3 *db;
char *zDb;
int onoff;
if( objc!=3 ){
Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF");
return TCL_ERROR;
}
zDb = Tcl_GetString(objv[1]);
/* Extract the C database handle from the Tcl command name */
if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
return TCL_ERROR;
}
db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
assert(db);
/* Get the onoff parameter */
if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){
return TCL_ERROR;
}
#ifdef SQLITE_OMIT_LOAD_EXTENSION
Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()");
return TCL_ERROR;
#else
sqlite3_enable_load_extension(db, onoff);
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_TCLAPI 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 */
){
#if defined(_MSC_VER)
/* We do this, otherwise the test will halt with a popup message
* that we have to click away before the test will continue.
*/
_set_abort_behavior( 0, _CALL_REPORTFAULT );
#endif
exit(255);
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 = (char*)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, (char*)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 SQLITE_TCLAPI 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 SQLITE_TCLAPI test_finalize(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
sqlite3_stmt *pStmt;
int rc;
sqlite3 *db = 0;
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 *)t1ErrorName(rc), TCL_STATIC);
if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
return TCL_OK;
}
/*
** Usage: sqlite3_stmt_status STMT CODE RESETFLAG
**
** Get the value of a status counter from a statement.
*/
static int SQLITE_TCLAPI test_stmt_status(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
int iValue;
int i, op = 0, resetFlag;
const char *zOpName;
sqlite3_stmt *pStmt;
static const struct {
const char *zName;
int op;
} aOp[] = {
{ "SQLITE_STMTSTATUS_FULLSCAN_STEP", SQLITE_STMTSTATUS_FULLSCAN_STEP },
{ "SQLITE_STMTSTATUS_SORT", SQLITE_STMTSTATUS_SORT },
{ "SQLITE_STMTSTATUS_AUTOINDEX", SQLITE_STMTSTATUS_AUTOINDEX },
{ "SQLITE_STMTSTATUS_VM_STEP", SQLITE_STMTSTATUS_VM_STEP },
{ "SQLITE_STMTSTATUS_REPREPARE", SQLITE_STMTSTATUS_REPREPARE },
{ "SQLITE_STMTSTATUS_RUN", SQLITE_STMTSTATUS_RUN },
{ "SQLITE_STMTSTATUS_MEMUSED", SQLITE_STMTSTATUS_MEMUSED },
};
if( objc!=4 ){
Tcl_WrongNumArgs(interp, 1, objv, "STMT PARAMETER RESETFLAG");
return TCL_ERROR;
}
if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
zOpName = Tcl_GetString(objv[2]);
for(i=0; i<ArraySize(aOp); i++){
if( strcmp(aOp[i].zName, zOpName)==0 ){
op = aOp[i].op;
break;
}
}
if( i>=ArraySize(aOp) ){
if( Tcl_GetIntFromObj(interp, objv[2], &op) ) return TCL_ERROR;
}
if( Tcl_GetBooleanFromObj(interp, objv[3], &resetFlag) ) return TCL_ERROR;
iValue = sqlite3_stmt_status(pStmt, op, resetFlag);
Tcl_SetObjResult(interp, Tcl_NewIntObj(iValue));
return TCL_OK;
}
#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
/*
** Usage: sqlite3_stmt_scanstatus STMT IDX
*/
static int SQLITE_TCLAPI test_stmt_scanstatus(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
sqlite3_stmt *pStmt; /* First argument */
int idx; /* Second argument */
const char *zName;
const char *zExplain;
sqlite3_int64 nLoop;
sqlite3_int64 nVisit;
double rEst;
int res;
if( objc!=3 ){
Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX");
return TCL_ERROR;
}
if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
res = sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop);
if( res==0 ){
Tcl_Obj *pRet = Tcl_NewObj();
Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nLoop", -1));
Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nLoop));
sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nVisit", -1));
Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nVisit));
sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_EST, (void*)&rEst);
Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nEst", -1));
Tcl_ListObjAppendElement(0, pRet, Tcl_NewDoubleObj(rEst));
sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_NAME, (void*)&zName);
Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zName", -1));
Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zName, -1));
sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zExplain", -1));
Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zExplain, -1));
Tcl_SetObjResult(interp, pRet);
}else{
Tcl_ResetResult(interp);
}
return TCL_OK;
}
/*
** Usage: sqlite3_stmt_scanstatus_reset STMT
*/
static int SQLITE_TCLAPI test_stmt_scanstatus_reset(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
sqlite3_stmt *pStmt; /* First argument */
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "STMT");
return TCL_ERROR;
}
if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
sqlite3_stmt_scanstatus_reset(pStmt);
return TCL_OK;
}
#endif
#ifdef SQLITE_ENABLE_SQLLOG
/*
** Usage: sqlite3_config_sqllog
**
** Zero the SQLITE_CONFIG_SQLLOG configuration
*/
static int SQLITE_TCLAPI test_config_sqllog(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
if( objc!=1 ){
Tcl_WrongNumArgs(interp, 1, objv, "");
return TCL_ERROR;
}
sqlite3_config(SQLITE_CONFIG_SQLLOG, 0, 0);
return TCL_OK;
}
#endif
/*
** Usage: sqlite3_config_sorterref
**
** Set the SQLITE_CONFIG_SORTERREF_SIZE configuration option
*/
static int SQLITE_TCLAPI test_config_sorterref(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
int iVal;
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "NBYTE");
return TCL_ERROR;
}
if( Tcl_GetIntFromObj(interp, objv[1], &iVal) ) return TCL_ERROR;
sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, iVal);
return TCL_OK;
}
/*
** Usage: vfs_current_time_int64
**
** Return the value returned by the default VFS's xCurrentTimeInt64 method.
*/
static int SQLITE_TCLAPI vfsCurrentTimeInt64(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
i64 t;
sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
if( objc!=1 ){
Tcl_WrongNumArgs(interp, 1, objv, "");
return TCL_ERROR;
}
pVfs->xCurrentTimeInt64(pVfs, &t);
Tcl_SetObjResult(interp, Tcl_NewWideIntObj(t));
return TCL_OK;
}
#ifdef SQLITE_ENABLE_SNAPSHOT
/*
** Usage: sqlite3_snapshot_get DB DBNAME
*/
static int SQLITE_TCLAPI test_snapshot_get(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
int rc;
sqlite3 *db;
char *zName;
sqlite3_snapshot *pSnapshot = 0;
if( objc!=3 ){
Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
return TCL_ERROR;
}
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
zName = Tcl_GetString(objv[2]);
rc = sqlite3_snapshot_get(db, zName, &pSnapshot);
if( rc!=SQLITE_OK ){
Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
return TCL_ERROR;
}else{
char zBuf[100];
if( sqlite3TestMakePointerStr(interp, zBuf, pSnapshot) ) return TCL_ERROR;
Tcl_SetObjResult(interp, Tcl_NewStringObj(zBuf, -1));
}
return TCL_OK;
}
#endif /* SQLITE_ENABLE_SNAPSHOT */
#ifdef SQLITE_ENABLE_SNAPSHOT
/*
** Usage: sqlite3_snapshot_recover DB DBNAME
*/
static int SQLITE_TCLAPI test_snapshot_recover(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
int rc;
sqlite3 *db;
char *zName;
if( objc!=3 ){
Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
return TCL_ERROR;
}
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
zName = Tcl_GetString(objv[2]);
rc = sqlite3_snapshot_recover(db, zName);
if( rc!=SQLITE_OK ){
Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
return TCL_ERROR;
}else{
Tcl_ResetResult(interp);
}
return TCL_OK;
}
#endif /* SQLITE_ENABLE_SNAPSHOT */
#ifdef SQLITE_ENABLE_SNAPSHOT
/*
** Usage: sqlite3_snapshot_open DB DBNAME SNAPSHOT
*/
static int SQLITE_TCLAPI test_snapshot_open(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
int rc;
sqlite3 *db;
char *zName;
sqlite3_snapshot *pSnapshot;
if( objc!=4 ){
Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SNAPSHOT");
return TCL_ERROR;
}
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
zName = Tcl_GetString(objv[2]);
pSnapshot = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[3]));
rc = sqlite3_snapshot_open(db, zName, pSnapshot);
if( rc!=SQLITE_OK ){
Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
return TCL_ERROR;
}else{
Tcl_ResetResult(interp);
}
return TCL_OK;
}
#endif /* SQLITE_ENABLE_SNAPSHOT */
#ifdef SQLITE_ENABLE_SNAPSHOT
/*
** Usage: sqlite3_snapshot_free SNAPSHOT
*/
static int SQLITE_TCLAPI test_snapshot_free(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
sqlite3_snapshot *pSnapshot;
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT");
return TCL_ERROR;
}
pSnapshot = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
sqlite3_snapshot_free(pSnapshot);
return TCL_OK;
}
#endif /* SQLITE_ENABLE_SNAPSHOT */
#ifdef SQLITE_ENABLE_SNAPSHOT
/*
** Usage: sqlite3_snapshot_cmp SNAPSHOT1 SNAPSHOT2
*/
static int SQLITE_TCLAPI test_snapshot_cmp(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
int res;
sqlite3_snapshot *p1;
sqlite3_snapshot *p2;
if( objc!=3 ){
Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT1 SNAPSHOT2");
return TCL_ERROR;
}
p1 = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
p2 = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[2]));
res = sqlite3_snapshot_cmp(p1, p2);
Tcl_SetObjResult(interp, Tcl_NewIntObj(res));
return TCL_OK;
}
#endif /* SQLITE_ENABLE_SNAPSHOT */
#ifdef SQLITE_ENABLE_SNAPSHOT
/*
** Usage: sqlite3_snapshot_get_blob DB DBNAME
*/
static int SQLITE_TCLAPI test_snapshot_get_blob(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
int rc;
sqlite3 *db;
char *zName;
sqlite3_snapshot *pSnapshot = 0;
if( objc!=3 ){
Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
return TCL_ERROR;
}
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
zName = Tcl_GetString(objv[2]);
rc = sqlite3_snapshot_get(db, zName, &pSnapshot);
if( rc!=SQLITE_OK ){
Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
return TCL_ERROR;
}else{
Tcl_SetObjResult(interp,
Tcl_NewByteArrayObj((unsigned char*)pSnapshot, sizeof(sqlite3_snapshot))
);
sqlite3_snapshot_free(pSnapshot);
}
return TCL_OK;
}
#endif /* SQLITE_ENABLE_SNAPSHOT */
#ifdef SQLITE_ENABLE_SNAPSHOT
/*
** Usage: sqlite3_snapshot_open_blob DB DBNAME SNAPSHOT
*/
static int SQLITE_TCLAPI test_snapshot_open_blob(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
int rc;
sqlite3 *db;
char *zName;
unsigned char *pBlob;
int nBlob;
if( objc!=4 ){
Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SNAPSHOT");
return TCL_ERROR;
}
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
zName = Tcl_GetString(objv[2]);
pBlob = Tcl_GetByteArrayFromObj(objv[3], &nBlob);
if( nBlob!=sizeof(sqlite3_snapshot) ){
Tcl_AppendResult(interp, "bad SNAPSHOT", 0);
return TCL_ERROR;
}
rc = sqlite3_snapshot_open(db, zName, (sqlite3_snapshot*)pBlob);
if( rc!=SQLITE_OK ){
Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
return TCL_ERROR;
}
return TCL_OK;
}
#endif /* SQLITE_ENABLE_SNAPSHOT */
#ifdef SQLITE_ENABLE_SNAPSHOT
/*
** Usage: sqlite3_snapshot_cmp_blob SNAPSHOT1 SNAPSHOT2
*/
static int SQLITE_TCLAPI test_snapshot_cmp_blob(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
int res;
unsigned char *p1;
unsigned char *p2;
int n1;
int n2;
if( objc!=3 ){
Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT1 SNAPSHOT2");
return TCL_ERROR;
}
p1 = Tcl_GetByteArrayFromObj(objv[1], &n1);
p2 = Tcl_GetByteArrayFromObj(objv[2], &n2);
if( n1!=sizeof(sqlite3_snapshot) || n1!=n2 ){
Tcl_AppendResult(interp, "bad SNAPSHOT", 0);
return TCL_ERROR;
}
res = sqlite3_snapshot_cmp((sqlite3_snapshot*)p1, (sqlite3_snapshot*)p2);
Tcl_SetObjResult(interp, Tcl_NewIntObj(res));
return TCL_OK;
}
#endif /* SQLITE_ENABLE_SNAPSHOT */
/*
** Usage: sqlite3_delete_database FILENAME
*/
int sqlite3_delete_database(const char*); /* in test_delete.c */
static int SQLITE_TCLAPI test_delete_database(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
int rc;
const char *zFile;
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "FILE");
return TCL_ERROR;
}
zFile = (const char*)Tcl_GetString(objv[1]);
rc = sqlite3_delete_database(zFile);
Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
return TCL_OK;
}
/*
** Usage: atomic_batch_write PATH
*/
static int SQLITE_TCLAPI test_atomic_batch_write(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
char *zFile = 0; /* Path to file to test */
sqlite3 *db = 0; /* Database handle */
sqlite3_file *pFd = 0; /* SQLite fd open on zFile */
int bRes = 0; /* Integer result of this command */
int dc = 0; /* Device-characteristics mask */
int rc; /* sqlite3_open() return code */
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "PATH");
return TCL_ERROR;
}
zFile = Tcl_GetString(objv[1]);
rc = sqlite3_open(zFile, &db);
if( rc!=SQLITE_OK ){
Tcl_AppendResult(interp, sqlite3_errmsg(db), 0);
sqlite3_close(db);
return TCL_ERROR;
}