drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | 05a8298 | 2006-03-19 13:00:25 +0000 | [diff] [blame] | 12 | ** Code for testing all sorts of SQLite interfaces. This code |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 13 | ** is not included in the SQLite library. It is used for automated |
| 14 | ** testing of the SQLite library. |
| 15 | ** |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame^] | 16 | ** $Id: test1.c,v 1.287 2008/01/23 14:51:50 drh Exp $ |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 17 | */ |
| 18 | #include "sqliteInt.h" |
| 19 | #include "tcl.h" |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 23 | /* |
| 24 | ** This is a copy of the first part of the SqliteDb structure in |
| 25 | ** tclsqlite.c. We need it here so that the get_sqlite_pointer routine |
| 26 | ** can extract the sqlite3* pointer from an existing Tcl SQLite |
| 27 | ** connection. |
| 28 | */ |
| 29 | struct SqliteDb { |
| 30 | sqlite3 *db; |
| 31 | }; |
| 32 | |
| 33 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 34 | ** Convert text generated by the "%p" conversion format back into |
| 35 | ** a pointer. |
| 36 | */ |
| 37 | static int testHexToInt(int h){ |
| 38 | if( h>='0' && h<='9' ){ |
| 39 | return h - '0'; |
| 40 | }else if( h>='a' && h<='f' ){ |
| 41 | return h - 'a' + 10; |
| 42 | }else{ |
| 43 | assert( h>='A' && h<='F' ); |
| 44 | return h - 'A' + 10; |
| 45 | } |
| 46 | } |
| 47 | void *sqlite3TextToPtr(const char *z){ |
| 48 | void *p; |
| 49 | u64 v; |
| 50 | u32 v2; |
| 51 | if( z[0]=='0' && z[1]=='x' ){ |
| 52 | z += 2; |
| 53 | } |
| 54 | v = 0; |
| 55 | while( *z ){ |
| 56 | v = (v<<4) + testHexToInt(*z); |
| 57 | z++; |
| 58 | } |
| 59 | if( sizeof(p)==sizeof(v) ){ |
| 60 | memcpy(&p, &v, sizeof(p)); |
| 61 | }else{ |
| 62 | assert( sizeof(p)==sizeof(v2) ); |
| 63 | v2 = (u32)v; |
| 64 | memcpy(&p, &v2, sizeof(p)); |
| 65 | } |
| 66 | return p; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | /* |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 71 | ** A TCL command that returns the address of the sqlite* pointer |
| 72 | ** for an sqlite connection instance. Bad things happen if the |
| 73 | ** input is not an sqlite connection. |
| 74 | */ |
| 75 | static int get_sqlite_pointer( |
| 76 | void * clientData, |
| 77 | Tcl_Interp *interp, |
| 78 | int objc, |
| 79 | Tcl_Obj *CONST objv[] |
| 80 | ){ |
| 81 | struct SqliteDb *p; |
| 82 | Tcl_CmdInfo cmdInfo; |
| 83 | char zBuf[100]; |
| 84 | if( objc!=2 ){ |
| 85 | Tcl_WrongNumArgs(interp, 1, objv, "SQLITE-CONNECTION"); |
| 86 | return TCL_ERROR; |
| 87 | } |
| 88 | if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ |
| 89 | Tcl_AppendResult(interp, "command not found: ", |
| 90 | Tcl_GetString(objv[1]), (char*)0); |
| 91 | return TCL_ERROR; |
| 92 | } |
| 93 | p = (struct SqliteDb*)cmdInfo.objClientData; |
| 94 | sprintf(zBuf, "%p", p->db); |
| 95 | if( strncmp(zBuf,"0x",2) ){ |
| 96 | sprintf(zBuf, "0x%p", p->db); |
| 97 | } |
| 98 | Tcl_AppendResult(interp, zBuf, 0); |
| 99 | return TCL_OK; |
| 100 | } |
| 101 | |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 102 | /* |
| 103 | ** Decode a pointer to an sqlite3 object. |
| 104 | */ |
| 105 | static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){ |
| 106 | struct SqliteDb *p; |
| 107 | Tcl_CmdInfo cmdInfo; |
| 108 | if( Tcl_GetCommandInfo(interp, zA, &cmdInfo) ){ |
| 109 | p = (struct SqliteDb*)cmdInfo.objClientData; |
| 110 | *ppDb = p->db; |
| 111 | }else{ |
| 112 | *ppDb = (sqlite3*)sqlite3TextToPtr(zA); |
| 113 | } |
| 114 | return TCL_OK; |
| 115 | } |
| 116 | |
| 117 | |
drh | 2e66f0b | 2005-04-28 17:18:48 +0000 | [diff] [blame] | 118 | const char *sqlite3TestErrorName(int rc){ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 119 | const char *zName = 0; |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 120 | switch( rc & 0xff ){ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 121 | case SQLITE_OK: zName = "SQLITE_OK"; break; |
| 122 | case SQLITE_ERROR: zName = "SQLITE_ERROR"; break; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 123 | case SQLITE_PERM: zName = "SQLITE_PERM"; break; |
| 124 | case SQLITE_ABORT: zName = "SQLITE_ABORT"; break; |
| 125 | case SQLITE_BUSY: zName = "SQLITE_BUSY"; break; |
| 126 | case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break; |
| 127 | case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break; |
| 128 | case SQLITE_READONLY: zName = "SQLITE_READONLY"; break; |
| 129 | case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break; |
| 130 | case SQLITE_IOERR: zName = "SQLITE_IOERR"; break; |
| 131 | case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 132 | case SQLITE_FULL: zName = "SQLITE_FULL"; break; |
| 133 | case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break; |
| 134 | case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break; |
| 135 | case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break; |
| 136 | case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 137 | case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break; |
| 138 | case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break; |
| 139 | case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break; |
| 140 | case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break; |
| 141 | case SQLITE_AUTH: zName = "SQLITE_AUTH"; break; |
| 142 | case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break; |
| 143 | case SQLITE_RANGE: zName = "SQLITE_RANGE"; break; |
| 144 | case SQLITE_ROW: zName = "SQLITE_ROW"; break; |
| 145 | case SQLITE_DONE: zName = "SQLITE_DONE"; break; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 146 | case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break; |
danielk1977 | 5ce240a | 2007-09-03 17:30:06 +0000 | [diff] [blame] | 147 | case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 148 | default: zName = "SQLITE_Unknown"; break; |
| 149 | } |
| 150 | return zName; |
| 151 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 152 | #define t1ErrorName sqlite3TestErrorName |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 153 | |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 154 | /* |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 155 | ** Convert an sqlite3_stmt* into an sqlite3*. This depends on the |
| 156 | ** fact that the sqlite3* is the first field in the Vdbe structure. |
| 157 | */ |
drh | 51942bc | 2005-06-12 22:01:42 +0000 | [diff] [blame] | 158 | #define StmtToDb(X) sqlite3_db_handle(X) |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | ** Check a return value to make sure it agrees with the results |
| 162 | ** from sqlite3_errcode. |
| 163 | */ |
| 164 | int sqlite3TestErrCode(Tcl_Interp *interp, sqlite3 *db, int rc){ |
| 165 | if( rc!=SQLITE_MISUSE && rc!=SQLITE_OK && sqlite3_errcode(db)!=rc ){ |
| 166 | char zBuf[200]; |
| 167 | int r2 = sqlite3_errcode(db); |
| 168 | sprintf(zBuf, "error code %s (%d) does not match sqlite3_errcode %s (%d)", |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 169 | t1ErrorName(rc), rc, t1ErrorName(r2), r2); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 170 | Tcl_ResetResult(interp); |
| 171 | Tcl_AppendResult(interp, zBuf, 0); |
| 172 | return 1; |
| 173 | } |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | /* |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 178 | ** Decode a pointer to an sqlite3_stmt object. |
| 179 | */ |
| 180 | static int getStmtPointer( |
| 181 | Tcl_Interp *interp, |
| 182 | const char *zArg, |
| 183 | sqlite3_stmt **ppStmt |
| 184 | ){ |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 185 | *ppStmt = (sqlite3_stmt*)sqlite3TextToPtr(zArg); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 186 | return TCL_OK; |
| 187 | } |
| 188 | |
| 189 | /* |
drh | 7d8085a | 2003-04-26 13:19:38 +0000 | [diff] [blame] | 190 | ** Generate a text representation of a pointer that can be understood |
| 191 | ** by the getDbPointer and getVmPointer routines above. |
| 192 | ** |
| 193 | ** The problem is, on some machines (Solaris) if you do a printf with |
| 194 | ** "%p" you cannot turn around and do a scanf with the same "%p" and |
| 195 | ** get your pointer back. You have to prepend a "0x" before it will |
| 196 | ** work. Or at least that is what is reported to me (drh). But this |
| 197 | ** behavior varies from machine to machine. The solution used her is |
| 198 | ** to test the string right after it is generated to see if it can be |
| 199 | ** understood by scanf, and if not, try prepending an "0x" to see if |
| 200 | ** that helps. If nothing works, a fatal error is generated. |
| 201 | */ |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 202 | int sqlite3TestMakePointerStr(Tcl_Interp *interp, char *zPtr, void *p){ |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 203 | sqlite3_snprintf(100, zPtr, "%p", p); |
drh | 7d8085a | 2003-04-26 13:19:38 +0000 | [diff] [blame] | 204 | return TCL_OK; |
| 205 | } |
| 206 | |
| 207 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 208 | ** The callback routine for sqlite3_exec_printf(). |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 209 | */ |
| 210 | static int exec_printf_cb(void *pArg, int argc, char **argv, char **name){ |
| 211 | Tcl_DString *str = (Tcl_DString*)pArg; |
| 212 | int i; |
| 213 | |
| 214 | if( Tcl_DStringLength(str)==0 ){ |
| 215 | for(i=0; i<argc; i++){ |
| 216 | Tcl_DStringAppendElement(str, name[i] ? name[i] : "NULL"); |
| 217 | } |
| 218 | } |
| 219 | for(i=0; i<argc; i++){ |
| 220 | Tcl_DStringAppendElement(str, argv[i] ? argv[i] : "NULL"); |
| 221 | } |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | /* |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 226 | ** The I/O tracing callback. |
| 227 | */ |
| 228 | static FILE *iotrace_file = 0; |
| 229 | static void io_trace_callback(const char *zFormat, ...){ |
| 230 | va_list ap; |
| 231 | va_start(ap, zFormat); |
| 232 | vfprintf(iotrace_file, zFormat, ap); |
| 233 | va_end(ap); |
| 234 | fflush(iotrace_file); |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | ** Usage: io_trace FILENAME |
| 239 | ** |
| 240 | ** Turn I/O tracing on or off. If FILENAME is not an empty string, |
| 241 | ** I/O tracing begins going into FILENAME. If FILENAME is an empty |
| 242 | ** string, I/O tracing is turned off. |
| 243 | */ |
| 244 | static int test_io_trace( |
| 245 | void *NotUsed, |
| 246 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 247 | int argc, /* Number of arguments */ |
| 248 | char **argv /* Text of each argument */ |
| 249 | ){ |
| 250 | if( argc!=2 ){ |
| 251 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 252 | " FILENAME\"", 0); |
| 253 | return TCL_ERROR; |
| 254 | } |
| 255 | if( iotrace_file ){ |
| 256 | if( iotrace_file!=stdout && iotrace_file!=stderr ){ |
| 257 | fclose(iotrace_file); |
| 258 | } |
| 259 | iotrace_file = 0; |
| 260 | sqlite3_io_trace = 0; |
| 261 | } |
| 262 | if( argv[1][0] ){ |
| 263 | if( strcmp(argv[1],"stdout")==0 ){ |
| 264 | iotrace_file = stdout; |
| 265 | }else if( strcmp(argv[1],"stderr")==0 ){ |
| 266 | iotrace_file = stderr; |
| 267 | }else{ |
| 268 | iotrace_file = fopen(argv[1], "w"); |
| 269 | } |
| 270 | sqlite3_io_trace = io_trace_callback; |
| 271 | } |
| 272 | return SQLITE_OK; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 277 | ** Usage: sqlite3_exec_printf DB FORMAT STRING |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 278 | ** |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 279 | ** Invoke the sqlite3_exec_printf() interface using the open database |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 280 | ** DB. The SQL is the string FORMAT. The format string should contain |
| 281 | ** one %s or %q. STRING is the value inserted into %s or %q. |
| 282 | */ |
| 283 | static int test_exec_printf( |
| 284 | void *NotUsed, |
| 285 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 286 | int argc, /* Number of arguments */ |
| 287 | char **argv /* Text of each argument */ |
| 288 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 289 | sqlite3 *db; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 290 | Tcl_DString str; |
| 291 | int rc; |
| 292 | char *zErr = 0; |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 293 | char *zSql; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 294 | char zBuf[30]; |
| 295 | if( argc!=4 ){ |
| 296 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 297 | " DB FORMAT STRING", 0); |
| 298 | return TCL_ERROR; |
| 299 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 300 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 301 | Tcl_DStringInit(&str); |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 302 | zSql = sqlite3_mprintf(argv[2], argv[3]); |
| 303 | rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr); |
| 304 | sqlite3_free(zSql); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 305 | sprintf(zBuf, "%d", rc); |
| 306 | Tcl_AppendElement(interp, zBuf); |
| 307 | Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr); |
| 308 | Tcl_DStringFree(&str); |
danielk1977 | 926aab2 | 2006-06-27 07:34:40 +0000 | [diff] [blame] | 309 | if( zErr ) sqlite3_free(zErr); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 310 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 311 | return TCL_OK; |
| 312 | } |
| 313 | |
| 314 | /* |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 315 | ** Usage: db_enter DB |
| 316 | ** db_leave DB |
| 317 | ** |
| 318 | ** Enter or leave the mutex on a database connection. |
| 319 | */ |
| 320 | static int db_enter( |
| 321 | void *NotUsed, |
| 322 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 323 | int argc, /* Number of arguments */ |
| 324 | char **argv /* Text of each argument */ |
| 325 | ){ |
| 326 | sqlite3 *db; |
| 327 | if( argc!=2 ){ |
| 328 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 329 | " DB", 0); |
| 330 | return TCL_ERROR; |
| 331 | } |
| 332 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 333 | sqlite3_mutex_enter(db->mutex); |
| 334 | return TCL_OK; |
| 335 | } |
| 336 | static int db_leave( |
| 337 | void *NotUsed, |
| 338 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 339 | int argc, /* Number of arguments */ |
| 340 | char **argv /* Text of each argument */ |
| 341 | ){ |
| 342 | sqlite3 *db; |
| 343 | if( argc!=2 ){ |
| 344 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 345 | " DB", 0); |
| 346 | return TCL_ERROR; |
| 347 | } |
| 348 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 349 | sqlite3_mutex_leave(db->mutex); |
| 350 | return TCL_OK; |
| 351 | } |
| 352 | |
| 353 | /* |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 354 | ** Usage: sqlite3_exec DB SQL |
| 355 | ** |
| 356 | ** Invoke the sqlite3_exec interface using the open database DB |
| 357 | */ |
| 358 | static int test_exec( |
| 359 | void *NotUsed, |
| 360 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 361 | int argc, /* Number of arguments */ |
| 362 | char **argv /* Text of each argument */ |
| 363 | ){ |
| 364 | sqlite3 *db; |
| 365 | Tcl_DString str; |
| 366 | int rc; |
| 367 | char *zErr = 0; |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 368 | char *zSql; |
| 369 | int i, j; |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 370 | char zBuf[30]; |
| 371 | if( argc!=3 ){ |
| 372 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 373 | " DB SQL", 0); |
| 374 | return TCL_ERROR; |
| 375 | } |
| 376 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 377 | Tcl_DStringInit(&str); |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 378 | zSql = sqlite3_mprintf("%s", argv[2]); |
| 379 | for(i=j=0; zSql[i];){ |
| 380 | if( zSql[i]=='%' ){ |
| 381 | zSql[j++] = (testHexToInt(zSql[i+1])<<4) + testHexToInt(zSql[i+2]); |
| 382 | i += 3; |
| 383 | }else{ |
| 384 | zSql[j++] = zSql[i++]; |
| 385 | } |
| 386 | } |
| 387 | zSql[j] = 0; |
| 388 | rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr); |
| 389 | sqlite3_free(zSql); |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 390 | sprintf(zBuf, "%d", rc); |
| 391 | Tcl_AppendElement(interp, zBuf); |
| 392 | Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr); |
| 393 | Tcl_DStringFree(&str); |
| 394 | if( zErr ) sqlite3_free(zErr); |
| 395 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 396 | return TCL_OK; |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | ** Usage: sqlite3_exec_nr DB SQL |
| 401 | ** |
| 402 | ** Invoke the sqlite3_exec interface using the open database DB. Discard |
| 403 | ** all results |
| 404 | */ |
| 405 | static int test_exec_nr( |
| 406 | void *NotUsed, |
| 407 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 408 | int argc, /* Number of arguments */ |
| 409 | char **argv /* Text of each argument */ |
| 410 | ){ |
| 411 | sqlite3 *db; |
| 412 | int rc; |
| 413 | char *zErr = 0; |
| 414 | if( argc!=3 ){ |
| 415 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 416 | " DB SQL", 0); |
| 417 | return TCL_ERROR; |
| 418 | } |
| 419 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 420 | rc = sqlite3_exec(db, argv[2], 0, 0, &zErr); |
| 421 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 422 | return TCL_OK; |
| 423 | } |
| 424 | |
| 425 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 426 | ** Usage: sqlite3_mprintf_z_test SEPARATOR ARG0 ARG1 ... |
drh | d93d8a8 | 2003-06-16 03:08:18 +0000 | [diff] [blame] | 427 | ** |
drh | 05a8298 | 2006-03-19 13:00:25 +0000 | [diff] [blame] | 428 | ** Test the %z format of sqliteMPrintf(). Use multiple mprintf() calls to |
drh | d93d8a8 | 2003-06-16 03:08:18 +0000 | [diff] [blame] | 429 | ** concatenate arg0 through argn using separator as the separator. |
| 430 | ** Return the result. |
| 431 | */ |
| 432 | static int test_mprintf_z( |
| 433 | void *NotUsed, |
| 434 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 435 | int argc, /* Number of arguments */ |
| 436 | char **argv /* Text of each argument */ |
| 437 | ){ |
| 438 | char *zResult = 0; |
| 439 | int i; |
| 440 | |
danielk1977 | ca0c897 | 2007-09-01 09:02:53 +0000 | [diff] [blame] | 441 | for(i=2; i<argc && (i==2 || zResult); i++){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 442 | zResult = sqlite3MPrintf(0, "%z%s%s", zResult, argv[1], argv[i]); |
drh | d93d8a8 | 2003-06-16 03:08:18 +0000 | [diff] [blame] | 443 | } |
| 444 | Tcl_AppendResult(interp, zResult, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 445 | sqlite3_free(zResult); |
drh | d93d8a8 | 2003-06-16 03:08:18 +0000 | [diff] [blame] | 446 | return TCL_OK; |
| 447 | } |
| 448 | |
| 449 | /* |
drh | 05a8298 | 2006-03-19 13:00:25 +0000 | [diff] [blame] | 450 | ** Usage: sqlite3_mprintf_n_test STRING |
| 451 | ** |
| 452 | ** Test the %n format of sqliteMPrintf(). Return the length of the |
| 453 | ** input string. |
| 454 | */ |
| 455 | static int test_mprintf_n( |
| 456 | void *NotUsed, |
| 457 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 458 | int argc, /* Number of arguments */ |
| 459 | char **argv /* Text of each argument */ |
| 460 | ){ |
| 461 | char *zStr; |
| 462 | int n = 0; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 463 | zStr = sqlite3MPrintf(0, "%s%n", argv[1], &n); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 464 | sqlite3_free(zStr); |
drh | 05a8298 | 2006-03-19 13:00:25 +0000 | [diff] [blame] | 465 | Tcl_SetObjResult(interp, Tcl_NewIntObj(n)); |
| 466 | return TCL_OK; |
| 467 | } |
| 468 | |
| 469 | /* |
drh | 6885390 | 2007-05-07 11:24:30 +0000 | [diff] [blame] | 470 | ** Usage: sqlite3_snprintf_int SIZE FORMAT INT |
| 471 | ** |
| 472 | ** Test the of sqlite3_snprintf() routine. SIZE is the size of the |
| 473 | ** output buffer in bytes. The maximum size is 100. FORMAT is the |
| 474 | ** format string. INT is a single integer argument. The FORMAT |
| 475 | ** string must require no more than this one integer argument. If |
| 476 | ** You pass in a format string that requires more than one argument, |
| 477 | ** bad things will happen. |
| 478 | */ |
| 479 | static int test_snprintf_int( |
| 480 | void *NotUsed, |
| 481 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 482 | int argc, /* Number of arguments */ |
| 483 | char **argv /* Text of each argument */ |
| 484 | ){ |
| 485 | char zStr[100]; |
| 486 | int n = atoi(argv[1]); |
drh | 6885390 | 2007-05-07 11:24:30 +0000 | [diff] [blame] | 487 | const char *zFormat = argv[2]; |
| 488 | int a1 = atoi(argv[3]); |
drh | daf276d | 2007-06-15 18:53:14 +0000 | [diff] [blame] | 489 | if( n>sizeof(zStr) ) n = sizeof(zStr); |
drh | 6885390 | 2007-05-07 11:24:30 +0000 | [diff] [blame] | 490 | strcpy(zStr, "abcdefghijklmnopqrstuvwxyz"); |
| 491 | sqlite3_snprintf(n, zStr, zFormat, a1); |
| 492 | Tcl_AppendResult(interp, zStr, 0); |
| 493 | return TCL_OK; |
| 494 | } |
| 495 | |
| 496 | /* |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame^] | 497 | ** Usage: sqlite3_get_table_printf DB FORMAT STRING ?--no-counts? |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 498 | ** |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 499 | ** Invoke the sqlite3_get_table_printf() interface using the open database |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 500 | ** DB. The SQL is the string FORMAT. The format string should contain |
| 501 | ** one %s or %q. STRING is the value inserted into %s or %q. |
| 502 | */ |
| 503 | static int test_get_table_printf( |
| 504 | void *NotUsed, |
| 505 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 506 | int argc, /* Number of arguments */ |
| 507 | char **argv /* Text of each argument */ |
| 508 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 509 | sqlite3 *db; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 510 | Tcl_DString str; |
| 511 | int rc; |
| 512 | char *zErr = 0; |
| 513 | int nRow, nCol; |
| 514 | char **aResult; |
| 515 | int i; |
| 516 | char zBuf[30]; |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 517 | char *zSql; |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame^] | 518 | int resCount = -1; |
| 519 | if( argc==5 ){ |
| 520 | if( Tcl_GetInt(interp, argv[4], &resCount) ) return TCL_ERROR; |
| 521 | } |
| 522 | if( argc!=4 && argc!=5 ){ |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 523 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame^] | 524 | " DB FORMAT STRING ?COUNT?", 0); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 525 | return TCL_ERROR; |
| 526 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 527 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 528 | Tcl_DStringInit(&str); |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 529 | zSql = sqlite3_mprintf(argv[2],argv[3]); |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame^] | 530 | if( argc==5 ){ |
| 531 | rc = sqlite3_get_table(db, zSql, &aResult, 0, 0, &zErr); |
| 532 | }else{ |
| 533 | rc = sqlite3_get_table(db, zSql, &aResult, &nRow, &nCol, &zErr); |
| 534 | resCount = (nRow+1)*nCol; |
| 535 | } |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 536 | sqlite3_free(zSql); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 537 | sprintf(zBuf, "%d", rc); |
| 538 | Tcl_AppendElement(interp, zBuf); |
| 539 | if( rc==SQLITE_OK ){ |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame^] | 540 | if( argc==4 ){ |
| 541 | sprintf(zBuf, "%d", nRow); |
| 542 | Tcl_AppendElement(interp, zBuf); |
| 543 | sprintf(zBuf, "%d", nCol); |
| 544 | Tcl_AppendElement(interp, zBuf); |
| 545 | } |
| 546 | for(i=0; i<resCount; i++){ |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 547 | Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL"); |
| 548 | } |
| 549 | }else{ |
| 550 | Tcl_AppendElement(interp, zErr); |
| 551 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 552 | sqlite3_free_table(aResult); |
danielk1977 | 926aab2 | 2006-06-27 07:34:40 +0000 | [diff] [blame] | 553 | if( zErr ) sqlite3_free(zErr); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 554 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 555 | return TCL_OK; |
| 556 | } |
| 557 | |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 558 | |
| 559 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 560 | ** Usage: sqlite3_last_insert_rowid DB |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 561 | ** |
| 562 | ** Returns the integer ROWID of the most recent insert. |
| 563 | */ |
| 564 | static int test_last_rowid( |
| 565 | void *NotUsed, |
| 566 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 567 | int argc, /* Number of arguments */ |
| 568 | char **argv /* Text of each argument */ |
| 569 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 570 | sqlite3 *db; |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 571 | char zBuf[30]; |
| 572 | |
| 573 | if( argc!=2 ){ |
| 574 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", 0); |
| 575 | return TCL_ERROR; |
| 576 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 577 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 578 | sprintf(zBuf, "%lld", sqlite3_last_insert_rowid(db)); |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 579 | Tcl_AppendResult(interp, zBuf, 0); |
| 580 | return SQLITE_OK; |
| 581 | } |
| 582 | |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 583 | /* |
drh | 25d6543 | 2004-07-22 15:02:25 +0000 | [diff] [blame] | 584 | ** Usage: sqlite3_key DB KEY |
| 585 | ** |
| 586 | ** Set the codec key. |
| 587 | */ |
| 588 | static int test_key( |
| 589 | void *NotUsed, |
| 590 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 591 | int argc, /* Number of arguments */ |
| 592 | char **argv /* Text of each argument */ |
| 593 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 594 | sqlite3 *db; |
drh | 25d6543 | 2004-07-22 15:02:25 +0000 | [diff] [blame] | 595 | const char *zKey; |
| 596 | int nKey; |
| 597 | if( argc!=3 ){ |
| 598 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 599 | " FILENAME\"", 0); |
| 600 | return TCL_ERROR; |
| 601 | } |
| 602 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 603 | zKey = argv[2]; |
| 604 | nKey = strlen(zKey); |
| 605 | #ifdef SQLITE_HAS_CODEC |
| 606 | sqlite3_key(db, zKey, nKey); |
| 607 | #endif |
| 608 | return TCL_OK; |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | ** Usage: sqlite3_rekey DB KEY |
| 613 | ** |
| 614 | ** Change the codec key. |
| 615 | */ |
| 616 | static int test_rekey( |
| 617 | void *NotUsed, |
| 618 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 619 | int argc, /* Number of arguments */ |
| 620 | char **argv /* Text of each argument */ |
| 621 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 622 | sqlite3 *db; |
drh | 25d6543 | 2004-07-22 15:02:25 +0000 | [diff] [blame] | 623 | const char *zKey; |
| 624 | int nKey; |
| 625 | if( argc!=3 ){ |
| 626 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 627 | " FILENAME\"", 0); |
| 628 | return TCL_ERROR; |
| 629 | } |
| 630 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 631 | zKey = argv[2]; |
| 632 | nKey = strlen(zKey); |
| 633 | #ifdef SQLITE_HAS_CODEC |
| 634 | sqlite3_rekey(db, zKey, nKey); |
| 635 | #endif |
| 636 | return TCL_OK; |
| 637 | } |
| 638 | |
| 639 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 640 | ** Usage: sqlite3_close DB |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 641 | ** |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 642 | ** Closes the database opened by sqlite3_open. |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 643 | */ |
| 644 | static int sqlite_test_close( |
| 645 | void *NotUsed, |
| 646 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 647 | int argc, /* Number of arguments */ |
| 648 | char **argv /* Text of each argument */ |
| 649 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 650 | sqlite3 *db; |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 651 | int rc; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 652 | if( argc!=2 ){ |
| 653 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 654 | " FILENAME\"", 0); |
| 655 | return TCL_ERROR; |
| 656 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 657 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 658 | rc = sqlite3_close(db); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 659 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 660 | return TCL_OK; |
| 661 | } |
| 662 | |
| 663 | /* |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 664 | ** Implementation of the x_coalesce() function. |
| 665 | ** Return the first argument non-NULL argument. |
| 666 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 667 | static void t1_ifnullFunc( |
| 668 | sqlite3_context *context, |
| 669 | int argc, |
| 670 | sqlite3_value **argv |
| 671 | ){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 672 | int i; |
| 673 | for(i=0; i<argc; i++){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 674 | if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){ |
drh | 9310ef2 | 2007-04-27 17:16:20 +0000 | [diff] [blame] | 675 | int n = sqlite3_value_bytes(argv[i]); |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 676 | sqlite3_result_text(context, (char*)sqlite3_value_text(argv[i]), |
drh | 9310ef2 | 2007-04-27 17:16:20 +0000 | [diff] [blame] | 677 | n, SQLITE_TRANSIENT); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 678 | break; |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | /* |
drh | f031381 | 2006-09-04 15:53:53 +0000 | [diff] [blame] | 684 | ** These are test functions. hex8() interprets its argument as |
| 685 | ** UTF8 and returns a hex encoding. hex16le() interprets its argument |
| 686 | ** as UTF16le and returns a hex encoding. |
| 687 | */ |
| 688 | static void hex8Func(sqlite3_context *p, int argc, sqlite3_value **argv){ |
| 689 | const unsigned char *z; |
| 690 | int i; |
| 691 | char zBuf[200]; |
| 692 | z = sqlite3_value_text(argv[0]); |
| 693 | for(i=0; i<sizeof(zBuf)/2 - 2 && z[i]; i++){ |
| 694 | sprintf(&zBuf[i*2], "%02x", z[i]&0xff); |
| 695 | } |
| 696 | zBuf[i*2] = 0; |
| 697 | sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT); |
| 698 | } |
drh | af30469 | 2007-04-23 23:56:31 +0000 | [diff] [blame] | 699 | #ifndef SQLITE_OMIT_UTF16 |
drh | f031381 | 2006-09-04 15:53:53 +0000 | [diff] [blame] | 700 | static void hex16Func(sqlite3_context *p, int argc, sqlite3_value **argv){ |
| 701 | const unsigned short int *z; |
| 702 | int i; |
| 703 | char zBuf[400]; |
| 704 | z = sqlite3_value_text16(argv[0]); |
| 705 | for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){ |
| 706 | sprintf(&zBuf[i*4], "%04x", z[i]&0xff); |
| 707 | } |
| 708 | zBuf[i*4] = 0; |
| 709 | sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT); |
| 710 | } |
drh | af30469 | 2007-04-23 23:56:31 +0000 | [diff] [blame] | 711 | #endif |
drh | f031381 | 2006-09-04 15:53:53 +0000 | [diff] [blame] | 712 | |
| 713 | /* |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 714 | ** A structure into which to accumulate text. |
| 715 | */ |
| 716 | struct dstr { |
| 717 | int nAlloc; /* Space allocated */ |
| 718 | int nUsed; /* Space used */ |
| 719 | char *z; /* The space */ |
| 720 | }; |
| 721 | |
| 722 | /* |
| 723 | ** Append text to a dstr |
| 724 | */ |
| 725 | static void dstrAppend(struct dstr *p, const char *z, int divider){ |
| 726 | int n = strlen(z); |
| 727 | if( p->nUsed + n + 2 > p->nAlloc ){ |
| 728 | char *zNew; |
| 729 | p->nAlloc = p->nAlloc*2 + n + 200; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 730 | zNew = sqlite3_realloc(p->z, p->nAlloc); |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 731 | if( zNew==0 ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 732 | sqlite3_free(p->z); |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 733 | memset(p, 0, sizeof(*p)); |
| 734 | return; |
| 735 | } |
| 736 | p->z = zNew; |
| 737 | } |
| 738 | if( divider && p->nUsed>0 ){ |
| 739 | p->z[p->nUsed++] = divider; |
| 740 | } |
| 741 | memcpy(&p->z[p->nUsed], z, n+1); |
| 742 | p->nUsed += n; |
| 743 | } |
| 744 | |
| 745 | /* |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 746 | ** Invoked for each callback from sqlite3ExecFunc |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 747 | */ |
| 748 | static int execFuncCallback(void *pData, int argc, char **argv, char **NotUsed){ |
| 749 | struct dstr *p = (struct dstr*)pData; |
| 750 | int i; |
| 751 | for(i=0; i<argc; i++){ |
| 752 | if( argv[i]==0 ){ |
| 753 | dstrAppend(p, "NULL", ' '); |
| 754 | }else{ |
| 755 | dstrAppend(p, argv[i], ' '); |
| 756 | } |
| 757 | } |
| 758 | return 0; |
| 759 | } |
| 760 | |
| 761 | /* |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 762 | ** Implementation of the x_sqlite_exec() function. This function takes |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 763 | ** a single argument and attempts to execute that argument as SQL code. |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 764 | ** This is illegal and should set the SQLITE_MISUSE flag on the database. |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 765 | ** |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 766 | ** 2004-Jan-07: We have changed this to make it legal to call sqlite3_exec() |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 767 | ** from within a function call. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 768 | ** |
| 769 | ** This routine simulates the effect of having two threads attempt to |
| 770 | ** use the same database at the same time. |
| 771 | */ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 772 | static void sqlite3ExecFunc( |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 773 | sqlite3_context *context, |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 774 | int argc, |
| 775 | sqlite3_value **argv |
| 776 | ){ |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 777 | struct dstr x; |
| 778 | memset(&x, 0, sizeof(x)); |
drh | 3752785 | 2006-03-16 16:19:56 +0000 | [diff] [blame] | 779 | (void)sqlite3_exec((sqlite3*)sqlite3_user_data(context), |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 780 | (char*)sqlite3_value_text(argv[0]), |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 781 | execFuncCallback, &x, 0); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 782 | sqlite3_result_text(context, x.z, x.nUsed, SQLITE_TRANSIENT); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 783 | sqlite3_free(x.z); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | /* |
danielk1977 | d726392 | 2007-02-05 14:21:47 +0000 | [diff] [blame] | 787 | ** Implementation of tkt2213func(), a scalar function that takes exactly |
| 788 | ** one argument. It has two interesting features: |
| 789 | ** |
| 790 | ** * It calls sqlite3_value_text() 3 times on the argument sqlite3_value*. |
| 791 | ** If the three pointers returned are not the same an SQL error is raised. |
| 792 | ** |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 793 | ** * Otherwise it returns a copy of the text representation of its |
danielk1977 | d726392 | 2007-02-05 14:21:47 +0000 | [diff] [blame] | 794 | ** argument in such a way as the VDBE representation is a Mem* cell |
| 795 | ** with the MEM_Term flag clear. |
| 796 | ** |
| 797 | ** Ticket #2213 can therefore be tested by evaluating the following |
| 798 | ** SQL expression: |
| 799 | ** |
| 800 | ** tkt2213func(tkt2213func('a string')); |
| 801 | */ |
| 802 | static void tkt2213Function( |
| 803 | sqlite3_context *context, |
| 804 | int argc, |
| 805 | sqlite3_value **argv |
| 806 | ){ |
| 807 | int nText; |
| 808 | unsigned char const *zText1; |
| 809 | unsigned char const *zText2; |
| 810 | unsigned char const *zText3; |
| 811 | |
| 812 | nText = sqlite3_value_bytes(argv[0]); |
| 813 | zText1 = sqlite3_value_text(argv[0]); |
| 814 | zText2 = sqlite3_value_text(argv[0]); |
| 815 | zText3 = sqlite3_value_text(argv[0]); |
| 816 | |
| 817 | if( zText1!=zText2 || zText2!=zText3 ){ |
| 818 | sqlite3_result_error(context, "tkt2213 is not fixed", -1); |
| 819 | }else{ |
| 820 | char *zCopy = (char *)sqlite3_malloc(nText); |
| 821 | memcpy(zCopy, zText1, nText); |
| 822 | sqlite3_result_text(context, zCopy, nText, sqlite3_free); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | /* |
drh | 9310ef2 | 2007-04-27 17:16:20 +0000 | [diff] [blame] | 827 | ** The following SQL function takes 4 arguments. The 2nd and |
| 828 | ** 4th argument must be one of these strings: 'text', 'text16', |
| 829 | ** or 'blob' corresponding to API functions |
| 830 | ** |
| 831 | ** sqlite3_value_text() |
| 832 | ** sqlite3_value_text16() |
| 833 | ** sqlite3_value_blob() |
| 834 | ** |
| 835 | ** The third argument is a string, either 'bytes' or 'bytes16' or 'noop', |
| 836 | ** corresponding to APIs: |
| 837 | ** |
| 838 | ** sqlite3_value_bytes() |
| 839 | ** sqlite3_value_bytes16() |
| 840 | ** noop |
| 841 | ** |
| 842 | ** The APIs designated by the 2nd through 4th arguments are applied |
| 843 | ** to the first argument in order. If the pointers returned by the |
| 844 | ** second and fourth are different, this routine returns 1. Otherwise, |
| 845 | ** this routine returns 0. |
| 846 | ** |
| 847 | ** This function is used to test to see when returned pointers from |
| 848 | ** the _text(), _text16() and _blob() APIs become invalidated. |
| 849 | */ |
| 850 | static void ptrChngFunction( |
| 851 | sqlite3_context *context, |
| 852 | int argc, |
| 853 | sqlite3_value **argv |
| 854 | ){ |
| 855 | const void *p1, *p2; |
| 856 | const char *zCmd; |
| 857 | if( argc!=4 ) return; |
| 858 | zCmd = (const char*)sqlite3_value_text(argv[1]); |
| 859 | if( zCmd==0 ) return; |
| 860 | if( strcmp(zCmd,"text")==0 ){ |
| 861 | p1 = (const void*)sqlite3_value_text(argv[0]); |
| 862 | #ifndef SQLITE_OMIT_UTF16 |
| 863 | }else if( strcmp(zCmd, "text16")==0 ){ |
| 864 | p1 = (const void*)sqlite3_value_text16(argv[0]); |
| 865 | #endif |
| 866 | }else if( strcmp(zCmd, "blob")==0 ){ |
| 867 | p1 = (const void*)sqlite3_value_blob(argv[0]); |
| 868 | }else{ |
| 869 | return; |
| 870 | } |
| 871 | zCmd = (const char*)sqlite3_value_text(argv[2]); |
| 872 | if( zCmd==0 ) return; |
| 873 | if( strcmp(zCmd,"bytes")==0 ){ |
| 874 | sqlite3_value_bytes(argv[0]); |
| 875 | #ifndef SQLITE_OMIT_UTF16 |
| 876 | }else if( strcmp(zCmd, "bytes16")==0 ){ |
| 877 | sqlite3_value_bytes16(argv[0]); |
| 878 | #endif |
| 879 | }else if( strcmp(zCmd, "noop")==0 ){ |
| 880 | /* do nothing */ |
| 881 | }else{ |
| 882 | return; |
| 883 | } |
| 884 | zCmd = (const char*)sqlite3_value_text(argv[3]); |
| 885 | if( zCmd==0 ) return; |
| 886 | if( strcmp(zCmd,"text")==0 ){ |
| 887 | p2 = (const void*)sqlite3_value_text(argv[0]); |
| 888 | #ifndef SQLITE_OMIT_UTF16 |
| 889 | }else if( strcmp(zCmd, "text16")==0 ){ |
| 890 | p2 = (const void*)sqlite3_value_text16(argv[0]); |
| 891 | #endif |
| 892 | }else if( strcmp(zCmd, "blob")==0 ){ |
| 893 | p2 = (const void*)sqlite3_value_blob(argv[0]); |
| 894 | }else{ |
| 895 | return; |
| 896 | } |
| 897 | sqlite3_result_int(context, p1!=p2); |
| 898 | } |
| 899 | |
| 900 | |
| 901 | /* |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 902 | ** Usage: sqlite_test_create_function DB |
| 903 | ** |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 904 | ** Call the sqlite3_create_function API on the given database in order |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 905 | ** to create a function named "x_coalesce". This function does the same thing |
| 906 | ** as the "coalesce" function. This function also registers an SQL function |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 907 | ** named "x_sqlite_exec" that invokes sqlite3_exec(). Invoking sqlite3_exec() |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 908 | ** in this way is illegal recursion and should raise an SQLITE_MISUSE error. |
| 909 | ** The effect is similar to trying to use the same database connection from |
| 910 | ** two threads at the same time. |
| 911 | ** |
| 912 | ** The original motivation for this routine was to be able to call the |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 913 | ** sqlite3_create_function function while a query is in progress in order |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 914 | ** to test the SQLITE_MISUSE detection logic. |
| 915 | */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 916 | static int test_create_function( |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 917 | void *NotUsed, |
| 918 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 919 | int argc, /* Number of arguments */ |
| 920 | char **argv /* Text of each argument */ |
| 921 | ){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 922 | int rc; |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 923 | sqlite3 *db; |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 924 | extern void Md5_Register(sqlite3*); |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 925 | |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 926 | if( argc!=2 ){ |
| 927 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 928 | " DB\"", 0); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 929 | return TCL_ERROR; |
| 930 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 931 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 932 | rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_ANY, 0, |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 933 | t1_ifnullFunc, 0, 0); |
drh | 235a818 | 2006-09-13 19:21:28 +0000 | [diff] [blame] | 934 | if( rc==SQLITE_OK ){ |
| 935 | rc = sqlite3_create_function(db, "hex8", 1, SQLITE_ANY, 0, |
| 936 | hex8Func, 0, 0); |
| 937 | } |
drh | af30469 | 2007-04-23 23:56:31 +0000 | [diff] [blame] | 938 | #ifndef SQLITE_OMIT_UTF16 |
drh | 235a818 | 2006-09-13 19:21:28 +0000 | [diff] [blame] | 939 | if( rc==SQLITE_OK ){ |
| 940 | rc = sqlite3_create_function(db, "hex16", 1, SQLITE_ANY, 0, |
| 941 | hex16Func, 0, 0); |
| 942 | } |
drh | af30469 | 2007-04-23 23:56:31 +0000 | [diff] [blame] | 943 | #endif |
danielk1977 | d726392 | 2007-02-05 14:21:47 +0000 | [diff] [blame] | 944 | if( rc==SQLITE_OK ){ |
| 945 | rc = sqlite3_create_function(db, "tkt2213func", 1, SQLITE_ANY, 0, |
| 946 | tkt2213Function, 0, 0); |
| 947 | } |
drh | 9310ef2 | 2007-04-27 17:16:20 +0000 | [diff] [blame] | 948 | if( rc==SQLITE_OK ){ |
| 949 | rc = sqlite3_create_function(db, "pointer_change", 4, SQLITE_ANY, 0, |
| 950 | ptrChngFunction, 0, 0); |
| 951 | } |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 952 | |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 953 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 954 | /* Use the sqlite3_create_function16() API here. Mainly for fun, but also |
| 955 | ** because it is not tested anywhere else. */ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 956 | if( rc==SQLITE_OK ){ |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 957 | sqlite3_value *pVal; |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 958 | sqlite3_mutex_enter(db->mutex); |
| 959 | pVal = sqlite3ValueNew(db); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 960 | sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC); |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 961 | if( db->mallocFailed ){ |
| 962 | rc = SQLITE_NOMEM; |
| 963 | }else{ |
| 964 | rc = sqlite3_create_function16(db, |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 965 | sqlite3ValueText(pVal, SQLITE_UTF16NATIVE), |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 966 | 1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0); |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 967 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 968 | sqlite3ValueFree(pVal); |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 969 | sqlite3_mutex_leave(db->mutex); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 970 | } |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 971 | #endif |
| 972 | |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 973 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 974 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 975 | return TCL_OK; |
| 976 | } |
| 977 | |
| 978 | /* |
| 979 | ** Routines to implement the x_count() aggregate function. |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 980 | ** |
| 981 | ** x_count() counts the number of non-null arguments. But there are |
| 982 | ** some twists for testing purposes. |
| 983 | ** |
| 984 | ** If the argument to x_count() is 40 then a UTF-8 error is reported |
| 985 | ** on the step function. If x_count(41) is seen, then a UTF-16 error |
| 986 | ** is reported on the step function. If the total count is 42, then |
| 987 | ** a UTF-8 error is reported on the finalize function. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 988 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 989 | typedef struct t1CountCtx t1CountCtx; |
| 990 | struct t1CountCtx { |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 991 | int n; |
| 992 | }; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 993 | static void t1CountStep( |
| 994 | sqlite3_context *context, |
| 995 | int argc, |
| 996 | sqlite3_value **argv |
| 997 | ){ |
| 998 | t1CountCtx *p; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 999 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 1000 | if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1001 | p->n++; |
| 1002 | } |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1003 | if( argc>0 ){ |
| 1004 | int v = sqlite3_value_int(argv[0]); |
| 1005 | if( v==40 ){ |
| 1006 | sqlite3_result_error(context, "value of 40 handed to x_count", -1); |
danielk1977 | a1686c9 | 2006-01-23 07:52:37 +0000 | [diff] [blame] | 1007 | #ifndef SQLITE_OMIT_UTF16 |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1008 | }else if( v==41 ){ |
| 1009 | const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0}; |
| 1010 | sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1); |
danielk1977 | a1686c9 | 2006-01-23 07:52:37 +0000 | [diff] [blame] | 1011 | #endif |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1012 | } |
| 1013 | } |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1014 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1015 | static void t1CountFinalize(sqlite3_context *context){ |
| 1016 | t1CountCtx *p; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1017 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1018 | if( p ){ |
| 1019 | if( p->n==42 ){ |
| 1020 | sqlite3_result_error(context, "x_count totals to 42", -1); |
| 1021 | }else{ |
| 1022 | sqlite3_result_int(context, p ? p->n : 0); |
| 1023 | } |
| 1024 | } |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1027 | static void legacyCountStep( |
| 1028 | sqlite3_context *context, |
| 1029 | int argc, |
| 1030 | sqlite3_value **argv |
| 1031 | ){ |
| 1032 | /* no-op */ |
| 1033 | } |
| 1034 | static void legacyCountFinalize(sqlite3_context *context){ |
| 1035 | sqlite3_result_int(context, sqlite3_aggregate_count(context)); |
| 1036 | } |
| 1037 | |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1038 | /* |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1039 | ** Usage: sqlite3_create_aggregate DB |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1040 | ** |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1041 | ** Call the sqlite3_create_function API on the given database in order |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1042 | ** to create a function named "x_count". This function is similar |
| 1043 | ** to the built-in count() function, with a few special quirks |
| 1044 | ** for testing the sqlite3_result_error() APIs. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1045 | ** |
| 1046 | ** The original motivation for this routine was to be able to call the |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1047 | ** sqlite3_create_aggregate function while a query is in progress in order |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1048 | ** to test the SQLITE_MISUSE detection logic. See misuse.test. |
| 1049 | ** |
| 1050 | ** This routine was later extended to test the use of sqlite3_result_error() |
| 1051 | ** within aggregate functions. |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1052 | ** |
| 1053 | ** Later: It is now also extended to register the aggregate function |
| 1054 | ** "legacy_count()" with the supplied database handle. This is used |
| 1055 | ** to test the deprecated sqlite3_aggregate_count() API. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1056 | */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1057 | static int test_create_aggregate( |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1058 | void *NotUsed, |
| 1059 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1060 | int argc, /* Number of arguments */ |
| 1061 | char **argv /* Text of each argument */ |
| 1062 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1063 | sqlite3 *db; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1064 | int rc; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1065 | if( argc!=2 ){ |
| 1066 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1067 | " FILENAME\"", 0); |
| 1068 | return TCL_ERROR; |
| 1069 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 1070 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1071 | rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0, |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1072 | t1CountStep,t1CountFinalize); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1073 | if( rc==SQLITE_OK ){ |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1074 | rc = sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0, |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1075 | t1CountStep,t1CountFinalize); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1076 | } |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1077 | if( rc==SQLITE_OK ){ |
| 1078 | rc = sqlite3_create_function(db, "legacy_count", 0, SQLITE_ANY, 0, 0, |
| 1079 | legacyCountStep, legacyCountFinalize |
| 1080 | ); |
| 1081 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1082 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1083 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1084 | return TCL_OK; |
| 1085 | } |
| 1086 | |
| 1087 | |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 1088 | /* |
| 1089 | ** Usage: printf TEXT |
| 1090 | ** |
| 1091 | ** Send output to printf. Use this rather than puts to merge the output |
| 1092 | ** in the correct sequence with debugging printfs inserted into C code. |
| 1093 | ** Puts uses a separate buffer and debugging statements will be out of |
| 1094 | ** sequence if it is used. |
| 1095 | */ |
| 1096 | static int test_printf( |
| 1097 | void *NotUsed, |
| 1098 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1099 | int argc, /* Number of arguments */ |
| 1100 | char **argv /* Text of each argument */ |
| 1101 | ){ |
| 1102 | if( argc!=2 ){ |
| 1103 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1104 | " TEXT\"", 0); |
| 1105 | return TCL_ERROR; |
| 1106 | } |
| 1107 | printf("%s\n", argv[1]); |
| 1108 | return TCL_OK; |
| 1109 | } |
| 1110 | |
| 1111 | |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1112 | |
| 1113 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1114 | ** Usage: sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1115 | ** |
| 1116 | ** Call mprintf with three integer arguments |
| 1117 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1118 | static int sqlite3_mprintf_int( |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1119 | void *NotUsed, |
| 1120 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1121 | int argc, /* Number of arguments */ |
| 1122 | char **argv /* Text of each argument */ |
| 1123 | ){ |
| 1124 | int a[3], i; |
| 1125 | char *z; |
| 1126 | if( argc!=5 ){ |
| 1127 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1128 | " FORMAT INT INT INT\"", 0); |
| 1129 | return TCL_ERROR; |
| 1130 | } |
| 1131 | for(i=2; i<5; i++){ |
| 1132 | if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; |
| 1133 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1134 | z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1135 | Tcl_AppendResult(interp, z, 0); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1136 | sqlite3_free(z); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1137 | return TCL_OK; |
| 1138 | } |
| 1139 | |
| 1140 | /* |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 1141 | ** If zNum represents an integer that will fit in 64-bits, then set |
| 1142 | ** *pValue to that integer and return true. Otherwise return false. |
| 1143 | */ |
| 1144 | static int sqlite3GetInt64(const char *zNum, i64 *pValue){ |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 1145 | if( sqlite3FitsIn64Bits(zNum, 0) ){ |
drh | b6a9ece | 2007-06-26 00:37:27 +0000 | [diff] [blame] | 1146 | sqlite3Atoi64(zNum, pValue); |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 1147 | return 1; |
| 1148 | } |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | /* |
drh | e970767 | 2004-06-25 01:10:48 +0000 | [diff] [blame] | 1153 | ** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER |
| 1154 | ** |
| 1155 | ** Call mprintf with three 64-bit integer arguments |
| 1156 | */ |
| 1157 | static int sqlite3_mprintf_int64( |
| 1158 | void *NotUsed, |
| 1159 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1160 | int argc, /* Number of arguments */ |
| 1161 | char **argv /* Text of each argument */ |
| 1162 | ){ |
| 1163 | int i; |
| 1164 | sqlite_int64 a[3]; |
| 1165 | char *z; |
| 1166 | if( argc!=5 ){ |
| 1167 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1168 | " FORMAT INT INT INT\"", 0); |
| 1169 | return TCL_ERROR; |
| 1170 | } |
| 1171 | for(i=2; i<5; i++){ |
| 1172 | if( !sqlite3GetInt64(argv[i], &a[i-2]) ){ |
| 1173 | Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", 0); |
| 1174 | return TCL_ERROR; |
| 1175 | } |
| 1176 | } |
| 1177 | z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]); |
| 1178 | Tcl_AppendResult(interp, z, 0); |
| 1179 | sqlite3_free(z); |
| 1180 | return TCL_OK; |
| 1181 | } |
| 1182 | |
| 1183 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1184 | ** Usage: sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1185 | ** |
| 1186 | ** Call mprintf with two integer arguments and one string argument |
| 1187 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1188 | static int sqlite3_mprintf_str( |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1189 | void *NotUsed, |
| 1190 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1191 | int argc, /* Number of arguments */ |
| 1192 | char **argv /* Text of each argument */ |
| 1193 | ){ |
| 1194 | int a[3], i; |
| 1195 | char *z; |
chw | f220b24 | 2002-06-16 04:54:28 +0000 | [diff] [blame] | 1196 | if( argc<4 || argc>5 ){ |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1197 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
chw | f220b24 | 2002-06-16 04:54:28 +0000 | [diff] [blame] | 1198 | " FORMAT INT INT ?STRING?\"", 0); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1199 | return TCL_ERROR; |
| 1200 | } |
| 1201 | for(i=2; i<4; i++){ |
| 1202 | if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; |
| 1203 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1204 | z = sqlite3_mprintf(argv[1], a[0], a[1], argc>4 ? argv[4] : NULL); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1205 | Tcl_AppendResult(interp, z, 0); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1206 | sqlite3_free(z); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1207 | return TCL_OK; |
| 1208 | } |
| 1209 | |
| 1210 | /* |
drh | b3738b6 | 2007-03-31 15:02:49 +0000 | [diff] [blame] | 1211 | ** Usage: sqlite3_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING |
| 1212 | ** |
| 1213 | ** Call mprintf with two integer arguments and one string argument |
| 1214 | */ |
| 1215 | static int sqlite3_snprintf_str( |
| 1216 | void *NotUsed, |
| 1217 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1218 | int argc, /* Number of arguments */ |
| 1219 | char **argv /* Text of each argument */ |
| 1220 | ){ |
| 1221 | int a[3], i; |
| 1222 | int n; |
| 1223 | char *z; |
| 1224 | if( argc<5 || argc>6 ){ |
| 1225 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1226 | " INT FORMAT INT INT ?STRING?\"", 0); |
| 1227 | return TCL_ERROR; |
| 1228 | } |
| 1229 | if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR; |
| 1230 | if( n<0 ){ |
| 1231 | Tcl_AppendResult(interp, "N must be non-negative", 0); |
| 1232 | return TCL_ERROR; |
| 1233 | } |
| 1234 | for(i=3; i<5; i++){ |
| 1235 | if( Tcl_GetInt(interp, argv[i], &a[i-3]) ) return TCL_ERROR; |
| 1236 | } |
| 1237 | z = sqlite3_malloc( n+1 ); |
| 1238 | sqlite3_snprintf(n, z, argv[2], a[0], a[1], argc>4 ? argv[5] : NULL); |
| 1239 | Tcl_AppendResult(interp, z, 0); |
| 1240 | sqlite3_free(z); |
| 1241 | return TCL_OK; |
| 1242 | } |
| 1243 | |
| 1244 | /* |
drh | 6378285 | 2005-08-30 19:30:59 +0000 | [diff] [blame] | 1245 | ** Usage: sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1246 | ** |
| 1247 | ** Call mprintf with two integer arguments and one double argument |
| 1248 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1249 | static int sqlite3_mprintf_double( |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1250 | void *NotUsed, |
| 1251 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1252 | int argc, /* Number of arguments */ |
| 1253 | char **argv /* Text of each argument */ |
| 1254 | ){ |
| 1255 | int a[3], i; |
| 1256 | double r; |
| 1257 | char *z; |
| 1258 | if( argc!=5 ){ |
| 1259 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
drh | 6378285 | 2005-08-30 19:30:59 +0000 | [diff] [blame] | 1260 | " FORMAT INT INT DOUBLE\"", 0); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1261 | return TCL_ERROR; |
| 1262 | } |
| 1263 | for(i=2; i<4; i++){ |
| 1264 | if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; |
| 1265 | } |
| 1266 | if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1267 | z = sqlite3_mprintf(argv[1], a[0], a[1], r); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1268 | Tcl_AppendResult(interp, z, 0); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1269 | sqlite3_free(z); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1270 | return TCL_OK; |
| 1271 | } |
| 1272 | |
| 1273 | /* |
drh | 6378285 | 2005-08-30 19:30:59 +0000 | [diff] [blame] | 1274 | ** Usage: sqlite3_mprintf_scaled FORMAT DOUBLE DOUBLE |
drh | b621c23 | 2004-02-21 19:41:04 +0000 | [diff] [blame] | 1275 | ** |
| 1276 | ** Call mprintf with a single double argument which is the product of the |
| 1277 | ** two arguments given above. This is used to generate overflow and underflow |
| 1278 | ** doubles to test that they are converted properly. |
| 1279 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1280 | static int sqlite3_mprintf_scaled( |
drh | b621c23 | 2004-02-21 19:41:04 +0000 | [diff] [blame] | 1281 | void *NotUsed, |
| 1282 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1283 | int argc, /* Number of arguments */ |
| 1284 | char **argv /* Text of each argument */ |
| 1285 | ){ |
| 1286 | int i; |
| 1287 | double r[2]; |
| 1288 | char *z; |
| 1289 | if( argc!=4 ){ |
| 1290 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1291 | " FORMAT DOUBLE DOUBLE\"", 0); |
| 1292 | return TCL_ERROR; |
| 1293 | } |
| 1294 | for(i=2; i<4; i++){ |
| 1295 | if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR; |
| 1296 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1297 | z = sqlite3_mprintf(argv[1], r[0]*r[1]); |
drh | b621c23 | 2004-02-21 19:41:04 +0000 | [diff] [blame] | 1298 | Tcl_AppendResult(interp, z, 0); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1299 | sqlite3_free(z); |
drh | b621c23 | 2004-02-21 19:41:04 +0000 | [diff] [blame] | 1300 | return TCL_OK; |
| 1301 | } |
| 1302 | |
| 1303 | /* |
drh | e29b1a0 | 2004-07-17 21:56:09 +0000 | [diff] [blame] | 1304 | ** Usage: sqlite3_mprintf_stronly FORMAT STRING |
| 1305 | ** |
| 1306 | ** Call mprintf with a single double argument which is the product of the |
| 1307 | ** two arguments given above. This is used to generate overflow and underflow |
| 1308 | ** doubles to test that they are converted properly. |
| 1309 | */ |
| 1310 | static int sqlite3_mprintf_stronly( |
| 1311 | void *NotUsed, |
| 1312 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1313 | int argc, /* Number of arguments */ |
| 1314 | char **argv /* Text of each argument */ |
| 1315 | ){ |
| 1316 | char *z; |
| 1317 | if( argc!=3 ){ |
| 1318 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1319 | " FORMAT STRING\"", 0); |
| 1320 | return TCL_ERROR; |
| 1321 | } |
| 1322 | z = sqlite3_mprintf(argv[1], argv[2]); |
| 1323 | Tcl_AppendResult(interp, z, 0); |
| 1324 | sqlite3_free(z); |
| 1325 | return TCL_OK; |
| 1326 | } |
| 1327 | |
| 1328 | /* |
drh | 6378285 | 2005-08-30 19:30:59 +0000 | [diff] [blame] | 1329 | ** Usage: sqlite3_mprintf_hexdouble FORMAT HEX |
| 1330 | ** |
| 1331 | ** Call mprintf with a single double argument which is derived from the |
| 1332 | ** hexadecimal encoding of an IEEE double. |
| 1333 | */ |
| 1334 | static int sqlite3_mprintf_hexdouble( |
| 1335 | void *NotUsed, |
| 1336 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1337 | int argc, /* Number of arguments */ |
| 1338 | char **argv /* Text of each argument */ |
| 1339 | ){ |
| 1340 | char *z; |
| 1341 | double r; |
| 1342 | unsigned x1, x2; |
| 1343 | long long unsigned d; |
| 1344 | if( argc!=3 ){ |
| 1345 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1346 | " FORMAT STRING\"", 0); |
| 1347 | return TCL_ERROR; |
| 1348 | } |
| 1349 | if( sscanf(argv[2], "%08x%08x", &x2, &x1)!=2 ){ |
| 1350 | Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", 0); |
| 1351 | return TCL_ERROR; |
| 1352 | } |
| 1353 | d = x2; |
| 1354 | d = (d<<32) + x1; |
| 1355 | memcpy(&r, &d, sizeof(r)); |
| 1356 | z = sqlite3_mprintf(argv[1], r); |
| 1357 | Tcl_AppendResult(interp, z, 0); |
| 1358 | sqlite3_free(z); |
| 1359 | return TCL_OK; |
| 1360 | } |
| 1361 | |
| 1362 | /* |
danielk1977 | 5262282 | 2006-01-09 09:59:49 +0000 | [diff] [blame] | 1363 | ** Usage: sqlite3_enable_shared_cache BOOLEAN |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1364 | ** |
| 1365 | */ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1366 | #if !defined(SQLITE_OMIT_SHARED_CACHE) |
| 1367 | static int test_enable_shared( |
danielk1977 | 5262282 | 2006-01-09 09:59:49 +0000 | [diff] [blame] | 1368 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1369 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1370 | int objc, /* Number of arguments */ |
| 1371 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1372 | ){ |
| 1373 | int rc; |
| 1374 | int enable; |
danielk1977 | 5262282 | 2006-01-09 09:59:49 +0000 | [diff] [blame] | 1375 | int ret = 0; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1376 | extern int sqlite3SharedCacheEnabled; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1377 | |
| 1378 | if( objc!=2 ){ |
| 1379 | Tcl_WrongNumArgs(interp, 1, objv, "BOOLEAN"); |
| 1380 | return TCL_ERROR; |
| 1381 | } |
| 1382 | if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ){ |
| 1383 | return TCL_ERROR; |
| 1384 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1385 | ret = sqlite3SharedCacheEnabled; |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1386 | rc = sqlite3_enable_shared_cache(enable); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1387 | if( rc!=SQLITE_OK ){ |
| 1388 | Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC); |
| 1389 | return TCL_ERROR; |
| 1390 | } |
danielk1977 | 5262282 | 2006-01-09 09:59:49 +0000 | [diff] [blame] | 1391 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(ret)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1392 | return TCL_OK; |
| 1393 | } |
| 1394 | #endif |
| 1395 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1396 | |
| 1397 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1398 | /* |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1399 | ** Usage: sqlite3_extended_result_codes DB BOOLEAN |
| 1400 | ** |
| 1401 | */ |
| 1402 | static int test_extended_result_codes( |
| 1403 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 1404 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1405 | int objc, /* Number of arguments */ |
| 1406 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1407 | ){ |
| 1408 | int enable; |
| 1409 | sqlite3 *db; |
| 1410 | |
| 1411 | if( objc!=3 ){ |
| 1412 | Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN"); |
| 1413 | return TCL_ERROR; |
| 1414 | } |
| 1415 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 1416 | if( Tcl_GetBooleanFromObj(interp, objv[2], &enable) ) return TCL_ERROR; |
| 1417 | sqlite3_extended_result_codes(db, enable); |
| 1418 | return TCL_OK; |
| 1419 | } |
| 1420 | |
| 1421 | /* |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 1422 | ** Usage: sqlite3_libversion_number |
| 1423 | ** |
| 1424 | */ |
| 1425 | static int test_libversion_number( |
| 1426 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 1427 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1428 | int objc, /* Number of arguments */ |
| 1429 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1430 | ){ |
| 1431 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_libversion_number())); |
| 1432 | return TCL_OK; |
| 1433 | } |
| 1434 | |
| 1435 | /* |
danielk1977 | deb802c | 2006-02-09 13:43:28 +0000 | [diff] [blame] | 1436 | ** Usage: sqlite3_table_column_metadata DB dbname tblname colname |
| 1437 | ** |
| 1438 | */ |
| 1439 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 1440 | static int test_table_column_metadata( |
| 1441 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 1442 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1443 | int objc, /* Number of arguments */ |
| 1444 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1445 | ){ |
| 1446 | sqlite3 *db; |
| 1447 | const char *zDb; |
| 1448 | const char *zTbl; |
| 1449 | const char *zCol; |
| 1450 | int rc; |
| 1451 | Tcl_Obj *pRet; |
| 1452 | |
| 1453 | const char *zDatatype; |
| 1454 | const char *zCollseq; |
| 1455 | int notnull; |
| 1456 | int primarykey; |
| 1457 | int autoincrement; |
| 1458 | |
| 1459 | if( objc!=5 ){ |
| 1460 | Tcl_WrongNumArgs(interp, 1, objv, "DB dbname tblname colname"); |
| 1461 | return TCL_ERROR; |
| 1462 | } |
| 1463 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 1464 | zDb = Tcl_GetString(objv[2]); |
| 1465 | zTbl = Tcl_GetString(objv[3]); |
| 1466 | zCol = Tcl_GetString(objv[4]); |
| 1467 | |
| 1468 | if( strlen(zDb)==0 ) zDb = 0; |
| 1469 | |
| 1470 | rc = sqlite3_table_column_metadata(db, zDb, zTbl, zCol, |
| 1471 | &zDatatype, &zCollseq, ¬null, &primarykey, &autoincrement); |
| 1472 | |
| 1473 | if( rc!=SQLITE_OK ){ |
| 1474 | Tcl_AppendResult(interp, sqlite3_errmsg(db), 0); |
| 1475 | return TCL_ERROR; |
| 1476 | } |
| 1477 | |
| 1478 | pRet = Tcl_NewObj(); |
| 1479 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zDatatype, -1)); |
| 1480 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zCollseq, -1)); |
| 1481 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(notnull)); |
| 1482 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(primarykey)); |
| 1483 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(autoincrement)); |
| 1484 | Tcl_SetObjResult(interp, pRet); |
| 1485 | |
| 1486 | return TCL_OK; |
| 1487 | } |
| 1488 | #endif |
| 1489 | |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1490 | #ifndef SQLITE_OMIT_INCRBLOB |
| 1491 | |
| 1492 | /* |
| 1493 | ** sqlite3_blob_read CHANNEL OFFSET N |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1494 | ** |
| 1495 | ** This command is used to test the sqlite3_blob_read() in ways that |
| 1496 | ** the Tcl channel interface does not. The first argument should |
| 1497 | ** be the name of a valid channel created by the [incrblob] method |
| 1498 | ** of a database handle. This function calls sqlite3_blob_read() |
| 1499 | ** to read N bytes from offset OFFSET from the underlying SQLite |
| 1500 | ** blob handle. |
| 1501 | ** |
| 1502 | ** On success, a byte-array object containing the read data is |
| 1503 | ** returned. On failure, the interpreter result is set to the |
| 1504 | ** text representation of the returned error code (i.e. "SQLITE_NOMEM") |
| 1505 | ** and a Tcl exception is thrown. |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1506 | */ |
| 1507 | static int test_blob_read( |
| 1508 | ClientData clientData, /* Not used */ |
| 1509 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1510 | int objc, /* Number of arguments */ |
| 1511 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1512 | ){ |
| 1513 | Tcl_Channel channel; |
| 1514 | ClientData instanceData; |
| 1515 | sqlite3_blob *pBlob; |
| 1516 | int notUsed; |
| 1517 | int nByte; |
| 1518 | int iOffset; |
| 1519 | unsigned char *zBuf; |
| 1520 | int rc; |
| 1521 | |
| 1522 | if( objc!=4 ){ |
| 1523 | Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET N"); |
| 1524 | return TCL_ERROR; |
| 1525 | } |
| 1526 | |
| 1527 | channel = Tcl_GetChannel(interp, Tcl_GetString(objv[1]), ¬Used); |
| 1528 | if( !channel |
| 1529 | || TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset) |
| 1530 | || TCL_OK!=Tcl_GetIntFromObj(interp, objv[3], &nByte) |
| 1531 | || nByte<0 || iOffset<0 |
| 1532 | ){ |
| 1533 | return TCL_ERROR; |
| 1534 | } |
| 1535 | |
| 1536 | instanceData = Tcl_GetChannelInstanceData(channel); |
| 1537 | pBlob = *((sqlite3_blob **)instanceData); |
| 1538 | |
| 1539 | zBuf = (unsigned char *)Tcl_Alloc(nByte); |
| 1540 | rc = sqlite3_blob_read(pBlob, zBuf, nByte, iOffset); |
| 1541 | if( rc==SQLITE_OK ){ |
| 1542 | Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zBuf, nByte)); |
| 1543 | }else{ |
| 1544 | Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE); |
| 1545 | } |
| 1546 | Tcl_Free((char *)zBuf); |
| 1547 | |
| 1548 | return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR); |
| 1549 | } |
| 1550 | |
| 1551 | /* |
| 1552 | ** sqlite3_blob_write CHANNEL OFFSET DATA |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1553 | ** |
| 1554 | ** This command is used to test the sqlite3_blob_write() in ways that |
| 1555 | ** the Tcl channel interface does not. The first argument should |
| 1556 | ** be the name of a valid channel created by the [incrblob] method |
| 1557 | ** of a database handle. This function calls sqlite3_blob_write() |
| 1558 | ** to write the DATA byte-array to the underlying SQLite blob handle. |
| 1559 | ** at offset OFFSET. |
| 1560 | ** |
| 1561 | ** On success, an empty string is returned. On failure, the interpreter |
| 1562 | ** result is set to the text representation of the returned error code |
| 1563 | ** (i.e. "SQLITE_NOMEM") and a Tcl exception is thrown. |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1564 | */ |
| 1565 | static int test_blob_write( |
| 1566 | ClientData clientData, /* Not used */ |
| 1567 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1568 | int objc, /* Number of arguments */ |
| 1569 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1570 | ){ |
| 1571 | Tcl_Channel channel; |
| 1572 | ClientData instanceData; |
| 1573 | sqlite3_blob *pBlob; |
| 1574 | int notUsed; |
| 1575 | int iOffset; |
| 1576 | int rc; |
| 1577 | |
| 1578 | unsigned char *zBuf; |
| 1579 | int nBuf; |
| 1580 | |
| 1581 | if( objc!=4 ){ |
| 1582 | Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET DATA"); |
| 1583 | return TCL_ERROR; |
| 1584 | } |
| 1585 | |
| 1586 | channel = Tcl_GetChannel(interp, Tcl_GetString(objv[1]), ¬Used); |
| 1587 | if( !channel |
| 1588 | || TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset) |
| 1589 | || iOffset<0 |
| 1590 | ){ |
| 1591 | return TCL_ERROR; |
| 1592 | } |
| 1593 | |
| 1594 | instanceData = Tcl_GetChannelInstanceData(channel); |
| 1595 | pBlob = *((sqlite3_blob **)instanceData); |
| 1596 | |
| 1597 | zBuf = Tcl_GetByteArrayFromObj(objv[3], &nBuf); |
| 1598 | rc = sqlite3_blob_write(pBlob, zBuf, nBuf, iOffset); |
| 1599 | if( rc!=SQLITE_OK ){ |
| 1600 | Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE); |
| 1601 | } |
| 1602 | |
| 1603 | return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR); |
| 1604 | } |
| 1605 | #endif |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 1606 | |
danielk1977 | deb802c | 2006-02-09 13:43:28 +0000 | [diff] [blame] | 1607 | /* |
danielk1977 | a393c03 | 2007-05-07 14:58:53 +0000 | [diff] [blame] | 1608 | ** Usage: sqlite3_create_collation_v2 DB-HANDLE NAME CMP-PROC DEL-PROC |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1609 | ** |
| 1610 | ** This Tcl proc is used for testing the experimental |
danielk1977 | a393c03 | 2007-05-07 14:58:53 +0000 | [diff] [blame] | 1611 | ** sqlite3_create_collation_v2() interface. |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1612 | */ |
| 1613 | struct TestCollationX { |
| 1614 | Tcl_Interp *interp; |
| 1615 | Tcl_Obj *pCmp; |
| 1616 | Tcl_Obj *pDel; |
| 1617 | }; |
| 1618 | typedef struct TestCollationX TestCollationX; |
| 1619 | static void testCreateCollationDel(void *pCtx){ |
| 1620 | TestCollationX *p = (TestCollationX *)pCtx; |
| 1621 | |
| 1622 | int rc = Tcl_EvalObjEx(p->interp, p->pDel, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL); |
| 1623 | if( rc!=TCL_OK ){ |
| 1624 | Tcl_BackgroundError(p->interp); |
| 1625 | } |
| 1626 | |
| 1627 | Tcl_DecrRefCount(p->pCmp); |
| 1628 | Tcl_DecrRefCount(p->pDel); |
| 1629 | sqlite3_free((void *)p); |
| 1630 | } |
| 1631 | static int testCreateCollationCmp( |
| 1632 | void *pCtx, |
| 1633 | int nLeft, |
| 1634 | const void *zLeft, |
| 1635 | int nRight, |
| 1636 | const void *zRight |
| 1637 | ){ |
| 1638 | TestCollationX *p = (TestCollationX *)pCtx; |
| 1639 | Tcl_Obj *pScript = Tcl_DuplicateObj(p->pCmp); |
| 1640 | int iRes = 0; |
| 1641 | |
| 1642 | Tcl_IncrRefCount(pScript); |
| 1643 | Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zLeft, nLeft)); |
| 1644 | Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zRight,nRight)); |
| 1645 | |
| 1646 | if( TCL_OK!=Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL) |
| 1647 | || TCL_OK!=Tcl_GetIntFromObj(p->interp, Tcl_GetObjResult(p->interp), &iRes) |
| 1648 | ){ |
| 1649 | Tcl_BackgroundError(p->interp); |
| 1650 | } |
| 1651 | Tcl_DecrRefCount(pScript); |
| 1652 | |
| 1653 | return iRes; |
| 1654 | } |
danielk1977 | a393c03 | 2007-05-07 14:58:53 +0000 | [diff] [blame] | 1655 | static int test_create_collation_v2( |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1656 | ClientData clientData, /* Not used */ |
| 1657 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1658 | int objc, /* Number of arguments */ |
| 1659 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1660 | ){ |
| 1661 | TestCollationX *p; |
| 1662 | sqlite3 *db; |
| 1663 | |
| 1664 | if( objc!=5 ){ |
| 1665 | Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE NAME CMP-PROC DEL-PROC"); |
| 1666 | return TCL_ERROR; |
| 1667 | } |
| 1668 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 1669 | |
| 1670 | p = (TestCollationX *)sqlite3_malloc(sizeof(TestCollationX)); |
| 1671 | p->pCmp = objv[3]; |
| 1672 | p->pDel = objv[4]; |
| 1673 | p->interp = interp; |
| 1674 | Tcl_IncrRefCount(p->pCmp); |
| 1675 | Tcl_IncrRefCount(p->pDel); |
| 1676 | |
danielk1977 | a393c03 | 2007-05-07 14:58:53 +0000 | [diff] [blame] | 1677 | sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), SQLITE_UTF8, |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1678 | (void *)p, testCreateCollationCmp, testCreateCollationDel |
| 1679 | ); |
| 1680 | return TCL_OK; |
| 1681 | } |
| 1682 | |
| 1683 | /* |
danielk1977 | 69e777f | 2006-06-14 10:38:02 +0000 | [diff] [blame] | 1684 | ** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC? |
| 1685 | */ |
| 1686 | static int test_load_extension( |
| 1687 | ClientData clientData, /* Not used */ |
| 1688 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1689 | int objc, /* Number of arguments */ |
| 1690 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1691 | ){ |
| 1692 | Tcl_CmdInfo cmdInfo; |
| 1693 | sqlite3 *db; |
| 1694 | int rc; |
| 1695 | char *zDb; |
| 1696 | char *zFile; |
| 1697 | char *zProc = 0; |
| 1698 | char *zErr = 0; |
| 1699 | |
| 1700 | if( objc!=4 && objc!=3 ){ |
| 1701 | Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE FILE ?PROC?"); |
| 1702 | return TCL_ERROR; |
| 1703 | } |
| 1704 | zDb = Tcl_GetString(objv[1]); |
| 1705 | zFile = Tcl_GetString(objv[2]); |
| 1706 | if( objc==4 ){ |
| 1707 | zProc = Tcl_GetString(objv[3]); |
| 1708 | } |
| 1709 | |
| 1710 | /* Extract the C database handle from the Tcl command name */ |
| 1711 | if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){ |
| 1712 | Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0); |
| 1713 | return TCL_ERROR; |
| 1714 | } |
| 1715 | db = ((struct SqliteDb*)cmdInfo.objClientData)->db; |
| 1716 | assert(db); |
| 1717 | |
| 1718 | /* Call the underlying C function. If an error occurs, set rc to |
| 1719 | ** TCL_ERROR and load any error string into the interpreter. If no |
| 1720 | ** error occurs, set rc to TCL_OK. |
| 1721 | */ |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 1722 | #ifdef SQLITE_OMIT_LOAD_EXTENSION |
| 1723 | rc = SQLITE_ERROR; |
| 1724 | zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()"); |
| 1725 | #else |
danielk1977 | 69e777f | 2006-06-14 10:38:02 +0000 | [diff] [blame] | 1726 | rc = sqlite3_load_extension(db, zFile, zProc, &zErr); |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 1727 | #endif |
danielk1977 | 69e777f | 2006-06-14 10:38:02 +0000 | [diff] [blame] | 1728 | if( rc!=SQLITE_OK ){ |
| 1729 | Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE); |
| 1730 | rc = TCL_ERROR; |
| 1731 | }else{ |
| 1732 | rc = TCL_OK; |
| 1733 | } |
| 1734 | sqlite3_free(zErr); |
| 1735 | |
| 1736 | return rc; |
| 1737 | } |
| 1738 | |
| 1739 | /* |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 1740 | ** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF |
| 1741 | */ |
| 1742 | static int test_enable_load( |
| 1743 | ClientData clientData, /* Not used */ |
| 1744 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1745 | int objc, /* Number of arguments */ |
| 1746 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1747 | ){ |
| 1748 | Tcl_CmdInfo cmdInfo; |
| 1749 | sqlite3 *db; |
| 1750 | char *zDb; |
| 1751 | int onoff; |
| 1752 | |
| 1753 | if( objc!=3 ){ |
| 1754 | Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF"); |
| 1755 | return TCL_ERROR; |
| 1756 | } |
| 1757 | zDb = Tcl_GetString(objv[1]); |
| 1758 | |
| 1759 | /* Extract the C database handle from the Tcl command name */ |
| 1760 | if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){ |
| 1761 | Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0); |
| 1762 | return TCL_ERROR; |
| 1763 | } |
| 1764 | db = ((struct SqliteDb*)cmdInfo.objClientData)->db; |
| 1765 | assert(db); |
| 1766 | |
| 1767 | /* Get the onoff parameter */ |
| 1768 | if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){ |
| 1769 | return TCL_ERROR; |
| 1770 | } |
| 1771 | |
| 1772 | #ifdef SQLITE_OMIT_LOAD_EXTENSION |
| 1773 | Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()"); |
| 1774 | return TCL_ERROR; |
| 1775 | #else |
| 1776 | sqlite3_enable_load_extension(db, onoff); |
| 1777 | return TCL_OK; |
| 1778 | #endif |
| 1779 | } |
| 1780 | |
| 1781 | /* |
drh | 28b4e48 | 2002-03-11 02:06:13 +0000 | [diff] [blame] | 1782 | ** Usage: sqlite_abort |
| 1783 | ** |
| 1784 | ** Shutdown the process immediately. This is not a clean shutdown. |
| 1785 | ** This command is used to test the recoverability of a database in |
| 1786 | ** the event of a program crash. |
| 1787 | */ |
| 1788 | static int sqlite_abort( |
| 1789 | void *NotUsed, |
| 1790 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1791 | int argc, /* Number of arguments */ |
| 1792 | char **argv /* Text of each argument */ |
| 1793 | ){ |
| 1794 | assert( interp==0 ); /* This will always fail */ |
| 1795 | return TCL_OK; |
| 1796 | } |
| 1797 | |
| 1798 | /* |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 1799 | ** The following routine is a user-defined SQL function whose purpose |
| 1800 | ** is to test the sqlite_set_result() API. |
| 1801 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 1802 | static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 1803 | while( argc>=2 ){ |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 1804 | const char *zArg0 = (char*)sqlite3_value_text(argv[0]); |
danielk1977 | 6d88bad | 2004-05-27 14:23:36 +0000 | [diff] [blame] | 1805 | if( zArg0 ){ |
| 1806 | if( 0==sqlite3StrICmp(zArg0, "int") ){ |
| 1807 | sqlite3_result_int(context, sqlite3_value_int(argv[1])); |
| 1808 | }else if( sqlite3StrICmp(zArg0,"int64")==0 ){ |
| 1809 | sqlite3_result_int64(context, sqlite3_value_int64(argv[1])); |
| 1810 | }else if( sqlite3StrICmp(zArg0,"string")==0 ){ |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 1811 | sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1812 | SQLITE_TRANSIENT); |
danielk1977 | 6d88bad | 2004-05-27 14:23:36 +0000 | [diff] [blame] | 1813 | }else if( sqlite3StrICmp(zArg0,"double")==0 ){ |
| 1814 | sqlite3_result_double(context, sqlite3_value_double(argv[1])); |
| 1815 | }else if( sqlite3StrICmp(zArg0,"null")==0 ){ |
| 1816 | sqlite3_result_null(context); |
| 1817 | }else if( sqlite3StrICmp(zArg0,"value")==0 ){ |
| 1818 | sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]); |
| 1819 | }else{ |
| 1820 | goto error_out; |
| 1821 | } |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 1822 | }else{ |
danielk1977 | 6d88bad | 2004-05-27 14:23:36 +0000 | [diff] [blame] | 1823 | goto error_out; |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 1824 | } |
| 1825 | argc -= 2; |
| 1826 | argv += 2; |
| 1827 | } |
danielk1977 | 6d88bad | 2004-05-27 14:23:36 +0000 | [diff] [blame] | 1828 | return; |
| 1829 | |
| 1830 | error_out: |
| 1831 | sqlite3_result_error(context,"first argument should be one of: " |
| 1832 | "int int64 string double null value", -1); |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | /* |
| 1836 | ** Usage: sqlite_register_test_function DB NAME |
| 1837 | ** |
| 1838 | ** Register the test SQL function on the database DB under the name NAME. |
| 1839 | */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1840 | static int test_register_func( |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 1841 | void *NotUsed, |
| 1842 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1843 | int argc, /* Number of arguments */ |
| 1844 | char **argv /* Text of each argument */ |
| 1845 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1846 | sqlite3 *db; |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 1847 | int rc; |
| 1848 | if( argc!=3 ){ |
| 1849 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1850 | " DB FUNCTION-NAME", 0); |
| 1851 | return TCL_ERROR; |
| 1852 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 1853 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 1854 | rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1855 | testFunc, 0, 0); |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 1856 | if( rc!=0 ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 1857 | Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0); |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 1858 | return TCL_ERROR; |
| 1859 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1860 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 1861 | return TCL_OK; |
| 1862 | } |
| 1863 | |
| 1864 | /* |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1865 | ** Usage: sqlite3_finalize STMT |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 1866 | ** |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1867 | ** Finalize a statement handle. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 1868 | */ |
| 1869 | static int test_finalize( |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1870 | void * clientData, |
| 1871 | Tcl_Interp *interp, |
| 1872 | int objc, |
| 1873 | Tcl_Obj *CONST objv[] |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 1874 | ){ |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1875 | sqlite3_stmt *pStmt; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 1876 | int rc; |
drh | dddb2f2 | 2007-01-03 23:37:28 +0000 | [diff] [blame] | 1877 | sqlite3 *db = 0; |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1878 | |
| 1879 | if( objc!=2 ){ |
| 1880 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 1881 | Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 1882 | return TCL_ERROR; |
| 1883 | } |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1884 | |
| 1885 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 1886 | |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 1887 | if( pStmt ){ |
| 1888 | db = StmtToDb(pStmt); |
| 1889 | } |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 1890 | rc = sqlite3_finalize(pStmt); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1891 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 1892 | if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1893 | return TCL_OK; |
| 1894 | } |
| 1895 | |
| 1896 | /* |
| 1897 | ** Usage: sqlite3_reset STMT |
| 1898 | ** |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 1899 | ** Reset a statement handle. |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1900 | */ |
| 1901 | static int test_reset( |
| 1902 | void * clientData, |
| 1903 | Tcl_Interp *interp, |
| 1904 | int objc, |
| 1905 | Tcl_Obj *CONST objv[] |
| 1906 | ){ |
| 1907 | sqlite3_stmt *pStmt; |
| 1908 | int rc; |
| 1909 | |
| 1910 | if( objc!=2 ){ |
| 1911 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 1912 | Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); |
| 1913 | return TCL_ERROR; |
| 1914 | } |
| 1915 | |
| 1916 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 1917 | |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 1918 | rc = sqlite3_reset(pStmt); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 1919 | if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){ |
| 1920 | return TCL_ERROR; |
| 1921 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1922 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 1923 | /* |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1924 | if( rc ){ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 1925 | return TCL_ERROR; |
| 1926 | } |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 1927 | */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 1928 | return TCL_OK; |
| 1929 | } |
| 1930 | |
drh | 5a38705 | 2003-01-11 14:19:51 +0000 | [diff] [blame] | 1931 | /* |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 1932 | ** Usage: sqlite3_expired STMT |
| 1933 | ** |
| 1934 | ** Return TRUE if a recompilation of the statement is recommended. |
| 1935 | */ |
| 1936 | static int test_expired( |
| 1937 | void * clientData, |
| 1938 | Tcl_Interp *interp, |
| 1939 | int objc, |
| 1940 | Tcl_Obj *CONST objv[] |
| 1941 | ){ |
| 1942 | sqlite3_stmt *pStmt; |
| 1943 | if( objc!=2 ){ |
| 1944 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 1945 | Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); |
| 1946 | return TCL_ERROR; |
| 1947 | } |
| 1948 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 1949 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt))); |
| 1950 | return TCL_OK; |
| 1951 | } |
| 1952 | |
| 1953 | /* |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 1954 | ** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT |
| 1955 | ** |
| 1956 | ** Transfer all bindings from FROMSTMT over to TOSTMT |
| 1957 | */ |
| 1958 | static int test_transfer_bind( |
| 1959 | void * clientData, |
| 1960 | Tcl_Interp *interp, |
| 1961 | int objc, |
| 1962 | Tcl_Obj *CONST objv[] |
| 1963 | ){ |
| 1964 | sqlite3_stmt *pStmt1, *pStmt2; |
| 1965 | if( objc!=3 ){ |
| 1966 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 1967 | Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0); |
| 1968 | return TCL_ERROR; |
| 1969 | } |
| 1970 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR; |
| 1971 | if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR; |
| 1972 | Tcl_SetObjResult(interp, |
| 1973 | Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2))); |
| 1974 | return TCL_OK; |
| 1975 | } |
| 1976 | |
| 1977 | /* |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 1978 | ** Usage: sqlite3_changes DB |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1979 | ** |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 1980 | ** Return the number of changes made to the database by the last SQL |
| 1981 | ** execution. |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1982 | */ |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 1983 | static int test_changes( |
| 1984 | void * clientData, |
| 1985 | Tcl_Interp *interp, |
| 1986 | int objc, |
| 1987 | Tcl_Obj *CONST objv[] |
| 1988 | ){ |
| 1989 | sqlite3 *db; |
| 1990 | if( objc!=2 ){ |
| 1991 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 1992 | Tcl_GetString(objv[0]), " DB", 0); |
| 1993 | return TCL_ERROR; |
| 1994 | } |
| 1995 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 1996 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db))); |
| 1997 | return TCL_OK; |
| 1998 | } |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1999 | |
| 2000 | /* |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2001 | ** This is the "static_bind_value" that variables are bound to when |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2002 | ** the FLAG option of sqlite3_bind is "static" |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2003 | */ |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2004 | static char *sqlite_static_bind_value = 0; |
drh | f031381 | 2006-09-04 15:53:53 +0000 | [diff] [blame] | 2005 | static int sqlite_static_bind_nbyte = 0; |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2006 | |
| 2007 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2008 | ** Usage: sqlite3_bind VM IDX VALUE FLAGS |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2009 | ** |
| 2010 | ** Sets the value of the IDX-th occurance of "?" in the original SQL |
| 2011 | ** string. VALUE is the new value. If FLAGS=="null" then VALUE is |
| 2012 | ** ignored and the value is set to NULL. If FLAGS=="static" then |
| 2013 | ** the value is set to the value of a static variable named |
| 2014 | ** "sqlite_static_bind_value". If FLAGS=="normal" then a copy |
drh | bf8aa2a | 2005-12-02 02:44:05 +0000 | [diff] [blame] | 2015 | ** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored |
| 2016 | ** an a 10-byte blob "abc\000xyz\000pq" is inserted. |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2017 | */ |
| 2018 | static int test_bind( |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2019 | void *NotUsed, |
| 2020 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 2021 | int argc, /* Number of arguments */ |
| 2022 | char **argv /* Text of each argument */ |
| 2023 | ){ |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 2024 | sqlite3_stmt *pStmt; |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2025 | int rc; |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2026 | int idx; |
| 2027 | if( argc!=5 ){ |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2028 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2029 | " VM IDX VALUE (null|static|normal)\"", 0); |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2030 | return TCL_ERROR; |
| 2031 | } |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 2032 | if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR; |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2033 | if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR; |
| 2034 | if( strcmp(argv[4],"null")==0 ){ |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 2035 | rc = sqlite3_bind_null(pStmt, idx); |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2036 | }else if( strcmp(argv[4],"static")==0 ){ |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 2037 | rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0); |
drh | f031381 | 2006-09-04 15:53:53 +0000 | [diff] [blame] | 2038 | }else if( strcmp(argv[4],"static-nbytes")==0 ){ |
| 2039 | rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, |
| 2040 | sqlite_static_bind_nbyte, 0); |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2041 | }else if( strcmp(argv[4],"normal")==0 ){ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2042 | rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT); |
drh | bf8aa2a | 2005-12-02 02:44:05 +0000 | [diff] [blame] | 2043 | }else if( strcmp(argv[4],"blob10")==0 ){ |
| 2044 | rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC); |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2045 | }else{ |
| 2046 | Tcl_AppendResult(interp, "4th argument should be " |
| 2047 | "\"null\" or \"static\" or \"normal\"", 0); |
| 2048 | return TCL_ERROR; |
| 2049 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2050 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2051 | if( rc ){ |
| 2052 | char zBuf[50]; |
| 2053 | sprintf(zBuf, "(%d) ", rc); |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 2054 | Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0); |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2055 | return TCL_ERROR; |
| 2056 | } |
| 2057 | return TCL_OK; |
| 2058 | } |
| 2059 | |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 2060 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 2061 | /* |
| 2062 | ** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be> |
| 2063 | ** |
| 2064 | ** This function is used to test that SQLite selects the correct collation |
| 2065 | ** sequence callback when multiple versions (for different text encodings) |
| 2066 | ** are available. |
| 2067 | ** |
| 2068 | ** Calling this routine registers the collation sequence "test_collate" |
| 2069 | ** with database handle <db>. The second argument must be a list of three |
| 2070 | ** boolean values. If the first is true, then a version of test_collate is |
| 2071 | ** registered for UTF-8, if the second is true, a version is registered for |
| 2072 | ** UTF-16le, if the third is true, a UTF-16be version is available. |
| 2073 | ** Previous versions of test_collate are deleted. |
| 2074 | ** |
| 2075 | ** The collation sequence test_collate is implemented by calling the |
| 2076 | ** following TCL script: |
| 2077 | ** |
| 2078 | ** "test_collate <enc> <lhs> <rhs>" |
| 2079 | ** |
| 2080 | ** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8. |
| 2081 | ** The <enc> parameter is the encoding of the collation function that |
| 2082 | ** SQLite selected to call. The TCL test script implements the |
| 2083 | ** "test_collate" proc. |
| 2084 | ** |
| 2085 | ** Note that this will only work with one intepreter at a time, as the |
| 2086 | ** interp pointer to use when evaluating the TCL script is stored in |
| 2087 | ** pTestCollateInterp. |
| 2088 | */ |
| 2089 | static Tcl_Interp* pTestCollateInterp; |
| 2090 | static int test_collate_func( |
| 2091 | void *pCtx, |
| 2092 | int nA, const void *zA, |
| 2093 | int nB, const void *zB |
| 2094 | ){ |
| 2095 | Tcl_Interp *i = pTestCollateInterp; |
| 2096 | int encin = (int)pCtx; |
| 2097 | int res; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 2098 | int n; |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 2099 | |
| 2100 | sqlite3_value *pVal; |
| 2101 | Tcl_Obj *pX; |
| 2102 | |
| 2103 | pX = Tcl_NewStringObj("test_collate", -1); |
| 2104 | Tcl_IncrRefCount(pX); |
| 2105 | |
| 2106 | switch( encin ){ |
| 2107 | case SQLITE_UTF8: |
| 2108 | Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1)); |
| 2109 | break; |
| 2110 | case SQLITE_UTF16LE: |
| 2111 | Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1)); |
| 2112 | break; |
| 2113 | case SQLITE_UTF16BE: |
| 2114 | Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1)); |
| 2115 | break; |
| 2116 | default: |
| 2117 | assert(0); |
| 2118 | } |
| 2119 | |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2120 | pVal = sqlite3ValueNew(0); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2121 | sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC); |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 2122 | n = sqlite3_value_bytes(pVal); |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 2123 | Tcl_ListObjAppendElement(i,pX, |
| 2124 | Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n)); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2125 | sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC); |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 2126 | n = sqlite3_value_bytes(pVal); |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 2127 | Tcl_ListObjAppendElement(i,pX, |
| 2128 | Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n)); |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 2129 | sqlite3ValueFree(pVal); |
| 2130 | |
| 2131 | Tcl_EvalObjEx(i, pX, 0); |
| 2132 | Tcl_DecrRefCount(pX); |
| 2133 | Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res); |
| 2134 | return res; |
| 2135 | } |
| 2136 | static int test_collate( |
| 2137 | void * clientData, |
| 2138 | Tcl_Interp *interp, |
| 2139 | int objc, |
| 2140 | Tcl_Obj *CONST objv[] |
| 2141 | ){ |
| 2142 | sqlite3 *db; |
| 2143 | int val; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2144 | sqlite3_value *pVal; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2145 | int rc; |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 2146 | |
| 2147 | if( objc!=5 ) goto bad_args; |
| 2148 | pTestCollateInterp = interp; |
| 2149 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 2150 | |
| 2151 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2152 | rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8, |
| 2153 | (void *)SQLITE_UTF8, val?test_collate_func:0); |
| 2154 | if( rc==SQLITE_OK ){ |
| 2155 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR; |
| 2156 | rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE, |
| 2157 | (void *)SQLITE_UTF16LE, val?test_collate_func:0); |
| 2158 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2159 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2160 | #if 0 |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 2161 | if( sqlite3_iMallocFail>0 ){ |
| 2162 | sqlite3_iMallocFail++; |
| 2163 | } |
| 2164 | #endif |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 2165 | sqlite3_mutex_enter(db->mutex); |
| 2166 | pVal = sqlite3ValueNew(db); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2167 | sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC); |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 2168 | if( db->mallocFailed ){ |
| 2169 | rc = SQLITE_NOMEM; |
| 2170 | }else{ |
| 2171 | rc = sqlite3_create_collation16(db, |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2172 | sqlite3ValueText(pVal, SQLITE_UTF16NATIVE), SQLITE_UTF16BE, |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 2173 | (void *)SQLITE_UTF16BE, val?test_collate_func:0); |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 2174 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2175 | sqlite3ValueFree(pVal); |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 2176 | sqlite3_mutex_leave(db->mutex); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2177 | } |
| 2178 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 2179 | |
| 2180 | if( rc!=SQLITE_OK ){ |
| 2181 | Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0); |
| 2182 | return TCL_ERROR; |
| 2183 | } |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 2184 | return TCL_OK; |
| 2185 | |
| 2186 | bad_args: |
| 2187 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2188 | Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0); |
| 2189 | return TCL_ERROR; |
| 2190 | } |
| 2191 | |
drh | 268803a | 2005-12-14 20:11:30 +0000 | [diff] [blame] | 2192 | /* |
| 2193 | ** When the collation needed callback is invoked, record the name of |
| 2194 | ** the requested collating function here. The recorded name is linked |
| 2195 | ** to a TCL variable and used to make sure that the requested collation |
| 2196 | ** name is correct. |
| 2197 | */ |
| 2198 | static char zNeededCollation[200]; |
| 2199 | static char *pzNeededCollation = zNeededCollation; |
| 2200 | |
| 2201 | |
| 2202 | /* |
| 2203 | ** Called when a collating sequence is needed. Registered using |
| 2204 | ** sqlite3_collation_needed16(). |
| 2205 | */ |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2206 | static void test_collate_needed_cb( |
| 2207 | void *pCtx, |
| 2208 | sqlite3 *db, |
| 2209 | int eTextRep, |
drh | 268803a | 2005-12-14 20:11:30 +0000 | [diff] [blame] | 2210 | const void *pName |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2211 | ){ |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 2212 | int enc = ENC(db); |
drh | 268803a | 2005-12-14 20:11:30 +0000 | [diff] [blame] | 2213 | int i; |
| 2214 | char *z; |
| 2215 | for(z = (char*)pName, i=0; *z || z[1]; z++){ |
| 2216 | if( *z ) zNeededCollation[i++] = *z; |
| 2217 | } |
| 2218 | zNeededCollation[i] = 0; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2219 | sqlite3_create_collation( |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 2220 | db, "test_collate", ENC(db), (void *)enc, test_collate_func); |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | /* |
| 2224 | ** Usage: add_test_collate_needed DB |
| 2225 | */ |
| 2226 | static int test_collate_needed( |
| 2227 | void * clientData, |
| 2228 | Tcl_Interp *interp, |
| 2229 | int objc, |
| 2230 | Tcl_Obj *CONST objv[] |
| 2231 | ){ |
| 2232 | sqlite3 *db; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2233 | int rc; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2234 | |
| 2235 | if( objc!=2 ) goto bad_args; |
| 2236 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2237 | rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb); |
drh | 268803a | 2005-12-14 20:11:30 +0000 | [diff] [blame] | 2238 | zNeededCollation[0] = 0; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2239 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2240 | return TCL_OK; |
| 2241 | |
| 2242 | bad_args: |
| 2243 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 2244 | return TCL_ERROR; |
| 2245 | } |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 2246 | |
| 2247 | /* |
| 2248 | ** tclcmd: add_alignment_test_collations DB |
| 2249 | ** |
| 2250 | ** Add two new collating sequences to the database DB |
| 2251 | ** |
| 2252 | ** utf16_aligned |
| 2253 | ** utf16_unaligned |
| 2254 | ** |
| 2255 | ** Both collating sequences use the same sort order as BINARY. |
| 2256 | ** The only difference is that the utf16_aligned collating |
| 2257 | ** sequence is declared with the SQLITE_UTF16_ALIGNED flag. |
| 2258 | ** Both collating functions increment the unaligned utf16 counter |
| 2259 | ** whenever they see a string that begins on an odd byte boundary. |
| 2260 | */ |
| 2261 | static int unaligned_string_counter = 0; |
| 2262 | static int alignmentCollFunc( |
| 2263 | void *NotUsed, |
| 2264 | int nKey1, const void *pKey1, |
| 2265 | int nKey2, const void *pKey2 |
| 2266 | ){ |
| 2267 | int rc, n; |
| 2268 | n = nKey1<nKey2 ? nKey1 : nKey2; |
| 2269 | if( nKey1>0 && 1==(1&(int)pKey1) ) unaligned_string_counter++; |
| 2270 | if( nKey2>0 && 1==(1&(int)pKey2) ) unaligned_string_counter++; |
| 2271 | rc = memcmp(pKey1, pKey2, n); |
| 2272 | if( rc==0 ){ |
| 2273 | rc = nKey1 - nKey2; |
| 2274 | } |
| 2275 | return rc; |
| 2276 | } |
| 2277 | static int add_alignment_test_collations( |
| 2278 | void * clientData, |
| 2279 | Tcl_Interp *interp, |
| 2280 | int objc, |
| 2281 | Tcl_Obj *CONST objv[] |
| 2282 | ){ |
| 2283 | sqlite3 *db; |
| 2284 | if( objc>=2 ){ |
| 2285 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 2286 | sqlite3_create_collation(db, "utf16_unaligned", |
| 2287 | SQLITE_UTF16, |
| 2288 | 0, alignmentCollFunc); |
| 2289 | sqlite3_create_collation(db, "utf16_aligned", |
| 2290 | SQLITE_UTF16 | SQLITE_UTF16_ALIGNED, |
| 2291 | 0, alignmentCollFunc); |
| 2292 | } |
| 2293 | return SQLITE_OK; |
| 2294 | } |
| 2295 | #endif /* !defined(SQLITE_OMIT_UTF16) */ |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2296 | |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2297 | /* |
| 2298 | ** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be> |
| 2299 | ** |
| 2300 | ** This function is used to test that SQLite selects the correct user |
| 2301 | ** function callback when multiple versions (for different text encodings) |
| 2302 | ** are available. |
| 2303 | ** |
| 2304 | ** Calling this routine registers up to three versions of the user function |
| 2305 | ** "test_function" with database handle <db>. If the second argument is |
| 2306 | ** true, then a version of test_function is registered for UTF-8, if the |
| 2307 | ** third is true, a version is registered for UTF-16le, if the fourth is |
| 2308 | ** true, a UTF-16be version is available. Previous versions of |
| 2309 | ** test_function are deleted. |
| 2310 | ** |
| 2311 | ** The user function is implemented by calling the following TCL script: |
| 2312 | ** |
| 2313 | ** "test_function <enc> <arg>" |
| 2314 | ** |
| 2315 | ** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the |
| 2316 | ** single argument passed to the SQL function. The value returned by |
| 2317 | ** the TCL script is used as the return value of the SQL function. It |
| 2318 | ** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8 |
| 2319 | ** for a UTF-16LE test_function(), and UTF-16LE for an implementation that |
| 2320 | ** prefers UTF-16BE. |
| 2321 | */ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 2322 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2323 | static void test_function_utf8( |
| 2324 | sqlite3_context *pCtx, |
| 2325 | int nArg, |
| 2326 | sqlite3_value **argv |
| 2327 | ){ |
| 2328 | Tcl_Interp *interp; |
| 2329 | Tcl_Obj *pX; |
| 2330 | sqlite3_value *pVal; |
| 2331 | interp = (Tcl_Interp *)sqlite3_user_data(pCtx); |
| 2332 | pX = Tcl_NewStringObj("test_function", -1); |
| 2333 | Tcl_IncrRefCount(pX); |
| 2334 | Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1)); |
| 2335 | Tcl_ListObjAppendElement(interp, pX, |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 2336 | Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2337 | Tcl_EvalObjEx(interp, pX, 0); |
| 2338 | Tcl_DecrRefCount(pX); |
| 2339 | sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2340 | pVal = sqlite3ValueNew(0); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2341 | sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2342 | SQLITE_UTF8, SQLITE_STATIC); |
| 2343 | sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal), |
| 2344 | -1, SQLITE_TRANSIENT); |
| 2345 | sqlite3ValueFree(pVal); |
| 2346 | } |
| 2347 | static void test_function_utf16le( |
| 2348 | sqlite3_context *pCtx, |
| 2349 | int nArg, |
| 2350 | sqlite3_value **argv |
| 2351 | ){ |
| 2352 | Tcl_Interp *interp; |
| 2353 | Tcl_Obj *pX; |
| 2354 | sqlite3_value *pVal; |
| 2355 | interp = (Tcl_Interp *)sqlite3_user_data(pCtx); |
| 2356 | pX = Tcl_NewStringObj("test_function", -1); |
| 2357 | Tcl_IncrRefCount(pX); |
| 2358 | Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1)); |
| 2359 | Tcl_ListObjAppendElement(interp, pX, |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 2360 | Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2361 | Tcl_EvalObjEx(interp, pX, 0); |
| 2362 | Tcl_DecrRefCount(pX); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2363 | pVal = sqlite3ValueNew(0); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2364 | sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2365 | SQLITE_UTF8, SQLITE_STATIC); |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 2366 | sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT); |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2367 | sqlite3ValueFree(pVal); |
| 2368 | } |
| 2369 | static void test_function_utf16be( |
| 2370 | sqlite3_context *pCtx, |
| 2371 | int nArg, |
| 2372 | sqlite3_value **argv |
| 2373 | ){ |
| 2374 | Tcl_Interp *interp; |
| 2375 | Tcl_Obj *pX; |
| 2376 | sqlite3_value *pVal; |
| 2377 | interp = (Tcl_Interp *)sqlite3_user_data(pCtx); |
| 2378 | pX = Tcl_NewStringObj("test_function", -1); |
| 2379 | Tcl_IncrRefCount(pX); |
| 2380 | Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1)); |
| 2381 | Tcl_ListObjAppendElement(interp, pX, |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 2382 | Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2383 | Tcl_EvalObjEx(interp, pX, 0); |
| 2384 | Tcl_DecrRefCount(pX); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2385 | pVal = sqlite3ValueNew(0); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2386 | sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2387 | SQLITE_UTF8, SQLITE_STATIC); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 2388 | sqlite3_result_text16(pCtx, sqlite3_value_text16le(pVal), |
| 2389 | -1, SQLITE_TRANSIENT); |
| 2390 | sqlite3_result_text16be(pCtx, sqlite3_value_text16le(pVal), |
| 2391 | -1, SQLITE_TRANSIENT); |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2392 | sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal), |
| 2393 | -1, SQLITE_TRANSIENT); |
| 2394 | sqlite3ValueFree(pVal); |
| 2395 | } |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 2396 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2397 | static int test_function( |
| 2398 | void * clientData, |
| 2399 | Tcl_Interp *interp, |
| 2400 | int objc, |
| 2401 | Tcl_Obj *CONST objv[] |
| 2402 | ){ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 2403 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2404 | sqlite3 *db; |
| 2405 | int val; |
| 2406 | |
| 2407 | if( objc!=5 ) goto bad_args; |
| 2408 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 2409 | |
| 2410 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; |
| 2411 | if( val ){ |
| 2412 | sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8, |
| 2413 | interp, test_function_utf8, 0, 0); |
| 2414 | } |
| 2415 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR; |
| 2416 | if( val ){ |
| 2417 | sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE, |
| 2418 | interp, test_function_utf16le, 0, 0); |
| 2419 | } |
| 2420 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR; |
| 2421 | if( val ){ |
| 2422 | sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE, |
| 2423 | interp, test_function_utf16be, 0, 0); |
| 2424 | } |
| 2425 | |
| 2426 | return TCL_OK; |
| 2427 | bad_args: |
| 2428 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2429 | Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0); |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 2430 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2431 | return TCL_ERROR; |
| 2432 | } |
| 2433 | |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2434 | /* |
| 2435 | ** Usage: test_errstr <err code> |
| 2436 | ** |
| 2437 | ** Test that the english language string equivalents for sqlite error codes |
| 2438 | ** are sane. The parameter is an integer representing an sqlite error code. |
| 2439 | ** The result is a list of two elements, the string representation of the |
| 2440 | ** error code and the english language explanation. |
| 2441 | */ |
| 2442 | static int test_errstr( |
| 2443 | void * clientData, |
| 2444 | Tcl_Interp *interp, |
| 2445 | int objc, |
| 2446 | Tcl_Obj *CONST objv[] |
| 2447 | ){ |
| 2448 | char *zCode; |
| 2449 | int i; |
| 2450 | if( objc!=1 ){ |
| 2451 | Tcl_WrongNumArgs(interp, 1, objv, "<error code>"); |
| 2452 | } |
| 2453 | |
| 2454 | zCode = Tcl_GetString(objv[1]); |
| 2455 | for(i=0; i<200; i++){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2456 | if( 0==strcmp(t1ErrorName(i), zCode) ) break; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2457 | } |
| 2458 | Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0); |
| 2459 | return TCL_OK; |
| 2460 | } |
| 2461 | |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2462 | /* |
drh | 99ee360 | 2003-02-16 19:13:36 +0000 | [diff] [blame] | 2463 | ** Usage: breakpoint |
| 2464 | ** |
| 2465 | ** This routine exists for one purpose - to provide a place to put a |
| 2466 | ** breakpoint with GDB that can be triggered using TCL code. The use |
| 2467 | ** for this is when a particular test fails on (say) the 1485th iteration. |
| 2468 | ** In the TCL test script, we can add code like this: |
| 2469 | ** |
| 2470 | ** if {$i==1485} breakpoint |
| 2471 | ** |
| 2472 | ** Then run testfixture in the debugger and wait for the breakpoint to |
| 2473 | ** fire. Then additional breakpoints can be set to trace down the bug. |
| 2474 | */ |
| 2475 | static int test_breakpoint( |
| 2476 | void *NotUsed, |
| 2477 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 2478 | int argc, /* Number of arguments */ |
| 2479 | char **argv /* Text of each argument */ |
| 2480 | ){ |
| 2481 | return TCL_OK; /* Do nothing */ |
| 2482 | } |
| 2483 | |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 2484 | /* |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 2485 | ** Usage: sqlite3_bind_zeroblob STMT IDX N |
| 2486 | ** |
| 2487 | ** Test the sqlite3_bind_zeroblob interface. STMT is a prepared statement. |
| 2488 | ** IDX is the index of a wildcard in the prepared statement. This command |
| 2489 | ** binds a N-byte zero-filled BLOB to the wildcard. |
| 2490 | */ |
| 2491 | static int test_bind_zeroblob( |
| 2492 | void * clientData, |
| 2493 | Tcl_Interp *interp, |
| 2494 | int objc, |
| 2495 | Tcl_Obj *CONST objv[] |
| 2496 | ){ |
| 2497 | sqlite3_stmt *pStmt; |
| 2498 | int idx; |
| 2499 | int n; |
| 2500 | int rc; |
| 2501 | |
| 2502 | if( objc!=4 ){ |
danielk1977 | 28c6630 | 2007-09-01 11:04:26 +0000 | [diff] [blame] | 2503 | Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N"); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 2504 | return TCL_ERROR; |
| 2505 | } |
| 2506 | |
| 2507 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2508 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 2509 | if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR; |
| 2510 | |
| 2511 | rc = sqlite3_bind_zeroblob(pStmt, idx, n); |
| 2512 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
| 2513 | if( rc!=SQLITE_OK ){ |
| 2514 | return TCL_ERROR; |
| 2515 | } |
| 2516 | |
| 2517 | return TCL_OK; |
| 2518 | } |
| 2519 | |
| 2520 | /* |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 2521 | ** Usage: sqlite3_bind_int STMT N VALUE |
| 2522 | ** |
| 2523 | ** Test the sqlite3_bind_int interface. STMT is a prepared statement. |
| 2524 | ** N is the index of a wildcard in the prepared statement. This command |
| 2525 | ** binds a 32-bit integer VALUE to that wildcard. |
| 2526 | */ |
| 2527 | static int test_bind_int( |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2528 | void * clientData, |
| 2529 | Tcl_Interp *interp, |
| 2530 | int objc, |
| 2531 | Tcl_Obj *CONST objv[] |
| 2532 | ){ |
| 2533 | sqlite3_stmt *pStmt; |
| 2534 | int idx; |
| 2535 | int value; |
| 2536 | int rc; |
| 2537 | |
| 2538 | if( objc!=4 ){ |
| 2539 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 2540 | Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2541 | return TCL_ERROR; |
| 2542 | } |
| 2543 | |
| 2544 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2545 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 2546 | if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR; |
| 2547 | |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 2548 | rc = sqlite3_bind_int(pStmt, idx, value); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2549 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2550 | if( rc!=SQLITE_OK ){ |
| 2551 | return TCL_ERROR; |
| 2552 | } |
| 2553 | |
| 2554 | return TCL_OK; |
| 2555 | } |
| 2556 | |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 2557 | |
| 2558 | /* |
| 2559 | ** Usage: sqlite3_bind_int64 STMT N VALUE |
| 2560 | ** |
| 2561 | ** Test the sqlite3_bind_int64 interface. STMT is a prepared statement. |
| 2562 | ** N is the index of a wildcard in the prepared statement. This command |
| 2563 | ** binds a 64-bit integer VALUE to that wildcard. |
| 2564 | */ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2565 | static int test_bind_int64( |
| 2566 | void * clientData, |
| 2567 | Tcl_Interp *interp, |
| 2568 | int objc, |
| 2569 | Tcl_Obj *CONST objv[] |
| 2570 | ){ |
| 2571 | sqlite3_stmt *pStmt; |
| 2572 | int idx; |
| 2573 | i64 value; |
| 2574 | int rc; |
| 2575 | |
| 2576 | if( objc!=4 ){ |
| 2577 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 2578 | Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2579 | return TCL_ERROR; |
| 2580 | } |
| 2581 | |
| 2582 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2583 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 2584 | if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR; |
| 2585 | |
| 2586 | rc = sqlite3_bind_int64(pStmt, idx, value); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2587 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2588 | if( rc!=SQLITE_OK ){ |
| 2589 | return TCL_ERROR; |
| 2590 | } |
| 2591 | |
| 2592 | return TCL_OK; |
| 2593 | } |
| 2594 | |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 2595 | |
| 2596 | /* |
| 2597 | ** Usage: sqlite3_bind_double STMT N VALUE |
| 2598 | ** |
| 2599 | ** Test the sqlite3_bind_double interface. STMT is a prepared statement. |
| 2600 | ** N is the index of a wildcard in the prepared statement. This command |
| 2601 | ** binds a 64-bit integer VALUE to that wildcard. |
| 2602 | */ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2603 | static int test_bind_double( |
| 2604 | void * clientData, |
| 2605 | Tcl_Interp *interp, |
| 2606 | int objc, |
| 2607 | Tcl_Obj *CONST objv[] |
| 2608 | ){ |
| 2609 | sqlite3_stmt *pStmt; |
| 2610 | int idx; |
| 2611 | double value; |
| 2612 | int rc; |
| 2613 | |
| 2614 | if( objc!=4 ){ |
| 2615 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 2616 | Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2617 | return TCL_ERROR; |
| 2618 | } |
| 2619 | |
| 2620 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2621 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 2622 | if( Tcl_GetDoubleFromObj(interp, objv[3], &value) ) return TCL_ERROR; |
| 2623 | |
| 2624 | rc = sqlite3_bind_double(pStmt, idx, value); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2625 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2626 | if( rc!=SQLITE_OK ){ |
| 2627 | return TCL_ERROR; |
| 2628 | } |
| 2629 | |
| 2630 | return TCL_OK; |
| 2631 | } |
| 2632 | |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 2633 | /* |
| 2634 | ** Usage: sqlite3_bind_null STMT N |
| 2635 | ** |
| 2636 | ** Test the sqlite3_bind_null interface. STMT is a prepared statement. |
| 2637 | ** N is the index of a wildcard in the prepared statement. This command |
| 2638 | ** binds a NULL to the wildcard. |
| 2639 | */ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2640 | static int test_bind_null( |
| 2641 | void * clientData, |
| 2642 | Tcl_Interp *interp, |
| 2643 | int objc, |
| 2644 | Tcl_Obj *CONST objv[] |
| 2645 | ){ |
| 2646 | sqlite3_stmt *pStmt; |
| 2647 | int idx; |
| 2648 | int rc; |
| 2649 | |
| 2650 | if( objc!=3 ){ |
| 2651 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 2652 | Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2653 | return TCL_ERROR; |
| 2654 | } |
| 2655 | |
| 2656 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2657 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 2658 | |
| 2659 | rc = sqlite3_bind_null(pStmt, idx); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2660 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2661 | if( rc!=SQLITE_OK ){ |
| 2662 | return TCL_ERROR; |
| 2663 | } |
| 2664 | |
| 2665 | return TCL_OK; |
| 2666 | } |
| 2667 | |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 2668 | /* |
| 2669 | ** Usage: sqlite3_bind_text STMT N STRING BYTES |
| 2670 | ** |
| 2671 | ** Test the sqlite3_bind_text interface. STMT is a prepared statement. |
| 2672 | ** N is the index of a wildcard in the prepared statement. This command |
| 2673 | ** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes |
| 2674 | ** long. |
| 2675 | */ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2676 | static int test_bind_text( |
| 2677 | void * clientData, |
| 2678 | Tcl_Interp *interp, |
| 2679 | int objc, |
| 2680 | Tcl_Obj *CONST objv[] |
| 2681 | ){ |
| 2682 | sqlite3_stmt *pStmt; |
| 2683 | int idx; |
| 2684 | int bytes; |
| 2685 | char *value; |
| 2686 | int rc; |
| 2687 | |
| 2688 | if( objc!=5 ){ |
| 2689 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 2690 | Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2691 | return TCL_ERROR; |
| 2692 | } |
| 2693 | |
| 2694 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2695 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 2696 | value = Tcl_GetString(objv[3]); |
| 2697 | if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR; |
| 2698 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2699 | rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2700 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2701 | if( rc!=SQLITE_OK ){ |
drh | 473d179 | 2005-06-06 17:54:55 +0000 | [diff] [blame] | 2702 | Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2703 | return TCL_ERROR; |
| 2704 | } |
| 2705 | |
| 2706 | return TCL_OK; |
| 2707 | } |
| 2708 | |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 2709 | /* |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 2710 | ** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 2711 | ** |
| 2712 | ** Test the sqlite3_bind_text16 interface. STMT is a prepared statement. |
| 2713 | ** N is the index of a wildcard in the prepared statement. This command |
| 2714 | ** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes |
| 2715 | ** long. |
| 2716 | */ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2717 | static int test_bind_text16( |
| 2718 | void * clientData, |
| 2719 | Tcl_Interp *interp, |
| 2720 | int objc, |
| 2721 | Tcl_Obj *CONST objv[] |
| 2722 | ){ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 2723 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2724 | sqlite3_stmt *pStmt; |
| 2725 | int idx; |
| 2726 | int bytes; |
| 2727 | char *value; |
| 2728 | int rc; |
| 2729 | |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 2730 | void (*xDel)() = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT); |
| 2731 | Tcl_Obj *oStmt = objv[objc-4]; |
| 2732 | Tcl_Obj *oN = objv[objc-3]; |
| 2733 | Tcl_Obj *oString = objv[objc-2]; |
| 2734 | Tcl_Obj *oBytes = objv[objc-1]; |
| 2735 | |
| 2736 | if( objc!=5 && objc!=6){ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2737 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 2738 | Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2739 | return TCL_ERROR; |
| 2740 | } |
| 2741 | |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 2742 | if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR; |
| 2743 | if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR; |
| 2744 | value = (char*)Tcl_GetByteArrayFromObj(oString, 0); |
| 2745 | if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2746 | |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 2747 | rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2748 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2749 | if( rc!=SQLITE_OK ){ |
| 2750 | return TCL_ERROR; |
| 2751 | } |
| 2752 | |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 2753 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2754 | return TCL_OK; |
| 2755 | } |
| 2756 | |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 2757 | /* |
danielk1977 | 5b159dc | 2007-05-17 16:34:43 +0000 | [diff] [blame] | 2758 | ** Usage: sqlite3_bind_blob ?-static? STMT N DATA BYTES |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 2759 | ** |
| 2760 | ** Test the sqlite3_bind_blob interface. STMT is a prepared statement. |
| 2761 | ** N is the index of a wildcard in the prepared statement. This command |
| 2762 | ** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size. |
| 2763 | */ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2764 | static int test_bind_blob( |
| 2765 | void * clientData, |
| 2766 | Tcl_Interp *interp, |
| 2767 | int objc, |
| 2768 | Tcl_Obj *CONST objv[] |
| 2769 | ){ |
| 2770 | sqlite3_stmt *pStmt; |
| 2771 | int idx; |
| 2772 | int bytes; |
| 2773 | char *value; |
| 2774 | int rc; |
danielk1977 | 5b159dc | 2007-05-17 16:34:43 +0000 | [diff] [blame] | 2775 | sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2776 | |
danielk1977 | 5b159dc | 2007-05-17 16:34:43 +0000 | [diff] [blame] | 2777 | if( objc!=5 && objc!=6 ){ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2778 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 2779 | Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2780 | return TCL_ERROR; |
| 2781 | } |
| 2782 | |
danielk1977 | 5b159dc | 2007-05-17 16:34:43 +0000 | [diff] [blame] | 2783 | if( objc==6 ){ |
| 2784 | xDestructor = SQLITE_STATIC; |
| 2785 | objv++; |
| 2786 | } |
| 2787 | |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2788 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2789 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 2790 | value = Tcl_GetString(objv[3]); |
danielk1977 | 49e4643 | 2004-05-27 13:55:27 +0000 | [diff] [blame] | 2791 | if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2792 | |
danielk1977 | 5b159dc | 2007-05-17 16:34:43 +0000 | [diff] [blame] | 2793 | rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2794 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2795 | if( rc!=SQLITE_OK ){ |
| 2796 | return TCL_ERROR; |
| 2797 | } |
| 2798 | |
| 2799 | return TCL_OK; |
| 2800 | } |
| 2801 | |
drh | 99ee360 | 2003-02-16 19:13:36 +0000 | [diff] [blame] | 2802 | /* |
drh | 75f6a03 | 2004-07-15 14:15:00 +0000 | [diff] [blame] | 2803 | ** Usage: sqlite3_bind_parameter_count STMT |
| 2804 | ** |
| 2805 | ** Return the number of wildcards in the given statement. |
| 2806 | */ |
| 2807 | static int test_bind_parameter_count( |
| 2808 | void * clientData, |
| 2809 | Tcl_Interp *interp, |
| 2810 | int objc, |
| 2811 | Tcl_Obj *CONST objv[] |
| 2812 | ){ |
| 2813 | sqlite3_stmt *pStmt; |
| 2814 | |
| 2815 | if( objc!=2 ){ |
| 2816 | Tcl_WrongNumArgs(interp, 1, objv, "STMT"); |
| 2817 | return TCL_ERROR; |
| 2818 | } |
| 2819 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2820 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt))); |
| 2821 | return TCL_OK; |
| 2822 | } |
| 2823 | |
| 2824 | /* |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 2825 | ** Usage: sqlite3_bind_parameter_name STMT N |
| 2826 | ** |
| 2827 | ** Return the name of the Nth wildcard. The first wildcard is 1. |
| 2828 | ** An empty string is returned if N is out of range or if the wildcard |
| 2829 | ** is nameless. |
| 2830 | */ |
| 2831 | static int test_bind_parameter_name( |
| 2832 | void * clientData, |
| 2833 | Tcl_Interp *interp, |
| 2834 | int objc, |
| 2835 | Tcl_Obj *CONST objv[] |
| 2836 | ){ |
| 2837 | sqlite3_stmt *pStmt; |
| 2838 | int i; |
| 2839 | |
| 2840 | if( objc!=3 ){ |
| 2841 | Tcl_WrongNumArgs(interp, 1, objv, "STMT N"); |
| 2842 | return TCL_ERROR; |
| 2843 | } |
| 2844 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2845 | if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR; |
| 2846 | Tcl_SetObjResult(interp, |
| 2847 | Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1) |
| 2848 | ); |
| 2849 | return TCL_OK; |
| 2850 | } |
| 2851 | |
| 2852 | /* |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 2853 | ** Usage: sqlite3_bind_parameter_index STMT NAME |
| 2854 | ** |
| 2855 | ** Return the index of the wildcard called NAME. Return 0 if there is |
| 2856 | ** no such wildcard. |
| 2857 | */ |
| 2858 | static int test_bind_parameter_index( |
| 2859 | void * clientData, |
| 2860 | Tcl_Interp *interp, |
| 2861 | int objc, |
| 2862 | Tcl_Obj *CONST objv[] |
| 2863 | ){ |
| 2864 | sqlite3_stmt *pStmt; |
| 2865 | |
| 2866 | if( objc!=3 ){ |
| 2867 | Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME"); |
| 2868 | return TCL_ERROR; |
| 2869 | } |
| 2870 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2871 | Tcl_SetObjResult(interp, |
| 2872 | Tcl_NewIntObj( |
| 2873 | sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2])) |
| 2874 | ) |
| 2875 | ); |
| 2876 | return TCL_OK; |
| 2877 | } |
| 2878 | |
| 2879 | /* |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 2880 | ** Usage: sqlite3_clear_bindings STMT |
| 2881 | ** |
| 2882 | */ |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 2883 | static int test_clear_bindings( |
| 2884 | void * clientData, |
| 2885 | Tcl_Interp *interp, |
| 2886 | int objc, |
| 2887 | Tcl_Obj *CONST objv[] |
| 2888 | ){ |
| 2889 | sqlite3_stmt *pStmt; |
| 2890 | |
| 2891 | if( objc!=2 ){ |
| 2892 | Tcl_WrongNumArgs(interp, 1, objv, "STMT"); |
| 2893 | return TCL_ERROR; |
| 2894 | } |
| 2895 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2896 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt))); |
| 2897 | return TCL_OK; |
| 2898 | } |
drh | f9cb7f5 | 2006-06-27 20:06:44 +0000 | [diff] [blame] | 2899 | |
| 2900 | /* |
| 2901 | ** Usage: sqlite3_sleep MILLISECONDS |
| 2902 | */ |
| 2903 | static int test_sleep( |
| 2904 | void * clientData, |
| 2905 | Tcl_Interp *interp, |
| 2906 | int objc, |
| 2907 | Tcl_Obj *CONST objv[] |
| 2908 | ){ |
| 2909 | int ms; |
| 2910 | |
| 2911 | if( objc!=2 ){ |
| 2912 | Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS"); |
| 2913 | return TCL_ERROR; |
| 2914 | } |
| 2915 | if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){ |
| 2916 | return TCL_ERROR; |
| 2917 | } |
| 2918 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms))); |
| 2919 | return TCL_OK; |
| 2920 | } |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 2921 | |
| 2922 | /* |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 2923 | ** Usage: sqlite3_errcode DB |
| 2924 | ** |
| 2925 | ** Return the string representation of the most recent sqlite3_* API |
| 2926 | ** error code. e.g. "SQLITE_ERROR". |
| 2927 | */ |
| 2928 | static int test_errcode( |
| 2929 | void * clientData, |
| 2930 | Tcl_Interp *interp, |
| 2931 | int objc, |
| 2932 | Tcl_Obj *CONST objv[] |
| 2933 | ){ |
| 2934 | sqlite3 *db; |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 2935 | int rc; |
| 2936 | char zBuf[30]; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 2937 | |
| 2938 | if( objc!=2 ){ |
| 2939 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2940 | Tcl_GetString(objv[0]), " DB", 0); |
| 2941 | return TCL_ERROR; |
| 2942 | } |
| 2943 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 2944 | rc = sqlite3_errcode(db); |
| 2945 | if( (rc&0xff)==rc ){ |
| 2946 | zBuf[0] = 0; |
| 2947 | }else{ |
| 2948 | sprintf(zBuf,"+%d", rc>>8); |
| 2949 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2950 | Tcl_AppendResult(interp, (char *)t1ErrorName(rc), zBuf, 0); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 2951 | return TCL_OK; |
| 2952 | } |
| 2953 | |
| 2954 | /* |
| 2955 | ** Usage: test_errmsg DB |
| 2956 | ** |
| 2957 | ** Returns the UTF-8 representation of the error message string for the |
| 2958 | ** most recent sqlite3_* API call. |
| 2959 | */ |
| 2960 | static int test_errmsg( |
| 2961 | void * clientData, |
| 2962 | Tcl_Interp *interp, |
| 2963 | int objc, |
| 2964 | Tcl_Obj *CONST objv[] |
| 2965 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 2966 | sqlite3 *db; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 2967 | const char *zErr; |
| 2968 | |
| 2969 | if( objc!=2 ){ |
| 2970 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2971 | Tcl_GetString(objv[0]), " DB", 0); |
| 2972 | return TCL_ERROR; |
| 2973 | } |
| 2974 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 2975 | |
| 2976 | zErr = sqlite3_errmsg(db); |
| 2977 | Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1)); |
| 2978 | return TCL_OK; |
| 2979 | } |
| 2980 | |
| 2981 | /* |
| 2982 | ** Usage: test_errmsg16 DB |
| 2983 | ** |
| 2984 | ** Returns the UTF-16 representation of the error message string for the |
| 2985 | ** most recent sqlite3_* API call. This is a byte array object at the TCL |
| 2986 | ** level, and it includes the 0x00 0x00 terminator bytes at the end of the |
| 2987 | ** UTF-16 string. |
| 2988 | */ |
| 2989 | static int test_errmsg16( |
| 2990 | void * clientData, |
| 2991 | Tcl_Interp *interp, |
| 2992 | int objc, |
| 2993 | Tcl_Obj *CONST objv[] |
| 2994 | ){ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 2995 | #ifndef SQLITE_OMIT_UTF16 |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 2996 | sqlite3 *db; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 2997 | const void *zErr; |
danielk1977 | 950f054 | 2006-01-18 05:51:57 +0000 | [diff] [blame] | 2998 | int bytes = 0; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 2999 | |
| 3000 | if( objc!=2 ){ |
| 3001 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3002 | Tcl_GetString(objv[0]), " DB", 0); |
| 3003 | return TCL_ERROR; |
| 3004 | } |
| 3005 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3006 | |
| 3007 | zErr = sqlite3_errmsg16(db); |
danielk1977 | 950f054 | 2006-01-18 05:51:57 +0000 | [diff] [blame] | 3008 | if( zErr ){ |
drh | ee85813 | 2007-05-08 20:37:38 +0000 | [diff] [blame] | 3009 | bytes = sqlite3Utf16ByteLen(zErr, -1); |
danielk1977 | 950f054 | 2006-01-18 05:51:57 +0000 | [diff] [blame] | 3010 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3011 | Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes)); |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3012 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3013 | return TCL_OK; |
| 3014 | } |
| 3015 | |
| 3016 | /* |
| 3017 | ** Usage: sqlite3_prepare DB sql bytes tailvar |
| 3018 | ** |
| 3019 | ** Compile up to <bytes> bytes of the supplied SQL string <sql> using |
| 3020 | ** database handle <DB>. The parameter <tailval> is the name of a global |
| 3021 | ** variable that is set to the unused portion of <sql> (if any). A |
| 3022 | ** STMT handle is returned. |
| 3023 | */ |
| 3024 | static int test_prepare( |
| 3025 | void * clientData, |
| 3026 | Tcl_Interp *interp, |
| 3027 | int objc, |
| 3028 | Tcl_Obj *CONST objv[] |
| 3029 | ){ |
| 3030 | sqlite3 *db; |
| 3031 | const char *zSql; |
| 3032 | int bytes; |
| 3033 | const char *zTail = 0; |
| 3034 | sqlite3_stmt *pStmt = 0; |
| 3035 | char zBuf[50]; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3036 | int rc; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3037 | |
| 3038 | if( objc!=5 ){ |
| 3039 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3040 | Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); |
| 3041 | return TCL_ERROR; |
| 3042 | } |
| 3043 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3044 | zSql = Tcl_GetString(objv[2]); |
| 3045 | if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; |
| 3046 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3047 | rc = sqlite3_prepare(db, zSql, bytes, &pStmt, &zTail); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3048 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3049 | if( zTail ){ |
| 3050 | if( bytes>=0 ){ |
| 3051 | bytes = bytes - (zTail-zSql); |
| 3052 | } |
| 3053 | Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0); |
| 3054 | } |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3055 | if( rc!=SQLITE_OK ){ |
| 3056 | assert( pStmt==0 ); |
| 3057 | sprintf(zBuf, "(%d) ", rc); |
| 3058 | Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); |
| 3059 | return TCL_ERROR; |
| 3060 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3061 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3062 | if( pStmt ){ |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 3063 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3064 | Tcl_AppendResult(interp, zBuf, 0); |
| 3065 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3066 | return TCL_OK; |
| 3067 | } |
| 3068 | |
| 3069 | /* |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3070 | ** Usage: sqlite3_prepare_v2 DB sql bytes tailvar |
| 3071 | ** |
| 3072 | ** Compile up to <bytes> bytes of the supplied SQL string <sql> using |
| 3073 | ** database handle <DB>. The parameter <tailval> is the name of a global |
| 3074 | ** variable that is set to the unused portion of <sql> (if any). A |
| 3075 | ** STMT handle is returned. |
| 3076 | */ |
| 3077 | static int test_prepare_v2( |
| 3078 | void * clientData, |
| 3079 | Tcl_Interp *interp, |
| 3080 | int objc, |
| 3081 | Tcl_Obj *CONST objv[] |
| 3082 | ){ |
| 3083 | sqlite3 *db; |
| 3084 | const char *zSql; |
| 3085 | int bytes; |
| 3086 | const char *zTail = 0; |
| 3087 | sqlite3_stmt *pStmt = 0; |
| 3088 | char zBuf[50]; |
| 3089 | int rc; |
| 3090 | |
| 3091 | if( objc!=5 ){ |
| 3092 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3093 | Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); |
| 3094 | return TCL_ERROR; |
| 3095 | } |
| 3096 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3097 | zSql = Tcl_GetString(objv[2]); |
| 3098 | if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; |
| 3099 | |
| 3100 | rc = sqlite3_prepare_v2(db, zSql, bytes, &pStmt, &zTail); |
danielk1977 | 7e29e95 | 2007-04-19 11:09:01 +0000 | [diff] [blame] | 3101 | assert(rc==SQLITE_OK || pStmt==0); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3102 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 3103 | if( zTail ){ |
| 3104 | if( bytes>=0 ){ |
| 3105 | bytes = bytes - (zTail-zSql); |
| 3106 | } |
| 3107 | Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0); |
| 3108 | } |
| 3109 | if( rc!=SQLITE_OK ){ |
| 3110 | assert( pStmt==0 ); |
| 3111 | sprintf(zBuf, "(%d) ", rc); |
| 3112 | Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); |
| 3113 | return TCL_ERROR; |
| 3114 | } |
| 3115 | |
| 3116 | if( pStmt ){ |
| 3117 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
| 3118 | Tcl_AppendResult(interp, zBuf, 0); |
| 3119 | } |
| 3120 | return TCL_OK; |
| 3121 | } |
| 3122 | |
| 3123 | /* |
| 3124 | ** Usage: sqlite3_prepare16 DB sql bytes tailvar |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3125 | ** |
| 3126 | ** Compile up to <bytes> bytes of the supplied SQL string <sql> using |
| 3127 | ** database handle <DB>. The parameter <tailval> is the name of a global |
| 3128 | ** variable that is set to the unused portion of <sql> (if any). A |
| 3129 | ** STMT handle is returned. |
| 3130 | */ |
| 3131 | static int test_prepare16( |
| 3132 | void * clientData, |
| 3133 | Tcl_Interp *interp, |
| 3134 | int objc, |
| 3135 | Tcl_Obj *CONST objv[] |
| 3136 | ){ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3137 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3138 | sqlite3 *db; |
| 3139 | const void *zSql; |
| 3140 | const void *zTail = 0; |
| 3141 | Tcl_Obj *pTail = 0; |
| 3142 | sqlite3_stmt *pStmt = 0; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3143 | char zBuf[50]; |
| 3144 | int rc; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3145 | int bytes; /* The integer specified as arg 3 */ |
| 3146 | int objlen; /* The byte-array length of arg 2 */ |
| 3147 | |
| 3148 | if( objc!=5 ){ |
| 3149 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3150 | Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); |
| 3151 | return TCL_ERROR; |
| 3152 | } |
| 3153 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3154 | zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen); |
| 3155 | if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; |
| 3156 | |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3157 | rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, &zTail); |
| 3158 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 3159 | if( rc ){ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3160 | return TCL_ERROR; |
| 3161 | } |
| 3162 | |
| 3163 | if( zTail ){ |
| 3164 | objlen = objlen - ((u8 *)zTail-(u8 *)zSql); |
| 3165 | }else{ |
| 3166 | objlen = 0; |
| 3167 | } |
| 3168 | pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen); |
| 3169 | Tcl_IncrRefCount(pTail); |
| 3170 | Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3171 | Tcl_DecrRefCount(pTail); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3172 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3173 | if( pStmt ){ |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 3174 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3175 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3176 | Tcl_AppendResult(interp, zBuf, 0); |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3177 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3178 | return TCL_OK; |
| 3179 | } |
| 3180 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3181 | /* |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3182 | ** Usage: sqlite3_prepare16_v2 DB sql bytes tailvar |
| 3183 | ** |
| 3184 | ** Compile up to <bytes> bytes of the supplied SQL string <sql> using |
| 3185 | ** database handle <DB>. The parameter <tailval> is the name of a global |
| 3186 | ** variable that is set to the unused portion of <sql> (if any). A |
| 3187 | ** STMT handle is returned. |
| 3188 | */ |
| 3189 | static int test_prepare16_v2( |
| 3190 | void * clientData, |
| 3191 | Tcl_Interp *interp, |
| 3192 | int objc, |
| 3193 | Tcl_Obj *CONST objv[] |
| 3194 | ){ |
| 3195 | #ifndef SQLITE_OMIT_UTF16 |
| 3196 | sqlite3 *db; |
| 3197 | const void *zSql; |
| 3198 | const void *zTail = 0; |
| 3199 | Tcl_Obj *pTail = 0; |
| 3200 | sqlite3_stmt *pStmt = 0; |
| 3201 | char zBuf[50]; |
| 3202 | int rc; |
| 3203 | int bytes; /* The integer specified as arg 3 */ |
| 3204 | int objlen; /* The byte-array length of arg 2 */ |
| 3205 | |
| 3206 | if( objc!=5 ){ |
| 3207 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3208 | Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); |
| 3209 | return TCL_ERROR; |
| 3210 | } |
| 3211 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3212 | zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen); |
| 3213 | if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; |
| 3214 | |
| 3215 | rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, &zTail); |
| 3216 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 3217 | if( rc ){ |
| 3218 | return TCL_ERROR; |
| 3219 | } |
| 3220 | |
| 3221 | if( zTail ){ |
| 3222 | objlen = objlen - ((u8 *)zTail-(u8 *)zSql); |
| 3223 | }else{ |
| 3224 | objlen = 0; |
| 3225 | } |
| 3226 | pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen); |
| 3227 | Tcl_IncrRefCount(pTail); |
| 3228 | Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0); |
| 3229 | Tcl_DecrRefCount(pTail); |
| 3230 | |
| 3231 | if( pStmt ){ |
| 3232 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
| 3233 | } |
| 3234 | Tcl_AppendResult(interp, zBuf, 0); |
| 3235 | #endif /* SQLITE_OMIT_UTF16 */ |
| 3236 | return TCL_OK; |
| 3237 | } |
| 3238 | |
| 3239 | /* |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3240 | ** Usage: sqlite3_open filename ?options-list? |
| 3241 | */ |
| 3242 | static int test_open( |
| 3243 | void * clientData, |
| 3244 | Tcl_Interp *interp, |
| 3245 | int objc, |
| 3246 | Tcl_Obj *CONST objv[] |
| 3247 | ){ |
| 3248 | const char *zFilename; |
| 3249 | sqlite3 *db; |
| 3250 | int rc; |
| 3251 | char zBuf[100]; |
| 3252 | |
| 3253 | if( objc!=3 && objc!=2 ){ |
| 3254 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3255 | Tcl_GetString(objv[0]), " filename options-list", 0); |
| 3256 | return TCL_ERROR; |
| 3257 | } |
| 3258 | |
| 3259 | zFilename = Tcl_GetString(objv[1]); |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 3260 | rc = sqlite3_open(zFilename, &db); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3261 | |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 3262 | if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3263 | Tcl_AppendResult(interp, zBuf, 0); |
| 3264 | return TCL_OK; |
| 3265 | } |
| 3266 | |
| 3267 | /* |
| 3268 | ** Usage: sqlite3_open16 filename options |
| 3269 | */ |
| 3270 | static int test_open16( |
| 3271 | void * clientData, |
| 3272 | Tcl_Interp *interp, |
| 3273 | int objc, |
| 3274 | Tcl_Obj *CONST objv[] |
| 3275 | ){ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3276 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3277 | const void *zFilename; |
| 3278 | sqlite3 *db; |
| 3279 | int rc; |
| 3280 | char zBuf[100]; |
| 3281 | |
| 3282 | if( objc!=3 ){ |
| 3283 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3284 | Tcl_GetString(objv[0]), " filename options-list", 0); |
| 3285 | return TCL_ERROR; |
| 3286 | } |
| 3287 | |
| 3288 | zFilename = Tcl_GetByteArrayFromObj(objv[1], 0); |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 3289 | rc = sqlite3_open16(zFilename, &db); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3290 | |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 3291 | if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3292 | Tcl_AppendResult(interp, zBuf, 0); |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3293 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3294 | return TCL_OK; |
| 3295 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 3296 | |
| 3297 | /* |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 3298 | ** Usage: sqlite3_complete16 <UTF-16 string> |
| 3299 | ** |
| 3300 | ** Return 1 if the supplied argument is a complete SQL statement, or zero |
| 3301 | ** otherwise. |
| 3302 | */ |
| 3303 | static int test_complete16( |
| 3304 | void * clientData, |
| 3305 | Tcl_Interp *interp, |
| 3306 | int objc, |
| 3307 | Tcl_Obj *CONST objv[] |
| 3308 | ){ |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 3309 | #if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16) |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 3310 | char *zBuf; |
| 3311 | |
| 3312 | if( objc!=2 ){ |
| 3313 | Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>"); |
| 3314 | return TCL_ERROR; |
| 3315 | } |
| 3316 | |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 3317 | zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 3318 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf))); |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 3319 | #endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 3320 | return TCL_OK; |
| 3321 | } |
| 3322 | |
| 3323 | /* |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3324 | ** Usage: sqlite3_step STMT |
| 3325 | ** |
| 3326 | ** Advance the statement to the next row. |
| 3327 | */ |
danielk1977 | 17240fd | 2004-05-26 00:07:25 +0000 | [diff] [blame] | 3328 | static int test_step( |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3329 | void * clientData, |
| 3330 | Tcl_Interp *interp, |
| 3331 | int objc, |
| 3332 | Tcl_Obj *CONST objv[] |
| 3333 | ){ |
| 3334 | sqlite3_stmt *pStmt; |
| 3335 | int rc; |
| 3336 | |
danielk1977 | e1cd987 | 2004-05-22 10:33:04 +0000 | [diff] [blame] | 3337 | if( objc!=2 ){ |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3338 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3339 | Tcl_GetString(objv[0]), " STMT", 0); |
| 3340 | return TCL_ERROR; |
| 3341 | } |
| 3342 | |
| 3343 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
danielk1977 | 17240fd | 2004-05-26 00:07:25 +0000 | [diff] [blame] | 3344 | rc = sqlite3_step(pStmt); |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3345 | |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 3346 | /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3347 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); |
danielk1977 | e1cd987 | 2004-05-22 10:33:04 +0000 | [diff] [blame] | 3348 | return TCL_OK; |
| 3349 | } |
| 3350 | |
| 3351 | /* |
danielk1977 | 17240fd | 2004-05-26 00:07:25 +0000 | [diff] [blame] | 3352 | ** Usage: sqlite3_column_count STMT |
| 3353 | ** |
| 3354 | ** Return the number of columns returned by the sql statement STMT. |
| 3355 | */ |
| 3356 | static int test_column_count( |
| 3357 | void * clientData, |
| 3358 | Tcl_Interp *interp, |
| 3359 | int objc, |
| 3360 | Tcl_Obj *CONST objv[] |
| 3361 | ){ |
| 3362 | sqlite3_stmt *pStmt; |
| 3363 | |
| 3364 | if( objc!=2 ){ |
| 3365 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3366 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 3367 | return TCL_ERROR; |
| 3368 | } |
| 3369 | |
| 3370 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3371 | |
| 3372 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt))); |
| 3373 | return TCL_OK; |
| 3374 | } |
| 3375 | |
| 3376 | /* |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 3377 | ** Usage: sqlite3_column_type STMT column |
| 3378 | ** |
| 3379 | ** Return the type of the data in column 'column' of the current row. |
| 3380 | */ |
| 3381 | static int test_column_type( |
| 3382 | void * clientData, |
| 3383 | Tcl_Interp *interp, |
| 3384 | int objc, |
| 3385 | Tcl_Obj *CONST objv[] |
| 3386 | ){ |
| 3387 | sqlite3_stmt *pStmt; |
| 3388 | int col; |
| 3389 | int tp; |
| 3390 | |
| 3391 | if( objc!=3 ){ |
| 3392 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3393 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 3394 | return TCL_ERROR; |
| 3395 | } |
| 3396 | |
| 3397 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3398 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 3399 | |
| 3400 | tp = sqlite3_column_type(pStmt, col); |
| 3401 | switch( tp ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 3402 | case SQLITE_INTEGER: |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 3403 | Tcl_SetResult(interp, "INTEGER", TCL_STATIC); |
| 3404 | break; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 3405 | case SQLITE_NULL: |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 3406 | Tcl_SetResult(interp, "NULL", TCL_STATIC); |
| 3407 | break; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 3408 | case SQLITE_FLOAT: |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 3409 | Tcl_SetResult(interp, "FLOAT", TCL_STATIC); |
| 3410 | break; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 3411 | case SQLITE_TEXT: |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 3412 | Tcl_SetResult(interp, "TEXT", TCL_STATIC); |
| 3413 | break; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 3414 | case SQLITE_BLOB: |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 3415 | Tcl_SetResult(interp, "BLOB", TCL_STATIC); |
| 3416 | break; |
| 3417 | default: |
| 3418 | assert(0); |
| 3419 | } |
| 3420 | |
| 3421 | return TCL_OK; |
| 3422 | } |
| 3423 | |
| 3424 | /* |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 3425 | ** Usage: sqlite3_column_int64 STMT column |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 3426 | ** |
| 3427 | ** Return the data in column 'column' of the current row cast as an |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 3428 | ** wide (64-bit) integer. |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 3429 | */ |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 3430 | static int test_column_int64( |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 3431 | void * clientData, |
| 3432 | Tcl_Interp *interp, |
| 3433 | int objc, |
| 3434 | Tcl_Obj *CONST objv[] |
| 3435 | ){ |
| 3436 | sqlite3_stmt *pStmt; |
| 3437 | int col; |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 3438 | i64 iVal; |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 3439 | |
| 3440 | if( objc!=3 ){ |
| 3441 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3442 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 3443 | return TCL_ERROR; |
| 3444 | } |
| 3445 | |
| 3446 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3447 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 3448 | |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 3449 | iVal = sqlite3_column_int64(pStmt, col); |
| 3450 | Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal)); |
| 3451 | return TCL_OK; |
| 3452 | } |
| 3453 | |
| 3454 | /* |
danielk1977 | ea61b2c | 2004-05-27 01:49:51 +0000 | [diff] [blame] | 3455 | ** Usage: sqlite3_column_blob STMT column |
| 3456 | */ |
| 3457 | static int test_column_blob( |
| 3458 | void * clientData, |
| 3459 | Tcl_Interp *interp, |
| 3460 | int objc, |
| 3461 | Tcl_Obj *CONST objv[] |
| 3462 | ){ |
| 3463 | sqlite3_stmt *pStmt; |
| 3464 | int col; |
| 3465 | |
| 3466 | int len; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 3467 | const void *pBlob; |
danielk1977 | ea61b2c | 2004-05-27 01:49:51 +0000 | [diff] [blame] | 3468 | |
| 3469 | if( objc!=3 ){ |
| 3470 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3471 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 3472 | return TCL_ERROR; |
| 3473 | } |
| 3474 | |
| 3475 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3476 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 3477 | |
danielk1977 | ea61b2c | 2004-05-27 01:49:51 +0000 | [diff] [blame] | 3478 | len = sqlite3_column_bytes(pStmt, col); |
drh | 9310ef2 | 2007-04-27 17:16:20 +0000 | [diff] [blame] | 3479 | pBlob = sqlite3_column_blob(pStmt, col); |
danielk1977 | ea61b2c | 2004-05-27 01:49:51 +0000 | [diff] [blame] | 3480 | Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len)); |
| 3481 | return TCL_OK; |
| 3482 | } |
| 3483 | |
| 3484 | /* |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 3485 | ** Usage: sqlite3_column_double STMT column |
| 3486 | ** |
| 3487 | ** Return the data in column 'column' of the current row cast as a double. |
| 3488 | */ |
| 3489 | static int test_column_double( |
| 3490 | void * clientData, |
| 3491 | Tcl_Interp *interp, |
| 3492 | int objc, |
| 3493 | Tcl_Obj *CONST objv[] |
| 3494 | ){ |
| 3495 | sqlite3_stmt *pStmt; |
| 3496 | int col; |
| 3497 | double rVal; |
| 3498 | |
| 3499 | if( objc!=3 ){ |
| 3500 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3501 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 3502 | return TCL_ERROR; |
| 3503 | } |
| 3504 | |
| 3505 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3506 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 3507 | |
| 3508 | rVal = sqlite3_column_double(pStmt, col); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 3509 | Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal)); |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 3510 | return TCL_OK; |
| 3511 | } |
| 3512 | |
| 3513 | /* |
danielk1977 | 17240fd | 2004-05-26 00:07:25 +0000 | [diff] [blame] | 3514 | ** Usage: sqlite3_data_count STMT |
| 3515 | ** |
| 3516 | ** Return the number of columns returned by the sql statement STMT. |
| 3517 | */ |
| 3518 | static int test_data_count( |
| 3519 | void * clientData, |
| 3520 | Tcl_Interp *interp, |
| 3521 | int objc, |
| 3522 | Tcl_Obj *CONST objv[] |
| 3523 | ){ |
| 3524 | sqlite3_stmt *pStmt; |
| 3525 | |
| 3526 | if( objc!=2 ){ |
| 3527 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3528 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 3529 | return TCL_ERROR; |
| 3530 | } |
| 3531 | |
| 3532 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3533 | |
| 3534 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt))); |
| 3535 | return TCL_OK; |
| 3536 | } |
| 3537 | |
| 3538 | /* |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 3539 | ** Usage: sqlite3_column_text STMT column |
| 3540 | ** |
| 3541 | ** Usage: sqlite3_column_decltype STMT column |
| 3542 | ** |
| 3543 | ** Usage: sqlite3_column_name STMT column |
| 3544 | */ |
| 3545 | static int test_stmt_utf8( |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3546 | void * clientData, /* Pointer to SQLite API function to be invoke */ |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 3547 | Tcl_Interp *interp, |
| 3548 | int objc, |
| 3549 | Tcl_Obj *CONST objv[] |
| 3550 | ){ |
| 3551 | sqlite3_stmt *pStmt; |
| 3552 | int col; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 3553 | const char *(*xFunc)(sqlite3_stmt*, int) = clientData; |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 3554 | const char *zRet; |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 3555 | |
| 3556 | if( objc!=3 ){ |
| 3557 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3558 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 3559 | return TCL_ERROR; |
| 3560 | } |
| 3561 | |
| 3562 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3563 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 3564 | zRet = xFunc(pStmt, col); |
| 3565 | if( zRet ){ |
| 3566 | Tcl_SetResult(interp, (char *)zRet, 0); |
| 3567 | } |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 3568 | return TCL_OK; |
| 3569 | } |
| 3570 | |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 3571 | static int test_global_recover( |
| 3572 | void * clientData, |
| 3573 | Tcl_Interp *interp, |
| 3574 | int objc, |
| 3575 | Tcl_Obj *CONST objv[] |
| 3576 | ){ |
| 3577 | #ifndef SQLITE_OMIT_GLOBALRECOVER |
| 3578 | int rc; |
| 3579 | if( objc!=1 ){ |
| 3580 | Tcl_WrongNumArgs(interp, 1, objv, ""); |
| 3581 | return TCL_ERROR; |
| 3582 | } |
| 3583 | rc = sqlite3_global_recover(); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3584 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 3585 | #endif |
| 3586 | return TCL_OK; |
| 3587 | } |
| 3588 | |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 3589 | /* |
| 3590 | ** Usage: sqlite3_column_text STMT column |
| 3591 | ** |
| 3592 | ** Usage: sqlite3_column_decltype STMT column |
| 3593 | ** |
| 3594 | ** Usage: sqlite3_column_name STMT column |
| 3595 | */ |
| 3596 | static int test_stmt_utf16( |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3597 | void * clientData, /* Pointer to SQLite API function to be invoked */ |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 3598 | Tcl_Interp *interp, |
| 3599 | int objc, |
| 3600 | Tcl_Obj *CONST objv[] |
| 3601 | ){ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3602 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 3603 | sqlite3_stmt *pStmt; |
| 3604 | int col; |
| 3605 | Tcl_Obj *pRet; |
| 3606 | const void *zName16; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 3607 | const void *(*xFunc)(sqlite3_stmt*, int) = clientData; |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 3608 | |
| 3609 | if( objc!=3 ){ |
| 3610 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3611 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 3612 | return TCL_ERROR; |
| 3613 | } |
| 3614 | |
| 3615 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3616 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 3617 | |
| 3618 | zName16 = xFunc(pStmt, col); |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 3619 | if( zName16 ){ |
drh | ee85813 | 2007-05-08 20:37:38 +0000 | [diff] [blame] | 3620 | pRet = Tcl_NewByteArrayObj(zName16, sqlite3Utf16ByteLen(zName16, -1)+2); |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 3621 | Tcl_SetObjResult(interp, pRet); |
| 3622 | } |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3623 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 3624 | |
| 3625 | return TCL_OK; |
| 3626 | } |
| 3627 | |
| 3628 | /* |
| 3629 | ** Usage: sqlite3_column_int STMT column |
| 3630 | ** |
| 3631 | ** Usage: sqlite3_column_bytes STMT column |
| 3632 | ** |
| 3633 | ** Usage: sqlite3_column_bytes16 STMT column |
| 3634 | ** |
| 3635 | */ |
| 3636 | static int test_stmt_int( |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3637 | void * clientData, /* Pointer to SQLite API function to be invoked */ |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 3638 | Tcl_Interp *interp, |
| 3639 | int objc, |
| 3640 | Tcl_Obj *CONST objv[] |
| 3641 | ){ |
| 3642 | sqlite3_stmt *pStmt; |
| 3643 | int col; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 3644 | int (*xFunc)(sqlite3_stmt*, int) = clientData; |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 3645 | |
| 3646 | if( objc!=3 ){ |
| 3647 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3648 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 3649 | return TCL_ERROR; |
| 3650 | } |
| 3651 | |
| 3652 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3653 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 3654 | |
| 3655 | Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col))); |
| 3656 | return TCL_OK; |
| 3657 | } |
| 3658 | |
danielk1977 | 44ee5bf | 2005-05-27 09:41:12 +0000 | [diff] [blame] | 3659 | #ifndef SQLITE_OMIT_DISKIO |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3660 | #if 0 |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 3661 | /* |
| 3662 | ** Usage: sqlite3OsOpenReadWrite <filename> |
| 3663 | */ |
| 3664 | static int test_sqlite3OsOpenReadWrite( |
| 3665 | void * clientData, |
| 3666 | Tcl_Interp *interp, |
| 3667 | int objc, |
| 3668 | Tcl_Obj *CONST objv[] |
| 3669 | ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 3670 | sqlite3_file *pFile; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 3671 | int rc; |
| 3672 | int dummy; |
| 3673 | char zBuf[100]; |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 3674 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 3675 | if( objc!=2 ){ |
| 3676 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3677 | Tcl_GetString(objv[0]), " filename", 0); |
| 3678 | return TCL_ERROR; |
| 3679 | } |
| 3680 | |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 3681 | rc = sqlite3OsOpenReadWrite(Tcl_GetString(objv[1]), &pFile, &dummy); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 3682 | if( rc!=SQLITE_OK ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3683 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 3684 | return TCL_ERROR; |
| 3685 | } |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 3686 | sqlite3TestMakePointerStr(interp, zBuf, pFile); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 3687 | Tcl_SetResult(interp, zBuf, 0); |
| 3688 | return TCL_ERROR; |
| 3689 | } |
| 3690 | |
| 3691 | /* |
| 3692 | ** Usage: sqlite3OsClose <file handle> |
| 3693 | */ |
| 3694 | static int test_sqlite3OsClose( |
| 3695 | void * clientData, |
| 3696 | Tcl_Interp *interp, |
| 3697 | int objc, |
| 3698 | Tcl_Obj *CONST objv[] |
| 3699 | ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 3700 | sqlite3_file *pFile; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 3701 | int rc; |
| 3702 | |
| 3703 | if( objc!=2 ){ |
| 3704 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3705 | Tcl_GetString(objv[0]), " filehandle", 0); |
| 3706 | return TCL_ERROR; |
| 3707 | } |
| 3708 | |
| 3709 | if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){ |
| 3710 | return TCL_ERROR; |
| 3711 | } |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 3712 | rc = sqlite3OsClose(&pFile); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 3713 | if( rc!=SQLITE_OK ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3714 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 3715 | return TCL_ERROR; |
| 3716 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 3717 | return TCL_OK; |
| 3718 | } |
| 3719 | |
| 3720 | /* |
| 3721 | ** Usage: sqlite3OsLock <file handle> <locktype> |
| 3722 | */ |
| 3723 | static int test_sqlite3OsLock( |
| 3724 | void * clientData, |
| 3725 | Tcl_Interp *interp, |
| 3726 | int objc, |
| 3727 | Tcl_Obj *CONST objv[] |
| 3728 | ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 3729 | sqlite3_file * pFile; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 3730 | int rc; |
| 3731 | |
| 3732 | if( objc!=3 ){ |
| 3733 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3734 | Tcl_GetString(objv[0]), |
| 3735 | " filehandle (SHARED|RESERVED|PENDING|EXCLUSIVE)", 0); |
| 3736 | return TCL_ERROR; |
| 3737 | } |
| 3738 | |
| 3739 | if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){ |
| 3740 | return TCL_ERROR; |
| 3741 | } |
| 3742 | |
| 3743 | if( 0==strcmp("SHARED", Tcl_GetString(objv[2])) ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 3744 | rc = sqlite3OsLock(pFile, SHARED_LOCK); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 3745 | } |
| 3746 | else if( 0==strcmp("RESERVED", Tcl_GetString(objv[2])) ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 3747 | rc = sqlite3OsLock(pFile, RESERVED_LOCK); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 3748 | } |
| 3749 | else if( 0==strcmp("PENDING", Tcl_GetString(objv[2])) ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 3750 | rc = sqlite3OsLock(pFile, PENDING_LOCK); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 3751 | } |
| 3752 | else if( 0==strcmp("EXCLUSIVE", Tcl_GetString(objv[2])) ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 3753 | rc = sqlite3OsLock(pFile, EXCLUSIVE_LOCK); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 3754 | }else{ |
| 3755 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3756 | Tcl_GetString(objv[0]), |
| 3757 | " filehandle (SHARED|RESERVED|PENDING|EXCLUSIVE)", 0); |
| 3758 | return TCL_ERROR; |
| 3759 | } |
| 3760 | |
| 3761 | if( rc!=SQLITE_OK ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3762 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 3763 | return TCL_ERROR; |
| 3764 | } |
| 3765 | return TCL_OK; |
| 3766 | } |
| 3767 | |
| 3768 | /* |
| 3769 | ** Usage: sqlite3OsUnlock <file handle> |
| 3770 | */ |
| 3771 | static int test_sqlite3OsUnlock( |
| 3772 | void * clientData, |
| 3773 | Tcl_Interp *interp, |
| 3774 | int objc, |
| 3775 | Tcl_Obj *CONST objv[] |
| 3776 | ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 3777 | sqlite3_file * pFile; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 3778 | int rc; |
| 3779 | |
| 3780 | if( objc!=2 ){ |
| 3781 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3782 | Tcl_GetString(objv[0]), " filehandle", 0); |
| 3783 | return TCL_ERROR; |
| 3784 | } |
| 3785 | |
| 3786 | if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){ |
| 3787 | return TCL_ERROR; |
| 3788 | } |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 3789 | rc = sqlite3OsUnlock(pFile, NO_LOCK); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 3790 | if( rc!=SQLITE_OK ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3791 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 3792 | return TCL_ERROR; |
| 3793 | } |
| 3794 | return TCL_OK; |
| 3795 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 3796 | |
drh | ab3f9fe | 2004-08-14 17:10:10 +0000 | [diff] [blame] | 3797 | /* |
| 3798 | ** Usage: sqlite3OsTempFileName |
| 3799 | */ |
| 3800 | static int test_sqlite3OsTempFileName( |
| 3801 | void * clientData, |
| 3802 | Tcl_Interp *interp, |
| 3803 | int objc, |
| 3804 | Tcl_Obj *CONST objv[] |
| 3805 | ){ |
| 3806 | char zFile[SQLITE_TEMPNAME_SIZE]; |
| 3807 | int rc; |
| 3808 | |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 3809 | rc = sqlite3OsTempFileName(zFile); |
drh | ab3f9fe | 2004-08-14 17:10:10 +0000 | [diff] [blame] | 3810 | if( rc!=SQLITE_OK ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3811 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
drh | ab3f9fe | 2004-08-14 17:10:10 +0000 | [diff] [blame] | 3812 | return TCL_ERROR; |
| 3813 | } |
| 3814 | Tcl_AppendResult(interp, zFile, 0); |
| 3815 | return TCL_OK; |
| 3816 | } |
danielk1977 | 44ee5bf | 2005-05-27 09:41:12 +0000 | [diff] [blame] | 3817 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3818 | #endif |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 3819 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3820 | /* |
drh | cacb208 | 2005-01-11 15:28:33 +0000 | [diff] [blame] | 3821 | ** Usage: sqlite_set_magic DB MAGIC-NUMBER |
| 3822 | ** |
| 3823 | ** Set the db->magic value. This is used to test error recovery logic. |
| 3824 | */ |
| 3825 | static int sqlite_set_magic( |
| 3826 | void * clientData, |
| 3827 | Tcl_Interp *interp, |
| 3828 | int argc, |
| 3829 | char **argv |
| 3830 | ){ |
| 3831 | sqlite3 *db; |
| 3832 | if( argc!=3 ){ |
| 3833 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 3834 | " DB MAGIC", 0); |
| 3835 | return TCL_ERROR; |
| 3836 | } |
| 3837 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 3838 | if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){ |
| 3839 | db->magic = SQLITE_MAGIC_OPEN; |
| 3840 | }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){ |
| 3841 | db->magic = SQLITE_MAGIC_CLOSED; |
| 3842 | }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){ |
| 3843 | db->magic = SQLITE_MAGIC_BUSY; |
| 3844 | }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){ |
| 3845 | db->magic = SQLITE_MAGIC_ERROR; |
| 3846 | }else if( Tcl_GetInt(interp, argv[2], &db->magic) ){ |
| 3847 | return TCL_ERROR; |
| 3848 | } |
| 3849 | return TCL_OK; |
| 3850 | } |
| 3851 | |
| 3852 | /* |
drh | c5cdca6 | 2005-01-11 16:54:14 +0000 | [diff] [blame] | 3853 | ** Usage: sqlite3_interrupt DB |
| 3854 | ** |
| 3855 | ** Trigger an interrupt on DB |
| 3856 | */ |
| 3857 | static int test_interrupt( |
| 3858 | void * clientData, |
| 3859 | Tcl_Interp *interp, |
| 3860 | int argc, |
| 3861 | char **argv |
| 3862 | ){ |
| 3863 | sqlite3 *db; |
| 3864 | if( argc!=2 ){ |
| 3865 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0); |
| 3866 | return TCL_ERROR; |
| 3867 | } |
| 3868 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 3869 | sqlite3_interrupt(db); |
| 3870 | return TCL_OK; |
| 3871 | } |
| 3872 | |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 3873 | static u8 *sqlite3_stack_baseline = 0; |
| 3874 | |
drh | c5cdca6 | 2005-01-11 16:54:14 +0000 | [diff] [blame] | 3875 | /* |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 3876 | ** Fill the stack with a known bitpattern. |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 3877 | */ |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 3878 | static void prepStack(void){ |
| 3879 | int i; |
| 3880 | u32 bigBuf[65536]; |
| 3881 | for(i=0; i<sizeof(bigBuf); i++) bigBuf[i] = 0xdeadbeef; |
| 3882 | sqlite3_stack_baseline = (u8*)&bigBuf[65536]; |
| 3883 | } |
| 3884 | |
| 3885 | /* |
| 3886 | ** Get the current stack depth. Used for debugging only. |
| 3887 | */ |
| 3888 | u64 sqlite3StackDepth(void){ |
| 3889 | u8 x; |
| 3890 | return (u64)(sqlite3_stack_baseline - &x); |
| 3891 | } |
| 3892 | |
| 3893 | /* |
| 3894 | ** Usage: sqlite3_stack_used DB SQL |
| 3895 | ** |
| 3896 | ** Try to measure the amount of stack space used by a call to sqlite3_exec |
| 3897 | */ |
| 3898 | static int test_stack_used( |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 3899 | void * clientData, |
| 3900 | Tcl_Interp *interp, |
| 3901 | int argc, |
| 3902 | char **argv |
| 3903 | ){ |
| 3904 | sqlite3 *db; |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 3905 | int i; |
| 3906 | if( argc!=3 ){ |
| 3907 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 3908 | " DB SQL", 0); |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 3909 | return TCL_ERROR; |
| 3910 | } |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 3911 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 3912 | prepStack(); |
drh | 3752785 | 2006-03-16 16:19:56 +0000 | [diff] [blame] | 3913 | (void)sqlite3_exec(db, argv[2], 0, 0, 0); |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 3914 | for(i=65535; i>=0 && ((u32*)sqlite3_stack_baseline)[-i]==0xdeadbeef; i--){} |
| 3915 | Tcl_SetObjResult(interp, Tcl_NewIntObj(i*4)); |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 3916 | return TCL_OK; |
| 3917 | } |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 3918 | |
| 3919 | /* |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 3920 | ** Usage: sqlite_delete_function DB function-name |
| 3921 | ** |
| 3922 | ** Delete the user function 'function-name' from database handle DB. It |
| 3923 | ** is assumed that the user function was created as UTF8, any number of |
| 3924 | ** arguments (the way the TCL interface does it). |
| 3925 | */ |
| 3926 | static int delete_function( |
| 3927 | void * clientData, |
| 3928 | Tcl_Interp *interp, |
| 3929 | int argc, |
| 3930 | char **argv |
| 3931 | ){ |
| 3932 | int rc; |
| 3933 | sqlite3 *db; |
| 3934 | if( argc!=3 ){ |
| 3935 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 3936 | " DB function-name", 0); |
| 3937 | return TCL_ERROR; |
| 3938 | } |
| 3939 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 3940 | rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3941 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 3942 | return TCL_OK; |
| 3943 | } |
| 3944 | |
| 3945 | /* |
| 3946 | ** Usage: sqlite_delete_collation DB collation-name |
| 3947 | ** |
| 3948 | ** Delete the collation sequence 'collation-name' from database handle |
| 3949 | ** DB. It is assumed that the collation sequence was created as UTF8 (the |
| 3950 | ** way the TCL interface does it). |
| 3951 | */ |
| 3952 | static int delete_collation( |
| 3953 | void * clientData, |
| 3954 | Tcl_Interp *interp, |
| 3955 | int argc, |
| 3956 | char **argv |
| 3957 | ){ |
| 3958 | int rc; |
| 3959 | sqlite3 *db; |
| 3960 | if( argc!=3 ){ |
| 3961 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 3962 | " DB function-name", 0); |
| 3963 | return TCL_ERROR; |
| 3964 | } |
| 3965 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 3966 | rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3967 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 3968 | return TCL_OK; |
| 3969 | } |
| 3970 | |
| 3971 | /* |
drh | 3e1d8e6 | 2005-05-26 16:23:34 +0000 | [diff] [blame] | 3972 | ** Usage: sqlite3_get_autocommit DB |
| 3973 | ** |
| 3974 | ** Return true if the database DB is currently in auto-commit mode. |
| 3975 | ** Return false if not. |
| 3976 | */ |
| 3977 | static int get_autocommit( |
| 3978 | void * clientData, |
| 3979 | Tcl_Interp *interp, |
| 3980 | int argc, |
| 3981 | char **argv |
| 3982 | ){ |
| 3983 | char zBuf[30]; |
| 3984 | sqlite3 *db; |
| 3985 | if( argc!=2 ){ |
| 3986 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 3987 | " DB", 0); |
| 3988 | return TCL_ERROR; |
| 3989 | } |
| 3990 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 3991 | sprintf(zBuf, "%d", sqlite3_get_autocommit(db)); |
| 3992 | Tcl_AppendResult(interp, zBuf, 0); |
| 3993 | return TCL_OK; |
| 3994 | } |
| 3995 | |
| 3996 | /* |
drh | 3086765 | 2006-07-06 10:59:57 +0000 | [diff] [blame] | 3997 | ** Usage: sqlite3_busy_timeout DB MS |
| 3998 | ** |
| 3999 | ** Set the busy timeout. This is more easily done using the timeout |
| 4000 | ** method of the TCL interface. But we need a way to test the case |
| 4001 | ** where it returns SQLITE_MISUSE. |
| 4002 | */ |
| 4003 | static int test_busy_timeout( |
| 4004 | void * clientData, |
| 4005 | Tcl_Interp *interp, |
| 4006 | int argc, |
| 4007 | char **argv |
| 4008 | ){ |
| 4009 | int rc, ms; |
| 4010 | sqlite3 *db; |
| 4011 | if( argc!=3 ){ |
| 4012 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 4013 | " DB", 0); |
| 4014 | return TCL_ERROR; |
| 4015 | } |
| 4016 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 4017 | if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR; |
| 4018 | rc = sqlite3_busy_timeout(db, ms); |
| 4019 | Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0); |
| 4020 | return TCL_OK; |
| 4021 | } |
| 4022 | |
| 4023 | /* |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 4024 | ** Usage: tcl_variable_type VARIABLENAME |
| 4025 | ** |
| 4026 | ** Return the name of the internal representation for the |
| 4027 | ** value of the given variable. |
| 4028 | */ |
| 4029 | static int tcl_variable_type( |
| 4030 | void * clientData, |
| 4031 | Tcl_Interp *interp, |
| 4032 | int objc, |
| 4033 | Tcl_Obj *CONST objv[] |
| 4034 | ){ |
| 4035 | Tcl_Obj *pVar; |
| 4036 | if( objc!=2 ){ |
| 4037 | Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE"); |
| 4038 | return TCL_ERROR; |
| 4039 | } |
| 4040 | pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG); |
| 4041 | if( pVar==0 ) return TCL_ERROR; |
| 4042 | if( pVar->typePtr ){ |
| 4043 | Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1)); |
| 4044 | } |
| 4045 | return TCL_OK; |
| 4046 | } |
| 4047 | |
| 4048 | /* |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4049 | ** Usage: sqlite3_release_memory ?N? |
| 4050 | ** |
| 4051 | ** Attempt to release memory currently held but not actually required. |
| 4052 | ** The integer N is the number of bytes we are trying to release. The |
| 4053 | ** return value is the amount of memory actually released. |
| 4054 | */ |
| 4055 | static int test_release_memory( |
| 4056 | void * clientData, |
| 4057 | Tcl_Interp *interp, |
| 4058 | int objc, |
| 4059 | Tcl_Obj *CONST objv[] |
| 4060 | ){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 4061 | #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO) |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4062 | int N; |
| 4063 | int amt; |
| 4064 | if( objc!=1 && objc!=2 ){ |
| 4065 | Tcl_WrongNumArgs(interp, 1, objv, "?N?"); |
| 4066 | return TCL_ERROR; |
| 4067 | } |
| 4068 | if( objc==2 ){ |
| 4069 | if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR; |
| 4070 | }else{ |
| 4071 | N = -1; |
| 4072 | } |
| 4073 | amt = sqlite3_release_memory(N); |
| 4074 | Tcl_SetObjResult(interp, Tcl_NewIntObj(amt)); |
| 4075 | #endif |
| 4076 | return TCL_OK; |
| 4077 | } |
| 4078 | |
| 4079 | /* |
| 4080 | ** Usage: sqlite3_soft_heap_limit ?N? |
| 4081 | ** |
| 4082 | ** Query or set the soft heap limit for the current thread. The |
| 4083 | ** limit is only changed if the N is present. The previous limit |
| 4084 | ** is returned. |
| 4085 | */ |
| 4086 | static int test_soft_heap_limit( |
| 4087 | void * clientData, |
| 4088 | Tcl_Interp *interp, |
| 4089 | int objc, |
| 4090 | Tcl_Obj *CONST objv[] |
| 4091 | ){ |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4092 | static int softHeapLimit = 0; |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4093 | int amt; |
| 4094 | if( objc!=1 && objc!=2 ){ |
| 4095 | Tcl_WrongNumArgs(interp, 1, objv, "?N?"); |
| 4096 | return TCL_ERROR; |
| 4097 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4098 | amt = softHeapLimit; |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4099 | if( objc==2 ){ |
| 4100 | int N; |
| 4101 | if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR; |
| 4102 | sqlite3_soft_heap_limit(N); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 4103 | softHeapLimit = N; |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4104 | } |
| 4105 | Tcl_SetObjResult(interp, Tcl_NewIntObj(amt)); |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4106 | return TCL_OK; |
| 4107 | } |
| 4108 | |
| 4109 | /* |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 4110 | ** Usage: sqlite3_thread_cleanup |
| 4111 | ** |
| 4112 | ** Call the sqlite3_thread_cleanup API. |
| 4113 | */ |
| 4114 | static int test_thread_cleanup( |
| 4115 | void * clientData, |
| 4116 | Tcl_Interp *interp, |
| 4117 | int objc, |
| 4118 | Tcl_Obj *CONST objv[] |
| 4119 | ){ |
| 4120 | sqlite3_thread_cleanup(); |
| 4121 | return TCL_OK; |
| 4122 | } |
| 4123 | |
| 4124 | |
| 4125 | /* |
drh | c6ba55f | 2007-04-05 17:36:18 +0000 | [diff] [blame] | 4126 | ** Usage: sqlite3_pager_refcounts DB |
| 4127 | ** |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4128 | ** Return a list of numbers which are the PagerRefcount for all |
| 4129 | ** pagers on each database connection. |
drh | c6ba55f | 2007-04-05 17:36:18 +0000 | [diff] [blame] | 4130 | */ |
| 4131 | static int test_pager_refcounts( |
| 4132 | void * clientData, |
| 4133 | Tcl_Interp *interp, |
| 4134 | int objc, |
| 4135 | Tcl_Obj *CONST objv[] |
| 4136 | ){ |
| 4137 | sqlite3 *db; |
| 4138 | int i; |
| 4139 | int v, *a; |
| 4140 | Tcl_Obj *pResult; |
| 4141 | |
| 4142 | if( objc!=2 ){ |
| 4143 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4144 | Tcl_GetStringFromObj(objv[0], 0), " DB", 0); |
drh | c6ba55f | 2007-04-05 17:36:18 +0000 | [diff] [blame] | 4145 | return TCL_ERROR; |
| 4146 | } |
| 4147 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 4148 | pResult = Tcl_NewObj(); |
| 4149 | for(i=0; i<db->nDb; i++){ |
| 4150 | if( db->aDb[i].pBt==0 ){ |
| 4151 | v = -1; |
| 4152 | }else{ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 4153 | sqlite3_mutex_enter(db->mutex); |
drh | c6ba55f | 2007-04-05 17:36:18 +0000 | [diff] [blame] | 4154 | a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt)); |
| 4155 | v = a[0]; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 4156 | sqlite3_mutex_leave(db->mutex); |
drh | c6ba55f | 2007-04-05 17:36:18 +0000 | [diff] [blame] | 4157 | } |
| 4158 | Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v)); |
| 4159 | } |
| 4160 | Tcl_SetObjResult(interp, pResult); |
| 4161 | return TCL_OK; |
| 4162 | } |
| 4163 | |
| 4164 | |
| 4165 | /* |
drh | 80788d8 | 2006-09-02 14:50:23 +0000 | [diff] [blame] | 4166 | ** tclcmd: working_64bit_int |
| 4167 | ** |
| 4168 | ** Some TCL builds (ex: cygwin) do not support 64-bit integers. This |
| 4169 | ** leads to a number of test failures. The present command checks the |
| 4170 | ** TCL build to see whether or not it supports 64-bit integers. It |
| 4171 | ** returns TRUE if it does and FALSE if not. |
| 4172 | ** |
| 4173 | ** This command is used to warn users that their TCL build is defective |
| 4174 | ** and that the errors they are seeing in the test scripts might be |
| 4175 | ** a result of their defective TCL rather than problems in SQLite. |
| 4176 | */ |
| 4177 | static int working_64bit_int( |
| 4178 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 4179 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 4180 | int objc, /* Number of arguments */ |
| 4181 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 4182 | ){ |
| 4183 | Tcl_Obj *pTestObj; |
| 4184 | int working = 0; |
| 4185 | |
| 4186 | pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890); |
| 4187 | working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0; |
| 4188 | Tcl_DecrRefCount(pTestObj); |
| 4189 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working)); |
| 4190 | return TCL_OK; |
| 4191 | } |
| 4192 | |
| 4193 | |
| 4194 | /* |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 4195 | ** tclcmd: vfs_unlink_test |
| 4196 | ** |
| 4197 | ** This TCL command unregisters the primary VFS and then registers |
| 4198 | ** it back again. This is used to test the ability to register a |
| 4199 | ** VFS when none are previously registered, and the ability to |
| 4200 | ** unregister the only available VFS. Ticket #2738 |
| 4201 | */ |
| 4202 | static int vfs_unlink_test( |
| 4203 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 4204 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 4205 | int objc, /* Number of arguments */ |
| 4206 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 4207 | ){ |
| 4208 | int i; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4209 | sqlite3_vfs *pMain; |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 4210 | sqlite3_vfs *apVfs[20]; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4211 | sqlite3_vfs one, two; |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 4212 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4213 | sqlite3_vfs_unregister(0); /* Unregister of NULL is harmless */ |
| 4214 | one.zName = "__one"; |
| 4215 | two.zName = "__two"; |
| 4216 | |
| 4217 | /* Calling sqlite3_vfs_register with 2nd argument of 0 does not |
| 4218 | ** change the default VFS |
| 4219 | */ |
| 4220 | pMain = sqlite3_vfs_find(0); |
| 4221 | sqlite3_vfs_register(&one, 0); |
| 4222 | assert( pMain==0 || pMain==sqlite3_vfs_find(0) ); |
| 4223 | sqlite3_vfs_register(&two, 0); |
| 4224 | assert( pMain==0 || pMain==sqlite3_vfs_find(0) ); |
| 4225 | |
| 4226 | /* We can find a VFS by its name */ |
| 4227 | assert( sqlite3_vfs_find("__one")==&one ); |
| 4228 | assert( sqlite3_vfs_find("__two")==&two ); |
| 4229 | |
| 4230 | /* Calling sqlite_vfs_register with non-zero second parameter changes the |
| 4231 | ** default VFS, even if the 1st parameter is an existig VFS that is |
| 4232 | ** previously registered as the non-default. |
| 4233 | */ |
| 4234 | sqlite3_vfs_register(&one, 1); |
| 4235 | assert( sqlite3_vfs_find("__one")==&one ); |
| 4236 | assert( sqlite3_vfs_find("__two")==&two ); |
| 4237 | assert( sqlite3_vfs_find(0)==&one ); |
| 4238 | sqlite3_vfs_register(&two, 1); |
| 4239 | assert( sqlite3_vfs_find("__one")==&one ); |
| 4240 | assert( sqlite3_vfs_find("__two")==&two ); |
| 4241 | assert( sqlite3_vfs_find(0)==&two ); |
| 4242 | if( pMain ){ |
| 4243 | sqlite3_vfs_register(pMain, 1); |
| 4244 | assert( sqlite3_vfs_find("__one")==&one ); |
| 4245 | assert( sqlite3_vfs_find("__two")==&two ); |
| 4246 | assert( sqlite3_vfs_find(0)==pMain ); |
| 4247 | } |
| 4248 | |
| 4249 | /* Unlink the default VFS. Repeat until there are no more VFSes |
| 4250 | ** registered. |
| 4251 | */ |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 4252 | for(i=0; i<sizeof(apVfs)/sizeof(apVfs[0]); i++){ |
| 4253 | apVfs[i] = sqlite3_vfs_find(0); |
| 4254 | if( apVfs[i] ){ |
| 4255 | assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) ); |
| 4256 | sqlite3_vfs_unregister(apVfs[i]); |
| 4257 | assert( 0==sqlite3_vfs_find(apVfs[i]->zName) ); |
| 4258 | } |
| 4259 | } |
| 4260 | assert( 0==sqlite3_vfs_find(0) ); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4261 | |
| 4262 | /* Relink all VFSes in reverse order. */ |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 4263 | for(i=sizeof(apVfs)/sizeof(apVfs[0])-1; i>=0; i--){ |
| 4264 | if( apVfs[i] ){ |
| 4265 | sqlite3_vfs_register(apVfs[i], 1); |
| 4266 | assert( apVfs[i]==sqlite3_vfs_find(0) ); |
| 4267 | assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) ); |
| 4268 | } |
| 4269 | } |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4270 | |
| 4271 | /* Unregister out sample VFSes. */ |
| 4272 | sqlite3_vfs_unregister(&one); |
| 4273 | sqlite3_vfs_unregister(&two); |
| 4274 | |
| 4275 | /* Unregistering a VFS that is not currently registered is harmless */ |
| 4276 | sqlite3_vfs_unregister(&one); |
| 4277 | sqlite3_vfs_unregister(&two); |
| 4278 | assert( sqlite3_vfs_find("__one")==0 ); |
| 4279 | assert( sqlite3_vfs_find("__two")==0 ); |
| 4280 | |
| 4281 | /* We should be left with the original default VFS back as the |
| 4282 | ** original */ |
| 4283 | assert( sqlite3_vfs_find(0)==pMain ); |
| 4284 | |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 4285 | return TCL_OK; |
| 4286 | } |
| 4287 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 4288 | /* |
drh | 5517625 | 2008-01-22 14:50:16 +0000 | [diff] [blame] | 4289 | ** tclcmd: file_control_test DB |
| 4290 | ** |
| 4291 | ** This TCL command runs the sqlite3_file_control interface and |
| 4292 | ** verifies correct operation of the same. |
| 4293 | */ |
| 4294 | static int file_control_test( |
| 4295 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 4296 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 4297 | int objc, /* Number of arguments */ |
| 4298 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 4299 | ){ |
| 4300 | int iArg = 0; |
| 4301 | sqlite3 *db; |
| 4302 | int rc; |
| 4303 | |
| 4304 | if( objc!=2 ){ |
| 4305 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4306 | Tcl_GetStringFromObj(objv[0], 0), " DB", 0); |
| 4307 | return TCL_ERROR; |
| 4308 | } |
| 4309 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 4310 | rc = sqlite3_file_control(db, 0, 0, &iArg); |
| 4311 | assert( rc==SQLITE_ERROR ); |
| 4312 | rc = sqlite3_file_control(db, "notadatabase", SQLITE_FCNTL_LOCKSTATE, &iArg); |
| 4313 | assert( rc==SQLITE_ERROR ); |
| 4314 | rc = sqlite3_file_control(db, "main", -1, &iArg); |
| 4315 | assert( rc==SQLITE_ERROR ); |
| 4316 | rc = sqlite3_file_control(db, "temp", -1, &iArg); |
| 4317 | assert( rc==SQLITE_ERROR ); |
| 4318 | return TCL_OK; |
| 4319 | } |
| 4320 | |
| 4321 | /* |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 4322 | ** tclcmd: save_prng_state |
| 4323 | */ |
| 4324 | static int save_prng_state( |
| 4325 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 4326 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 4327 | int objc, /* Number of arguments */ |
| 4328 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 4329 | ){ |
| 4330 | extern void sqlite3SavePrngState(void); |
| 4331 | sqlite3SavePrngState(); |
| 4332 | return TCL_OK; |
| 4333 | } |
| 4334 | /* |
| 4335 | ** tclcmd: restore_prng_state |
| 4336 | */ |
| 4337 | static int restore_prng_state( |
| 4338 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 4339 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 4340 | int objc, /* Number of arguments */ |
| 4341 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 4342 | ){ |
| 4343 | extern void sqlite3RestorePrngState(void); |
| 4344 | sqlite3RestorePrngState(); |
| 4345 | return TCL_OK; |
| 4346 | } |
| 4347 | /* |
| 4348 | ** tclcmd: reset_prng_state |
| 4349 | */ |
| 4350 | static int reset_prng_state( |
| 4351 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 4352 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 4353 | int objc, /* Number of arguments */ |
| 4354 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 4355 | ){ |
| 4356 | extern void sqlite3ResetPrngState(void); |
| 4357 | sqlite3ResetPrngState(); |
| 4358 | return TCL_OK; |
| 4359 | } |
| 4360 | |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 4361 | |
| 4362 | /* |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 4363 | ** Register commands with the TCL interpreter. |
| 4364 | */ |
| 4365 | int Sqlitetest1_Init(Tcl_Interp *interp){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 4366 | extern int sqlite3_search_count; |
| 4367 | extern int sqlite3_interrupt_count; |
| 4368 | extern int sqlite3_open_file_count; |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 4369 | extern int sqlite3_sort_count; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 4370 | extern int sqlite3_current_time; |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 4371 | extern int sqlite3_max_blobsize; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4372 | extern int sqlite3BtreeSharedCacheReport(void*, |
| 4373 | Tcl_Interp*,int,Tcl_Obj*CONST*); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 4374 | static struct { |
| 4375 | char *zName; |
| 4376 | Tcl_CmdProc *xProc; |
| 4377 | } aCmd[] = { |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 4378 | { "db_enter", (Tcl_CmdProc*)db_enter }, |
| 4379 | { "db_leave", (Tcl_CmdProc*)db_leave }, |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 4380 | { "sqlite3_mprintf_int", (Tcl_CmdProc*)sqlite3_mprintf_int }, |
drh | e970767 | 2004-06-25 01:10:48 +0000 | [diff] [blame] | 4381 | { "sqlite3_mprintf_int64", (Tcl_CmdProc*)sqlite3_mprintf_int64 }, |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 4382 | { "sqlite3_mprintf_str", (Tcl_CmdProc*)sqlite3_mprintf_str }, |
drh | b3738b6 | 2007-03-31 15:02:49 +0000 | [diff] [blame] | 4383 | { "sqlite3_snprintf_str", (Tcl_CmdProc*)sqlite3_snprintf_str }, |
drh | e29b1a0 | 2004-07-17 21:56:09 +0000 | [diff] [blame] | 4384 | { "sqlite3_mprintf_stronly", (Tcl_CmdProc*)sqlite3_mprintf_stronly}, |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 4385 | { "sqlite3_mprintf_double", (Tcl_CmdProc*)sqlite3_mprintf_double }, |
| 4386 | { "sqlite3_mprintf_scaled", (Tcl_CmdProc*)sqlite3_mprintf_scaled }, |
drh | 6378285 | 2005-08-30 19:30:59 +0000 | [diff] [blame] | 4387 | { "sqlite3_mprintf_hexdouble", (Tcl_CmdProc*)sqlite3_mprintf_hexdouble}, |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 4388 | { "sqlite3_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z }, |
drh | 05a8298 | 2006-03-19 13:00:25 +0000 | [diff] [blame] | 4389 | { "sqlite3_mprintf_n_test", (Tcl_CmdProc*)test_mprintf_n }, |
drh | 6885390 | 2007-05-07 11:24:30 +0000 | [diff] [blame] | 4390 | { "sqlite3_snprintf_int", (Tcl_CmdProc*)test_snprintf_int }, |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 4391 | { "sqlite3_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid }, |
| 4392 | { "sqlite3_exec_printf", (Tcl_CmdProc*)test_exec_printf }, |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 4393 | { "sqlite3_exec", (Tcl_CmdProc*)test_exec }, |
| 4394 | { "sqlite3_exec_nr", (Tcl_CmdProc*)test_exec_nr }, |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 4395 | { "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf }, |
| 4396 | { "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close }, |
| 4397 | { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function }, |
| 4398 | { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate }, |
| 4399 | { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func }, |
| 4400 | { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort }, |
drh | 25d6543 | 2004-07-22 15:02:25 +0000 | [diff] [blame] | 4401 | { "sqlite_bind", (Tcl_CmdProc*)test_bind }, |
| 4402 | { "breakpoint", (Tcl_CmdProc*)test_breakpoint }, |
| 4403 | { "sqlite3_key", (Tcl_CmdProc*)test_key }, |
| 4404 | { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey }, |
drh | cacb208 | 2005-01-11 15:28:33 +0000 | [diff] [blame] | 4405 | { "sqlite_set_magic", (Tcl_CmdProc*)sqlite_set_magic }, |
drh | c5cdca6 | 2005-01-11 16:54:14 +0000 | [diff] [blame] | 4406 | { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt }, |
drh | 3e1d8e6 | 2005-05-26 16:23:34 +0000 | [diff] [blame] | 4407 | { "sqlite_delete_function", (Tcl_CmdProc*)delete_function }, |
| 4408 | { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation }, |
| 4409 | { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit }, |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 4410 | { "sqlite3_stack_used", (Tcl_CmdProc*)test_stack_used }, |
drh | 3086765 | 2006-07-06 10:59:57 +0000 | [diff] [blame] | 4411 | { "sqlite3_busy_timeout", (Tcl_CmdProc*)test_busy_timeout }, |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 4412 | { "printf", (Tcl_CmdProc*)test_printf }, |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4413 | { "sqlite3_io_trace", (Tcl_CmdProc*)test_io_trace }, |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 4414 | }; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 4415 | static struct { |
| 4416 | char *zName; |
| 4417 | Tcl_ObjCmdProc *xProc; |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4418 | void *clientData; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 4419 | } aObjCmd[] = { |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 4420 | { "sqlite3_connection_pointer", get_sqlite_pointer, 0 }, |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 4421 | { "sqlite3_bind_int", test_bind_int, 0 }, |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4422 | { "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 }, |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 4423 | { "sqlite3_bind_int64", test_bind_int64, 0 }, |
| 4424 | { "sqlite3_bind_double", test_bind_double, 0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4425 | { "sqlite3_bind_null", test_bind_null ,0 }, |
| 4426 | { "sqlite3_bind_text", test_bind_text ,0 }, |
| 4427 | { "sqlite3_bind_text16", test_bind_text16 ,0 }, |
| 4428 | { "sqlite3_bind_blob", test_bind_blob ,0 }, |
drh | 75f6a03 | 2004-07-15 14:15:00 +0000 | [diff] [blame] | 4429 | { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0}, |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 4430 | { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0}, |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 4431 | { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0}, |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 4432 | { "sqlite3_clear_bindings", test_clear_bindings, 0}, |
drh | f9cb7f5 | 2006-06-27 20:06:44 +0000 | [diff] [blame] | 4433 | { "sqlite3_sleep", test_sleep, 0}, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4434 | { "sqlite3_errcode", test_errcode ,0 }, |
| 4435 | { "sqlite3_errmsg", test_errmsg ,0 }, |
| 4436 | { "sqlite3_errmsg16", test_errmsg16 ,0 }, |
| 4437 | { "sqlite3_open", test_open ,0 }, |
| 4438 | { "sqlite3_open16", test_open16 ,0 }, |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 4439 | { "sqlite3_complete16", test_complete16 ,0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4440 | |
| 4441 | { "sqlite3_prepare", test_prepare ,0 }, |
| 4442 | { "sqlite3_prepare16", test_prepare16 ,0 }, |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 4443 | { "sqlite3_prepare_v2", test_prepare_v2 ,0 }, |
| 4444 | { "sqlite3_prepare16_v2", test_prepare16_v2 ,0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4445 | { "sqlite3_finalize", test_finalize ,0 }, |
| 4446 | { "sqlite3_reset", test_reset ,0 }, |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 4447 | { "sqlite3_expired", test_expired ,0 }, |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 4448 | { "sqlite3_transfer_bindings", test_transfer_bind ,0 }, |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 4449 | { "sqlite3_changes", test_changes ,0 }, |
| 4450 | { "sqlite3_step", test_step ,0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4451 | |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 4452 | { "sqlite3_release_memory", test_release_memory, 0}, |
| 4453 | { "sqlite3_soft_heap_limit", test_soft_heap_limit, 0}, |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 4454 | { "sqlite3_thread_cleanup", test_thread_cleanup, 0}, |
drh | c6ba55f | 2007-04-05 17:36:18 +0000 | [diff] [blame] | 4455 | { "sqlite3_pager_refcounts", test_pager_refcounts, 0}, |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4456 | |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 4457 | { "sqlite3_load_extension", test_load_extension, 0}, |
| 4458 | { "sqlite3_enable_load_extension", test_enable_load, 0}, |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 4459 | { "sqlite3_extended_result_codes", test_extended_result_codes, 0}, |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 4460 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 4461 | { "save_prng_state", save_prng_state, 0 }, |
| 4462 | { "restore_prng_state", restore_prng_state, 0 }, |
| 4463 | { "reset_prng_state", reset_prng_state, 0 }, |
| 4464 | |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4465 | /* sqlite3_column_*() API */ |
| 4466 | { "sqlite3_column_count", test_column_count ,0 }, |
| 4467 | { "sqlite3_data_count", test_data_count ,0 }, |
| 4468 | { "sqlite3_column_type", test_column_type ,0 }, |
danielk1977 | ea61b2c | 2004-05-27 01:49:51 +0000 | [diff] [blame] | 4469 | { "sqlite3_column_blob", test_column_blob ,0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4470 | { "sqlite3_column_double", test_column_double ,0 }, |
| 4471 | { "sqlite3_column_int64", test_column_int64 ,0 }, |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 4472 | { "sqlite3_column_text", test_stmt_utf8, sqlite3_column_text }, |
| 4473 | { "sqlite3_column_decltype", test_stmt_utf8, sqlite3_column_decltype }, |
| 4474 | { "sqlite3_column_name", test_stmt_utf8, sqlite3_column_name }, |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 4475 | { "sqlite3_column_int", test_stmt_int, sqlite3_column_int }, |
| 4476 | { "sqlite3_column_bytes", test_stmt_int, sqlite3_column_bytes }, |
danielk1977 | 4b1ae99 | 2006-02-10 03:06:10 +0000 | [diff] [blame] | 4477 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 4478 | { "sqlite3_column_database_name", test_stmt_utf8, sqlite3_column_database_name}, |
| 4479 | { "sqlite3_column_table_name", test_stmt_utf8, sqlite3_column_table_name}, |
| 4480 | { "sqlite3_column_origin_name", test_stmt_utf8, sqlite3_column_origin_name}, |
danielk1977 | 4b1ae99 | 2006-02-10 03:06:10 +0000 | [diff] [blame] | 4481 | #endif |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 4482 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 4483 | #ifndef SQLITE_OMIT_UTF16 |
| 4484 | { "sqlite3_column_bytes16", test_stmt_int, sqlite3_column_bytes16 }, |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 4485 | { "sqlite3_column_text16", test_stmt_utf16, sqlite3_column_text16 }, |
| 4486 | { "sqlite3_column_decltype16", test_stmt_utf16, sqlite3_column_decltype16}, |
| 4487 | { "sqlite3_column_name16", test_stmt_utf16, sqlite3_column_name16 }, |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 4488 | { "add_alignment_test_collations", add_alignment_test_collations, 0 }, |
danielk1977 | 4b1ae99 | 2006-02-10 03:06:10 +0000 | [diff] [blame] | 4489 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 4490 | {"sqlite3_column_database_name16", |
| 4491 | test_stmt_utf16, sqlite3_column_database_name16}, |
| 4492 | {"sqlite3_column_table_name16", test_stmt_utf16, sqlite3_column_table_name16}, |
| 4493 | {"sqlite3_column_origin_name16", test_stmt_utf16, sqlite3_column_origin_name16}, |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 4494 | #endif |
danielk1977 | 4b1ae99 | 2006-02-10 03:06:10 +0000 | [diff] [blame] | 4495 | #endif |
danielk1977 | a393c03 | 2007-05-07 14:58:53 +0000 | [diff] [blame] | 4496 | { "sqlite3_create_collation_v2", test_create_collation_v2, 0 }, |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 4497 | { "sqlite3_global_recover", test_global_recover, 0 }, |
| 4498 | { "working_64bit_int", working_64bit_int, 0 }, |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 4499 | { "vfs_unlink_test", vfs_unlink_test, 0 }, |
drh | 5517625 | 2008-01-22 14:50:16 +0000 | [diff] [blame] | 4500 | { "file_control_test", file_control_test, 0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4501 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 4502 | /* Functions from os.h */ |
danielk1977 | 44ee5bf | 2005-05-27 09:41:12 +0000 | [diff] [blame] | 4503 | #ifndef SQLITE_OMIT_DISKIO |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4504 | #if 0 |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 4505 | { "sqlite3OsOpenReadWrite",test_sqlite3OsOpenReadWrite, 0 }, |
| 4506 | { "sqlite3OsClose", test_sqlite3OsClose, 0 }, |
| 4507 | { "sqlite3OsLock", test_sqlite3OsLock, 0 }, |
drh | ab3f9fe | 2004-08-14 17:10:10 +0000 | [diff] [blame] | 4508 | { "sqlite3OsTempFileName", test_sqlite3OsTempFileName, 0 }, |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 4509 | |
| 4510 | /* Custom test interfaces */ |
| 4511 | { "sqlite3OsUnlock", test_sqlite3OsUnlock, 0 }, |
danielk1977 | 44ee5bf | 2005-05-27 09:41:12 +0000 | [diff] [blame] | 4512 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4513 | #endif |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 4514 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 4515 | { "add_test_collate", test_collate, 0 }, |
| 4516 | { "add_test_collate_needed", test_collate_needed, 0 }, |
| 4517 | { "add_test_function", test_function, 0 }, |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 4518 | #endif |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 4519 | { "sqlite3_test_errstr", test_errstr, 0 }, |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 4520 | { "tcl_variable_type", tcl_variable_type, 0 }, |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4521 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 4522 | { "sqlite3_enable_shared_cache", test_enable_shared, 0 }, |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4523 | { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0}, |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4524 | #endif |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 4525 | { "sqlite3_libversion_number", test_libversion_number, 0 }, |
danielk1977 | deb802c | 2006-02-09 13:43:28 +0000 | [diff] [blame] | 4526 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 4527 | { "sqlite3_table_column_metadata", test_table_column_metadata, 0 }, |
| 4528 | #endif |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 4529 | #ifndef SQLITE_OMIT_INCRBLOB |
| 4530 | { "sqlite3_blob_read", test_blob_read, 0 }, |
| 4531 | { "sqlite3_blob_write", test_blob_write, 0 }, |
| 4532 | #endif |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 4533 | }; |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 4534 | static int bitmask_size = sizeof(Bitmask)*8; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 4535 | int i; |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 4536 | extern int sqlite3_sync_count, sqlite3_fullsync_count; |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 4537 | extern int sqlite3_opentemp_count; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 4538 | extern int sqlite3_like_count; |
drh | dd73521 | 2007-02-24 13:53:05 +0000 | [diff] [blame] | 4539 | extern int sqlite3_xferopt_count; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4540 | extern int sqlite3_pager_readdb_count; |
| 4541 | extern int sqlite3_pager_writedb_count; |
| 4542 | extern int sqlite3_pager_writej_count; |
| 4543 | extern int sqlite3_pager_pgfree_count; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4544 | #if OS_UNIX && defined(SQLITE_TEST) && SQLITE_THREADSAFE |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 4545 | extern int threadsOverrideEachOthersLocks; |
| 4546 | #endif |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 4547 | #if OS_WIN |
| 4548 | extern int sqlite3_os_type; |
| 4549 | #endif |
drh | 8b3d990 | 2005-08-19 00:14:42 +0000 | [diff] [blame] | 4550 | #ifdef SQLITE_DEBUG |
danielk1977 | e6a58a4 | 2007-08-31 17:42:48 +0000 | [diff] [blame] | 4551 | extern int sqlite3_where_trace; |
drh | 73be501 | 2007-08-08 12:11:21 +0000 | [diff] [blame] | 4552 | extern int sqlite3_os_trace; |
drh | 8b3d990 | 2005-08-19 00:14:42 +0000 | [diff] [blame] | 4553 | extern int sqlite3_vdbe_addop_trace; |
drh | 549c8b6 | 2005-09-19 13:15:23 +0000 | [diff] [blame] | 4554 | #endif |
| 4555 | #ifdef SQLITE_TEST |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 4556 | extern int sqlite3_enable_in_opt; |
drh | 549c8b6 | 2005-09-19 13:15:23 +0000 | [diff] [blame] | 4557 | extern char sqlite3_query_plan[]; |
drh | 9042f39 | 2005-07-15 23:24:23 +0000 | [diff] [blame] | 4558 | static char *query_plan = sqlite3_query_plan; |
drh | 48083ce | 2005-09-19 12:37:27 +0000 | [diff] [blame] | 4559 | #endif |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 4560 | |
| 4561 | for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ |
| 4562 | Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); |
| 4563 | } |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 4564 | for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 4565 | Tcl_CreateObjCommand(interp, aObjCmd[i].zName, |
| 4566 | aObjCmd[i].xProc, aObjCmd[i].clientData, 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 4567 | } |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 4568 | Tcl_LinkVar(interp, "sqlite_search_count", |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 4569 | (char*)&sqlite3_search_count, TCL_LINK_INT); |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 4570 | Tcl_LinkVar(interp, "sqlite_sort_count", |
| 4571 | (char*)&sqlite3_sort_count, TCL_LINK_INT); |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 4572 | Tcl_LinkVar(interp, "sqlite3_max_blobsize", |
| 4573 | (char*)&sqlite3_max_blobsize, TCL_LINK_INT); |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 4574 | Tcl_LinkVar(interp, "sqlite_like_count", |
| 4575 | (char*)&sqlite3_like_count, TCL_LINK_INT); |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 4576 | Tcl_LinkVar(interp, "sqlite_interrupt_count", |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 4577 | (char*)&sqlite3_interrupt_count, TCL_LINK_INT); |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 4578 | Tcl_LinkVar(interp, "sqlite_open_file_count", |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 4579 | (char*)&sqlite3_open_file_count, TCL_LINK_INT); |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 4580 | Tcl_LinkVar(interp, "sqlite_current_time", |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 4581 | (char*)&sqlite3_current_time, TCL_LINK_INT); |
drh | dd73521 | 2007-02-24 13:53:05 +0000 | [diff] [blame] | 4582 | Tcl_LinkVar(interp, "sqlite3_xferopt_count", |
| 4583 | (char*)&sqlite3_xferopt_count, TCL_LINK_INT); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4584 | Tcl_LinkVar(interp, "sqlite3_pager_readdb_count", |
| 4585 | (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT); |
| 4586 | Tcl_LinkVar(interp, "sqlite3_pager_writedb_count", |
| 4587 | (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT); |
| 4588 | Tcl_LinkVar(interp, "sqlite3_pager_writej_count", |
| 4589 | (char*)&sqlite3_pager_writej_count, TCL_LINK_INT); |
| 4590 | Tcl_LinkVar(interp, "sqlite3_pager_pgfree_count", |
| 4591 | (char*)&sqlite3_pager_pgfree_count, TCL_LINK_INT); |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 4592 | #ifndef SQLITE_OMIT_UTF16 |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 4593 | Tcl_LinkVar(interp, "unaligned_string_counter", |
| 4594 | (char*)&unaligned_string_counter, TCL_LINK_INT); |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 4595 | #endif |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4596 | #if OS_UNIX && defined(SQLITE_TEST) && SQLITE_THREADSAFE |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 4597 | Tcl_LinkVar(interp, "threadsOverrideEachOthersLocks", |
| 4598 | (char*)&threadsOverrideEachOthersLocks, TCL_LINK_INT); |
| 4599 | #endif |
drh | 268803a | 2005-12-14 20:11:30 +0000 | [diff] [blame] | 4600 | #ifndef SQLITE_OMIT_UTF16 |
| 4601 | Tcl_LinkVar(interp, "sqlite_last_needed_collation", |
| 4602 | (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY); |
| 4603 | #endif |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 4604 | #if OS_WIN |
| 4605 | Tcl_LinkVar(interp, "sqlite_os_type", |
| 4606 | (char*)&sqlite3_os_type, TCL_LINK_INT); |
| 4607 | #endif |
drh | 549c8b6 | 2005-09-19 13:15:23 +0000 | [diff] [blame] | 4608 | #ifdef SQLITE_TEST |
| 4609 | Tcl_LinkVar(interp, "sqlite_query_plan", |
| 4610 | (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY); |
| 4611 | #endif |
drh | 8b3d990 | 2005-08-19 00:14:42 +0000 | [diff] [blame] | 4612 | #ifdef SQLITE_DEBUG |
| 4613 | Tcl_LinkVar(interp, "sqlite_addop_trace", |
| 4614 | (char*)&sqlite3_vdbe_addop_trace, TCL_LINK_INT); |
drh | 48083ce | 2005-09-19 12:37:27 +0000 | [diff] [blame] | 4615 | Tcl_LinkVar(interp, "sqlite_where_trace", |
| 4616 | (char*)&sqlite3_where_trace, TCL_LINK_INT); |
drh | 73be501 | 2007-08-08 12:11:21 +0000 | [diff] [blame] | 4617 | Tcl_LinkVar(interp, "sqlite_os_trace", |
| 4618 | (char*)&sqlite3_os_trace, TCL_LINK_INT); |
drh | 8b3d990 | 2005-08-19 00:14:42 +0000 | [diff] [blame] | 4619 | #endif |
danielk1977 | cbe21be | 2005-06-07 07:58:48 +0000 | [diff] [blame] | 4620 | #ifndef SQLITE_OMIT_DISKIO |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 4621 | Tcl_LinkVar(interp, "sqlite_opentemp_count", |
| 4622 | (char*)&sqlite3_opentemp_count, TCL_LINK_INT); |
danielk1977 | cbe21be | 2005-06-07 07:58:48 +0000 | [diff] [blame] | 4623 | #endif |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 4624 | Tcl_LinkVar(interp, "sqlite_static_bind_value", |
| 4625 | (char*)&sqlite_static_bind_value, TCL_LINK_STRING); |
drh | f031381 | 2006-09-04 15:53:53 +0000 | [diff] [blame] | 4626 | Tcl_LinkVar(interp, "sqlite_static_bind_nbyte", |
| 4627 | (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT); |
drh | ab3f9fe | 2004-08-14 17:10:10 +0000 | [diff] [blame] | 4628 | Tcl_LinkVar(interp, "sqlite_temp_directory", |
drh | effd02b | 2004-08-29 23:42:13 +0000 | [diff] [blame] | 4629 | (char*)&sqlite3_temp_directory, TCL_LINK_STRING); |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 4630 | Tcl_LinkVar(interp, "bitmask_size", |
| 4631 | (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY); |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 4632 | Tcl_LinkVar(interp, "sqlite_sync_count", |
| 4633 | (char*)&sqlite3_sync_count, TCL_LINK_INT); |
| 4634 | Tcl_LinkVar(interp, "sqlite_fullsync_count", |
| 4635 | (char*)&sqlite3_fullsync_count, TCL_LINK_INT); |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 4636 | #ifdef SQLITE_TEST |
| 4637 | Tcl_LinkVar(interp, "sqlite_enable_in_opt", |
| 4638 | (char*)&sqlite3_enable_in_opt, TCL_LINK_INT); |
| 4639 | #endif |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 4640 | return TCL_OK; |
| 4641 | } |