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