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