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