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. |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 15 | */ |
| 16 | #include "sqliteInt.h" |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 17 | #include "vdbeInt.h" |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 18 | #include "tcl.h" |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 22 | /* |
| 23 | ** This is a copy of the first part of the SqliteDb structure in |
| 24 | ** tclsqlite.c. We need it here so that the get_sqlite_pointer routine |
| 25 | ** can extract the sqlite3* pointer from an existing Tcl SQLite |
| 26 | ** connection. |
| 27 | */ |
| 28 | struct SqliteDb { |
| 29 | sqlite3 *db; |
| 30 | }; |
| 31 | |
| 32 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 33 | ** Convert text generated by the "%p" conversion format back into |
| 34 | ** a pointer. |
| 35 | */ |
| 36 | static int testHexToInt(int h){ |
| 37 | if( h>='0' && h<='9' ){ |
| 38 | return h - '0'; |
| 39 | }else if( h>='a' && h<='f' ){ |
| 40 | return h - 'a' + 10; |
| 41 | }else{ |
| 42 | assert( h>='A' && h<='F' ); |
| 43 | return h - 'A' + 10; |
| 44 | } |
| 45 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 46 | void *sqlite3TestTextToPtr(const char *z){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 47 | void *p; |
| 48 | u64 v; |
| 49 | u32 v2; |
| 50 | if( z[0]=='0' && z[1]=='x' ){ |
| 51 | z += 2; |
| 52 | } |
| 53 | v = 0; |
| 54 | while( *z ){ |
| 55 | v = (v<<4) + testHexToInt(*z); |
| 56 | z++; |
| 57 | } |
| 58 | if( sizeof(p)==sizeof(v) ){ |
| 59 | memcpy(&p, &v, sizeof(p)); |
| 60 | }else{ |
| 61 | assert( sizeof(p)==sizeof(v2) ); |
| 62 | v2 = (u32)v; |
| 63 | memcpy(&p, &v2, sizeof(p)); |
| 64 | } |
| 65 | return p; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /* |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 70 | ** A TCL command that returns the address of the sqlite* pointer |
| 71 | ** for an sqlite connection instance. Bad things happen if the |
| 72 | ** input is not an sqlite connection. |
| 73 | */ |
| 74 | static int get_sqlite_pointer( |
| 75 | void * clientData, |
| 76 | Tcl_Interp *interp, |
| 77 | int objc, |
| 78 | Tcl_Obj *CONST objv[] |
| 79 | ){ |
| 80 | struct SqliteDb *p; |
| 81 | Tcl_CmdInfo cmdInfo; |
| 82 | char zBuf[100]; |
| 83 | if( objc!=2 ){ |
| 84 | Tcl_WrongNumArgs(interp, 1, objv, "SQLITE-CONNECTION"); |
| 85 | return TCL_ERROR; |
| 86 | } |
| 87 | if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ |
| 88 | Tcl_AppendResult(interp, "command not found: ", |
| 89 | Tcl_GetString(objv[1]), (char*)0); |
| 90 | return TCL_ERROR; |
| 91 | } |
| 92 | p = (struct SqliteDb*)cmdInfo.objClientData; |
| 93 | sprintf(zBuf, "%p", p->db); |
| 94 | if( strncmp(zBuf,"0x",2) ){ |
| 95 | sprintf(zBuf, "0x%p", p->db); |
| 96 | } |
| 97 | Tcl_AppendResult(interp, zBuf, 0); |
| 98 | return TCL_OK; |
| 99 | } |
| 100 | |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 101 | /* |
| 102 | ** Decode a pointer to an sqlite3 object. |
| 103 | */ |
drh | 24b58dd | 2008-07-07 14:50:14 +0000 | [diff] [blame] | 104 | int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){ |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 105 | struct SqliteDb *p; |
| 106 | Tcl_CmdInfo cmdInfo; |
| 107 | if( Tcl_GetCommandInfo(interp, zA, &cmdInfo) ){ |
| 108 | p = (struct SqliteDb*)cmdInfo.objClientData; |
| 109 | *ppDb = p->db; |
| 110 | }else{ |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 111 | *ppDb = (sqlite3*)sqlite3TestTextToPtr(zA); |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 112 | } |
| 113 | return TCL_OK; |
| 114 | } |
| 115 | |
| 116 | |
drh | 2e66f0b | 2005-04-28 17:18:48 +0000 | [diff] [blame] | 117 | const char *sqlite3TestErrorName(int rc){ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 118 | const char *zName = 0; |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 119 | switch( rc ){ |
| 120 | case SQLITE_OK: zName = "SQLITE_OK"; break; |
| 121 | case SQLITE_ERROR: zName = "SQLITE_ERROR"; break; |
| 122 | case SQLITE_INTERNAL: zName = "SQLITE_INTERNAL"; break; |
| 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; |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 127 | case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break; |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 128 | case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break; |
| 129 | case SQLITE_READONLY: zName = "SQLITE_READONLY"; break; |
| 130 | case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break; |
| 131 | case SQLITE_IOERR: zName = "SQLITE_IOERR"; break; |
| 132 | case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break; |
| 133 | case SQLITE_NOTFOUND: zName = "SQLITE_NOTFOUND"; break; |
| 134 | case SQLITE_FULL: zName = "SQLITE_FULL"; break; |
| 135 | case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break; |
| 136 | case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break; |
| 137 | case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break; |
| 138 | case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break; |
| 139 | case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break; |
| 140 | case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break; |
| 141 | case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break; |
| 142 | case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break; |
| 143 | case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break; |
| 144 | case SQLITE_AUTH: zName = "SQLITE_AUTH"; break; |
| 145 | case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break; |
| 146 | case SQLITE_RANGE: zName = "SQLITE_RANGE"; break; |
| 147 | case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break; |
| 148 | case SQLITE_ROW: zName = "SQLITE_ROW"; break; |
| 149 | case SQLITE_DONE: zName = "SQLITE_DONE"; break; |
| 150 | case SQLITE_IOERR_READ: zName = "SQLITE_IOERR_READ"; break; |
| 151 | case SQLITE_IOERR_SHORT_READ: zName = "SQLITE_IOERR_SHORT_READ"; break; |
| 152 | case SQLITE_IOERR_WRITE: zName = "SQLITE_IOERR_WRITE"; break; |
| 153 | case SQLITE_IOERR_FSYNC: zName = "SQLITE_IOERR_FSYNC"; break; |
| 154 | case SQLITE_IOERR_DIR_FSYNC: zName = "SQLITE_IOERR_DIR_FSYNC"; break; |
| 155 | case SQLITE_IOERR_TRUNCATE: zName = "SQLITE_IOERR_TRUNCATE"; break; |
| 156 | case SQLITE_IOERR_FSTAT: zName = "SQLITE_IOERR_FSTAT"; break; |
| 157 | case SQLITE_IOERR_UNLOCK: zName = "SQLITE_IOERR_UNLOCK"; break; |
| 158 | case SQLITE_IOERR_RDLOCK: zName = "SQLITE_IOERR_RDLOCK"; break; |
| 159 | case SQLITE_IOERR_DELETE: zName = "SQLITE_IOERR_DELETE"; break; |
| 160 | case SQLITE_IOERR_BLOCKED: zName = "SQLITE_IOERR_BLOCKED"; break; |
| 161 | case SQLITE_IOERR_NOMEM: zName = "SQLITE_IOERR_NOMEM"; break; |
| 162 | case SQLITE_IOERR_ACCESS: zName = "SQLITE_IOERR_ACCESS"; break; |
| 163 | case SQLITE_IOERR_CHECKRESERVEDLOCK: |
| 164 | zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break; |
| 165 | case SQLITE_IOERR_LOCK: zName = "SQLITE_IOERR_LOCK"; break; |
dan | 133d7da | 2011-05-17 15:56:16 +0000 | [diff] [blame] | 166 | case SQLITE_CORRUPT_VTAB: zName = "SQLITE_CORRUPT_VTAB"; break; |
dan | 4edc6bf | 2011-05-10 17:31:29 +0000 | [diff] [blame] | 167 | case SQLITE_READONLY_RECOVERY: zName = "SQLITE_READONLY_RECOVERY"; break; |
| 168 | case SQLITE_READONLY_CANTLOCK: zName = "SQLITE_READONLY_CANTLOCK"; break; |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 169 | default: zName = "SQLITE_Unknown"; break; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 170 | } |
| 171 | return zName; |
| 172 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 173 | #define t1ErrorName sqlite3TestErrorName |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 174 | |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 175 | /* |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 176 | ** Convert an sqlite3_stmt* into an sqlite3*. This depends on the |
| 177 | ** fact that the sqlite3* is the first field in the Vdbe structure. |
| 178 | */ |
drh | 51942bc | 2005-06-12 22:01:42 +0000 | [diff] [blame] | 179 | #define StmtToDb(X) sqlite3_db_handle(X) |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 180 | |
| 181 | /* |
| 182 | ** Check a return value to make sure it agrees with the results |
| 183 | ** from sqlite3_errcode. |
| 184 | */ |
| 185 | int sqlite3TestErrCode(Tcl_Interp *interp, sqlite3 *db, int rc){ |
drh | b8613ab | 2009-01-19 17:40:12 +0000 | [diff] [blame] | 186 | if( sqlite3_threadsafe()==0 && rc!=SQLITE_MISUSE && rc!=SQLITE_OK |
| 187 | && sqlite3_errcode(db)!=rc ){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 188 | char zBuf[200]; |
| 189 | int r2 = sqlite3_errcode(db); |
| 190 | sprintf(zBuf, "error code %s (%d) does not match sqlite3_errcode %s (%d)", |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 191 | t1ErrorName(rc), rc, t1ErrorName(r2), r2); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 192 | Tcl_ResetResult(interp); |
| 193 | Tcl_AppendResult(interp, zBuf, 0); |
| 194 | return 1; |
| 195 | } |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | /* |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 200 | ** Decode a pointer to an sqlite3_stmt object. |
| 201 | */ |
| 202 | static int getStmtPointer( |
| 203 | Tcl_Interp *interp, |
| 204 | const char *zArg, |
| 205 | sqlite3_stmt **ppStmt |
| 206 | ){ |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 207 | *ppStmt = (sqlite3_stmt*)sqlite3TestTextToPtr(zArg); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 208 | return TCL_OK; |
| 209 | } |
| 210 | |
| 211 | /* |
drh | 7d8085a | 2003-04-26 13:19:38 +0000 | [diff] [blame] | 212 | ** Generate a text representation of a pointer that can be understood |
| 213 | ** by the getDbPointer and getVmPointer routines above. |
| 214 | ** |
| 215 | ** The problem is, on some machines (Solaris) if you do a printf with |
| 216 | ** "%p" you cannot turn around and do a scanf with the same "%p" and |
| 217 | ** get your pointer back. You have to prepend a "0x" before it will |
| 218 | ** work. Or at least that is what is reported to me (drh). But this |
| 219 | ** behavior varies from machine to machine. The solution used her is |
| 220 | ** to test the string right after it is generated to see if it can be |
| 221 | ** understood by scanf, and if not, try prepending an "0x" to see if |
| 222 | ** that helps. If nothing works, a fatal error is generated. |
| 223 | */ |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 224 | int sqlite3TestMakePointerStr(Tcl_Interp *interp, char *zPtr, void *p){ |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 225 | sqlite3_snprintf(100, zPtr, "%p", p); |
drh | 7d8085a | 2003-04-26 13:19:38 +0000 | [diff] [blame] | 226 | return TCL_OK; |
| 227 | } |
| 228 | |
| 229 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 230 | ** The callback routine for sqlite3_exec_printf(). |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 231 | */ |
| 232 | static int exec_printf_cb(void *pArg, int argc, char **argv, char **name){ |
| 233 | Tcl_DString *str = (Tcl_DString*)pArg; |
| 234 | int i; |
| 235 | |
| 236 | if( Tcl_DStringLength(str)==0 ){ |
| 237 | for(i=0; i<argc; i++){ |
| 238 | Tcl_DStringAppendElement(str, name[i] ? name[i] : "NULL"); |
| 239 | } |
| 240 | } |
| 241 | for(i=0; i<argc; i++){ |
| 242 | Tcl_DStringAppendElement(str, argv[i] ? argv[i] : "NULL"); |
| 243 | } |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | /* |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 248 | ** The I/O tracing callback. |
| 249 | */ |
shane | afdd23a | 2008-05-29 02:57:47 +0000 | [diff] [blame] | 250 | #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 251 | static FILE *iotrace_file = 0; |
| 252 | static void io_trace_callback(const char *zFormat, ...){ |
| 253 | va_list ap; |
| 254 | va_start(ap, zFormat); |
| 255 | vfprintf(iotrace_file, zFormat, ap); |
| 256 | va_end(ap); |
| 257 | fflush(iotrace_file); |
| 258 | } |
shane | afdd23a | 2008-05-29 02:57:47 +0000 | [diff] [blame] | 259 | #endif |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 260 | |
| 261 | /* |
| 262 | ** Usage: io_trace FILENAME |
| 263 | ** |
| 264 | ** Turn I/O tracing on or off. If FILENAME is not an empty string, |
| 265 | ** I/O tracing begins going into FILENAME. If FILENAME is an empty |
| 266 | ** string, I/O tracing is turned off. |
| 267 | */ |
| 268 | static int test_io_trace( |
| 269 | void *NotUsed, |
| 270 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 271 | int argc, /* Number of arguments */ |
| 272 | char **argv /* Text of each argument */ |
| 273 | ){ |
danielk1977 | 286d2f4 | 2008-05-05 11:33:47 +0000 | [diff] [blame] | 274 | #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 275 | if( argc!=2 ){ |
| 276 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 277 | " FILENAME\"", 0); |
| 278 | return TCL_ERROR; |
| 279 | } |
| 280 | if( iotrace_file ){ |
| 281 | if( iotrace_file!=stdout && iotrace_file!=stderr ){ |
| 282 | fclose(iotrace_file); |
| 283 | } |
| 284 | iotrace_file = 0; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 285 | sqlite3IoTrace = 0; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 286 | } |
| 287 | if( argv[1][0] ){ |
| 288 | if( strcmp(argv[1],"stdout")==0 ){ |
| 289 | iotrace_file = stdout; |
| 290 | }else if( strcmp(argv[1],"stderr")==0 ){ |
| 291 | iotrace_file = stderr; |
| 292 | }else{ |
| 293 | iotrace_file = fopen(argv[1], "w"); |
| 294 | } |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 295 | sqlite3IoTrace = io_trace_callback; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 296 | } |
danielk1977 | 286d2f4 | 2008-05-05 11:33:47 +0000 | [diff] [blame] | 297 | #endif |
| 298 | return TCL_OK; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | |
| 302 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 303 | ** Usage: sqlite3_exec_printf DB FORMAT STRING |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 304 | ** |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 305 | ** Invoke the sqlite3_exec_printf() interface using the open database |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 306 | ** DB. The SQL is the string FORMAT. The format string should contain |
| 307 | ** one %s or %q. STRING is the value inserted into %s or %q. |
| 308 | */ |
| 309 | static int test_exec_printf( |
| 310 | void *NotUsed, |
| 311 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 312 | int argc, /* Number of arguments */ |
| 313 | char **argv /* Text of each argument */ |
| 314 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 315 | sqlite3 *db; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 316 | Tcl_DString str; |
| 317 | int rc; |
| 318 | char *zErr = 0; |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 319 | char *zSql; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 320 | char zBuf[30]; |
| 321 | if( argc!=4 ){ |
| 322 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 323 | " DB FORMAT STRING", 0); |
| 324 | return TCL_ERROR; |
| 325 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 326 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 327 | Tcl_DStringInit(&str); |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 328 | zSql = sqlite3_mprintf(argv[2], argv[3]); |
| 329 | rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr); |
| 330 | sqlite3_free(zSql); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 331 | sprintf(zBuf, "%d", rc); |
| 332 | Tcl_AppendElement(interp, zBuf); |
| 333 | Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr); |
| 334 | Tcl_DStringFree(&str); |
danielk1977 | 926aab2 | 2006-06-27 07:34:40 +0000 | [diff] [blame] | 335 | if( zErr ) sqlite3_free(zErr); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 336 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 337 | return TCL_OK; |
| 338 | } |
| 339 | |
| 340 | /* |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 341 | ** Usage: sqlite3_exec_hex DB HEX |
| 342 | ** |
| 343 | ** Invoke the sqlite3_exec() on a string that is obtained by translating |
| 344 | ** HEX into ASCII. Most characters are translated as is. %HH becomes |
| 345 | ** a hex character. |
| 346 | */ |
| 347 | static int test_exec_hex( |
| 348 | void *NotUsed, |
| 349 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 350 | int argc, /* Number of arguments */ |
| 351 | char **argv /* Text of each argument */ |
| 352 | ){ |
| 353 | sqlite3 *db; |
| 354 | Tcl_DString str; |
| 355 | int rc, i, j; |
| 356 | char *zErr = 0; |
| 357 | char *zHex; |
| 358 | char zSql[500]; |
| 359 | char zBuf[30]; |
| 360 | if( argc!=3 ){ |
| 361 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 362 | " DB HEX", 0); |
| 363 | return TCL_ERROR; |
| 364 | } |
| 365 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 366 | zHex = argv[2]; |
| 367 | for(i=j=0; i<sizeof(zSql) && zHex[j]; i++, j++){ |
| 368 | if( zHex[j]=='%' && zHex[j+2] && zHex[j+2] ){ |
| 369 | zSql[i] = (testHexToInt(zHex[j+1])<<4) + testHexToInt(zHex[j+2]); |
| 370 | j += 2; |
| 371 | }else{ |
| 372 | zSql[i] = zHex[j]; |
| 373 | } |
| 374 | } |
| 375 | zSql[i] = 0; |
| 376 | Tcl_DStringInit(&str); |
| 377 | rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr); |
| 378 | sprintf(zBuf, "%d", rc); |
| 379 | Tcl_AppendElement(interp, zBuf); |
| 380 | Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr); |
| 381 | Tcl_DStringFree(&str); |
| 382 | if( zErr ) sqlite3_free(zErr); |
| 383 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 384 | return TCL_OK; |
| 385 | } |
| 386 | |
| 387 | /* |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 388 | ** Usage: db_enter DB |
| 389 | ** db_leave DB |
| 390 | ** |
| 391 | ** Enter or leave the mutex on a database connection. |
| 392 | */ |
| 393 | static int db_enter( |
| 394 | void *NotUsed, |
| 395 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 396 | int argc, /* Number of arguments */ |
| 397 | char **argv /* Text of each argument */ |
| 398 | ){ |
| 399 | sqlite3 *db; |
| 400 | if( argc!=2 ){ |
| 401 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 402 | " DB", 0); |
| 403 | return TCL_ERROR; |
| 404 | } |
| 405 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 406 | sqlite3_mutex_enter(db->mutex); |
| 407 | return TCL_OK; |
| 408 | } |
| 409 | static int db_leave( |
| 410 | void *NotUsed, |
| 411 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 412 | int argc, /* Number of arguments */ |
| 413 | char **argv /* Text of each argument */ |
| 414 | ){ |
| 415 | sqlite3 *db; |
| 416 | if( argc!=2 ){ |
| 417 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 418 | " DB", 0); |
| 419 | return TCL_ERROR; |
| 420 | } |
| 421 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 422 | sqlite3_mutex_leave(db->mutex); |
| 423 | return TCL_OK; |
| 424 | } |
| 425 | |
| 426 | /* |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 427 | ** Usage: sqlite3_exec DB SQL |
| 428 | ** |
| 429 | ** Invoke the sqlite3_exec interface using the open database DB |
| 430 | */ |
| 431 | static int test_exec( |
| 432 | void *NotUsed, |
| 433 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 434 | int argc, /* Number of arguments */ |
| 435 | char **argv /* Text of each argument */ |
| 436 | ){ |
| 437 | sqlite3 *db; |
| 438 | Tcl_DString str; |
| 439 | int rc; |
| 440 | char *zErr = 0; |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 441 | char *zSql; |
| 442 | int i, j; |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 443 | char zBuf[30]; |
| 444 | if( argc!=3 ){ |
| 445 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 446 | " DB SQL", 0); |
| 447 | return TCL_ERROR; |
| 448 | } |
| 449 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 450 | Tcl_DStringInit(&str); |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 451 | zSql = sqlite3_mprintf("%s", argv[2]); |
| 452 | for(i=j=0; zSql[i];){ |
| 453 | if( zSql[i]=='%' ){ |
| 454 | zSql[j++] = (testHexToInt(zSql[i+1])<<4) + testHexToInt(zSql[i+2]); |
| 455 | i += 3; |
| 456 | }else{ |
| 457 | zSql[j++] = zSql[i++]; |
| 458 | } |
| 459 | } |
| 460 | zSql[j] = 0; |
| 461 | rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr); |
| 462 | sqlite3_free(zSql); |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 463 | sprintf(zBuf, "%d", rc); |
| 464 | Tcl_AppendElement(interp, zBuf); |
| 465 | Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr); |
| 466 | Tcl_DStringFree(&str); |
| 467 | if( zErr ) sqlite3_free(zErr); |
| 468 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 469 | return TCL_OK; |
| 470 | } |
| 471 | |
| 472 | /* |
| 473 | ** Usage: sqlite3_exec_nr DB SQL |
| 474 | ** |
| 475 | ** Invoke the sqlite3_exec interface using the open database DB. Discard |
| 476 | ** all results |
| 477 | */ |
| 478 | static int test_exec_nr( |
| 479 | void *NotUsed, |
| 480 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 481 | int argc, /* Number of arguments */ |
| 482 | char **argv /* Text of each argument */ |
| 483 | ){ |
| 484 | sqlite3 *db; |
| 485 | int rc; |
| 486 | char *zErr = 0; |
| 487 | if( argc!=3 ){ |
| 488 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 489 | " DB SQL", 0); |
| 490 | return TCL_ERROR; |
| 491 | } |
| 492 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 493 | rc = sqlite3_exec(db, argv[2], 0, 0, &zErr); |
| 494 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 495 | return TCL_OK; |
| 496 | } |
| 497 | |
| 498 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 499 | ** Usage: sqlite3_mprintf_z_test SEPARATOR ARG0 ARG1 ... |
drh | d93d8a8 | 2003-06-16 03:08:18 +0000 | [diff] [blame] | 500 | ** |
drh | bc6160b | 2009-04-08 15:45:31 +0000 | [diff] [blame] | 501 | ** Test the %z format of sqlite_mprintf(). Use multiple mprintf() calls to |
drh | d93d8a8 | 2003-06-16 03:08:18 +0000 | [diff] [blame] | 502 | ** concatenate arg0 through argn using separator as the separator. |
| 503 | ** Return the result. |
| 504 | */ |
| 505 | static int test_mprintf_z( |
| 506 | void *NotUsed, |
| 507 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 508 | int argc, /* Number of arguments */ |
| 509 | char **argv /* Text of each argument */ |
| 510 | ){ |
| 511 | char *zResult = 0; |
| 512 | int i; |
| 513 | |
danielk1977 | ca0c897 | 2007-09-01 09:02:53 +0000 | [diff] [blame] | 514 | for(i=2; i<argc && (i==2 || zResult); i++){ |
drh | bc6160b | 2009-04-08 15:45:31 +0000 | [diff] [blame] | 515 | zResult = sqlite3_mprintf("%z%s%s", zResult, argv[1], argv[i]); |
drh | d93d8a8 | 2003-06-16 03:08:18 +0000 | [diff] [blame] | 516 | } |
| 517 | Tcl_AppendResult(interp, zResult, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 518 | sqlite3_free(zResult); |
drh | d93d8a8 | 2003-06-16 03:08:18 +0000 | [diff] [blame] | 519 | return TCL_OK; |
| 520 | } |
| 521 | |
| 522 | /* |
drh | 05a8298 | 2006-03-19 13:00:25 +0000 | [diff] [blame] | 523 | ** Usage: sqlite3_mprintf_n_test STRING |
| 524 | ** |
drh | bc6160b | 2009-04-08 15:45:31 +0000 | [diff] [blame] | 525 | ** Test the %n format of sqlite_mprintf(). Return the length of the |
drh | 05a8298 | 2006-03-19 13:00:25 +0000 | [diff] [blame] | 526 | ** input string. |
| 527 | */ |
| 528 | static int test_mprintf_n( |
| 529 | void *NotUsed, |
| 530 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 531 | int argc, /* Number of arguments */ |
| 532 | char **argv /* Text of each argument */ |
| 533 | ){ |
| 534 | char *zStr; |
| 535 | int n = 0; |
drh | bc6160b | 2009-04-08 15:45:31 +0000 | [diff] [blame] | 536 | zStr = sqlite3_mprintf("%s%n", argv[1], &n); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 537 | sqlite3_free(zStr); |
drh | 05a8298 | 2006-03-19 13:00:25 +0000 | [diff] [blame] | 538 | Tcl_SetObjResult(interp, Tcl_NewIntObj(n)); |
| 539 | return TCL_OK; |
| 540 | } |
| 541 | |
| 542 | /* |
drh | 6885390 | 2007-05-07 11:24:30 +0000 | [diff] [blame] | 543 | ** Usage: sqlite3_snprintf_int SIZE FORMAT INT |
| 544 | ** |
| 545 | ** Test the of sqlite3_snprintf() routine. SIZE is the size of the |
| 546 | ** output buffer in bytes. The maximum size is 100. FORMAT is the |
| 547 | ** format string. INT is a single integer argument. The FORMAT |
| 548 | ** string must require no more than this one integer argument. If |
| 549 | ** You pass in a format string that requires more than one argument, |
| 550 | ** bad things will happen. |
| 551 | */ |
| 552 | static int test_snprintf_int( |
| 553 | void *NotUsed, |
| 554 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 555 | int argc, /* Number of arguments */ |
| 556 | char **argv /* Text of each argument */ |
| 557 | ){ |
| 558 | char zStr[100]; |
| 559 | int n = atoi(argv[1]); |
drh | 6885390 | 2007-05-07 11:24:30 +0000 | [diff] [blame] | 560 | const char *zFormat = argv[2]; |
| 561 | int a1 = atoi(argv[3]); |
drh | daf276d | 2007-06-15 18:53:14 +0000 | [diff] [blame] | 562 | if( n>sizeof(zStr) ) n = sizeof(zStr); |
drh | 7694574 | 2008-02-19 18:29:07 +0000 | [diff] [blame] | 563 | sqlite3_snprintf(sizeof(zStr), zStr, "abcdefghijklmnopqrstuvwxyz"); |
drh | 6885390 | 2007-05-07 11:24:30 +0000 | [diff] [blame] | 564 | sqlite3_snprintf(n, zStr, zFormat, a1); |
| 565 | Tcl_AppendResult(interp, zStr, 0); |
| 566 | return TCL_OK; |
| 567 | } |
| 568 | |
shane | 8225f5a | 2008-07-31 02:05:04 +0000 | [diff] [blame] | 569 | #ifndef SQLITE_OMIT_GET_TABLE |
| 570 | |
drh | 6885390 | 2007-05-07 11:24:30 +0000 | [diff] [blame] | 571 | /* |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 572 | ** Usage: sqlite3_get_table_printf DB FORMAT STRING ?--no-counts? |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 573 | ** |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 574 | ** Invoke the sqlite3_get_table_printf() interface using the open database |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 575 | ** DB. The SQL is the string FORMAT. The format string should contain |
| 576 | ** one %s or %q. STRING is the value inserted into %s or %q. |
| 577 | */ |
| 578 | static int test_get_table_printf( |
| 579 | void *NotUsed, |
| 580 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 581 | int argc, /* Number of arguments */ |
| 582 | char **argv /* Text of each argument */ |
| 583 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 584 | sqlite3 *db; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 585 | Tcl_DString str; |
| 586 | int rc; |
| 587 | char *zErr = 0; |
| 588 | int nRow, nCol; |
| 589 | char **aResult; |
| 590 | int i; |
| 591 | char zBuf[30]; |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 592 | char *zSql; |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 593 | int resCount = -1; |
| 594 | if( argc==5 ){ |
| 595 | if( Tcl_GetInt(interp, argv[4], &resCount) ) return TCL_ERROR; |
| 596 | } |
| 597 | if( argc!=4 && argc!=5 ){ |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 598 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 599 | " DB FORMAT STRING ?COUNT?", 0); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 600 | return TCL_ERROR; |
| 601 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 602 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 603 | Tcl_DStringInit(&str); |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 604 | zSql = sqlite3_mprintf(argv[2],argv[3]); |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 605 | if( argc==5 ){ |
| 606 | rc = sqlite3_get_table(db, zSql, &aResult, 0, 0, &zErr); |
| 607 | }else{ |
| 608 | rc = sqlite3_get_table(db, zSql, &aResult, &nRow, &nCol, &zErr); |
| 609 | resCount = (nRow+1)*nCol; |
| 610 | } |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 611 | sqlite3_free(zSql); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 612 | sprintf(zBuf, "%d", rc); |
| 613 | Tcl_AppendElement(interp, zBuf); |
| 614 | if( rc==SQLITE_OK ){ |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 615 | if( argc==4 ){ |
| 616 | sprintf(zBuf, "%d", nRow); |
| 617 | Tcl_AppendElement(interp, zBuf); |
| 618 | sprintf(zBuf, "%d", nCol); |
| 619 | Tcl_AppendElement(interp, zBuf); |
| 620 | } |
| 621 | for(i=0; i<resCount; i++){ |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 622 | Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL"); |
| 623 | } |
| 624 | }else{ |
| 625 | Tcl_AppendElement(interp, zErr); |
| 626 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 627 | sqlite3_free_table(aResult); |
danielk1977 | 926aab2 | 2006-06-27 07:34:40 +0000 | [diff] [blame] | 628 | if( zErr ) sqlite3_free(zErr); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 629 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 630 | return TCL_OK; |
| 631 | } |
| 632 | |
shane | 8225f5a | 2008-07-31 02:05:04 +0000 | [diff] [blame] | 633 | #endif /* SQLITE_OMIT_GET_TABLE */ |
| 634 | |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 635 | |
| 636 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 637 | ** Usage: sqlite3_last_insert_rowid DB |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 638 | ** |
| 639 | ** Returns the integer ROWID of the most recent insert. |
| 640 | */ |
| 641 | static int test_last_rowid( |
| 642 | void *NotUsed, |
| 643 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 644 | int argc, /* Number of arguments */ |
| 645 | char **argv /* Text of each argument */ |
| 646 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 647 | sqlite3 *db; |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 648 | char zBuf[30]; |
| 649 | |
| 650 | if( argc!=2 ){ |
| 651 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", 0); |
| 652 | return TCL_ERROR; |
| 653 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 654 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 655 | sprintf(zBuf, "%lld", sqlite3_last_insert_rowid(db)); |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 656 | Tcl_AppendResult(interp, zBuf, 0); |
| 657 | return SQLITE_OK; |
| 658 | } |
| 659 | |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 660 | /* |
drh | 25d6543 | 2004-07-22 15:02:25 +0000 | [diff] [blame] | 661 | ** Usage: sqlite3_key DB KEY |
| 662 | ** |
| 663 | ** Set the codec key. |
| 664 | */ |
| 665 | static int test_key( |
| 666 | void *NotUsed, |
| 667 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 668 | int argc, /* Number of arguments */ |
| 669 | char **argv /* Text of each argument */ |
| 670 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 671 | sqlite3 *db; |
drh | 25d6543 | 2004-07-22 15:02:25 +0000 | [diff] [blame] | 672 | const char *zKey; |
| 673 | int nKey; |
| 674 | if( argc!=3 ){ |
| 675 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 676 | " FILENAME\"", 0); |
| 677 | return TCL_ERROR; |
| 678 | } |
| 679 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 680 | zKey = argv[2]; |
| 681 | nKey = strlen(zKey); |
| 682 | #ifdef SQLITE_HAS_CODEC |
| 683 | sqlite3_key(db, zKey, nKey); |
| 684 | #endif |
| 685 | return TCL_OK; |
| 686 | } |
| 687 | |
| 688 | /* |
| 689 | ** Usage: sqlite3_rekey DB KEY |
| 690 | ** |
| 691 | ** Change the codec key. |
| 692 | */ |
| 693 | static int test_rekey( |
| 694 | void *NotUsed, |
| 695 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 696 | int argc, /* Number of arguments */ |
| 697 | char **argv /* Text of each argument */ |
| 698 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 699 | sqlite3 *db; |
drh | 25d6543 | 2004-07-22 15:02:25 +0000 | [diff] [blame] | 700 | const char *zKey; |
| 701 | int nKey; |
| 702 | if( argc!=3 ){ |
| 703 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 704 | " FILENAME\"", 0); |
| 705 | return TCL_ERROR; |
| 706 | } |
| 707 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 708 | zKey = argv[2]; |
| 709 | nKey = strlen(zKey); |
| 710 | #ifdef SQLITE_HAS_CODEC |
| 711 | sqlite3_rekey(db, zKey, nKey); |
| 712 | #endif |
| 713 | return TCL_OK; |
| 714 | } |
| 715 | |
| 716 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 717 | ** Usage: sqlite3_close DB |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 718 | ** |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 719 | ** Closes the database opened by sqlite3_open. |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 720 | */ |
| 721 | static int sqlite_test_close( |
| 722 | void *NotUsed, |
| 723 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 724 | int argc, /* Number of arguments */ |
| 725 | char **argv /* Text of each argument */ |
| 726 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 727 | sqlite3 *db; |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 728 | int rc; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 729 | if( argc!=2 ){ |
| 730 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 731 | " FILENAME\"", 0); |
| 732 | return TCL_ERROR; |
| 733 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 734 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 735 | rc = sqlite3_close(db); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 736 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 737 | return TCL_OK; |
| 738 | } |
| 739 | |
| 740 | /* |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 741 | ** Implementation of the x_coalesce() function. |
| 742 | ** Return the first argument non-NULL argument. |
| 743 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 744 | static void t1_ifnullFunc( |
| 745 | sqlite3_context *context, |
| 746 | int argc, |
| 747 | sqlite3_value **argv |
| 748 | ){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 749 | int i; |
| 750 | for(i=0; i<argc; i++){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 751 | if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){ |
drh | 9310ef2 | 2007-04-27 17:16:20 +0000 | [diff] [blame] | 752 | int n = sqlite3_value_bytes(argv[i]); |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 753 | sqlite3_result_text(context, (char*)sqlite3_value_text(argv[i]), |
drh | 9310ef2 | 2007-04-27 17:16:20 +0000 | [diff] [blame] | 754 | n, SQLITE_TRANSIENT); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 755 | break; |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | /* |
drh | f031381 | 2006-09-04 15:53:53 +0000 | [diff] [blame] | 761 | ** These are test functions. hex8() interprets its argument as |
| 762 | ** UTF8 and returns a hex encoding. hex16le() interprets its argument |
| 763 | ** as UTF16le and returns a hex encoding. |
| 764 | */ |
| 765 | static void hex8Func(sqlite3_context *p, int argc, sqlite3_value **argv){ |
| 766 | const unsigned char *z; |
| 767 | int i; |
| 768 | char zBuf[200]; |
| 769 | z = sqlite3_value_text(argv[0]); |
| 770 | for(i=0; i<sizeof(zBuf)/2 - 2 && z[i]; i++){ |
| 771 | sprintf(&zBuf[i*2], "%02x", z[i]&0xff); |
| 772 | } |
| 773 | zBuf[i*2] = 0; |
| 774 | sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT); |
| 775 | } |
drh | af30469 | 2007-04-23 23:56:31 +0000 | [diff] [blame] | 776 | #ifndef SQLITE_OMIT_UTF16 |
drh | f031381 | 2006-09-04 15:53:53 +0000 | [diff] [blame] | 777 | static void hex16Func(sqlite3_context *p, int argc, sqlite3_value **argv){ |
| 778 | const unsigned short int *z; |
| 779 | int i; |
| 780 | char zBuf[400]; |
| 781 | z = sqlite3_value_text16(argv[0]); |
| 782 | for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){ |
| 783 | sprintf(&zBuf[i*4], "%04x", z[i]&0xff); |
| 784 | } |
| 785 | zBuf[i*4] = 0; |
| 786 | sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT); |
| 787 | } |
drh | af30469 | 2007-04-23 23:56:31 +0000 | [diff] [blame] | 788 | #endif |
drh | f031381 | 2006-09-04 15:53:53 +0000 | [diff] [blame] | 789 | |
| 790 | /* |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 791 | ** A structure into which to accumulate text. |
| 792 | */ |
| 793 | struct dstr { |
| 794 | int nAlloc; /* Space allocated */ |
| 795 | int nUsed; /* Space used */ |
| 796 | char *z; /* The space */ |
| 797 | }; |
| 798 | |
| 799 | /* |
| 800 | ** Append text to a dstr |
| 801 | */ |
| 802 | static void dstrAppend(struct dstr *p, const char *z, int divider){ |
| 803 | int n = strlen(z); |
| 804 | if( p->nUsed + n + 2 > p->nAlloc ){ |
| 805 | char *zNew; |
| 806 | p->nAlloc = p->nAlloc*2 + n + 200; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 807 | zNew = sqlite3_realloc(p->z, p->nAlloc); |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 808 | if( zNew==0 ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 809 | sqlite3_free(p->z); |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 810 | memset(p, 0, sizeof(*p)); |
| 811 | return; |
| 812 | } |
| 813 | p->z = zNew; |
| 814 | } |
| 815 | if( divider && p->nUsed>0 ){ |
| 816 | p->z[p->nUsed++] = divider; |
| 817 | } |
| 818 | memcpy(&p->z[p->nUsed], z, n+1); |
| 819 | p->nUsed += n; |
| 820 | } |
| 821 | |
| 822 | /* |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 823 | ** Invoked for each callback from sqlite3ExecFunc |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 824 | */ |
| 825 | static int execFuncCallback(void *pData, int argc, char **argv, char **NotUsed){ |
| 826 | struct dstr *p = (struct dstr*)pData; |
| 827 | int i; |
| 828 | for(i=0; i<argc; i++){ |
| 829 | if( argv[i]==0 ){ |
| 830 | dstrAppend(p, "NULL", ' '); |
| 831 | }else{ |
| 832 | dstrAppend(p, argv[i], ' '); |
| 833 | } |
| 834 | } |
| 835 | return 0; |
| 836 | } |
| 837 | |
| 838 | /* |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 839 | ** Implementation of the x_sqlite_exec() function. This function takes |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 840 | ** a single argument and attempts to execute that argument as SQL code. |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 841 | ** This is illegal and should set the SQLITE_MISUSE flag on the database. |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 842 | ** |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 843 | ** 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] | 844 | ** from within a function call. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 845 | ** |
| 846 | ** This routine simulates the effect of having two threads attempt to |
| 847 | ** use the same database at the same time. |
| 848 | */ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 849 | static void sqlite3ExecFunc( |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 850 | sqlite3_context *context, |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 851 | int argc, |
| 852 | sqlite3_value **argv |
| 853 | ){ |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 854 | struct dstr x; |
| 855 | memset(&x, 0, sizeof(x)); |
drh | 3752785 | 2006-03-16 16:19:56 +0000 | [diff] [blame] | 856 | (void)sqlite3_exec((sqlite3*)sqlite3_user_data(context), |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 857 | (char*)sqlite3_value_text(argv[0]), |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 858 | execFuncCallback, &x, 0); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 859 | sqlite3_result_text(context, x.z, x.nUsed, SQLITE_TRANSIENT); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 860 | sqlite3_free(x.z); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | /* |
danielk1977 | d726392 | 2007-02-05 14:21:47 +0000 | [diff] [blame] | 864 | ** Implementation of tkt2213func(), a scalar function that takes exactly |
| 865 | ** one argument. It has two interesting features: |
| 866 | ** |
| 867 | ** * It calls sqlite3_value_text() 3 times on the argument sqlite3_value*. |
| 868 | ** If the three pointers returned are not the same an SQL error is raised. |
| 869 | ** |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 870 | ** * Otherwise it returns a copy of the text representation of its |
danielk1977 | d726392 | 2007-02-05 14:21:47 +0000 | [diff] [blame] | 871 | ** argument in such a way as the VDBE representation is a Mem* cell |
| 872 | ** with the MEM_Term flag clear. |
| 873 | ** |
| 874 | ** Ticket #2213 can therefore be tested by evaluating the following |
| 875 | ** SQL expression: |
| 876 | ** |
| 877 | ** tkt2213func(tkt2213func('a string')); |
| 878 | */ |
| 879 | static void tkt2213Function( |
| 880 | sqlite3_context *context, |
| 881 | int argc, |
| 882 | sqlite3_value **argv |
| 883 | ){ |
| 884 | int nText; |
| 885 | unsigned char const *zText1; |
| 886 | unsigned char const *zText2; |
| 887 | unsigned char const *zText3; |
| 888 | |
| 889 | nText = sqlite3_value_bytes(argv[0]); |
| 890 | zText1 = sqlite3_value_text(argv[0]); |
| 891 | zText2 = sqlite3_value_text(argv[0]); |
| 892 | zText3 = sqlite3_value_text(argv[0]); |
| 893 | |
| 894 | if( zText1!=zText2 || zText2!=zText3 ){ |
| 895 | sqlite3_result_error(context, "tkt2213 is not fixed", -1); |
| 896 | }else{ |
| 897 | char *zCopy = (char *)sqlite3_malloc(nText); |
| 898 | memcpy(zCopy, zText1, nText); |
| 899 | sqlite3_result_text(context, zCopy, nText, sqlite3_free); |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | /* |
drh | 9310ef2 | 2007-04-27 17:16:20 +0000 | [diff] [blame] | 904 | ** The following SQL function takes 4 arguments. The 2nd and |
| 905 | ** 4th argument must be one of these strings: 'text', 'text16', |
| 906 | ** or 'blob' corresponding to API functions |
| 907 | ** |
| 908 | ** sqlite3_value_text() |
| 909 | ** sqlite3_value_text16() |
| 910 | ** sqlite3_value_blob() |
| 911 | ** |
| 912 | ** The third argument is a string, either 'bytes' or 'bytes16' or 'noop', |
| 913 | ** corresponding to APIs: |
| 914 | ** |
| 915 | ** sqlite3_value_bytes() |
| 916 | ** sqlite3_value_bytes16() |
| 917 | ** noop |
| 918 | ** |
| 919 | ** The APIs designated by the 2nd through 4th arguments are applied |
| 920 | ** to the first argument in order. If the pointers returned by the |
| 921 | ** second and fourth are different, this routine returns 1. Otherwise, |
| 922 | ** this routine returns 0. |
| 923 | ** |
| 924 | ** This function is used to test to see when returned pointers from |
| 925 | ** the _text(), _text16() and _blob() APIs become invalidated. |
| 926 | */ |
| 927 | static void ptrChngFunction( |
| 928 | sqlite3_context *context, |
| 929 | int argc, |
| 930 | sqlite3_value **argv |
| 931 | ){ |
| 932 | const void *p1, *p2; |
| 933 | const char *zCmd; |
| 934 | if( argc!=4 ) return; |
| 935 | zCmd = (const char*)sqlite3_value_text(argv[1]); |
| 936 | if( zCmd==0 ) return; |
| 937 | if( strcmp(zCmd,"text")==0 ){ |
| 938 | p1 = (const void*)sqlite3_value_text(argv[0]); |
| 939 | #ifndef SQLITE_OMIT_UTF16 |
| 940 | }else if( strcmp(zCmd, "text16")==0 ){ |
| 941 | p1 = (const void*)sqlite3_value_text16(argv[0]); |
| 942 | #endif |
| 943 | }else if( strcmp(zCmd, "blob")==0 ){ |
| 944 | p1 = (const void*)sqlite3_value_blob(argv[0]); |
| 945 | }else{ |
| 946 | return; |
| 947 | } |
| 948 | zCmd = (const char*)sqlite3_value_text(argv[2]); |
| 949 | if( zCmd==0 ) return; |
| 950 | if( strcmp(zCmd,"bytes")==0 ){ |
| 951 | sqlite3_value_bytes(argv[0]); |
| 952 | #ifndef SQLITE_OMIT_UTF16 |
| 953 | }else if( strcmp(zCmd, "bytes16")==0 ){ |
| 954 | sqlite3_value_bytes16(argv[0]); |
| 955 | #endif |
| 956 | }else if( strcmp(zCmd, "noop")==0 ){ |
| 957 | /* do nothing */ |
| 958 | }else{ |
| 959 | return; |
| 960 | } |
| 961 | zCmd = (const char*)sqlite3_value_text(argv[3]); |
| 962 | if( zCmd==0 ) return; |
| 963 | if( strcmp(zCmd,"text")==0 ){ |
| 964 | p2 = (const void*)sqlite3_value_text(argv[0]); |
| 965 | #ifndef SQLITE_OMIT_UTF16 |
| 966 | }else if( strcmp(zCmd, "text16")==0 ){ |
| 967 | p2 = (const void*)sqlite3_value_text16(argv[0]); |
| 968 | #endif |
| 969 | }else if( strcmp(zCmd, "blob")==0 ){ |
| 970 | p2 = (const void*)sqlite3_value_blob(argv[0]); |
| 971 | }else{ |
| 972 | return; |
| 973 | } |
| 974 | sqlite3_result_int(context, p1!=p2); |
| 975 | } |
| 976 | |
| 977 | |
| 978 | /* |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 979 | ** Usage: sqlite_test_create_function DB |
| 980 | ** |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 981 | ** Call the sqlite3_create_function API on the given database in order |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 982 | ** to create a function named "x_coalesce". This function does the same thing |
| 983 | ** as the "coalesce" function. This function also registers an SQL function |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 984 | ** named "x_sqlite_exec" that invokes sqlite3_exec(). Invoking sqlite3_exec() |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 985 | ** in this way is illegal recursion and should raise an SQLITE_MISUSE error. |
| 986 | ** The effect is similar to trying to use the same database connection from |
| 987 | ** two threads at the same time. |
| 988 | ** |
| 989 | ** The original motivation for this routine was to be able to call the |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 990 | ** sqlite3_create_function function while a query is in progress in order |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 991 | ** to test the SQLITE_MISUSE detection logic. |
| 992 | */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 993 | static int test_create_function( |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 994 | void *NotUsed, |
| 995 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 996 | int argc, /* Number of arguments */ |
| 997 | char **argv /* Text of each argument */ |
| 998 | ){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 999 | int rc; |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1000 | sqlite3 *db; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 1001 | |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1002 | if( argc!=2 ){ |
| 1003 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 1004 | " DB\"", 0); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1005 | return TCL_ERROR; |
| 1006 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 1007 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1008 | rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_ANY, 0, |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1009 | t1_ifnullFunc, 0, 0); |
drh | 235a818 | 2006-09-13 19:21:28 +0000 | [diff] [blame] | 1010 | if( rc==SQLITE_OK ){ |
| 1011 | rc = sqlite3_create_function(db, "hex8", 1, SQLITE_ANY, 0, |
| 1012 | hex8Func, 0, 0); |
| 1013 | } |
drh | af30469 | 2007-04-23 23:56:31 +0000 | [diff] [blame] | 1014 | #ifndef SQLITE_OMIT_UTF16 |
drh | 235a818 | 2006-09-13 19:21:28 +0000 | [diff] [blame] | 1015 | if( rc==SQLITE_OK ){ |
| 1016 | rc = sqlite3_create_function(db, "hex16", 1, SQLITE_ANY, 0, |
| 1017 | hex16Func, 0, 0); |
| 1018 | } |
drh | af30469 | 2007-04-23 23:56:31 +0000 | [diff] [blame] | 1019 | #endif |
danielk1977 | d726392 | 2007-02-05 14:21:47 +0000 | [diff] [blame] | 1020 | if( rc==SQLITE_OK ){ |
| 1021 | rc = sqlite3_create_function(db, "tkt2213func", 1, SQLITE_ANY, 0, |
| 1022 | tkt2213Function, 0, 0); |
| 1023 | } |
drh | 9310ef2 | 2007-04-27 17:16:20 +0000 | [diff] [blame] | 1024 | if( rc==SQLITE_OK ){ |
| 1025 | rc = sqlite3_create_function(db, "pointer_change", 4, SQLITE_ANY, 0, |
| 1026 | ptrChngFunction, 0, 0); |
| 1027 | } |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 1028 | |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 1029 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 1030 | /* Use the sqlite3_create_function16() API here. Mainly for fun, but also |
| 1031 | ** because it is not tested anywhere else. */ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1032 | if( rc==SQLITE_OK ){ |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 1033 | const void *zUtf16; |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1034 | sqlite3_value *pVal; |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 1035 | sqlite3_mutex_enter(db->mutex); |
| 1036 | pVal = sqlite3ValueNew(db); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1037 | sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1038 | zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 1039 | if( db->mallocFailed ){ |
| 1040 | rc = SQLITE_NOMEM; |
| 1041 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1042 | rc = sqlite3_create_function16(db, zUtf16, |
| 1043 | 1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0); |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 1044 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1045 | sqlite3ValueFree(pVal); |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 1046 | sqlite3_mutex_leave(db->mutex); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1047 | } |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 1048 | #endif |
| 1049 | |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1050 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1051 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1052 | return TCL_OK; |
| 1053 | } |
| 1054 | |
| 1055 | /* |
| 1056 | ** Routines to implement the x_count() aggregate function. |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1057 | ** |
| 1058 | ** x_count() counts the number of non-null arguments. But there are |
| 1059 | ** some twists for testing purposes. |
| 1060 | ** |
| 1061 | ** If the argument to x_count() is 40 then a UTF-8 error is reported |
| 1062 | ** on the step function. If x_count(41) is seen, then a UTF-16 error |
| 1063 | ** is reported on the step function. If the total count is 42, then |
| 1064 | ** a UTF-8 error is reported on the finalize function. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1065 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1066 | typedef struct t1CountCtx t1CountCtx; |
| 1067 | struct t1CountCtx { |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1068 | int n; |
| 1069 | }; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1070 | static void t1CountStep( |
| 1071 | sqlite3_context *context, |
| 1072 | int argc, |
| 1073 | sqlite3_value **argv |
| 1074 | ){ |
| 1075 | t1CountCtx *p; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1076 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 1077 | if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1078 | p->n++; |
| 1079 | } |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1080 | if( argc>0 ){ |
| 1081 | int v = sqlite3_value_int(argv[0]); |
| 1082 | if( v==40 ){ |
| 1083 | sqlite3_result_error(context, "value of 40 handed to x_count", -1); |
danielk1977 | a1686c9 | 2006-01-23 07:52:37 +0000 | [diff] [blame] | 1084 | #ifndef SQLITE_OMIT_UTF16 |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1085 | }else if( v==41 ){ |
| 1086 | const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0}; |
| 1087 | sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1); |
danielk1977 | a1686c9 | 2006-01-23 07:52:37 +0000 | [diff] [blame] | 1088 | #endif |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1089 | } |
| 1090 | } |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1091 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1092 | static void t1CountFinalize(sqlite3_context *context){ |
| 1093 | t1CountCtx *p; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1094 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1095 | if( p ){ |
| 1096 | if( p->n==42 ){ |
| 1097 | sqlite3_result_error(context, "x_count totals to 42", -1); |
| 1098 | }else{ |
| 1099 | sqlite3_result_int(context, p ? p->n : 0); |
| 1100 | } |
| 1101 | } |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
danielk1977 | c551288 | 2009-08-10 04:37:49 +0000 | [diff] [blame] | 1104 | #ifndef SQLITE_OMIT_DEPRECATED |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1105 | static void legacyCountStep( |
| 1106 | sqlite3_context *context, |
| 1107 | int argc, |
| 1108 | sqlite3_value **argv |
| 1109 | ){ |
| 1110 | /* no-op */ |
| 1111 | } |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 1112 | |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1113 | static void legacyCountFinalize(sqlite3_context *context){ |
| 1114 | sqlite3_result_int(context, sqlite3_aggregate_count(context)); |
| 1115 | } |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 1116 | #endif |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1117 | |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1118 | /* |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1119 | ** Usage: sqlite3_create_aggregate DB |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1120 | ** |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1121 | ** Call the sqlite3_create_function API on the given database in order |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1122 | ** to create a function named "x_count". This function is similar |
| 1123 | ** to the built-in count() function, with a few special quirks |
| 1124 | ** for testing the sqlite3_result_error() APIs. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1125 | ** |
| 1126 | ** The original motivation for this routine was to be able to call the |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1127 | ** sqlite3_create_aggregate function while a query is in progress in order |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1128 | ** to test the SQLITE_MISUSE detection logic. See misuse.test. |
| 1129 | ** |
| 1130 | ** This routine was later extended to test the use of sqlite3_result_error() |
| 1131 | ** within aggregate functions. |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1132 | ** |
| 1133 | ** Later: It is now also extended to register the aggregate function |
| 1134 | ** "legacy_count()" with the supplied database handle. This is used |
| 1135 | ** to test the deprecated sqlite3_aggregate_count() API. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1136 | */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1137 | static int test_create_aggregate( |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1138 | void *NotUsed, |
| 1139 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1140 | int argc, /* Number of arguments */ |
| 1141 | char **argv /* Text of each argument */ |
| 1142 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1143 | sqlite3 *db; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1144 | int rc; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1145 | if( argc!=2 ){ |
| 1146 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1147 | " FILENAME\"", 0); |
| 1148 | return TCL_ERROR; |
| 1149 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 1150 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1151 | rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0, |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1152 | t1CountStep,t1CountFinalize); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1153 | if( rc==SQLITE_OK ){ |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1154 | rc = sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0, |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1155 | t1CountStep,t1CountFinalize); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1156 | } |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 1157 | #ifndef SQLITE_OMIT_DEPRECATED |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1158 | if( rc==SQLITE_OK ){ |
| 1159 | rc = sqlite3_create_function(db, "legacy_count", 0, SQLITE_ANY, 0, 0, |
| 1160 | legacyCountStep, legacyCountFinalize |
| 1161 | ); |
| 1162 | } |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 1163 | #endif |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1164 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1165 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1166 | return TCL_OK; |
| 1167 | } |
| 1168 | |
| 1169 | |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 1170 | /* |
| 1171 | ** Usage: printf TEXT |
| 1172 | ** |
| 1173 | ** Send output to printf. Use this rather than puts to merge the output |
| 1174 | ** in the correct sequence with debugging printfs inserted into C code. |
| 1175 | ** Puts uses a separate buffer and debugging statements will be out of |
| 1176 | ** sequence if it is used. |
| 1177 | */ |
| 1178 | static int test_printf( |
| 1179 | void *NotUsed, |
| 1180 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1181 | int argc, /* Number of arguments */ |
| 1182 | char **argv /* Text of each argument */ |
| 1183 | ){ |
| 1184 | if( argc!=2 ){ |
| 1185 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1186 | " TEXT\"", 0); |
| 1187 | return TCL_ERROR; |
| 1188 | } |
| 1189 | printf("%s\n", argv[1]); |
| 1190 | return TCL_OK; |
| 1191 | } |
| 1192 | |
| 1193 | |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1194 | |
| 1195 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1196 | ** Usage: sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1197 | ** |
| 1198 | ** Call mprintf with three integer arguments |
| 1199 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1200 | static int sqlite3_mprintf_int( |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1201 | void *NotUsed, |
| 1202 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1203 | int argc, /* Number of arguments */ |
| 1204 | char **argv /* Text of each argument */ |
| 1205 | ){ |
| 1206 | int a[3], i; |
| 1207 | char *z; |
| 1208 | if( argc!=5 ){ |
| 1209 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1210 | " FORMAT INT INT INT\"", 0); |
| 1211 | return TCL_ERROR; |
| 1212 | } |
| 1213 | for(i=2; i<5; i++){ |
| 1214 | if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; |
| 1215 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1216 | z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1217 | Tcl_AppendResult(interp, z, 0); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1218 | sqlite3_free(z); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1219 | return TCL_OK; |
| 1220 | } |
| 1221 | |
| 1222 | /* |
drh | e970767 | 2004-06-25 01:10:48 +0000 | [diff] [blame] | 1223 | ** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER |
| 1224 | ** |
| 1225 | ** Call mprintf with three 64-bit integer arguments |
| 1226 | */ |
| 1227 | static int sqlite3_mprintf_int64( |
| 1228 | void *NotUsed, |
| 1229 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1230 | int argc, /* Number of arguments */ |
| 1231 | char **argv /* Text of each argument */ |
| 1232 | ){ |
| 1233 | int i; |
| 1234 | sqlite_int64 a[3]; |
| 1235 | char *z; |
| 1236 | if( argc!=5 ){ |
| 1237 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1238 | " FORMAT INT INT INT\"", 0); |
| 1239 | return TCL_ERROR; |
| 1240 | } |
| 1241 | for(i=2; i<5; i++){ |
shaneh | 5f1d6b6 | 2010-09-30 16:51:25 +0000 | [diff] [blame] | 1242 | if( sqlite3Atoi64(argv[i], &a[i-2], 1000000, SQLITE_UTF8) ){ |
drh | e970767 | 2004-06-25 01:10:48 +0000 | [diff] [blame] | 1243 | Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", 0); |
| 1244 | return TCL_ERROR; |
| 1245 | } |
| 1246 | } |
| 1247 | z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]); |
| 1248 | Tcl_AppendResult(interp, z, 0); |
| 1249 | sqlite3_free(z); |
| 1250 | return TCL_OK; |
| 1251 | } |
| 1252 | |
| 1253 | /* |
drh | c5cad1e | 2009-02-01 00:21:09 +0000 | [diff] [blame] | 1254 | ** Usage: sqlite3_mprintf_long FORMAT INTEGER INTEGER INTEGER |
| 1255 | ** |
| 1256 | ** Call mprintf with three long integer arguments. This might be the |
| 1257 | ** same as sqlite3_mprintf_int or sqlite3_mprintf_int64, depending on |
| 1258 | ** platform. |
| 1259 | */ |
| 1260 | static int sqlite3_mprintf_long( |
| 1261 | void *NotUsed, |
| 1262 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1263 | int argc, /* Number of arguments */ |
| 1264 | char **argv /* Text of each argument */ |
| 1265 | ){ |
| 1266 | int i; |
| 1267 | long int a[3]; |
| 1268 | int b[3]; |
| 1269 | char *z; |
| 1270 | if( argc!=5 ){ |
| 1271 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1272 | " FORMAT INT INT INT\"", 0); |
| 1273 | return TCL_ERROR; |
| 1274 | } |
| 1275 | for(i=2; i<5; i++){ |
| 1276 | if( Tcl_GetInt(interp, argv[i], &b[i-2]) ) return TCL_ERROR; |
| 1277 | a[i-2] = (long int)b[i-2]; |
drh | 7ed0cae | 2009-02-03 16:25:47 +0000 | [diff] [blame] | 1278 | a[i-2] &= (((u64)1)<<(sizeof(int)*8))-1; |
drh | c5cad1e | 2009-02-01 00:21:09 +0000 | [diff] [blame] | 1279 | } |
| 1280 | z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]); |
| 1281 | Tcl_AppendResult(interp, z, 0); |
| 1282 | sqlite3_free(z); |
| 1283 | return TCL_OK; |
| 1284 | } |
| 1285 | |
| 1286 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1287 | ** Usage: sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1288 | ** |
| 1289 | ** Call mprintf with two integer arguments and one string argument |
| 1290 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1291 | static int sqlite3_mprintf_str( |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1292 | void *NotUsed, |
| 1293 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1294 | int argc, /* Number of arguments */ |
| 1295 | char **argv /* Text of each argument */ |
| 1296 | ){ |
| 1297 | int a[3], i; |
| 1298 | char *z; |
chw | f220b24 | 2002-06-16 04:54:28 +0000 | [diff] [blame] | 1299 | if( argc<4 || argc>5 ){ |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1300 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
chw | f220b24 | 2002-06-16 04:54:28 +0000 | [diff] [blame] | 1301 | " FORMAT INT INT ?STRING?\"", 0); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1302 | return TCL_ERROR; |
| 1303 | } |
| 1304 | for(i=2; i<4; i++){ |
| 1305 | if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; |
| 1306 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1307 | 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] | 1308 | Tcl_AppendResult(interp, z, 0); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1309 | sqlite3_free(z); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1310 | return TCL_OK; |
| 1311 | } |
| 1312 | |
| 1313 | /* |
drh | b3738b6 | 2007-03-31 15:02:49 +0000 | [diff] [blame] | 1314 | ** Usage: sqlite3_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING |
| 1315 | ** |
| 1316 | ** Call mprintf with two integer arguments and one string argument |
| 1317 | */ |
| 1318 | static int sqlite3_snprintf_str( |
| 1319 | void *NotUsed, |
| 1320 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1321 | int argc, /* Number of arguments */ |
| 1322 | char **argv /* Text of each argument */ |
| 1323 | ){ |
| 1324 | int a[3], i; |
| 1325 | int n; |
| 1326 | char *z; |
| 1327 | if( argc<5 || argc>6 ){ |
| 1328 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1329 | " INT FORMAT INT INT ?STRING?\"", 0); |
| 1330 | return TCL_ERROR; |
| 1331 | } |
| 1332 | if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR; |
| 1333 | if( n<0 ){ |
| 1334 | Tcl_AppendResult(interp, "N must be non-negative", 0); |
| 1335 | return TCL_ERROR; |
| 1336 | } |
| 1337 | for(i=3; i<5; i++){ |
| 1338 | if( Tcl_GetInt(interp, argv[i], &a[i-3]) ) return TCL_ERROR; |
| 1339 | } |
| 1340 | z = sqlite3_malloc( n+1 ); |
| 1341 | sqlite3_snprintf(n, z, argv[2], a[0], a[1], argc>4 ? argv[5] : NULL); |
| 1342 | Tcl_AppendResult(interp, z, 0); |
| 1343 | sqlite3_free(z); |
| 1344 | return TCL_OK; |
| 1345 | } |
| 1346 | |
| 1347 | /* |
drh | 6378285 | 2005-08-30 19:30:59 +0000 | [diff] [blame] | 1348 | ** Usage: sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1349 | ** |
| 1350 | ** Call mprintf with two integer arguments and one double argument |
| 1351 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1352 | static int sqlite3_mprintf_double( |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1353 | void *NotUsed, |
| 1354 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1355 | int argc, /* Number of arguments */ |
| 1356 | char **argv /* Text of each argument */ |
| 1357 | ){ |
| 1358 | int a[3], i; |
| 1359 | double r; |
| 1360 | char *z; |
| 1361 | if( argc!=5 ){ |
| 1362 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
drh | 6378285 | 2005-08-30 19:30:59 +0000 | [diff] [blame] | 1363 | " FORMAT INT INT DOUBLE\"", 0); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1364 | return TCL_ERROR; |
| 1365 | } |
| 1366 | for(i=2; i<4; i++){ |
| 1367 | if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; |
| 1368 | } |
| 1369 | if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1370 | z = sqlite3_mprintf(argv[1], a[0], a[1], r); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1371 | Tcl_AppendResult(interp, z, 0); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1372 | sqlite3_free(z); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1373 | return TCL_OK; |
| 1374 | } |
| 1375 | |
| 1376 | /* |
drh | 6378285 | 2005-08-30 19:30:59 +0000 | [diff] [blame] | 1377 | ** Usage: sqlite3_mprintf_scaled FORMAT DOUBLE DOUBLE |
drh | b621c23 | 2004-02-21 19:41:04 +0000 | [diff] [blame] | 1378 | ** |
| 1379 | ** Call mprintf with a single double argument which is the product of the |
| 1380 | ** two arguments given above. This is used to generate overflow and underflow |
| 1381 | ** doubles to test that they are converted properly. |
| 1382 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1383 | static int sqlite3_mprintf_scaled( |
drh | b621c23 | 2004-02-21 19:41:04 +0000 | [diff] [blame] | 1384 | void *NotUsed, |
| 1385 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1386 | int argc, /* Number of arguments */ |
| 1387 | char **argv /* Text of each argument */ |
| 1388 | ){ |
| 1389 | int i; |
| 1390 | double r[2]; |
| 1391 | char *z; |
| 1392 | if( argc!=4 ){ |
| 1393 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1394 | " FORMAT DOUBLE DOUBLE\"", 0); |
| 1395 | return TCL_ERROR; |
| 1396 | } |
| 1397 | for(i=2; i<4; i++){ |
| 1398 | if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR; |
| 1399 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1400 | z = sqlite3_mprintf(argv[1], r[0]*r[1]); |
drh | b621c23 | 2004-02-21 19:41:04 +0000 | [diff] [blame] | 1401 | Tcl_AppendResult(interp, z, 0); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1402 | sqlite3_free(z); |
drh | b621c23 | 2004-02-21 19:41:04 +0000 | [diff] [blame] | 1403 | return TCL_OK; |
| 1404 | } |
| 1405 | |
| 1406 | /* |
drh | e29b1a0 | 2004-07-17 21:56:09 +0000 | [diff] [blame] | 1407 | ** Usage: sqlite3_mprintf_stronly FORMAT STRING |
| 1408 | ** |
| 1409 | ** Call mprintf with a single double argument which is the product of the |
| 1410 | ** two arguments given above. This is used to generate overflow and underflow |
| 1411 | ** doubles to test that they are converted properly. |
| 1412 | */ |
| 1413 | static int sqlite3_mprintf_stronly( |
| 1414 | void *NotUsed, |
| 1415 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1416 | int argc, /* Number of arguments */ |
| 1417 | char **argv /* Text of each argument */ |
| 1418 | ){ |
| 1419 | char *z; |
| 1420 | if( argc!=3 ){ |
| 1421 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1422 | " FORMAT STRING\"", 0); |
| 1423 | return TCL_ERROR; |
| 1424 | } |
| 1425 | z = sqlite3_mprintf(argv[1], argv[2]); |
| 1426 | Tcl_AppendResult(interp, z, 0); |
| 1427 | sqlite3_free(z); |
| 1428 | return TCL_OK; |
| 1429 | } |
| 1430 | |
| 1431 | /* |
drh | 6378285 | 2005-08-30 19:30:59 +0000 | [diff] [blame] | 1432 | ** Usage: sqlite3_mprintf_hexdouble FORMAT HEX |
| 1433 | ** |
| 1434 | ** Call mprintf with a single double argument which is derived from the |
| 1435 | ** hexadecimal encoding of an IEEE double. |
| 1436 | */ |
| 1437 | static int sqlite3_mprintf_hexdouble( |
| 1438 | void *NotUsed, |
| 1439 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1440 | int argc, /* Number of arguments */ |
| 1441 | char **argv /* Text of each argument */ |
| 1442 | ){ |
| 1443 | char *z; |
| 1444 | double r; |
shane | 5e73db3 | 2008-07-08 03:04:58 +0000 | [diff] [blame] | 1445 | unsigned int x1, x2; |
| 1446 | sqlite_uint64 d; |
drh | 6378285 | 2005-08-30 19:30:59 +0000 | [diff] [blame] | 1447 | if( argc!=3 ){ |
| 1448 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1449 | " FORMAT STRING\"", 0); |
| 1450 | return TCL_ERROR; |
| 1451 | } |
| 1452 | if( sscanf(argv[2], "%08x%08x", &x2, &x1)!=2 ){ |
| 1453 | Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", 0); |
| 1454 | return TCL_ERROR; |
| 1455 | } |
| 1456 | d = x2; |
| 1457 | d = (d<<32) + x1; |
| 1458 | memcpy(&r, &d, sizeof(r)); |
| 1459 | z = sqlite3_mprintf(argv[1], r); |
| 1460 | Tcl_AppendResult(interp, z, 0); |
| 1461 | sqlite3_free(z); |
| 1462 | return TCL_OK; |
| 1463 | } |
| 1464 | |
| 1465 | /* |
danielk1977 | c1def3e | 2008-08-30 13:25:10 +0000 | [diff] [blame] | 1466 | ** Usage: sqlite3_enable_shared_cache ?BOOLEAN? |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1467 | ** |
| 1468 | */ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1469 | #if !defined(SQLITE_OMIT_SHARED_CACHE) |
| 1470 | static int test_enable_shared( |
danielk1977 | 5262282 | 2006-01-09 09:59:49 +0000 | [diff] [blame] | 1471 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1472 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1473 | int objc, /* Number of arguments */ |
| 1474 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1475 | ){ |
| 1476 | int rc; |
| 1477 | int enable; |
danielk1977 | 5262282 | 2006-01-09 09:59:49 +0000 | [diff] [blame] | 1478 | int ret = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1479 | |
danielk1977 | c1def3e | 2008-08-30 13:25:10 +0000 | [diff] [blame] | 1480 | if( objc!=2 && objc!=1 ){ |
| 1481 | Tcl_WrongNumArgs(interp, 1, objv, "?BOOLEAN?"); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1482 | return TCL_ERROR; |
| 1483 | } |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 1484 | ret = sqlite3GlobalConfig.sharedCacheEnabled; |
danielk1977 | c1def3e | 2008-08-30 13:25:10 +0000 | [diff] [blame] | 1485 | |
| 1486 | if( objc==2 ){ |
| 1487 | if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ){ |
| 1488 | return TCL_ERROR; |
| 1489 | } |
| 1490 | rc = sqlite3_enable_shared_cache(enable); |
| 1491 | if( rc!=SQLITE_OK ){ |
| 1492 | Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC); |
| 1493 | return TCL_ERROR; |
| 1494 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1495 | } |
danielk1977 | 5262282 | 2006-01-09 09:59:49 +0000 | [diff] [blame] | 1496 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(ret)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1497 | return TCL_OK; |
| 1498 | } |
| 1499 | #endif |
| 1500 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1501 | |
| 1502 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1503 | /* |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1504 | ** Usage: sqlite3_extended_result_codes DB BOOLEAN |
| 1505 | ** |
| 1506 | */ |
| 1507 | static int test_extended_result_codes( |
| 1508 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 1509 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1510 | int objc, /* Number of arguments */ |
| 1511 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1512 | ){ |
| 1513 | int enable; |
| 1514 | sqlite3 *db; |
| 1515 | |
| 1516 | if( objc!=3 ){ |
| 1517 | Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN"); |
| 1518 | return TCL_ERROR; |
| 1519 | } |
| 1520 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 1521 | if( Tcl_GetBooleanFromObj(interp, objv[2], &enable) ) return TCL_ERROR; |
| 1522 | sqlite3_extended_result_codes(db, enable); |
| 1523 | return TCL_OK; |
| 1524 | } |
| 1525 | |
| 1526 | /* |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 1527 | ** Usage: sqlite3_libversion_number |
| 1528 | ** |
| 1529 | */ |
| 1530 | static int test_libversion_number( |
| 1531 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 1532 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1533 | int objc, /* Number of arguments */ |
| 1534 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1535 | ){ |
| 1536 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_libversion_number())); |
| 1537 | return TCL_OK; |
| 1538 | } |
| 1539 | |
| 1540 | /* |
danielk1977 | deb802c | 2006-02-09 13:43:28 +0000 | [diff] [blame] | 1541 | ** Usage: sqlite3_table_column_metadata DB dbname tblname colname |
| 1542 | ** |
| 1543 | */ |
| 1544 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 1545 | static int test_table_column_metadata( |
| 1546 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 1547 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1548 | int objc, /* Number of arguments */ |
| 1549 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1550 | ){ |
| 1551 | sqlite3 *db; |
| 1552 | const char *zDb; |
| 1553 | const char *zTbl; |
| 1554 | const char *zCol; |
| 1555 | int rc; |
| 1556 | Tcl_Obj *pRet; |
| 1557 | |
| 1558 | const char *zDatatype; |
| 1559 | const char *zCollseq; |
| 1560 | int notnull; |
| 1561 | int primarykey; |
| 1562 | int autoincrement; |
| 1563 | |
| 1564 | if( objc!=5 ){ |
| 1565 | Tcl_WrongNumArgs(interp, 1, objv, "DB dbname tblname colname"); |
| 1566 | return TCL_ERROR; |
| 1567 | } |
| 1568 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 1569 | zDb = Tcl_GetString(objv[2]); |
| 1570 | zTbl = Tcl_GetString(objv[3]); |
| 1571 | zCol = Tcl_GetString(objv[4]); |
| 1572 | |
| 1573 | if( strlen(zDb)==0 ) zDb = 0; |
| 1574 | |
| 1575 | rc = sqlite3_table_column_metadata(db, zDb, zTbl, zCol, |
| 1576 | &zDatatype, &zCollseq, ¬null, &primarykey, &autoincrement); |
| 1577 | |
| 1578 | if( rc!=SQLITE_OK ){ |
| 1579 | Tcl_AppendResult(interp, sqlite3_errmsg(db), 0); |
| 1580 | return TCL_ERROR; |
| 1581 | } |
| 1582 | |
| 1583 | pRet = Tcl_NewObj(); |
| 1584 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zDatatype, -1)); |
| 1585 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zCollseq, -1)); |
| 1586 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(notnull)); |
| 1587 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(primarykey)); |
| 1588 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(autoincrement)); |
| 1589 | Tcl_SetObjResult(interp, pRet); |
| 1590 | |
| 1591 | return TCL_OK; |
| 1592 | } |
| 1593 | #endif |
| 1594 | |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1595 | #ifndef SQLITE_OMIT_INCRBLOB |
| 1596 | |
dan | 61c7f59 | 2010-10-26 18:42:52 +0000 | [diff] [blame] | 1597 | static int blobHandleFromObj( |
| 1598 | Tcl_Interp *interp, |
| 1599 | Tcl_Obj *pObj, |
| 1600 | sqlite3_blob **ppBlob |
| 1601 | ){ |
| 1602 | char *z; |
| 1603 | int n; |
| 1604 | |
| 1605 | z = Tcl_GetStringFromObj(pObj, &n); |
| 1606 | if( n==0 ){ |
| 1607 | *ppBlob = 0; |
| 1608 | }else{ |
| 1609 | int notUsed; |
| 1610 | Tcl_Channel channel; |
| 1611 | ClientData instanceData; |
| 1612 | |
| 1613 | channel = Tcl_GetChannel(interp, z, ¬Used); |
| 1614 | if( !channel ) return TCL_ERROR; |
| 1615 | |
| 1616 | Tcl_Flush(channel); |
| 1617 | Tcl_Seek(channel, 0, SEEK_SET); |
| 1618 | |
| 1619 | instanceData = Tcl_GetChannelInstanceData(channel); |
| 1620 | *ppBlob = *((sqlite3_blob **)instanceData); |
| 1621 | } |
| 1622 | |
| 1623 | return TCL_OK; |
| 1624 | } |
| 1625 | |
| 1626 | /* |
| 1627 | ** sqlite3_blob_bytes CHANNEL |
| 1628 | */ |
| 1629 | static int test_blob_bytes( |
| 1630 | ClientData clientData, /* Not used */ |
| 1631 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1632 | int objc, /* Number of arguments */ |
| 1633 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1634 | ){ |
| 1635 | sqlite3_blob *pBlob; |
| 1636 | int nByte; |
| 1637 | |
| 1638 | if( objc!=2 ){ |
| 1639 | Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL"); |
| 1640 | return TCL_ERROR; |
| 1641 | } |
| 1642 | |
| 1643 | if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR; |
| 1644 | nByte = sqlite3_blob_bytes(pBlob); |
| 1645 | Tcl_SetObjResult(interp, Tcl_NewIntObj(nByte)); |
| 1646 | |
| 1647 | return TCL_OK; |
| 1648 | } |
| 1649 | |
| 1650 | /* |
| 1651 | ** sqlite3_blob_close CHANNEL |
| 1652 | */ |
| 1653 | static int test_blob_close( |
| 1654 | ClientData clientData, /* Not used */ |
| 1655 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1656 | int objc, /* Number of arguments */ |
| 1657 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1658 | ){ |
| 1659 | sqlite3_blob *pBlob; |
dan | 61c7f59 | 2010-10-26 18:42:52 +0000 | [diff] [blame] | 1660 | |
| 1661 | if( objc!=2 ){ |
| 1662 | Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL"); |
| 1663 | return TCL_ERROR; |
| 1664 | } |
| 1665 | |
| 1666 | if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR; |
| 1667 | sqlite3_blob_close(pBlob); |
| 1668 | |
| 1669 | return TCL_OK; |
| 1670 | } |
| 1671 | |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1672 | /* |
| 1673 | ** sqlite3_blob_read CHANNEL OFFSET N |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1674 | ** |
| 1675 | ** This command is used to test the sqlite3_blob_read() in ways that |
| 1676 | ** the Tcl channel interface does not. The first argument should |
| 1677 | ** be the name of a valid channel created by the [incrblob] method |
| 1678 | ** of a database handle. This function calls sqlite3_blob_read() |
| 1679 | ** to read N bytes from offset OFFSET from the underlying SQLite |
| 1680 | ** blob handle. |
| 1681 | ** |
| 1682 | ** On success, a byte-array object containing the read data is |
| 1683 | ** returned. On failure, the interpreter result is set to the |
| 1684 | ** text representation of the returned error code (i.e. "SQLITE_NOMEM") |
| 1685 | ** and a Tcl exception is thrown. |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1686 | */ |
| 1687 | static int test_blob_read( |
| 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 | ){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1693 | sqlite3_blob *pBlob; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1694 | int nByte; |
| 1695 | int iOffset; |
dan | f1f22bc | 2010-10-27 19:08:26 +0000 | [diff] [blame] | 1696 | unsigned char *zBuf = 0; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1697 | int rc; |
| 1698 | |
| 1699 | if( objc!=4 ){ |
| 1700 | Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET N"); |
| 1701 | return TCL_ERROR; |
| 1702 | } |
| 1703 | |
dan | 61c7f59 | 2010-10-26 18:42:52 +0000 | [diff] [blame] | 1704 | if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR; |
| 1705 | if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset) |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1706 | || TCL_OK!=Tcl_GetIntFromObj(interp, objv[3], &nByte) |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1707 | ){ |
| 1708 | return TCL_ERROR; |
| 1709 | } |
| 1710 | |
dan | f1f22bc | 2010-10-27 19:08:26 +0000 | [diff] [blame] | 1711 | if( nByte>0 ){ |
| 1712 | zBuf = (unsigned char *)Tcl_Alloc(nByte); |
| 1713 | } |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1714 | rc = sqlite3_blob_read(pBlob, zBuf, nByte, iOffset); |
| 1715 | if( rc==SQLITE_OK ){ |
| 1716 | Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zBuf, nByte)); |
| 1717 | }else{ |
| 1718 | Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE); |
| 1719 | } |
| 1720 | Tcl_Free((char *)zBuf); |
| 1721 | |
| 1722 | return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR); |
| 1723 | } |
| 1724 | |
| 1725 | /* |
danielk1977 | a8b3018 | 2008-10-02 14:49:01 +0000 | [diff] [blame] | 1726 | ** sqlite3_blob_write CHANNEL OFFSET DATA ?NDATA? |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1727 | ** |
| 1728 | ** This command is used to test the sqlite3_blob_write() in ways that |
| 1729 | ** the Tcl channel interface does not. The first argument should |
| 1730 | ** be the name of a valid channel created by the [incrblob] method |
| 1731 | ** of a database handle. This function calls sqlite3_blob_write() |
| 1732 | ** to write the DATA byte-array to the underlying SQLite blob handle. |
| 1733 | ** at offset OFFSET. |
| 1734 | ** |
| 1735 | ** On success, an empty string is returned. On failure, the interpreter |
| 1736 | ** result is set to the text representation of the returned error code |
| 1737 | ** (i.e. "SQLITE_NOMEM") and a Tcl exception is thrown. |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1738 | */ |
| 1739 | static int test_blob_write( |
| 1740 | ClientData clientData, /* Not used */ |
| 1741 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1742 | int objc, /* Number of arguments */ |
| 1743 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1744 | ){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1745 | sqlite3_blob *pBlob; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1746 | int iOffset; |
| 1747 | int rc; |
| 1748 | |
| 1749 | unsigned char *zBuf; |
| 1750 | int nBuf; |
| 1751 | |
danielk1977 | a8b3018 | 2008-10-02 14:49:01 +0000 | [diff] [blame] | 1752 | if( objc!=4 && objc!=5 ){ |
| 1753 | Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET DATA ?NDATA?"); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1754 | return TCL_ERROR; |
| 1755 | } |
| 1756 | |
dan | 61c7f59 | 2010-10-26 18:42:52 +0000 | [diff] [blame] | 1757 | if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR; |
| 1758 | if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset) ){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1759 | return TCL_ERROR; |
| 1760 | } |
| 1761 | |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1762 | zBuf = Tcl_GetByteArrayFromObj(objv[3], &nBuf); |
danielk1977 | a8b3018 | 2008-10-02 14:49:01 +0000 | [diff] [blame] | 1763 | if( objc==5 && Tcl_GetIntFromObj(interp, objv[4], &nBuf) ){ |
| 1764 | return TCL_ERROR; |
| 1765 | } |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1766 | rc = sqlite3_blob_write(pBlob, zBuf, nBuf, iOffset); |
| 1767 | if( rc!=SQLITE_OK ){ |
| 1768 | Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE); |
| 1769 | } |
| 1770 | |
| 1771 | return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR); |
| 1772 | } |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 1773 | |
| 1774 | static int test_blob_reopen( |
| 1775 | ClientData clientData, /* Not used */ |
| 1776 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1777 | int objc, /* Number of arguments */ |
| 1778 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1779 | ){ |
| 1780 | Tcl_WideInt iRowid; |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 1781 | sqlite3_blob *pBlob; |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 1782 | int rc; |
| 1783 | |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 1784 | if( objc!=3 ){ |
| 1785 | Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL ROWID"); |
| 1786 | return TCL_ERROR; |
| 1787 | } |
| 1788 | |
dan | 61c7f59 | 2010-10-26 18:42:52 +0000 | [diff] [blame] | 1789 | if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR; |
| 1790 | if( Tcl_GetWideIntFromObj(interp, objv[2], &iRowid) ) return TCL_ERROR; |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 1791 | |
| 1792 | rc = sqlite3_blob_reopen(pBlob, iRowid); |
| 1793 | if( rc!=SQLITE_OK ){ |
| 1794 | Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE); |
| 1795 | } |
| 1796 | |
| 1797 | return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR); |
| 1798 | } |
| 1799 | |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1800 | #endif |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 1801 | |
danielk1977 | deb802c | 2006-02-09 13:43:28 +0000 | [diff] [blame] | 1802 | /* |
danielk1977 | a393c03 | 2007-05-07 14:58:53 +0000 | [diff] [blame] | 1803 | ** Usage: sqlite3_create_collation_v2 DB-HANDLE NAME CMP-PROC DEL-PROC |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1804 | ** |
| 1805 | ** This Tcl proc is used for testing the experimental |
danielk1977 | a393c03 | 2007-05-07 14:58:53 +0000 | [diff] [blame] | 1806 | ** sqlite3_create_collation_v2() interface. |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1807 | */ |
| 1808 | struct TestCollationX { |
| 1809 | Tcl_Interp *interp; |
| 1810 | Tcl_Obj *pCmp; |
| 1811 | Tcl_Obj *pDel; |
| 1812 | }; |
| 1813 | typedef struct TestCollationX TestCollationX; |
| 1814 | static void testCreateCollationDel(void *pCtx){ |
| 1815 | TestCollationX *p = (TestCollationX *)pCtx; |
| 1816 | |
| 1817 | int rc = Tcl_EvalObjEx(p->interp, p->pDel, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL); |
| 1818 | if( rc!=TCL_OK ){ |
| 1819 | Tcl_BackgroundError(p->interp); |
| 1820 | } |
| 1821 | |
| 1822 | Tcl_DecrRefCount(p->pCmp); |
| 1823 | Tcl_DecrRefCount(p->pDel); |
| 1824 | sqlite3_free((void *)p); |
| 1825 | } |
| 1826 | static int testCreateCollationCmp( |
| 1827 | void *pCtx, |
| 1828 | int nLeft, |
| 1829 | const void *zLeft, |
| 1830 | int nRight, |
| 1831 | const void *zRight |
| 1832 | ){ |
| 1833 | TestCollationX *p = (TestCollationX *)pCtx; |
| 1834 | Tcl_Obj *pScript = Tcl_DuplicateObj(p->pCmp); |
| 1835 | int iRes = 0; |
| 1836 | |
| 1837 | Tcl_IncrRefCount(pScript); |
| 1838 | Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zLeft, nLeft)); |
| 1839 | Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zRight,nRight)); |
| 1840 | |
| 1841 | if( TCL_OK!=Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL) |
| 1842 | || TCL_OK!=Tcl_GetIntFromObj(p->interp, Tcl_GetObjResult(p->interp), &iRes) |
| 1843 | ){ |
| 1844 | Tcl_BackgroundError(p->interp); |
| 1845 | } |
| 1846 | Tcl_DecrRefCount(pScript); |
| 1847 | |
| 1848 | return iRes; |
| 1849 | } |
danielk1977 | a393c03 | 2007-05-07 14:58:53 +0000 | [diff] [blame] | 1850 | static int test_create_collation_v2( |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1851 | ClientData clientData, /* Not used */ |
| 1852 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1853 | int objc, /* Number of arguments */ |
| 1854 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1855 | ){ |
| 1856 | TestCollationX *p; |
| 1857 | sqlite3 *db; |
drh | d55d57e | 2008-07-07 17:53:07 +0000 | [diff] [blame] | 1858 | int rc; |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1859 | |
| 1860 | if( objc!=5 ){ |
| 1861 | Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE NAME CMP-PROC DEL-PROC"); |
| 1862 | return TCL_ERROR; |
| 1863 | } |
| 1864 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 1865 | |
| 1866 | p = (TestCollationX *)sqlite3_malloc(sizeof(TestCollationX)); |
| 1867 | p->pCmp = objv[3]; |
| 1868 | p->pDel = objv[4]; |
| 1869 | p->interp = interp; |
| 1870 | Tcl_IncrRefCount(p->pCmp); |
| 1871 | Tcl_IncrRefCount(p->pDel); |
| 1872 | |
drh | d55d57e | 2008-07-07 17:53:07 +0000 | [diff] [blame] | 1873 | rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), 16, |
| 1874 | (void *)p, testCreateCollationCmp, testCreateCollationDel |
| 1875 | ); |
| 1876 | if( rc!=SQLITE_MISUSE ){ |
| 1877 | Tcl_AppendResult(interp, "sqlite3_create_collate_v2() failed to detect " |
| 1878 | "an invalid encoding", (char*)0); |
| 1879 | return TCL_ERROR; |
| 1880 | } |
| 1881 | rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), SQLITE_UTF8, |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1882 | (void *)p, testCreateCollationCmp, testCreateCollationDel |
| 1883 | ); |
| 1884 | return TCL_OK; |
| 1885 | } |
| 1886 | |
| 1887 | /* |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 1888 | ** USAGE: sqlite3_create_function_v2 DB NAME NARG ENC ?SWITCHES? |
| 1889 | ** |
| 1890 | ** Available switches are: |
| 1891 | ** |
| 1892 | ** -func SCRIPT |
| 1893 | ** -step SCRIPT |
| 1894 | ** -final SCRIPT |
| 1895 | ** -destroy SCRIPT |
| 1896 | */ |
| 1897 | typedef struct CreateFunctionV2 CreateFunctionV2; |
| 1898 | struct CreateFunctionV2 { |
| 1899 | Tcl_Interp *interp; |
| 1900 | Tcl_Obj *pFunc; /* Script for function invocation */ |
| 1901 | Tcl_Obj *pStep; /* Script for agg. step invocation */ |
| 1902 | Tcl_Obj *pFinal; /* Script for agg. finalization invocation */ |
| 1903 | Tcl_Obj *pDestroy; /* Destructor script */ |
| 1904 | }; |
| 1905 | static void cf2Func(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){ |
| 1906 | } |
| 1907 | static void cf2Step(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){ |
| 1908 | } |
| 1909 | static void cf2Final(sqlite3_context *ctx){ |
| 1910 | } |
| 1911 | static void cf2Destroy(void *pUser){ |
| 1912 | CreateFunctionV2 *p = (CreateFunctionV2 *)pUser; |
| 1913 | |
| 1914 | if( p->interp && p->pDestroy ){ |
| 1915 | int rc = Tcl_EvalObjEx(p->interp, p->pDestroy, 0); |
| 1916 | if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp); |
| 1917 | } |
| 1918 | |
| 1919 | if( p->pFunc ) Tcl_DecrRefCount(p->pFunc); |
| 1920 | if( p->pStep ) Tcl_DecrRefCount(p->pStep); |
| 1921 | if( p->pFinal ) Tcl_DecrRefCount(p->pFinal); |
| 1922 | if( p->pDestroy ) Tcl_DecrRefCount(p->pDestroy); |
| 1923 | sqlite3_free(p); |
| 1924 | } |
| 1925 | static int test_create_function_v2( |
| 1926 | ClientData clientData, /* Not used */ |
| 1927 | Tcl_Interp *interp, /* The invoking TCL interpreter */ |
| 1928 | int objc, /* Number of arguments */ |
| 1929 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1930 | ){ |
| 1931 | sqlite3 *db; |
| 1932 | const char *zFunc; |
| 1933 | int nArg; |
| 1934 | int enc; |
| 1935 | CreateFunctionV2 *p; |
| 1936 | int i; |
| 1937 | int rc; |
| 1938 | |
| 1939 | struct EncTable { |
| 1940 | const char *zEnc; |
| 1941 | int enc; |
| 1942 | } aEnc[] = { |
| 1943 | {"utf8", SQLITE_UTF8 }, |
| 1944 | {"utf16", SQLITE_UTF16 }, |
| 1945 | {"utf16le", SQLITE_UTF16LE }, |
| 1946 | {"utf16be", SQLITE_UTF16BE }, |
| 1947 | {"any", SQLITE_ANY }, |
| 1948 | {"0", 0 } |
| 1949 | }; |
| 1950 | |
| 1951 | if( objc<5 || (objc%2)==0 ){ |
| 1952 | Tcl_WrongNumArgs(interp, 1, objv, "DB NAME NARG ENC SWITCHES..."); |
| 1953 | return TCL_ERROR; |
| 1954 | } |
| 1955 | |
| 1956 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 1957 | zFunc = Tcl_GetString(objv[2]); |
| 1958 | if( Tcl_GetIntFromObj(interp, objv[3], &nArg) ) return TCL_ERROR; |
| 1959 | if( Tcl_GetIndexFromObjStruct(interp, objv[4], aEnc, sizeof(aEnc[0]), |
| 1960 | "encoding", 0, &enc) |
| 1961 | ){ |
| 1962 | return TCL_ERROR; |
| 1963 | } |
| 1964 | enc = aEnc[enc].enc; |
| 1965 | |
| 1966 | p = sqlite3_malloc(sizeof(CreateFunctionV2)); |
| 1967 | assert( p ); |
| 1968 | memset(p, 0, sizeof(CreateFunctionV2)); |
| 1969 | p->interp = interp; |
| 1970 | |
| 1971 | for(i=5; i<objc; i+=2){ |
| 1972 | int iSwitch; |
| 1973 | const char *azSwitch[] = {"-func", "-step", "-final", "-destroy", 0}; |
| 1974 | if( Tcl_GetIndexFromObj(interp, objv[i], azSwitch, "switch", 0, &iSwitch) ){ |
| 1975 | sqlite3_free(p); |
| 1976 | return TCL_ERROR; |
| 1977 | } |
| 1978 | |
| 1979 | switch( iSwitch ){ |
| 1980 | case 0: p->pFunc = objv[i+1]; break; |
| 1981 | case 1: p->pStep = objv[i+1]; break; |
| 1982 | case 2: p->pFinal = objv[i+1]; break; |
| 1983 | case 3: p->pDestroy = objv[i+1]; break; |
| 1984 | } |
| 1985 | } |
| 1986 | if( p->pFunc ) p->pFunc = Tcl_DuplicateObj(p->pFunc); |
| 1987 | if( p->pStep ) p->pStep = Tcl_DuplicateObj(p->pStep); |
| 1988 | if( p->pFinal ) p->pFinal = Tcl_DuplicateObj(p->pFinal); |
| 1989 | if( p->pDestroy ) p->pDestroy = Tcl_DuplicateObj(p->pDestroy); |
| 1990 | |
| 1991 | if( p->pFunc ) Tcl_IncrRefCount(p->pFunc); |
| 1992 | if( p->pStep ) Tcl_IncrRefCount(p->pStep); |
| 1993 | if( p->pFinal ) Tcl_IncrRefCount(p->pFinal); |
| 1994 | if( p->pDestroy ) Tcl_IncrRefCount(p->pDestroy); |
| 1995 | |
| 1996 | rc = sqlite3_create_function_v2(db, zFunc, nArg, enc, (void *)p, |
| 1997 | (p->pFunc ? cf2Func : 0), |
| 1998 | (p->pStep ? cf2Step : 0), |
| 1999 | (p->pFinal ? cf2Final : 0), |
| 2000 | cf2Destroy |
| 2001 | ); |
| 2002 | if( rc!=SQLITE_OK ){ |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 2003 | Tcl_ResetResult(interp); |
| 2004 | Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0); |
| 2005 | return TCL_ERROR; |
| 2006 | } |
| 2007 | return TCL_OK; |
| 2008 | } |
| 2009 | |
| 2010 | /* |
danielk1977 | 69e777f | 2006-06-14 10:38:02 +0000 | [diff] [blame] | 2011 | ** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC? |
| 2012 | */ |
| 2013 | static int test_load_extension( |
| 2014 | ClientData clientData, /* Not used */ |
| 2015 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 2016 | int objc, /* Number of arguments */ |
| 2017 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 2018 | ){ |
| 2019 | Tcl_CmdInfo cmdInfo; |
| 2020 | sqlite3 *db; |
| 2021 | int rc; |
| 2022 | char *zDb; |
| 2023 | char *zFile; |
| 2024 | char *zProc = 0; |
| 2025 | char *zErr = 0; |
| 2026 | |
| 2027 | if( objc!=4 && objc!=3 ){ |
| 2028 | Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE FILE ?PROC?"); |
| 2029 | return TCL_ERROR; |
| 2030 | } |
| 2031 | zDb = Tcl_GetString(objv[1]); |
| 2032 | zFile = Tcl_GetString(objv[2]); |
| 2033 | if( objc==4 ){ |
| 2034 | zProc = Tcl_GetString(objv[3]); |
| 2035 | } |
| 2036 | |
| 2037 | /* Extract the C database handle from the Tcl command name */ |
| 2038 | if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){ |
| 2039 | Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0); |
| 2040 | return TCL_ERROR; |
| 2041 | } |
| 2042 | db = ((struct SqliteDb*)cmdInfo.objClientData)->db; |
| 2043 | assert(db); |
| 2044 | |
| 2045 | /* Call the underlying C function. If an error occurs, set rc to |
| 2046 | ** TCL_ERROR and load any error string into the interpreter. If no |
| 2047 | ** error occurs, set rc to TCL_OK. |
| 2048 | */ |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 2049 | #ifdef SQLITE_OMIT_LOAD_EXTENSION |
| 2050 | rc = SQLITE_ERROR; |
| 2051 | zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()"); |
| 2052 | #else |
danielk1977 | 69e777f | 2006-06-14 10:38:02 +0000 | [diff] [blame] | 2053 | rc = sqlite3_load_extension(db, zFile, zProc, &zErr); |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 2054 | #endif |
danielk1977 | 69e777f | 2006-06-14 10:38:02 +0000 | [diff] [blame] | 2055 | if( rc!=SQLITE_OK ){ |
| 2056 | Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE); |
| 2057 | rc = TCL_ERROR; |
| 2058 | }else{ |
| 2059 | rc = TCL_OK; |
| 2060 | } |
| 2061 | sqlite3_free(zErr); |
| 2062 | |
| 2063 | return rc; |
| 2064 | } |
| 2065 | |
| 2066 | /* |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 2067 | ** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF |
| 2068 | */ |
| 2069 | static int test_enable_load( |
| 2070 | ClientData clientData, /* Not used */ |
| 2071 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 2072 | int objc, /* Number of arguments */ |
| 2073 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 2074 | ){ |
| 2075 | Tcl_CmdInfo cmdInfo; |
| 2076 | sqlite3 *db; |
| 2077 | char *zDb; |
| 2078 | int onoff; |
| 2079 | |
| 2080 | if( objc!=3 ){ |
| 2081 | Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF"); |
| 2082 | return TCL_ERROR; |
| 2083 | } |
| 2084 | zDb = Tcl_GetString(objv[1]); |
| 2085 | |
| 2086 | /* Extract the C database handle from the Tcl command name */ |
| 2087 | if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){ |
| 2088 | Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0); |
| 2089 | return TCL_ERROR; |
| 2090 | } |
| 2091 | db = ((struct SqliteDb*)cmdInfo.objClientData)->db; |
| 2092 | assert(db); |
| 2093 | |
| 2094 | /* Get the onoff parameter */ |
| 2095 | if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){ |
| 2096 | return TCL_ERROR; |
| 2097 | } |
| 2098 | |
| 2099 | #ifdef SQLITE_OMIT_LOAD_EXTENSION |
| 2100 | Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()"); |
| 2101 | return TCL_ERROR; |
| 2102 | #else |
| 2103 | sqlite3_enable_load_extension(db, onoff); |
| 2104 | return TCL_OK; |
| 2105 | #endif |
| 2106 | } |
| 2107 | |
| 2108 | /* |
drh | 28b4e48 | 2002-03-11 02:06:13 +0000 | [diff] [blame] | 2109 | ** Usage: sqlite_abort |
| 2110 | ** |
| 2111 | ** Shutdown the process immediately. This is not a clean shutdown. |
| 2112 | ** This command is used to test the recoverability of a database in |
| 2113 | ** the event of a program crash. |
| 2114 | */ |
| 2115 | static int sqlite_abort( |
| 2116 | void *NotUsed, |
| 2117 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 2118 | int argc, /* Number of arguments */ |
| 2119 | char **argv /* Text of each argument */ |
| 2120 | ){ |
shaneh | 2ceced1 | 2010-07-07 16:51:36 +0000 | [diff] [blame] | 2121 | #if defined(_MSC_VER) |
| 2122 | /* We do this, otherwise the test will halt with a popup message |
| 2123 | * that we have to click away before the test will continue. |
| 2124 | */ |
| 2125 | _set_abort_behavior( 0, _CALL_REPORTFAULT ); |
| 2126 | #endif |
dan | d3f6b81 | 2010-07-19 12:44:14 +0000 | [diff] [blame] | 2127 | exit(255); |
drh | 28b4e48 | 2002-03-11 02:06:13 +0000 | [diff] [blame] | 2128 | assert( interp==0 ); /* This will always fail */ |
| 2129 | return TCL_OK; |
| 2130 | } |
| 2131 | |
| 2132 | /* |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 2133 | ** The following routine is a user-defined SQL function whose purpose |
| 2134 | ** is to test the sqlite_set_result() API. |
| 2135 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 2136 | static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 2137 | while( argc>=2 ){ |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 2138 | const char *zArg0 = (char*)sqlite3_value_text(argv[0]); |
danielk1977 | 6d88bad | 2004-05-27 14:23:36 +0000 | [diff] [blame] | 2139 | if( zArg0 ){ |
| 2140 | if( 0==sqlite3StrICmp(zArg0, "int") ){ |
| 2141 | sqlite3_result_int(context, sqlite3_value_int(argv[1])); |
| 2142 | }else if( sqlite3StrICmp(zArg0,"int64")==0 ){ |
| 2143 | sqlite3_result_int64(context, sqlite3_value_int64(argv[1])); |
| 2144 | }else if( sqlite3StrICmp(zArg0,"string")==0 ){ |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 2145 | sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2146 | SQLITE_TRANSIENT); |
danielk1977 | 6d88bad | 2004-05-27 14:23:36 +0000 | [diff] [blame] | 2147 | }else if( sqlite3StrICmp(zArg0,"double")==0 ){ |
| 2148 | sqlite3_result_double(context, sqlite3_value_double(argv[1])); |
| 2149 | }else if( sqlite3StrICmp(zArg0,"null")==0 ){ |
| 2150 | sqlite3_result_null(context); |
| 2151 | }else if( sqlite3StrICmp(zArg0,"value")==0 ){ |
| 2152 | sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]); |
| 2153 | }else{ |
| 2154 | goto error_out; |
| 2155 | } |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 2156 | }else{ |
danielk1977 | 6d88bad | 2004-05-27 14:23:36 +0000 | [diff] [blame] | 2157 | goto error_out; |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 2158 | } |
| 2159 | argc -= 2; |
| 2160 | argv += 2; |
| 2161 | } |
danielk1977 | 6d88bad | 2004-05-27 14:23:36 +0000 | [diff] [blame] | 2162 | return; |
| 2163 | |
| 2164 | error_out: |
| 2165 | sqlite3_result_error(context,"first argument should be one of: " |
| 2166 | "int int64 string double null value", -1); |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | /* |
| 2170 | ** Usage: sqlite_register_test_function DB NAME |
| 2171 | ** |
| 2172 | ** Register the test SQL function on the database DB under the name NAME. |
| 2173 | */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 2174 | static int test_register_func( |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 2175 | void *NotUsed, |
| 2176 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 2177 | int argc, /* Number of arguments */ |
| 2178 | char **argv /* Text of each argument */ |
| 2179 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 2180 | sqlite3 *db; |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 2181 | int rc; |
| 2182 | if( argc!=3 ){ |
| 2183 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 2184 | " DB FUNCTION-NAME", 0); |
| 2185 | return TCL_ERROR; |
| 2186 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2187 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 2188 | rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2189 | testFunc, 0, 0); |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 2190 | if( rc!=0 ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 2191 | Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0); |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 2192 | return TCL_ERROR; |
| 2193 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2194 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 2195 | return TCL_OK; |
| 2196 | } |
| 2197 | |
| 2198 | /* |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2199 | ** Usage: sqlite3_finalize STMT |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2200 | ** |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2201 | ** Finalize a statement handle. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2202 | */ |
| 2203 | static int test_finalize( |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2204 | void * clientData, |
| 2205 | Tcl_Interp *interp, |
| 2206 | int objc, |
| 2207 | Tcl_Obj *CONST objv[] |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2208 | ){ |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2209 | sqlite3_stmt *pStmt; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2210 | int rc; |
drh | dddb2f2 | 2007-01-03 23:37:28 +0000 | [diff] [blame] | 2211 | sqlite3 *db = 0; |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2212 | |
| 2213 | if( objc!=2 ){ |
| 2214 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2215 | Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2216 | return TCL_ERROR; |
| 2217 | } |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2218 | |
| 2219 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2220 | |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 2221 | if( pStmt ){ |
| 2222 | db = StmtToDb(pStmt); |
| 2223 | } |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 2224 | rc = sqlite3_finalize(pStmt); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2225 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 2226 | if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2227 | return TCL_OK; |
| 2228 | } |
| 2229 | |
| 2230 | /* |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 2231 | ** Usage: sqlite3_stmt_status STMT CODE RESETFLAG |
| 2232 | ** |
| 2233 | ** Get the value of a status counter from a statement. |
| 2234 | */ |
| 2235 | static int test_stmt_status( |
| 2236 | void * clientData, |
| 2237 | Tcl_Interp *interp, |
| 2238 | int objc, |
| 2239 | Tcl_Obj *CONST objv[] |
| 2240 | ){ |
| 2241 | int iValue; |
| 2242 | int i, op, resetFlag; |
| 2243 | const char *zOpName; |
| 2244 | sqlite3_stmt *pStmt; |
| 2245 | |
| 2246 | static const struct { |
| 2247 | const char *zName; |
| 2248 | int op; |
| 2249 | } aOp[] = { |
| 2250 | { "SQLITE_STMTSTATUS_FULLSCAN_STEP", SQLITE_STMTSTATUS_FULLSCAN_STEP }, |
| 2251 | { "SQLITE_STMTSTATUS_SORT", SQLITE_STMTSTATUS_SORT }, |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 2252 | { "SQLITE_STMTSTATUS_AUTOINDEX", SQLITE_STMTSTATUS_AUTOINDEX }, |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 2253 | }; |
| 2254 | if( objc!=4 ){ |
| 2255 | Tcl_WrongNumArgs(interp, 1, objv, "STMT PARAMETER RESETFLAG"); |
| 2256 | return TCL_ERROR; |
| 2257 | } |
| 2258 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2259 | zOpName = Tcl_GetString(objv[2]); |
| 2260 | for(i=0; i<ArraySize(aOp); i++){ |
| 2261 | if( strcmp(aOp[i].zName, zOpName)==0 ){ |
| 2262 | op = aOp[i].op; |
| 2263 | break; |
| 2264 | } |
| 2265 | } |
| 2266 | if( i>=ArraySize(aOp) ){ |
| 2267 | if( Tcl_GetIntFromObj(interp, objv[2], &op) ) return TCL_ERROR; |
| 2268 | } |
| 2269 | if( Tcl_GetBooleanFromObj(interp, objv[3], &resetFlag) ) return TCL_ERROR; |
| 2270 | iValue = sqlite3_stmt_status(pStmt, op, resetFlag); |
| 2271 | Tcl_SetObjResult(interp, Tcl_NewIntObj(iValue)); |
| 2272 | return TCL_OK; |
| 2273 | } |
| 2274 | |
| 2275 | /* |
drh | bb5a9c3 | 2008-06-19 02:52:25 +0000 | [diff] [blame] | 2276 | ** Usage: sqlite3_next_stmt DB STMT |
| 2277 | ** |
| 2278 | ** Return the next statment in sequence after STMT. |
| 2279 | */ |
| 2280 | static int test_next_stmt( |
| 2281 | void * clientData, |
| 2282 | Tcl_Interp *interp, |
| 2283 | int objc, |
| 2284 | Tcl_Obj *CONST objv[] |
| 2285 | ){ |
| 2286 | sqlite3_stmt *pStmt; |
| 2287 | sqlite3 *db = 0; |
| 2288 | char zBuf[50]; |
| 2289 | |
| 2290 | if( objc!=3 ){ |
| 2291 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2292 | Tcl_GetStringFromObj(objv[0], 0), " DB STMT", 0); |
| 2293 | return TCL_ERROR; |
| 2294 | } |
| 2295 | |
| 2296 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 2297 | if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt) ) return TCL_ERROR; |
| 2298 | pStmt = sqlite3_next_stmt(db, pStmt); |
| 2299 | if( pStmt ){ |
| 2300 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
| 2301 | Tcl_AppendResult(interp, zBuf, 0); |
| 2302 | } |
| 2303 | return TCL_OK; |
| 2304 | } |
| 2305 | |
drh | f03d9cc | 2010-11-16 23:10:25 +0000 | [diff] [blame] | 2306 | /* |
| 2307 | ** Usage: sqlite3_stmt_readonly STMT |
| 2308 | ** |
| 2309 | ** Return true if STMT is a NULL pointer or a pointer to a statement |
| 2310 | ** that is guaranteed to leave the database unmodified. |
| 2311 | */ |
| 2312 | static int test_stmt_readonly( |
| 2313 | void * clientData, |
| 2314 | Tcl_Interp *interp, |
| 2315 | int objc, |
| 2316 | Tcl_Obj *CONST objv[] |
| 2317 | ){ |
| 2318 | sqlite3_stmt *pStmt; |
| 2319 | int rc; |
| 2320 | |
| 2321 | if( objc!=2 ){ |
| 2322 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2323 | Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); |
| 2324 | return TCL_ERROR; |
| 2325 | } |
| 2326 | |
| 2327 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2328 | rc = sqlite3_stmt_readonly(pStmt); |
| 2329 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc)); |
| 2330 | return TCL_OK; |
| 2331 | } |
| 2332 | |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 2333 | /* |
| 2334 | ** Usage: uses_stmt_journal STMT |
| 2335 | ** |
| 2336 | ** Return true if STMT uses a statement journal. |
| 2337 | */ |
| 2338 | static int uses_stmt_journal( |
| 2339 | void * clientData, |
| 2340 | Tcl_Interp *interp, |
| 2341 | int objc, |
| 2342 | Tcl_Obj *CONST objv[] |
| 2343 | ){ |
| 2344 | sqlite3_stmt *pStmt; |
| 2345 | int rc; |
| 2346 | |
| 2347 | if( objc!=2 ){ |
| 2348 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2349 | Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); |
| 2350 | return TCL_ERROR; |
| 2351 | } |
| 2352 | |
| 2353 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2354 | rc = sqlite3_stmt_readonly(pStmt); |
| 2355 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(((Vdbe *)pStmt)->usesStmtJournal)); |
| 2356 | return TCL_OK; |
| 2357 | } |
| 2358 | |
drh | bb5a9c3 | 2008-06-19 02:52:25 +0000 | [diff] [blame] | 2359 | |
| 2360 | /* |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2361 | ** Usage: sqlite3_reset STMT |
| 2362 | ** |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 2363 | ** Reset a statement handle. |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2364 | */ |
| 2365 | static int test_reset( |
| 2366 | void * clientData, |
| 2367 | Tcl_Interp *interp, |
| 2368 | int objc, |
| 2369 | Tcl_Obj *CONST objv[] |
| 2370 | ){ |
| 2371 | sqlite3_stmt *pStmt; |
| 2372 | int rc; |
| 2373 | |
| 2374 | if( objc!=2 ){ |
| 2375 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2376 | Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); |
| 2377 | return TCL_ERROR; |
| 2378 | } |
| 2379 | |
| 2380 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2381 | |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 2382 | rc = sqlite3_reset(pStmt); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 2383 | if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){ |
| 2384 | return TCL_ERROR; |
| 2385 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2386 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 2387 | /* |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2388 | if( rc ){ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2389 | return TCL_ERROR; |
| 2390 | } |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 2391 | */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2392 | return TCL_OK; |
| 2393 | } |
| 2394 | |
drh | 5a38705 | 2003-01-11 14:19:51 +0000 | [diff] [blame] | 2395 | /* |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 2396 | ** Usage: sqlite3_expired STMT |
| 2397 | ** |
| 2398 | ** Return TRUE if a recompilation of the statement is recommended. |
| 2399 | */ |
| 2400 | static int test_expired( |
| 2401 | void * clientData, |
| 2402 | Tcl_Interp *interp, |
| 2403 | int objc, |
| 2404 | Tcl_Obj *CONST objv[] |
| 2405 | ){ |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 2406 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 2407 | sqlite3_stmt *pStmt; |
| 2408 | if( objc!=2 ){ |
| 2409 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2410 | Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); |
| 2411 | return TCL_ERROR; |
| 2412 | } |
| 2413 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2414 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt))); |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 2415 | #endif |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 2416 | return TCL_OK; |
| 2417 | } |
| 2418 | |
| 2419 | /* |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 2420 | ** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT |
| 2421 | ** |
| 2422 | ** Transfer all bindings from FROMSTMT over to TOSTMT |
| 2423 | */ |
| 2424 | static int test_transfer_bind( |
| 2425 | void * clientData, |
| 2426 | Tcl_Interp *interp, |
| 2427 | int objc, |
| 2428 | Tcl_Obj *CONST objv[] |
| 2429 | ){ |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 2430 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 2431 | sqlite3_stmt *pStmt1, *pStmt2; |
| 2432 | if( objc!=3 ){ |
| 2433 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2434 | Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0); |
| 2435 | return TCL_ERROR; |
| 2436 | } |
| 2437 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR; |
| 2438 | if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR; |
| 2439 | Tcl_SetObjResult(interp, |
| 2440 | Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2))); |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 2441 | #endif |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 2442 | return TCL_OK; |
| 2443 | } |
| 2444 | |
| 2445 | /* |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2446 | ** Usage: sqlite3_changes DB |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2447 | ** |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2448 | ** Return the number of changes made to the database by the last SQL |
| 2449 | ** execution. |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2450 | */ |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2451 | static int test_changes( |
| 2452 | void * clientData, |
| 2453 | Tcl_Interp *interp, |
| 2454 | int objc, |
| 2455 | Tcl_Obj *CONST objv[] |
| 2456 | ){ |
| 2457 | sqlite3 *db; |
| 2458 | if( objc!=2 ){ |
| 2459 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2460 | Tcl_GetString(objv[0]), " DB", 0); |
| 2461 | return TCL_ERROR; |
| 2462 | } |
| 2463 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 2464 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db))); |
| 2465 | return TCL_OK; |
| 2466 | } |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2467 | |
| 2468 | /* |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2469 | ** This is the "static_bind_value" that variables are bound to when |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2470 | ** the FLAG option of sqlite3_bind is "static" |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2471 | */ |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2472 | static char *sqlite_static_bind_value = 0; |
drh | f031381 | 2006-09-04 15:53:53 +0000 | [diff] [blame] | 2473 | static int sqlite_static_bind_nbyte = 0; |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2474 | |
| 2475 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2476 | ** Usage: sqlite3_bind VM IDX VALUE FLAGS |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2477 | ** |
| 2478 | ** Sets the value of the IDX-th occurance of "?" in the original SQL |
| 2479 | ** string. VALUE is the new value. If FLAGS=="null" then VALUE is |
| 2480 | ** ignored and the value is set to NULL. If FLAGS=="static" then |
| 2481 | ** the value is set to the value of a static variable named |
| 2482 | ** "sqlite_static_bind_value". If FLAGS=="normal" then a copy |
drh | bf8aa2a | 2005-12-02 02:44:05 +0000 | [diff] [blame] | 2483 | ** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored |
| 2484 | ** an a 10-byte blob "abc\000xyz\000pq" is inserted. |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2485 | */ |
| 2486 | static int test_bind( |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2487 | void *NotUsed, |
| 2488 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 2489 | int argc, /* Number of arguments */ |
| 2490 | char **argv /* Text of each argument */ |
| 2491 | ){ |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 2492 | sqlite3_stmt *pStmt; |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2493 | int rc; |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2494 | int idx; |
| 2495 | if( argc!=5 ){ |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2496 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2497 | " VM IDX VALUE (null|static|normal)\"", 0); |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2498 | return TCL_ERROR; |
| 2499 | } |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 2500 | if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR; |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2501 | if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR; |
| 2502 | if( strcmp(argv[4],"null")==0 ){ |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 2503 | rc = sqlite3_bind_null(pStmt, idx); |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2504 | }else if( strcmp(argv[4],"static")==0 ){ |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 2505 | rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0); |
drh | f031381 | 2006-09-04 15:53:53 +0000 | [diff] [blame] | 2506 | }else if( strcmp(argv[4],"static-nbytes")==0 ){ |
| 2507 | rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, |
| 2508 | sqlite_static_bind_nbyte, 0); |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2509 | }else if( strcmp(argv[4],"normal")==0 ){ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2510 | rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT); |
drh | bf8aa2a | 2005-12-02 02:44:05 +0000 | [diff] [blame] | 2511 | }else if( strcmp(argv[4],"blob10")==0 ){ |
| 2512 | rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC); |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2513 | }else{ |
| 2514 | Tcl_AppendResult(interp, "4th argument should be " |
| 2515 | "\"null\" or \"static\" or \"normal\"", 0); |
| 2516 | return TCL_ERROR; |
| 2517 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2518 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2519 | if( rc ){ |
| 2520 | char zBuf[50]; |
| 2521 | sprintf(zBuf, "(%d) ", rc); |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 2522 | Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0); |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2523 | return TCL_ERROR; |
| 2524 | } |
| 2525 | return TCL_OK; |
| 2526 | } |
| 2527 | |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 2528 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 2529 | /* |
| 2530 | ** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be> |
| 2531 | ** |
| 2532 | ** This function is used to test that SQLite selects the correct collation |
| 2533 | ** sequence callback when multiple versions (for different text encodings) |
| 2534 | ** are available. |
| 2535 | ** |
| 2536 | ** Calling this routine registers the collation sequence "test_collate" |
| 2537 | ** with database handle <db>. The second argument must be a list of three |
| 2538 | ** boolean values. If the first is true, then a version of test_collate is |
| 2539 | ** registered for UTF-8, if the second is true, a version is registered for |
| 2540 | ** UTF-16le, if the third is true, a UTF-16be version is available. |
| 2541 | ** Previous versions of test_collate are deleted. |
| 2542 | ** |
| 2543 | ** The collation sequence test_collate is implemented by calling the |
| 2544 | ** following TCL script: |
| 2545 | ** |
| 2546 | ** "test_collate <enc> <lhs> <rhs>" |
| 2547 | ** |
| 2548 | ** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8. |
| 2549 | ** The <enc> parameter is the encoding of the collation function that |
| 2550 | ** SQLite selected to call. The TCL test script implements the |
| 2551 | ** "test_collate" proc. |
| 2552 | ** |
| 2553 | ** Note that this will only work with one intepreter at a time, as the |
| 2554 | ** interp pointer to use when evaluating the TCL script is stored in |
| 2555 | ** pTestCollateInterp. |
| 2556 | */ |
| 2557 | static Tcl_Interp* pTestCollateInterp; |
| 2558 | static int test_collate_func( |
| 2559 | void *pCtx, |
| 2560 | int nA, const void *zA, |
| 2561 | int nB, const void *zB |
| 2562 | ){ |
| 2563 | Tcl_Interp *i = pTestCollateInterp; |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 2564 | int encin = SQLITE_PTR_TO_INT(pCtx); |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 2565 | int res; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 2566 | int n; |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 2567 | |
| 2568 | sqlite3_value *pVal; |
| 2569 | Tcl_Obj *pX; |
| 2570 | |
| 2571 | pX = Tcl_NewStringObj("test_collate", -1); |
| 2572 | Tcl_IncrRefCount(pX); |
| 2573 | |
| 2574 | switch( encin ){ |
| 2575 | case SQLITE_UTF8: |
| 2576 | Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1)); |
| 2577 | break; |
| 2578 | case SQLITE_UTF16LE: |
| 2579 | Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1)); |
| 2580 | break; |
| 2581 | case SQLITE_UTF16BE: |
| 2582 | Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1)); |
| 2583 | break; |
| 2584 | default: |
| 2585 | assert(0); |
| 2586 | } |
| 2587 | |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2588 | sqlite3BeginBenignMalloc(); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2589 | pVal = sqlite3ValueNew(0); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2590 | if( pVal ){ |
| 2591 | sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC); |
| 2592 | n = sqlite3_value_bytes(pVal); |
| 2593 | Tcl_ListObjAppendElement(i,pX, |
| 2594 | Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n)); |
| 2595 | sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC); |
| 2596 | n = sqlite3_value_bytes(pVal); |
| 2597 | Tcl_ListObjAppendElement(i,pX, |
| 2598 | Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n)); |
| 2599 | sqlite3ValueFree(pVal); |
| 2600 | } |
| 2601 | sqlite3EndBenignMalloc(); |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 2602 | |
| 2603 | Tcl_EvalObjEx(i, pX, 0); |
| 2604 | Tcl_DecrRefCount(pX); |
| 2605 | Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res); |
| 2606 | return res; |
| 2607 | } |
| 2608 | static int test_collate( |
| 2609 | void * clientData, |
| 2610 | Tcl_Interp *interp, |
| 2611 | int objc, |
| 2612 | Tcl_Obj *CONST objv[] |
| 2613 | ){ |
| 2614 | sqlite3 *db; |
| 2615 | int val; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2616 | sqlite3_value *pVal; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2617 | int rc; |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 2618 | |
| 2619 | if( objc!=5 ) goto bad_args; |
| 2620 | pTestCollateInterp = interp; |
| 2621 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 2622 | |
| 2623 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2624 | rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8, |
| 2625 | (void *)SQLITE_UTF8, val?test_collate_func:0); |
| 2626 | if( rc==SQLITE_OK ){ |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 2627 | const void *zUtf16; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2628 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR; |
| 2629 | rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE, |
| 2630 | (void *)SQLITE_UTF16LE, val?test_collate_func:0); |
| 2631 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2632 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2633 | #if 0 |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 2634 | if( sqlite3_iMallocFail>0 ){ |
| 2635 | sqlite3_iMallocFail++; |
| 2636 | } |
| 2637 | #endif |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 2638 | sqlite3_mutex_enter(db->mutex); |
| 2639 | pVal = sqlite3ValueNew(db); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2640 | sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2641 | zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 2642 | if( db->mallocFailed ){ |
| 2643 | rc = SQLITE_NOMEM; |
| 2644 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2645 | rc = sqlite3_create_collation16(db, zUtf16, SQLITE_UTF16BE, |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 2646 | (void *)SQLITE_UTF16BE, val?test_collate_func:0); |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 2647 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2648 | sqlite3ValueFree(pVal); |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 2649 | sqlite3_mutex_leave(db->mutex); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2650 | } |
| 2651 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 2652 | |
| 2653 | if( rc!=SQLITE_OK ){ |
| 2654 | Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0); |
| 2655 | return TCL_ERROR; |
| 2656 | } |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 2657 | return TCL_OK; |
| 2658 | |
| 2659 | bad_args: |
| 2660 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2661 | Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0); |
| 2662 | return TCL_ERROR; |
| 2663 | } |
| 2664 | |
drh | 268803a | 2005-12-14 20:11:30 +0000 | [diff] [blame] | 2665 | /* |
| 2666 | ** When the collation needed callback is invoked, record the name of |
| 2667 | ** the requested collating function here. The recorded name is linked |
| 2668 | ** to a TCL variable and used to make sure that the requested collation |
| 2669 | ** name is correct. |
| 2670 | */ |
| 2671 | static char zNeededCollation[200]; |
| 2672 | static char *pzNeededCollation = zNeededCollation; |
| 2673 | |
| 2674 | |
| 2675 | /* |
| 2676 | ** Called when a collating sequence is needed. Registered using |
| 2677 | ** sqlite3_collation_needed16(). |
| 2678 | */ |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2679 | static void test_collate_needed_cb( |
| 2680 | void *pCtx, |
| 2681 | sqlite3 *db, |
| 2682 | int eTextRep, |
drh | 268803a | 2005-12-14 20:11:30 +0000 | [diff] [blame] | 2683 | const void *pName |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2684 | ){ |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 2685 | int enc = ENC(db); |
drh | 268803a | 2005-12-14 20:11:30 +0000 | [diff] [blame] | 2686 | int i; |
| 2687 | char *z; |
| 2688 | for(z = (char*)pName, i=0; *z || z[1]; z++){ |
| 2689 | if( *z ) zNeededCollation[i++] = *z; |
| 2690 | } |
| 2691 | zNeededCollation[i] = 0; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2692 | sqlite3_create_collation( |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 2693 | db, "test_collate", ENC(db), SQLITE_INT_TO_PTR(enc), test_collate_func); |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
| 2696 | /* |
| 2697 | ** Usage: add_test_collate_needed DB |
| 2698 | */ |
| 2699 | static int test_collate_needed( |
| 2700 | void * clientData, |
| 2701 | Tcl_Interp *interp, |
| 2702 | int objc, |
| 2703 | Tcl_Obj *CONST objv[] |
| 2704 | ){ |
| 2705 | sqlite3 *db; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2706 | int rc; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2707 | |
| 2708 | if( objc!=2 ) goto bad_args; |
| 2709 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2710 | rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb); |
drh | 268803a | 2005-12-14 20:11:30 +0000 | [diff] [blame] | 2711 | zNeededCollation[0] = 0; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2712 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2713 | return TCL_OK; |
| 2714 | |
| 2715 | bad_args: |
| 2716 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 2717 | return TCL_ERROR; |
| 2718 | } |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 2719 | |
| 2720 | /* |
| 2721 | ** tclcmd: add_alignment_test_collations DB |
| 2722 | ** |
| 2723 | ** Add two new collating sequences to the database DB |
| 2724 | ** |
| 2725 | ** utf16_aligned |
| 2726 | ** utf16_unaligned |
| 2727 | ** |
| 2728 | ** Both collating sequences use the same sort order as BINARY. |
| 2729 | ** The only difference is that the utf16_aligned collating |
| 2730 | ** sequence is declared with the SQLITE_UTF16_ALIGNED flag. |
| 2731 | ** Both collating functions increment the unaligned utf16 counter |
| 2732 | ** whenever they see a string that begins on an odd byte boundary. |
| 2733 | */ |
| 2734 | static int unaligned_string_counter = 0; |
| 2735 | static int alignmentCollFunc( |
| 2736 | void *NotUsed, |
| 2737 | int nKey1, const void *pKey1, |
| 2738 | int nKey2, const void *pKey2 |
| 2739 | ){ |
| 2740 | int rc, n; |
| 2741 | n = nKey1<nKey2 ? nKey1 : nKey2; |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 2742 | if( nKey1>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey1))) ) unaligned_string_counter++; |
| 2743 | if( nKey2>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey2))) ) unaligned_string_counter++; |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 2744 | rc = memcmp(pKey1, pKey2, n); |
| 2745 | if( rc==0 ){ |
| 2746 | rc = nKey1 - nKey2; |
| 2747 | } |
| 2748 | return rc; |
| 2749 | } |
| 2750 | static int add_alignment_test_collations( |
| 2751 | void * clientData, |
| 2752 | Tcl_Interp *interp, |
| 2753 | int objc, |
| 2754 | Tcl_Obj *CONST objv[] |
| 2755 | ){ |
| 2756 | sqlite3 *db; |
| 2757 | if( objc>=2 ){ |
| 2758 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
danielk1977 | ebb3293 | 2009-04-28 15:35:38 +0000 | [diff] [blame] | 2759 | sqlite3_create_collation(db, "utf16_unaligned", SQLITE_UTF16, |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 2760 | 0, alignmentCollFunc); |
danielk1977 | ebb3293 | 2009-04-28 15:35:38 +0000 | [diff] [blame] | 2761 | sqlite3_create_collation(db, "utf16_aligned", SQLITE_UTF16_ALIGNED, |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 2762 | 0, alignmentCollFunc); |
| 2763 | } |
| 2764 | return SQLITE_OK; |
| 2765 | } |
| 2766 | #endif /* !defined(SQLITE_OMIT_UTF16) */ |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2767 | |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2768 | /* |
| 2769 | ** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be> |
| 2770 | ** |
| 2771 | ** This function is used to test that SQLite selects the correct user |
| 2772 | ** function callback when multiple versions (for different text encodings) |
| 2773 | ** are available. |
| 2774 | ** |
| 2775 | ** Calling this routine registers up to three versions of the user function |
| 2776 | ** "test_function" with database handle <db>. If the second argument is |
| 2777 | ** true, then a version of test_function is registered for UTF-8, if the |
| 2778 | ** third is true, a version is registered for UTF-16le, if the fourth is |
| 2779 | ** true, a UTF-16be version is available. Previous versions of |
| 2780 | ** test_function are deleted. |
| 2781 | ** |
| 2782 | ** The user function is implemented by calling the following TCL script: |
| 2783 | ** |
| 2784 | ** "test_function <enc> <arg>" |
| 2785 | ** |
| 2786 | ** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the |
| 2787 | ** single argument passed to the SQL function. The value returned by |
| 2788 | ** the TCL script is used as the return value of the SQL function. It |
| 2789 | ** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8 |
| 2790 | ** for a UTF-16LE test_function(), and UTF-16LE for an implementation that |
| 2791 | ** prefers UTF-16BE. |
| 2792 | */ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 2793 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2794 | static void test_function_utf8( |
| 2795 | sqlite3_context *pCtx, |
| 2796 | int nArg, |
| 2797 | sqlite3_value **argv |
| 2798 | ){ |
| 2799 | Tcl_Interp *interp; |
| 2800 | Tcl_Obj *pX; |
| 2801 | sqlite3_value *pVal; |
| 2802 | interp = (Tcl_Interp *)sqlite3_user_data(pCtx); |
| 2803 | pX = Tcl_NewStringObj("test_function", -1); |
| 2804 | Tcl_IncrRefCount(pX); |
| 2805 | Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1)); |
| 2806 | Tcl_ListObjAppendElement(interp, pX, |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 2807 | Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2808 | Tcl_EvalObjEx(interp, pX, 0); |
| 2809 | Tcl_DecrRefCount(pX); |
| 2810 | sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2811 | pVal = sqlite3ValueNew(0); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2812 | sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2813 | SQLITE_UTF8, SQLITE_STATIC); |
| 2814 | sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal), |
| 2815 | -1, SQLITE_TRANSIENT); |
| 2816 | sqlite3ValueFree(pVal); |
| 2817 | } |
| 2818 | static void test_function_utf16le( |
| 2819 | sqlite3_context *pCtx, |
| 2820 | int nArg, |
| 2821 | sqlite3_value **argv |
| 2822 | ){ |
| 2823 | Tcl_Interp *interp; |
| 2824 | Tcl_Obj *pX; |
| 2825 | sqlite3_value *pVal; |
| 2826 | interp = (Tcl_Interp *)sqlite3_user_data(pCtx); |
| 2827 | pX = Tcl_NewStringObj("test_function", -1); |
| 2828 | Tcl_IncrRefCount(pX); |
| 2829 | Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1)); |
| 2830 | Tcl_ListObjAppendElement(interp, pX, |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 2831 | Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2832 | Tcl_EvalObjEx(interp, pX, 0); |
| 2833 | Tcl_DecrRefCount(pX); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2834 | pVal = sqlite3ValueNew(0); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2835 | sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2836 | SQLITE_UTF8, SQLITE_STATIC); |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 2837 | sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT); |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2838 | sqlite3ValueFree(pVal); |
| 2839 | } |
| 2840 | static void test_function_utf16be( |
| 2841 | sqlite3_context *pCtx, |
| 2842 | int nArg, |
| 2843 | sqlite3_value **argv |
| 2844 | ){ |
| 2845 | Tcl_Interp *interp; |
| 2846 | Tcl_Obj *pX; |
| 2847 | sqlite3_value *pVal; |
| 2848 | interp = (Tcl_Interp *)sqlite3_user_data(pCtx); |
| 2849 | pX = Tcl_NewStringObj("test_function", -1); |
| 2850 | Tcl_IncrRefCount(pX); |
| 2851 | Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1)); |
| 2852 | Tcl_ListObjAppendElement(interp, pX, |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 2853 | Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2854 | Tcl_EvalObjEx(interp, pX, 0); |
| 2855 | Tcl_DecrRefCount(pX); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2856 | pVal = sqlite3ValueNew(0); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2857 | sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2858 | SQLITE_UTF8, SQLITE_STATIC); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 2859 | sqlite3_result_text16(pCtx, sqlite3_value_text16le(pVal), |
| 2860 | -1, SQLITE_TRANSIENT); |
| 2861 | sqlite3_result_text16be(pCtx, sqlite3_value_text16le(pVal), |
| 2862 | -1, SQLITE_TRANSIENT); |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2863 | sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal), |
| 2864 | -1, SQLITE_TRANSIENT); |
| 2865 | sqlite3ValueFree(pVal); |
| 2866 | } |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 2867 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2868 | static int test_function( |
| 2869 | void * clientData, |
| 2870 | Tcl_Interp *interp, |
| 2871 | int objc, |
| 2872 | Tcl_Obj *CONST objv[] |
| 2873 | ){ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 2874 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2875 | sqlite3 *db; |
| 2876 | int val; |
| 2877 | |
| 2878 | if( objc!=5 ) goto bad_args; |
| 2879 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 2880 | |
| 2881 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; |
| 2882 | if( val ){ |
| 2883 | sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8, |
| 2884 | interp, test_function_utf8, 0, 0); |
| 2885 | } |
| 2886 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR; |
| 2887 | if( val ){ |
| 2888 | sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE, |
| 2889 | interp, test_function_utf16le, 0, 0); |
| 2890 | } |
| 2891 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR; |
| 2892 | if( val ){ |
| 2893 | sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE, |
| 2894 | interp, test_function_utf16be, 0, 0); |
| 2895 | } |
| 2896 | |
| 2897 | return TCL_OK; |
| 2898 | bad_args: |
| 2899 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2900 | Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0); |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 2901 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2902 | return TCL_ERROR; |
| 2903 | } |
| 2904 | |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2905 | /* |
dan | ba3cbf3 | 2010-06-30 04:29:03 +0000 | [diff] [blame] | 2906 | ** Usage: sqlite3_test_errstr <err code> |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2907 | ** |
| 2908 | ** Test that the english language string equivalents for sqlite error codes |
| 2909 | ** are sane. The parameter is an integer representing an sqlite error code. |
| 2910 | ** The result is a list of two elements, the string representation of the |
| 2911 | ** error code and the english language explanation. |
| 2912 | */ |
| 2913 | static int test_errstr( |
| 2914 | void * clientData, |
| 2915 | Tcl_Interp *interp, |
| 2916 | int objc, |
| 2917 | Tcl_Obj *CONST objv[] |
| 2918 | ){ |
| 2919 | char *zCode; |
| 2920 | int i; |
| 2921 | if( objc!=1 ){ |
| 2922 | Tcl_WrongNumArgs(interp, 1, objv, "<error code>"); |
| 2923 | } |
| 2924 | |
| 2925 | zCode = Tcl_GetString(objv[1]); |
| 2926 | for(i=0; i<200; i++){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2927 | if( 0==strcmp(t1ErrorName(i), zCode) ) break; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2928 | } |
| 2929 | Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0); |
| 2930 | return TCL_OK; |
| 2931 | } |
| 2932 | |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2933 | /* |
drh | 99ee360 | 2003-02-16 19:13:36 +0000 | [diff] [blame] | 2934 | ** Usage: breakpoint |
| 2935 | ** |
| 2936 | ** This routine exists for one purpose - to provide a place to put a |
| 2937 | ** breakpoint with GDB that can be triggered using TCL code. The use |
| 2938 | ** for this is when a particular test fails on (say) the 1485th iteration. |
| 2939 | ** In the TCL test script, we can add code like this: |
| 2940 | ** |
| 2941 | ** if {$i==1485} breakpoint |
| 2942 | ** |
| 2943 | ** Then run testfixture in the debugger and wait for the breakpoint to |
| 2944 | ** fire. Then additional breakpoints can be set to trace down the bug. |
| 2945 | */ |
| 2946 | static int test_breakpoint( |
| 2947 | void *NotUsed, |
| 2948 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 2949 | int argc, /* Number of arguments */ |
| 2950 | char **argv /* Text of each argument */ |
| 2951 | ){ |
| 2952 | return TCL_OK; /* Do nothing */ |
| 2953 | } |
| 2954 | |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 2955 | /* |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 2956 | ** Usage: sqlite3_bind_zeroblob STMT IDX N |
| 2957 | ** |
| 2958 | ** Test the sqlite3_bind_zeroblob interface. STMT is a prepared statement. |
| 2959 | ** IDX is the index of a wildcard in the prepared statement. This command |
| 2960 | ** binds a N-byte zero-filled BLOB to the wildcard. |
| 2961 | */ |
| 2962 | static int test_bind_zeroblob( |
| 2963 | void * clientData, |
| 2964 | Tcl_Interp *interp, |
| 2965 | int objc, |
| 2966 | Tcl_Obj *CONST objv[] |
| 2967 | ){ |
| 2968 | sqlite3_stmt *pStmt; |
| 2969 | int idx; |
| 2970 | int n; |
| 2971 | int rc; |
| 2972 | |
| 2973 | if( objc!=4 ){ |
danielk1977 | 28c6630 | 2007-09-01 11:04:26 +0000 | [diff] [blame] | 2974 | Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N"); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 2975 | return TCL_ERROR; |
| 2976 | } |
| 2977 | |
| 2978 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2979 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 2980 | if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR; |
| 2981 | |
| 2982 | rc = sqlite3_bind_zeroblob(pStmt, idx, n); |
| 2983 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
| 2984 | if( rc!=SQLITE_OK ){ |
| 2985 | return TCL_ERROR; |
| 2986 | } |
| 2987 | |
| 2988 | return TCL_OK; |
| 2989 | } |
| 2990 | |
| 2991 | /* |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 2992 | ** Usage: sqlite3_bind_int STMT N VALUE |
| 2993 | ** |
| 2994 | ** Test the sqlite3_bind_int interface. STMT is a prepared statement. |
| 2995 | ** N is the index of a wildcard in the prepared statement. This command |
| 2996 | ** binds a 32-bit integer VALUE to that wildcard. |
| 2997 | */ |
| 2998 | static int test_bind_int( |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2999 | void * clientData, |
| 3000 | Tcl_Interp *interp, |
| 3001 | int objc, |
| 3002 | Tcl_Obj *CONST objv[] |
| 3003 | ){ |
| 3004 | sqlite3_stmt *pStmt; |
| 3005 | int idx; |
| 3006 | int value; |
| 3007 | int rc; |
| 3008 | |
| 3009 | if( objc!=4 ){ |
| 3010 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3011 | Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3012 | return TCL_ERROR; |
| 3013 | } |
| 3014 | |
| 3015 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3016 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 3017 | if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR; |
| 3018 | |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 3019 | rc = sqlite3_bind_int(pStmt, idx, value); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3020 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3021 | if( rc!=SQLITE_OK ){ |
| 3022 | return TCL_ERROR; |
| 3023 | } |
| 3024 | |
| 3025 | return TCL_OK; |
| 3026 | } |
| 3027 | |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3028 | |
| 3029 | /* |
| 3030 | ** Usage: sqlite3_bind_int64 STMT N VALUE |
| 3031 | ** |
| 3032 | ** Test the sqlite3_bind_int64 interface. STMT is a prepared statement. |
| 3033 | ** N is the index of a wildcard in the prepared statement. This command |
| 3034 | ** binds a 64-bit integer VALUE to that wildcard. |
| 3035 | */ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3036 | static int test_bind_int64( |
| 3037 | void * clientData, |
| 3038 | Tcl_Interp *interp, |
| 3039 | int objc, |
| 3040 | Tcl_Obj *CONST objv[] |
| 3041 | ){ |
| 3042 | sqlite3_stmt *pStmt; |
| 3043 | int idx; |
| 3044 | i64 value; |
| 3045 | int rc; |
| 3046 | |
| 3047 | if( objc!=4 ){ |
| 3048 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3049 | Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3050 | return TCL_ERROR; |
| 3051 | } |
| 3052 | |
| 3053 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3054 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 3055 | if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR; |
| 3056 | |
| 3057 | rc = sqlite3_bind_int64(pStmt, idx, value); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3058 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3059 | if( rc!=SQLITE_OK ){ |
| 3060 | return TCL_ERROR; |
| 3061 | } |
| 3062 | |
| 3063 | return TCL_OK; |
| 3064 | } |
| 3065 | |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3066 | |
| 3067 | /* |
| 3068 | ** Usage: sqlite3_bind_double STMT N VALUE |
| 3069 | ** |
| 3070 | ** Test the sqlite3_bind_double interface. STMT is a prepared statement. |
| 3071 | ** N is the index of a wildcard in the prepared statement. This command |
| 3072 | ** binds a 64-bit integer VALUE to that wildcard. |
| 3073 | */ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3074 | static int test_bind_double( |
| 3075 | void * clientData, |
| 3076 | Tcl_Interp *interp, |
| 3077 | int objc, |
| 3078 | Tcl_Obj *CONST objv[] |
| 3079 | ){ |
| 3080 | sqlite3_stmt *pStmt; |
| 3081 | int idx; |
| 3082 | double value; |
| 3083 | int rc; |
drh | a06f17f | 2008-05-11 11:07:06 +0000 | [diff] [blame] | 3084 | const char *zVal; |
| 3085 | int i; |
| 3086 | static const struct { |
| 3087 | const char *zName; /* Name of the special floating point value */ |
| 3088 | unsigned int iUpper; /* Upper 32 bits */ |
| 3089 | unsigned int iLower; /* Lower 32 bits */ |
| 3090 | } aSpecialFp[] = { |
| 3091 | { "NaN", 0x7fffffff, 0xffffffff }, |
| 3092 | { "SNaN", 0x7ff7ffff, 0xffffffff }, |
| 3093 | { "-NaN", 0xffffffff, 0xffffffff }, |
| 3094 | { "-SNaN", 0xfff7ffff, 0xffffffff }, |
| 3095 | { "+Inf", 0x7ff00000, 0x00000000 }, |
| 3096 | { "-Inf", 0xfff00000, 0x00000000 }, |
| 3097 | { "Epsilon", 0x00000000, 0x00000001 }, |
| 3098 | { "-Epsilon", 0x80000000, 0x00000001 }, |
| 3099 | { "NaN0", 0x7ff80000, 0x00000000 }, |
| 3100 | { "-NaN0", 0xfff80000, 0x00000000 }, |
| 3101 | }; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3102 | |
| 3103 | if( objc!=4 ){ |
| 3104 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3105 | Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3106 | return TCL_ERROR; |
| 3107 | } |
| 3108 | |
| 3109 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3110 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3111 | |
drh | 394f07e | 2008-04-28 15:23:02 +0000 | [diff] [blame] | 3112 | /* Intercept the string "NaN" and generate a NaN value for it. |
| 3113 | ** All other strings are passed through to Tcl_GetDoubleFromObj(). |
| 3114 | ** Tcl_GetDoubleFromObj() should understand "NaN" but some versions |
| 3115 | ** contain a bug. |
| 3116 | */ |
drh | a06f17f | 2008-05-11 11:07:06 +0000 | [diff] [blame] | 3117 | zVal = Tcl_GetString(objv[3]); |
| 3118 | for(i=0; i<sizeof(aSpecialFp)/sizeof(aSpecialFp[0]); i++){ |
| 3119 | if( strcmp(aSpecialFp[i].zName, zVal)==0 ){ |
| 3120 | sqlite3_uint64 x; |
| 3121 | x = aSpecialFp[i].iUpper; |
| 3122 | x <<= 32; |
| 3123 | x |= aSpecialFp[i].iLower; |
drh | 0a66733 | 2008-05-11 17:22:01 +0000 | [diff] [blame] | 3124 | assert( sizeof(value)==8 ); |
| 3125 | assert( sizeof(x)==8 ); |
| 3126 | memcpy(&value, &x, 8); |
drh | a06f17f | 2008-05-11 11:07:06 +0000 | [diff] [blame] | 3127 | break; |
| 3128 | } |
| 3129 | } |
| 3130 | if( i>=sizeof(aSpecialFp)/sizeof(aSpecialFp[0]) && |
| 3131 | Tcl_GetDoubleFromObj(interp, objv[3], &value) ){ |
drh | 394f07e | 2008-04-28 15:23:02 +0000 | [diff] [blame] | 3132 | return TCL_ERROR; |
| 3133 | } |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3134 | rc = sqlite3_bind_double(pStmt, idx, value); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3135 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3136 | if( rc!=SQLITE_OK ){ |
| 3137 | return TCL_ERROR; |
| 3138 | } |
| 3139 | |
| 3140 | return TCL_OK; |
| 3141 | } |
| 3142 | |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3143 | /* |
| 3144 | ** Usage: sqlite3_bind_null STMT N |
| 3145 | ** |
| 3146 | ** Test the sqlite3_bind_null interface. STMT is a prepared statement. |
| 3147 | ** N is the index of a wildcard in the prepared statement. This command |
| 3148 | ** binds a NULL to the wildcard. |
| 3149 | */ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3150 | static int test_bind_null( |
| 3151 | void * clientData, |
| 3152 | Tcl_Interp *interp, |
| 3153 | int objc, |
| 3154 | Tcl_Obj *CONST objv[] |
| 3155 | ){ |
| 3156 | sqlite3_stmt *pStmt; |
| 3157 | int idx; |
| 3158 | int rc; |
| 3159 | |
| 3160 | if( objc!=3 ){ |
| 3161 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3162 | Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3163 | return TCL_ERROR; |
| 3164 | } |
| 3165 | |
| 3166 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3167 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 3168 | |
| 3169 | rc = sqlite3_bind_null(pStmt, idx); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3170 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3171 | if( rc!=SQLITE_OK ){ |
| 3172 | return TCL_ERROR; |
| 3173 | } |
| 3174 | |
| 3175 | return TCL_OK; |
| 3176 | } |
| 3177 | |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3178 | /* |
| 3179 | ** Usage: sqlite3_bind_text STMT N STRING BYTES |
| 3180 | ** |
| 3181 | ** Test the sqlite3_bind_text interface. STMT is a prepared statement. |
| 3182 | ** N is the index of a wildcard in the prepared statement. This command |
| 3183 | ** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes |
| 3184 | ** long. |
| 3185 | */ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3186 | static int test_bind_text( |
| 3187 | void * clientData, |
| 3188 | Tcl_Interp *interp, |
| 3189 | int objc, |
| 3190 | Tcl_Obj *CONST objv[] |
| 3191 | ){ |
| 3192 | sqlite3_stmt *pStmt; |
| 3193 | int idx; |
| 3194 | int bytes; |
| 3195 | char *value; |
| 3196 | int rc; |
| 3197 | |
| 3198 | if( objc!=5 ){ |
| 3199 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3200 | Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3201 | return TCL_ERROR; |
| 3202 | } |
| 3203 | |
| 3204 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3205 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
drh | 10dfbbb | 2008-04-16 12:58:53 +0000 | [diff] [blame] | 3206 | value = (char*)Tcl_GetByteArrayFromObj(objv[3], &bytes); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3207 | if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR; |
| 3208 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 3209 | rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3210 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3211 | if( rc!=SQLITE_OK ){ |
drh | 473d179 | 2005-06-06 17:54:55 +0000 | [diff] [blame] | 3212 | Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3213 | return TCL_ERROR; |
| 3214 | } |
| 3215 | |
| 3216 | return TCL_OK; |
| 3217 | } |
| 3218 | |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3219 | /* |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 3220 | ** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3221 | ** |
| 3222 | ** Test the sqlite3_bind_text16 interface. STMT is a prepared statement. |
| 3223 | ** N is the index of a wildcard in the prepared statement. This command |
| 3224 | ** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes |
| 3225 | ** long. |
| 3226 | */ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3227 | static int test_bind_text16( |
| 3228 | void * clientData, |
| 3229 | Tcl_Interp *interp, |
| 3230 | int objc, |
| 3231 | Tcl_Obj *CONST objv[] |
| 3232 | ){ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3233 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3234 | sqlite3_stmt *pStmt; |
| 3235 | int idx; |
| 3236 | int bytes; |
| 3237 | char *value; |
| 3238 | int rc; |
| 3239 | |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 3240 | void (*xDel)() = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT); |
| 3241 | Tcl_Obj *oStmt = objv[objc-4]; |
| 3242 | Tcl_Obj *oN = objv[objc-3]; |
| 3243 | Tcl_Obj *oString = objv[objc-2]; |
| 3244 | Tcl_Obj *oBytes = objv[objc-1]; |
| 3245 | |
| 3246 | if( objc!=5 && objc!=6){ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3247 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3248 | Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3249 | return TCL_ERROR; |
| 3250 | } |
| 3251 | |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 3252 | if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR; |
| 3253 | if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR; |
| 3254 | value = (char*)Tcl_GetByteArrayFromObj(oString, 0); |
| 3255 | if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3256 | |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 3257 | rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3258 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3259 | if( rc!=SQLITE_OK ){ |
drh | ddff9ae | 2008-07-08 15:26:49 +0000 | [diff] [blame] | 3260 | Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3261 | return TCL_ERROR; |
| 3262 | } |
| 3263 | |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3264 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3265 | return TCL_OK; |
| 3266 | } |
| 3267 | |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3268 | /* |
danielk1977 | 5b159dc | 2007-05-17 16:34:43 +0000 | [diff] [blame] | 3269 | ** Usage: sqlite3_bind_blob ?-static? STMT N DATA BYTES |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3270 | ** |
| 3271 | ** Test the sqlite3_bind_blob interface. STMT is a prepared statement. |
| 3272 | ** N is the index of a wildcard in the prepared statement. This command |
| 3273 | ** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size. |
| 3274 | */ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3275 | static int test_bind_blob( |
| 3276 | void * clientData, |
| 3277 | Tcl_Interp *interp, |
| 3278 | int objc, |
| 3279 | Tcl_Obj *CONST objv[] |
| 3280 | ){ |
| 3281 | sqlite3_stmt *pStmt; |
| 3282 | int idx; |
| 3283 | int bytes; |
| 3284 | char *value; |
| 3285 | int rc; |
danielk1977 | 5b159dc | 2007-05-17 16:34:43 +0000 | [diff] [blame] | 3286 | sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3287 | |
danielk1977 | 5b159dc | 2007-05-17 16:34:43 +0000 | [diff] [blame] | 3288 | if( objc!=5 && objc!=6 ){ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3289 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3290 | Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3291 | return TCL_ERROR; |
| 3292 | } |
| 3293 | |
danielk1977 | 5b159dc | 2007-05-17 16:34:43 +0000 | [diff] [blame] | 3294 | if( objc==6 ){ |
| 3295 | xDestructor = SQLITE_STATIC; |
| 3296 | objv++; |
| 3297 | } |
| 3298 | |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3299 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3300 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 3301 | value = Tcl_GetString(objv[3]); |
danielk1977 | 49e4643 | 2004-05-27 13:55:27 +0000 | [diff] [blame] | 3302 | if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3303 | |
danielk1977 | 5b159dc | 2007-05-17 16:34:43 +0000 | [diff] [blame] | 3304 | rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3305 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3306 | if( rc!=SQLITE_OK ){ |
| 3307 | return TCL_ERROR; |
| 3308 | } |
| 3309 | |
| 3310 | return TCL_OK; |
| 3311 | } |
| 3312 | |
drh | 99ee360 | 2003-02-16 19:13:36 +0000 | [diff] [blame] | 3313 | /* |
drh | 75f6a03 | 2004-07-15 14:15:00 +0000 | [diff] [blame] | 3314 | ** Usage: sqlite3_bind_parameter_count STMT |
| 3315 | ** |
| 3316 | ** Return the number of wildcards in the given statement. |
| 3317 | */ |
| 3318 | static int test_bind_parameter_count( |
| 3319 | void * clientData, |
| 3320 | Tcl_Interp *interp, |
| 3321 | int objc, |
| 3322 | Tcl_Obj *CONST objv[] |
| 3323 | ){ |
| 3324 | sqlite3_stmt *pStmt; |
| 3325 | |
| 3326 | if( objc!=2 ){ |
| 3327 | Tcl_WrongNumArgs(interp, 1, objv, "STMT"); |
| 3328 | return TCL_ERROR; |
| 3329 | } |
| 3330 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3331 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt))); |
| 3332 | return TCL_OK; |
| 3333 | } |
| 3334 | |
| 3335 | /* |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 3336 | ** Usage: sqlite3_bind_parameter_name STMT N |
| 3337 | ** |
| 3338 | ** Return the name of the Nth wildcard. The first wildcard is 1. |
| 3339 | ** An empty string is returned if N is out of range or if the wildcard |
| 3340 | ** is nameless. |
| 3341 | */ |
| 3342 | static int test_bind_parameter_name( |
| 3343 | void * clientData, |
| 3344 | Tcl_Interp *interp, |
| 3345 | int objc, |
| 3346 | Tcl_Obj *CONST objv[] |
| 3347 | ){ |
| 3348 | sqlite3_stmt *pStmt; |
| 3349 | int i; |
| 3350 | |
| 3351 | if( objc!=3 ){ |
| 3352 | Tcl_WrongNumArgs(interp, 1, objv, "STMT N"); |
| 3353 | return TCL_ERROR; |
| 3354 | } |
| 3355 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3356 | if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR; |
| 3357 | Tcl_SetObjResult(interp, |
| 3358 | Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1) |
| 3359 | ); |
| 3360 | return TCL_OK; |
| 3361 | } |
| 3362 | |
| 3363 | /* |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 3364 | ** Usage: sqlite3_bind_parameter_index STMT NAME |
| 3365 | ** |
| 3366 | ** Return the index of the wildcard called NAME. Return 0 if there is |
| 3367 | ** no such wildcard. |
| 3368 | */ |
| 3369 | static int test_bind_parameter_index( |
| 3370 | void * clientData, |
| 3371 | Tcl_Interp *interp, |
| 3372 | int objc, |
| 3373 | Tcl_Obj *CONST objv[] |
| 3374 | ){ |
| 3375 | sqlite3_stmt *pStmt; |
| 3376 | |
| 3377 | if( objc!=3 ){ |
| 3378 | Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME"); |
| 3379 | return TCL_ERROR; |
| 3380 | } |
| 3381 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3382 | Tcl_SetObjResult(interp, |
| 3383 | Tcl_NewIntObj( |
| 3384 | sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2])) |
| 3385 | ) |
| 3386 | ); |
| 3387 | return TCL_OK; |
| 3388 | } |
| 3389 | |
| 3390 | /* |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 3391 | ** Usage: sqlite3_clear_bindings STMT |
| 3392 | ** |
| 3393 | */ |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 3394 | static int test_clear_bindings( |
| 3395 | void * clientData, |
| 3396 | Tcl_Interp *interp, |
| 3397 | int objc, |
| 3398 | Tcl_Obj *CONST objv[] |
| 3399 | ){ |
| 3400 | sqlite3_stmt *pStmt; |
| 3401 | |
| 3402 | if( objc!=2 ){ |
| 3403 | Tcl_WrongNumArgs(interp, 1, objv, "STMT"); |
| 3404 | return TCL_ERROR; |
| 3405 | } |
| 3406 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3407 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt))); |
| 3408 | return TCL_OK; |
| 3409 | } |
drh | f9cb7f5 | 2006-06-27 20:06:44 +0000 | [diff] [blame] | 3410 | |
| 3411 | /* |
| 3412 | ** Usage: sqlite3_sleep MILLISECONDS |
| 3413 | */ |
| 3414 | static int test_sleep( |
| 3415 | void * clientData, |
| 3416 | Tcl_Interp *interp, |
| 3417 | int objc, |
| 3418 | Tcl_Obj *CONST objv[] |
| 3419 | ){ |
| 3420 | int ms; |
| 3421 | |
| 3422 | if( objc!=2 ){ |
| 3423 | Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS"); |
| 3424 | return TCL_ERROR; |
| 3425 | } |
| 3426 | if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){ |
| 3427 | return TCL_ERROR; |
| 3428 | } |
| 3429 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms))); |
| 3430 | return TCL_OK; |
| 3431 | } |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 3432 | |
| 3433 | /* |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 3434 | ** Usage: sqlite3_extended_errcode DB |
| 3435 | ** |
| 3436 | ** Return the string representation of the most recent sqlite3_* API |
| 3437 | ** error code. e.g. "SQLITE_ERROR". |
| 3438 | */ |
| 3439 | static int test_ex_errcode( |
| 3440 | void * clientData, |
| 3441 | Tcl_Interp *interp, |
| 3442 | int objc, |
| 3443 | Tcl_Obj *CONST objv[] |
| 3444 | ){ |
| 3445 | sqlite3 *db; |
| 3446 | int rc; |
| 3447 | |
| 3448 | if( objc!=2 ){ |
| 3449 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3450 | Tcl_GetString(objv[0]), " DB", 0); |
| 3451 | return TCL_ERROR; |
| 3452 | } |
| 3453 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3454 | rc = sqlite3_extended_errcode(db); |
| 3455 | Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0); |
| 3456 | return TCL_OK; |
| 3457 | } |
| 3458 | |
| 3459 | |
| 3460 | /* |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3461 | ** Usage: sqlite3_errcode DB |
| 3462 | ** |
| 3463 | ** Return the string representation of the most recent sqlite3_* API |
| 3464 | ** error code. e.g. "SQLITE_ERROR". |
| 3465 | */ |
| 3466 | static int test_errcode( |
| 3467 | void * clientData, |
| 3468 | Tcl_Interp *interp, |
| 3469 | int objc, |
| 3470 | Tcl_Obj *CONST objv[] |
| 3471 | ){ |
| 3472 | sqlite3 *db; |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 3473 | int rc; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3474 | |
| 3475 | if( objc!=2 ){ |
| 3476 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3477 | Tcl_GetString(objv[0]), " DB", 0); |
| 3478 | return TCL_ERROR; |
| 3479 | } |
| 3480 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 3481 | rc = sqlite3_errcode(db); |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 3482 | Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3483 | return TCL_OK; |
| 3484 | } |
| 3485 | |
| 3486 | /* |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 3487 | ** Usage: sqlite3_errmsg DB |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3488 | ** |
| 3489 | ** Returns the UTF-8 representation of the error message string for the |
| 3490 | ** most recent sqlite3_* API call. |
| 3491 | */ |
| 3492 | static int test_errmsg( |
| 3493 | void * clientData, |
| 3494 | Tcl_Interp *interp, |
| 3495 | int objc, |
| 3496 | Tcl_Obj *CONST objv[] |
| 3497 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 3498 | sqlite3 *db; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3499 | const char *zErr; |
| 3500 | |
| 3501 | if( objc!=2 ){ |
| 3502 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3503 | Tcl_GetString(objv[0]), " DB", 0); |
| 3504 | return TCL_ERROR; |
| 3505 | } |
| 3506 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3507 | |
| 3508 | zErr = sqlite3_errmsg(db); |
| 3509 | Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1)); |
| 3510 | return TCL_OK; |
| 3511 | } |
| 3512 | |
| 3513 | /* |
| 3514 | ** Usage: test_errmsg16 DB |
| 3515 | ** |
| 3516 | ** Returns the UTF-16 representation of the error message string for the |
| 3517 | ** most recent sqlite3_* API call. This is a byte array object at the TCL |
| 3518 | ** level, and it includes the 0x00 0x00 terminator bytes at the end of the |
| 3519 | ** UTF-16 string. |
| 3520 | */ |
| 3521 | static int test_errmsg16( |
| 3522 | void * clientData, |
| 3523 | Tcl_Interp *interp, |
| 3524 | int objc, |
| 3525 | Tcl_Obj *CONST objv[] |
| 3526 | ){ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3527 | #ifndef SQLITE_OMIT_UTF16 |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 3528 | sqlite3 *db; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3529 | const void *zErr; |
drh | aed382f | 2009-04-01 18:40:32 +0000 | [diff] [blame] | 3530 | const char *z; |
danielk1977 | 950f054 | 2006-01-18 05:51:57 +0000 | [diff] [blame] | 3531 | int bytes = 0; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3532 | |
| 3533 | if( objc!=2 ){ |
| 3534 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3535 | Tcl_GetString(objv[0]), " DB", 0); |
| 3536 | return TCL_ERROR; |
| 3537 | } |
| 3538 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3539 | |
| 3540 | zErr = sqlite3_errmsg16(db); |
danielk1977 | 950f054 | 2006-01-18 05:51:57 +0000 | [diff] [blame] | 3541 | if( zErr ){ |
drh | aed382f | 2009-04-01 18:40:32 +0000 | [diff] [blame] | 3542 | z = zErr; |
| 3543 | for(bytes=0; z[bytes] || z[bytes+1]; bytes+=2){} |
danielk1977 | 950f054 | 2006-01-18 05:51:57 +0000 | [diff] [blame] | 3544 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3545 | Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes)); |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3546 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3547 | return TCL_OK; |
| 3548 | } |
| 3549 | |
| 3550 | /* |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3551 | ** Usage: sqlite3_prepare DB sql bytes ?tailvar? |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3552 | ** |
| 3553 | ** Compile up to <bytes> bytes of the supplied SQL string <sql> using |
| 3554 | ** database handle <DB>. The parameter <tailval> is the name of a global |
| 3555 | ** variable that is set to the unused portion of <sql> (if any). A |
| 3556 | ** STMT handle is returned. |
| 3557 | */ |
| 3558 | static int test_prepare( |
| 3559 | void * clientData, |
| 3560 | Tcl_Interp *interp, |
| 3561 | int objc, |
| 3562 | Tcl_Obj *CONST objv[] |
| 3563 | ){ |
| 3564 | sqlite3 *db; |
| 3565 | const char *zSql; |
| 3566 | int bytes; |
| 3567 | const char *zTail = 0; |
| 3568 | sqlite3_stmt *pStmt = 0; |
| 3569 | char zBuf[50]; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3570 | int rc; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3571 | |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3572 | if( objc!=5 && objc!=4 ){ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3573 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3574 | Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3575 | return TCL_ERROR; |
| 3576 | } |
| 3577 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3578 | zSql = Tcl_GetString(objv[2]); |
| 3579 | if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; |
| 3580 | |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3581 | rc = sqlite3_prepare(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 3582 | Tcl_ResetResult(interp); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3583 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3584 | if( zTail && objc>=5 ){ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3585 | if( bytes>=0 ){ |
| 3586 | bytes = bytes - (zTail-zSql); |
| 3587 | } |
danielk1977 | 3a2c8c8 | 2008-04-03 14:36:25 +0000 | [diff] [blame] | 3588 | if( strlen(zTail)<bytes ){ |
| 3589 | bytes = strlen(zTail); |
| 3590 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3591 | Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0); |
| 3592 | } |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3593 | if( rc!=SQLITE_OK ){ |
| 3594 | assert( pStmt==0 ); |
| 3595 | sprintf(zBuf, "(%d) ", rc); |
| 3596 | Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); |
| 3597 | return TCL_ERROR; |
| 3598 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3599 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3600 | if( pStmt ){ |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 3601 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3602 | Tcl_AppendResult(interp, zBuf, 0); |
| 3603 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3604 | return TCL_OK; |
| 3605 | } |
| 3606 | |
| 3607 | /* |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3608 | ** Usage: sqlite3_prepare_v2 DB sql bytes ?tailvar? |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3609 | ** |
| 3610 | ** Compile up to <bytes> bytes of the supplied SQL string <sql> using |
| 3611 | ** database handle <DB>. The parameter <tailval> is the name of a global |
| 3612 | ** variable that is set to the unused portion of <sql> (if any). A |
| 3613 | ** STMT handle is returned. |
| 3614 | */ |
| 3615 | static int test_prepare_v2( |
| 3616 | void * clientData, |
| 3617 | Tcl_Interp *interp, |
| 3618 | int objc, |
| 3619 | Tcl_Obj *CONST objv[] |
| 3620 | ){ |
| 3621 | sqlite3 *db; |
| 3622 | const char *zSql; |
| 3623 | int bytes; |
| 3624 | const char *zTail = 0; |
| 3625 | sqlite3_stmt *pStmt = 0; |
| 3626 | char zBuf[50]; |
| 3627 | int rc; |
| 3628 | |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3629 | if( objc!=5 && objc!=4 ){ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3630 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3631 | Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); |
| 3632 | return TCL_ERROR; |
| 3633 | } |
| 3634 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3635 | zSql = Tcl_GetString(objv[2]); |
| 3636 | if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; |
| 3637 | |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3638 | rc = sqlite3_prepare_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); |
danielk1977 | 7e29e95 | 2007-04-19 11:09:01 +0000 | [diff] [blame] | 3639 | assert(rc==SQLITE_OK || pStmt==0); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 3640 | Tcl_ResetResult(interp); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3641 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3642 | if( zTail && objc>=5 ){ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3643 | if( bytes>=0 ){ |
| 3644 | bytes = bytes - (zTail-zSql); |
| 3645 | } |
| 3646 | Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0); |
| 3647 | } |
| 3648 | if( rc!=SQLITE_OK ){ |
| 3649 | assert( pStmt==0 ); |
| 3650 | sprintf(zBuf, "(%d) ", rc); |
| 3651 | Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); |
| 3652 | return TCL_ERROR; |
| 3653 | } |
| 3654 | |
| 3655 | if( pStmt ){ |
| 3656 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
| 3657 | Tcl_AppendResult(interp, zBuf, 0); |
| 3658 | } |
| 3659 | return TCL_OK; |
| 3660 | } |
| 3661 | |
| 3662 | /* |
drh | 4837f53 | 2008-05-23 14:49:49 +0000 | [diff] [blame] | 3663 | ** Usage: sqlite3_prepare_tkt3134 DB |
| 3664 | ** |
| 3665 | ** Generate a prepared statement for a zero-byte string as a test |
| 3666 | ** for ticket #3134. The string should be preceeded by a zero byte. |
| 3667 | */ |
| 3668 | static int test_prepare_tkt3134( |
| 3669 | void * clientData, |
| 3670 | Tcl_Interp *interp, |
| 3671 | int objc, |
| 3672 | Tcl_Obj *CONST objv[] |
| 3673 | ){ |
| 3674 | sqlite3 *db; |
| 3675 | static const char zSql[] = "\000SELECT 1"; |
| 3676 | sqlite3_stmt *pStmt = 0; |
| 3677 | char zBuf[50]; |
| 3678 | int rc; |
| 3679 | |
| 3680 | if( objc!=2 ){ |
| 3681 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3682 | Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); |
| 3683 | return TCL_ERROR; |
| 3684 | } |
| 3685 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3686 | rc = sqlite3_prepare_v2(db, &zSql[1], 0, &pStmt, 0); |
| 3687 | assert(rc==SQLITE_OK || pStmt==0); |
| 3688 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 3689 | if( rc!=SQLITE_OK ){ |
| 3690 | assert( pStmt==0 ); |
| 3691 | sprintf(zBuf, "(%d) ", rc); |
| 3692 | Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); |
| 3693 | return TCL_ERROR; |
| 3694 | } |
| 3695 | |
| 3696 | if( pStmt ){ |
| 3697 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
| 3698 | Tcl_AppendResult(interp, zBuf, 0); |
| 3699 | } |
| 3700 | return TCL_OK; |
| 3701 | } |
| 3702 | |
| 3703 | /* |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3704 | ** Usage: sqlite3_prepare16 DB sql bytes tailvar |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3705 | ** |
| 3706 | ** Compile up to <bytes> bytes of the supplied SQL string <sql> using |
| 3707 | ** database handle <DB>. The parameter <tailval> is the name of a global |
| 3708 | ** variable that is set to the unused portion of <sql> (if any). A |
| 3709 | ** STMT handle is returned. |
| 3710 | */ |
| 3711 | static int test_prepare16( |
| 3712 | void * clientData, |
| 3713 | Tcl_Interp *interp, |
| 3714 | int objc, |
| 3715 | Tcl_Obj *CONST objv[] |
| 3716 | ){ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3717 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3718 | sqlite3 *db; |
| 3719 | const void *zSql; |
| 3720 | const void *zTail = 0; |
| 3721 | Tcl_Obj *pTail = 0; |
| 3722 | sqlite3_stmt *pStmt = 0; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3723 | char zBuf[50]; |
| 3724 | int rc; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3725 | int bytes; /* The integer specified as arg 3 */ |
| 3726 | int objlen; /* The byte-array length of arg 2 */ |
| 3727 | |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3728 | if( objc!=5 && objc!=4 ){ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3729 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3730 | Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3731 | return TCL_ERROR; |
| 3732 | } |
| 3733 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3734 | zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen); |
| 3735 | if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; |
| 3736 | |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3737 | rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3738 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 3739 | if( rc ){ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3740 | return TCL_ERROR; |
| 3741 | } |
| 3742 | |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3743 | if( objc>=5 ){ |
| 3744 | if( zTail ){ |
| 3745 | objlen = objlen - ((u8 *)zTail-(u8 *)zSql); |
| 3746 | }else{ |
| 3747 | objlen = 0; |
| 3748 | } |
| 3749 | pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen); |
| 3750 | Tcl_IncrRefCount(pTail); |
| 3751 | Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0); |
| 3752 | Tcl_DecrRefCount(pTail); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3753 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3754 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3755 | if( pStmt ){ |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 3756 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3757 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3758 | Tcl_AppendResult(interp, zBuf, 0); |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3759 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3760 | return TCL_OK; |
| 3761 | } |
| 3762 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3763 | /* |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3764 | ** Usage: sqlite3_prepare16_v2 DB sql bytes ?tailvar? |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3765 | ** |
| 3766 | ** Compile up to <bytes> bytes of the supplied SQL string <sql> using |
| 3767 | ** database handle <DB>. The parameter <tailval> is the name of a global |
| 3768 | ** variable that is set to the unused portion of <sql> (if any). A |
| 3769 | ** STMT handle is returned. |
| 3770 | */ |
| 3771 | static int test_prepare16_v2( |
| 3772 | void * clientData, |
| 3773 | Tcl_Interp *interp, |
| 3774 | int objc, |
| 3775 | Tcl_Obj *CONST objv[] |
| 3776 | ){ |
| 3777 | #ifndef SQLITE_OMIT_UTF16 |
| 3778 | sqlite3 *db; |
| 3779 | const void *zSql; |
| 3780 | const void *zTail = 0; |
| 3781 | Tcl_Obj *pTail = 0; |
| 3782 | sqlite3_stmt *pStmt = 0; |
| 3783 | char zBuf[50]; |
| 3784 | int rc; |
| 3785 | int bytes; /* The integer specified as arg 3 */ |
| 3786 | int objlen; /* The byte-array length of arg 2 */ |
| 3787 | |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3788 | if( objc!=5 && objc!=4 ){ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3789 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3790 | Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3791 | return TCL_ERROR; |
| 3792 | } |
| 3793 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3794 | zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen); |
| 3795 | if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; |
| 3796 | |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3797 | rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3798 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 3799 | if( rc ){ |
| 3800 | return TCL_ERROR; |
| 3801 | } |
| 3802 | |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3803 | if( objc>=5 ){ |
| 3804 | if( zTail ){ |
| 3805 | objlen = objlen - ((u8 *)zTail-(u8 *)zSql); |
| 3806 | }else{ |
| 3807 | objlen = 0; |
| 3808 | } |
| 3809 | pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen); |
| 3810 | Tcl_IncrRefCount(pTail); |
| 3811 | Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0); |
| 3812 | Tcl_DecrRefCount(pTail); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3813 | } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3814 | |
| 3815 | if( pStmt ){ |
| 3816 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
| 3817 | } |
| 3818 | Tcl_AppendResult(interp, zBuf, 0); |
| 3819 | #endif /* SQLITE_OMIT_UTF16 */ |
| 3820 | return TCL_OK; |
| 3821 | } |
| 3822 | |
| 3823 | /* |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3824 | ** Usage: sqlite3_open filename ?options-list? |
| 3825 | */ |
| 3826 | static int test_open( |
| 3827 | void * clientData, |
| 3828 | Tcl_Interp *interp, |
| 3829 | int objc, |
| 3830 | Tcl_Obj *CONST objv[] |
| 3831 | ){ |
| 3832 | const char *zFilename; |
| 3833 | sqlite3 *db; |
| 3834 | int rc; |
| 3835 | char zBuf[100]; |
| 3836 | |
drh | afc9104 | 2008-02-21 02:09:45 +0000 | [diff] [blame] | 3837 | if( objc!=3 && objc!=2 && objc!=1 ){ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3838 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3839 | Tcl_GetString(objv[0]), " filename options-list", 0); |
| 3840 | return TCL_ERROR; |
| 3841 | } |
| 3842 | |
drh | afc9104 | 2008-02-21 02:09:45 +0000 | [diff] [blame] | 3843 | zFilename = objc>1 ? Tcl_GetString(objv[1]) : 0; |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 3844 | rc = sqlite3_open(zFilename, &db); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3845 | |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 3846 | if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3847 | Tcl_AppendResult(interp, zBuf, 0); |
| 3848 | return TCL_OK; |
| 3849 | } |
| 3850 | |
| 3851 | /* |
dan | 286ab7c | 2011-05-06 18:34:54 +0000 | [diff] [blame] | 3852 | ** Usage: sqlite3_open_v2 FILENAME FLAGS VFS |
| 3853 | */ |
| 3854 | static int test_open_v2( |
| 3855 | void * clientData, |
| 3856 | Tcl_Interp *interp, |
| 3857 | int objc, |
| 3858 | Tcl_Obj *CONST objv[] |
| 3859 | ){ |
| 3860 | const char *zFilename; |
| 3861 | const char *zVfs; |
| 3862 | int flags = 0; |
| 3863 | sqlite3 *db; |
| 3864 | int rc; |
| 3865 | char zBuf[100]; |
| 3866 | |
| 3867 | int nFlag; |
| 3868 | Tcl_Obj **apFlag; |
| 3869 | int i; |
| 3870 | |
| 3871 | if( objc!=4 ){ |
| 3872 | Tcl_WrongNumArgs(interp, 1, objv, "FILENAME FLAGS VFS"); |
| 3873 | return TCL_ERROR; |
| 3874 | } |
| 3875 | zFilename = Tcl_GetString(objv[1]); |
| 3876 | zVfs = Tcl_GetString(objv[3]); |
| 3877 | if( zVfs[0]==0x00 ) zVfs = 0; |
| 3878 | |
| 3879 | rc = Tcl_ListObjGetElements(interp, objv[2], &nFlag, &apFlag); |
| 3880 | if( rc!=TCL_OK ) return rc; |
| 3881 | for(i=0; i<nFlag; i++){ |
| 3882 | int iFlag; |
| 3883 | struct OpenFlag { |
| 3884 | const char *zFlag; |
| 3885 | int flag; |
| 3886 | } aFlag[] = { |
| 3887 | { "SQLITE_OPEN_READONLY", SQLITE_OPEN_READONLY }, |
| 3888 | { "SQLITE_OPEN_READWRITE", SQLITE_OPEN_READWRITE }, |
| 3889 | { "SQLITE_OPEN_CREATE", SQLITE_OPEN_CREATE }, |
| 3890 | { "SQLITE_OPEN_DELETEONCLOSE", SQLITE_OPEN_DELETEONCLOSE }, |
| 3891 | { "SQLITE_OPEN_EXCLUSIVE", SQLITE_OPEN_EXCLUSIVE }, |
| 3892 | { "SQLITE_OPEN_AUTOPROXY", SQLITE_OPEN_AUTOPROXY }, |
| 3893 | { "SQLITE_OPEN_MAIN_DB", SQLITE_OPEN_MAIN_DB }, |
| 3894 | { "SQLITE_OPEN_TEMP_DB", SQLITE_OPEN_TEMP_DB }, |
| 3895 | { "SQLITE_OPEN_TRANSIENT_DB", SQLITE_OPEN_TRANSIENT_DB }, |
| 3896 | { "SQLITE_OPEN_MAIN_JOURNAL", SQLITE_OPEN_MAIN_JOURNAL }, |
| 3897 | { "SQLITE_OPEN_TEMP_JOURNAL", SQLITE_OPEN_TEMP_JOURNAL }, |
| 3898 | { "SQLITE_OPEN_SUBJOURNAL", SQLITE_OPEN_SUBJOURNAL }, |
| 3899 | { "SQLITE_OPEN_MASTER_JOURNAL", SQLITE_OPEN_MASTER_JOURNAL }, |
| 3900 | { "SQLITE_OPEN_NOMUTEX", SQLITE_OPEN_NOMUTEX }, |
| 3901 | { "SQLITE_OPEN_FULLMUTEX", SQLITE_OPEN_FULLMUTEX }, |
| 3902 | { "SQLITE_OPEN_SHAREDCACHE", SQLITE_OPEN_SHAREDCACHE }, |
| 3903 | { "SQLITE_OPEN_PRIVATECACHE", SQLITE_OPEN_PRIVATECACHE }, |
| 3904 | { "SQLITE_OPEN_WAL", SQLITE_OPEN_WAL }, |
| 3905 | { "SQLITE_OPEN_URI", SQLITE_OPEN_URI }, |
| 3906 | { 0, 0 } |
| 3907 | }; |
| 3908 | rc = Tcl_GetIndexFromObjStruct(interp, apFlag[i], aFlag, sizeof(aFlag[0]), |
| 3909 | "flag", 0, &iFlag |
| 3910 | ); |
| 3911 | if( rc!=TCL_OK ) return rc; |
| 3912 | flags |= aFlag[iFlag].flag; |
| 3913 | } |
| 3914 | |
| 3915 | rc = sqlite3_open_v2(zFilename, &db, flags, zVfs); |
| 3916 | if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; |
| 3917 | Tcl_AppendResult(interp, zBuf, 0); |
| 3918 | return TCL_OK; |
| 3919 | } |
| 3920 | |
| 3921 | /* |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3922 | ** Usage: sqlite3_open16 filename options |
| 3923 | */ |
| 3924 | static int test_open16( |
| 3925 | void * clientData, |
| 3926 | Tcl_Interp *interp, |
| 3927 | int objc, |
| 3928 | Tcl_Obj *CONST objv[] |
| 3929 | ){ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3930 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3931 | const void *zFilename; |
| 3932 | sqlite3 *db; |
| 3933 | int rc; |
| 3934 | char zBuf[100]; |
| 3935 | |
| 3936 | if( objc!=3 ){ |
| 3937 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3938 | Tcl_GetString(objv[0]), " filename options-list", 0); |
| 3939 | return TCL_ERROR; |
| 3940 | } |
| 3941 | |
| 3942 | zFilename = Tcl_GetByteArrayFromObj(objv[1], 0); |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 3943 | rc = sqlite3_open16(zFilename, &db); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3944 | |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 3945 | if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3946 | Tcl_AppendResult(interp, zBuf, 0); |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3947 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3948 | return TCL_OK; |
| 3949 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 3950 | |
| 3951 | /* |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 3952 | ** Usage: sqlite3_complete16 <UTF-16 string> |
| 3953 | ** |
| 3954 | ** Return 1 if the supplied argument is a complete SQL statement, or zero |
| 3955 | ** otherwise. |
| 3956 | */ |
| 3957 | static int test_complete16( |
| 3958 | void * clientData, |
| 3959 | Tcl_Interp *interp, |
| 3960 | int objc, |
| 3961 | Tcl_Obj *CONST objv[] |
| 3962 | ){ |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 3963 | #if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16) |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 3964 | char *zBuf; |
| 3965 | |
| 3966 | if( objc!=2 ){ |
| 3967 | Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>"); |
| 3968 | return TCL_ERROR; |
| 3969 | } |
| 3970 | |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 3971 | zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 3972 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf))); |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 3973 | #endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 3974 | return TCL_OK; |
| 3975 | } |
| 3976 | |
| 3977 | /* |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3978 | ** Usage: sqlite3_step STMT |
| 3979 | ** |
| 3980 | ** Advance the statement to the next row. |
| 3981 | */ |
danielk1977 | 17240fd | 2004-05-26 00:07:25 +0000 | [diff] [blame] | 3982 | static int test_step( |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3983 | void * clientData, |
| 3984 | Tcl_Interp *interp, |
| 3985 | int objc, |
| 3986 | Tcl_Obj *CONST objv[] |
| 3987 | ){ |
| 3988 | sqlite3_stmt *pStmt; |
| 3989 | int rc; |
| 3990 | |
danielk1977 | e1cd987 | 2004-05-22 10:33:04 +0000 | [diff] [blame] | 3991 | if( objc!=2 ){ |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3992 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3993 | Tcl_GetString(objv[0]), " STMT", 0); |
| 3994 | return TCL_ERROR; |
| 3995 | } |
| 3996 | |
| 3997 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
danielk1977 | 17240fd | 2004-05-26 00:07:25 +0000 | [diff] [blame] | 3998 | rc = sqlite3_step(pStmt); |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3999 | |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 4000 | /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4001 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); |
danielk1977 | e1cd987 | 2004-05-22 10:33:04 +0000 | [diff] [blame] | 4002 | return TCL_OK; |
| 4003 | } |
| 4004 | |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 4005 | static int test_sql( |
| 4006 | void * clientData, |
| 4007 | Tcl_Interp *interp, |
| 4008 | int objc, |
| 4009 | Tcl_Obj *CONST objv[] |
| 4010 | ){ |
| 4011 | sqlite3_stmt *pStmt; |
| 4012 | |
| 4013 | if( objc!=2 ){ |
| 4014 | Tcl_WrongNumArgs(interp, 1, objv, "STMT"); |
| 4015 | return TCL_ERROR; |
| 4016 | } |
| 4017 | |
| 4018 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4019 | Tcl_SetResult(interp, (char *)sqlite3_sql(pStmt), TCL_VOLATILE); |
| 4020 | return TCL_OK; |
| 4021 | } |
| 4022 | |
danielk1977 | e1cd987 | 2004-05-22 10:33:04 +0000 | [diff] [blame] | 4023 | /* |
danielk1977 | 17240fd | 2004-05-26 00:07:25 +0000 | [diff] [blame] | 4024 | ** Usage: sqlite3_column_count STMT |
| 4025 | ** |
| 4026 | ** Return the number of columns returned by the sql statement STMT. |
| 4027 | */ |
| 4028 | static int test_column_count( |
| 4029 | void * clientData, |
| 4030 | Tcl_Interp *interp, |
| 4031 | int objc, |
| 4032 | Tcl_Obj *CONST objv[] |
| 4033 | ){ |
| 4034 | sqlite3_stmt *pStmt; |
| 4035 | |
| 4036 | if( objc!=2 ){ |
| 4037 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4038 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 4039 | return TCL_ERROR; |
| 4040 | } |
| 4041 | |
| 4042 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4043 | |
| 4044 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt))); |
| 4045 | return TCL_OK; |
| 4046 | } |
| 4047 | |
| 4048 | /* |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4049 | ** Usage: sqlite3_column_type STMT column |
| 4050 | ** |
| 4051 | ** Return the type of the data in column 'column' of the current row. |
| 4052 | */ |
| 4053 | static int test_column_type( |
| 4054 | void * clientData, |
| 4055 | Tcl_Interp *interp, |
| 4056 | int objc, |
| 4057 | Tcl_Obj *CONST objv[] |
| 4058 | ){ |
| 4059 | sqlite3_stmt *pStmt; |
| 4060 | int col; |
| 4061 | int tp; |
| 4062 | |
| 4063 | if( objc!=3 ){ |
| 4064 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4065 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 4066 | return TCL_ERROR; |
| 4067 | } |
| 4068 | |
| 4069 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4070 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 4071 | |
| 4072 | tp = sqlite3_column_type(pStmt, col); |
| 4073 | switch( tp ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 4074 | case SQLITE_INTEGER: |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4075 | Tcl_SetResult(interp, "INTEGER", TCL_STATIC); |
| 4076 | break; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 4077 | case SQLITE_NULL: |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4078 | Tcl_SetResult(interp, "NULL", TCL_STATIC); |
| 4079 | break; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 4080 | case SQLITE_FLOAT: |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4081 | Tcl_SetResult(interp, "FLOAT", TCL_STATIC); |
| 4082 | break; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 4083 | case SQLITE_TEXT: |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4084 | Tcl_SetResult(interp, "TEXT", TCL_STATIC); |
| 4085 | break; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 4086 | case SQLITE_BLOB: |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4087 | Tcl_SetResult(interp, "BLOB", TCL_STATIC); |
| 4088 | break; |
| 4089 | default: |
| 4090 | assert(0); |
| 4091 | } |
| 4092 | |
| 4093 | return TCL_OK; |
| 4094 | } |
| 4095 | |
| 4096 | /* |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4097 | ** Usage: sqlite3_column_int64 STMT column |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4098 | ** |
| 4099 | ** Return the data in column 'column' of the current row cast as an |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4100 | ** wide (64-bit) integer. |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4101 | */ |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4102 | static int test_column_int64( |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4103 | void * clientData, |
| 4104 | Tcl_Interp *interp, |
| 4105 | int objc, |
| 4106 | Tcl_Obj *CONST objv[] |
| 4107 | ){ |
| 4108 | sqlite3_stmt *pStmt; |
| 4109 | int col; |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4110 | i64 iVal; |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4111 | |
| 4112 | if( objc!=3 ){ |
| 4113 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4114 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 4115 | return TCL_ERROR; |
| 4116 | } |
| 4117 | |
| 4118 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4119 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 4120 | |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4121 | iVal = sqlite3_column_int64(pStmt, col); |
| 4122 | Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal)); |
| 4123 | return TCL_OK; |
| 4124 | } |
| 4125 | |
| 4126 | /* |
danielk1977 | ea61b2c | 2004-05-27 01:49:51 +0000 | [diff] [blame] | 4127 | ** Usage: sqlite3_column_blob STMT column |
| 4128 | */ |
| 4129 | static int test_column_blob( |
| 4130 | void * clientData, |
| 4131 | Tcl_Interp *interp, |
| 4132 | int objc, |
| 4133 | Tcl_Obj *CONST objv[] |
| 4134 | ){ |
| 4135 | sqlite3_stmt *pStmt; |
| 4136 | int col; |
| 4137 | |
| 4138 | int len; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 4139 | const void *pBlob; |
danielk1977 | ea61b2c | 2004-05-27 01:49:51 +0000 | [diff] [blame] | 4140 | |
| 4141 | if( objc!=3 ){ |
| 4142 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4143 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 4144 | return TCL_ERROR; |
| 4145 | } |
| 4146 | |
| 4147 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4148 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 4149 | |
danielk1977 | ea61b2c | 2004-05-27 01:49:51 +0000 | [diff] [blame] | 4150 | len = sqlite3_column_bytes(pStmt, col); |
drh | 9310ef2 | 2007-04-27 17:16:20 +0000 | [diff] [blame] | 4151 | pBlob = sqlite3_column_blob(pStmt, col); |
danielk1977 | ea61b2c | 2004-05-27 01:49:51 +0000 | [diff] [blame] | 4152 | Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len)); |
| 4153 | return TCL_OK; |
| 4154 | } |
| 4155 | |
| 4156 | /* |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4157 | ** Usage: sqlite3_column_double STMT column |
| 4158 | ** |
| 4159 | ** Return the data in column 'column' of the current row cast as a double. |
| 4160 | */ |
| 4161 | static int test_column_double( |
| 4162 | void * clientData, |
| 4163 | Tcl_Interp *interp, |
| 4164 | int objc, |
| 4165 | Tcl_Obj *CONST objv[] |
| 4166 | ){ |
| 4167 | sqlite3_stmt *pStmt; |
| 4168 | int col; |
| 4169 | double rVal; |
| 4170 | |
| 4171 | if( objc!=3 ){ |
| 4172 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4173 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 4174 | return TCL_ERROR; |
| 4175 | } |
| 4176 | |
| 4177 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4178 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 4179 | |
| 4180 | rVal = sqlite3_column_double(pStmt, col); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 4181 | Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal)); |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4182 | return TCL_OK; |
| 4183 | } |
| 4184 | |
| 4185 | /* |
danielk1977 | 17240fd | 2004-05-26 00:07:25 +0000 | [diff] [blame] | 4186 | ** Usage: sqlite3_data_count STMT |
| 4187 | ** |
| 4188 | ** Return the number of columns returned by the sql statement STMT. |
| 4189 | */ |
| 4190 | static int test_data_count( |
| 4191 | void * clientData, |
| 4192 | Tcl_Interp *interp, |
| 4193 | int objc, |
| 4194 | Tcl_Obj *CONST objv[] |
| 4195 | ){ |
| 4196 | sqlite3_stmt *pStmt; |
| 4197 | |
| 4198 | if( objc!=2 ){ |
| 4199 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4200 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 4201 | return TCL_ERROR; |
| 4202 | } |
| 4203 | |
| 4204 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4205 | |
| 4206 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt))); |
| 4207 | return TCL_OK; |
| 4208 | } |
| 4209 | |
| 4210 | /* |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4211 | ** Usage: sqlite3_column_text STMT column |
| 4212 | ** |
| 4213 | ** Usage: sqlite3_column_decltype STMT column |
| 4214 | ** |
| 4215 | ** Usage: sqlite3_column_name STMT column |
| 4216 | */ |
| 4217 | static int test_stmt_utf8( |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 4218 | void * clientData, /* Pointer to SQLite API function to be invoke */ |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4219 | Tcl_Interp *interp, |
| 4220 | int objc, |
| 4221 | Tcl_Obj *CONST objv[] |
| 4222 | ){ |
| 4223 | sqlite3_stmt *pStmt; |
| 4224 | int col; |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 4225 | const char *(*xFunc)(sqlite3_stmt*, int); |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 4226 | const char *zRet; |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4227 | |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 4228 | xFunc = (const char *(*)(sqlite3_stmt*, int))clientData; |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4229 | if( objc!=3 ){ |
| 4230 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4231 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 4232 | return TCL_ERROR; |
| 4233 | } |
| 4234 | |
| 4235 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4236 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 4237 | zRet = xFunc(pStmt, col); |
| 4238 | if( zRet ){ |
| 4239 | Tcl_SetResult(interp, (char *)zRet, 0); |
| 4240 | } |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4241 | return TCL_OK; |
| 4242 | } |
| 4243 | |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4244 | static int test_global_recover( |
| 4245 | void * clientData, |
| 4246 | Tcl_Interp *interp, |
| 4247 | int objc, |
| 4248 | Tcl_Obj *CONST objv[] |
| 4249 | ){ |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 4250 | #ifndef SQLITE_OMIT_DEPRECATED |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4251 | int rc; |
| 4252 | if( objc!=1 ){ |
| 4253 | Tcl_WrongNumArgs(interp, 1, objv, ""); |
| 4254 | return TCL_ERROR; |
| 4255 | } |
| 4256 | rc = sqlite3_global_recover(); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4257 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4258 | #endif |
| 4259 | return TCL_OK; |
| 4260 | } |
| 4261 | |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4262 | /* |
| 4263 | ** Usage: sqlite3_column_text STMT column |
| 4264 | ** |
| 4265 | ** Usage: sqlite3_column_decltype STMT column |
| 4266 | ** |
| 4267 | ** Usage: sqlite3_column_name STMT column |
| 4268 | */ |
| 4269 | static int test_stmt_utf16( |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 4270 | void * clientData, /* Pointer to SQLite API function to be invoked */ |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4271 | Tcl_Interp *interp, |
| 4272 | int objc, |
| 4273 | Tcl_Obj *CONST objv[] |
| 4274 | ){ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 4275 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4276 | sqlite3_stmt *pStmt; |
| 4277 | int col; |
| 4278 | Tcl_Obj *pRet; |
| 4279 | const void *zName16; |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 4280 | const void *(*xFunc)(sqlite3_stmt*, int); |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4281 | |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 4282 | xFunc = (const void *(*)(sqlite3_stmt*, int))clientData; |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4283 | if( objc!=3 ){ |
| 4284 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4285 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 4286 | return TCL_ERROR; |
| 4287 | } |
| 4288 | |
| 4289 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4290 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 4291 | |
| 4292 | zName16 = xFunc(pStmt, col); |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 4293 | if( zName16 ){ |
drh | aed382f | 2009-04-01 18:40:32 +0000 | [diff] [blame] | 4294 | int n; |
| 4295 | const char *z = zName16; |
| 4296 | for(n=0; z[n] || z[n+1]; n+=2){} |
| 4297 | pRet = Tcl_NewByteArrayObj(zName16, n+2); |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 4298 | Tcl_SetObjResult(interp, pRet); |
| 4299 | } |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 4300 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4301 | |
| 4302 | return TCL_OK; |
| 4303 | } |
| 4304 | |
| 4305 | /* |
| 4306 | ** Usage: sqlite3_column_int STMT column |
| 4307 | ** |
| 4308 | ** Usage: sqlite3_column_bytes STMT column |
| 4309 | ** |
| 4310 | ** Usage: sqlite3_column_bytes16 STMT column |
| 4311 | ** |
| 4312 | */ |
| 4313 | static int test_stmt_int( |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 4314 | void * clientData, /* Pointer to SQLite API function to be invoked */ |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4315 | Tcl_Interp *interp, |
| 4316 | int objc, |
| 4317 | Tcl_Obj *CONST objv[] |
| 4318 | ){ |
| 4319 | sqlite3_stmt *pStmt; |
| 4320 | int col; |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 4321 | int (*xFunc)(sqlite3_stmt*, int); |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4322 | |
danielk1977 | 55a25a1 | 2008-09-11 10:29:15 +0000 | [diff] [blame] | 4323 | xFunc = (int (*)(sqlite3_stmt*, int))clientData; |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4324 | if( objc!=3 ){ |
| 4325 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4326 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 4327 | return TCL_ERROR; |
| 4328 | } |
| 4329 | |
| 4330 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4331 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 4332 | |
| 4333 | Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col))); |
| 4334 | return TCL_OK; |
| 4335 | } |
| 4336 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 4337 | /* |
drh | cacb208 | 2005-01-11 15:28:33 +0000 | [diff] [blame] | 4338 | ** Usage: sqlite_set_magic DB MAGIC-NUMBER |
| 4339 | ** |
| 4340 | ** Set the db->magic value. This is used to test error recovery logic. |
| 4341 | */ |
| 4342 | static int sqlite_set_magic( |
| 4343 | void * clientData, |
| 4344 | Tcl_Interp *interp, |
| 4345 | int argc, |
| 4346 | char **argv |
| 4347 | ){ |
| 4348 | sqlite3 *db; |
| 4349 | if( argc!=3 ){ |
| 4350 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 4351 | " DB MAGIC", 0); |
| 4352 | return TCL_ERROR; |
| 4353 | } |
| 4354 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 4355 | if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){ |
| 4356 | db->magic = SQLITE_MAGIC_OPEN; |
| 4357 | }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){ |
| 4358 | db->magic = SQLITE_MAGIC_CLOSED; |
| 4359 | }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){ |
| 4360 | db->magic = SQLITE_MAGIC_BUSY; |
| 4361 | }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){ |
| 4362 | db->magic = SQLITE_MAGIC_ERROR; |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 4363 | }else if( Tcl_GetInt(interp, argv[2], (int*)&db->magic) ){ |
drh | cacb208 | 2005-01-11 15:28:33 +0000 | [diff] [blame] | 4364 | return TCL_ERROR; |
| 4365 | } |
| 4366 | return TCL_OK; |
| 4367 | } |
| 4368 | |
| 4369 | /* |
drh | c5cdca6 | 2005-01-11 16:54:14 +0000 | [diff] [blame] | 4370 | ** Usage: sqlite3_interrupt DB |
| 4371 | ** |
| 4372 | ** Trigger an interrupt on DB |
| 4373 | */ |
| 4374 | static int test_interrupt( |
| 4375 | void * clientData, |
| 4376 | Tcl_Interp *interp, |
| 4377 | int argc, |
| 4378 | char **argv |
| 4379 | ){ |
| 4380 | sqlite3 *db; |
| 4381 | if( argc!=2 ){ |
| 4382 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0); |
| 4383 | return TCL_ERROR; |
| 4384 | } |
| 4385 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 4386 | sqlite3_interrupt(db); |
| 4387 | return TCL_OK; |
| 4388 | } |
| 4389 | |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 4390 | static u8 *sqlite3_stack_baseline = 0; |
| 4391 | |
drh | c5cdca6 | 2005-01-11 16:54:14 +0000 | [diff] [blame] | 4392 | /* |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 4393 | ** Fill the stack with a known bitpattern. |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 4394 | */ |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 4395 | static void prepStack(void){ |
| 4396 | int i; |
| 4397 | u32 bigBuf[65536]; |
drh | bc2be0c | 2011-08-30 00:53:50 +0000 | [diff] [blame] | 4398 | for(i=0; i<sizeof(bigBuf)/sizeof(bigBuf[0]); i++) bigBuf[i] = 0xdeadbeef; |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 4399 | sqlite3_stack_baseline = (u8*)&bigBuf[65536]; |
| 4400 | } |
| 4401 | |
| 4402 | /* |
| 4403 | ** Get the current stack depth. Used for debugging only. |
| 4404 | */ |
| 4405 | u64 sqlite3StackDepth(void){ |
| 4406 | u8 x; |
| 4407 | return (u64)(sqlite3_stack_baseline - &x); |
| 4408 | } |
| 4409 | |
| 4410 | /* |
| 4411 | ** Usage: sqlite3_stack_used DB SQL |
| 4412 | ** |
| 4413 | ** Try to measure the amount of stack space used by a call to sqlite3_exec |
| 4414 | */ |
| 4415 | static int test_stack_used( |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 4416 | void * clientData, |
| 4417 | Tcl_Interp *interp, |
| 4418 | int argc, |
| 4419 | char **argv |
| 4420 | ){ |
| 4421 | sqlite3 *db; |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 4422 | int i; |
| 4423 | if( argc!=3 ){ |
| 4424 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 4425 | " DB SQL", 0); |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 4426 | return TCL_ERROR; |
| 4427 | } |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 4428 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 4429 | prepStack(); |
drh | 3752785 | 2006-03-16 16:19:56 +0000 | [diff] [blame] | 4430 | (void)sqlite3_exec(db, argv[2], 0, 0, 0); |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 4431 | for(i=65535; i>=0 && ((u32*)sqlite3_stack_baseline)[-i]==0xdeadbeef; i--){} |
| 4432 | Tcl_SetObjResult(interp, Tcl_NewIntObj(i*4)); |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 4433 | return TCL_OK; |
| 4434 | } |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 4435 | |
| 4436 | /* |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 4437 | ** Usage: sqlite_delete_function DB function-name |
| 4438 | ** |
| 4439 | ** Delete the user function 'function-name' from database handle DB. It |
| 4440 | ** is assumed that the user function was created as UTF8, any number of |
| 4441 | ** arguments (the way the TCL interface does it). |
| 4442 | */ |
| 4443 | static int delete_function( |
| 4444 | void * clientData, |
| 4445 | Tcl_Interp *interp, |
| 4446 | int argc, |
| 4447 | char **argv |
| 4448 | ){ |
| 4449 | int rc; |
| 4450 | sqlite3 *db; |
| 4451 | if( argc!=3 ){ |
| 4452 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 4453 | " DB function-name", 0); |
| 4454 | return TCL_ERROR; |
| 4455 | } |
| 4456 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 4457 | 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] | 4458 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 4459 | return TCL_OK; |
| 4460 | } |
| 4461 | |
| 4462 | /* |
| 4463 | ** Usage: sqlite_delete_collation DB collation-name |
| 4464 | ** |
| 4465 | ** Delete the collation sequence 'collation-name' from database handle |
| 4466 | ** DB. It is assumed that the collation sequence was created as UTF8 (the |
| 4467 | ** way the TCL interface does it). |
| 4468 | */ |
| 4469 | static int delete_collation( |
| 4470 | void * clientData, |
| 4471 | Tcl_Interp *interp, |
| 4472 | int argc, |
| 4473 | char **argv |
| 4474 | ){ |
| 4475 | int rc; |
| 4476 | sqlite3 *db; |
| 4477 | if( argc!=3 ){ |
| 4478 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 4479 | " DB function-name", 0); |
| 4480 | return TCL_ERROR; |
| 4481 | } |
| 4482 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 4483 | rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4484 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 4485 | return TCL_OK; |
| 4486 | } |
| 4487 | |
| 4488 | /* |
drh | 3e1d8e6 | 2005-05-26 16:23:34 +0000 | [diff] [blame] | 4489 | ** Usage: sqlite3_get_autocommit DB |
| 4490 | ** |
| 4491 | ** Return true if the database DB is currently in auto-commit mode. |
| 4492 | ** Return false if not. |
| 4493 | */ |
| 4494 | static int get_autocommit( |
| 4495 | void * clientData, |
| 4496 | Tcl_Interp *interp, |
| 4497 | int argc, |
| 4498 | char **argv |
| 4499 | ){ |
| 4500 | char zBuf[30]; |
| 4501 | sqlite3 *db; |
| 4502 | if( argc!=2 ){ |
| 4503 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 4504 | " DB", 0); |
| 4505 | return TCL_ERROR; |
| 4506 | } |
| 4507 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 4508 | sprintf(zBuf, "%d", sqlite3_get_autocommit(db)); |
| 4509 | Tcl_AppendResult(interp, zBuf, 0); |
| 4510 | return TCL_OK; |
| 4511 | } |
| 4512 | |
| 4513 | /* |
drh | 3086765 | 2006-07-06 10:59:57 +0000 | [diff] [blame] | 4514 | ** Usage: sqlite3_busy_timeout DB MS |
| 4515 | ** |
| 4516 | ** Set the busy timeout. This is more easily done using the timeout |
| 4517 | ** method of the TCL interface. But we need a way to test the case |
| 4518 | ** where it returns SQLITE_MISUSE. |
| 4519 | */ |
| 4520 | static int test_busy_timeout( |
| 4521 | void * clientData, |
| 4522 | Tcl_Interp *interp, |
| 4523 | int argc, |
| 4524 | char **argv |
| 4525 | ){ |
| 4526 | int rc, ms; |
| 4527 | sqlite3 *db; |
| 4528 | if( argc!=3 ){ |
| 4529 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 4530 | " DB", 0); |
| 4531 | return TCL_ERROR; |
| 4532 | } |
| 4533 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 4534 | if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR; |
| 4535 | rc = sqlite3_busy_timeout(db, ms); |
| 4536 | Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0); |
| 4537 | return TCL_OK; |
| 4538 | } |
| 4539 | |
| 4540 | /* |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 4541 | ** Usage: tcl_variable_type VARIABLENAME |
| 4542 | ** |
| 4543 | ** Return the name of the internal representation for the |
| 4544 | ** value of the given variable. |
| 4545 | */ |
| 4546 | static int tcl_variable_type( |
| 4547 | void * clientData, |
| 4548 | Tcl_Interp *interp, |
| 4549 | int objc, |
| 4550 | Tcl_Obj *CONST objv[] |
| 4551 | ){ |
| 4552 | Tcl_Obj *pVar; |
| 4553 | if( objc!=2 ){ |
| 4554 | Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE"); |
| 4555 | return TCL_ERROR; |
| 4556 | } |
| 4557 | pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG); |
| 4558 | if( pVar==0 ) return TCL_ERROR; |
| 4559 | if( pVar->typePtr ){ |
| 4560 | Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1)); |
| 4561 | } |
| 4562 | return TCL_OK; |
| 4563 | } |
| 4564 | |
| 4565 | /* |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4566 | ** Usage: sqlite3_release_memory ?N? |
| 4567 | ** |
| 4568 | ** Attempt to release memory currently held but not actually required. |
| 4569 | ** The integer N is the number of bytes we are trying to release. The |
| 4570 | ** return value is the amount of memory actually released. |
| 4571 | */ |
| 4572 | static int test_release_memory( |
| 4573 | void * clientData, |
| 4574 | Tcl_Interp *interp, |
| 4575 | int objc, |
| 4576 | Tcl_Obj *CONST objv[] |
| 4577 | ){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 4578 | #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO) |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4579 | int N; |
| 4580 | int amt; |
| 4581 | if( objc!=1 && objc!=2 ){ |
| 4582 | Tcl_WrongNumArgs(interp, 1, objv, "?N?"); |
| 4583 | return TCL_ERROR; |
| 4584 | } |
| 4585 | if( objc==2 ){ |
| 4586 | if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR; |
| 4587 | }else{ |
| 4588 | N = -1; |
| 4589 | } |
| 4590 | amt = sqlite3_release_memory(N); |
| 4591 | Tcl_SetObjResult(interp, Tcl_NewIntObj(amt)); |
| 4592 | #endif |
| 4593 | return TCL_OK; |
| 4594 | } |
| 4595 | |
drh | 09419b4 | 2011-11-16 19:29:17 +0000 | [diff] [blame] | 4596 | |
| 4597 | /* |
| 4598 | ** Usage: sqlite3_db_release_memory DB |
| 4599 | ** |
| 4600 | ** Attempt to release memory currently held by database DB. Return the |
| 4601 | ** result code (which in the current implementation is always zero). |
| 4602 | */ |
| 4603 | static int test_db_release_memory( |
| 4604 | void * clientData, |
| 4605 | Tcl_Interp *interp, |
| 4606 | int objc, |
| 4607 | Tcl_Obj *CONST objv[] |
| 4608 | ){ |
| 4609 | sqlite3 *db; |
| 4610 | int rc; |
| 4611 | if( objc!=2 ){ |
| 4612 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 4613 | return TCL_ERROR; |
| 4614 | } |
| 4615 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 4616 | rc = sqlite3_db_release_memory(db); |
| 4617 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 4618 | return TCL_OK; |
| 4619 | } |
| 4620 | |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4621 | /* |
drh | 283829c | 2011-11-17 00:56:20 +0000 | [diff] [blame] | 4622 | ** Usage: sqlite3_db_filename DB DBNAME |
| 4623 | ** |
| 4624 | ** Return the name of a file associated with a database. |
| 4625 | */ |
| 4626 | static int test_db_filename( |
| 4627 | void * clientData, |
| 4628 | Tcl_Interp *interp, |
| 4629 | int objc, |
| 4630 | Tcl_Obj *CONST objv[] |
| 4631 | ){ |
| 4632 | sqlite3 *db; |
| 4633 | const char *zDbName; |
| 4634 | if( objc!=3 ){ |
| 4635 | Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); |
| 4636 | return TCL_ERROR; |
| 4637 | } |
| 4638 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 4639 | zDbName = Tcl_GetString(objv[2]); |
| 4640 | Tcl_AppendResult(interp, sqlite3_db_filename(db, zDbName), (void*)0); |
| 4641 | return TCL_OK; |
| 4642 | } |
| 4643 | |
| 4644 | /* |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4645 | ** Usage: sqlite3_soft_heap_limit ?N? |
| 4646 | ** |
| 4647 | ** Query or set the soft heap limit for the current thread. The |
| 4648 | ** limit is only changed if the N is present. The previous limit |
| 4649 | ** is returned. |
| 4650 | */ |
| 4651 | static int test_soft_heap_limit( |
| 4652 | void * clientData, |
| 4653 | Tcl_Interp *interp, |
| 4654 | int objc, |
| 4655 | Tcl_Obj *CONST objv[] |
| 4656 | ){ |
drh | f82ccf6 | 2010-09-15 17:54:31 +0000 | [diff] [blame] | 4657 | sqlite3_int64 amt; |
| 4658 | sqlite3_int64 N = -1; |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4659 | if( objc!=1 && objc!=2 ){ |
| 4660 | Tcl_WrongNumArgs(interp, 1, objv, "?N?"); |
| 4661 | return TCL_ERROR; |
| 4662 | } |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4663 | if( objc==2 ){ |
drh | f82ccf6 | 2010-09-15 17:54:31 +0000 | [diff] [blame] | 4664 | if( Tcl_GetWideIntFromObj(interp, objv[1], &N) ) return TCL_ERROR; |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4665 | } |
drh | f82ccf6 | 2010-09-15 17:54:31 +0000 | [diff] [blame] | 4666 | amt = sqlite3_soft_heap_limit64(N); |
| 4667 | Tcl_SetObjResult(interp, Tcl_NewWideIntObj(amt)); |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4668 | return TCL_OK; |
| 4669 | } |
| 4670 | |
| 4671 | /* |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 4672 | ** Usage: sqlite3_thread_cleanup |
| 4673 | ** |
| 4674 | ** Call the sqlite3_thread_cleanup API. |
| 4675 | */ |
| 4676 | static int test_thread_cleanup( |
| 4677 | void * clientData, |
| 4678 | Tcl_Interp *interp, |
| 4679 | int objc, |
| 4680 | Tcl_Obj *CONST objv[] |
| 4681 | ){ |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 4682 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 4683 | sqlite3_thread_cleanup(); |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 4684 | #endif |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 4685 | return TCL_OK; |
| 4686 | } |
| 4687 | |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 4688 | /* |
drh | c6ba55f | 2007-04-05 17:36:18 +0000 | [diff] [blame] | 4689 | ** Usage: sqlite3_pager_refcounts DB |
| 4690 | ** |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4691 | ** Return a list of numbers which are the PagerRefcount for all |
| 4692 | ** pagers on each database connection. |
drh | c6ba55f | 2007-04-05 17:36:18 +0000 | [diff] [blame] | 4693 | */ |
| 4694 | static int test_pager_refcounts( |
| 4695 | void * clientData, |
| 4696 | Tcl_Interp *interp, |
| 4697 | int objc, |
| 4698 | Tcl_Obj *CONST objv[] |
| 4699 | ){ |
| 4700 | sqlite3 *db; |
| 4701 | int i; |
| 4702 | int v, *a; |
| 4703 | Tcl_Obj *pResult; |
| 4704 | |
| 4705 | if( objc!=2 ){ |
| 4706 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4707 | Tcl_GetStringFromObj(objv[0], 0), " DB", 0); |
drh | c6ba55f | 2007-04-05 17:36:18 +0000 | [diff] [blame] | 4708 | return TCL_ERROR; |
| 4709 | } |
| 4710 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 4711 | pResult = Tcl_NewObj(); |
| 4712 | for(i=0; i<db->nDb; i++){ |
| 4713 | if( db->aDb[i].pBt==0 ){ |
| 4714 | v = -1; |
| 4715 | }else{ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 4716 | sqlite3_mutex_enter(db->mutex); |
drh | c6ba55f | 2007-04-05 17:36:18 +0000 | [diff] [blame] | 4717 | a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt)); |
| 4718 | v = a[0]; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 4719 | sqlite3_mutex_leave(db->mutex); |
drh | c6ba55f | 2007-04-05 17:36:18 +0000 | [diff] [blame] | 4720 | } |
| 4721 | Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v)); |
| 4722 | } |
| 4723 | Tcl_SetObjResult(interp, pResult); |
| 4724 | return TCL_OK; |
| 4725 | } |
| 4726 | |
| 4727 | |
| 4728 | /* |
drh | 80788d8 | 2006-09-02 14:50:23 +0000 | [diff] [blame] | 4729 | ** tclcmd: working_64bit_int |
| 4730 | ** |
| 4731 | ** Some TCL builds (ex: cygwin) do not support 64-bit integers. This |
| 4732 | ** leads to a number of test failures. The present command checks the |
| 4733 | ** TCL build to see whether or not it supports 64-bit integers. It |
| 4734 | ** returns TRUE if it does and FALSE if not. |
| 4735 | ** |
| 4736 | ** This command is used to warn users that their TCL build is defective |
| 4737 | ** and that the errors they are seeing in the test scripts might be |
| 4738 | ** a result of their defective TCL rather than problems in SQLite. |
| 4739 | */ |
| 4740 | static int working_64bit_int( |
| 4741 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 4742 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 4743 | int objc, /* Number of arguments */ |
| 4744 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 4745 | ){ |
| 4746 | Tcl_Obj *pTestObj; |
| 4747 | int working = 0; |
| 4748 | |
| 4749 | pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890); |
| 4750 | working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0; |
| 4751 | Tcl_DecrRefCount(pTestObj); |
| 4752 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working)); |
| 4753 | return TCL_OK; |
| 4754 | } |
| 4755 | |
| 4756 | |
| 4757 | /* |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 4758 | ** tclcmd: vfs_unlink_test |
| 4759 | ** |
| 4760 | ** This TCL command unregisters the primary VFS and then registers |
| 4761 | ** it back again. This is used to test the ability to register a |
| 4762 | ** VFS when none are previously registered, and the ability to |
| 4763 | ** unregister the only available VFS. Ticket #2738 |
| 4764 | */ |
| 4765 | static int vfs_unlink_test( |
| 4766 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 4767 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 4768 | int objc, /* Number of arguments */ |
| 4769 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 4770 | ){ |
| 4771 | int i; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4772 | sqlite3_vfs *pMain; |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 4773 | sqlite3_vfs *apVfs[20]; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4774 | sqlite3_vfs one, two; |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 4775 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4776 | sqlite3_vfs_unregister(0); /* Unregister of NULL is harmless */ |
| 4777 | one.zName = "__one"; |
| 4778 | two.zName = "__two"; |
| 4779 | |
| 4780 | /* Calling sqlite3_vfs_register with 2nd argument of 0 does not |
| 4781 | ** change the default VFS |
| 4782 | */ |
| 4783 | pMain = sqlite3_vfs_find(0); |
| 4784 | sqlite3_vfs_register(&one, 0); |
| 4785 | assert( pMain==0 || pMain==sqlite3_vfs_find(0) ); |
| 4786 | sqlite3_vfs_register(&two, 0); |
| 4787 | assert( pMain==0 || pMain==sqlite3_vfs_find(0) ); |
| 4788 | |
| 4789 | /* We can find a VFS by its name */ |
| 4790 | assert( sqlite3_vfs_find("__one")==&one ); |
| 4791 | assert( sqlite3_vfs_find("__two")==&two ); |
| 4792 | |
| 4793 | /* Calling sqlite_vfs_register with non-zero second parameter changes the |
| 4794 | ** default VFS, even if the 1st parameter is an existig VFS that is |
| 4795 | ** previously registered as the non-default. |
| 4796 | */ |
| 4797 | sqlite3_vfs_register(&one, 1); |
| 4798 | assert( sqlite3_vfs_find("__one")==&one ); |
| 4799 | assert( sqlite3_vfs_find("__two")==&two ); |
| 4800 | assert( sqlite3_vfs_find(0)==&one ); |
| 4801 | sqlite3_vfs_register(&two, 1); |
| 4802 | assert( sqlite3_vfs_find("__one")==&one ); |
| 4803 | assert( sqlite3_vfs_find("__two")==&two ); |
| 4804 | assert( sqlite3_vfs_find(0)==&two ); |
| 4805 | if( pMain ){ |
| 4806 | sqlite3_vfs_register(pMain, 1); |
| 4807 | assert( sqlite3_vfs_find("__one")==&one ); |
| 4808 | assert( sqlite3_vfs_find("__two")==&two ); |
| 4809 | assert( sqlite3_vfs_find(0)==pMain ); |
| 4810 | } |
| 4811 | |
| 4812 | /* Unlink the default VFS. Repeat until there are no more VFSes |
| 4813 | ** registered. |
| 4814 | */ |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 4815 | for(i=0; i<sizeof(apVfs)/sizeof(apVfs[0]); i++){ |
| 4816 | apVfs[i] = sqlite3_vfs_find(0); |
| 4817 | if( apVfs[i] ){ |
| 4818 | assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) ); |
| 4819 | sqlite3_vfs_unregister(apVfs[i]); |
| 4820 | assert( 0==sqlite3_vfs_find(apVfs[i]->zName) ); |
| 4821 | } |
| 4822 | } |
| 4823 | assert( 0==sqlite3_vfs_find(0) ); |
mlcreech | 1f04533 | 2008-04-08 03:07:54 +0000 | [diff] [blame] | 4824 | |
| 4825 | /* Register the main VFS as non-default (will be made default, since |
| 4826 | ** it'll be the only one in existence). |
| 4827 | */ |
| 4828 | sqlite3_vfs_register(pMain, 0); |
| 4829 | assert( sqlite3_vfs_find(0)==pMain ); |
| 4830 | |
| 4831 | /* Un-register the main VFS again to restore an empty VFS list */ |
| 4832 | sqlite3_vfs_unregister(pMain); |
| 4833 | assert( 0==sqlite3_vfs_find(0) ); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4834 | |
| 4835 | /* Relink all VFSes in reverse order. */ |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 4836 | for(i=sizeof(apVfs)/sizeof(apVfs[0])-1; i>=0; i--){ |
| 4837 | if( apVfs[i] ){ |
| 4838 | sqlite3_vfs_register(apVfs[i], 1); |
| 4839 | assert( apVfs[i]==sqlite3_vfs_find(0) ); |
| 4840 | assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) ); |
| 4841 | } |
| 4842 | } |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4843 | |
| 4844 | /* Unregister out sample VFSes. */ |
| 4845 | sqlite3_vfs_unregister(&one); |
| 4846 | sqlite3_vfs_unregister(&two); |
| 4847 | |
| 4848 | /* Unregistering a VFS that is not currently registered is harmless */ |
| 4849 | sqlite3_vfs_unregister(&one); |
| 4850 | sqlite3_vfs_unregister(&two); |
| 4851 | assert( sqlite3_vfs_find("__one")==0 ); |
| 4852 | assert( sqlite3_vfs_find("__two")==0 ); |
| 4853 | |
| 4854 | /* We should be left with the original default VFS back as the |
| 4855 | ** original */ |
| 4856 | assert( sqlite3_vfs_find(0)==pMain ); |
| 4857 | |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 4858 | return TCL_OK; |
| 4859 | } |
| 4860 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 4861 | /* |
drh | c8d7567 | 2008-07-08 02:12:37 +0000 | [diff] [blame] | 4862 | ** tclcmd: vfs_initfail_test |
| 4863 | ** |
| 4864 | ** This TCL command attempts to vfs_find and vfs_register when the |
| 4865 | ** sqlite3_initialize() interface is failing. All calls should fail. |
| 4866 | */ |
| 4867 | static int vfs_initfail_test( |
| 4868 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 4869 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 4870 | int objc, /* Number of arguments */ |
| 4871 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 4872 | ){ |
| 4873 | sqlite3_vfs one; |
| 4874 | one.zName = "__one"; |
| 4875 | |
| 4876 | if( sqlite3_vfs_find(0) ) return TCL_ERROR; |
| 4877 | sqlite3_vfs_register(&one, 0); |
| 4878 | if( sqlite3_vfs_find(0) ) return TCL_ERROR; |
| 4879 | sqlite3_vfs_register(&one, 1); |
| 4880 | if( sqlite3_vfs_find(0) ) return TCL_ERROR; |
| 4881 | return TCL_OK; |
| 4882 | } |
| 4883 | |
| 4884 | /* |
drh | a282097 | 2008-07-07 13:31:58 +0000 | [diff] [blame] | 4885 | ** Saved VFSes |
| 4886 | */ |
| 4887 | static sqlite3_vfs *apVfs[20]; |
| 4888 | static int nVfs = 0; |
| 4889 | |
| 4890 | /* |
| 4891 | ** tclcmd: vfs_unregister_all |
| 4892 | ** |
| 4893 | ** Unregister all VFSes. |
| 4894 | */ |
| 4895 | static int vfs_unregister_all( |
| 4896 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 4897 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 4898 | int objc, /* Number of arguments */ |
| 4899 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 4900 | ){ |
| 4901 | int i; |
| 4902 | for(i=0; i<ArraySize(apVfs); i++){ |
| 4903 | apVfs[i] = sqlite3_vfs_find(0); |
| 4904 | if( apVfs[i]==0 ) break; |
| 4905 | sqlite3_vfs_unregister(apVfs[i]); |
| 4906 | } |
| 4907 | nVfs = i; |
| 4908 | return TCL_OK; |
| 4909 | } |
| 4910 | /* |
| 4911 | ** tclcmd: vfs_reregister_all |
| 4912 | ** |
| 4913 | ** Restore all VFSes that were removed using vfs_unregister_all |
| 4914 | */ |
| 4915 | static int vfs_reregister_all( |
| 4916 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 4917 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 4918 | int objc, /* Number of arguments */ |
| 4919 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 4920 | ){ |
| 4921 | int i; |
| 4922 | for(i=0; i<nVfs; i++){ |
| 4923 | sqlite3_vfs_register(apVfs[i], i==0); |
| 4924 | } |
| 4925 | return TCL_OK; |
| 4926 | } |
| 4927 | |
| 4928 | |
| 4929 | /* |
drh | 5517625 | 2008-01-22 14:50:16 +0000 | [diff] [blame] | 4930 | ** tclcmd: file_control_test DB |
| 4931 | ** |
| 4932 | ** This TCL command runs the sqlite3_file_control interface and |
| 4933 | ** verifies correct operation of the same. |
| 4934 | */ |
| 4935 | static int file_control_test( |
| 4936 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 4937 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 4938 | int objc, /* Number of arguments */ |
| 4939 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 4940 | ){ |
| 4941 | int iArg = 0; |
| 4942 | sqlite3 *db; |
| 4943 | int rc; |
| 4944 | |
| 4945 | if( objc!=2 ){ |
| 4946 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4947 | Tcl_GetStringFromObj(objv[0], 0), " DB", 0); |
| 4948 | return TCL_ERROR; |
| 4949 | } |
| 4950 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 4951 | rc = sqlite3_file_control(db, 0, 0, &iArg); |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 4952 | assert( rc==SQLITE_NOTFOUND ); |
drh | 5517625 | 2008-01-22 14:50:16 +0000 | [diff] [blame] | 4953 | rc = sqlite3_file_control(db, "notadatabase", SQLITE_FCNTL_LOCKSTATE, &iArg); |
| 4954 | assert( rc==SQLITE_ERROR ); |
| 4955 | rc = sqlite3_file_control(db, "main", -1, &iArg); |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 4956 | assert( rc==SQLITE_NOTFOUND ); |
drh | 5517625 | 2008-01-22 14:50:16 +0000 | [diff] [blame] | 4957 | rc = sqlite3_file_control(db, "temp", -1, &iArg); |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 4958 | assert( rc==SQLITE_NOTFOUND || rc==SQLITE_ERROR ); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4959 | |
| 4960 | return TCL_OK; |
| 4961 | } |
| 4962 | |
| 4963 | |
| 4964 | /* |
| 4965 | ** tclcmd: file_control_lasterrno_test DB |
| 4966 | ** |
| 4967 | ** This TCL command runs the sqlite3_file_control interface and |
| 4968 | ** verifies correct operation of the SQLITE_LAST_ERRNO verb. |
| 4969 | */ |
| 4970 | static int file_control_lasterrno_test( |
| 4971 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 4972 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 4973 | int objc, /* Number of arguments */ |
| 4974 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 4975 | ){ |
| 4976 | int iArg = 0; |
| 4977 | sqlite3 *db; |
| 4978 | int rc; |
| 4979 | |
| 4980 | if( objc!=2 ){ |
| 4981 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4982 | Tcl_GetStringFromObj(objv[0], 0), " DB", 0); |
| 4983 | return TCL_ERROR; |
| 4984 | } |
shane | 9db299f | 2009-01-30 05:59:10 +0000 | [diff] [blame] | 4985 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 4986 | return TCL_ERROR; |
| 4987 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4988 | rc = sqlite3_file_control(db, NULL, SQLITE_LAST_ERRNO, &iArg); |
shane | 9db299f | 2009-01-30 05:59:10 +0000 | [diff] [blame] | 4989 | if( rc ){ |
| 4990 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 4991 | return TCL_ERROR; |
| 4992 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4993 | if( iArg!=0 ) { |
| 4994 | Tcl_AppendResult(interp, "Unexpected non-zero errno: ", |
| 4995 | Tcl_GetStringFromObj(Tcl_NewIntObj(iArg), 0), " ", 0); |
| 4996 | return TCL_ERROR; |
| 4997 | } |
drh | 5517625 | 2008-01-22 14:50:16 +0000 | [diff] [blame] | 4998 | return TCL_OK; |
| 4999 | } |
| 5000 | |
| 5001 | /* |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 5002 | ** tclcmd: file_control_chunksize_test DB DBNAME SIZE |
| 5003 | ** |
| 5004 | ** This TCL command runs the sqlite3_file_control interface and |
| 5005 | ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and |
| 5006 | ** SQLITE_SET_LOCKPROXYFILE verbs. |
| 5007 | */ |
| 5008 | static int file_control_chunksize_test( |
| 5009 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5010 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5011 | int objc, /* Number of arguments */ |
| 5012 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5013 | ){ |
| 5014 | int nSize; /* New chunk size */ |
| 5015 | char *zDb; /* Db name ("main", "temp" etc.) */ |
| 5016 | sqlite3 *db; /* Database handle */ |
| 5017 | int rc; /* file_control() return code */ |
| 5018 | |
| 5019 | if( objc!=4 ){ |
| 5020 | Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE"); |
| 5021 | return TCL_ERROR; |
| 5022 | } |
| 5023 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) |
| 5024 | || Tcl_GetIntFromObj(interp, objv[3], &nSize) |
| 5025 | ){ |
| 5026 | return TCL_ERROR; |
| 5027 | } |
| 5028 | zDb = Tcl_GetString(objv[2]); |
| 5029 | if( zDb[0]=='\0' ) zDb = NULL; |
| 5030 | |
| 5031 | rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_CHUNK_SIZE, (void *)&nSize); |
| 5032 | if( rc ){ |
| 5033 | Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); |
| 5034 | return TCL_ERROR; |
| 5035 | } |
| 5036 | return TCL_OK; |
| 5037 | } |
| 5038 | |
| 5039 | /* |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 5040 | ** tclcmd: file_control_sizehint_test DB DBNAME SIZE |
| 5041 | ** |
drh | fdd7f71 | 2011-08-13 10:47:51 +0000 | [diff] [blame] | 5042 | ** This TCL command runs the sqlite3_file_control interface |
| 5043 | ** with SQLITE_FCNTL_SIZE_HINT |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 5044 | */ |
| 5045 | static int file_control_sizehint_test( |
| 5046 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5047 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5048 | int objc, /* Number of arguments */ |
| 5049 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5050 | ){ |
| 5051 | sqlite3_int64 nSize; /* Hinted size */ |
| 5052 | char *zDb; /* Db name ("main", "temp" etc.) */ |
| 5053 | sqlite3 *db; /* Database handle */ |
| 5054 | int rc; /* file_control() return code */ |
| 5055 | |
| 5056 | if( objc!=4 ){ |
| 5057 | Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE"); |
| 5058 | return TCL_ERROR; |
| 5059 | } |
| 5060 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) |
| 5061 | || Tcl_GetWideIntFromObj(interp, objv[3], &nSize) |
| 5062 | ){ |
| 5063 | return TCL_ERROR; |
| 5064 | } |
| 5065 | zDb = Tcl_GetString(objv[2]); |
| 5066 | if( zDb[0]=='\0' ) zDb = NULL; |
| 5067 | |
| 5068 | rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_SIZE_HINT, (void *)&nSize); |
| 5069 | if( rc ){ |
| 5070 | Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); |
| 5071 | return TCL_ERROR; |
| 5072 | } |
| 5073 | return TCL_OK; |
| 5074 | } |
| 5075 | |
| 5076 | /* |
drh | ad24581 | 2010-06-01 00:28:42 +0000 | [diff] [blame] | 5077 | ** tclcmd: file_control_lockproxy_test DB PWD |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5078 | ** |
| 5079 | ** This TCL command runs the sqlite3_file_control interface and |
| 5080 | ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and |
| 5081 | ** SQLITE_SET_LOCKPROXYFILE verbs. |
| 5082 | */ |
| 5083 | static int file_control_lockproxy_test( |
| 5084 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5085 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5086 | int objc, /* Number of arguments */ |
| 5087 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5088 | ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5089 | sqlite3 *db; |
drh | ad24581 | 2010-06-01 00:28:42 +0000 | [diff] [blame] | 5090 | const char *zPwd; |
| 5091 | int nPwd; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5092 | |
drh | ad24581 | 2010-06-01 00:28:42 +0000 | [diff] [blame] | 5093 | if( objc!=3 ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5094 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | ad24581 | 2010-06-01 00:28:42 +0000 | [diff] [blame] | 5095 | Tcl_GetStringFromObj(objv[0], 0), " DB PWD", 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5096 | return TCL_ERROR; |
| 5097 | } |
shane | 9db299f | 2009-01-30 05:59:10 +0000 | [diff] [blame] | 5098 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 5099 | return TCL_ERROR; |
| 5100 | } |
drh | ad24581 | 2010-06-01 00:28:42 +0000 | [diff] [blame] | 5101 | zPwd = Tcl_GetStringFromObj(objv[2], &nPwd); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5102 | |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 5103 | #if !defined(SQLITE_ENABLE_LOCKING_STYLE) |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5104 | # if defined(__APPLE__) |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 5105 | # define SQLITE_ENABLE_LOCKING_STYLE 1 |
| 5106 | # else |
| 5107 | # define SQLITE_ENABLE_LOCKING_STYLE 0 |
| 5108 | # endif |
| 5109 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5110 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5111 | { |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5112 | char *testPath; |
drh | 103fe74 | 2008-12-11 02:56:07 +0000 | [diff] [blame] | 5113 | int rc; |
drh | ad24581 | 2010-06-01 00:28:42 +0000 | [diff] [blame] | 5114 | char proxyPath[400]; |
| 5115 | |
| 5116 | if( sizeof(proxyPath)<nPwd+20 ){ |
| 5117 | Tcl_AppendResult(interp, "PWD too big", (void*)0); |
| 5118 | return TCL_ERROR; |
| 5119 | } |
| 5120 | sprintf(proxyPath, "%s/test.proxy", zPwd); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5121 | rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath); |
| 5122 | if( rc ){ |
shane | 9db299f | 2009-01-30 05:59:10 +0000 | [diff] [blame] | 5123 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 5124 | return TCL_ERROR; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5125 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5126 | rc = sqlite3_file_control(db, NULL, SQLITE_GET_LOCKPROXYFILE, &testPath); |
shane | 9db299f | 2009-01-30 05:59:10 +0000 | [diff] [blame] | 5127 | if( strncmp(proxyPath,testPath,11) ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5128 | Tcl_AppendResult(interp, "Lock proxy file did not match the " |
| 5129 | "previously assigned value", 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5130 | return TCL_ERROR; |
| 5131 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5132 | if( rc ){ |
| 5133 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 5134 | return TCL_ERROR; |
| 5135 | } |
| 5136 | rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath); |
| 5137 | if( rc ){ |
| 5138 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 5139 | return TCL_ERROR; |
| 5140 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5141 | } |
| 5142 | #endif |
| 5143 | return TCL_OK; |
| 5144 | } |
| 5145 | |
drh | d0cdf01 | 2011-07-13 16:03:46 +0000 | [diff] [blame] | 5146 | /* |
| 5147 | ** tclcmd: file_control_win32_av_retry DB NRETRY DELAY |
| 5148 | ** |
| 5149 | ** This TCL command runs the sqlite3_file_control interface with |
| 5150 | ** the SQLITE_FCNTL_WIN32_AV_RETRY opcode. |
| 5151 | */ |
| 5152 | static int file_control_win32_av_retry( |
| 5153 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5154 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5155 | int objc, /* Number of arguments */ |
| 5156 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5157 | ){ |
| 5158 | sqlite3 *db; |
| 5159 | int rc; |
| 5160 | int a[2]; |
| 5161 | char z[100]; |
| 5162 | |
| 5163 | if( objc!=4 ){ |
| 5164 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5165 | Tcl_GetStringFromObj(objv[0], 0), " DB NRETRY DELAY", 0); |
| 5166 | return TCL_ERROR; |
| 5167 | } |
| 5168 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 5169 | return TCL_ERROR; |
| 5170 | } |
| 5171 | if( Tcl_GetIntFromObj(interp, objv[2], &a[0]) ) return TCL_ERROR; |
| 5172 | if( Tcl_GetIntFromObj(interp, objv[3], &a[1]) ) return TCL_ERROR; |
| 5173 | rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_AV_RETRY, (void*)a); |
| 5174 | sqlite3_snprintf(sizeof(z), z, "%d %d %d", rc, a[0], a[1]); |
| 5175 | Tcl_AppendResult(interp, z, (char*)0); |
| 5176 | return TCL_OK; |
| 5177 | } |
| 5178 | |
drh | 253cea5 | 2011-07-26 16:23:25 +0000 | [diff] [blame] | 5179 | /* |
| 5180 | ** tclcmd: file_control_persist_wal DB PERSIST-FLAG |
| 5181 | ** |
| 5182 | ** This TCL command runs the sqlite3_file_control interface with |
| 5183 | ** the SQLITE_FCNTL_PERSIST_WAL opcode. |
| 5184 | */ |
| 5185 | static int file_control_persist_wal( |
| 5186 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5187 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5188 | int objc, /* Number of arguments */ |
| 5189 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5190 | ){ |
| 5191 | sqlite3 *db; |
| 5192 | int rc; |
| 5193 | int bPersist; |
| 5194 | char z[100]; |
| 5195 | |
| 5196 | if( objc!=3 ){ |
| 5197 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5198 | Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0); |
| 5199 | return TCL_ERROR; |
| 5200 | } |
| 5201 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 5202 | return TCL_ERROR; |
| 5203 | } |
| 5204 | if( Tcl_GetIntFromObj(interp, objv[2], &bPersist) ) return TCL_ERROR; |
| 5205 | rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_PERSIST_WAL, (void*)&bPersist); |
| 5206 | sqlite3_snprintf(sizeof(z), z, "%d %d", rc, bPersist); |
| 5207 | Tcl_AppendResult(interp, z, (char*)0); |
| 5208 | return TCL_OK; |
| 5209 | } |
| 5210 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5211 | |
| 5212 | /* |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 5213 | ** tclcmd: file_control_vfsname DB ?AUXDB? |
| 5214 | ** |
| 5215 | ** Return a string that describes the stack of VFSes. |
| 5216 | */ |
| 5217 | static int file_control_vfsname( |
| 5218 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5219 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5220 | int objc, /* Number of arguments */ |
| 5221 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5222 | ){ |
| 5223 | sqlite3 *db; |
| 5224 | const char *zDbName = "main"; |
| 5225 | char *zVfsName = 0; |
| 5226 | |
| 5227 | if( objc!=2 && objc!=3 ){ |
| 5228 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5229 | Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0); |
| 5230 | return TCL_ERROR; |
| 5231 | } |
| 5232 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 5233 | return TCL_ERROR; |
| 5234 | } |
| 5235 | if( objc==3 ){ |
| 5236 | zDbName = Tcl_GetString(objv[2]); |
| 5237 | } |
| 5238 | sqlite3_file_control(db, zDbName, SQLITE_FCNTL_VFSNAME,(void*)&zVfsName); |
| 5239 | Tcl_AppendResult(interp, zVfsName, (char*)0); |
| 5240 | sqlite3_free(zVfsName); |
| 5241 | return TCL_OK; |
| 5242 | } |
| 5243 | |
| 5244 | |
| 5245 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5246 | ** tclcmd: sqlite3_vfs_list |
| 5247 | ** |
| 5248 | ** Return a tcl list containing the names of all registered vfs's. |
| 5249 | */ |
| 5250 | static int vfs_list( |
| 5251 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5252 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5253 | int objc, /* Number of arguments */ |
| 5254 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5255 | ){ |
| 5256 | sqlite3_vfs *pVfs; |
| 5257 | Tcl_Obj *pRet = Tcl_NewObj(); |
| 5258 | if( objc!=1 ){ |
| 5259 | Tcl_WrongNumArgs(interp, 1, objv, ""); |
| 5260 | return TCL_ERROR; |
| 5261 | } |
| 5262 | for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ |
| 5263 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(pVfs->zName, -1)); |
| 5264 | } |
| 5265 | Tcl_SetObjResult(interp, pRet); |
| 5266 | return TCL_OK; |
| 5267 | } |
| 5268 | |
| 5269 | /* |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 5270 | ** tclcmd: sqlite3_limit DB ID VALUE |
| 5271 | ** |
| 5272 | ** This TCL command runs the sqlite3_limit interface and |
| 5273 | ** verifies correct operation of the same. |
| 5274 | */ |
| 5275 | static int test_limit( |
| 5276 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5277 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5278 | int objc, /* Number of arguments */ |
| 5279 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5280 | ){ |
| 5281 | sqlite3 *db; |
| 5282 | int rc; |
| 5283 | static const struct { |
| 5284 | char *zName; |
| 5285 | int id; |
| 5286 | } aId[] = { |
| 5287 | { "SQLITE_LIMIT_LENGTH", SQLITE_LIMIT_LENGTH }, |
| 5288 | { "SQLITE_LIMIT_SQL_LENGTH", SQLITE_LIMIT_SQL_LENGTH }, |
| 5289 | { "SQLITE_LIMIT_COLUMN", SQLITE_LIMIT_COLUMN }, |
| 5290 | { "SQLITE_LIMIT_EXPR_DEPTH", SQLITE_LIMIT_EXPR_DEPTH }, |
| 5291 | { "SQLITE_LIMIT_COMPOUND_SELECT", SQLITE_LIMIT_COMPOUND_SELECT }, |
| 5292 | { "SQLITE_LIMIT_VDBE_OP", SQLITE_LIMIT_VDBE_OP }, |
| 5293 | { "SQLITE_LIMIT_FUNCTION_ARG", SQLITE_LIMIT_FUNCTION_ARG }, |
| 5294 | { "SQLITE_LIMIT_ATTACHED", SQLITE_LIMIT_ATTACHED }, |
| 5295 | { "SQLITE_LIMIT_LIKE_PATTERN_LENGTH", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, |
| 5296 | { "SQLITE_LIMIT_VARIABLE_NUMBER", SQLITE_LIMIT_VARIABLE_NUMBER }, |
drh | 417168a | 2009-09-07 18:14:02 +0000 | [diff] [blame] | 5297 | { "SQLITE_LIMIT_TRIGGER_DEPTH", SQLITE_LIMIT_TRIGGER_DEPTH }, |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 5298 | |
| 5299 | /* Out of range test cases */ |
| 5300 | { "SQLITE_LIMIT_TOOSMALL", -1, }, |
drh | 417168a | 2009-09-07 18:14:02 +0000 | [diff] [blame] | 5301 | { "SQLITE_LIMIT_TOOBIG", SQLITE_LIMIT_TRIGGER_DEPTH+1 }, |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 5302 | }; |
| 5303 | int i, id; |
| 5304 | int val; |
| 5305 | const char *zId; |
| 5306 | |
| 5307 | if( objc!=4 ){ |
| 5308 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5309 | Tcl_GetStringFromObj(objv[0], 0), " DB ID VALUE", 0); |
| 5310 | return TCL_ERROR; |
| 5311 | } |
| 5312 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 5313 | zId = Tcl_GetString(objv[2]); |
| 5314 | for(i=0; i<sizeof(aId)/sizeof(aId[0]); i++){ |
| 5315 | if( strcmp(zId, aId[i].zName)==0 ){ |
| 5316 | id = aId[i].id; |
| 5317 | break; |
| 5318 | } |
| 5319 | } |
| 5320 | if( i>=sizeof(aId)/sizeof(aId[0]) ){ |
| 5321 | Tcl_AppendResult(interp, "unknown limit type: ", zId, (char*)0); |
| 5322 | return TCL_ERROR; |
| 5323 | } |
| 5324 | if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR; |
| 5325 | rc = sqlite3_limit(db, id, val); |
| 5326 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 5327 | return TCL_OK; |
| 5328 | } |
| 5329 | |
| 5330 | /* |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 5331 | ** tclcmd: save_prng_state |
drh | a282097 | 2008-07-07 13:31:58 +0000 | [diff] [blame] | 5332 | ** |
| 5333 | ** Save the state of the pseudo-random number generator. |
| 5334 | ** At the same time, verify that sqlite3_test_control works even when |
| 5335 | ** called with an out-of-range opcode. |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 5336 | */ |
| 5337 | static int save_prng_state( |
| 5338 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5339 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5340 | int objc, /* Number of arguments */ |
| 5341 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5342 | ){ |
drh | a282097 | 2008-07-07 13:31:58 +0000 | [diff] [blame] | 5343 | int rc = sqlite3_test_control(9999); |
| 5344 | assert( rc==0 ); |
| 5345 | rc = sqlite3_test_control(-1); |
| 5346 | assert( rc==0 ); |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 5347 | sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SAVE); |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 5348 | return TCL_OK; |
| 5349 | } |
| 5350 | /* |
| 5351 | ** tclcmd: restore_prng_state |
| 5352 | */ |
| 5353 | static int restore_prng_state( |
| 5354 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5355 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5356 | int objc, /* Number of arguments */ |
| 5357 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5358 | ){ |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 5359 | sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESTORE); |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 5360 | return TCL_OK; |
| 5361 | } |
| 5362 | /* |
| 5363 | ** tclcmd: reset_prng_state |
| 5364 | */ |
| 5365 | static int reset_prng_state( |
| 5366 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5367 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5368 | int objc, /* Number of arguments */ |
| 5369 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5370 | ){ |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 5371 | sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESET); |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 5372 | return TCL_OK; |
| 5373 | } |
| 5374 | |
danielk1977 | 062d4cb | 2008-08-29 09:10:02 +0000 | [diff] [blame] | 5375 | /* |
| 5376 | ** tclcmd: pcache_stats |
| 5377 | */ |
| 5378 | static int test_pcache_stats( |
| 5379 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5380 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5381 | int objc, /* Number of arguments */ |
| 5382 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5383 | ){ |
| 5384 | int nMin; |
| 5385 | int nMax; |
| 5386 | int nCurrent; |
| 5387 | int nRecyclable; |
| 5388 | Tcl_Obj *pRet; |
| 5389 | |
| 5390 | sqlite3PcacheStats(&nCurrent, &nMax, &nMin, &nRecyclable); |
| 5391 | |
| 5392 | pRet = Tcl_NewObj(); |
| 5393 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("current", -1)); |
| 5394 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCurrent)); |
| 5395 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("max", -1)); |
| 5396 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMax)); |
| 5397 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("min", -1)); |
| 5398 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMin)); |
| 5399 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("recyclable", -1)); |
| 5400 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nRecyclable)); |
| 5401 | |
| 5402 | Tcl_SetObjResult(interp, pRet); |
| 5403 | |
| 5404 | return TCL_OK; |
| 5405 | } |
| 5406 | |
drh | 69910da | 2009-03-27 12:32:54 +0000 | [diff] [blame] | 5407 | #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 5408 | static void test_unlock_notify_cb(void **aArg, int nArg){ |
| 5409 | int ii; |
| 5410 | for(ii=0; ii<nArg; ii++){ |
| 5411 | Tcl_EvalEx((Tcl_Interp *)aArg[ii], "unlock_notify", -1, TCL_EVAL_GLOBAL); |
| 5412 | } |
| 5413 | } |
drh | 69910da | 2009-03-27 12:32:54 +0000 | [diff] [blame] | 5414 | #endif /* SQLITE_ENABLE_UNLOCK_NOTIFY */ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 5415 | |
| 5416 | /* |
| 5417 | ** tclcmd: sqlite3_unlock_notify db |
| 5418 | */ |
| 5419 | #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY |
| 5420 | static int test_unlock_notify( |
| 5421 | ClientData clientData, /* Unused */ |
| 5422 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5423 | int objc, /* Number of arguments */ |
| 5424 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5425 | ){ |
| 5426 | sqlite3 *db; |
| 5427 | int rc; |
| 5428 | |
| 5429 | if( objc!=2 ){ |
| 5430 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 5431 | return TCL_ERROR; |
| 5432 | } |
| 5433 | |
| 5434 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 5435 | return TCL_ERROR; |
| 5436 | } |
| 5437 | rc = sqlite3_unlock_notify(db, test_unlock_notify_cb, (void *)interp); |
| 5438 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
| 5439 | return TCL_OK; |
| 5440 | } |
| 5441 | #endif |
| 5442 | |
dan | 87c1fe1 | 2010-05-03 12:14:15 +0000 | [diff] [blame] | 5443 | /* |
| 5444 | ** tclcmd: sqlite3_wal_checkpoint db ?NAME? |
| 5445 | */ |
| 5446 | static int test_wal_checkpoint( |
| 5447 | ClientData clientData, /* Unused */ |
| 5448 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5449 | int objc, /* Number of arguments */ |
| 5450 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5451 | ){ |
| 5452 | char *zDb = 0; |
| 5453 | sqlite3 *db; |
| 5454 | int rc; |
| 5455 | |
| 5456 | if( objc!=3 && objc!=2 ){ |
| 5457 | Tcl_WrongNumArgs(interp, 1, objv, "DB ?NAME?"); |
| 5458 | return TCL_ERROR; |
| 5459 | } |
| 5460 | |
| 5461 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 5462 | return TCL_ERROR; |
| 5463 | } |
| 5464 | if( objc==3 ){ |
| 5465 | zDb = Tcl_GetString(objv[2]); |
| 5466 | } |
| 5467 | rc = sqlite3_wal_checkpoint(db, zDb); |
| 5468 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
| 5469 | return TCL_OK; |
| 5470 | } |
| 5471 | |
dan | eb8763d | 2010-08-17 14:52:22 +0000 | [diff] [blame] | 5472 | /* |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 5473 | ** tclcmd: sqlite3_wal_checkpoint_v2 db MODE ?NAME? |
| 5474 | ** |
| 5475 | ** This command calls the wal_checkpoint_v2() function with the specified |
| 5476 | ** mode argument (passive, full or restart). If present, the database name |
| 5477 | ** NAME is passed as the second argument to wal_checkpoint_v2(). If it the |
| 5478 | ** NAME argument is not present, a NULL pointer is passed instead. |
| 5479 | ** |
| 5480 | ** If wal_checkpoint_v2() returns any value other than SQLITE_BUSY or |
| 5481 | ** SQLITE_OK, then this command returns TCL_ERROR. The Tcl result is set |
| 5482 | ** to the error message obtained from sqlite3_errmsg(). |
| 5483 | ** |
| 5484 | ** Otherwise, this command returns a list of three integers. The first integer |
| 5485 | ** is 1 if SQLITE_BUSY was returned, or 0 otherwise. The following two integers |
| 5486 | ** are the values returned via the output paramaters by wal_checkpoint_v2() - |
| 5487 | ** the number of frames in the log and the number of frames in the log |
| 5488 | ** that have been checkpointed. |
| 5489 | */ |
| 5490 | static int test_wal_checkpoint_v2( |
| 5491 | ClientData clientData, /* Unused */ |
| 5492 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5493 | int objc, /* Number of arguments */ |
| 5494 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5495 | ){ |
| 5496 | char *zDb = 0; |
| 5497 | sqlite3 *db; |
| 5498 | int rc; |
| 5499 | |
| 5500 | int eMode; |
| 5501 | int nLog = -555; |
| 5502 | int nCkpt = -555; |
| 5503 | Tcl_Obj *pRet; |
| 5504 | |
| 5505 | const char * aMode[] = { "passive", "full", "restart", 0 }; |
| 5506 | assert( SQLITE_CHECKPOINT_PASSIVE==0 ); |
| 5507 | assert( SQLITE_CHECKPOINT_FULL==1 ); |
| 5508 | assert( SQLITE_CHECKPOINT_RESTART==2 ); |
| 5509 | |
| 5510 | if( objc!=3 && objc!=4 ){ |
| 5511 | Tcl_WrongNumArgs(interp, 1, objv, "DB MODE ?NAME?"); |
| 5512 | return TCL_ERROR; |
| 5513 | } |
| 5514 | |
| 5515 | if( objc==4 ){ |
| 5516 | zDb = Tcl_GetString(objv[3]); |
| 5517 | } |
| 5518 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) |
| 5519 | || Tcl_GetIndexFromObj(interp, objv[2], aMode, "mode", 0, &eMode) |
| 5520 | ){ |
| 5521 | return TCL_ERROR; |
| 5522 | } |
| 5523 | |
| 5524 | rc = sqlite3_wal_checkpoint_v2(db, zDb, eMode, &nLog, &nCkpt); |
| 5525 | if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ |
| 5526 | Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE); |
| 5527 | return TCL_ERROR; |
| 5528 | } |
| 5529 | |
| 5530 | pRet = Tcl_NewObj(); |
| 5531 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(rc==SQLITE_BUSY?1:0)); |
| 5532 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nLog)); |
| 5533 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCkpt)); |
| 5534 | Tcl_SetObjResult(interp, pRet); |
| 5535 | |
| 5536 | return TCL_OK; |
| 5537 | } |
| 5538 | |
| 5539 | /* |
dan | eb8763d | 2010-08-17 14:52:22 +0000 | [diff] [blame] | 5540 | ** tclcmd: test_sqlite3_log ?SCRIPT? |
| 5541 | */ |
| 5542 | static struct LogCallback { |
| 5543 | Tcl_Interp *pInterp; |
| 5544 | Tcl_Obj *pObj; |
| 5545 | } logcallback = {0, 0}; |
| 5546 | static void xLogcallback(void *unused, int err, char *zMsg){ |
| 5547 | Tcl_Obj *pNew = Tcl_DuplicateObj(logcallback.pObj); |
| 5548 | Tcl_IncrRefCount(pNew); |
| 5549 | Tcl_ListObjAppendElement( |
| 5550 | 0, pNew, Tcl_NewStringObj(sqlite3TestErrorName(err), -1) |
| 5551 | ); |
| 5552 | Tcl_ListObjAppendElement(0, pNew, Tcl_NewStringObj(zMsg, -1)); |
| 5553 | Tcl_EvalObjEx(logcallback.pInterp, pNew, TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT); |
| 5554 | Tcl_DecrRefCount(pNew); |
| 5555 | } |
| 5556 | static int test_sqlite3_log( |
| 5557 | ClientData clientData, |
| 5558 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5559 | int objc, /* Number of arguments */ |
| 5560 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5561 | ){ |
| 5562 | if( objc>2 ){ |
| 5563 | Tcl_WrongNumArgs(interp, 1, objv, "SCRIPT"); |
| 5564 | return TCL_ERROR; |
| 5565 | } |
| 5566 | if( logcallback.pObj ){ |
| 5567 | Tcl_DecrRefCount(logcallback.pObj); |
| 5568 | logcallback.pObj = 0; |
| 5569 | logcallback.pInterp = 0; |
| 5570 | sqlite3_config(SQLITE_CONFIG_LOG, 0, 0); |
| 5571 | } |
| 5572 | if( objc>1 ){ |
| 5573 | logcallback.pObj = objv[1]; |
| 5574 | Tcl_IncrRefCount(logcallback.pObj); |
| 5575 | logcallback.pInterp = interp; |
| 5576 | sqlite3_config(SQLITE_CONFIG_LOG, xLogcallback, 0); |
| 5577 | } |
| 5578 | return TCL_OK; |
| 5579 | } |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 5580 | |
| 5581 | /* |
drh | a2c8a95 | 2009-10-13 18:38:34 +0000 | [diff] [blame] | 5582 | ** tcl_objproc COMMANDNAME ARGS... |
| 5583 | ** |
| 5584 | ** Run a TCL command using its objProc interface. Throw an error if |
| 5585 | ** the command has no objProc interface. |
| 5586 | */ |
| 5587 | static int runAsObjProc( |
| 5588 | void * clientData, |
| 5589 | Tcl_Interp *interp, |
| 5590 | int objc, |
| 5591 | Tcl_Obj *CONST objv[] |
| 5592 | ){ |
| 5593 | Tcl_CmdInfo cmdInfo; |
| 5594 | if( objc<2 ){ |
| 5595 | Tcl_WrongNumArgs(interp, 1, objv, "COMMAND ..."); |
| 5596 | return TCL_ERROR; |
| 5597 | } |
| 5598 | if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ |
| 5599 | Tcl_AppendResult(interp, "command not found: ", |
| 5600 | Tcl_GetString(objv[1]), (char*)0); |
| 5601 | return TCL_ERROR; |
| 5602 | } |
| 5603 | if( cmdInfo.objProc==0 ){ |
| 5604 | Tcl_AppendResult(interp, "command has no objProc: ", |
| 5605 | Tcl_GetString(objv[1]), (char*)0); |
| 5606 | return TCL_ERROR; |
| 5607 | } |
| 5608 | return cmdInfo.objProc(cmdInfo.objClientData, interp, objc-1, objv+1); |
| 5609 | } |
| 5610 | |
dan | 91da6b8 | 2010-11-15 14:51:33 +0000 | [diff] [blame] | 5611 | #ifndef SQLITE_OMIT_EXPLAIN |
| 5612 | /* |
| 5613 | ** WARNING: The following function, printExplainQueryPlan() is an exact |
| 5614 | ** copy of example code from eqp.in (eqp.html). If this code is modified, |
| 5615 | ** then the documentation copy needs to be modified as well. |
| 5616 | */ |
| 5617 | /* |
| 5618 | ** Argument pStmt is a prepared SQL statement. This function compiles |
| 5619 | ** an EXPLAIN QUERY PLAN command to report on the prepared statement, |
| 5620 | ** and prints the report to stdout using printf(). |
| 5621 | */ |
| 5622 | int printExplainQueryPlan(sqlite3_stmt *pStmt){ |
| 5623 | const char *zSql; /* Input SQL */ |
| 5624 | char *zExplain; /* SQL with EXPLAIN QUERY PLAN prepended */ |
| 5625 | sqlite3_stmt *pExplain; /* Compiled EXPLAIN QUERY PLAN command */ |
| 5626 | int rc; /* Return code from sqlite3_prepare_v2() */ |
| 5627 | |
| 5628 | zSql = sqlite3_sql(pStmt); |
| 5629 | if( zSql==0 ) return SQLITE_ERROR; |
| 5630 | |
| 5631 | zExplain = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zSql); |
| 5632 | if( zExplain==0 ) return SQLITE_NOMEM; |
| 5633 | |
| 5634 | rc = sqlite3_prepare_v2(sqlite3_db_handle(pStmt), zExplain, -1, &pExplain, 0); |
| 5635 | sqlite3_free(zExplain); |
| 5636 | if( rc!=SQLITE_OK ) return rc; |
| 5637 | |
| 5638 | while( SQLITE_ROW==sqlite3_step(pExplain) ){ |
| 5639 | int iSelectid = sqlite3_column_int(pExplain, 0); |
| 5640 | int iOrder = sqlite3_column_int(pExplain, 1); |
| 5641 | int iFrom = sqlite3_column_int(pExplain, 2); |
| 5642 | const char *zDetail = (const char *)sqlite3_column_text(pExplain, 3); |
| 5643 | |
| 5644 | printf("%d %d %d %s\n", iSelectid, iOrder, iFrom, zDetail); |
| 5645 | } |
| 5646 | |
| 5647 | return sqlite3_finalize(pExplain); |
| 5648 | } |
| 5649 | |
| 5650 | static int test_print_eqp( |
| 5651 | void * clientData, |
| 5652 | Tcl_Interp *interp, |
| 5653 | int objc, |
| 5654 | Tcl_Obj *CONST objv[] |
| 5655 | ){ |
| 5656 | int rc; |
| 5657 | sqlite3_stmt *pStmt; |
| 5658 | |
| 5659 | if( objc!=2 ){ |
| 5660 | Tcl_WrongNumArgs(interp, 1, objv, "STMT"); |
| 5661 | return TCL_ERROR; |
| 5662 | } |
| 5663 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 5664 | rc = printExplainQueryPlan(pStmt); |
shaneh | 7c5d8fb | 2011-06-22 14:21:31 +0000 | [diff] [blame] | 5665 | /* This is needed on Windows so that a test case using this |
| 5666 | ** function can open a read pipe and get the output of |
| 5667 | ** printExplainQueryPlan() immediately. |
| 5668 | */ |
| 5669 | fflush(stdout); |
dan | 91da6b8 | 2010-11-15 14:51:33 +0000 | [diff] [blame] | 5670 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); |
| 5671 | return TCL_OK; |
| 5672 | } |
| 5673 | #endif /* SQLITE_OMIT_EXPLAIN */ |
drh | a2c8a95 | 2009-10-13 18:38:34 +0000 | [diff] [blame] | 5674 | |
| 5675 | /* |
dan | c17d696 | 2011-06-21 12:47:30 +0000 | [diff] [blame] | 5676 | ** sqlite3_test_control VERB ARGS... |
| 5677 | */ |
| 5678 | static int test_test_control( |
| 5679 | void * clientData, |
| 5680 | Tcl_Interp *interp, |
| 5681 | int objc, |
| 5682 | Tcl_Obj *CONST objv[] |
| 5683 | ){ |
| 5684 | struct Verb { |
| 5685 | const char *zName; |
| 5686 | int i; |
| 5687 | } aVerb[] = { |
| 5688 | { "SQLITE_TESTCTRL_LOCALTIME_FAULT", SQLITE_TESTCTRL_LOCALTIME_FAULT }, |
| 5689 | }; |
| 5690 | int iVerb; |
| 5691 | int iFlag; |
| 5692 | int rc; |
| 5693 | |
| 5694 | if( objc<2 ){ |
| 5695 | Tcl_WrongNumArgs(interp, 1, objv, "VERB ARGS..."); |
| 5696 | return TCL_ERROR; |
| 5697 | } |
| 5698 | |
| 5699 | rc = Tcl_GetIndexFromObjStruct( |
| 5700 | interp, objv[1], aVerb, sizeof(aVerb[0]), "VERB", 0, &iVerb |
| 5701 | ); |
| 5702 | if( rc!=TCL_OK ) return rc; |
| 5703 | |
| 5704 | iFlag = aVerb[iVerb].i; |
| 5705 | switch( iFlag ){ |
| 5706 | case SQLITE_TESTCTRL_LOCALTIME_FAULT: { |
| 5707 | int val; |
| 5708 | if( objc!=3 ){ |
| 5709 | Tcl_WrongNumArgs(interp, 2, objv, "ONOFF"); |
| 5710 | return TCL_ERROR; |
| 5711 | } |
| 5712 | if( Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; |
| 5713 | sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, val); |
| 5714 | break; |
| 5715 | } |
| 5716 | } |
| 5717 | |
| 5718 | Tcl_ResetResult(interp); |
| 5719 | return TCL_OK; |
| 5720 | } |
| 5721 | |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 5722 | #if SQLITE_OS_WIN |
| 5723 | /* |
| 5724 | ** Information passed from the main thread into the windows file locker |
| 5725 | ** background thread. |
| 5726 | */ |
| 5727 | struct win32FileLocker { |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 5728 | char *evName; /* Name of event to signal thread startup */ |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 5729 | HANDLE h; /* Handle of the file to be locked */ |
| 5730 | int delay1; /* Delay before locking */ |
| 5731 | int delay2; /* Delay before unlocking */ |
| 5732 | int ok; /* Finished ok */ |
| 5733 | int err; /* True if an error occurs */ |
| 5734 | }; |
| 5735 | #endif |
| 5736 | |
| 5737 | |
| 5738 | #if SQLITE_OS_WIN |
| 5739 | /* |
| 5740 | ** The background thread that does file locking. |
| 5741 | */ |
| 5742 | static void win32_file_locker(void *pAppData){ |
| 5743 | struct win32FileLocker *p = (struct win32FileLocker*)pAppData; |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 5744 | if( p->evName ){ |
| 5745 | HANDLE ev = OpenEvent(EVENT_MODIFY_STATE, FALSE, p->evName); |
| 5746 | if ( ev ){ |
| 5747 | SetEvent(ev); |
| 5748 | CloseHandle(ev); |
| 5749 | } |
| 5750 | } |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 5751 | if( p->delay1 ) Sleep(p->delay1); |
| 5752 | if( LockFile(p->h, 0, 0, 100000000, 0) ){ |
| 5753 | Sleep(p->delay2); |
| 5754 | UnlockFile(p->h, 0, 0, 100000000, 0); |
| 5755 | p->ok = 1; |
| 5756 | }else{ |
| 5757 | p->err = 1; |
| 5758 | } |
| 5759 | CloseHandle(p->h); |
| 5760 | p->h = 0; |
| 5761 | p->delay1 = 0; |
| 5762 | p->delay2 = 0; |
| 5763 | } |
| 5764 | #endif |
| 5765 | |
| 5766 | #if SQLITE_OS_WIN |
| 5767 | /* |
| 5768 | ** lock_win32_file FILENAME DELAY1 DELAY2 |
| 5769 | ** |
| 5770 | ** Get an exclusive manditory lock on file for DELAY2 milliseconds. |
| 5771 | ** Wait DELAY1 milliseconds before acquiring the lock. |
| 5772 | */ |
| 5773 | static int win32_file_lock( |
| 5774 | void * clientData, |
| 5775 | Tcl_Interp *interp, |
| 5776 | int objc, |
| 5777 | Tcl_Obj *CONST objv[] |
| 5778 | ){ |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 5779 | static struct win32FileLocker x = { "win32_file_lock", 0, 0, 0, 0, 0 }; |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 5780 | const char *zFilename; |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 5781 | char zBuf[200]; |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 5782 | int retry = 0; |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 5783 | HANDLE ev; |
| 5784 | DWORD wResult; |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 5785 | |
| 5786 | if( objc!=4 && objc!=1 ){ |
| 5787 | Tcl_WrongNumArgs(interp, 1, objv, "FILENAME DELAY1 DELAY2"); |
| 5788 | return TCL_ERROR; |
| 5789 | } |
| 5790 | if( objc==1 ){ |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 5791 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%d %d %d %d %d", |
| 5792 | x.ok, x.err, x.delay1, x.delay2, x.h); |
| 5793 | Tcl_AppendResult(interp, zBuf, (char*)0); |
| 5794 | return TCL_OK; |
| 5795 | } |
drh | d0cdf01 | 2011-07-13 16:03:46 +0000 | [diff] [blame] | 5796 | while( x.h && retry<30 ){ |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 5797 | retry++; |
| 5798 | Sleep(100); |
| 5799 | } |
| 5800 | if( x.h ){ |
| 5801 | Tcl_AppendResult(interp, "busy", (char*)0); |
| 5802 | return TCL_ERROR; |
| 5803 | } |
| 5804 | if( Tcl_GetIntFromObj(interp, objv[2], &x.delay1) ) return TCL_ERROR; |
| 5805 | if( Tcl_GetIntFromObj(interp, objv[3], &x.delay2) ) return TCL_ERROR; |
| 5806 | zFilename = Tcl_GetString(objv[1]); |
| 5807 | x.h = CreateFile(zFilename, GENERIC_READ|GENERIC_WRITE, |
| 5808 | FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_ALWAYS, |
| 5809 | FILE_ATTRIBUTE_NORMAL, 0); |
| 5810 | if( !x.h ){ |
| 5811 | Tcl_AppendResult(interp, "cannot open file: ", zFilename, (char*)0); |
| 5812 | return TCL_ERROR; |
| 5813 | } |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 5814 | ev = CreateEvent(NULL, TRUE, FALSE, x.evName); |
| 5815 | if ( !ev ){ |
| 5816 | Tcl_AppendResult(interp, "cannot create event: ", x.evName, (char*)0); |
| 5817 | return TCL_ERROR; |
| 5818 | } |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 5819 | _beginthread(win32_file_locker, 0, (void*)&x); |
| 5820 | Sleep(0); |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 5821 | if ( (wResult = WaitForSingleObject(ev, 10000))!=WAIT_OBJECT_0 ){ |
| 5822 | sqlite3_snprintf(sizeof(zBuf), zBuf, "0x%x", wResult); |
| 5823 | Tcl_AppendResult(interp, "wait failed: ", zBuf, (char*)0); |
| 5824 | CloseHandle(ev); |
| 5825 | return TCL_ERROR; |
| 5826 | } |
| 5827 | CloseHandle(ev); |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 5828 | return TCL_OK; |
| 5829 | } |
| 5830 | #endif |
dan | c17d696 | 2011-06-21 12:47:30 +0000 | [diff] [blame] | 5831 | |
drh | d0cdf01 | 2011-07-13 16:03:46 +0000 | [diff] [blame] | 5832 | |
dan | c17d696 | 2011-06-21 12:47:30 +0000 | [diff] [blame] | 5833 | /* |
drh | f58ee7f | 2010-12-06 21:06:09 +0000 | [diff] [blame] | 5834 | ** optimization_control DB OPT BOOLEAN |
| 5835 | ** |
| 5836 | ** Enable or disable query optimizations using the sqlite3_test_control() |
| 5837 | ** interface. Disable if BOOLEAN is false and enable if BOOLEAN is true. |
| 5838 | ** OPT is the name of the optimization to be disabled. |
| 5839 | */ |
| 5840 | static int optimization_control( |
| 5841 | void * clientData, |
| 5842 | Tcl_Interp *interp, |
| 5843 | int objc, |
| 5844 | Tcl_Obj *CONST objv[] |
| 5845 | ){ |
| 5846 | int i; |
| 5847 | sqlite3 *db; |
| 5848 | const char *zOpt; |
| 5849 | int onoff; |
| 5850 | int mask; |
| 5851 | static const struct { |
| 5852 | const char *zOptName; |
| 5853 | int mask; |
| 5854 | } aOpt[] = { |
| 5855 | { "all", SQLITE_OptMask }, |
| 5856 | { "query-flattener", SQLITE_QueryFlattener }, |
| 5857 | { "column-cache", SQLITE_ColumnCache }, |
| 5858 | { "index-sort", SQLITE_IndexSort }, |
| 5859 | { "index-search", SQLITE_IndexSearch }, |
| 5860 | { "index-cover", SQLITE_IndexCover }, |
| 5861 | { "groupby-order", SQLITE_GroupByOrder }, |
| 5862 | { "factor-constants", SQLITE_FactorOutConst }, |
drh | 097ce2c | 2011-06-23 17:29:33 +0000 | [diff] [blame] | 5863 | { "real-as-int", SQLITE_IdxRealAsInt }, |
drh | f58ee7f | 2010-12-06 21:06:09 +0000 | [diff] [blame] | 5864 | }; |
| 5865 | |
| 5866 | if( objc!=4 ){ |
| 5867 | Tcl_WrongNumArgs(interp, 1, objv, "DB OPT BOOLEAN"); |
| 5868 | return TCL_ERROR; |
| 5869 | } |
| 5870 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 5871 | if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ) return TCL_ERROR; |
| 5872 | zOpt = Tcl_GetString(objv[2]); |
| 5873 | for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){ |
| 5874 | if( strcmp(zOpt, aOpt[i].zOptName)==0 ){ |
| 5875 | mask = aOpt[i].mask; |
| 5876 | break; |
| 5877 | } |
| 5878 | } |
| 5879 | if( onoff ) mask = ~mask; |
| 5880 | if( i>=sizeof(aOpt)/sizeof(aOpt[0]) ){ |
| 5881 | Tcl_AppendResult(interp, "unknown optimization - should be one of:", |
| 5882 | (char*)0); |
| 5883 | for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){ |
| 5884 | Tcl_AppendResult(interp, " ", aOpt[i].zOptName); |
| 5885 | } |
| 5886 | return TCL_ERROR; |
| 5887 | } |
| 5888 | sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, mask); |
| 5889 | return TCL_OK; |
| 5890 | } |
| 5891 | |
| 5892 | /* |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 5893 | ** Register commands with the TCL interpreter. |
| 5894 | */ |
| 5895 | int Sqlitetest1_Init(Tcl_Interp *interp){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 5896 | extern int sqlite3_search_count; |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 5897 | extern int sqlite3_found_count; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 5898 | extern int sqlite3_interrupt_count; |
| 5899 | extern int sqlite3_open_file_count; |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 5900 | extern int sqlite3_sort_count; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 5901 | extern int sqlite3_current_time; |
drh | 84a2bf6 | 2010-03-05 13:41:06 +0000 | [diff] [blame] | 5902 | #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5903 | extern int sqlite3_hostid_num; |
pweilbacher | aabbed2 | 2008-11-21 23:35:02 +0000 | [diff] [blame] | 5904 | #endif |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 5905 | extern int sqlite3_max_blobsize; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5906 | extern int sqlite3BtreeSharedCacheReport(void*, |
| 5907 | Tcl_Interp*,int,Tcl_Obj*CONST*); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 5908 | static struct { |
| 5909 | char *zName; |
| 5910 | Tcl_CmdProc *xProc; |
| 5911 | } aCmd[] = { |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 5912 | { "db_enter", (Tcl_CmdProc*)db_enter }, |
| 5913 | { "db_leave", (Tcl_CmdProc*)db_leave }, |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 5914 | { "sqlite3_mprintf_int", (Tcl_CmdProc*)sqlite3_mprintf_int }, |
drh | e970767 | 2004-06-25 01:10:48 +0000 | [diff] [blame] | 5915 | { "sqlite3_mprintf_int64", (Tcl_CmdProc*)sqlite3_mprintf_int64 }, |
drh | c5cad1e | 2009-02-01 00:21:09 +0000 | [diff] [blame] | 5916 | { "sqlite3_mprintf_long", (Tcl_CmdProc*)sqlite3_mprintf_long }, |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 5917 | { "sqlite3_mprintf_str", (Tcl_CmdProc*)sqlite3_mprintf_str }, |
drh | b3738b6 | 2007-03-31 15:02:49 +0000 | [diff] [blame] | 5918 | { "sqlite3_snprintf_str", (Tcl_CmdProc*)sqlite3_snprintf_str }, |
drh | e29b1a0 | 2004-07-17 21:56:09 +0000 | [diff] [blame] | 5919 | { "sqlite3_mprintf_stronly", (Tcl_CmdProc*)sqlite3_mprintf_stronly}, |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 5920 | { "sqlite3_mprintf_double", (Tcl_CmdProc*)sqlite3_mprintf_double }, |
| 5921 | { "sqlite3_mprintf_scaled", (Tcl_CmdProc*)sqlite3_mprintf_scaled }, |
drh | 6378285 | 2005-08-30 19:30:59 +0000 | [diff] [blame] | 5922 | { "sqlite3_mprintf_hexdouble", (Tcl_CmdProc*)sqlite3_mprintf_hexdouble}, |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 5923 | { "sqlite3_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z }, |
drh | 05a8298 | 2006-03-19 13:00:25 +0000 | [diff] [blame] | 5924 | { "sqlite3_mprintf_n_test", (Tcl_CmdProc*)test_mprintf_n }, |
drh | 6885390 | 2007-05-07 11:24:30 +0000 | [diff] [blame] | 5925 | { "sqlite3_snprintf_int", (Tcl_CmdProc*)test_snprintf_int }, |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 5926 | { "sqlite3_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid }, |
| 5927 | { "sqlite3_exec_printf", (Tcl_CmdProc*)test_exec_printf }, |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 5928 | { "sqlite3_exec_hex", (Tcl_CmdProc*)test_exec_hex }, |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 5929 | { "sqlite3_exec", (Tcl_CmdProc*)test_exec }, |
| 5930 | { "sqlite3_exec_nr", (Tcl_CmdProc*)test_exec_nr }, |
shane | 8225f5a | 2008-07-31 02:05:04 +0000 | [diff] [blame] | 5931 | #ifndef SQLITE_OMIT_GET_TABLE |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 5932 | { "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf }, |
shane | 8225f5a | 2008-07-31 02:05:04 +0000 | [diff] [blame] | 5933 | #endif |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 5934 | { "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close }, |
| 5935 | { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function }, |
| 5936 | { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate }, |
| 5937 | { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func }, |
| 5938 | { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort }, |
drh | 25d6543 | 2004-07-22 15:02:25 +0000 | [diff] [blame] | 5939 | { "sqlite_bind", (Tcl_CmdProc*)test_bind }, |
| 5940 | { "breakpoint", (Tcl_CmdProc*)test_breakpoint }, |
| 5941 | { "sqlite3_key", (Tcl_CmdProc*)test_key }, |
| 5942 | { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey }, |
drh | cacb208 | 2005-01-11 15:28:33 +0000 | [diff] [blame] | 5943 | { "sqlite_set_magic", (Tcl_CmdProc*)sqlite_set_magic }, |
drh | c5cdca6 | 2005-01-11 16:54:14 +0000 | [diff] [blame] | 5944 | { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt }, |
drh | 3e1d8e6 | 2005-05-26 16:23:34 +0000 | [diff] [blame] | 5945 | { "sqlite_delete_function", (Tcl_CmdProc*)delete_function }, |
| 5946 | { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation }, |
| 5947 | { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit }, |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 5948 | { "sqlite3_stack_used", (Tcl_CmdProc*)test_stack_used }, |
drh | 3086765 | 2006-07-06 10:59:57 +0000 | [diff] [blame] | 5949 | { "sqlite3_busy_timeout", (Tcl_CmdProc*)test_busy_timeout }, |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 5950 | { "printf", (Tcl_CmdProc*)test_printf }, |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 5951 | { "sqlite3IoTrace", (Tcl_CmdProc*)test_io_trace }, |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 5952 | }; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 5953 | static struct { |
| 5954 | char *zName; |
| 5955 | Tcl_ObjCmdProc *xProc; |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 5956 | void *clientData; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 5957 | } aObjCmd[] = { |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 5958 | { "sqlite3_connection_pointer", get_sqlite_pointer, 0 }, |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 5959 | { "sqlite3_bind_int", test_bind_int, 0 }, |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5960 | { "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 }, |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 5961 | { "sqlite3_bind_int64", test_bind_int64, 0 }, |
| 5962 | { "sqlite3_bind_double", test_bind_double, 0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 5963 | { "sqlite3_bind_null", test_bind_null ,0 }, |
| 5964 | { "sqlite3_bind_text", test_bind_text ,0 }, |
| 5965 | { "sqlite3_bind_text16", test_bind_text16 ,0 }, |
| 5966 | { "sqlite3_bind_blob", test_bind_blob ,0 }, |
drh | 75f6a03 | 2004-07-15 14:15:00 +0000 | [diff] [blame] | 5967 | { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0}, |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 5968 | { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0}, |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 5969 | { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0}, |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 5970 | { "sqlite3_clear_bindings", test_clear_bindings, 0}, |
drh | f9cb7f5 | 2006-06-27 20:06:44 +0000 | [diff] [blame] | 5971 | { "sqlite3_sleep", test_sleep, 0}, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 5972 | { "sqlite3_errcode", test_errcode ,0 }, |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 5973 | { "sqlite3_extended_errcode", test_ex_errcode ,0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 5974 | { "sqlite3_errmsg", test_errmsg ,0 }, |
| 5975 | { "sqlite3_errmsg16", test_errmsg16 ,0 }, |
| 5976 | { "sqlite3_open", test_open ,0 }, |
| 5977 | { "sqlite3_open16", test_open16 ,0 }, |
dan | 286ab7c | 2011-05-06 18:34:54 +0000 | [diff] [blame] | 5978 | { "sqlite3_open_v2", test_open_v2 ,0 }, |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 5979 | { "sqlite3_complete16", test_complete16 ,0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 5980 | |
| 5981 | { "sqlite3_prepare", test_prepare ,0 }, |
| 5982 | { "sqlite3_prepare16", test_prepare16 ,0 }, |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 5983 | { "sqlite3_prepare_v2", test_prepare_v2 ,0 }, |
drh | 4837f53 | 2008-05-23 14:49:49 +0000 | [diff] [blame] | 5984 | { "sqlite3_prepare_tkt3134", test_prepare_tkt3134, 0}, |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 5985 | { "sqlite3_prepare16_v2", test_prepare16_v2 ,0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 5986 | { "sqlite3_finalize", test_finalize ,0 }, |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 5987 | { "sqlite3_stmt_status", test_stmt_status ,0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 5988 | { "sqlite3_reset", test_reset ,0 }, |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 5989 | { "sqlite3_expired", test_expired ,0 }, |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 5990 | { "sqlite3_transfer_bindings", test_transfer_bind ,0 }, |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 5991 | { "sqlite3_changes", test_changes ,0 }, |
| 5992 | { "sqlite3_step", test_step ,0 }, |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 5993 | { "sqlite3_sql", test_sql ,0 }, |
drh | bb5a9c3 | 2008-06-19 02:52:25 +0000 | [diff] [blame] | 5994 | { "sqlite3_next_stmt", test_next_stmt ,0 }, |
drh | f03d9cc | 2010-11-16 23:10:25 +0000 | [diff] [blame] | 5995 | { "sqlite3_stmt_readonly", test_stmt_readonly ,0 }, |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 5996 | { "uses_stmt_journal", uses_stmt_journal ,0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 5997 | |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 5998 | { "sqlite3_release_memory", test_release_memory, 0}, |
drh | 09419b4 | 2011-11-16 19:29:17 +0000 | [diff] [blame] | 5999 | { "sqlite3_db_release_memory", test_db_release_memory, 0}, |
drh | 283829c | 2011-11-17 00:56:20 +0000 | [diff] [blame] | 6000 | { "sqlite3_db_filename", test_db_filename, 0}, |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 6001 | { "sqlite3_soft_heap_limit", test_soft_heap_limit, 0}, |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 6002 | { "sqlite3_thread_cleanup", test_thread_cleanup, 0}, |
drh | c6ba55f | 2007-04-05 17:36:18 +0000 | [diff] [blame] | 6003 | { "sqlite3_pager_refcounts", test_pager_refcounts, 0}, |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 6004 | |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 6005 | { "sqlite3_load_extension", test_load_extension, 0}, |
| 6006 | { "sqlite3_enable_load_extension", test_enable_load, 0}, |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 6007 | { "sqlite3_extended_result_codes", test_extended_result_codes, 0}, |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 6008 | { "sqlite3_limit", test_limit, 0}, |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 6009 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 6010 | { "save_prng_state", save_prng_state, 0 }, |
| 6011 | { "restore_prng_state", restore_prng_state, 0 }, |
| 6012 | { "reset_prng_state", reset_prng_state, 0 }, |
drh | f58ee7f | 2010-12-06 21:06:09 +0000 | [diff] [blame] | 6013 | { "optimization_control", optimization_control,0}, |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 6014 | #if SQLITE_OS_WIN |
| 6015 | { "lock_win32_file", win32_file_lock, 0 }, |
| 6016 | #endif |
drh | a2c8a95 | 2009-10-13 18:38:34 +0000 | [diff] [blame] | 6017 | { "tcl_objproc", runAsObjProc, 0 }, |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 6018 | |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 6019 | /* sqlite3_column_*() API */ |
| 6020 | { "sqlite3_column_count", test_column_count ,0 }, |
| 6021 | { "sqlite3_data_count", test_data_count ,0 }, |
| 6022 | { "sqlite3_column_type", test_column_type ,0 }, |
danielk1977 | ea61b2c | 2004-05-27 01:49:51 +0000 | [diff] [blame] | 6023 | { "sqlite3_column_blob", test_column_blob ,0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 6024 | { "sqlite3_column_double", test_column_double ,0 }, |
| 6025 | { "sqlite3_column_int64", test_column_int64 ,0 }, |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 6026 | { "sqlite3_column_text", test_stmt_utf8, (void*)sqlite3_column_text }, |
| 6027 | { "sqlite3_column_name", test_stmt_utf8, (void*)sqlite3_column_name }, |
| 6028 | { "sqlite3_column_int", test_stmt_int, (void*)sqlite3_column_int }, |
| 6029 | { "sqlite3_column_bytes", test_stmt_int, (void*)sqlite3_column_bytes}, |
drh | 3f91357 | 2008-03-22 01:07:17 +0000 | [diff] [blame] | 6030 | #ifndef SQLITE_OMIT_DECLTYPE |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 6031 | { "sqlite3_column_decltype",test_stmt_utf8,(void*)sqlite3_column_decltype}, |
drh | 3f91357 | 2008-03-22 01:07:17 +0000 | [diff] [blame] | 6032 | #endif |
danielk1977 | 4b1ae99 | 2006-02-10 03:06:10 +0000 | [diff] [blame] | 6033 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 6034 | { "sqlite3_column_database_name",test_stmt_utf8,(void*)sqlite3_column_database_name}, |
| 6035 | { "sqlite3_column_table_name",test_stmt_utf8,(void*)sqlite3_column_table_name}, |
| 6036 | { "sqlite3_column_origin_name",test_stmt_utf8,(void*)sqlite3_column_origin_name}, |
danielk1977 | 4b1ae99 | 2006-02-10 03:06:10 +0000 | [diff] [blame] | 6037 | #endif |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 6038 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 6039 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 6040 | { "sqlite3_column_bytes16", test_stmt_int, (void*)sqlite3_column_bytes16 }, |
| 6041 | { "sqlite3_column_text16", test_stmt_utf16, (void*)sqlite3_column_text16}, |
| 6042 | { "sqlite3_column_name16", test_stmt_utf16, (void*)sqlite3_column_name16}, |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 6043 | { "add_alignment_test_collations", add_alignment_test_collations, 0 }, |
drh | 3f91357 | 2008-03-22 01:07:17 +0000 | [diff] [blame] | 6044 | #ifndef SQLITE_OMIT_DECLTYPE |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 6045 | { "sqlite3_column_decltype16",test_stmt_utf16,(void*)sqlite3_column_decltype16}, |
drh | 3f91357 | 2008-03-22 01:07:17 +0000 | [diff] [blame] | 6046 | #endif |
danielk1977 | 4b1ae99 | 2006-02-10 03:06:10 +0000 | [diff] [blame] | 6047 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 6048 | {"sqlite3_column_database_name16", |
| 6049 | test_stmt_utf16, sqlite3_column_database_name16}, |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 6050 | {"sqlite3_column_table_name16", test_stmt_utf16, (void*)sqlite3_column_table_name16}, |
| 6051 | {"sqlite3_column_origin_name16", test_stmt_utf16, (void*)sqlite3_column_origin_name16}, |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 6052 | #endif |
danielk1977 | 4b1ae99 | 2006-02-10 03:06:10 +0000 | [diff] [blame] | 6053 | #endif |
danielk1977 | a393c03 | 2007-05-07 14:58:53 +0000 | [diff] [blame] | 6054 | { "sqlite3_create_collation_v2", test_create_collation_v2, 0 }, |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 6055 | { "sqlite3_global_recover", test_global_recover, 0 }, |
| 6056 | { "working_64bit_int", working_64bit_int, 0 }, |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 6057 | { "vfs_unlink_test", vfs_unlink_test, 0 }, |
drh | c8d7567 | 2008-07-08 02:12:37 +0000 | [diff] [blame] | 6058 | { "vfs_initfail_test", vfs_initfail_test, 0 }, |
drh | a282097 | 2008-07-07 13:31:58 +0000 | [diff] [blame] | 6059 | { "vfs_unregister_all", vfs_unregister_all, 0 }, |
| 6060 | { "vfs_reregister_all", vfs_reregister_all, 0 }, |
drh | 5517625 | 2008-01-22 14:50:16 +0000 | [diff] [blame] | 6061 | { "file_control_test", file_control_test, 0 }, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6062 | { "file_control_lasterrno_test", file_control_lasterrno_test, 0 }, |
| 6063 | { "file_control_lockproxy_test", file_control_lockproxy_test, 0 }, |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 6064 | { "file_control_chunksize_test", file_control_chunksize_test, 0 }, |
drh | d0cdf01 | 2011-07-13 16:03:46 +0000 | [diff] [blame] | 6065 | { "file_control_sizehint_test", file_control_sizehint_test, 0 }, |
| 6066 | { "file_control_win32_av_retry", file_control_win32_av_retry, 0 }, |
drh | 253cea5 | 2011-07-26 16:23:25 +0000 | [diff] [blame] | 6067 | { "file_control_persist_wal", file_control_persist_wal, 0 }, |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 6068 | { "file_control_vfsname", file_control_vfsname, 0 }, |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6069 | { "sqlite3_vfs_list", vfs_list, 0 }, |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 6070 | { "sqlite3_create_function_v2", test_create_function_v2, 0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 6071 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 6072 | /* Functions from os.h */ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 6073 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 6074 | { "add_test_collate", test_collate, 0 }, |
| 6075 | { "add_test_collate_needed", test_collate_needed, 0 }, |
| 6076 | { "add_test_function", test_function, 0 }, |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 6077 | #endif |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 6078 | { "sqlite3_test_errstr", test_errstr, 0 }, |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 6079 | { "tcl_variable_type", tcl_variable_type, 0 }, |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6080 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 6081 | { "sqlite3_enable_shared_cache", test_enable_shared, 0 }, |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6082 | { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0}, |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6083 | #endif |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 6084 | { "sqlite3_libversion_number", test_libversion_number, 0 }, |
danielk1977 | deb802c | 2006-02-09 13:43:28 +0000 | [diff] [blame] | 6085 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 6086 | { "sqlite3_table_column_metadata", test_table_column_metadata, 0 }, |
| 6087 | #endif |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6088 | #ifndef SQLITE_OMIT_INCRBLOB |
dan | 61c7f59 | 2010-10-26 18:42:52 +0000 | [diff] [blame] | 6089 | { "sqlite3_blob_read", test_blob_read, 0 }, |
| 6090 | { "sqlite3_blob_write", test_blob_write, 0 }, |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 6091 | { "sqlite3_blob_reopen", test_blob_reopen, 0 }, |
dan | 61c7f59 | 2010-10-26 18:42:52 +0000 | [diff] [blame] | 6092 | { "sqlite3_blob_bytes", test_blob_bytes, 0 }, |
| 6093 | { "sqlite3_blob_close", test_blob_close, 0 }, |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6094 | #endif |
danielk1977 | 062d4cb | 2008-08-29 09:10:02 +0000 | [diff] [blame] | 6095 | { "pcache_stats", test_pcache_stats, 0 }, |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 6096 | #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY |
| 6097 | { "sqlite3_unlock_notify", test_unlock_notify, 0 }, |
| 6098 | #endif |
dan | 91da6b8 | 2010-11-15 14:51:33 +0000 | [diff] [blame] | 6099 | { "sqlite3_wal_checkpoint", test_wal_checkpoint, 0 }, |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 6100 | { "sqlite3_wal_checkpoint_v2",test_wal_checkpoint_v2, 0 }, |
dan | 91da6b8 | 2010-11-15 14:51:33 +0000 | [diff] [blame] | 6101 | { "test_sqlite3_log", test_sqlite3_log, 0 }, |
shaneh | bb20134 | 2011-02-09 19:55:20 +0000 | [diff] [blame] | 6102 | #ifndef SQLITE_OMIT_EXPLAIN |
dan | 91da6b8 | 2010-11-15 14:51:33 +0000 | [diff] [blame] | 6103 | { "print_explain_query_plan", test_print_eqp, 0 }, |
shaneh | bb20134 | 2011-02-09 19:55:20 +0000 | [diff] [blame] | 6104 | #endif |
dan | c17d696 | 2011-06-21 12:47:30 +0000 | [diff] [blame] | 6105 | { "sqlite3_test_control", test_test_control }, |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 6106 | }; |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 6107 | static int bitmask_size = sizeof(Bitmask)*8; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 6108 | int i; |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 6109 | extern int sqlite3_sync_count, sqlite3_fullsync_count; |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 6110 | extern int sqlite3_opentemp_count; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 6111 | extern int sqlite3_like_count; |
drh | dd73521 | 2007-02-24 13:53:05 +0000 | [diff] [blame] | 6112 | extern int sqlite3_xferopt_count; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6113 | extern int sqlite3_pager_readdb_count; |
| 6114 | extern int sqlite3_pager_writedb_count; |
| 6115 | extern int sqlite3_pager_writej_count; |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 6116 | #if SQLITE_OS_WIN |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 6117 | extern int sqlite3_os_type; |
| 6118 | #endif |
drh | 8b3d990 | 2005-08-19 00:14:42 +0000 | [diff] [blame] | 6119 | #ifdef SQLITE_DEBUG |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 6120 | extern int sqlite3WhereTrace; |
| 6121 | extern int sqlite3OSTrace; |
| 6122 | extern int sqlite3VdbeAddopTrace; |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 6123 | extern int sqlite3WalTrace; |
drh | 549c8b6 | 2005-09-19 13:15:23 +0000 | [diff] [blame] | 6124 | #endif |
| 6125 | #ifdef SQLITE_TEST |
| 6126 | extern char sqlite3_query_plan[]; |
drh | 9042f39 | 2005-07-15 23:24:23 +0000 | [diff] [blame] | 6127 | static char *query_plan = sqlite3_query_plan; |
danielk1977 | 33e8903 | 2008-12-17 15:18:17 +0000 | [diff] [blame] | 6128 | #ifdef SQLITE_ENABLE_FTS3 |
| 6129 | extern int sqlite3_fts3_enable_parentheses; |
| 6130 | #endif |
drh | 48083ce | 2005-09-19 12:37:27 +0000 | [diff] [blame] | 6131 | #endif |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 6132 | |
| 6133 | for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ |
| 6134 | Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); |
| 6135 | } |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 6136 | for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 6137 | Tcl_CreateObjCommand(interp, aObjCmd[i].zName, |
| 6138 | aObjCmd[i].xProc, aObjCmd[i].clientData, 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 6139 | } |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 6140 | Tcl_LinkVar(interp, "sqlite_search_count", |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 6141 | (char*)&sqlite3_search_count, TCL_LINK_INT); |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 6142 | Tcl_LinkVar(interp, "sqlite_found_count", |
| 6143 | (char*)&sqlite3_found_count, TCL_LINK_INT); |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 6144 | Tcl_LinkVar(interp, "sqlite_sort_count", |
| 6145 | (char*)&sqlite3_sort_count, TCL_LINK_INT); |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 6146 | Tcl_LinkVar(interp, "sqlite3_max_blobsize", |
| 6147 | (char*)&sqlite3_max_blobsize, TCL_LINK_INT); |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 6148 | Tcl_LinkVar(interp, "sqlite_like_count", |
| 6149 | (char*)&sqlite3_like_count, TCL_LINK_INT); |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 6150 | Tcl_LinkVar(interp, "sqlite_interrupt_count", |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 6151 | (char*)&sqlite3_interrupt_count, TCL_LINK_INT); |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 6152 | Tcl_LinkVar(interp, "sqlite_open_file_count", |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 6153 | (char*)&sqlite3_open_file_count, TCL_LINK_INT); |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 6154 | Tcl_LinkVar(interp, "sqlite_current_time", |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 6155 | (char*)&sqlite3_current_time, TCL_LINK_INT); |
drh | 84a2bf6 | 2010-03-05 13:41:06 +0000 | [diff] [blame] | 6156 | #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6157 | Tcl_LinkVar(interp, "sqlite_hostid_num", |
| 6158 | (char*)&sqlite3_hostid_num, TCL_LINK_INT); |
pweilbacher | aabbed2 | 2008-11-21 23:35:02 +0000 | [diff] [blame] | 6159 | #endif |
drh | dd73521 | 2007-02-24 13:53:05 +0000 | [diff] [blame] | 6160 | Tcl_LinkVar(interp, "sqlite3_xferopt_count", |
| 6161 | (char*)&sqlite3_xferopt_count, TCL_LINK_INT); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6162 | Tcl_LinkVar(interp, "sqlite3_pager_readdb_count", |
| 6163 | (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT); |
| 6164 | Tcl_LinkVar(interp, "sqlite3_pager_writedb_count", |
| 6165 | (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT); |
| 6166 | Tcl_LinkVar(interp, "sqlite3_pager_writej_count", |
| 6167 | (char*)&sqlite3_pager_writej_count, TCL_LINK_INT); |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 6168 | #ifndef SQLITE_OMIT_UTF16 |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 6169 | Tcl_LinkVar(interp, "unaligned_string_counter", |
| 6170 | (char*)&unaligned_string_counter, TCL_LINK_INT); |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 6171 | #endif |
drh | 268803a | 2005-12-14 20:11:30 +0000 | [diff] [blame] | 6172 | #ifndef SQLITE_OMIT_UTF16 |
| 6173 | Tcl_LinkVar(interp, "sqlite_last_needed_collation", |
| 6174 | (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY); |
| 6175 | #endif |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 6176 | #if SQLITE_OS_WIN |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 6177 | Tcl_LinkVar(interp, "sqlite_os_type", |
| 6178 | (char*)&sqlite3_os_type, TCL_LINK_INT); |
| 6179 | #endif |
drh | 549c8b6 | 2005-09-19 13:15:23 +0000 | [diff] [blame] | 6180 | #ifdef SQLITE_TEST |
| 6181 | Tcl_LinkVar(interp, "sqlite_query_plan", |
| 6182 | (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY); |
| 6183 | #endif |
drh | 8b3d990 | 2005-08-19 00:14:42 +0000 | [diff] [blame] | 6184 | #ifdef SQLITE_DEBUG |
| 6185 | Tcl_LinkVar(interp, "sqlite_addop_trace", |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 6186 | (char*)&sqlite3VdbeAddopTrace, TCL_LINK_INT); |
drh | 48083ce | 2005-09-19 12:37:27 +0000 | [diff] [blame] | 6187 | Tcl_LinkVar(interp, "sqlite_where_trace", |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 6188 | (char*)&sqlite3WhereTrace, TCL_LINK_INT); |
drh | 73be501 | 2007-08-08 12:11:21 +0000 | [diff] [blame] | 6189 | Tcl_LinkVar(interp, "sqlite_os_trace", |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 6190 | (char*)&sqlite3OSTrace, TCL_LINK_INT); |
dan | 38e1a27 | 2010-06-28 11:23:09 +0000 | [diff] [blame] | 6191 | #ifndef SQLITE_OMIT_WAL |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 6192 | Tcl_LinkVar(interp, "sqlite_wal_trace", |
| 6193 | (char*)&sqlite3WalTrace, TCL_LINK_INT); |
drh | 8b3d990 | 2005-08-19 00:14:42 +0000 | [diff] [blame] | 6194 | #endif |
dan | 38e1a27 | 2010-06-28 11:23:09 +0000 | [diff] [blame] | 6195 | #endif |
danielk1977 | cbe21be | 2005-06-07 07:58:48 +0000 | [diff] [blame] | 6196 | #ifndef SQLITE_OMIT_DISKIO |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 6197 | Tcl_LinkVar(interp, "sqlite_opentemp_count", |
| 6198 | (char*)&sqlite3_opentemp_count, TCL_LINK_INT); |
danielk1977 | cbe21be | 2005-06-07 07:58:48 +0000 | [diff] [blame] | 6199 | #endif |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 6200 | Tcl_LinkVar(interp, "sqlite_static_bind_value", |
| 6201 | (char*)&sqlite_static_bind_value, TCL_LINK_STRING); |
drh | f031381 | 2006-09-04 15:53:53 +0000 | [diff] [blame] | 6202 | Tcl_LinkVar(interp, "sqlite_static_bind_nbyte", |
| 6203 | (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT); |
drh | ab3f9fe | 2004-08-14 17:10:10 +0000 | [diff] [blame] | 6204 | Tcl_LinkVar(interp, "sqlite_temp_directory", |
drh | effd02b | 2004-08-29 23:42:13 +0000 | [diff] [blame] | 6205 | (char*)&sqlite3_temp_directory, TCL_LINK_STRING); |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 6206 | Tcl_LinkVar(interp, "bitmask_size", |
| 6207 | (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY); |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 6208 | Tcl_LinkVar(interp, "sqlite_sync_count", |
| 6209 | (char*)&sqlite3_sync_count, TCL_LINK_INT); |
| 6210 | Tcl_LinkVar(interp, "sqlite_fullsync_count", |
| 6211 | (char*)&sqlite3_fullsync_count, TCL_LINK_INT); |
drh | d1fa7bc | 2009-01-10 13:24:50 +0000 | [diff] [blame] | 6212 | #if defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_TEST) |
danielk1977 | 33e8903 | 2008-12-17 15:18:17 +0000 | [diff] [blame] | 6213 | Tcl_LinkVar(interp, "sqlite_fts3_enable_parentheses", |
| 6214 | (char*)&sqlite3_fts3_enable_parentheses, TCL_LINK_INT); |
| 6215 | #endif |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 6216 | return TCL_OK; |
| 6217 | } |