#pragma ident "%Z%%M% %I% %E% SMI"
/*
** 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 the printf() interface to SQLite. This code
** is not included in the SQLite library. It is used for automated
** testing of the SQLite library.
**
** $Id: test1.c,v 1.36.2.1 2004/05/07 00:57:06 drh Exp $
*/
#include "sqliteInt.h"
#include "tcl.h"
#include "os.h"
#include <stdlib.h>
#include <string.h>
#if OS_WIN
#else
#endif
/*
** Decode a pointer to an sqlite object.
*/
){
return TCL_ERROR;
}
return TCL_OK;
}
/*
** Decode a pointer to an sqlite_vm object.
*/
return TCL_ERROR;
}
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.
*/
void *p2;
"report this problem to the SQLite mailing list or as a new but "
"report. Please provide detailed information about how you compiled "
"SQLite and what computer you are running on.", 0);
return TCL_ERROR;
}
}
return TCL_OK;
}
/*
** Usage: sqlite_open filename
**
** Returns: The name of an open database.
*/
static int sqlite_test_open(
void *NotUsed,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
char *zErr = 0;
if( argc!=2 ){
" FILENAME\"", 0);
return TCL_ERROR;
}
if( db==0 ){
return TCL_ERROR;
}
return TCL_OK;
}
/*
** The callback routine for sqlite_exec_printf().
*/
int i;
if( Tcl_DStringLength(str)==0 ){
for(i=0; i<argc; i++){
}
}
for(i=0; i<argc; i++){
}
return 0;
}
/*
** Usage: sqlite_exec_printf DB FORMAT STRING
**
** Invoke the sqlite_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 test_exec_printf(
void *NotUsed,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
int rc;
char *zErr = 0;
if( argc!=4 ){
" DB FORMAT STRING", 0);
return TCL_ERROR;
}
return TCL_OK;
}
/*
** Usage: sqlite_mprintf_z_test SEPARATOR ARG0 ARG1 ...
**
** Test the %z format of mprintf(). Use multiple mprintf() calls to
** concatenate arg0 through argn using separator as the separator.
** Return the result.
*/
static int test_mprintf_z(
void *NotUsed,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
char *zResult = 0;
int i;
for(i=2; i<argc; i++){
}
return TCL_OK;
}
/*
** Usage: sqlite_get_table_printf DB FORMAT STRING
**
** Invoke the sqlite_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 test_get_table_printf(
void *NotUsed,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
int rc;
char *zErr = 0;
char **aResult;
int i;
if( argc!=4 ){
" DB FORMAT STRING", 0);
return TCL_ERROR;
}
}
}else{
}
return TCL_OK;
}
/*
** Usage: sqlite_last_insert_rowid DB
**
** Returns the integer ROWID of the most recent insert.
*/
static int test_last_rowid(
void *NotUsed,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
if( argc!=2 ){
return TCL_ERROR;
}
return SQLITE_OK;
}
/*
** Usage: sqlite_close DB
**
** Closes the database opened by sqlite_open.
*/
static int sqlite_test_close(
void *NotUsed,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
if( argc!=2 ){
" FILENAME\"", 0);
return TCL_ERROR;
}
return TCL_OK;
}
/*
** Implementation of the x_coalesce() function.
** Return the first argument non-NULL argument.
*/
int i;
for(i=0; i<argc; i++){
if( argv[i] ){
break;
}
}
}
/*
** A structure into which to accumulate text.
*/
struct dstr {
char *z; /* The space */
};
/*
** Append text to a dstr
*/
int n = strlen(z);
char *zNew;
if( zNew==0 ){
sqliteFree(p->z);
memset(p, 0, sizeof(*p));
return;
}
p->z = zNew;
}
}
p->nUsed += n;
}
/*
** Invoked for each callback from sqliteExecFunc
*/
int i;
for(i=0; i<argc; i++){
if( argv[i]==0 ){
}else{
}
}
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 sqlite_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.
*/
struct dstr x;
memset(&x, 0, sizeof(x));
execFuncCallback, &x, 0);
sqliteFree(x.z);
}
/*
** Usage: sqlite_test_create_function DB
**
** Call the sqlite_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 sqlite_exec(). Invoking sqlite_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
** sqlite_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,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
extern void Md5_Register(sqlite*);
if( argc!=2 ){
" FILENAME\"", 0);
return TCL_ERROR;
}
return TCL_OK;
}
/*
** Routines to implement the x_count() aggregate function.
*/
struct CountCtx {
int n;
};
CountCtx *p;
p = sqlite_aggregate_context(context, sizeof(*p));
p->n++;
}
}
CountCtx *p;
p = sqlite_aggregate_context(context, sizeof(*p));
sqlite_set_result_int(context, p ? p->n : 0);
}
/*
** Usage: sqlite_test_create_aggregate DB
**
** Call the sqlite_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
** sqlite_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,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
if( argc!=2 ){
" FILENAME\"", 0);
return TCL_ERROR;
}
return TCL_OK;
}
/*
** Usage: sqlite_mprintf_int FORMAT INTEGER INTEGER INTEGER
**
** Call mprintf with three integer arguments
*/
static int sqlite_mprintf_int(
void *NotUsed,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
int a[3], i;
char *z;
if( argc!=5 ){
" FORMAT INT INT INT\"", 0);
return TCL_ERROR;
}
for(i=2; i<5; i++){
}
Tcl_AppendResult(interp, z, 0);
sqlite_freemem(z);
return TCL_OK;
}
/*
** Usage: sqlite_mprintf_str FORMAT INTEGER INTEGER STRING
**
** Call mprintf with two integer arguments and one string argument
*/
static int sqlite_mprintf_str(
void *NotUsed,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
int a[3], i;
char *z;
" FORMAT INT INT ?STRING?\"", 0);
return TCL_ERROR;
}
for(i=2; i<4; i++){
}
Tcl_AppendResult(interp, z, 0);
sqlite_freemem(z);
return TCL_OK;
}
/*
** Usage: sqlite_mprintf_str FORMAT INTEGER INTEGER DOUBLE
**
** Call mprintf with two integer arguments and one double argument
*/
static int sqlite_mprintf_double(
void *NotUsed,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
int a[3], i;
double r;
char *z;
if( argc!=5 ){
" FORMAT INT INT STRING\"", 0);
return TCL_ERROR;
}
for(i=2; i<4; i++){
}
Tcl_AppendResult(interp, z, 0);
sqlite_freemem(z);
return TCL_OK;
}
/*
** Usage: sqlite_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 sqlite_mprintf_scaled(
void *NotUsed,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
int i;
double r[2];
char *z;
if( argc!=4 ){
" FORMAT DOUBLE DOUBLE\"", 0);
return TCL_ERROR;
}
for(i=2; i<4; i++){
}
Tcl_AppendResult(interp, z, 0);
sqlite_freemem(z);
return TCL_OK;
}
/*
** Usage: sqlite_malloc_fail N
**
** Rig sqliteMalloc() to fail on the N-th call. Turn off this mechanism
** and reset the sqlite_malloc_failed variable is N==0.
*/
#ifdef MEMORY_DEBUG
static int sqlite_malloc_fail(
void *NotUsed,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
int n;
if( argc!=2 ){
return TCL_ERROR;
}
sqlite_iMallocFail = n;
sqlite_malloc_failed = 0;
return TCL_OK;
}
#endif
/*
** Usage: sqlite_malloc_stat
**
** Return the number of prior calls to sqliteMalloc() and sqliteFree().
*/
#ifdef MEMORY_DEBUG
static int sqlite_malloc_stat(
void *NotUsed,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
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,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
return TCL_OK;
}
/*
** The following routine is a user-defined SQL function whose purpose
** is to test the sqlite_set_result() API.
*/
while( argc>=2 ){
if( argv[0]==0 ){
"may not be NULL", -1);
}else if( argv[1]==0 ){
"first argument is not \"string\"", -1);
}else{
"string int double", -1);
}
argc -= 2;
argv += 2;
}
}
/*
** 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,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
int rc;
if( argc!=3 ){
" DB FUNCTION-NAME", 0);
return TCL_ERROR;
}
if( rc!=0 ){
return TCL_ERROR;
}
return TCL_OK;
}
/*
** This SQLite callback records the datatype of all columns.
**
** The pArg argument is really a pointer to a TCL interpreter. The
** column names are inserted as the result of this interpreter.
**
** This routine returns non-zero which causes the query to abort.
*/
int i;
return 1;
}
pList = Tcl_NewObj();
for(i=0; i<nCol; i++){
}
return 1;
}
/*
** Invoke an SQL statement but ignore all the data in the result. Instead,
** return a list that consists of the datatypes of the various columns.
**
** This only works if "PRAGMA show_datatypes=on" has been executed against
** the database connection.
*/
static int sqlite_datatypes(
void *NotUsed,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
int rc;
if( argc!=3 ){
" DB SQL", 0);
return TCL_ERROR;
}
return TCL_ERROR;
}
return TCL_OK;
}
/*
** Usage: sqlite_compile DB SQL ?TAILVAR?
**
** Attempt to compile an SQL statement. Return a pointer to the virtual
** machine used to execute that statement. Unprocessed SQL is written
** into TAILVAR.
*/
static int test_compile(
void *NotUsed,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
int rc;
char *zErr = 0;
const char *zTail;
" DB SQL TAILVAR", 0);
return TCL_ERROR;
}
if( rc ){
return TCL_ERROR;
}
if( vm ){
}
return TCL_OK;
}
/*
** Usage: sqlite_step VM ?NVAR? ?VALUEVAR? ?COLNAMEVAR?
**
** Step a virtual machine. Return a the result code as a string.
** Column results are written into three variables.
*/
static int test_step(
void *NotUsed,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
int rc, i;
const char **azValue = 0;
const char **azColName = 0;
int N = 0;
char *zRc;
" VM NVAR VALUEVAR COLNAMEVAR", 0);
return TCL_ERROR;
}
if( argc>=3 ){
}
if( argc>=4 ){
if( azValue ){
for(i=0; i<N; i++){
}
}
}
if( argc==5 ){
if( azColName ){
for(i=0; i<N*2; i++){
}
}
}
switch( rc ){
default: zRc = "unknown"; break;
}
return TCL_OK;
}
/*
** Usage: sqlite_finalize VM
**
** Shutdown a virtual machine.
*/
static int test_finalize(
void *NotUsed,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
int rc;
char *zErrMsg = 0;
if( argc!=2 ){
" VM\"", 0);
return TCL_ERROR;
}
if( rc ){
return TCL_ERROR;
}
return TCL_OK;
}
/*
** Usage: sqlite_reset VM
**
** Reset a virtual machine and prepare it to be run again.
*/
static int test_reset(
void *NotUsed,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
int rc;
char *zErrMsg = 0;
if( argc!=2 ){
" VM\"", 0);
return TCL_ERROR;
}
if( rc ){
return TCL_ERROR;
}
return TCL_OK;
}
/*
** This is the "static_bind_value" that variables are bound to when
** the FLAG option of sqlite_bind is "static"
*/
static char *sqlite_static_bind_value = 0;
/*
** Usage: sqlite_bind VM IDX VALUE FLAGS
**
** Sets the value of the IDX-th occurance of "?" in the original SQL
** string. VALUE is the new value. If FLAGS=="null" then VALUE is
** ignored and the value is set to NULL. If FLAGS=="static" then
** the value is set to the value of a static variable named
** "sqlite_static_bind_value". If FLAGS=="normal" then a copy
** of the VALUE is made.
*/
static int test_bind(
void *NotUsed,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
int rc;
int idx;
if( argc!=5 ){
" VM IDX VALUE (null|static|normal)\"", 0);
return TCL_ERROR;
}
}else{
"\"null\" or \"static\" or \"normal\"", 0);
return TCL_ERROR;
}
if( rc ){
return TCL_ERROR;
}
return TCL_OK;
}
/*
** Usage: breakpoint
**
** This routine exists for one purpose - to provide a place to put a
** breakpoint with GDB that can be triggered using TCL code. The use
** for this is when a particular test fails on (say) the 1485th iteration.
** In the TCL test script, we can add code like this:
**
** if {$i==1485} breakpoint
**
** Then run testfixture in the debugger and wait for the breakpoint to
** fire. Then additional breakpoints can be set to trace down the bug.
*/
static int test_breakpoint(
void *NotUsed,
int argc, /* Number of arguments */
char **argv /* Text of each argument */
){
return TCL_OK; /* Do nothing */
}
/*
** Register commands with the TCL interpreter.
*/
extern int sqlite_search_count;
extern int sqlite_interrupt_count;
extern int sqlite_open_file_count;
extern int sqlite_current_time;
extern int sqlite_temp_directory;
static struct {
char *zName;
} aCmd[] = {
#ifdef MEMORY_DEBUG
#endif
};
int i;
}
(char*)&sqlite_search_count, TCL_LINK_INT);
(char*)&sqlite_interrupt_count, TCL_LINK_INT);
(char*)&sqlite_open_file_count, TCL_LINK_INT);
(char*)&sqlite_current_time, TCL_LINK_INT);
(char*)&sqlite_static_bind_value, TCL_LINK_STRING);
(char*)&sqlite_temp_directory, TCL_LINK_STRING);
return TCL_OK;
}