drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | 05a8298 | 2006-03-19 13:00:25 +0000 | [diff] [blame] | 12 | ** Code for testing all sorts of SQLite interfaces. This code |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 13 | ** is not included in the SQLite library. It is used for automated |
| 14 | ** testing of the SQLite library. |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 15 | */ |
| 16 | #include "sqliteInt.h" |
mistachkin | f74b9e0 | 2013-11-26 01:00:31 +0000 | [diff] [blame^] | 17 | #if SQLITE_OS_WIN |
| 18 | # include "os_win.h" |
| 19 | #endif |
| 20 | |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 21 | #include "vdbeInt.h" |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 22 | #include "tcl.h" |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 26 | /* |
| 27 | ** This is a copy of the first part of the SqliteDb structure in |
| 28 | ** tclsqlite.c. We need it here so that the get_sqlite_pointer routine |
| 29 | ** can extract the sqlite3* pointer from an existing Tcl SQLite |
| 30 | ** connection. |
| 31 | */ |
| 32 | struct SqliteDb { |
| 33 | sqlite3 *db; |
| 34 | }; |
| 35 | |
| 36 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 37 | ** Convert text generated by the "%p" conversion format back into |
| 38 | ** a pointer. |
| 39 | */ |
| 40 | static int testHexToInt(int h){ |
| 41 | if( h>='0' && h<='9' ){ |
| 42 | return h - '0'; |
| 43 | }else if( h>='a' && h<='f' ){ |
| 44 | return h - 'a' + 10; |
| 45 | }else{ |
| 46 | assert( h>='A' && h<='F' ); |
| 47 | return h - 'A' + 10; |
| 48 | } |
| 49 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 50 | void *sqlite3TestTextToPtr(const char *z){ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 51 | void *p; |
| 52 | u64 v; |
| 53 | u32 v2; |
| 54 | if( z[0]=='0' && z[1]=='x' ){ |
| 55 | z += 2; |
| 56 | } |
| 57 | v = 0; |
| 58 | while( *z ){ |
| 59 | v = (v<<4) + testHexToInt(*z); |
| 60 | z++; |
| 61 | } |
| 62 | if( sizeof(p)==sizeof(v) ){ |
| 63 | memcpy(&p, &v, sizeof(p)); |
| 64 | }else{ |
| 65 | assert( sizeof(p)==sizeof(v2) ); |
| 66 | v2 = (u32)v; |
| 67 | memcpy(&p, &v2, sizeof(p)); |
| 68 | } |
| 69 | return p; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /* |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 74 | ** A TCL command that returns the address of the sqlite* pointer |
| 75 | ** for an sqlite connection instance. Bad things happen if the |
| 76 | ** input is not an sqlite connection. |
| 77 | */ |
| 78 | static int get_sqlite_pointer( |
| 79 | void * clientData, |
| 80 | Tcl_Interp *interp, |
| 81 | int objc, |
| 82 | Tcl_Obj *CONST objv[] |
| 83 | ){ |
| 84 | struct SqliteDb *p; |
| 85 | Tcl_CmdInfo cmdInfo; |
| 86 | char zBuf[100]; |
| 87 | if( objc!=2 ){ |
| 88 | Tcl_WrongNumArgs(interp, 1, objv, "SQLITE-CONNECTION"); |
| 89 | return TCL_ERROR; |
| 90 | } |
| 91 | if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ |
| 92 | Tcl_AppendResult(interp, "command not found: ", |
| 93 | Tcl_GetString(objv[1]), (char*)0); |
| 94 | return TCL_ERROR; |
| 95 | } |
| 96 | p = (struct SqliteDb*)cmdInfo.objClientData; |
| 97 | sprintf(zBuf, "%p", p->db); |
| 98 | if( strncmp(zBuf,"0x",2) ){ |
| 99 | sprintf(zBuf, "0x%p", p->db); |
| 100 | } |
| 101 | Tcl_AppendResult(interp, zBuf, 0); |
| 102 | return TCL_OK; |
| 103 | } |
| 104 | |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 105 | /* |
| 106 | ** Decode a pointer to an sqlite3 object. |
| 107 | */ |
drh | 24b58dd | 2008-07-07 14:50:14 +0000 | [diff] [blame] | 108 | int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){ |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 109 | struct SqliteDb *p; |
| 110 | Tcl_CmdInfo cmdInfo; |
| 111 | if( Tcl_GetCommandInfo(interp, zA, &cmdInfo) ){ |
| 112 | p = (struct SqliteDb*)cmdInfo.objClientData; |
| 113 | *ppDb = p->db; |
| 114 | }else{ |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 115 | *ppDb = (sqlite3*)sqlite3TestTextToPtr(zA); |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 116 | } |
| 117 | return TCL_OK; |
| 118 | } |
| 119 | |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 120 | extern const char *sqlite3ErrName(int); |
| 121 | #define t1ErrorName sqlite3ErrName |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 122 | |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 123 | /* |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 124 | ** Convert an sqlite3_stmt* into an sqlite3*. This depends on the |
| 125 | ** fact that the sqlite3* is the first field in the Vdbe structure. |
| 126 | */ |
drh | 51942bc | 2005-06-12 22:01:42 +0000 | [diff] [blame] | 127 | #define StmtToDb(X) sqlite3_db_handle(X) |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 128 | |
| 129 | /* |
| 130 | ** Check a return value to make sure it agrees with the results |
| 131 | ** from sqlite3_errcode. |
| 132 | */ |
| 133 | int sqlite3TestErrCode(Tcl_Interp *interp, sqlite3 *db, int rc){ |
drh | b8613ab | 2009-01-19 17:40:12 +0000 | [diff] [blame] | 134 | if( sqlite3_threadsafe()==0 && rc!=SQLITE_MISUSE && rc!=SQLITE_OK |
| 135 | && sqlite3_errcode(db)!=rc ){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 136 | char zBuf[200]; |
| 137 | int r2 = sqlite3_errcode(db); |
| 138 | sprintf(zBuf, "error code %s (%d) does not match sqlite3_errcode %s (%d)", |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 139 | t1ErrorName(rc), rc, t1ErrorName(r2), r2); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 140 | Tcl_ResetResult(interp); |
| 141 | Tcl_AppendResult(interp, zBuf, 0); |
| 142 | return 1; |
| 143 | } |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | /* |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 148 | ** Decode a pointer to an sqlite3_stmt object. |
| 149 | */ |
| 150 | static int getStmtPointer( |
| 151 | Tcl_Interp *interp, |
| 152 | const char *zArg, |
| 153 | sqlite3_stmt **ppStmt |
| 154 | ){ |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 155 | *ppStmt = (sqlite3_stmt*)sqlite3TestTextToPtr(zArg); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 156 | return TCL_OK; |
| 157 | } |
| 158 | |
| 159 | /* |
drh | 7d8085a | 2003-04-26 13:19:38 +0000 | [diff] [blame] | 160 | ** Generate a text representation of a pointer that can be understood |
| 161 | ** by the getDbPointer and getVmPointer routines above. |
| 162 | ** |
| 163 | ** The problem is, on some machines (Solaris) if you do a printf with |
| 164 | ** "%p" you cannot turn around and do a scanf with the same "%p" and |
| 165 | ** get your pointer back. You have to prepend a "0x" before it will |
| 166 | ** work. Or at least that is what is reported to me (drh). But this |
| 167 | ** behavior varies from machine to machine. The solution used her is |
| 168 | ** to test the string right after it is generated to see if it can be |
| 169 | ** understood by scanf, and if not, try prepending an "0x" to see if |
| 170 | ** that helps. If nothing works, a fatal error is generated. |
| 171 | */ |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 172 | int sqlite3TestMakePointerStr(Tcl_Interp *interp, char *zPtr, void *p){ |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 173 | sqlite3_snprintf(100, zPtr, "%p", p); |
drh | 7d8085a | 2003-04-26 13:19:38 +0000 | [diff] [blame] | 174 | return TCL_OK; |
| 175 | } |
| 176 | |
| 177 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 178 | ** The callback routine for sqlite3_exec_printf(). |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 179 | */ |
| 180 | static int exec_printf_cb(void *pArg, int argc, char **argv, char **name){ |
| 181 | Tcl_DString *str = (Tcl_DString*)pArg; |
| 182 | int i; |
| 183 | |
| 184 | if( Tcl_DStringLength(str)==0 ){ |
| 185 | for(i=0; i<argc; i++){ |
| 186 | Tcl_DStringAppendElement(str, name[i] ? name[i] : "NULL"); |
| 187 | } |
| 188 | } |
| 189 | for(i=0; i<argc; i++){ |
| 190 | Tcl_DStringAppendElement(str, argv[i] ? argv[i] : "NULL"); |
| 191 | } |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | /* |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 196 | ** The I/O tracing callback. |
| 197 | */ |
shane | afdd23a | 2008-05-29 02:57:47 +0000 | [diff] [blame] | 198 | #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 199 | static FILE *iotrace_file = 0; |
| 200 | static void io_trace_callback(const char *zFormat, ...){ |
| 201 | va_list ap; |
| 202 | va_start(ap, zFormat); |
| 203 | vfprintf(iotrace_file, zFormat, ap); |
| 204 | va_end(ap); |
| 205 | fflush(iotrace_file); |
| 206 | } |
shane | afdd23a | 2008-05-29 02:57:47 +0000 | [diff] [blame] | 207 | #endif |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 208 | |
| 209 | /* |
| 210 | ** Usage: io_trace FILENAME |
| 211 | ** |
| 212 | ** Turn I/O tracing on or off. If FILENAME is not an empty string, |
| 213 | ** I/O tracing begins going into FILENAME. If FILENAME is an empty |
| 214 | ** string, I/O tracing is turned off. |
| 215 | */ |
| 216 | static int test_io_trace( |
| 217 | void *NotUsed, |
| 218 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 219 | int argc, /* Number of arguments */ |
| 220 | char **argv /* Text of each argument */ |
| 221 | ){ |
danielk1977 | 286d2f4 | 2008-05-05 11:33:47 +0000 | [diff] [blame] | 222 | #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 223 | if( argc!=2 ){ |
| 224 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 225 | " FILENAME\"", 0); |
| 226 | return TCL_ERROR; |
| 227 | } |
| 228 | if( iotrace_file ){ |
| 229 | if( iotrace_file!=stdout && iotrace_file!=stderr ){ |
| 230 | fclose(iotrace_file); |
| 231 | } |
| 232 | iotrace_file = 0; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 233 | sqlite3IoTrace = 0; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 234 | } |
| 235 | if( argv[1][0] ){ |
| 236 | if( strcmp(argv[1],"stdout")==0 ){ |
| 237 | iotrace_file = stdout; |
| 238 | }else if( strcmp(argv[1],"stderr")==0 ){ |
| 239 | iotrace_file = stderr; |
| 240 | }else{ |
| 241 | iotrace_file = fopen(argv[1], "w"); |
| 242 | } |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 243 | sqlite3IoTrace = io_trace_callback; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 244 | } |
danielk1977 | 286d2f4 | 2008-05-05 11:33:47 +0000 | [diff] [blame] | 245 | #endif |
| 246 | return TCL_OK; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | |
| 250 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 251 | ** Usage: sqlite3_exec_printf DB FORMAT STRING |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 252 | ** |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 253 | ** Invoke the sqlite3_exec_printf() interface using the open database |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 254 | ** DB. The SQL is the string FORMAT. The format string should contain |
| 255 | ** one %s or %q. STRING is the value inserted into %s or %q. |
| 256 | */ |
| 257 | static int test_exec_printf( |
| 258 | void *NotUsed, |
| 259 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 260 | int argc, /* Number of arguments */ |
| 261 | char **argv /* Text of each argument */ |
| 262 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 263 | sqlite3 *db; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 264 | Tcl_DString str; |
| 265 | int rc; |
| 266 | char *zErr = 0; |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 267 | char *zSql; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 268 | char zBuf[30]; |
| 269 | if( argc!=4 ){ |
| 270 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 271 | " DB FORMAT STRING", 0); |
| 272 | return TCL_ERROR; |
| 273 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 274 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 275 | Tcl_DStringInit(&str); |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 276 | zSql = sqlite3_mprintf(argv[2], argv[3]); |
| 277 | rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr); |
| 278 | sqlite3_free(zSql); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 279 | sprintf(zBuf, "%d", rc); |
| 280 | Tcl_AppendElement(interp, zBuf); |
| 281 | Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr); |
| 282 | Tcl_DStringFree(&str); |
danielk1977 | 926aab2 | 2006-06-27 07:34:40 +0000 | [diff] [blame] | 283 | if( zErr ) sqlite3_free(zErr); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 284 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 285 | return TCL_OK; |
| 286 | } |
| 287 | |
| 288 | /* |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 289 | ** Usage: sqlite3_exec_hex DB HEX |
| 290 | ** |
| 291 | ** Invoke the sqlite3_exec() on a string that is obtained by translating |
| 292 | ** HEX into ASCII. Most characters are translated as is. %HH becomes |
| 293 | ** a hex character. |
| 294 | */ |
| 295 | static int test_exec_hex( |
| 296 | void *NotUsed, |
| 297 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 298 | int argc, /* Number of arguments */ |
| 299 | char **argv /* Text of each argument */ |
| 300 | ){ |
| 301 | sqlite3 *db; |
| 302 | Tcl_DString str; |
| 303 | int rc, i, j; |
| 304 | char *zErr = 0; |
| 305 | char *zHex; |
| 306 | char zSql[500]; |
| 307 | char zBuf[30]; |
| 308 | if( argc!=3 ){ |
| 309 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 310 | " DB HEX", 0); |
| 311 | return TCL_ERROR; |
| 312 | } |
| 313 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 314 | zHex = argv[2]; |
| 315 | for(i=j=0; i<sizeof(zSql) && zHex[j]; i++, j++){ |
| 316 | if( zHex[j]=='%' && zHex[j+2] && zHex[j+2] ){ |
| 317 | zSql[i] = (testHexToInt(zHex[j+1])<<4) + testHexToInt(zHex[j+2]); |
| 318 | j += 2; |
| 319 | }else{ |
| 320 | zSql[i] = zHex[j]; |
| 321 | } |
| 322 | } |
| 323 | zSql[i] = 0; |
| 324 | Tcl_DStringInit(&str); |
| 325 | rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr); |
| 326 | sprintf(zBuf, "%d", rc); |
| 327 | Tcl_AppendElement(interp, zBuf); |
| 328 | Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr); |
| 329 | Tcl_DStringFree(&str); |
| 330 | if( zErr ) sqlite3_free(zErr); |
| 331 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 332 | return TCL_OK; |
| 333 | } |
| 334 | |
| 335 | /* |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 336 | ** Usage: db_enter DB |
| 337 | ** db_leave DB |
| 338 | ** |
| 339 | ** Enter or leave the mutex on a database connection. |
| 340 | */ |
| 341 | static int db_enter( |
| 342 | void *NotUsed, |
| 343 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 344 | int argc, /* Number of arguments */ |
| 345 | char **argv /* Text of each argument */ |
| 346 | ){ |
| 347 | sqlite3 *db; |
| 348 | if( argc!=2 ){ |
| 349 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 350 | " DB", 0); |
| 351 | return TCL_ERROR; |
| 352 | } |
| 353 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 354 | sqlite3_mutex_enter(db->mutex); |
| 355 | return TCL_OK; |
| 356 | } |
| 357 | static int db_leave( |
| 358 | void *NotUsed, |
| 359 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 360 | int argc, /* Number of arguments */ |
| 361 | char **argv /* Text of each argument */ |
| 362 | ){ |
| 363 | sqlite3 *db; |
| 364 | if( argc!=2 ){ |
| 365 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 366 | " DB", 0); |
| 367 | return TCL_ERROR; |
| 368 | } |
| 369 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 370 | sqlite3_mutex_leave(db->mutex); |
| 371 | return TCL_OK; |
| 372 | } |
| 373 | |
| 374 | /* |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 375 | ** Usage: sqlite3_exec DB SQL |
| 376 | ** |
| 377 | ** Invoke the sqlite3_exec interface using the open database DB |
| 378 | */ |
| 379 | static int test_exec( |
| 380 | void *NotUsed, |
| 381 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 382 | int argc, /* Number of arguments */ |
| 383 | char **argv /* Text of each argument */ |
| 384 | ){ |
| 385 | sqlite3 *db; |
| 386 | Tcl_DString str; |
| 387 | int rc; |
| 388 | char *zErr = 0; |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 389 | char *zSql; |
| 390 | int i, j; |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 391 | char zBuf[30]; |
| 392 | if( argc!=3 ){ |
| 393 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 394 | " DB SQL", 0); |
| 395 | return TCL_ERROR; |
| 396 | } |
| 397 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 398 | Tcl_DStringInit(&str); |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 399 | zSql = sqlite3_mprintf("%s", argv[2]); |
| 400 | for(i=j=0; zSql[i];){ |
| 401 | if( zSql[i]=='%' ){ |
| 402 | zSql[j++] = (testHexToInt(zSql[i+1])<<4) + testHexToInt(zSql[i+2]); |
| 403 | i += 3; |
| 404 | }else{ |
| 405 | zSql[j++] = zSql[i++]; |
| 406 | } |
| 407 | } |
| 408 | zSql[j] = 0; |
| 409 | rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr); |
| 410 | sqlite3_free(zSql); |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 411 | sprintf(zBuf, "%d", rc); |
| 412 | Tcl_AppendElement(interp, zBuf); |
| 413 | Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr); |
| 414 | Tcl_DStringFree(&str); |
| 415 | if( zErr ) sqlite3_free(zErr); |
| 416 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 417 | return TCL_OK; |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | ** Usage: sqlite3_exec_nr DB SQL |
| 422 | ** |
| 423 | ** Invoke the sqlite3_exec interface using the open database DB. Discard |
| 424 | ** all results |
| 425 | */ |
| 426 | static int test_exec_nr( |
| 427 | void *NotUsed, |
| 428 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 429 | int argc, /* Number of arguments */ |
| 430 | char **argv /* Text of each argument */ |
| 431 | ){ |
| 432 | sqlite3 *db; |
| 433 | int rc; |
| 434 | char *zErr = 0; |
| 435 | if( argc!=3 ){ |
| 436 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 437 | " DB SQL", 0); |
| 438 | return TCL_ERROR; |
| 439 | } |
| 440 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 441 | rc = sqlite3_exec(db, argv[2], 0, 0, &zErr); |
| 442 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 443 | return TCL_OK; |
| 444 | } |
| 445 | |
| 446 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 447 | ** Usage: sqlite3_mprintf_z_test SEPARATOR ARG0 ARG1 ... |
drh | d93d8a8 | 2003-06-16 03:08:18 +0000 | [diff] [blame] | 448 | ** |
drh | bc6160b | 2009-04-08 15:45:31 +0000 | [diff] [blame] | 449 | ** Test the %z format of sqlite_mprintf(). Use multiple mprintf() calls to |
drh | d93d8a8 | 2003-06-16 03:08:18 +0000 | [diff] [blame] | 450 | ** concatenate arg0 through argn using separator as the separator. |
| 451 | ** Return the result. |
| 452 | */ |
| 453 | static int test_mprintf_z( |
| 454 | void *NotUsed, |
| 455 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 456 | int argc, /* Number of arguments */ |
| 457 | char **argv /* Text of each argument */ |
| 458 | ){ |
| 459 | char *zResult = 0; |
| 460 | int i; |
| 461 | |
danielk1977 | ca0c897 | 2007-09-01 09:02:53 +0000 | [diff] [blame] | 462 | for(i=2; i<argc && (i==2 || zResult); i++){ |
drh | bc6160b | 2009-04-08 15:45:31 +0000 | [diff] [blame] | 463 | zResult = sqlite3_mprintf("%z%s%s", zResult, argv[1], argv[i]); |
drh | d93d8a8 | 2003-06-16 03:08:18 +0000 | [diff] [blame] | 464 | } |
| 465 | Tcl_AppendResult(interp, zResult, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 466 | sqlite3_free(zResult); |
drh | d93d8a8 | 2003-06-16 03:08:18 +0000 | [diff] [blame] | 467 | return TCL_OK; |
| 468 | } |
| 469 | |
| 470 | /* |
drh | 05a8298 | 2006-03-19 13:00:25 +0000 | [diff] [blame] | 471 | ** Usage: sqlite3_mprintf_n_test STRING |
| 472 | ** |
drh | bc6160b | 2009-04-08 15:45:31 +0000 | [diff] [blame] | 473 | ** Test the %n format of sqlite_mprintf(). Return the length of the |
drh | 05a8298 | 2006-03-19 13:00:25 +0000 | [diff] [blame] | 474 | ** input string. |
| 475 | */ |
| 476 | static int test_mprintf_n( |
| 477 | void *NotUsed, |
| 478 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 479 | int argc, /* Number of arguments */ |
| 480 | char **argv /* Text of each argument */ |
| 481 | ){ |
| 482 | char *zStr; |
| 483 | int n = 0; |
drh | bc6160b | 2009-04-08 15:45:31 +0000 | [diff] [blame] | 484 | zStr = sqlite3_mprintf("%s%n", argv[1], &n); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 485 | sqlite3_free(zStr); |
drh | 05a8298 | 2006-03-19 13:00:25 +0000 | [diff] [blame] | 486 | Tcl_SetObjResult(interp, Tcl_NewIntObj(n)); |
| 487 | return TCL_OK; |
| 488 | } |
| 489 | |
| 490 | /* |
drh | 6885390 | 2007-05-07 11:24:30 +0000 | [diff] [blame] | 491 | ** Usage: sqlite3_snprintf_int SIZE FORMAT INT |
| 492 | ** |
| 493 | ** Test the of sqlite3_snprintf() routine. SIZE is the size of the |
| 494 | ** output buffer in bytes. The maximum size is 100. FORMAT is the |
| 495 | ** format string. INT is a single integer argument. The FORMAT |
| 496 | ** string must require no more than this one integer argument. If |
| 497 | ** You pass in a format string that requires more than one argument, |
| 498 | ** bad things will happen. |
| 499 | */ |
| 500 | static int test_snprintf_int( |
| 501 | void *NotUsed, |
| 502 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 503 | int argc, /* Number of arguments */ |
| 504 | char **argv /* Text of each argument */ |
| 505 | ){ |
| 506 | char zStr[100]; |
| 507 | int n = atoi(argv[1]); |
drh | 6885390 | 2007-05-07 11:24:30 +0000 | [diff] [blame] | 508 | const char *zFormat = argv[2]; |
| 509 | int a1 = atoi(argv[3]); |
drh | daf276d | 2007-06-15 18:53:14 +0000 | [diff] [blame] | 510 | if( n>sizeof(zStr) ) n = sizeof(zStr); |
drh | 7694574 | 2008-02-19 18:29:07 +0000 | [diff] [blame] | 511 | sqlite3_snprintf(sizeof(zStr), zStr, "abcdefghijklmnopqrstuvwxyz"); |
drh | 6885390 | 2007-05-07 11:24:30 +0000 | [diff] [blame] | 512 | sqlite3_snprintf(n, zStr, zFormat, a1); |
| 513 | Tcl_AppendResult(interp, zStr, 0); |
| 514 | return TCL_OK; |
| 515 | } |
| 516 | |
shane | 8225f5a | 2008-07-31 02:05:04 +0000 | [diff] [blame] | 517 | #ifndef SQLITE_OMIT_GET_TABLE |
| 518 | |
drh | 6885390 | 2007-05-07 11:24:30 +0000 | [diff] [blame] | 519 | /* |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 520 | ** Usage: sqlite3_get_table_printf DB FORMAT STRING ?--no-counts? |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 521 | ** |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 522 | ** Invoke the sqlite3_get_table_printf() interface using the open database |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 523 | ** DB. The SQL is the string FORMAT. The format string should contain |
| 524 | ** one %s or %q. STRING is the value inserted into %s or %q. |
| 525 | */ |
| 526 | static int test_get_table_printf( |
| 527 | void *NotUsed, |
| 528 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 529 | int argc, /* Number of arguments */ |
| 530 | char **argv /* Text of each argument */ |
| 531 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 532 | sqlite3 *db; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 533 | Tcl_DString str; |
| 534 | int rc; |
| 535 | char *zErr = 0; |
| 536 | int nRow, nCol; |
| 537 | char **aResult; |
| 538 | int i; |
| 539 | char zBuf[30]; |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 540 | char *zSql; |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 541 | int resCount = -1; |
| 542 | if( argc==5 ){ |
| 543 | if( Tcl_GetInt(interp, argv[4], &resCount) ) return TCL_ERROR; |
| 544 | } |
| 545 | if( argc!=4 && argc!=5 ){ |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 546 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 547 | " DB FORMAT STRING ?COUNT?", 0); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 548 | return TCL_ERROR; |
| 549 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 550 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 551 | Tcl_DStringInit(&str); |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 552 | zSql = sqlite3_mprintf(argv[2],argv[3]); |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 553 | if( argc==5 ){ |
| 554 | rc = sqlite3_get_table(db, zSql, &aResult, 0, 0, &zErr); |
| 555 | }else{ |
| 556 | rc = sqlite3_get_table(db, zSql, &aResult, &nRow, &nCol, &zErr); |
| 557 | resCount = (nRow+1)*nCol; |
| 558 | } |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 559 | sqlite3_free(zSql); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 560 | sprintf(zBuf, "%d", rc); |
| 561 | Tcl_AppendElement(interp, zBuf); |
| 562 | if( rc==SQLITE_OK ){ |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 563 | if( argc==4 ){ |
| 564 | sprintf(zBuf, "%d", nRow); |
| 565 | Tcl_AppendElement(interp, zBuf); |
| 566 | sprintf(zBuf, "%d", nCol); |
| 567 | Tcl_AppendElement(interp, zBuf); |
| 568 | } |
| 569 | for(i=0; i<resCount; i++){ |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 570 | Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL"); |
| 571 | } |
| 572 | }else{ |
| 573 | Tcl_AppendElement(interp, zErr); |
| 574 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 575 | sqlite3_free_table(aResult); |
danielk1977 | 926aab2 | 2006-06-27 07:34:40 +0000 | [diff] [blame] | 576 | if( zErr ) sqlite3_free(zErr); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 577 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 578 | return TCL_OK; |
| 579 | } |
| 580 | |
shane | 8225f5a | 2008-07-31 02:05:04 +0000 | [diff] [blame] | 581 | #endif /* SQLITE_OMIT_GET_TABLE */ |
| 582 | |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 583 | |
| 584 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 585 | ** Usage: sqlite3_last_insert_rowid DB |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 586 | ** |
| 587 | ** Returns the integer ROWID of the most recent insert. |
| 588 | */ |
| 589 | static int test_last_rowid( |
| 590 | void *NotUsed, |
| 591 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 592 | int argc, /* Number of arguments */ |
| 593 | char **argv /* Text of each argument */ |
| 594 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 595 | sqlite3 *db; |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 596 | char zBuf[30]; |
| 597 | |
| 598 | if( argc!=2 ){ |
| 599 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", 0); |
| 600 | return TCL_ERROR; |
| 601 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 602 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 603 | sprintf(zBuf, "%lld", sqlite3_last_insert_rowid(db)); |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 604 | Tcl_AppendResult(interp, zBuf, 0); |
| 605 | return SQLITE_OK; |
| 606 | } |
| 607 | |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 608 | /* |
drh | 25d6543 | 2004-07-22 15:02:25 +0000 | [diff] [blame] | 609 | ** Usage: sqlite3_key DB KEY |
| 610 | ** |
| 611 | ** Set the codec key. |
| 612 | */ |
| 613 | static int test_key( |
| 614 | void *NotUsed, |
| 615 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 616 | int argc, /* Number of arguments */ |
| 617 | char **argv /* Text of each argument */ |
| 618 | ){ |
drh | caffb1a | 2012-01-30 18:00:31 +0000 | [diff] [blame] | 619 | #ifdef SQLITE_HAS_CODEC |
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); |
drh | 25d6543 | 2004-07-22 15:02:25 +0000 | [diff] [blame] | 631 | sqlite3_key(db, zKey, nKey); |
| 632 | #endif |
| 633 | return TCL_OK; |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | ** Usage: sqlite3_rekey DB KEY |
| 638 | ** |
| 639 | ** Change the codec key. |
| 640 | */ |
| 641 | static int test_rekey( |
| 642 | void *NotUsed, |
| 643 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 644 | int argc, /* Number of arguments */ |
| 645 | char **argv /* Text of each argument */ |
| 646 | ){ |
drh | caffb1a | 2012-01-30 18:00:31 +0000 | [diff] [blame] | 647 | #ifdef SQLITE_HAS_CODEC |
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); |
drh | 25d6543 | 2004-07-22 15:02:25 +0000 | [diff] [blame] | 659 | sqlite3_rekey(db, zKey, nKey); |
| 660 | #endif |
| 661 | return TCL_OK; |
| 662 | } |
| 663 | |
| 664 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 665 | ** Usage: sqlite3_close DB |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 666 | ** |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 667 | ** Closes the database opened by sqlite3_open. |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 668 | */ |
| 669 | static int sqlite_test_close( |
| 670 | void *NotUsed, |
| 671 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 672 | int argc, /* Number of arguments */ |
| 673 | char **argv /* Text of each argument */ |
| 674 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 675 | sqlite3 *db; |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 676 | int rc; |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 677 | if( argc!=2 ){ |
| 678 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 679 | " FILENAME\"", 0); |
| 680 | return TCL_ERROR; |
| 681 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 682 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 683 | rc = sqlite3_close(db); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 684 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 685 | return TCL_OK; |
| 686 | } |
| 687 | |
| 688 | /* |
dan | 617dc86 | 2013-05-16 11:57:28 +0000 | [diff] [blame] | 689 | ** Usage: sqlite3_close_v2 DB |
| 690 | ** |
| 691 | ** Closes the database opened by sqlite3_open. |
| 692 | */ |
| 693 | static int sqlite_test_close_v2( |
| 694 | void *NotUsed, |
| 695 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 696 | int argc, /* Number of arguments */ |
| 697 | char **argv /* Text of each argument */ |
| 698 | ){ |
| 699 | sqlite3 *db; |
| 700 | int rc; |
| 701 | if( argc!=2 ){ |
| 702 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 703 | " FILENAME\"", 0); |
| 704 | return TCL_ERROR; |
| 705 | } |
| 706 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 707 | rc = sqlite3_close_v2(db); |
| 708 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
| 709 | return TCL_OK; |
| 710 | } |
| 711 | |
| 712 | /* |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 713 | ** Implementation of the x_coalesce() function. |
| 714 | ** Return the first argument non-NULL argument. |
| 715 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 716 | static void t1_ifnullFunc( |
| 717 | sqlite3_context *context, |
| 718 | int argc, |
| 719 | sqlite3_value **argv |
| 720 | ){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 721 | int i; |
| 722 | for(i=0; i<argc; i++){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 723 | if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){ |
drh | 9310ef2 | 2007-04-27 17:16:20 +0000 | [diff] [blame] | 724 | int n = sqlite3_value_bytes(argv[i]); |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 725 | sqlite3_result_text(context, (char*)sqlite3_value_text(argv[i]), |
drh | 9310ef2 | 2007-04-27 17:16:20 +0000 | [diff] [blame] | 726 | n, SQLITE_TRANSIENT); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 727 | break; |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | /* |
drh | f031381 | 2006-09-04 15:53:53 +0000 | [diff] [blame] | 733 | ** These are test functions. hex8() interprets its argument as |
| 734 | ** UTF8 and returns a hex encoding. hex16le() interprets its argument |
| 735 | ** as UTF16le and returns a hex encoding. |
| 736 | */ |
| 737 | static void hex8Func(sqlite3_context *p, int argc, sqlite3_value **argv){ |
| 738 | const unsigned char *z; |
| 739 | int i; |
| 740 | char zBuf[200]; |
| 741 | z = sqlite3_value_text(argv[0]); |
| 742 | for(i=0; i<sizeof(zBuf)/2 - 2 && z[i]; i++){ |
| 743 | sprintf(&zBuf[i*2], "%02x", z[i]&0xff); |
| 744 | } |
| 745 | zBuf[i*2] = 0; |
| 746 | sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT); |
| 747 | } |
drh | af30469 | 2007-04-23 23:56:31 +0000 | [diff] [blame] | 748 | #ifndef SQLITE_OMIT_UTF16 |
drh | f031381 | 2006-09-04 15:53:53 +0000 | [diff] [blame] | 749 | static void hex16Func(sqlite3_context *p, int argc, sqlite3_value **argv){ |
| 750 | const unsigned short int *z; |
| 751 | int i; |
| 752 | char zBuf[400]; |
| 753 | z = sqlite3_value_text16(argv[0]); |
| 754 | for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){ |
| 755 | sprintf(&zBuf[i*4], "%04x", z[i]&0xff); |
| 756 | } |
| 757 | zBuf[i*4] = 0; |
| 758 | sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT); |
| 759 | } |
drh | af30469 | 2007-04-23 23:56:31 +0000 | [diff] [blame] | 760 | #endif |
drh | f031381 | 2006-09-04 15:53:53 +0000 | [diff] [blame] | 761 | |
| 762 | /* |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 763 | ** A structure into which to accumulate text. |
| 764 | */ |
| 765 | struct dstr { |
| 766 | int nAlloc; /* Space allocated */ |
| 767 | int nUsed; /* Space used */ |
| 768 | char *z; /* The space */ |
| 769 | }; |
| 770 | |
| 771 | /* |
| 772 | ** Append text to a dstr |
| 773 | */ |
| 774 | static void dstrAppend(struct dstr *p, const char *z, int divider){ |
drh | 83cc139 | 2012-04-19 18:04:28 +0000 | [diff] [blame] | 775 | int n = (int)strlen(z); |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 776 | if( p->nUsed + n + 2 > p->nAlloc ){ |
| 777 | char *zNew; |
| 778 | p->nAlloc = p->nAlloc*2 + n + 200; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 779 | zNew = sqlite3_realloc(p->z, p->nAlloc); |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 780 | if( zNew==0 ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 781 | sqlite3_free(p->z); |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 782 | memset(p, 0, sizeof(*p)); |
| 783 | return; |
| 784 | } |
| 785 | p->z = zNew; |
| 786 | } |
| 787 | if( divider && p->nUsed>0 ){ |
| 788 | p->z[p->nUsed++] = divider; |
| 789 | } |
| 790 | memcpy(&p->z[p->nUsed], z, n+1); |
| 791 | p->nUsed += n; |
| 792 | } |
| 793 | |
| 794 | /* |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 795 | ** Invoked for each callback from sqlite3ExecFunc |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 796 | */ |
| 797 | static int execFuncCallback(void *pData, int argc, char **argv, char **NotUsed){ |
| 798 | struct dstr *p = (struct dstr*)pData; |
| 799 | int i; |
| 800 | for(i=0; i<argc; i++){ |
| 801 | if( argv[i]==0 ){ |
| 802 | dstrAppend(p, "NULL", ' '); |
| 803 | }else{ |
| 804 | dstrAppend(p, argv[i], ' '); |
| 805 | } |
| 806 | } |
| 807 | return 0; |
| 808 | } |
| 809 | |
| 810 | /* |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 811 | ** Implementation of the x_sqlite_exec() function. This function takes |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 812 | ** a single argument and attempts to execute that argument as SQL code. |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 813 | ** This is illegal and should set the SQLITE_MISUSE flag on the database. |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 814 | ** |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 815 | ** 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] | 816 | ** from within a function call. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 817 | ** |
| 818 | ** This routine simulates the effect of having two threads attempt to |
| 819 | ** use the same database at the same time. |
| 820 | */ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 821 | static void sqlite3ExecFunc( |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 822 | sqlite3_context *context, |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 823 | int argc, |
| 824 | sqlite3_value **argv |
| 825 | ){ |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 826 | struct dstr x; |
| 827 | memset(&x, 0, sizeof(x)); |
drh | 3752785 | 2006-03-16 16:19:56 +0000 | [diff] [blame] | 828 | (void)sqlite3_exec((sqlite3*)sqlite3_user_data(context), |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 829 | (char*)sqlite3_value_text(argv[0]), |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 830 | execFuncCallback, &x, 0); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 831 | sqlite3_result_text(context, x.z, x.nUsed, SQLITE_TRANSIENT); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 832 | sqlite3_free(x.z); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | /* |
danielk1977 | d726392 | 2007-02-05 14:21:47 +0000 | [diff] [blame] | 836 | ** Implementation of tkt2213func(), a scalar function that takes exactly |
| 837 | ** one argument. It has two interesting features: |
| 838 | ** |
| 839 | ** * It calls sqlite3_value_text() 3 times on the argument sqlite3_value*. |
| 840 | ** If the three pointers returned are not the same an SQL error is raised. |
| 841 | ** |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 842 | ** * Otherwise it returns a copy of the text representation of its |
danielk1977 | d726392 | 2007-02-05 14:21:47 +0000 | [diff] [blame] | 843 | ** argument in such a way as the VDBE representation is a Mem* cell |
| 844 | ** with the MEM_Term flag clear. |
| 845 | ** |
| 846 | ** Ticket #2213 can therefore be tested by evaluating the following |
| 847 | ** SQL expression: |
| 848 | ** |
| 849 | ** tkt2213func(tkt2213func('a string')); |
| 850 | */ |
| 851 | static void tkt2213Function( |
| 852 | sqlite3_context *context, |
| 853 | int argc, |
| 854 | sqlite3_value **argv |
| 855 | ){ |
| 856 | int nText; |
| 857 | unsigned char const *zText1; |
| 858 | unsigned char const *zText2; |
| 859 | unsigned char const *zText3; |
| 860 | |
| 861 | nText = sqlite3_value_bytes(argv[0]); |
| 862 | zText1 = sqlite3_value_text(argv[0]); |
| 863 | zText2 = sqlite3_value_text(argv[0]); |
| 864 | zText3 = sqlite3_value_text(argv[0]); |
| 865 | |
| 866 | if( zText1!=zText2 || zText2!=zText3 ){ |
| 867 | sqlite3_result_error(context, "tkt2213 is not fixed", -1); |
| 868 | }else{ |
| 869 | char *zCopy = (char *)sqlite3_malloc(nText); |
| 870 | memcpy(zCopy, zText1, nText); |
| 871 | sqlite3_result_text(context, zCopy, nText, sqlite3_free); |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | /* |
drh | 9310ef2 | 2007-04-27 17:16:20 +0000 | [diff] [blame] | 876 | ** The following SQL function takes 4 arguments. The 2nd and |
| 877 | ** 4th argument must be one of these strings: 'text', 'text16', |
| 878 | ** or 'blob' corresponding to API functions |
| 879 | ** |
| 880 | ** sqlite3_value_text() |
| 881 | ** sqlite3_value_text16() |
| 882 | ** sqlite3_value_blob() |
| 883 | ** |
| 884 | ** The third argument is a string, either 'bytes' or 'bytes16' or 'noop', |
| 885 | ** corresponding to APIs: |
| 886 | ** |
| 887 | ** sqlite3_value_bytes() |
| 888 | ** sqlite3_value_bytes16() |
| 889 | ** noop |
| 890 | ** |
| 891 | ** The APIs designated by the 2nd through 4th arguments are applied |
| 892 | ** to the first argument in order. If the pointers returned by the |
| 893 | ** second and fourth are different, this routine returns 1. Otherwise, |
| 894 | ** this routine returns 0. |
| 895 | ** |
| 896 | ** This function is used to test to see when returned pointers from |
| 897 | ** the _text(), _text16() and _blob() APIs become invalidated. |
| 898 | */ |
| 899 | static void ptrChngFunction( |
| 900 | sqlite3_context *context, |
| 901 | int argc, |
| 902 | sqlite3_value **argv |
| 903 | ){ |
| 904 | const void *p1, *p2; |
| 905 | const char *zCmd; |
| 906 | if( argc!=4 ) return; |
| 907 | zCmd = (const char*)sqlite3_value_text(argv[1]); |
| 908 | if( zCmd==0 ) return; |
| 909 | if( strcmp(zCmd,"text")==0 ){ |
| 910 | p1 = (const void*)sqlite3_value_text(argv[0]); |
| 911 | #ifndef SQLITE_OMIT_UTF16 |
| 912 | }else if( strcmp(zCmd, "text16")==0 ){ |
| 913 | p1 = (const void*)sqlite3_value_text16(argv[0]); |
| 914 | #endif |
| 915 | }else if( strcmp(zCmd, "blob")==0 ){ |
| 916 | p1 = (const void*)sqlite3_value_blob(argv[0]); |
| 917 | }else{ |
| 918 | return; |
| 919 | } |
| 920 | zCmd = (const char*)sqlite3_value_text(argv[2]); |
| 921 | if( zCmd==0 ) return; |
| 922 | if( strcmp(zCmd,"bytes")==0 ){ |
| 923 | sqlite3_value_bytes(argv[0]); |
| 924 | #ifndef SQLITE_OMIT_UTF16 |
| 925 | }else if( strcmp(zCmd, "bytes16")==0 ){ |
| 926 | sqlite3_value_bytes16(argv[0]); |
| 927 | #endif |
| 928 | }else if( strcmp(zCmd, "noop")==0 ){ |
| 929 | /* do nothing */ |
| 930 | }else{ |
| 931 | return; |
| 932 | } |
| 933 | zCmd = (const char*)sqlite3_value_text(argv[3]); |
| 934 | if( zCmd==0 ) return; |
| 935 | if( strcmp(zCmd,"text")==0 ){ |
| 936 | p2 = (const void*)sqlite3_value_text(argv[0]); |
| 937 | #ifndef SQLITE_OMIT_UTF16 |
| 938 | }else if( strcmp(zCmd, "text16")==0 ){ |
| 939 | p2 = (const void*)sqlite3_value_text16(argv[0]); |
| 940 | #endif |
| 941 | }else if( strcmp(zCmd, "blob")==0 ){ |
| 942 | p2 = (const void*)sqlite3_value_blob(argv[0]); |
| 943 | }else{ |
| 944 | return; |
| 945 | } |
| 946 | sqlite3_result_int(context, p1!=p2); |
| 947 | } |
| 948 | |
| 949 | |
| 950 | /* |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 951 | ** Usage: sqlite_test_create_function DB |
| 952 | ** |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 953 | ** Call the sqlite3_create_function API on the given database in order |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 954 | ** to create a function named "x_coalesce". This function does the same thing |
| 955 | ** as the "coalesce" function. This function also registers an SQL function |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 956 | ** named "x_sqlite_exec" that invokes sqlite3_exec(). Invoking sqlite3_exec() |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 957 | ** in this way is illegal recursion and should raise an SQLITE_MISUSE error. |
| 958 | ** The effect is similar to trying to use the same database connection from |
| 959 | ** two threads at the same time. |
| 960 | ** |
| 961 | ** The original motivation for this routine was to be able to call the |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 962 | ** sqlite3_create_function function while a query is in progress in order |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 963 | ** to test the SQLITE_MISUSE detection logic. |
| 964 | */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 965 | static int test_create_function( |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 966 | void *NotUsed, |
| 967 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 968 | int argc, /* Number of arguments */ |
| 969 | char **argv /* Text of each argument */ |
| 970 | ){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 971 | int rc; |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 972 | sqlite3 *db; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 973 | |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 974 | if( argc!=2 ){ |
| 975 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 976 | " DB\"", 0); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 977 | return TCL_ERROR; |
| 978 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 979 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 980 | rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_ANY, 0, |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 981 | t1_ifnullFunc, 0, 0); |
drh | 235a818 | 2006-09-13 19:21:28 +0000 | [diff] [blame] | 982 | if( rc==SQLITE_OK ){ |
| 983 | rc = sqlite3_create_function(db, "hex8", 1, SQLITE_ANY, 0, |
| 984 | hex8Func, 0, 0); |
| 985 | } |
drh | af30469 | 2007-04-23 23:56:31 +0000 | [diff] [blame] | 986 | #ifndef SQLITE_OMIT_UTF16 |
drh | 235a818 | 2006-09-13 19:21:28 +0000 | [diff] [blame] | 987 | if( rc==SQLITE_OK ){ |
| 988 | rc = sqlite3_create_function(db, "hex16", 1, SQLITE_ANY, 0, |
| 989 | hex16Func, 0, 0); |
| 990 | } |
drh | af30469 | 2007-04-23 23:56:31 +0000 | [diff] [blame] | 991 | #endif |
danielk1977 | d726392 | 2007-02-05 14:21:47 +0000 | [diff] [blame] | 992 | if( rc==SQLITE_OK ){ |
| 993 | rc = sqlite3_create_function(db, "tkt2213func", 1, SQLITE_ANY, 0, |
| 994 | tkt2213Function, 0, 0); |
| 995 | } |
drh | 9310ef2 | 2007-04-27 17:16:20 +0000 | [diff] [blame] | 996 | if( rc==SQLITE_OK ){ |
| 997 | rc = sqlite3_create_function(db, "pointer_change", 4, SQLITE_ANY, 0, |
| 998 | ptrChngFunction, 0, 0); |
| 999 | } |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 1000 | |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 1001 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 1002 | /* Use the sqlite3_create_function16() API here. Mainly for fun, but also |
| 1003 | ** because it is not tested anywhere else. */ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1004 | if( rc==SQLITE_OK ){ |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 1005 | const void *zUtf16; |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1006 | sqlite3_value *pVal; |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 1007 | sqlite3_mutex_enter(db->mutex); |
| 1008 | pVal = sqlite3ValueNew(db); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1009 | sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1010 | zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 1011 | if( db->mallocFailed ){ |
| 1012 | rc = SQLITE_NOMEM; |
| 1013 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1014 | rc = sqlite3_create_function16(db, zUtf16, |
| 1015 | 1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0); |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 1016 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1017 | sqlite3ValueFree(pVal); |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 1018 | sqlite3_mutex_leave(db->mutex); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1019 | } |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 1020 | #endif |
| 1021 | |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1022 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1023 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1024 | return TCL_OK; |
| 1025 | } |
| 1026 | |
| 1027 | /* |
| 1028 | ** Routines to implement the x_count() aggregate function. |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1029 | ** |
| 1030 | ** x_count() counts the number of non-null arguments. But there are |
| 1031 | ** some twists for testing purposes. |
| 1032 | ** |
| 1033 | ** If the argument to x_count() is 40 then a UTF-8 error is reported |
| 1034 | ** on the step function. If x_count(41) is seen, then a UTF-16 error |
| 1035 | ** is reported on the step function. If the total count is 42, then |
| 1036 | ** a UTF-8 error is reported on the finalize function. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1037 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1038 | typedef struct t1CountCtx t1CountCtx; |
| 1039 | struct t1CountCtx { |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1040 | int n; |
| 1041 | }; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1042 | static void t1CountStep( |
| 1043 | sqlite3_context *context, |
| 1044 | int argc, |
| 1045 | sqlite3_value **argv |
| 1046 | ){ |
| 1047 | t1CountCtx *p; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1048 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 1049 | if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1050 | p->n++; |
| 1051 | } |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1052 | if( argc>0 ){ |
| 1053 | int v = sqlite3_value_int(argv[0]); |
| 1054 | if( v==40 ){ |
| 1055 | sqlite3_result_error(context, "value of 40 handed to x_count", -1); |
danielk1977 | a1686c9 | 2006-01-23 07:52:37 +0000 | [diff] [blame] | 1056 | #ifndef SQLITE_OMIT_UTF16 |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1057 | }else if( v==41 ){ |
| 1058 | const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0}; |
| 1059 | sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1); |
danielk1977 | a1686c9 | 2006-01-23 07:52:37 +0000 | [diff] [blame] | 1060 | #endif |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1061 | } |
| 1062 | } |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1063 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1064 | static void t1CountFinalize(sqlite3_context *context){ |
| 1065 | t1CountCtx *p; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 1066 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1067 | if( p ){ |
| 1068 | if( p->n==42 ){ |
| 1069 | sqlite3_result_error(context, "x_count totals to 42", -1); |
| 1070 | }else{ |
| 1071 | sqlite3_result_int(context, p ? p->n : 0); |
| 1072 | } |
| 1073 | } |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
danielk1977 | c551288 | 2009-08-10 04:37:49 +0000 | [diff] [blame] | 1076 | #ifndef SQLITE_OMIT_DEPRECATED |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1077 | static void legacyCountStep( |
| 1078 | sqlite3_context *context, |
| 1079 | int argc, |
| 1080 | sqlite3_value **argv |
| 1081 | ){ |
| 1082 | /* no-op */ |
| 1083 | } |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 1084 | |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1085 | static void legacyCountFinalize(sqlite3_context *context){ |
| 1086 | sqlite3_result_int(context, sqlite3_aggregate_count(context)); |
| 1087 | } |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 1088 | #endif |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1089 | |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1090 | /* |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1091 | ** Usage: sqlite3_create_aggregate DB |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1092 | ** |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1093 | ** Call the sqlite3_create_function API on the given database in order |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1094 | ** to create a function named "x_count". This function is similar |
| 1095 | ** to the built-in count() function, with a few special quirks |
| 1096 | ** for testing the sqlite3_result_error() APIs. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1097 | ** |
| 1098 | ** The original motivation for this routine was to be able to call the |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1099 | ** sqlite3_create_aggregate function while a query is in progress in order |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 1100 | ** to test the SQLITE_MISUSE detection logic. See misuse.test. |
| 1101 | ** |
| 1102 | ** This routine was later extended to test the use of sqlite3_result_error() |
| 1103 | ** within aggregate functions. |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1104 | ** |
| 1105 | ** Later: It is now also extended to register the aggregate function |
| 1106 | ** "legacy_count()" with the supplied database handle. This is used |
| 1107 | ** to test the deprecated sqlite3_aggregate_count() API. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1108 | */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1109 | static int test_create_aggregate( |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1110 | void *NotUsed, |
| 1111 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1112 | int argc, /* Number of arguments */ |
| 1113 | char **argv /* Text of each argument */ |
| 1114 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1115 | sqlite3 *db; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1116 | int rc; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1117 | if( argc!=2 ){ |
| 1118 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1119 | " FILENAME\"", 0); |
| 1120 | return TCL_ERROR; |
| 1121 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 1122 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1123 | rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0, |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1124 | t1CountStep,t1CountFinalize); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1125 | if( rc==SQLITE_OK ){ |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1126 | rc = sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0, |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1127 | t1CountStep,t1CountFinalize); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1128 | } |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 1129 | #ifndef SQLITE_OMIT_DEPRECATED |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1130 | if( rc==SQLITE_OK ){ |
| 1131 | rc = sqlite3_create_function(db, "legacy_count", 0, SQLITE_ANY, 0, 0, |
| 1132 | legacyCountStep, legacyCountFinalize |
| 1133 | ); |
| 1134 | } |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 1135 | #endif |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1136 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1137 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1138 | return TCL_OK; |
| 1139 | } |
| 1140 | |
| 1141 | |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 1142 | /* |
| 1143 | ** Usage: printf TEXT |
| 1144 | ** |
| 1145 | ** Send output to printf. Use this rather than puts to merge the output |
| 1146 | ** in the correct sequence with debugging printfs inserted into C code. |
| 1147 | ** Puts uses a separate buffer and debugging statements will be out of |
| 1148 | ** sequence if it is used. |
| 1149 | */ |
| 1150 | static int test_printf( |
| 1151 | void *NotUsed, |
| 1152 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1153 | int argc, /* Number of arguments */ |
| 1154 | char **argv /* Text of each argument */ |
| 1155 | ){ |
| 1156 | if( argc!=2 ){ |
| 1157 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1158 | " TEXT\"", 0); |
| 1159 | return TCL_ERROR; |
| 1160 | } |
| 1161 | printf("%s\n", argv[1]); |
| 1162 | return TCL_OK; |
| 1163 | } |
| 1164 | |
| 1165 | |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1166 | |
| 1167 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1168 | ** Usage: sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1169 | ** |
| 1170 | ** Call mprintf with three integer arguments |
| 1171 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1172 | static int sqlite3_mprintf_int( |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1173 | void *NotUsed, |
| 1174 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1175 | int argc, /* Number of arguments */ |
| 1176 | char **argv /* Text of each argument */ |
| 1177 | ){ |
| 1178 | int a[3], i; |
| 1179 | char *z; |
| 1180 | if( argc!=5 ){ |
| 1181 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1182 | " FORMAT INT INT INT\"", 0); |
| 1183 | return TCL_ERROR; |
| 1184 | } |
| 1185 | for(i=2; i<5; i++){ |
| 1186 | if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; |
| 1187 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1188 | z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1189 | Tcl_AppendResult(interp, z, 0); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1190 | sqlite3_free(z); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1191 | return TCL_OK; |
| 1192 | } |
| 1193 | |
| 1194 | /* |
drh | e970767 | 2004-06-25 01:10:48 +0000 | [diff] [blame] | 1195 | ** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER |
| 1196 | ** |
| 1197 | ** Call mprintf with three 64-bit integer arguments |
| 1198 | */ |
| 1199 | static int sqlite3_mprintf_int64( |
| 1200 | void *NotUsed, |
| 1201 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1202 | int argc, /* Number of arguments */ |
| 1203 | char **argv /* Text of each argument */ |
| 1204 | ){ |
| 1205 | int i; |
| 1206 | sqlite_int64 a[3]; |
| 1207 | char *z; |
| 1208 | if( argc!=5 ){ |
| 1209 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1210 | " FORMAT INT INT INT\"", 0); |
| 1211 | return TCL_ERROR; |
| 1212 | } |
| 1213 | for(i=2; i<5; i++){ |
shaneh | 5f1d6b6 | 2010-09-30 16:51:25 +0000 | [diff] [blame] | 1214 | if( sqlite3Atoi64(argv[i], &a[i-2], 1000000, SQLITE_UTF8) ){ |
drh | e970767 | 2004-06-25 01:10:48 +0000 | [diff] [blame] | 1215 | Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", 0); |
| 1216 | return TCL_ERROR; |
| 1217 | } |
| 1218 | } |
| 1219 | z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]); |
| 1220 | Tcl_AppendResult(interp, z, 0); |
| 1221 | sqlite3_free(z); |
| 1222 | return TCL_OK; |
| 1223 | } |
| 1224 | |
| 1225 | /* |
drh | c5cad1e | 2009-02-01 00:21:09 +0000 | [diff] [blame] | 1226 | ** Usage: sqlite3_mprintf_long FORMAT INTEGER INTEGER INTEGER |
| 1227 | ** |
| 1228 | ** Call mprintf with three long integer arguments. This might be the |
| 1229 | ** same as sqlite3_mprintf_int or sqlite3_mprintf_int64, depending on |
| 1230 | ** platform. |
| 1231 | */ |
| 1232 | static int sqlite3_mprintf_long( |
| 1233 | void *NotUsed, |
| 1234 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1235 | int argc, /* Number of arguments */ |
| 1236 | char **argv /* Text of each argument */ |
| 1237 | ){ |
| 1238 | int i; |
| 1239 | long int a[3]; |
| 1240 | int b[3]; |
| 1241 | char *z; |
| 1242 | if( argc!=5 ){ |
| 1243 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1244 | " FORMAT INT INT INT\"", 0); |
| 1245 | return TCL_ERROR; |
| 1246 | } |
| 1247 | for(i=2; i<5; i++){ |
| 1248 | if( Tcl_GetInt(interp, argv[i], &b[i-2]) ) return TCL_ERROR; |
| 1249 | a[i-2] = (long int)b[i-2]; |
drh | 7ed0cae | 2009-02-03 16:25:47 +0000 | [diff] [blame] | 1250 | a[i-2] &= (((u64)1)<<(sizeof(int)*8))-1; |
drh | c5cad1e | 2009-02-01 00:21:09 +0000 | [diff] [blame] | 1251 | } |
| 1252 | z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]); |
| 1253 | Tcl_AppendResult(interp, z, 0); |
| 1254 | sqlite3_free(z); |
| 1255 | return TCL_OK; |
| 1256 | } |
| 1257 | |
| 1258 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1259 | ** Usage: sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1260 | ** |
| 1261 | ** Call mprintf with two integer arguments and one string argument |
| 1262 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1263 | static int sqlite3_mprintf_str( |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1264 | void *NotUsed, |
| 1265 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1266 | int argc, /* Number of arguments */ |
| 1267 | char **argv /* Text of each argument */ |
| 1268 | ){ |
| 1269 | int a[3], i; |
| 1270 | char *z; |
chw | f220b24 | 2002-06-16 04:54:28 +0000 | [diff] [blame] | 1271 | if( argc<4 || argc>5 ){ |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1272 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
chw | f220b24 | 2002-06-16 04:54:28 +0000 | [diff] [blame] | 1273 | " FORMAT INT INT ?STRING?\"", 0); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1274 | return TCL_ERROR; |
| 1275 | } |
| 1276 | for(i=2; i<4; i++){ |
| 1277 | if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; |
| 1278 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1279 | 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] | 1280 | Tcl_AppendResult(interp, z, 0); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1281 | sqlite3_free(z); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1282 | return TCL_OK; |
| 1283 | } |
| 1284 | |
| 1285 | /* |
drh | b3738b6 | 2007-03-31 15:02:49 +0000 | [diff] [blame] | 1286 | ** Usage: sqlite3_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING |
| 1287 | ** |
| 1288 | ** Call mprintf with two integer arguments and one string argument |
| 1289 | */ |
| 1290 | static int sqlite3_snprintf_str( |
| 1291 | void *NotUsed, |
| 1292 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1293 | int argc, /* Number of arguments */ |
| 1294 | char **argv /* Text of each argument */ |
| 1295 | ){ |
| 1296 | int a[3], i; |
| 1297 | int n; |
| 1298 | char *z; |
| 1299 | if( argc<5 || argc>6 ){ |
| 1300 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1301 | " INT FORMAT INT INT ?STRING?\"", 0); |
| 1302 | return TCL_ERROR; |
| 1303 | } |
| 1304 | if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR; |
| 1305 | if( n<0 ){ |
| 1306 | Tcl_AppendResult(interp, "N must be non-negative", 0); |
| 1307 | return TCL_ERROR; |
| 1308 | } |
| 1309 | for(i=3; i<5; i++){ |
| 1310 | if( Tcl_GetInt(interp, argv[i], &a[i-3]) ) return TCL_ERROR; |
| 1311 | } |
| 1312 | z = sqlite3_malloc( n+1 ); |
| 1313 | sqlite3_snprintf(n, z, argv[2], a[0], a[1], argc>4 ? argv[5] : NULL); |
| 1314 | Tcl_AppendResult(interp, z, 0); |
| 1315 | sqlite3_free(z); |
| 1316 | return TCL_OK; |
| 1317 | } |
| 1318 | |
| 1319 | /* |
drh | 6378285 | 2005-08-30 19:30:59 +0000 | [diff] [blame] | 1320 | ** Usage: sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1321 | ** |
| 1322 | ** Call mprintf with two integer arguments and one double argument |
| 1323 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1324 | static int sqlite3_mprintf_double( |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1325 | void *NotUsed, |
| 1326 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1327 | int argc, /* Number of arguments */ |
| 1328 | char **argv /* Text of each argument */ |
| 1329 | ){ |
| 1330 | int a[3], i; |
| 1331 | double r; |
| 1332 | char *z; |
| 1333 | if( argc!=5 ){ |
| 1334 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
drh | 6378285 | 2005-08-30 19:30:59 +0000 | [diff] [blame] | 1335 | " FORMAT INT INT DOUBLE\"", 0); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1336 | return TCL_ERROR; |
| 1337 | } |
| 1338 | for(i=2; i<4; i++){ |
| 1339 | if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; |
| 1340 | } |
| 1341 | if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1342 | z = sqlite3_mprintf(argv[1], a[0], a[1], r); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1343 | Tcl_AppendResult(interp, z, 0); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1344 | sqlite3_free(z); |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 1345 | return TCL_OK; |
| 1346 | } |
| 1347 | |
| 1348 | /* |
drh | 6378285 | 2005-08-30 19:30:59 +0000 | [diff] [blame] | 1349 | ** Usage: sqlite3_mprintf_scaled FORMAT DOUBLE DOUBLE |
drh | b621c23 | 2004-02-21 19:41:04 +0000 | [diff] [blame] | 1350 | ** |
| 1351 | ** Call mprintf with a single double argument which is the product of the |
| 1352 | ** two arguments given above. This is used to generate overflow and underflow |
| 1353 | ** doubles to test that they are converted properly. |
| 1354 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1355 | static int sqlite3_mprintf_scaled( |
drh | b621c23 | 2004-02-21 19:41:04 +0000 | [diff] [blame] | 1356 | void *NotUsed, |
| 1357 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1358 | int argc, /* Number of arguments */ |
| 1359 | char **argv /* Text of each argument */ |
| 1360 | ){ |
| 1361 | int i; |
| 1362 | double r[2]; |
| 1363 | char *z; |
| 1364 | if( argc!=4 ){ |
| 1365 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1366 | " FORMAT DOUBLE DOUBLE\"", 0); |
| 1367 | return TCL_ERROR; |
| 1368 | } |
| 1369 | for(i=2; i<4; i++){ |
| 1370 | if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR; |
| 1371 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1372 | z = sqlite3_mprintf(argv[1], r[0]*r[1]); |
drh | b621c23 | 2004-02-21 19:41:04 +0000 | [diff] [blame] | 1373 | Tcl_AppendResult(interp, z, 0); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1374 | sqlite3_free(z); |
drh | b621c23 | 2004-02-21 19:41:04 +0000 | [diff] [blame] | 1375 | return TCL_OK; |
| 1376 | } |
| 1377 | |
| 1378 | /* |
drh | e29b1a0 | 2004-07-17 21:56:09 +0000 | [diff] [blame] | 1379 | ** Usage: sqlite3_mprintf_stronly FORMAT STRING |
| 1380 | ** |
| 1381 | ** Call mprintf with a single double argument which is the product of the |
| 1382 | ** two arguments given above. This is used to generate overflow and underflow |
| 1383 | ** doubles to test that they are converted properly. |
| 1384 | */ |
| 1385 | static int sqlite3_mprintf_stronly( |
| 1386 | void *NotUsed, |
| 1387 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1388 | int argc, /* Number of arguments */ |
| 1389 | char **argv /* Text of each argument */ |
| 1390 | ){ |
| 1391 | char *z; |
| 1392 | if( argc!=3 ){ |
| 1393 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1394 | " FORMAT STRING\"", 0); |
| 1395 | return TCL_ERROR; |
| 1396 | } |
| 1397 | z = sqlite3_mprintf(argv[1], argv[2]); |
| 1398 | Tcl_AppendResult(interp, z, 0); |
| 1399 | sqlite3_free(z); |
| 1400 | return TCL_OK; |
| 1401 | } |
| 1402 | |
| 1403 | /* |
drh | 6378285 | 2005-08-30 19:30:59 +0000 | [diff] [blame] | 1404 | ** Usage: sqlite3_mprintf_hexdouble FORMAT HEX |
| 1405 | ** |
| 1406 | ** Call mprintf with a single double argument which is derived from the |
| 1407 | ** hexadecimal encoding of an IEEE double. |
| 1408 | */ |
| 1409 | static int sqlite3_mprintf_hexdouble( |
| 1410 | void *NotUsed, |
| 1411 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1412 | int argc, /* Number of arguments */ |
| 1413 | char **argv /* Text of each argument */ |
| 1414 | ){ |
| 1415 | char *z; |
| 1416 | double r; |
shane | 5e73db3 | 2008-07-08 03:04:58 +0000 | [diff] [blame] | 1417 | unsigned int x1, x2; |
| 1418 | sqlite_uint64 d; |
drh | 6378285 | 2005-08-30 19:30:59 +0000 | [diff] [blame] | 1419 | if( argc!=3 ){ |
| 1420 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1421 | " FORMAT STRING\"", 0); |
| 1422 | return TCL_ERROR; |
| 1423 | } |
| 1424 | if( sscanf(argv[2], "%08x%08x", &x2, &x1)!=2 ){ |
| 1425 | Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", 0); |
| 1426 | return TCL_ERROR; |
| 1427 | } |
| 1428 | d = x2; |
| 1429 | d = (d<<32) + x1; |
| 1430 | memcpy(&r, &d, sizeof(r)); |
| 1431 | z = sqlite3_mprintf(argv[1], r); |
| 1432 | Tcl_AppendResult(interp, z, 0); |
| 1433 | sqlite3_free(z); |
| 1434 | return TCL_OK; |
| 1435 | } |
| 1436 | |
| 1437 | /* |
danielk1977 | c1def3e | 2008-08-30 13:25:10 +0000 | [diff] [blame] | 1438 | ** Usage: sqlite3_enable_shared_cache ?BOOLEAN? |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1439 | ** |
| 1440 | */ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1441 | #if !defined(SQLITE_OMIT_SHARED_CACHE) |
| 1442 | static int test_enable_shared( |
danielk1977 | 5262282 | 2006-01-09 09:59:49 +0000 | [diff] [blame] | 1443 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1444 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1445 | int objc, /* Number of arguments */ |
| 1446 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1447 | ){ |
| 1448 | int rc; |
| 1449 | int enable; |
danielk1977 | 5262282 | 2006-01-09 09:59:49 +0000 | [diff] [blame] | 1450 | int ret = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1451 | |
danielk1977 | c1def3e | 2008-08-30 13:25:10 +0000 | [diff] [blame] | 1452 | if( objc!=2 && objc!=1 ){ |
| 1453 | Tcl_WrongNumArgs(interp, 1, objv, "?BOOLEAN?"); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1454 | return TCL_ERROR; |
| 1455 | } |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 1456 | ret = sqlite3GlobalConfig.sharedCacheEnabled; |
danielk1977 | c1def3e | 2008-08-30 13:25:10 +0000 | [diff] [blame] | 1457 | |
| 1458 | if( objc==2 ){ |
| 1459 | if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ){ |
| 1460 | return TCL_ERROR; |
| 1461 | } |
| 1462 | rc = sqlite3_enable_shared_cache(enable); |
| 1463 | if( rc!=SQLITE_OK ){ |
| 1464 | Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC); |
| 1465 | return TCL_ERROR; |
| 1466 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1467 | } |
danielk1977 | 5262282 | 2006-01-09 09:59:49 +0000 | [diff] [blame] | 1468 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(ret)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1469 | return TCL_OK; |
| 1470 | } |
| 1471 | #endif |
| 1472 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1473 | |
| 1474 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1475 | /* |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1476 | ** Usage: sqlite3_extended_result_codes DB BOOLEAN |
| 1477 | ** |
| 1478 | */ |
| 1479 | static int test_extended_result_codes( |
| 1480 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 1481 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1482 | int objc, /* Number of arguments */ |
| 1483 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1484 | ){ |
| 1485 | int enable; |
| 1486 | sqlite3 *db; |
| 1487 | |
| 1488 | if( objc!=3 ){ |
| 1489 | Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN"); |
| 1490 | return TCL_ERROR; |
| 1491 | } |
| 1492 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 1493 | if( Tcl_GetBooleanFromObj(interp, objv[2], &enable) ) return TCL_ERROR; |
| 1494 | sqlite3_extended_result_codes(db, enable); |
| 1495 | return TCL_OK; |
| 1496 | } |
| 1497 | |
| 1498 | /* |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 1499 | ** Usage: sqlite3_libversion_number |
| 1500 | ** |
| 1501 | */ |
| 1502 | static int test_libversion_number( |
| 1503 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 1504 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1505 | int objc, /* Number of arguments */ |
| 1506 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1507 | ){ |
| 1508 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_libversion_number())); |
| 1509 | return TCL_OK; |
| 1510 | } |
| 1511 | |
| 1512 | /* |
danielk1977 | deb802c | 2006-02-09 13:43:28 +0000 | [diff] [blame] | 1513 | ** Usage: sqlite3_table_column_metadata DB dbname tblname colname |
| 1514 | ** |
| 1515 | */ |
| 1516 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 1517 | static int test_table_column_metadata( |
| 1518 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 1519 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1520 | int objc, /* Number of arguments */ |
| 1521 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1522 | ){ |
| 1523 | sqlite3 *db; |
| 1524 | const char *zDb; |
| 1525 | const char *zTbl; |
| 1526 | const char *zCol; |
| 1527 | int rc; |
| 1528 | Tcl_Obj *pRet; |
| 1529 | |
| 1530 | const char *zDatatype; |
| 1531 | const char *zCollseq; |
| 1532 | int notnull; |
| 1533 | int primarykey; |
| 1534 | int autoincrement; |
| 1535 | |
| 1536 | if( objc!=5 ){ |
| 1537 | Tcl_WrongNumArgs(interp, 1, objv, "DB dbname tblname colname"); |
| 1538 | return TCL_ERROR; |
| 1539 | } |
| 1540 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 1541 | zDb = Tcl_GetString(objv[2]); |
| 1542 | zTbl = Tcl_GetString(objv[3]); |
| 1543 | zCol = Tcl_GetString(objv[4]); |
| 1544 | |
| 1545 | if( strlen(zDb)==0 ) zDb = 0; |
| 1546 | |
| 1547 | rc = sqlite3_table_column_metadata(db, zDb, zTbl, zCol, |
| 1548 | &zDatatype, &zCollseq, ¬null, &primarykey, &autoincrement); |
| 1549 | |
| 1550 | if( rc!=SQLITE_OK ){ |
| 1551 | Tcl_AppendResult(interp, sqlite3_errmsg(db), 0); |
| 1552 | return TCL_ERROR; |
| 1553 | } |
| 1554 | |
| 1555 | pRet = Tcl_NewObj(); |
| 1556 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zDatatype, -1)); |
| 1557 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zCollseq, -1)); |
| 1558 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(notnull)); |
| 1559 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(primarykey)); |
| 1560 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(autoincrement)); |
| 1561 | Tcl_SetObjResult(interp, pRet); |
| 1562 | |
| 1563 | return TCL_OK; |
| 1564 | } |
| 1565 | #endif |
| 1566 | |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1567 | #ifndef SQLITE_OMIT_INCRBLOB |
| 1568 | |
dan | 61c7f59 | 2010-10-26 18:42:52 +0000 | [diff] [blame] | 1569 | static int blobHandleFromObj( |
| 1570 | Tcl_Interp *interp, |
| 1571 | Tcl_Obj *pObj, |
| 1572 | sqlite3_blob **ppBlob |
| 1573 | ){ |
| 1574 | char *z; |
| 1575 | int n; |
| 1576 | |
| 1577 | z = Tcl_GetStringFromObj(pObj, &n); |
| 1578 | if( n==0 ){ |
| 1579 | *ppBlob = 0; |
| 1580 | }else{ |
| 1581 | int notUsed; |
| 1582 | Tcl_Channel channel; |
| 1583 | ClientData instanceData; |
| 1584 | |
| 1585 | channel = Tcl_GetChannel(interp, z, ¬Used); |
| 1586 | if( !channel ) return TCL_ERROR; |
| 1587 | |
| 1588 | Tcl_Flush(channel); |
| 1589 | Tcl_Seek(channel, 0, SEEK_SET); |
| 1590 | |
| 1591 | instanceData = Tcl_GetChannelInstanceData(channel); |
| 1592 | *ppBlob = *((sqlite3_blob **)instanceData); |
| 1593 | } |
| 1594 | |
| 1595 | return TCL_OK; |
| 1596 | } |
| 1597 | |
| 1598 | /* |
| 1599 | ** sqlite3_blob_bytes CHANNEL |
| 1600 | */ |
| 1601 | static int test_blob_bytes( |
| 1602 | ClientData clientData, /* Not used */ |
| 1603 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1604 | int objc, /* Number of arguments */ |
| 1605 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1606 | ){ |
| 1607 | sqlite3_blob *pBlob; |
| 1608 | int nByte; |
| 1609 | |
| 1610 | if( objc!=2 ){ |
| 1611 | Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL"); |
| 1612 | return TCL_ERROR; |
| 1613 | } |
| 1614 | |
| 1615 | if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR; |
| 1616 | nByte = sqlite3_blob_bytes(pBlob); |
| 1617 | Tcl_SetObjResult(interp, Tcl_NewIntObj(nByte)); |
| 1618 | |
| 1619 | return TCL_OK; |
| 1620 | } |
| 1621 | |
| 1622 | /* |
| 1623 | ** sqlite3_blob_close CHANNEL |
| 1624 | */ |
| 1625 | static int test_blob_close( |
| 1626 | ClientData clientData, /* Not used */ |
| 1627 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1628 | int objc, /* Number of arguments */ |
| 1629 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1630 | ){ |
| 1631 | sqlite3_blob *pBlob; |
dan | 61c7f59 | 2010-10-26 18:42:52 +0000 | [diff] [blame] | 1632 | |
| 1633 | if( objc!=2 ){ |
| 1634 | Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL"); |
| 1635 | return TCL_ERROR; |
| 1636 | } |
| 1637 | |
| 1638 | if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR; |
| 1639 | sqlite3_blob_close(pBlob); |
| 1640 | |
| 1641 | return TCL_OK; |
| 1642 | } |
| 1643 | |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1644 | /* |
| 1645 | ** sqlite3_blob_read CHANNEL OFFSET N |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1646 | ** |
| 1647 | ** This command is used to test the sqlite3_blob_read() in ways that |
| 1648 | ** the Tcl channel interface does not. The first argument should |
| 1649 | ** be the name of a valid channel created by the [incrblob] method |
| 1650 | ** of a database handle. This function calls sqlite3_blob_read() |
| 1651 | ** to read N bytes from offset OFFSET from the underlying SQLite |
| 1652 | ** blob handle. |
| 1653 | ** |
| 1654 | ** On success, a byte-array object containing the read data is |
| 1655 | ** returned. On failure, the interpreter result is set to the |
| 1656 | ** text representation of the returned error code (i.e. "SQLITE_NOMEM") |
| 1657 | ** and a Tcl exception is thrown. |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1658 | */ |
| 1659 | static int test_blob_read( |
| 1660 | ClientData clientData, /* Not used */ |
| 1661 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1662 | int objc, /* Number of arguments */ |
| 1663 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1664 | ){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1665 | sqlite3_blob *pBlob; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1666 | int nByte; |
| 1667 | int iOffset; |
dan | f1f22bc | 2010-10-27 19:08:26 +0000 | [diff] [blame] | 1668 | unsigned char *zBuf = 0; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1669 | int rc; |
| 1670 | |
| 1671 | if( objc!=4 ){ |
| 1672 | Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET N"); |
| 1673 | return TCL_ERROR; |
| 1674 | } |
| 1675 | |
dan | 61c7f59 | 2010-10-26 18:42:52 +0000 | [diff] [blame] | 1676 | if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR; |
| 1677 | if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset) |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1678 | || TCL_OK!=Tcl_GetIntFromObj(interp, objv[3], &nByte) |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1679 | ){ |
| 1680 | return TCL_ERROR; |
| 1681 | } |
| 1682 | |
dan | f1f22bc | 2010-10-27 19:08:26 +0000 | [diff] [blame] | 1683 | if( nByte>0 ){ |
| 1684 | zBuf = (unsigned char *)Tcl_Alloc(nByte); |
| 1685 | } |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1686 | rc = sqlite3_blob_read(pBlob, zBuf, nByte, iOffset); |
| 1687 | if( rc==SQLITE_OK ){ |
| 1688 | Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zBuf, nByte)); |
| 1689 | }else{ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 1690 | Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1691 | } |
| 1692 | Tcl_Free((char *)zBuf); |
| 1693 | |
| 1694 | return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR); |
| 1695 | } |
| 1696 | |
| 1697 | /* |
danielk1977 | a8b3018 | 2008-10-02 14:49:01 +0000 | [diff] [blame] | 1698 | ** sqlite3_blob_write CHANNEL OFFSET DATA ?NDATA? |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1699 | ** |
| 1700 | ** This command is used to test the sqlite3_blob_write() in ways that |
| 1701 | ** the Tcl channel interface does not. The first argument should |
| 1702 | ** be the name of a valid channel created by the [incrblob] method |
| 1703 | ** of a database handle. This function calls sqlite3_blob_write() |
| 1704 | ** to write the DATA byte-array to the underlying SQLite blob handle. |
| 1705 | ** at offset OFFSET. |
| 1706 | ** |
| 1707 | ** On success, an empty string is returned. On failure, the interpreter |
| 1708 | ** result is set to the text representation of the returned error code |
| 1709 | ** (i.e. "SQLITE_NOMEM") and a Tcl exception is thrown. |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1710 | */ |
| 1711 | static int test_blob_write( |
| 1712 | ClientData clientData, /* Not used */ |
| 1713 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1714 | int objc, /* Number of arguments */ |
| 1715 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1716 | ){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1717 | sqlite3_blob *pBlob; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1718 | int iOffset; |
| 1719 | int rc; |
| 1720 | |
| 1721 | unsigned char *zBuf; |
| 1722 | int nBuf; |
| 1723 | |
danielk1977 | a8b3018 | 2008-10-02 14:49:01 +0000 | [diff] [blame] | 1724 | if( objc!=4 && objc!=5 ){ |
| 1725 | Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET DATA ?NDATA?"); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1726 | return TCL_ERROR; |
| 1727 | } |
| 1728 | |
dan | 61c7f59 | 2010-10-26 18:42:52 +0000 | [diff] [blame] | 1729 | if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR; |
| 1730 | if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset) ){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1731 | return TCL_ERROR; |
| 1732 | } |
| 1733 | |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1734 | zBuf = Tcl_GetByteArrayFromObj(objv[3], &nBuf); |
danielk1977 | a8b3018 | 2008-10-02 14:49:01 +0000 | [diff] [blame] | 1735 | if( objc==5 && Tcl_GetIntFromObj(interp, objv[4], &nBuf) ){ |
| 1736 | return TCL_ERROR; |
| 1737 | } |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1738 | rc = sqlite3_blob_write(pBlob, zBuf, nBuf, iOffset); |
| 1739 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 1740 | Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR); |
| 1744 | } |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 1745 | |
| 1746 | static int test_blob_reopen( |
| 1747 | ClientData clientData, /* Not used */ |
| 1748 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1749 | int objc, /* Number of arguments */ |
| 1750 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1751 | ){ |
| 1752 | Tcl_WideInt iRowid; |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 1753 | sqlite3_blob *pBlob; |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 1754 | int rc; |
| 1755 | |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 1756 | if( objc!=3 ){ |
| 1757 | Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL ROWID"); |
| 1758 | return TCL_ERROR; |
| 1759 | } |
| 1760 | |
dan | 61c7f59 | 2010-10-26 18:42:52 +0000 | [diff] [blame] | 1761 | if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR; |
| 1762 | if( Tcl_GetWideIntFromObj(interp, objv[2], &iRowid) ) return TCL_ERROR; |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 1763 | |
| 1764 | rc = sqlite3_blob_reopen(pBlob, iRowid); |
| 1765 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 1766 | Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE); |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR); |
| 1770 | } |
| 1771 | |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 1772 | #endif |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 1773 | |
danielk1977 | deb802c | 2006-02-09 13:43:28 +0000 | [diff] [blame] | 1774 | /* |
danielk1977 | a393c03 | 2007-05-07 14:58:53 +0000 | [diff] [blame] | 1775 | ** Usage: sqlite3_create_collation_v2 DB-HANDLE NAME CMP-PROC DEL-PROC |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1776 | ** |
| 1777 | ** This Tcl proc is used for testing the experimental |
danielk1977 | a393c03 | 2007-05-07 14:58:53 +0000 | [diff] [blame] | 1778 | ** sqlite3_create_collation_v2() interface. |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1779 | */ |
| 1780 | struct TestCollationX { |
| 1781 | Tcl_Interp *interp; |
| 1782 | Tcl_Obj *pCmp; |
| 1783 | Tcl_Obj *pDel; |
| 1784 | }; |
| 1785 | typedef struct TestCollationX TestCollationX; |
| 1786 | static void testCreateCollationDel(void *pCtx){ |
| 1787 | TestCollationX *p = (TestCollationX *)pCtx; |
| 1788 | |
| 1789 | int rc = Tcl_EvalObjEx(p->interp, p->pDel, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL); |
| 1790 | if( rc!=TCL_OK ){ |
| 1791 | Tcl_BackgroundError(p->interp); |
| 1792 | } |
| 1793 | |
| 1794 | Tcl_DecrRefCount(p->pCmp); |
| 1795 | Tcl_DecrRefCount(p->pDel); |
| 1796 | sqlite3_free((void *)p); |
| 1797 | } |
| 1798 | static int testCreateCollationCmp( |
| 1799 | void *pCtx, |
| 1800 | int nLeft, |
| 1801 | const void *zLeft, |
| 1802 | int nRight, |
| 1803 | const void *zRight |
| 1804 | ){ |
| 1805 | TestCollationX *p = (TestCollationX *)pCtx; |
| 1806 | Tcl_Obj *pScript = Tcl_DuplicateObj(p->pCmp); |
| 1807 | int iRes = 0; |
| 1808 | |
| 1809 | Tcl_IncrRefCount(pScript); |
| 1810 | Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zLeft, nLeft)); |
| 1811 | Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zRight,nRight)); |
| 1812 | |
| 1813 | if( TCL_OK!=Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL) |
| 1814 | || TCL_OK!=Tcl_GetIntFromObj(p->interp, Tcl_GetObjResult(p->interp), &iRes) |
| 1815 | ){ |
| 1816 | Tcl_BackgroundError(p->interp); |
| 1817 | } |
| 1818 | Tcl_DecrRefCount(pScript); |
| 1819 | |
| 1820 | return iRes; |
| 1821 | } |
danielk1977 | a393c03 | 2007-05-07 14:58:53 +0000 | [diff] [blame] | 1822 | static int test_create_collation_v2( |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1823 | ClientData clientData, /* Not used */ |
| 1824 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1825 | int objc, /* Number of arguments */ |
| 1826 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1827 | ){ |
| 1828 | TestCollationX *p; |
| 1829 | sqlite3 *db; |
drh | d55d57e | 2008-07-07 17:53:07 +0000 | [diff] [blame] | 1830 | int rc; |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1831 | |
| 1832 | if( objc!=5 ){ |
| 1833 | Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE NAME CMP-PROC DEL-PROC"); |
| 1834 | return TCL_ERROR; |
| 1835 | } |
| 1836 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 1837 | |
| 1838 | p = (TestCollationX *)sqlite3_malloc(sizeof(TestCollationX)); |
| 1839 | p->pCmp = objv[3]; |
| 1840 | p->pDel = objv[4]; |
| 1841 | p->interp = interp; |
| 1842 | Tcl_IncrRefCount(p->pCmp); |
| 1843 | Tcl_IncrRefCount(p->pDel); |
| 1844 | |
drh | d55d57e | 2008-07-07 17:53:07 +0000 | [diff] [blame] | 1845 | rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), 16, |
| 1846 | (void *)p, testCreateCollationCmp, testCreateCollationDel |
| 1847 | ); |
| 1848 | if( rc!=SQLITE_MISUSE ){ |
| 1849 | Tcl_AppendResult(interp, "sqlite3_create_collate_v2() failed to detect " |
| 1850 | "an invalid encoding", (char*)0); |
| 1851 | return TCL_ERROR; |
| 1852 | } |
| 1853 | rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), SQLITE_UTF8, |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1854 | (void *)p, testCreateCollationCmp, testCreateCollationDel |
| 1855 | ); |
| 1856 | return TCL_OK; |
| 1857 | } |
| 1858 | |
| 1859 | /* |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 1860 | ** USAGE: sqlite3_create_function_v2 DB NAME NARG ENC ?SWITCHES? |
| 1861 | ** |
| 1862 | ** Available switches are: |
| 1863 | ** |
| 1864 | ** -func SCRIPT |
| 1865 | ** -step SCRIPT |
| 1866 | ** -final SCRIPT |
| 1867 | ** -destroy SCRIPT |
| 1868 | */ |
| 1869 | typedef struct CreateFunctionV2 CreateFunctionV2; |
| 1870 | struct CreateFunctionV2 { |
| 1871 | Tcl_Interp *interp; |
| 1872 | Tcl_Obj *pFunc; /* Script for function invocation */ |
| 1873 | Tcl_Obj *pStep; /* Script for agg. step invocation */ |
| 1874 | Tcl_Obj *pFinal; /* Script for agg. finalization invocation */ |
| 1875 | Tcl_Obj *pDestroy; /* Destructor script */ |
| 1876 | }; |
| 1877 | static void cf2Func(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){ |
| 1878 | } |
| 1879 | static void cf2Step(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){ |
| 1880 | } |
| 1881 | static void cf2Final(sqlite3_context *ctx){ |
| 1882 | } |
| 1883 | static void cf2Destroy(void *pUser){ |
| 1884 | CreateFunctionV2 *p = (CreateFunctionV2 *)pUser; |
| 1885 | |
| 1886 | if( p->interp && p->pDestroy ){ |
| 1887 | int rc = Tcl_EvalObjEx(p->interp, p->pDestroy, 0); |
| 1888 | if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp); |
| 1889 | } |
| 1890 | |
| 1891 | if( p->pFunc ) Tcl_DecrRefCount(p->pFunc); |
| 1892 | if( p->pStep ) Tcl_DecrRefCount(p->pStep); |
| 1893 | if( p->pFinal ) Tcl_DecrRefCount(p->pFinal); |
| 1894 | if( p->pDestroy ) Tcl_DecrRefCount(p->pDestroy); |
| 1895 | sqlite3_free(p); |
| 1896 | } |
| 1897 | static int test_create_function_v2( |
| 1898 | ClientData clientData, /* Not used */ |
| 1899 | Tcl_Interp *interp, /* The invoking TCL interpreter */ |
| 1900 | int objc, /* Number of arguments */ |
| 1901 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1902 | ){ |
| 1903 | sqlite3 *db; |
| 1904 | const char *zFunc; |
| 1905 | int nArg; |
| 1906 | int enc; |
| 1907 | CreateFunctionV2 *p; |
| 1908 | int i; |
| 1909 | int rc; |
| 1910 | |
| 1911 | struct EncTable { |
| 1912 | const char *zEnc; |
| 1913 | int enc; |
| 1914 | } aEnc[] = { |
| 1915 | {"utf8", SQLITE_UTF8 }, |
| 1916 | {"utf16", SQLITE_UTF16 }, |
| 1917 | {"utf16le", SQLITE_UTF16LE }, |
| 1918 | {"utf16be", SQLITE_UTF16BE }, |
| 1919 | {"any", SQLITE_ANY }, |
| 1920 | {"0", 0 } |
| 1921 | }; |
| 1922 | |
| 1923 | if( objc<5 || (objc%2)==0 ){ |
| 1924 | Tcl_WrongNumArgs(interp, 1, objv, "DB NAME NARG ENC SWITCHES..."); |
| 1925 | return TCL_ERROR; |
| 1926 | } |
| 1927 | |
| 1928 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 1929 | zFunc = Tcl_GetString(objv[2]); |
| 1930 | if( Tcl_GetIntFromObj(interp, objv[3], &nArg) ) return TCL_ERROR; |
| 1931 | if( Tcl_GetIndexFromObjStruct(interp, objv[4], aEnc, sizeof(aEnc[0]), |
| 1932 | "encoding", 0, &enc) |
| 1933 | ){ |
| 1934 | return TCL_ERROR; |
| 1935 | } |
| 1936 | enc = aEnc[enc].enc; |
| 1937 | |
| 1938 | p = sqlite3_malloc(sizeof(CreateFunctionV2)); |
| 1939 | assert( p ); |
| 1940 | memset(p, 0, sizeof(CreateFunctionV2)); |
| 1941 | p->interp = interp; |
| 1942 | |
| 1943 | for(i=5; i<objc; i+=2){ |
| 1944 | int iSwitch; |
| 1945 | const char *azSwitch[] = {"-func", "-step", "-final", "-destroy", 0}; |
| 1946 | if( Tcl_GetIndexFromObj(interp, objv[i], azSwitch, "switch", 0, &iSwitch) ){ |
| 1947 | sqlite3_free(p); |
| 1948 | return TCL_ERROR; |
| 1949 | } |
| 1950 | |
| 1951 | switch( iSwitch ){ |
| 1952 | case 0: p->pFunc = objv[i+1]; break; |
| 1953 | case 1: p->pStep = objv[i+1]; break; |
| 1954 | case 2: p->pFinal = objv[i+1]; break; |
| 1955 | case 3: p->pDestroy = objv[i+1]; break; |
| 1956 | } |
| 1957 | } |
| 1958 | if( p->pFunc ) p->pFunc = Tcl_DuplicateObj(p->pFunc); |
| 1959 | if( p->pStep ) p->pStep = Tcl_DuplicateObj(p->pStep); |
| 1960 | if( p->pFinal ) p->pFinal = Tcl_DuplicateObj(p->pFinal); |
| 1961 | if( p->pDestroy ) p->pDestroy = Tcl_DuplicateObj(p->pDestroy); |
| 1962 | |
| 1963 | if( p->pFunc ) Tcl_IncrRefCount(p->pFunc); |
| 1964 | if( p->pStep ) Tcl_IncrRefCount(p->pStep); |
| 1965 | if( p->pFinal ) Tcl_IncrRefCount(p->pFinal); |
| 1966 | if( p->pDestroy ) Tcl_IncrRefCount(p->pDestroy); |
| 1967 | |
| 1968 | rc = sqlite3_create_function_v2(db, zFunc, nArg, enc, (void *)p, |
| 1969 | (p->pFunc ? cf2Func : 0), |
| 1970 | (p->pStep ? cf2Step : 0), |
| 1971 | (p->pFinal ? cf2Final : 0), |
| 1972 | cf2Destroy |
| 1973 | ); |
| 1974 | if( rc!=SQLITE_OK ){ |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 1975 | Tcl_ResetResult(interp); |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 1976 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 1977 | return TCL_ERROR; |
| 1978 | } |
| 1979 | return TCL_OK; |
| 1980 | } |
| 1981 | |
| 1982 | /* |
danielk1977 | 69e777f | 2006-06-14 10:38:02 +0000 | [diff] [blame] | 1983 | ** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC? |
| 1984 | */ |
| 1985 | static int test_load_extension( |
| 1986 | ClientData clientData, /* Not used */ |
| 1987 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1988 | int objc, /* Number of arguments */ |
| 1989 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1990 | ){ |
| 1991 | Tcl_CmdInfo cmdInfo; |
| 1992 | sqlite3 *db; |
| 1993 | int rc; |
| 1994 | char *zDb; |
| 1995 | char *zFile; |
| 1996 | char *zProc = 0; |
| 1997 | char *zErr = 0; |
| 1998 | |
| 1999 | if( objc!=4 && objc!=3 ){ |
| 2000 | Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE FILE ?PROC?"); |
| 2001 | return TCL_ERROR; |
| 2002 | } |
| 2003 | zDb = Tcl_GetString(objv[1]); |
| 2004 | zFile = Tcl_GetString(objv[2]); |
| 2005 | if( objc==4 ){ |
| 2006 | zProc = Tcl_GetString(objv[3]); |
| 2007 | } |
| 2008 | |
| 2009 | /* Extract the C database handle from the Tcl command name */ |
| 2010 | if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){ |
| 2011 | Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0); |
| 2012 | return TCL_ERROR; |
| 2013 | } |
| 2014 | db = ((struct SqliteDb*)cmdInfo.objClientData)->db; |
| 2015 | assert(db); |
| 2016 | |
| 2017 | /* Call the underlying C function. If an error occurs, set rc to |
| 2018 | ** TCL_ERROR and load any error string into the interpreter. If no |
| 2019 | ** error occurs, set rc to TCL_OK. |
| 2020 | */ |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 2021 | #ifdef SQLITE_OMIT_LOAD_EXTENSION |
| 2022 | rc = SQLITE_ERROR; |
| 2023 | zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()"); |
| 2024 | #else |
danielk1977 | 69e777f | 2006-06-14 10:38:02 +0000 | [diff] [blame] | 2025 | rc = sqlite3_load_extension(db, zFile, zProc, &zErr); |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 2026 | #endif |
danielk1977 | 69e777f | 2006-06-14 10:38:02 +0000 | [diff] [blame] | 2027 | if( rc!=SQLITE_OK ){ |
| 2028 | Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE); |
| 2029 | rc = TCL_ERROR; |
| 2030 | }else{ |
| 2031 | rc = TCL_OK; |
| 2032 | } |
| 2033 | sqlite3_free(zErr); |
| 2034 | |
| 2035 | return rc; |
| 2036 | } |
| 2037 | |
| 2038 | /* |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 2039 | ** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF |
| 2040 | */ |
| 2041 | static int test_enable_load( |
| 2042 | ClientData clientData, /* Not used */ |
| 2043 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 2044 | int objc, /* Number of arguments */ |
| 2045 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 2046 | ){ |
| 2047 | Tcl_CmdInfo cmdInfo; |
| 2048 | sqlite3 *db; |
| 2049 | char *zDb; |
| 2050 | int onoff; |
| 2051 | |
| 2052 | if( objc!=3 ){ |
| 2053 | Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF"); |
| 2054 | return TCL_ERROR; |
| 2055 | } |
| 2056 | zDb = Tcl_GetString(objv[1]); |
| 2057 | |
| 2058 | /* Extract the C database handle from the Tcl command name */ |
| 2059 | if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){ |
| 2060 | Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0); |
| 2061 | return TCL_ERROR; |
| 2062 | } |
| 2063 | db = ((struct SqliteDb*)cmdInfo.objClientData)->db; |
| 2064 | assert(db); |
| 2065 | |
| 2066 | /* Get the onoff parameter */ |
| 2067 | if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){ |
| 2068 | return TCL_ERROR; |
| 2069 | } |
| 2070 | |
| 2071 | #ifdef SQLITE_OMIT_LOAD_EXTENSION |
| 2072 | Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()"); |
| 2073 | return TCL_ERROR; |
| 2074 | #else |
| 2075 | sqlite3_enable_load_extension(db, onoff); |
| 2076 | return TCL_OK; |
| 2077 | #endif |
| 2078 | } |
| 2079 | |
| 2080 | /* |
drh | 28b4e48 | 2002-03-11 02:06:13 +0000 | [diff] [blame] | 2081 | ** Usage: sqlite_abort |
| 2082 | ** |
| 2083 | ** Shutdown the process immediately. This is not a clean shutdown. |
| 2084 | ** This command is used to test the recoverability of a database in |
| 2085 | ** the event of a program crash. |
| 2086 | */ |
| 2087 | static int sqlite_abort( |
| 2088 | void *NotUsed, |
| 2089 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 2090 | int argc, /* Number of arguments */ |
| 2091 | char **argv /* Text of each argument */ |
| 2092 | ){ |
shaneh | 2ceced1 | 2010-07-07 16:51:36 +0000 | [diff] [blame] | 2093 | #if defined(_MSC_VER) |
| 2094 | /* We do this, otherwise the test will halt with a popup message |
| 2095 | * that we have to click away before the test will continue. |
| 2096 | */ |
| 2097 | _set_abort_behavior( 0, _CALL_REPORTFAULT ); |
| 2098 | #endif |
dan | d3f6b81 | 2010-07-19 12:44:14 +0000 | [diff] [blame] | 2099 | exit(255); |
drh | 28b4e48 | 2002-03-11 02:06:13 +0000 | [diff] [blame] | 2100 | assert( interp==0 ); /* This will always fail */ |
| 2101 | return TCL_OK; |
| 2102 | } |
| 2103 | |
| 2104 | /* |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 2105 | ** The following routine is a user-defined SQL function whose purpose |
| 2106 | ** is to test the sqlite_set_result() API. |
| 2107 | */ |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 2108 | static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 2109 | while( argc>=2 ){ |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 2110 | const char *zArg0 = (char*)sqlite3_value_text(argv[0]); |
danielk1977 | 6d88bad | 2004-05-27 14:23:36 +0000 | [diff] [blame] | 2111 | if( zArg0 ){ |
| 2112 | if( 0==sqlite3StrICmp(zArg0, "int") ){ |
| 2113 | sqlite3_result_int(context, sqlite3_value_int(argv[1])); |
| 2114 | }else if( sqlite3StrICmp(zArg0,"int64")==0 ){ |
| 2115 | sqlite3_result_int64(context, sqlite3_value_int64(argv[1])); |
| 2116 | }else if( sqlite3StrICmp(zArg0,"string")==0 ){ |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 2117 | sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2118 | SQLITE_TRANSIENT); |
danielk1977 | 6d88bad | 2004-05-27 14:23:36 +0000 | [diff] [blame] | 2119 | }else if( sqlite3StrICmp(zArg0,"double")==0 ){ |
| 2120 | sqlite3_result_double(context, sqlite3_value_double(argv[1])); |
| 2121 | }else if( sqlite3StrICmp(zArg0,"null")==0 ){ |
| 2122 | sqlite3_result_null(context); |
| 2123 | }else if( sqlite3StrICmp(zArg0,"value")==0 ){ |
| 2124 | sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]); |
| 2125 | }else{ |
| 2126 | goto error_out; |
| 2127 | } |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 2128 | }else{ |
danielk1977 | 6d88bad | 2004-05-27 14:23:36 +0000 | [diff] [blame] | 2129 | goto error_out; |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 2130 | } |
| 2131 | argc -= 2; |
| 2132 | argv += 2; |
| 2133 | } |
danielk1977 | 6d88bad | 2004-05-27 14:23:36 +0000 | [diff] [blame] | 2134 | return; |
| 2135 | |
| 2136 | error_out: |
| 2137 | sqlite3_result_error(context,"first argument should be one of: " |
| 2138 | "int int64 string double null value", -1); |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
| 2141 | /* |
| 2142 | ** Usage: sqlite_register_test_function DB NAME |
| 2143 | ** |
| 2144 | ** Register the test SQL function on the database DB under the name NAME. |
| 2145 | */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 2146 | static int test_register_func( |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 2147 | void *NotUsed, |
| 2148 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 2149 | int argc, /* Number of arguments */ |
| 2150 | char **argv /* Text of each argument */ |
| 2151 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 2152 | sqlite3 *db; |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 2153 | int rc; |
| 2154 | if( argc!=3 ){ |
| 2155 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 2156 | " DB FUNCTION-NAME", 0); |
| 2157 | return TCL_ERROR; |
| 2158 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2159 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 2160 | rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2161 | testFunc, 0, 0); |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 2162 | if( rc!=0 ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 2163 | Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0); |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 2164 | return TCL_ERROR; |
| 2165 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2166 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 2167 | return TCL_OK; |
| 2168 | } |
| 2169 | |
| 2170 | /* |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2171 | ** Usage: sqlite3_finalize STMT |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2172 | ** |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2173 | ** Finalize a statement handle. |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2174 | */ |
| 2175 | static int test_finalize( |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2176 | void * clientData, |
| 2177 | Tcl_Interp *interp, |
| 2178 | int objc, |
| 2179 | Tcl_Obj *CONST objv[] |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2180 | ){ |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2181 | sqlite3_stmt *pStmt; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2182 | int rc; |
drh | dddb2f2 | 2007-01-03 23:37:28 +0000 | [diff] [blame] | 2183 | sqlite3 *db = 0; |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2184 | |
| 2185 | if( objc!=2 ){ |
| 2186 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2187 | Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2188 | return TCL_ERROR; |
| 2189 | } |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2190 | |
| 2191 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2192 | |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 2193 | if( pStmt ){ |
| 2194 | db = StmtToDb(pStmt); |
| 2195 | } |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 2196 | rc = sqlite3_finalize(pStmt); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2197 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 2198 | if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2199 | return TCL_OK; |
| 2200 | } |
| 2201 | |
| 2202 | /* |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 2203 | ** Usage: sqlite3_stmt_status STMT CODE RESETFLAG |
| 2204 | ** |
| 2205 | ** Get the value of a status counter from a statement. |
| 2206 | */ |
| 2207 | static int test_stmt_status( |
| 2208 | void * clientData, |
| 2209 | Tcl_Interp *interp, |
| 2210 | int objc, |
| 2211 | Tcl_Obj *CONST objv[] |
| 2212 | ){ |
| 2213 | int iValue; |
| 2214 | int i, op, resetFlag; |
| 2215 | const char *zOpName; |
| 2216 | sqlite3_stmt *pStmt; |
| 2217 | |
| 2218 | static const struct { |
| 2219 | const char *zName; |
| 2220 | int op; |
| 2221 | } aOp[] = { |
| 2222 | { "SQLITE_STMTSTATUS_FULLSCAN_STEP", SQLITE_STMTSTATUS_FULLSCAN_STEP }, |
| 2223 | { "SQLITE_STMTSTATUS_SORT", SQLITE_STMTSTATUS_SORT }, |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 2224 | { "SQLITE_STMTSTATUS_AUTOINDEX", SQLITE_STMTSTATUS_AUTOINDEX }, |
drh | bf159fa | 2013-06-25 22:01:22 +0000 | [diff] [blame] | 2225 | { "SQLITE_STMTSTATUS_VM_STEP", SQLITE_STMTSTATUS_VM_STEP }, |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 2226 | }; |
| 2227 | if( objc!=4 ){ |
| 2228 | Tcl_WrongNumArgs(interp, 1, objv, "STMT PARAMETER RESETFLAG"); |
| 2229 | return TCL_ERROR; |
| 2230 | } |
| 2231 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2232 | zOpName = Tcl_GetString(objv[2]); |
| 2233 | for(i=0; i<ArraySize(aOp); i++){ |
| 2234 | if( strcmp(aOp[i].zName, zOpName)==0 ){ |
| 2235 | op = aOp[i].op; |
| 2236 | break; |
| 2237 | } |
| 2238 | } |
| 2239 | if( i>=ArraySize(aOp) ){ |
| 2240 | if( Tcl_GetIntFromObj(interp, objv[2], &op) ) return TCL_ERROR; |
| 2241 | } |
| 2242 | if( Tcl_GetBooleanFromObj(interp, objv[3], &resetFlag) ) return TCL_ERROR; |
| 2243 | iValue = sqlite3_stmt_status(pStmt, op, resetFlag); |
| 2244 | Tcl_SetObjResult(interp, Tcl_NewIntObj(iValue)); |
| 2245 | return TCL_OK; |
| 2246 | } |
| 2247 | |
| 2248 | /* |
drh | bb5a9c3 | 2008-06-19 02:52:25 +0000 | [diff] [blame] | 2249 | ** Usage: sqlite3_next_stmt DB STMT |
| 2250 | ** |
| 2251 | ** Return the next statment in sequence after STMT. |
| 2252 | */ |
| 2253 | static int test_next_stmt( |
| 2254 | void * clientData, |
| 2255 | Tcl_Interp *interp, |
| 2256 | int objc, |
| 2257 | Tcl_Obj *CONST objv[] |
| 2258 | ){ |
| 2259 | sqlite3_stmt *pStmt; |
| 2260 | sqlite3 *db = 0; |
| 2261 | char zBuf[50]; |
| 2262 | |
| 2263 | if( objc!=3 ){ |
| 2264 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2265 | Tcl_GetStringFromObj(objv[0], 0), " DB STMT", 0); |
| 2266 | return TCL_ERROR; |
| 2267 | } |
| 2268 | |
| 2269 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 2270 | if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt) ) return TCL_ERROR; |
| 2271 | pStmt = sqlite3_next_stmt(db, pStmt); |
| 2272 | if( pStmt ){ |
| 2273 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
| 2274 | Tcl_AppendResult(interp, zBuf, 0); |
| 2275 | } |
| 2276 | return TCL_OK; |
| 2277 | } |
| 2278 | |
drh | f03d9cc | 2010-11-16 23:10:25 +0000 | [diff] [blame] | 2279 | /* |
| 2280 | ** Usage: sqlite3_stmt_readonly STMT |
| 2281 | ** |
| 2282 | ** Return true if STMT is a NULL pointer or a pointer to a statement |
| 2283 | ** that is guaranteed to leave the database unmodified. |
| 2284 | */ |
| 2285 | static int test_stmt_readonly( |
| 2286 | void * clientData, |
| 2287 | Tcl_Interp *interp, |
| 2288 | int objc, |
| 2289 | Tcl_Obj *CONST objv[] |
| 2290 | ){ |
| 2291 | sqlite3_stmt *pStmt; |
| 2292 | int rc; |
| 2293 | |
| 2294 | if( objc!=2 ){ |
| 2295 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2296 | Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); |
| 2297 | return TCL_ERROR; |
| 2298 | } |
| 2299 | |
| 2300 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2301 | rc = sqlite3_stmt_readonly(pStmt); |
| 2302 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc)); |
| 2303 | return TCL_OK; |
| 2304 | } |
| 2305 | |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 2306 | /* |
drh | 2fb6693 | 2011-11-25 17:21:47 +0000 | [diff] [blame] | 2307 | ** Usage: sqlite3_stmt_busy STMT |
| 2308 | ** |
| 2309 | ** Return true if STMT is a non-NULL pointer to a statement |
| 2310 | ** that has been stepped but not to completion. |
| 2311 | */ |
| 2312 | static int test_stmt_busy( |
| 2313 | void * clientData, |
| 2314 | Tcl_Interp *interp, |
| 2315 | int objc, |
| 2316 | Tcl_Obj *CONST objv[] |
| 2317 | ){ |
| 2318 | sqlite3_stmt *pStmt; |
| 2319 | int rc; |
| 2320 | |
| 2321 | if( objc!=2 ){ |
| 2322 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2323 | Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); |
| 2324 | return TCL_ERROR; |
| 2325 | } |
| 2326 | |
| 2327 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2328 | rc = sqlite3_stmt_busy(pStmt); |
| 2329 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc)); |
| 2330 | return TCL_OK; |
| 2331 | } |
| 2332 | |
| 2333 | /* |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 2334 | ** Usage: uses_stmt_journal STMT |
| 2335 | ** |
| 2336 | ** Return true if STMT uses a statement journal. |
| 2337 | */ |
| 2338 | static int uses_stmt_journal( |
| 2339 | void * clientData, |
| 2340 | Tcl_Interp *interp, |
| 2341 | int objc, |
| 2342 | Tcl_Obj *CONST objv[] |
| 2343 | ){ |
| 2344 | sqlite3_stmt *pStmt; |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 2345 | |
| 2346 | if( objc!=2 ){ |
| 2347 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2348 | Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); |
| 2349 | return TCL_ERROR; |
| 2350 | } |
| 2351 | |
| 2352 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
drh | caffb1a | 2012-01-30 18:00:31 +0000 | [diff] [blame] | 2353 | sqlite3_stmt_readonly(pStmt); |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 2354 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(((Vdbe *)pStmt)->usesStmtJournal)); |
| 2355 | return TCL_OK; |
| 2356 | } |
| 2357 | |
drh | bb5a9c3 | 2008-06-19 02:52:25 +0000 | [diff] [blame] | 2358 | |
| 2359 | /* |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2360 | ** Usage: sqlite3_reset STMT |
| 2361 | ** |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 2362 | ** Reset a statement handle. |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2363 | */ |
| 2364 | static int test_reset( |
| 2365 | void * clientData, |
| 2366 | Tcl_Interp *interp, |
| 2367 | int objc, |
| 2368 | Tcl_Obj *CONST objv[] |
| 2369 | ){ |
| 2370 | sqlite3_stmt *pStmt; |
| 2371 | int rc; |
| 2372 | |
| 2373 | if( objc!=2 ){ |
| 2374 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2375 | Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); |
| 2376 | return TCL_ERROR; |
| 2377 | } |
| 2378 | |
| 2379 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2380 | |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 2381 | rc = sqlite3_reset(pStmt); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 2382 | if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){ |
| 2383 | return TCL_ERROR; |
| 2384 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2385 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 2386 | /* |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2387 | if( rc ){ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2388 | return TCL_ERROR; |
| 2389 | } |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 2390 | */ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 2391 | return TCL_OK; |
| 2392 | } |
| 2393 | |
drh | 5a38705 | 2003-01-11 14:19:51 +0000 | [diff] [blame] | 2394 | /* |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 2395 | ** Usage: sqlite3_expired STMT |
| 2396 | ** |
| 2397 | ** Return TRUE if a recompilation of the statement is recommended. |
| 2398 | */ |
| 2399 | static int test_expired( |
| 2400 | void * clientData, |
| 2401 | Tcl_Interp *interp, |
| 2402 | int objc, |
| 2403 | Tcl_Obj *CONST objv[] |
| 2404 | ){ |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 2405 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 2406 | sqlite3_stmt *pStmt; |
| 2407 | if( objc!=2 ){ |
| 2408 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2409 | Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); |
| 2410 | return TCL_ERROR; |
| 2411 | } |
| 2412 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2413 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt))); |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 2414 | #endif |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 2415 | return TCL_OK; |
| 2416 | } |
| 2417 | |
| 2418 | /* |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 2419 | ** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT |
| 2420 | ** |
| 2421 | ** Transfer all bindings from FROMSTMT over to TOSTMT |
| 2422 | */ |
| 2423 | static int test_transfer_bind( |
| 2424 | void * clientData, |
| 2425 | Tcl_Interp *interp, |
| 2426 | int objc, |
| 2427 | Tcl_Obj *CONST objv[] |
| 2428 | ){ |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 2429 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 2430 | sqlite3_stmt *pStmt1, *pStmt2; |
| 2431 | if( objc!=3 ){ |
| 2432 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2433 | Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0); |
| 2434 | return TCL_ERROR; |
| 2435 | } |
| 2436 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR; |
| 2437 | if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR; |
| 2438 | Tcl_SetObjResult(interp, |
| 2439 | Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2))); |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 2440 | #endif |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 2441 | return TCL_OK; |
| 2442 | } |
| 2443 | |
| 2444 | /* |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2445 | ** Usage: sqlite3_changes DB |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2446 | ** |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2447 | ** Return the number of changes made to the database by the last SQL |
| 2448 | ** execution. |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2449 | */ |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2450 | static int test_changes( |
| 2451 | void * clientData, |
| 2452 | Tcl_Interp *interp, |
| 2453 | int objc, |
| 2454 | Tcl_Obj *CONST objv[] |
| 2455 | ){ |
| 2456 | sqlite3 *db; |
| 2457 | if( objc!=2 ){ |
| 2458 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2459 | Tcl_GetString(objv[0]), " DB", 0); |
| 2460 | return TCL_ERROR; |
| 2461 | } |
| 2462 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 2463 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db))); |
| 2464 | return TCL_OK; |
| 2465 | } |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2466 | |
| 2467 | /* |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2468 | ** This is the "static_bind_value" that variables are bound to when |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2469 | ** the FLAG option of sqlite3_bind is "static" |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2470 | */ |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2471 | static char *sqlite_static_bind_value = 0; |
drh | f031381 | 2006-09-04 15:53:53 +0000 | [diff] [blame] | 2472 | static int sqlite_static_bind_nbyte = 0; |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2473 | |
| 2474 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2475 | ** Usage: sqlite3_bind VM IDX VALUE FLAGS |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2476 | ** |
drh | f7b5496 | 2013-05-28 12:11:54 +0000 | [diff] [blame] | 2477 | ** Sets the value of the IDX-th occurrence of "?" in the original SQL |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2478 | ** string. VALUE is the new value. If FLAGS=="null" then VALUE is |
| 2479 | ** ignored and the value is set to NULL. If FLAGS=="static" then |
| 2480 | ** the value is set to the value of a static variable named |
| 2481 | ** "sqlite_static_bind_value". If FLAGS=="normal" then a copy |
drh | bf8aa2a | 2005-12-02 02:44:05 +0000 | [diff] [blame] | 2482 | ** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored |
| 2483 | ** an a 10-byte blob "abc\000xyz\000pq" is inserted. |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2484 | */ |
| 2485 | static int test_bind( |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2486 | void *NotUsed, |
| 2487 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 2488 | int argc, /* Number of arguments */ |
| 2489 | char **argv /* Text of each argument */ |
| 2490 | ){ |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 2491 | sqlite3_stmt *pStmt; |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2492 | int rc; |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2493 | int idx; |
| 2494 | if( argc!=5 ){ |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2495 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2496 | " VM IDX VALUE (null|static|normal)\"", 0); |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2497 | return TCL_ERROR; |
| 2498 | } |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 2499 | if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR; |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2500 | if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR; |
| 2501 | if( strcmp(argv[4],"null")==0 ){ |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 2502 | rc = sqlite3_bind_null(pStmt, idx); |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2503 | }else if( strcmp(argv[4],"static")==0 ){ |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 2504 | rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0); |
drh | f031381 | 2006-09-04 15:53:53 +0000 | [diff] [blame] | 2505 | }else if( strcmp(argv[4],"static-nbytes")==0 ){ |
| 2506 | rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, |
| 2507 | sqlite_static_bind_nbyte, 0); |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2508 | }else if( strcmp(argv[4],"normal")==0 ){ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2509 | rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT); |
drh | bf8aa2a | 2005-12-02 02:44:05 +0000 | [diff] [blame] | 2510 | }else if( strcmp(argv[4],"blob10")==0 ){ |
| 2511 | rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC); |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 2512 | }else{ |
| 2513 | Tcl_AppendResult(interp, "4th argument should be " |
| 2514 | "\"null\" or \"static\" or \"normal\"", 0); |
| 2515 | return TCL_ERROR; |
| 2516 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2517 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2518 | if( rc ){ |
| 2519 | char zBuf[50]; |
| 2520 | sprintf(zBuf, "(%d) ", rc); |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 2521 | Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0); |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2522 | return TCL_ERROR; |
| 2523 | } |
| 2524 | return TCL_OK; |
| 2525 | } |
| 2526 | |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 2527 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 2528 | /* |
| 2529 | ** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be> |
| 2530 | ** |
| 2531 | ** This function is used to test that SQLite selects the correct collation |
| 2532 | ** sequence callback when multiple versions (for different text encodings) |
| 2533 | ** are available. |
| 2534 | ** |
| 2535 | ** Calling this routine registers the collation sequence "test_collate" |
| 2536 | ** with database handle <db>. The second argument must be a list of three |
| 2537 | ** boolean values. If the first is true, then a version of test_collate is |
| 2538 | ** registered for UTF-8, if the second is true, a version is registered for |
| 2539 | ** UTF-16le, if the third is true, a UTF-16be version is available. |
| 2540 | ** Previous versions of test_collate are deleted. |
| 2541 | ** |
| 2542 | ** The collation sequence test_collate is implemented by calling the |
| 2543 | ** following TCL script: |
| 2544 | ** |
| 2545 | ** "test_collate <enc> <lhs> <rhs>" |
| 2546 | ** |
| 2547 | ** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8. |
| 2548 | ** The <enc> parameter is the encoding of the collation function that |
| 2549 | ** SQLite selected to call. The TCL test script implements the |
| 2550 | ** "test_collate" proc. |
| 2551 | ** |
| 2552 | ** Note that this will only work with one intepreter at a time, as the |
| 2553 | ** interp pointer to use when evaluating the TCL script is stored in |
| 2554 | ** pTestCollateInterp. |
| 2555 | */ |
| 2556 | static Tcl_Interp* pTestCollateInterp; |
| 2557 | static int test_collate_func( |
| 2558 | void *pCtx, |
| 2559 | int nA, const void *zA, |
| 2560 | int nB, const void *zB |
| 2561 | ){ |
| 2562 | Tcl_Interp *i = pTestCollateInterp; |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 2563 | int encin = SQLITE_PTR_TO_INT(pCtx); |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 2564 | int res; |
drh | 4db38a7 | 2005-09-01 12:16:28 +0000 | [diff] [blame] | 2565 | int n; |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 2566 | |
| 2567 | sqlite3_value *pVal; |
| 2568 | Tcl_Obj *pX; |
| 2569 | |
| 2570 | pX = Tcl_NewStringObj("test_collate", -1); |
| 2571 | Tcl_IncrRefCount(pX); |
| 2572 | |
| 2573 | switch( encin ){ |
| 2574 | case SQLITE_UTF8: |
| 2575 | Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1)); |
| 2576 | break; |
| 2577 | case SQLITE_UTF16LE: |
| 2578 | Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1)); |
| 2579 | break; |
| 2580 | case SQLITE_UTF16BE: |
| 2581 | Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1)); |
| 2582 | break; |
| 2583 | default: |
| 2584 | assert(0); |
| 2585 | } |
| 2586 | |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2587 | sqlite3BeginBenignMalloc(); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2588 | pVal = sqlite3ValueNew(0); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2589 | if( pVal ){ |
| 2590 | sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC); |
| 2591 | n = sqlite3_value_bytes(pVal); |
| 2592 | Tcl_ListObjAppendElement(i,pX, |
| 2593 | Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n)); |
| 2594 | sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC); |
| 2595 | n = sqlite3_value_bytes(pVal); |
| 2596 | Tcl_ListObjAppendElement(i,pX, |
| 2597 | Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n)); |
| 2598 | sqlite3ValueFree(pVal); |
| 2599 | } |
| 2600 | sqlite3EndBenignMalloc(); |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 2601 | |
| 2602 | Tcl_EvalObjEx(i, pX, 0); |
| 2603 | Tcl_DecrRefCount(pX); |
| 2604 | Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res); |
| 2605 | return res; |
| 2606 | } |
| 2607 | static int test_collate( |
| 2608 | void * clientData, |
| 2609 | Tcl_Interp *interp, |
| 2610 | int objc, |
| 2611 | Tcl_Obj *CONST objv[] |
| 2612 | ){ |
| 2613 | sqlite3 *db; |
| 2614 | int val; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2615 | sqlite3_value *pVal; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2616 | int rc; |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 2617 | |
| 2618 | if( objc!=5 ) goto bad_args; |
| 2619 | pTestCollateInterp = interp; |
| 2620 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 2621 | |
| 2622 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2623 | rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8, |
| 2624 | (void *)SQLITE_UTF8, val?test_collate_func:0); |
| 2625 | if( rc==SQLITE_OK ){ |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 2626 | const void *zUtf16; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2627 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR; |
| 2628 | rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE, |
| 2629 | (void *)SQLITE_UTF16LE, val?test_collate_func:0); |
| 2630 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2631 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 2632 | #if 0 |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 2633 | if( sqlite3_iMallocFail>0 ){ |
| 2634 | sqlite3_iMallocFail++; |
| 2635 | } |
| 2636 | #endif |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 2637 | sqlite3_mutex_enter(db->mutex); |
| 2638 | pVal = sqlite3ValueNew(db); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2639 | sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2640 | zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 2641 | if( db->mallocFailed ){ |
| 2642 | rc = SQLITE_NOMEM; |
| 2643 | }else{ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 2644 | rc = sqlite3_create_collation16(db, zUtf16, SQLITE_UTF16BE, |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 2645 | (void *)SQLITE_UTF16BE, val?test_collate_func:0); |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 2646 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2647 | sqlite3ValueFree(pVal); |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 2648 | sqlite3_mutex_leave(db->mutex); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2649 | } |
| 2650 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 2651 | |
| 2652 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 2653 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 2654 | return TCL_ERROR; |
| 2655 | } |
danielk1977 | 4e6af13 | 2004-06-10 14:01:08 +0000 | [diff] [blame] | 2656 | return TCL_OK; |
| 2657 | |
| 2658 | bad_args: |
| 2659 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2660 | Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0); |
| 2661 | return TCL_ERROR; |
| 2662 | } |
| 2663 | |
drh | 268803a | 2005-12-14 20:11:30 +0000 | [diff] [blame] | 2664 | /* |
| 2665 | ** When the collation needed callback is invoked, record the name of |
| 2666 | ** the requested collating function here. The recorded name is linked |
| 2667 | ** to a TCL variable and used to make sure that the requested collation |
| 2668 | ** name is correct. |
| 2669 | */ |
| 2670 | static char zNeededCollation[200]; |
| 2671 | static char *pzNeededCollation = zNeededCollation; |
| 2672 | |
| 2673 | |
| 2674 | /* |
| 2675 | ** Called when a collating sequence is needed. Registered using |
| 2676 | ** sqlite3_collation_needed16(). |
| 2677 | */ |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2678 | static void test_collate_needed_cb( |
| 2679 | void *pCtx, |
| 2680 | sqlite3 *db, |
| 2681 | int eTextRep, |
drh | 268803a | 2005-12-14 20:11:30 +0000 | [diff] [blame] | 2682 | const void *pName |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2683 | ){ |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 2684 | int enc = ENC(db); |
drh | 268803a | 2005-12-14 20:11:30 +0000 | [diff] [blame] | 2685 | int i; |
| 2686 | char *z; |
| 2687 | for(z = (char*)pName, i=0; *z || z[1]; z++){ |
| 2688 | if( *z ) zNeededCollation[i++] = *z; |
| 2689 | } |
| 2690 | zNeededCollation[i] = 0; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2691 | sqlite3_create_collation( |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 2692 | db, "test_collate", ENC(db), SQLITE_INT_TO_PTR(enc), test_collate_func); |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | /* |
| 2696 | ** Usage: add_test_collate_needed DB |
| 2697 | */ |
| 2698 | static int test_collate_needed( |
| 2699 | void * clientData, |
| 2700 | Tcl_Interp *interp, |
| 2701 | int objc, |
| 2702 | Tcl_Obj *CONST objv[] |
| 2703 | ){ |
| 2704 | sqlite3 *db; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2705 | int rc; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2706 | |
| 2707 | if( objc!=2 ) goto bad_args; |
| 2708 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2709 | rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb); |
drh | 268803a | 2005-12-14 20:11:30 +0000 | [diff] [blame] | 2710 | zNeededCollation[0] = 0; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 2711 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2712 | return TCL_OK; |
| 2713 | |
| 2714 | bad_args: |
| 2715 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 2716 | return TCL_ERROR; |
| 2717 | } |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 2718 | |
| 2719 | /* |
| 2720 | ** tclcmd: add_alignment_test_collations DB |
| 2721 | ** |
| 2722 | ** Add two new collating sequences to the database DB |
| 2723 | ** |
| 2724 | ** utf16_aligned |
| 2725 | ** utf16_unaligned |
| 2726 | ** |
| 2727 | ** Both collating sequences use the same sort order as BINARY. |
| 2728 | ** The only difference is that the utf16_aligned collating |
| 2729 | ** sequence is declared with the SQLITE_UTF16_ALIGNED flag. |
| 2730 | ** Both collating functions increment the unaligned utf16 counter |
| 2731 | ** whenever they see a string that begins on an odd byte boundary. |
| 2732 | */ |
| 2733 | static int unaligned_string_counter = 0; |
| 2734 | static int alignmentCollFunc( |
| 2735 | void *NotUsed, |
| 2736 | int nKey1, const void *pKey1, |
| 2737 | int nKey2, const void *pKey2 |
| 2738 | ){ |
| 2739 | int rc, n; |
| 2740 | n = nKey1<nKey2 ? nKey1 : nKey2; |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 2741 | if( nKey1>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey1))) ) unaligned_string_counter++; |
| 2742 | if( nKey2>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey2))) ) unaligned_string_counter++; |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 2743 | rc = memcmp(pKey1, pKey2, n); |
| 2744 | if( rc==0 ){ |
| 2745 | rc = nKey1 - nKey2; |
| 2746 | } |
| 2747 | return rc; |
| 2748 | } |
| 2749 | static int add_alignment_test_collations( |
| 2750 | void * clientData, |
| 2751 | Tcl_Interp *interp, |
| 2752 | int objc, |
| 2753 | Tcl_Obj *CONST objv[] |
| 2754 | ){ |
| 2755 | sqlite3 *db; |
| 2756 | if( objc>=2 ){ |
| 2757 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
danielk1977 | ebb3293 | 2009-04-28 15:35:38 +0000 | [diff] [blame] | 2758 | sqlite3_create_collation(db, "utf16_unaligned", SQLITE_UTF16, |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 2759 | 0, alignmentCollFunc); |
danielk1977 | ebb3293 | 2009-04-28 15:35:38 +0000 | [diff] [blame] | 2760 | sqlite3_create_collation(db, "utf16_aligned", SQLITE_UTF16_ALIGNED, |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 2761 | 0, alignmentCollFunc); |
| 2762 | } |
| 2763 | return SQLITE_OK; |
| 2764 | } |
| 2765 | #endif /* !defined(SQLITE_OMIT_UTF16) */ |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2766 | |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2767 | /* |
| 2768 | ** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be> |
| 2769 | ** |
| 2770 | ** This function is used to test that SQLite selects the correct user |
| 2771 | ** function callback when multiple versions (for different text encodings) |
| 2772 | ** are available. |
| 2773 | ** |
| 2774 | ** Calling this routine registers up to three versions of the user function |
| 2775 | ** "test_function" with database handle <db>. If the second argument is |
| 2776 | ** true, then a version of test_function is registered for UTF-8, if the |
| 2777 | ** third is true, a version is registered for UTF-16le, if the fourth is |
| 2778 | ** true, a UTF-16be version is available. Previous versions of |
| 2779 | ** test_function are deleted. |
| 2780 | ** |
| 2781 | ** The user function is implemented by calling the following TCL script: |
| 2782 | ** |
| 2783 | ** "test_function <enc> <arg>" |
| 2784 | ** |
| 2785 | ** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the |
| 2786 | ** single argument passed to the SQL function. The value returned by |
| 2787 | ** the TCL script is used as the return value of the SQL function. It |
| 2788 | ** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8 |
| 2789 | ** for a UTF-16LE test_function(), and UTF-16LE for an implementation that |
| 2790 | ** prefers UTF-16BE. |
| 2791 | */ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 2792 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2793 | static void test_function_utf8( |
| 2794 | sqlite3_context *pCtx, |
| 2795 | int nArg, |
| 2796 | sqlite3_value **argv |
| 2797 | ){ |
| 2798 | Tcl_Interp *interp; |
| 2799 | Tcl_Obj *pX; |
| 2800 | sqlite3_value *pVal; |
| 2801 | interp = (Tcl_Interp *)sqlite3_user_data(pCtx); |
| 2802 | pX = Tcl_NewStringObj("test_function", -1); |
| 2803 | Tcl_IncrRefCount(pX); |
| 2804 | Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1)); |
| 2805 | Tcl_ListObjAppendElement(interp, pX, |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 2806 | Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2807 | Tcl_EvalObjEx(interp, pX, 0); |
| 2808 | Tcl_DecrRefCount(pX); |
| 2809 | sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2810 | pVal = sqlite3ValueNew(0); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2811 | sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2812 | SQLITE_UTF8, SQLITE_STATIC); |
| 2813 | sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal), |
| 2814 | -1, SQLITE_TRANSIENT); |
| 2815 | sqlite3ValueFree(pVal); |
| 2816 | } |
| 2817 | static void test_function_utf16le( |
| 2818 | sqlite3_context *pCtx, |
| 2819 | int nArg, |
| 2820 | sqlite3_value **argv |
| 2821 | ){ |
| 2822 | Tcl_Interp *interp; |
| 2823 | Tcl_Obj *pX; |
| 2824 | sqlite3_value *pVal; |
| 2825 | interp = (Tcl_Interp *)sqlite3_user_data(pCtx); |
| 2826 | pX = Tcl_NewStringObj("test_function", -1); |
| 2827 | Tcl_IncrRefCount(pX); |
| 2828 | Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1)); |
| 2829 | Tcl_ListObjAppendElement(interp, pX, |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 2830 | Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2831 | Tcl_EvalObjEx(interp, pX, 0); |
| 2832 | Tcl_DecrRefCount(pX); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2833 | pVal = sqlite3ValueNew(0); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2834 | sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2835 | SQLITE_UTF8, SQLITE_STATIC); |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 2836 | sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT); |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2837 | sqlite3ValueFree(pVal); |
| 2838 | } |
| 2839 | static void test_function_utf16be( |
| 2840 | sqlite3_context *pCtx, |
| 2841 | int nArg, |
| 2842 | sqlite3_value **argv |
| 2843 | ){ |
| 2844 | Tcl_Interp *interp; |
| 2845 | Tcl_Obj *pX; |
| 2846 | sqlite3_value *pVal; |
| 2847 | interp = (Tcl_Interp *)sqlite3_user_data(pCtx); |
| 2848 | pX = Tcl_NewStringObj("test_function", -1); |
| 2849 | Tcl_IncrRefCount(pX); |
| 2850 | Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1)); |
| 2851 | Tcl_ListObjAppendElement(interp, pX, |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 2852 | Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2853 | Tcl_EvalObjEx(interp, pX, 0); |
| 2854 | Tcl_DecrRefCount(pX); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2855 | pVal = sqlite3ValueNew(0); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2856 | sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2857 | SQLITE_UTF8, SQLITE_STATIC); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 2858 | sqlite3_result_text16(pCtx, sqlite3_value_text16le(pVal), |
| 2859 | -1, SQLITE_TRANSIENT); |
| 2860 | sqlite3_result_text16be(pCtx, sqlite3_value_text16le(pVal), |
| 2861 | -1, SQLITE_TRANSIENT); |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2862 | sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal), |
| 2863 | -1, SQLITE_TRANSIENT); |
| 2864 | sqlite3ValueFree(pVal); |
| 2865 | } |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 2866 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2867 | static int test_function( |
| 2868 | void * clientData, |
| 2869 | Tcl_Interp *interp, |
| 2870 | int objc, |
| 2871 | Tcl_Obj *CONST objv[] |
| 2872 | ){ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 2873 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2874 | sqlite3 *db; |
| 2875 | int val; |
| 2876 | |
| 2877 | if( objc!=5 ) goto bad_args; |
| 2878 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 2879 | |
| 2880 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; |
| 2881 | if( val ){ |
| 2882 | sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8, |
| 2883 | interp, test_function_utf8, 0, 0); |
| 2884 | } |
| 2885 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR; |
| 2886 | if( val ){ |
| 2887 | sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE, |
| 2888 | interp, test_function_utf16le, 0, 0); |
| 2889 | } |
| 2890 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR; |
| 2891 | if( val ){ |
| 2892 | sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE, |
| 2893 | interp, test_function_utf16be, 0, 0); |
| 2894 | } |
| 2895 | |
| 2896 | return TCL_OK; |
| 2897 | bad_args: |
| 2898 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2899 | Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0); |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 2900 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | c8e9a2d | 2004-06-25 12:08:46 +0000 | [diff] [blame] | 2901 | return TCL_ERROR; |
| 2902 | } |
| 2903 | |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2904 | /* |
dan | ba3cbf3 | 2010-06-30 04:29:03 +0000 | [diff] [blame] | 2905 | ** Usage: sqlite3_test_errstr <err code> |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2906 | ** |
| 2907 | ** Test that the english language string equivalents for sqlite error codes |
| 2908 | ** are sane. The parameter is an integer representing an sqlite error code. |
| 2909 | ** The result is a list of two elements, the string representation of the |
| 2910 | ** error code and the english language explanation. |
| 2911 | */ |
| 2912 | static int test_errstr( |
| 2913 | void * clientData, |
| 2914 | Tcl_Interp *interp, |
| 2915 | int objc, |
| 2916 | Tcl_Obj *CONST objv[] |
| 2917 | ){ |
| 2918 | char *zCode; |
| 2919 | int i; |
| 2920 | if( objc!=1 ){ |
| 2921 | Tcl_WrongNumArgs(interp, 1, objv, "<error code>"); |
| 2922 | } |
| 2923 | |
| 2924 | zCode = Tcl_GetString(objv[1]); |
| 2925 | for(i=0; i<200; i++){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2926 | if( 0==strcmp(t1ErrorName(i), zCode) ) break; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 2927 | } |
| 2928 | Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0); |
| 2929 | return TCL_OK; |
| 2930 | } |
| 2931 | |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 2932 | /* |
drh | 99ee360 | 2003-02-16 19:13:36 +0000 | [diff] [blame] | 2933 | ** Usage: breakpoint |
| 2934 | ** |
| 2935 | ** This routine exists for one purpose - to provide a place to put a |
| 2936 | ** breakpoint with GDB that can be triggered using TCL code. The use |
| 2937 | ** for this is when a particular test fails on (say) the 1485th iteration. |
| 2938 | ** In the TCL test script, we can add code like this: |
| 2939 | ** |
| 2940 | ** if {$i==1485} breakpoint |
| 2941 | ** |
| 2942 | ** Then run testfixture in the debugger and wait for the breakpoint to |
| 2943 | ** fire. Then additional breakpoints can be set to trace down the bug. |
| 2944 | */ |
| 2945 | static int test_breakpoint( |
| 2946 | void *NotUsed, |
| 2947 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 2948 | int argc, /* Number of arguments */ |
| 2949 | char **argv /* Text of each argument */ |
| 2950 | ){ |
| 2951 | return TCL_OK; /* Do nothing */ |
| 2952 | } |
| 2953 | |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 2954 | /* |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 2955 | ** Usage: sqlite3_bind_zeroblob STMT IDX N |
| 2956 | ** |
| 2957 | ** Test the sqlite3_bind_zeroblob interface. STMT is a prepared statement. |
| 2958 | ** IDX is the index of a wildcard in the prepared statement. This command |
| 2959 | ** binds a N-byte zero-filled BLOB to the wildcard. |
| 2960 | */ |
| 2961 | static int test_bind_zeroblob( |
| 2962 | void * clientData, |
| 2963 | Tcl_Interp *interp, |
| 2964 | int objc, |
| 2965 | Tcl_Obj *CONST objv[] |
| 2966 | ){ |
| 2967 | sqlite3_stmt *pStmt; |
| 2968 | int idx; |
| 2969 | int n; |
| 2970 | int rc; |
| 2971 | |
| 2972 | if( objc!=4 ){ |
danielk1977 | 28c6630 | 2007-09-01 11:04:26 +0000 | [diff] [blame] | 2973 | Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N"); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 2974 | return TCL_ERROR; |
| 2975 | } |
| 2976 | |
| 2977 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2978 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 2979 | if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR; |
| 2980 | |
| 2981 | rc = sqlite3_bind_zeroblob(pStmt, idx, n); |
| 2982 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
| 2983 | if( rc!=SQLITE_OK ){ |
| 2984 | return TCL_ERROR; |
| 2985 | } |
| 2986 | |
| 2987 | return TCL_OK; |
| 2988 | } |
| 2989 | |
| 2990 | /* |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 2991 | ** Usage: sqlite3_bind_int STMT N VALUE |
| 2992 | ** |
| 2993 | ** Test the sqlite3_bind_int interface. STMT is a prepared statement. |
| 2994 | ** N is the index of a wildcard in the prepared statement. This command |
| 2995 | ** binds a 32-bit integer VALUE to that wildcard. |
| 2996 | */ |
| 2997 | static int test_bind_int( |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 2998 | void * clientData, |
| 2999 | Tcl_Interp *interp, |
| 3000 | int objc, |
| 3001 | Tcl_Obj *CONST objv[] |
| 3002 | ){ |
| 3003 | sqlite3_stmt *pStmt; |
| 3004 | int idx; |
| 3005 | int value; |
| 3006 | int rc; |
| 3007 | |
| 3008 | if( objc!=4 ){ |
| 3009 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3010 | Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3011 | return TCL_ERROR; |
| 3012 | } |
| 3013 | |
| 3014 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3015 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 3016 | if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR; |
| 3017 | |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 3018 | rc = sqlite3_bind_int(pStmt, idx, value); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3019 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3020 | if( rc!=SQLITE_OK ){ |
| 3021 | return TCL_ERROR; |
| 3022 | } |
| 3023 | |
| 3024 | return TCL_OK; |
| 3025 | } |
| 3026 | |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3027 | |
| 3028 | /* |
| 3029 | ** Usage: sqlite3_bind_int64 STMT N VALUE |
| 3030 | ** |
| 3031 | ** Test the sqlite3_bind_int64 interface. STMT is a prepared statement. |
| 3032 | ** N is the index of a wildcard in the prepared statement. This command |
| 3033 | ** binds a 64-bit integer VALUE to that wildcard. |
| 3034 | */ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3035 | static int test_bind_int64( |
| 3036 | void * clientData, |
| 3037 | Tcl_Interp *interp, |
| 3038 | int objc, |
| 3039 | Tcl_Obj *CONST objv[] |
| 3040 | ){ |
| 3041 | sqlite3_stmt *pStmt; |
| 3042 | int idx; |
drh | b3f787f | 2012-09-29 14:45:54 +0000 | [diff] [blame] | 3043 | Tcl_WideInt value; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3044 | int rc; |
| 3045 | |
| 3046 | if( objc!=4 ){ |
| 3047 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3048 | Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3049 | return TCL_ERROR; |
| 3050 | } |
| 3051 | |
| 3052 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3053 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 3054 | if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR; |
| 3055 | |
| 3056 | rc = sqlite3_bind_int64(pStmt, idx, value); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3057 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3058 | if( rc!=SQLITE_OK ){ |
| 3059 | return TCL_ERROR; |
| 3060 | } |
| 3061 | |
| 3062 | return TCL_OK; |
| 3063 | } |
| 3064 | |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3065 | |
| 3066 | /* |
| 3067 | ** Usage: sqlite3_bind_double STMT N VALUE |
| 3068 | ** |
| 3069 | ** Test the sqlite3_bind_double interface. STMT is a prepared statement. |
| 3070 | ** N is the index of a wildcard in the prepared statement. This command |
| 3071 | ** binds a 64-bit integer VALUE to that wildcard. |
| 3072 | */ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3073 | static int test_bind_double( |
| 3074 | void * clientData, |
| 3075 | Tcl_Interp *interp, |
| 3076 | int objc, |
| 3077 | Tcl_Obj *CONST objv[] |
| 3078 | ){ |
| 3079 | sqlite3_stmt *pStmt; |
| 3080 | int idx; |
| 3081 | double value; |
| 3082 | int rc; |
drh | a06f17f | 2008-05-11 11:07:06 +0000 | [diff] [blame] | 3083 | const char *zVal; |
| 3084 | int i; |
| 3085 | static const struct { |
| 3086 | const char *zName; /* Name of the special floating point value */ |
| 3087 | unsigned int iUpper; /* Upper 32 bits */ |
| 3088 | unsigned int iLower; /* Lower 32 bits */ |
| 3089 | } aSpecialFp[] = { |
| 3090 | { "NaN", 0x7fffffff, 0xffffffff }, |
| 3091 | { "SNaN", 0x7ff7ffff, 0xffffffff }, |
| 3092 | { "-NaN", 0xffffffff, 0xffffffff }, |
| 3093 | { "-SNaN", 0xfff7ffff, 0xffffffff }, |
| 3094 | { "+Inf", 0x7ff00000, 0x00000000 }, |
| 3095 | { "-Inf", 0xfff00000, 0x00000000 }, |
| 3096 | { "Epsilon", 0x00000000, 0x00000001 }, |
| 3097 | { "-Epsilon", 0x80000000, 0x00000001 }, |
| 3098 | { "NaN0", 0x7ff80000, 0x00000000 }, |
| 3099 | { "-NaN0", 0xfff80000, 0x00000000 }, |
| 3100 | }; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3101 | |
| 3102 | if( objc!=4 ){ |
| 3103 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3104 | Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3105 | return TCL_ERROR; |
| 3106 | } |
| 3107 | |
| 3108 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3109 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3110 | |
drh | 394f07e | 2008-04-28 15:23:02 +0000 | [diff] [blame] | 3111 | /* Intercept the string "NaN" and generate a NaN value for it. |
| 3112 | ** All other strings are passed through to Tcl_GetDoubleFromObj(). |
| 3113 | ** Tcl_GetDoubleFromObj() should understand "NaN" but some versions |
| 3114 | ** contain a bug. |
| 3115 | */ |
drh | a06f17f | 2008-05-11 11:07:06 +0000 | [diff] [blame] | 3116 | zVal = Tcl_GetString(objv[3]); |
| 3117 | for(i=0; i<sizeof(aSpecialFp)/sizeof(aSpecialFp[0]); i++){ |
| 3118 | if( strcmp(aSpecialFp[i].zName, zVal)==0 ){ |
| 3119 | sqlite3_uint64 x; |
| 3120 | x = aSpecialFp[i].iUpper; |
| 3121 | x <<= 32; |
| 3122 | x |= aSpecialFp[i].iLower; |
drh | 0a66733 | 2008-05-11 17:22:01 +0000 | [diff] [blame] | 3123 | assert( sizeof(value)==8 ); |
| 3124 | assert( sizeof(x)==8 ); |
| 3125 | memcpy(&value, &x, 8); |
drh | a06f17f | 2008-05-11 11:07:06 +0000 | [diff] [blame] | 3126 | break; |
| 3127 | } |
| 3128 | } |
| 3129 | if( i>=sizeof(aSpecialFp)/sizeof(aSpecialFp[0]) && |
| 3130 | Tcl_GetDoubleFromObj(interp, objv[3], &value) ){ |
drh | 394f07e | 2008-04-28 15:23:02 +0000 | [diff] [blame] | 3131 | return TCL_ERROR; |
| 3132 | } |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3133 | rc = sqlite3_bind_double(pStmt, idx, value); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3134 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3135 | if( rc!=SQLITE_OK ){ |
| 3136 | return TCL_ERROR; |
| 3137 | } |
| 3138 | |
| 3139 | return TCL_OK; |
| 3140 | } |
| 3141 | |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3142 | /* |
| 3143 | ** Usage: sqlite3_bind_null STMT N |
| 3144 | ** |
| 3145 | ** Test the sqlite3_bind_null interface. STMT is a prepared statement. |
| 3146 | ** N is the index of a wildcard in the prepared statement. This command |
| 3147 | ** binds a NULL to the wildcard. |
| 3148 | */ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3149 | static int test_bind_null( |
| 3150 | void * clientData, |
| 3151 | Tcl_Interp *interp, |
| 3152 | int objc, |
| 3153 | Tcl_Obj *CONST objv[] |
| 3154 | ){ |
| 3155 | sqlite3_stmt *pStmt; |
| 3156 | int idx; |
| 3157 | int rc; |
| 3158 | |
| 3159 | if( objc!=3 ){ |
| 3160 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3161 | Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3162 | return TCL_ERROR; |
| 3163 | } |
| 3164 | |
| 3165 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3166 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 3167 | |
| 3168 | rc = sqlite3_bind_null(pStmt, idx); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3169 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3170 | if( rc!=SQLITE_OK ){ |
| 3171 | return TCL_ERROR; |
| 3172 | } |
| 3173 | |
| 3174 | return TCL_OK; |
| 3175 | } |
| 3176 | |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3177 | /* |
| 3178 | ** Usage: sqlite3_bind_text STMT N STRING BYTES |
| 3179 | ** |
| 3180 | ** Test the sqlite3_bind_text interface. STMT is a prepared statement. |
| 3181 | ** N is the index of a wildcard in the prepared statement. This command |
| 3182 | ** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes |
| 3183 | ** long. |
| 3184 | */ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3185 | static int test_bind_text( |
| 3186 | void * clientData, |
| 3187 | Tcl_Interp *interp, |
| 3188 | int objc, |
| 3189 | Tcl_Obj *CONST objv[] |
| 3190 | ){ |
| 3191 | sqlite3_stmt *pStmt; |
| 3192 | int idx; |
| 3193 | int bytes; |
| 3194 | char *value; |
| 3195 | int rc; |
| 3196 | |
| 3197 | if( objc!=5 ){ |
| 3198 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3199 | Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3200 | return TCL_ERROR; |
| 3201 | } |
| 3202 | |
| 3203 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3204 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
drh | 10dfbbb | 2008-04-16 12:58:53 +0000 | [diff] [blame] | 3205 | value = (char*)Tcl_GetByteArrayFromObj(objv[3], &bytes); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3206 | if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR; |
| 3207 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 3208 | rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3209 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3210 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 3211 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3212 | return TCL_ERROR; |
| 3213 | } |
| 3214 | |
| 3215 | return TCL_OK; |
| 3216 | } |
| 3217 | |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3218 | /* |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 3219 | ** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3220 | ** |
| 3221 | ** Test the sqlite3_bind_text16 interface. STMT is a prepared statement. |
| 3222 | ** N is the index of a wildcard in the prepared statement. This command |
| 3223 | ** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes |
| 3224 | ** long. |
| 3225 | */ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3226 | static int test_bind_text16( |
| 3227 | void * clientData, |
| 3228 | Tcl_Interp *interp, |
| 3229 | int objc, |
| 3230 | Tcl_Obj *CONST objv[] |
| 3231 | ){ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3232 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3233 | sqlite3_stmt *pStmt; |
| 3234 | int idx; |
| 3235 | int bytes; |
| 3236 | char *value; |
| 3237 | int rc; |
| 3238 | |
drh | 7da5fcb | 2012-03-30 14:59:43 +0000 | [diff] [blame] | 3239 | void (*xDel)(void*) = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT); |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 3240 | Tcl_Obj *oStmt = objv[objc-4]; |
| 3241 | Tcl_Obj *oN = objv[objc-3]; |
| 3242 | Tcl_Obj *oString = objv[objc-2]; |
| 3243 | Tcl_Obj *oBytes = objv[objc-1]; |
| 3244 | |
| 3245 | if( objc!=5 && objc!=6){ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3246 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3247 | Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3248 | return TCL_ERROR; |
| 3249 | } |
| 3250 | |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 3251 | if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR; |
| 3252 | if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR; |
| 3253 | value = (char*)Tcl_GetByteArrayFromObj(oString, 0); |
| 3254 | if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3255 | |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 3256 | rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3257 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3258 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 3259 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3260 | return TCL_ERROR; |
| 3261 | } |
| 3262 | |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3263 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3264 | return TCL_OK; |
| 3265 | } |
| 3266 | |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3267 | /* |
danielk1977 | 5b159dc | 2007-05-17 16:34:43 +0000 | [diff] [blame] | 3268 | ** Usage: sqlite3_bind_blob ?-static? STMT N DATA BYTES |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3269 | ** |
| 3270 | ** Test the sqlite3_bind_blob interface. STMT is a prepared statement. |
| 3271 | ** N is the index of a wildcard in the prepared statement. This command |
| 3272 | ** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size. |
| 3273 | */ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3274 | static int test_bind_blob( |
| 3275 | void * clientData, |
| 3276 | Tcl_Interp *interp, |
| 3277 | int objc, |
| 3278 | Tcl_Obj *CONST objv[] |
| 3279 | ){ |
| 3280 | sqlite3_stmt *pStmt; |
| 3281 | int idx; |
| 3282 | int bytes; |
| 3283 | char *value; |
| 3284 | int rc; |
danielk1977 | 5b159dc | 2007-05-17 16:34:43 +0000 | [diff] [blame] | 3285 | sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3286 | |
danielk1977 | 5b159dc | 2007-05-17 16:34:43 +0000 | [diff] [blame] | 3287 | if( objc!=5 && objc!=6 ){ |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3288 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 3289 | Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3290 | return TCL_ERROR; |
| 3291 | } |
| 3292 | |
danielk1977 | 5b159dc | 2007-05-17 16:34:43 +0000 | [diff] [blame] | 3293 | if( objc==6 ){ |
| 3294 | xDestructor = SQLITE_STATIC; |
| 3295 | objv++; |
| 3296 | } |
| 3297 | |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3298 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3299 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 3300 | value = Tcl_GetString(objv[3]); |
danielk1977 | 49e4643 | 2004-05-27 13:55:27 +0000 | [diff] [blame] | 3301 | if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3302 | |
danielk1977 | 5b159dc | 2007-05-17 16:34:43 +0000 | [diff] [blame] | 3303 | rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3304 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 3305 | if( rc!=SQLITE_OK ){ |
| 3306 | return TCL_ERROR; |
| 3307 | } |
| 3308 | |
| 3309 | return TCL_OK; |
| 3310 | } |
| 3311 | |
drh | 99ee360 | 2003-02-16 19:13:36 +0000 | [diff] [blame] | 3312 | /* |
drh | 75f6a03 | 2004-07-15 14:15:00 +0000 | [diff] [blame] | 3313 | ** Usage: sqlite3_bind_parameter_count STMT |
| 3314 | ** |
| 3315 | ** Return the number of wildcards in the given statement. |
| 3316 | */ |
| 3317 | static int test_bind_parameter_count( |
| 3318 | void * clientData, |
| 3319 | Tcl_Interp *interp, |
| 3320 | int objc, |
| 3321 | Tcl_Obj *CONST objv[] |
| 3322 | ){ |
| 3323 | sqlite3_stmt *pStmt; |
| 3324 | |
| 3325 | if( objc!=2 ){ |
| 3326 | Tcl_WrongNumArgs(interp, 1, objv, "STMT"); |
| 3327 | return TCL_ERROR; |
| 3328 | } |
| 3329 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3330 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt))); |
| 3331 | return TCL_OK; |
| 3332 | } |
| 3333 | |
| 3334 | /* |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 3335 | ** Usage: sqlite3_bind_parameter_name STMT N |
| 3336 | ** |
| 3337 | ** Return the name of the Nth wildcard. The first wildcard is 1. |
| 3338 | ** An empty string is returned if N is out of range or if the wildcard |
| 3339 | ** is nameless. |
| 3340 | */ |
| 3341 | static int test_bind_parameter_name( |
| 3342 | void * clientData, |
| 3343 | Tcl_Interp *interp, |
| 3344 | int objc, |
| 3345 | Tcl_Obj *CONST objv[] |
| 3346 | ){ |
| 3347 | sqlite3_stmt *pStmt; |
| 3348 | int i; |
| 3349 | |
| 3350 | if( objc!=3 ){ |
| 3351 | Tcl_WrongNumArgs(interp, 1, objv, "STMT N"); |
| 3352 | return TCL_ERROR; |
| 3353 | } |
| 3354 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3355 | if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR; |
| 3356 | Tcl_SetObjResult(interp, |
| 3357 | Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1) |
| 3358 | ); |
| 3359 | return TCL_OK; |
| 3360 | } |
| 3361 | |
| 3362 | /* |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 3363 | ** Usage: sqlite3_bind_parameter_index STMT NAME |
| 3364 | ** |
| 3365 | ** Return the index of the wildcard called NAME. Return 0 if there is |
| 3366 | ** no such wildcard. |
| 3367 | */ |
| 3368 | static int test_bind_parameter_index( |
| 3369 | void * clientData, |
| 3370 | Tcl_Interp *interp, |
| 3371 | int objc, |
| 3372 | Tcl_Obj *CONST objv[] |
| 3373 | ){ |
| 3374 | sqlite3_stmt *pStmt; |
| 3375 | |
| 3376 | if( objc!=3 ){ |
| 3377 | Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME"); |
| 3378 | return TCL_ERROR; |
| 3379 | } |
| 3380 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3381 | Tcl_SetObjResult(interp, |
| 3382 | Tcl_NewIntObj( |
| 3383 | sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2])) |
| 3384 | ) |
| 3385 | ); |
| 3386 | return TCL_OK; |
| 3387 | } |
| 3388 | |
| 3389 | /* |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 3390 | ** Usage: sqlite3_clear_bindings STMT |
| 3391 | ** |
| 3392 | */ |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 3393 | static int test_clear_bindings( |
| 3394 | void * clientData, |
| 3395 | Tcl_Interp *interp, |
| 3396 | int objc, |
| 3397 | Tcl_Obj *CONST objv[] |
| 3398 | ){ |
| 3399 | sqlite3_stmt *pStmt; |
| 3400 | |
| 3401 | if( objc!=2 ){ |
| 3402 | Tcl_WrongNumArgs(interp, 1, objv, "STMT"); |
| 3403 | return TCL_ERROR; |
| 3404 | } |
| 3405 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3406 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt))); |
| 3407 | return TCL_OK; |
| 3408 | } |
drh | f9cb7f5 | 2006-06-27 20:06:44 +0000 | [diff] [blame] | 3409 | |
| 3410 | /* |
| 3411 | ** Usage: sqlite3_sleep MILLISECONDS |
| 3412 | */ |
| 3413 | static int test_sleep( |
| 3414 | void * clientData, |
| 3415 | Tcl_Interp *interp, |
| 3416 | int objc, |
| 3417 | Tcl_Obj *CONST objv[] |
| 3418 | ){ |
| 3419 | int ms; |
| 3420 | |
| 3421 | if( objc!=2 ){ |
| 3422 | Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS"); |
| 3423 | return TCL_ERROR; |
| 3424 | } |
| 3425 | if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){ |
| 3426 | return TCL_ERROR; |
| 3427 | } |
| 3428 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms))); |
| 3429 | return TCL_OK; |
| 3430 | } |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 3431 | |
| 3432 | /* |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 3433 | ** Usage: sqlite3_extended_errcode DB |
| 3434 | ** |
| 3435 | ** Return the string representation of the most recent sqlite3_* API |
| 3436 | ** error code. e.g. "SQLITE_ERROR". |
| 3437 | */ |
| 3438 | static int test_ex_errcode( |
| 3439 | void * clientData, |
| 3440 | Tcl_Interp *interp, |
| 3441 | int objc, |
| 3442 | Tcl_Obj *CONST objv[] |
| 3443 | ){ |
| 3444 | sqlite3 *db; |
| 3445 | int rc; |
| 3446 | |
| 3447 | if( objc!=2 ){ |
| 3448 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3449 | Tcl_GetString(objv[0]), " DB", 0); |
| 3450 | return TCL_ERROR; |
| 3451 | } |
| 3452 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3453 | rc = sqlite3_extended_errcode(db); |
| 3454 | Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0); |
| 3455 | return TCL_OK; |
| 3456 | } |
| 3457 | |
| 3458 | |
| 3459 | /* |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3460 | ** Usage: sqlite3_errcode DB |
| 3461 | ** |
| 3462 | ** Return the string representation of the most recent sqlite3_* API |
| 3463 | ** error code. e.g. "SQLITE_ERROR". |
| 3464 | */ |
| 3465 | static int test_errcode( |
| 3466 | void * clientData, |
| 3467 | Tcl_Interp *interp, |
| 3468 | int objc, |
| 3469 | Tcl_Obj *CONST objv[] |
| 3470 | ){ |
| 3471 | sqlite3 *db; |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 3472 | int rc; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3473 | |
| 3474 | if( objc!=2 ){ |
| 3475 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3476 | Tcl_GetString(objv[0]), " DB", 0); |
| 3477 | return TCL_ERROR; |
| 3478 | } |
| 3479 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 3480 | rc = sqlite3_errcode(db); |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 3481 | Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3482 | return TCL_OK; |
| 3483 | } |
| 3484 | |
| 3485 | /* |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 3486 | ** Usage: sqlite3_errmsg DB |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3487 | ** |
| 3488 | ** Returns the UTF-8 representation of the error message string for the |
| 3489 | ** most recent sqlite3_* API call. |
| 3490 | */ |
| 3491 | static int test_errmsg( |
| 3492 | void * clientData, |
| 3493 | Tcl_Interp *interp, |
| 3494 | int objc, |
| 3495 | Tcl_Obj *CONST objv[] |
| 3496 | ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 3497 | sqlite3 *db; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3498 | const char *zErr; |
| 3499 | |
| 3500 | if( objc!=2 ){ |
| 3501 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3502 | Tcl_GetString(objv[0]), " DB", 0); |
| 3503 | return TCL_ERROR; |
| 3504 | } |
| 3505 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3506 | |
| 3507 | zErr = sqlite3_errmsg(db); |
| 3508 | Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1)); |
| 3509 | return TCL_OK; |
| 3510 | } |
| 3511 | |
| 3512 | /* |
| 3513 | ** Usage: test_errmsg16 DB |
| 3514 | ** |
| 3515 | ** Returns the UTF-16 representation of the error message string for the |
| 3516 | ** most recent sqlite3_* API call. This is a byte array object at the TCL |
| 3517 | ** level, and it includes the 0x00 0x00 terminator bytes at the end of the |
| 3518 | ** UTF-16 string. |
| 3519 | */ |
| 3520 | static int test_errmsg16( |
| 3521 | void * clientData, |
| 3522 | Tcl_Interp *interp, |
| 3523 | int objc, |
| 3524 | Tcl_Obj *CONST objv[] |
| 3525 | ){ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3526 | #ifndef SQLITE_OMIT_UTF16 |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 3527 | sqlite3 *db; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3528 | const void *zErr; |
drh | aed382f | 2009-04-01 18:40:32 +0000 | [diff] [blame] | 3529 | const char *z; |
danielk1977 | 950f054 | 2006-01-18 05:51:57 +0000 | [diff] [blame] | 3530 | int bytes = 0; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3531 | |
| 3532 | if( objc!=2 ){ |
| 3533 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3534 | Tcl_GetString(objv[0]), " DB", 0); |
| 3535 | return TCL_ERROR; |
| 3536 | } |
| 3537 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3538 | |
| 3539 | zErr = sqlite3_errmsg16(db); |
danielk1977 | 950f054 | 2006-01-18 05:51:57 +0000 | [diff] [blame] | 3540 | if( zErr ){ |
drh | aed382f | 2009-04-01 18:40:32 +0000 | [diff] [blame] | 3541 | z = zErr; |
| 3542 | for(bytes=0; z[bytes] || z[bytes+1]; bytes+=2){} |
danielk1977 | 950f054 | 2006-01-18 05:51:57 +0000 | [diff] [blame] | 3543 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3544 | Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes)); |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3545 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3546 | return TCL_OK; |
| 3547 | } |
| 3548 | |
| 3549 | /* |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3550 | ** Usage: sqlite3_prepare DB sql bytes ?tailvar? |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3551 | ** |
| 3552 | ** Compile up to <bytes> bytes of the supplied SQL string <sql> using |
| 3553 | ** database handle <DB>. The parameter <tailval> is the name of a global |
| 3554 | ** variable that is set to the unused portion of <sql> (if any). A |
| 3555 | ** STMT handle is returned. |
| 3556 | */ |
| 3557 | static int test_prepare( |
| 3558 | void * clientData, |
| 3559 | Tcl_Interp *interp, |
| 3560 | int objc, |
| 3561 | Tcl_Obj *CONST objv[] |
| 3562 | ){ |
| 3563 | sqlite3 *db; |
| 3564 | const char *zSql; |
| 3565 | int bytes; |
| 3566 | const char *zTail = 0; |
| 3567 | sqlite3_stmt *pStmt = 0; |
| 3568 | char zBuf[50]; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3569 | int rc; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3570 | |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3571 | if( objc!=5 && objc!=4 ){ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3572 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3573 | Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3574 | return TCL_ERROR; |
| 3575 | } |
| 3576 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3577 | zSql = Tcl_GetString(objv[2]); |
| 3578 | if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; |
| 3579 | |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3580 | rc = sqlite3_prepare(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 3581 | Tcl_ResetResult(interp); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3582 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3583 | if( zTail && objc>=5 ){ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3584 | if( bytes>=0 ){ |
drh | 83cc139 | 2012-04-19 18:04:28 +0000 | [diff] [blame] | 3585 | bytes = bytes - (int)(zTail-zSql); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3586 | } |
drh | 7da5fcb | 2012-03-30 14:59:43 +0000 | [diff] [blame] | 3587 | if( (int)strlen(zTail)<bytes ){ |
drh | 83cc139 | 2012-04-19 18:04:28 +0000 | [diff] [blame] | 3588 | bytes = (int)strlen(zTail); |
danielk1977 | 3a2c8c8 | 2008-04-03 14:36:25 +0000 | [diff] [blame] | 3589 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3590 | Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0); |
| 3591 | } |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3592 | if( rc!=SQLITE_OK ){ |
| 3593 | assert( pStmt==0 ); |
| 3594 | sprintf(zBuf, "(%d) ", rc); |
| 3595 | Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); |
| 3596 | return TCL_ERROR; |
| 3597 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3598 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3599 | if( pStmt ){ |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 3600 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3601 | Tcl_AppendResult(interp, zBuf, 0); |
| 3602 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3603 | return TCL_OK; |
| 3604 | } |
| 3605 | |
| 3606 | /* |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3607 | ** Usage: sqlite3_prepare_v2 DB sql bytes ?tailvar? |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3608 | ** |
| 3609 | ** Compile up to <bytes> bytes of the supplied SQL string <sql> using |
| 3610 | ** database handle <DB>. The parameter <tailval> is the name of a global |
| 3611 | ** variable that is set to the unused portion of <sql> (if any). A |
| 3612 | ** STMT handle is returned. |
| 3613 | */ |
| 3614 | static int test_prepare_v2( |
| 3615 | void * clientData, |
| 3616 | Tcl_Interp *interp, |
| 3617 | int objc, |
| 3618 | Tcl_Obj *CONST objv[] |
| 3619 | ){ |
| 3620 | sqlite3 *db; |
| 3621 | const char *zSql; |
| 3622 | int bytes; |
| 3623 | const char *zTail = 0; |
| 3624 | sqlite3_stmt *pStmt = 0; |
| 3625 | char zBuf[50]; |
| 3626 | int rc; |
| 3627 | |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3628 | if( objc!=5 && objc!=4 ){ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3629 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3630 | Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); |
| 3631 | return TCL_ERROR; |
| 3632 | } |
| 3633 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3634 | zSql = Tcl_GetString(objv[2]); |
| 3635 | if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; |
| 3636 | |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3637 | rc = sqlite3_prepare_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); |
danielk1977 | 7e29e95 | 2007-04-19 11:09:01 +0000 | [diff] [blame] | 3638 | assert(rc==SQLITE_OK || pStmt==0); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 3639 | Tcl_ResetResult(interp); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3640 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3641 | if( zTail && objc>=5 ){ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3642 | if( bytes>=0 ){ |
drh | 83cc139 | 2012-04-19 18:04:28 +0000 | [diff] [blame] | 3643 | bytes = bytes - (int)(zTail-zSql); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3644 | } |
| 3645 | Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0); |
| 3646 | } |
| 3647 | if( rc!=SQLITE_OK ){ |
| 3648 | assert( pStmt==0 ); |
| 3649 | sprintf(zBuf, "(%d) ", rc); |
| 3650 | Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); |
| 3651 | return TCL_ERROR; |
| 3652 | } |
| 3653 | |
| 3654 | if( pStmt ){ |
| 3655 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
| 3656 | Tcl_AppendResult(interp, zBuf, 0); |
| 3657 | } |
| 3658 | return TCL_OK; |
| 3659 | } |
| 3660 | |
| 3661 | /* |
drh | 4837f53 | 2008-05-23 14:49:49 +0000 | [diff] [blame] | 3662 | ** Usage: sqlite3_prepare_tkt3134 DB |
| 3663 | ** |
| 3664 | ** Generate a prepared statement for a zero-byte string as a test |
| 3665 | ** for ticket #3134. The string should be preceeded by a zero byte. |
| 3666 | */ |
| 3667 | static int test_prepare_tkt3134( |
| 3668 | void * clientData, |
| 3669 | Tcl_Interp *interp, |
| 3670 | int objc, |
| 3671 | Tcl_Obj *CONST objv[] |
| 3672 | ){ |
| 3673 | sqlite3 *db; |
| 3674 | static const char zSql[] = "\000SELECT 1"; |
| 3675 | sqlite3_stmt *pStmt = 0; |
| 3676 | char zBuf[50]; |
| 3677 | int rc; |
| 3678 | |
| 3679 | if( objc!=2 ){ |
| 3680 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3681 | Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); |
| 3682 | return TCL_ERROR; |
| 3683 | } |
| 3684 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3685 | rc = sqlite3_prepare_v2(db, &zSql[1], 0, &pStmt, 0); |
| 3686 | assert(rc==SQLITE_OK || pStmt==0); |
| 3687 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 3688 | if( rc!=SQLITE_OK ){ |
| 3689 | assert( pStmt==0 ); |
| 3690 | sprintf(zBuf, "(%d) ", rc); |
| 3691 | Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); |
| 3692 | return TCL_ERROR; |
| 3693 | } |
| 3694 | |
| 3695 | if( pStmt ){ |
| 3696 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
| 3697 | Tcl_AppendResult(interp, zBuf, 0); |
| 3698 | } |
| 3699 | return TCL_OK; |
| 3700 | } |
| 3701 | |
| 3702 | /* |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3703 | ** Usage: sqlite3_prepare16 DB sql bytes tailvar |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3704 | ** |
| 3705 | ** Compile up to <bytes> bytes of the supplied SQL string <sql> using |
| 3706 | ** database handle <DB>. The parameter <tailval> is the name of a global |
| 3707 | ** variable that is set to the unused portion of <sql> (if any). A |
| 3708 | ** STMT handle is returned. |
| 3709 | */ |
| 3710 | static int test_prepare16( |
| 3711 | void * clientData, |
| 3712 | Tcl_Interp *interp, |
| 3713 | int objc, |
| 3714 | Tcl_Obj *CONST objv[] |
| 3715 | ){ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3716 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3717 | sqlite3 *db; |
| 3718 | const void *zSql; |
| 3719 | const void *zTail = 0; |
| 3720 | Tcl_Obj *pTail = 0; |
| 3721 | sqlite3_stmt *pStmt = 0; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3722 | char zBuf[50]; |
| 3723 | int rc; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3724 | int bytes; /* The integer specified as arg 3 */ |
| 3725 | int objlen; /* The byte-array length of arg 2 */ |
| 3726 | |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3727 | if( objc!=5 && objc!=4 ){ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3728 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3729 | Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3730 | return TCL_ERROR; |
| 3731 | } |
| 3732 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3733 | zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen); |
| 3734 | if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; |
| 3735 | |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3736 | rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 3737 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 3738 | if( rc ){ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3739 | return TCL_ERROR; |
| 3740 | } |
| 3741 | |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3742 | if( objc>=5 ){ |
| 3743 | if( zTail ){ |
drh | 83cc139 | 2012-04-19 18:04:28 +0000 | [diff] [blame] | 3744 | objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql); |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3745 | }else{ |
| 3746 | objlen = 0; |
| 3747 | } |
| 3748 | pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen); |
| 3749 | Tcl_IncrRefCount(pTail); |
| 3750 | Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0); |
| 3751 | Tcl_DecrRefCount(pTail); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3752 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3753 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3754 | if( pStmt ){ |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 3755 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3756 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3757 | Tcl_AppendResult(interp, zBuf, 0); |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3758 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 3759 | return TCL_OK; |
| 3760 | } |
| 3761 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3762 | /* |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3763 | ** Usage: sqlite3_prepare16_v2 DB sql bytes ?tailvar? |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3764 | ** |
| 3765 | ** Compile up to <bytes> bytes of the supplied SQL string <sql> using |
| 3766 | ** database handle <DB>. The parameter <tailval> is the name of a global |
| 3767 | ** variable that is set to the unused portion of <sql> (if any). A |
| 3768 | ** STMT handle is returned. |
| 3769 | */ |
| 3770 | static int test_prepare16_v2( |
| 3771 | void * clientData, |
| 3772 | Tcl_Interp *interp, |
| 3773 | int objc, |
| 3774 | Tcl_Obj *CONST objv[] |
| 3775 | ){ |
| 3776 | #ifndef SQLITE_OMIT_UTF16 |
| 3777 | sqlite3 *db; |
| 3778 | const void *zSql; |
| 3779 | const void *zTail = 0; |
| 3780 | Tcl_Obj *pTail = 0; |
| 3781 | sqlite3_stmt *pStmt = 0; |
| 3782 | char zBuf[50]; |
| 3783 | int rc; |
| 3784 | int bytes; /* The integer specified as arg 3 */ |
| 3785 | int objlen; /* The byte-array length of arg 2 */ |
| 3786 | |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3787 | if( objc!=5 && objc!=4 ){ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3788 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3789 | Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3790 | return TCL_ERROR; |
| 3791 | } |
| 3792 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3793 | zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen); |
| 3794 | if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; |
| 3795 | |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3796 | rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3797 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 3798 | if( rc ){ |
| 3799 | return TCL_ERROR; |
| 3800 | } |
| 3801 | |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3802 | if( objc>=5 ){ |
| 3803 | if( zTail ){ |
drh | 83cc139 | 2012-04-19 18:04:28 +0000 | [diff] [blame] | 3804 | objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql); |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 3805 | }else{ |
| 3806 | objlen = 0; |
| 3807 | } |
| 3808 | pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen); |
| 3809 | Tcl_IncrRefCount(pTail); |
| 3810 | Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0); |
| 3811 | Tcl_DecrRefCount(pTail); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3812 | } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 3813 | |
| 3814 | if( pStmt ){ |
| 3815 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
| 3816 | } |
| 3817 | Tcl_AppendResult(interp, zBuf, 0); |
| 3818 | #endif /* SQLITE_OMIT_UTF16 */ |
| 3819 | return TCL_OK; |
| 3820 | } |
| 3821 | |
| 3822 | /* |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3823 | ** Usage: sqlite3_open filename ?options-list? |
| 3824 | */ |
| 3825 | static int test_open( |
| 3826 | void * clientData, |
| 3827 | Tcl_Interp *interp, |
| 3828 | int objc, |
| 3829 | Tcl_Obj *CONST objv[] |
| 3830 | ){ |
| 3831 | const char *zFilename; |
| 3832 | sqlite3 *db; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3833 | char zBuf[100]; |
| 3834 | |
drh | afc9104 | 2008-02-21 02:09:45 +0000 | [diff] [blame] | 3835 | if( objc!=3 && objc!=2 && objc!=1 ){ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3836 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3837 | Tcl_GetString(objv[0]), " filename options-list", 0); |
| 3838 | return TCL_ERROR; |
| 3839 | } |
| 3840 | |
drh | afc9104 | 2008-02-21 02:09:45 +0000 | [diff] [blame] | 3841 | zFilename = objc>1 ? Tcl_GetString(objv[1]) : 0; |
drh | caffb1a | 2012-01-30 18:00:31 +0000 | [diff] [blame] | 3842 | sqlite3_open(zFilename, &db); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3843 | |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 3844 | if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3845 | Tcl_AppendResult(interp, zBuf, 0); |
| 3846 | return TCL_OK; |
| 3847 | } |
| 3848 | |
| 3849 | /* |
dan | 286ab7c | 2011-05-06 18:34:54 +0000 | [diff] [blame] | 3850 | ** Usage: sqlite3_open_v2 FILENAME FLAGS VFS |
| 3851 | */ |
| 3852 | static int test_open_v2( |
| 3853 | void * clientData, |
| 3854 | Tcl_Interp *interp, |
| 3855 | int objc, |
| 3856 | Tcl_Obj *CONST objv[] |
| 3857 | ){ |
| 3858 | const char *zFilename; |
| 3859 | const char *zVfs; |
| 3860 | int flags = 0; |
| 3861 | sqlite3 *db; |
| 3862 | int rc; |
| 3863 | char zBuf[100]; |
| 3864 | |
| 3865 | int nFlag; |
| 3866 | Tcl_Obj **apFlag; |
| 3867 | int i; |
| 3868 | |
| 3869 | if( objc!=4 ){ |
| 3870 | Tcl_WrongNumArgs(interp, 1, objv, "FILENAME FLAGS VFS"); |
| 3871 | return TCL_ERROR; |
| 3872 | } |
| 3873 | zFilename = Tcl_GetString(objv[1]); |
| 3874 | zVfs = Tcl_GetString(objv[3]); |
| 3875 | if( zVfs[0]==0x00 ) zVfs = 0; |
| 3876 | |
| 3877 | rc = Tcl_ListObjGetElements(interp, objv[2], &nFlag, &apFlag); |
| 3878 | if( rc!=TCL_OK ) return rc; |
| 3879 | for(i=0; i<nFlag; i++){ |
| 3880 | int iFlag; |
| 3881 | struct OpenFlag { |
| 3882 | const char *zFlag; |
| 3883 | int flag; |
| 3884 | } aFlag[] = { |
| 3885 | { "SQLITE_OPEN_READONLY", SQLITE_OPEN_READONLY }, |
| 3886 | { "SQLITE_OPEN_READWRITE", SQLITE_OPEN_READWRITE }, |
| 3887 | { "SQLITE_OPEN_CREATE", SQLITE_OPEN_CREATE }, |
| 3888 | { "SQLITE_OPEN_DELETEONCLOSE", SQLITE_OPEN_DELETEONCLOSE }, |
| 3889 | { "SQLITE_OPEN_EXCLUSIVE", SQLITE_OPEN_EXCLUSIVE }, |
| 3890 | { "SQLITE_OPEN_AUTOPROXY", SQLITE_OPEN_AUTOPROXY }, |
| 3891 | { "SQLITE_OPEN_MAIN_DB", SQLITE_OPEN_MAIN_DB }, |
| 3892 | { "SQLITE_OPEN_TEMP_DB", SQLITE_OPEN_TEMP_DB }, |
| 3893 | { "SQLITE_OPEN_TRANSIENT_DB", SQLITE_OPEN_TRANSIENT_DB }, |
| 3894 | { "SQLITE_OPEN_MAIN_JOURNAL", SQLITE_OPEN_MAIN_JOURNAL }, |
| 3895 | { "SQLITE_OPEN_TEMP_JOURNAL", SQLITE_OPEN_TEMP_JOURNAL }, |
| 3896 | { "SQLITE_OPEN_SUBJOURNAL", SQLITE_OPEN_SUBJOURNAL }, |
| 3897 | { "SQLITE_OPEN_MASTER_JOURNAL", SQLITE_OPEN_MASTER_JOURNAL }, |
| 3898 | { "SQLITE_OPEN_NOMUTEX", SQLITE_OPEN_NOMUTEX }, |
| 3899 | { "SQLITE_OPEN_FULLMUTEX", SQLITE_OPEN_FULLMUTEX }, |
| 3900 | { "SQLITE_OPEN_SHAREDCACHE", SQLITE_OPEN_SHAREDCACHE }, |
| 3901 | { "SQLITE_OPEN_PRIVATECACHE", SQLITE_OPEN_PRIVATECACHE }, |
| 3902 | { "SQLITE_OPEN_WAL", SQLITE_OPEN_WAL }, |
| 3903 | { "SQLITE_OPEN_URI", SQLITE_OPEN_URI }, |
| 3904 | { 0, 0 } |
| 3905 | }; |
| 3906 | rc = Tcl_GetIndexFromObjStruct(interp, apFlag[i], aFlag, sizeof(aFlag[0]), |
| 3907 | "flag", 0, &iFlag |
| 3908 | ); |
| 3909 | if( rc!=TCL_OK ) return rc; |
| 3910 | flags |= aFlag[iFlag].flag; |
| 3911 | } |
| 3912 | |
| 3913 | rc = sqlite3_open_v2(zFilename, &db, flags, zVfs); |
| 3914 | if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; |
| 3915 | Tcl_AppendResult(interp, zBuf, 0); |
| 3916 | return TCL_OK; |
| 3917 | } |
| 3918 | |
| 3919 | /* |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3920 | ** Usage: sqlite3_open16 filename options |
| 3921 | */ |
| 3922 | static int test_open16( |
| 3923 | void * clientData, |
| 3924 | Tcl_Interp *interp, |
| 3925 | int objc, |
| 3926 | Tcl_Obj *CONST objv[] |
| 3927 | ){ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3928 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3929 | const void *zFilename; |
| 3930 | sqlite3 *db; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3931 | char zBuf[100]; |
| 3932 | |
| 3933 | if( objc!=3 ){ |
| 3934 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3935 | Tcl_GetString(objv[0]), " filename options-list", 0); |
| 3936 | return TCL_ERROR; |
| 3937 | } |
| 3938 | |
| 3939 | zFilename = Tcl_GetByteArrayFromObj(objv[1], 0); |
drh | caffb1a | 2012-01-30 18:00:31 +0000 | [diff] [blame] | 3940 | sqlite3_open16(zFilename, &db); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3941 | |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 3942 | if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3943 | Tcl_AppendResult(interp, zBuf, 0); |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 3944 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 3945 | return TCL_OK; |
| 3946 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 3947 | |
| 3948 | /* |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 3949 | ** Usage: sqlite3_complete16 <UTF-16 string> |
| 3950 | ** |
| 3951 | ** Return 1 if the supplied argument is a complete SQL statement, or zero |
| 3952 | ** otherwise. |
| 3953 | */ |
| 3954 | static int test_complete16( |
| 3955 | void * clientData, |
| 3956 | Tcl_Interp *interp, |
| 3957 | int objc, |
| 3958 | Tcl_Obj *CONST objv[] |
| 3959 | ){ |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 3960 | #if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16) |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 3961 | char *zBuf; |
| 3962 | |
| 3963 | if( objc!=2 ){ |
| 3964 | Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>"); |
| 3965 | return TCL_ERROR; |
| 3966 | } |
| 3967 | |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 3968 | zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 3969 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf))); |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 3970 | #endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 3971 | return TCL_OK; |
| 3972 | } |
| 3973 | |
| 3974 | /* |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3975 | ** Usage: sqlite3_step STMT |
| 3976 | ** |
| 3977 | ** Advance the statement to the next row. |
| 3978 | */ |
danielk1977 | 17240fd | 2004-05-26 00:07:25 +0000 | [diff] [blame] | 3979 | static int test_step( |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3980 | void * clientData, |
| 3981 | Tcl_Interp *interp, |
| 3982 | int objc, |
| 3983 | Tcl_Obj *CONST objv[] |
| 3984 | ){ |
| 3985 | sqlite3_stmt *pStmt; |
| 3986 | int rc; |
| 3987 | |
danielk1977 | e1cd987 | 2004-05-22 10:33:04 +0000 | [diff] [blame] | 3988 | if( objc!=2 ){ |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3989 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3990 | Tcl_GetString(objv[0]), " STMT", 0); |
| 3991 | return TCL_ERROR; |
| 3992 | } |
| 3993 | |
| 3994 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
danielk1977 | 17240fd | 2004-05-26 00:07:25 +0000 | [diff] [blame] | 3995 | rc = sqlite3_step(pStmt); |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 3996 | |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 3997 | /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3998 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); |
danielk1977 | e1cd987 | 2004-05-22 10:33:04 +0000 | [diff] [blame] | 3999 | return TCL_OK; |
| 4000 | } |
| 4001 | |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 4002 | static int test_sql( |
| 4003 | void * clientData, |
| 4004 | Tcl_Interp *interp, |
| 4005 | int objc, |
| 4006 | Tcl_Obj *CONST objv[] |
| 4007 | ){ |
| 4008 | sqlite3_stmt *pStmt; |
| 4009 | |
| 4010 | if( objc!=2 ){ |
| 4011 | Tcl_WrongNumArgs(interp, 1, objv, "STMT"); |
| 4012 | return TCL_ERROR; |
| 4013 | } |
| 4014 | |
| 4015 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4016 | Tcl_SetResult(interp, (char *)sqlite3_sql(pStmt), TCL_VOLATILE); |
| 4017 | return TCL_OK; |
| 4018 | } |
| 4019 | |
danielk1977 | e1cd987 | 2004-05-22 10:33:04 +0000 | [diff] [blame] | 4020 | /* |
danielk1977 | 17240fd | 2004-05-26 00:07:25 +0000 | [diff] [blame] | 4021 | ** Usage: sqlite3_column_count STMT |
| 4022 | ** |
| 4023 | ** Return the number of columns returned by the sql statement STMT. |
| 4024 | */ |
| 4025 | static int test_column_count( |
| 4026 | void * clientData, |
| 4027 | Tcl_Interp *interp, |
| 4028 | int objc, |
| 4029 | Tcl_Obj *CONST objv[] |
| 4030 | ){ |
| 4031 | sqlite3_stmt *pStmt; |
| 4032 | |
| 4033 | if( objc!=2 ){ |
| 4034 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4035 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 4036 | return TCL_ERROR; |
| 4037 | } |
| 4038 | |
| 4039 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4040 | |
| 4041 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt))); |
| 4042 | return TCL_OK; |
| 4043 | } |
| 4044 | |
| 4045 | /* |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4046 | ** Usage: sqlite3_column_type STMT column |
| 4047 | ** |
| 4048 | ** Return the type of the data in column 'column' of the current row. |
| 4049 | */ |
| 4050 | static int test_column_type( |
| 4051 | void * clientData, |
| 4052 | Tcl_Interp *interp, |
| 4053 | int objc, |
| 4054 | Tcl_Obj *CONST objv[] |
| 4055 | ){ |
| 4056 | sqlite3_stmt *pStmt; |
| 4057 | int col; |
| 4058 | int tp; |
| 4059 | |
| 4060 | if( objc!=3 ){ |
| 4061 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4062 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 4063 | return TCL_ERROR; |
| 4064 | } |
| 4065 | |
| 4066 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4067 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 4068 | |
| 4069 | tp = sqlite3_column_type(pStmt, col); |
| 4070 | switch( tp ){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 4071 | case SQLITE_INTEGER: |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4072 | Tcl_SetResult(interp, "INTEGER", TCL_STATIC); |
| 4073 | break; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 4074 | case SQLITE_NULL: |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4075 | Tcl_SetResult(interp, "NULL", TCL_STATIC); |
| 4076 | break; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 4077 | case SQLITE_FLOAT: |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4078 | Tcl_SetResult(interp, "FLOAT", TCL_STATIC); |
| 4079 | break; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 4080 | case SQLITE_TEXT: |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4081 | Tcl_SetResult(interp, "TEXT", TCL_STATIC); |
| 4082 | break; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 4083 | case SQLITE_BLOB: |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4084 | Tcl_SetResult(interp, "BLOB", TCL_STATIC); |
| 4085 | break; |
| 4086 | default: |
| 4087 | assert(0); |
| 4088 | } |
| 4089 | |
| 4090 | return TCL_OK; |
| 4091 | } |
| 4092 | |
| 4093 | /* |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4094 | ** Usage: sqlite3_column_int64 STMT column |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4095 | ** |
| 4096 | ** Return the data in column 'column' of the current row cast as an |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4097 | ** wide (64-bit) integer. |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4098 | */ |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4099 | static int test_column_int64( |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4100 | void * clientData, |
| 4101 | Tcl_Interp *interp, |
| 4102 | int objc, |
| 4103 | Tcl_Obj *CONST objv[] |
| 4104 | ){ |
| 4105 | sqlite3_stmt *pStmt; |
| 4106 | int col; |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4107 | i64 iVal; |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4108 | |
| 4109 | if( objc!=3 ){ |
| 4110 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4111 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 4112 | return TCL_ERROR; |
| 4113 | } |
| 4114 | |
| 4115 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4116 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 4117 | |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4118 | iVal = sqlite3_column_int64(pStmt, col); |
| 4119 | Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal)); |
| 4120 | return TCL_OK; |
| 4121 | } |
| 4122 | |
| 4123 | /* |
danielk1977 | ea61b2c | 2004-05-27 01:49:51 +0000 | [diff] [blame] | 4124 | ** Usage: sqlite3_column_blob STMT column |
| 4125 | */ |
| 4126 | static int test_column_blob( |
| 4127 | void * clientData, |
| 4128 | Tcl_Interp *interp, |
| 4129 | int objc, |
| 4130 | Tcl_Obj *CONST objv[] |
| 4131 | ){ |
| 4132 | sqlite3_stmt *pStmt; |
| 4133 | int col; |
| 4134 | |
| 4135 | int len; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 4136 | const void *pBlob; |
danielk1977 | ea61b2c | 2004-05-27 01:49:51 +0000 | [diff] [blame] | 4137 | |
| 4138 | if( objc!=3 ){ |
| 4139 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4140 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 4141 | return TCL_ERROR; |
| 4142 | } |
| 4143 | |
| 4144 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4145 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 4146 | |
danielk1977 | ea61b2c | 2004-05-27 01:49:51 +0000 | [diff] [blame] | 4147 | len = sqlite3_column_bytes(pStmt, col); |
drh | 9310ef2 | 2007-04-27 17:16:20 +0000 | [diff] [blame] | 4148 | pBlob = sqlite3_column_blob(pStmt, col); |
danielk1977 | ea61b2c | 2004-05-27 01:49:51 +0000 | [diff] [blame] | 4149 | Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len)); |
| 4150 | return TCL_OK; |
| 4151 | } |
| 4152 | |
| 4153 | /* |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4154 | ** Usage: sqlite3_column_double STMT column |
| 4155 | ** |
| 4156 | ** Return the data in column 'column' of the current row cast as a double. |
| 4157 | */ |
| 4158 | static int test_column_double( |
| 4159 | void * clientData, |
| 4160 | Tcl_Interp *interp, |
| 4161 | int objc, |
| 4162 | Tcl_Obj *CONST objv[] |
| 4163 | ){ |
| 4164 | sqlite3_stmt *pStmt; |
| 4165 | int col; |
| 4166 | double rVal; |
| 4167 | |
| 4168 | if( objc!=3 ){ |
| 4169 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4170 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 4171 | return TCL_ERROR; |
| 4172 | } |
| 4173 | |
| 4174 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4175 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 4176 | |
| 4177 | rVal = sqlite3_column_double(pStmt, col); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 4178 | Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal)); |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 4179 | return TCL_OK; |
| 4180 | } |
| 4181 | |
| 4182 | /* |
danielk1977 | 17240fd | 2004-05-26 00:07:25 +0000 | [diff] [blame] | 4183 | ** Usage: sqlite3_data_count STMT |
| 4184 | ** |
| 4185 | ** Return the number of columns returned by the sql statement STMT. |
| 4186 | */ |
| 4187 | static int test_data_count( |
| 4188 | void * clientData, |
| 4189 | Tcl_Interp *interp, |
| 4190 | int objc, |
| 4191 | Tcl_Obj *CONST objv[] |
| 4192 | ){ |
| 4193 | sqlite3_stmt *pStmt; |
| 4194 | |
| 4195 | if( objc!=2 ){ |
| 4196 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4197 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 4198 | return TCL_ERROR; |
| 4199 | } |
| 4200 | |
| 4201 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4202 | |
| 4203 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt))); |
| 4204 | return TCL_OK; |
| 4205 | } |
| 4206 | |
| 4207 | /* |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4208 | ** Usage: sqlite3_column_text STMT column |
| 4209 | ** |
| 4210 | ** Usage: sqlite3_column_decltype STMT column |
| 4211 | ** |
| 4212 | ** Usage: sqlite3_column_name STMT column |
| 4213 | */ |
| 4214 | static int test_stmt_utf8( |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 4215 | void * clientData, /* Pointer to SQLite API function to be invoke */ |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4216 | Tcl_Interp *interp, |
| 4217 | int objc, |
| 4218 | Tcl_Obj *CONST objv[] |
| 4219 | ){ |
| 4220 | sqlite3_stmt *pStmt; |
| 4221 | int col; |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 4222 | const char *(*xFunc)(sqlite3_stmt*, int); |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 4223 | const char *zRet; |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4224 | |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 4225 | xFunc = (const char *(*)(sqlite3_stmt*, int))clientData; |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4226 | if( objc!=3 ){ |
| 4227 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4228 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 4229 | return TCL_ERROR; |
| 4230 | } |
| 4231 | |
| 4232 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4233 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 4234 | zRet = xFunc(pStmt, col); |
| 4235 | if( zRet ){ |
| 4236 | Tcl_SetResult(interp, (char *)zRet, 0); |
| 4237 | } |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4238 | return TCL_OK; |
| 4239 | } |
| 4240 | |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4241 | static int test_global_recover( |
| 4242 | void * clientData, |
| 4243 | Tcl_Interp *interp, |
| 4244 | int objc, |
| 4245 | Tcl_Obj *CONST objv[] |
| 4246 | ){ |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 4247 | #ifndef SQLITE_OMIT_DEPRECATED |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4248 | int rc; |
| 4249 | if( objc!=1 ){ |
| 4250 | Tcl_WrongNumArgs(interp, 1, objv, ""); |
| 4251 | return TCL_ERROR; |
| 4252 | } |
| 4253 | rc = sqlite3_global_recover(); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4254 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4255 | #endif |
| 4256 | return TCL_OK; |
| 4257 | } |
| 4258 | |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4259 | /* |
| 4260 | ** Usage: sqlite3_column_text STMT column |
| 4261 | ** |
| 4262 | ** Usage: sqlite3_column_decltype STMT column |
| 4263 | ** |
| 4264 | ** Usage: sqlite3_column_name STMT column |
| 4265 | */ |
| 4266 | static int test_stmt_utf16( |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 4267 | void * clientData, /* Pointer to SQLite API function to be invoked */ |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4268 | Tcl_Interp *interp, |
| 4269 | int objc, |
| 4270 | Tcl_Obj *CONST objv[] |
| 4271 | ){ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 4272 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4273 | sqlite3_stmt *pStmt; |
| 4274 | int col; |
| 4275 | Tcl_Obj *pRet; |
| 4276 | const void *zName16; |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 4277 | const void *(*xFunc)(sqlite3_stmt*, int); |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4278 | |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 4279 | xFunc = (const void *(*)(sqlite3_stmt*, int))clientData; |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4280 | if( objc!=3 ){ |
| 4281 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4282 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 4283 | return TCL_ERROR; |
| 4284 | } |
| 4285 | |
| 4286 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4287 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 4288 | |
| 4289 | zName16 = xFunc(pStmt, col); |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 4290 | if( zName16 ){ |
drh | aed382f | 2009-04-01 18:40:32 +0000 | [diff] [blame] | 4291 | int n; |
| 4292 | const char *z = zName16; |
| 4293 | for(n=0; z[n] || z[n+1]; n+=2){} |
| 4294 | pRet = Tcl_NewByteArrayObj(zName16, n+2); |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 4295 | Tcl_SetObjResult(interp, pRet); |
| 4296 | } |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 4297 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4298 | |
| 4299 | return TCL_OK; |
| 4300 | } |
| 4301 | |
| 4302 | /* |
| 4303 | ** Usage: sqlite3_column_int STMT column |
| 4304 | ** |
| 4305 | ** Usage: sqlite3_column_bytes STMT column |
| 4306 | ** |
| 4307 | ** Usage: sqlite3_column_bytes16 STMT column |
| 4308 | ** |
| 4309 | */ |
| 4310 | static int test_stmt_int( |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 4311 | void * clientData, /* Pointer to SQLite API function to be invoked */ |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4312 | Tcl_Interp *interp, |
| 4313 | int objc, |
| 4314 | Tcl_Obj *CONST objv[] |
| 4315 | ){ |
| 4316 | sqlite3_stmt *pStmt; |
| 4317 | int col; |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 4318 | int (*xFunc)(sqlite3_stmt*, int); |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4319 | |
danielk1977 | 55a25a1 | 2008-09-11 10:29:15 +0000 | [diff] [blame] | 4320 | xFunc = (int (*)(sqlite3_stmt*, int))clientData; |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 4321 | if( objc!=3 ){ |
| 4322 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4323 | Tcl_GetString(objv[0]), " STMT column", 0); |
| 4324 | return TCL_ERROR; |
| 4325 | } |
| 4326 | |
| 4327 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4328 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 4329 | |
| 4330 | Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col))); |
| 4331 | return TCL_OK; |
| 4332 | } |
| 4333 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 4334 | /* |
drh | cacb208 | 2005-01-11 15:28:33 +0000 | [diff] [blame] | 4335 | ** Usage: sqlite_set_magic DB MAGIC-NUMBER |
| 4336 | ** |
| 4337 | ** Set the db->magic value. This is used to test error recovery logic. |
| 4338 | */ |
| 4339 | static int sqlite_set_magic( |
| 4340 | void * clientData, |
| 4341 | Tcl_Interp *interp, |
| 4342 | int argc, |
| 4343 | char **argv |
| 4344 | ){ |
| 4345 | sqlite3 *db; |
| 4346 | if( argc!=3 ){ |
| 4347 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 4348 | " DB MAGIC", 0); |
| 4349 | return TCL_ERROR; |
| 4350 | } |
| 4351 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 4352 | if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){ |
| 4353 | db->magic = SQLITE_MAGIC_OPEN; |
| 4354 | }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){ |
| 4355 | db->magic = SQLITE_MAGIC_CLOSED; |
| 4356 | }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){ |
| 4357 | db->magic = SQLITE_MAGIC_BUSY; |
| 4358 | }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){ |
| 4359 | db->magic = SQLITE_MAGIC_ERROR; |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 4360 | }else if( Tcl_GetInt(interp, argv[2], (int*)&db->magic) ){ |
drh | cacb208 | 2005-01-11 15:28:33 +0000 | [diff] [blame] | 4361 | return TCL_ERROR; |
| 4362 | } |
| 4363 | return TCL_OK; |
| 4364 | } |
| 4365 | |
| 4366 | /* |
drh | c5cdca6 | 2005-01-11 16:54:14 +0000 | [diff] [blame] | 4367 | ** Usage: sqlite3_interrupt DB |
| 4368 | ** |
| 4369 | ** Trigger an interrupt on DB |
| 4370 | */ |
| 4371 | static int test_interrupt( |
| 4372 | void * clientData, |
| 4373 | Tcl_Interp *interp, |
| 4374 | int argc, |
| 4375 | char **argv |
| 4376 | ){ |
| 4377 | sqlite3 *db; |
| 4378 | if( argc!=2 ){ |
| 4379 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0); |
| 4380 | return TCL_ERROR; |
| 4381 | } |
| 4382 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 4383 | sqlite3_interrupt(db); |
| 4384 | return TCL_OK; |
| 4385 | } |
| 4386 | |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 4387 | static u8 *sqlite3_stack_baseline = 0; |
| 4388 | |
drh | c5cdca6 | 2005-01-11 16:54:14 +0000 | [diff] [blame] | 4389 | /* |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 4390 | ** Fill the stack with a known bitpattern. |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 4391 | */ |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 4392 | static void prepStack(void){ |
| 4393 | int i; |
| 4394 | u32 bigBuf[65536]; |
drh | bc2be0c | 2011-08-30 00:53:50 +0000 | [diff] [blame] | 4395 | for(i=0; i<sizeof(bigBuf)/sizeof(bigBuf[0]); i++) bigBuf[i] = 0xdeadbeef; |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 4396 | sqlite3_stack_baseline = (u8*)&bigBuf[65536]; |
| 4397 | } |
| 4398 | |
| 4399 | /* |
| 4400 | ** Get the current stack depth. Used for debugging only. |
| 4401 | */ |
| 4402 | u64 sqlite3StackDepth(void){ |
| 4403 | u8 x; |
| 4404 | return (u64)(sqlite3_stack_baseline - &x); |
| 4405 | } |
| 4406 | |
| 4407 | /* |
| 4408 | ** Usage: sqlite3_stack_used DB SQL |
| 4409 | ** |
| 4410 | ** Try to measure the amount of stack space used by a call to sqlite3_exec |
| 4411 | */ |
| 4412 | static int test_stack_used( |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 4413 | void * clientData, |
| 4414 | Tcl_Interp *interp, |
| 4415 | int argc, |
| 4416 | char **argv |
| 4417 | ){ |
| 4418 | sqlite3 *db; |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 4419 | int i; |
| 4420 | if( argc!=3 ){ |
| 4421 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 4422 | " DB SQL", 0); |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 4423 | return TCL_ERROR; |
| 4424 | } |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 4425 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 4426 | prepStack(); |
drh | 3752785 | 2006-03-16 16:19:56 +0000 | [diff] [blame] | 4427 | (void)sqlite3_exec(db, argv[2], 0, 0, 0); |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 4428 | for(i=65535; i>=0 && ((u32*)sqlite3_stack_baseline)[-i]==0xdeadbeef; i--){} |
| 4429 | Tcl_SetObjResult(interp, Tcl_NewIntObj(i*4)); |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 4430 | return TCL_OK; |
| 4431 | } |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 4432 | |
| 4433 | /* |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 4434 | ** Usage: sqlite_delete_function DB function-name |
| 4435 | ** |
| 4436 | ** Delete the user function 'function-name' from database handle DB. It |
| 4437 | ** is assumed that the user function was created as UTF8, any number of |
| 4438 | ** arguments (the way the TCL interface does it). |
| 4439 | */ |
| 4440 | static int delete_function( |
| 4441 | void * clientData, |
| 4442 | Tcl_Interp *interp, |
| 4443 | int argc, |
| 4444 | char **argv |
| 4445 | ){ |
| 4446 | int rc; |
| 4447 | sqlite3 *db; |
| 4448 | if( argc!=3 ){ |
| 4449 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 4450 | " DB function-name", 0); |
| 4451 | return TCL_ERROR; |
| 4452 | } |
| 4453 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 4454 | 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] | 4455 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 4456 | return TCL_OK; |
| 4457 | } |
| 4458 | |
| 4459 | /* |
| 4460 | ** Usage: sqlite_delete_collation DB collation-name |
| 4461 | ** |
| 4462 | ** Delete the collation sequence 'collation-name' from database handle |
| 4463 | ** DB. It is assumed that the collation sequence was created as UTF8 (the |
| 4464 | ** way the TCL interface does it). |
| 4465 | */ |
| 4466 | static int delete_collation( |
| 4467 | void * clientData, |
| 4468 | Tcl_Interp *interp, |
| 4469 | int argc, |
| 4470 | char **argv |
| 4471 | ){ |
| 4472 | int rc; |
| 4473 | sqlite3 *db; |
| 4474 | if( argc!=3 ){ |
| 4475 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 4476 | " DB function-name", 0); |
| 4477 | return TCL_ERROR; |
| 4478 | } |
| 4479 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 4480 | rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4481 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 4482 | return TCL_OK; |
| 4483 | } |
| 4484 | |
| 4485 | /* |
drh | 3e1d8e6 | 2005-05-26 16:23:34 +0000 | [diff] [blame] | 4486 | ** Usage: sqlite3_get_autocommit DB |
| 4487 | ** |
| 4488 | ** Return true if the database DB is currently in auto-commit mode. |
| 4489 | ** Return false if not. |
| 4490 | */ |
| 4491 | static int get_autocommit( |
| 4492 | void * clientData, |
| 4493 | Tcl_Interp *interp, |
| 4494 | int argc, |
| 4495 | char **argv |
| 4496 | ){ |
| 4497 | char zBuf[30]; |
| 4498 | sqlite3 *db; |
| 4499 | if( argc!=2 ){ |
| 4500 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 4501 | " DB", 0); |
| 4502 | return TCL_ERROR; |
| 4503 | } |
| 4504 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 4505 | sprintf(zBuf, "%d", sqlite3_get_autocommit(db)); |
| 4506 | Tcl_AppendResult(interp, zBuf, 0); |
| 4507 | return TCL_OK; |
| 4508 | } |
| 4509 | |
| 4510 | /* |
drh | 3086765 | 2006-07-06 10:59:57 +0000 | [diff] [blame] | 4511 | ** Usage: sqlite3_busy_timeout DB MS |
| 4512 | ** |
| 4513 | ** Set the busy timeout. This is more easily done using the timeout |
| 4514 | ** method of the TCL interface. But we need a way to test the case |
| 4515 | ** where it returns SQLITE_MISUSE. |
| 4516 | */ |
| 4517 | static int test_busy_timeout( |
| 4518 | void * clientData, |
| 4519 | Tcl_Interp *interp, |
| 4520 | int argc, |
| 4521 | char **argv |
| 4522 | ){ |
| 4523 | int rc, ms; |
| 4524 | sqlite3 *db; |
| 4525 | if( argc!=3 ){ |
| 4526 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 4527 | " DB", 0); |
| 4528 | return TCL_ERROR; |
| 4529 | } |
| 4530 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 4531 | if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR; |
| 4532 | rc = sqlite3_busy_timeout(db, ms); |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 4533 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
drh | 3086765 | 2006-07-06 10:59:57 +0000 | [diff] [blame] | 4534 | return TCL_OK; |
| 4535 | } |
| 4536 | |
| 4537 | /* |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 4538 | ** Usage: tcl_variable_type VARIABLENAME |
| 4539 | ** |
| 4540 | ** Return the name of the internal representation for the |
| 4541 | ** value of the given variable. |
| 4542 | */ |
| 4543 | static int tcl_variable_type( |
| 4544 | void * clientData, |
| 4545 | Tcl_Interp *interp, |
| 4546 | int objc, |
| 4547 | Tcl_Obj *CONST objv[] |
| 4548 | ){ |
| 4549 | Tcl_Obj *pVar; |
| 4550 | if( objc!=2 ){ |
| 4551 | Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE"); |
| 4552 | return TCL_ERROR; |
| 4553 | } |
| 4554 | pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG); |
| 4555 | if( pVar==0 ) return TCL_ERROR; |
| 4556 | if( pVar->typePtr ){ |
| 4557 | Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1)); |
| 4558 | } |
| 4559 | return TCL_OK; |
| 4560 | } |
| 4561 | |
| 4562 | /* |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4563 | ** Usage: sqlite3_release_memory ?N? |
| 4564 | ** |
| 4565 | ** Attempt to release memory currently held but not actually required. |
| 4566 | ** The integer N is the number of bytes we are trying to release. The |
| 4567 | ** return value is the amount of memory actually released. |
| 4568 | */ |
| 4569 | static int test_release_memory( |
| 4570 | void * clientData, |
| 4571 | Tcl_Interp *interp, |
| 4572 | int objc, |
| 4573 | Tcl_Obj *CONST objv[] |
| 4574 | ){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 4575 | #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO) |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4576 | int N; |
| 4577 | int amt; |
| 4578 | if( objc!=1 && objc!=2 ){ |
| 4579 | Tcl_WrongNumArgs(interp, 1, objv, "?N?"); |
| 4580 | return TCL_ERROR; |
| 4581 | } |
| 4582 | if( objc==2 ){ |
| 4583 | if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR; |
| 4584 | }else{ |
| 4585 | N = -1; |
| 4586 | } |
| 4587 | amt = sqlite3_release_memory(N); |
| 4588 | Tcl_SetObjResult(interp, Tcl_NewIntObj(amt)); |
| 4589 | #endif |
| 4590 | return TCL_OK; |
| 4591 | } |
| 4592 | |
drh | 09419b4 | 2011-11-16 19:29:17 +0000 | [diff] [blame] | 4593 | |
| 4594 | /* |
| 4595 | ** Usage: sqlite3_db_release_memory DB |
| 4596 | ** |
| 4597 | ** Attempt to release memory currently held by database DB. Return the |
| 4598 | ** result code (which in the current implementation is always zero). |
| 4599 | */ |
| 4600 | static int test_db_release_memory( |
| 4601 | void * clientData, |
| 4602 | Tcl_Interp *interp, |
| 4603 | int objc, |
| 4604 | Tcl_Obj *CONST objv[] |
| 4605 | ){ |
| 4606 | sqlite3 *db; |
| 4607 | int rc; |
| 4608 | if( objc!=2 ){ |
| 4609 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 4610 | return TCL_ERROR; |
| 4611 | } |
| 4612 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 4613 | rc = sqlite3_db_release_memory(db); |
| 4614 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 4615 | return TCL_OK; |
| 4616 | } |
| 4617 | |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4618 | /* |
drh | 283829c | 2011-11-17 00:56:20 +0000 | [diff] [blame] | 4619 | ** Usage: sqlite3_db_filename DB DBNAME |
| 4620 | ** |
| 4621 | ** Return the name of a file associated with a database. |
| 4622 | */ |
| 4623 | static int test_db_filename( |
| 4624 | void * clientData, |
| 4625 | Tcl_Interp *interp, |
| 4626 | int objc, |
| 4627 | Tcl_Obj *CONST objv[] |
| 4628 | ){ |
| 4629 | sqlite3 *db; |
| 4630 | const char *zDbName; |
| 4631 | if( objc!=3 ){ |
| 4632 | Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); |
| 4633 | return TCL_ERROR; |
| 4634 | } |
| 4635 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 4636 | zDbName = Tcl_GetString(objv[2]); |
| 4637 | Tcl_AppendResult(interp, sqlite3_db_filename(db, zDbName), (void*)0); |
| 4638 | return TCL_OK; |
| 4639 | } |
| 4640 | |
| 4641 | /* |
drh | 421377e | 2012-03-15 21:28:54 +0000 | [diff] [blame] | 4642 | ** Usage: sqlite3_db_readonly DB DBNAME |
| 4643 | ** |
| 4644 | ** Return 1 or 0 if DBNAME is readonly or not. Return -1 if DBNAME does |
| 4645 | ** not exist. |
| 4646 | */ |
| 4647 | static int test_db_readonly( |
| 4648 | void * clientData, |
| 4649 | Tcl_Interp *interp, |
| 4650 | int objc, |
| 4651 | Tcl_Obj *CONST objv[] |
| 4652 | ){ |
| 4653 | sqlite3 *db; |
| 4654 | const char *zDbName; |
| 4655 | if( objc!=3 ){ |
| 4656 | Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); |
| 4657 | return TCL_ERROR; |
| 4658 | } |
| 4659 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 4660 | zDbName = Tcl_GetString(objv[2]); |
| 4661 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_db_readonly(db, zDbName))); |
| 4662 | return TCL_OK; |
| 4663 | } |
| 4664 | |
| 4665 | /* |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4666 | ** Usage: sqlite3_soft_heap_limit ?N? |
| 4667 | ** |
| 4668 | ** Query or set the soft heap limit for the current thread. The |
| 4669 | ** limit is only changed if the N is present. The previous limit |
| 4670 | ** is returned. |
| 4671 | */ |
| 4672 | static int test_soft_heap_limit( |
| 4673 | void * clientData, |
| 4674 | Tcl_Interp *interp, |
| 4675 | int objc, |
| 4676 | Tcl_Obj *CONST objv[] |
| 4677 | ){ |
drh | f82ccf6 | 2010-09-15 17:54:31 +0000 | [diff] [blame] | 4678 | sqlite3_int64 amt; |
drh | b3f787f | 2012-09-29 14:45:54 +0000 | [diff] [blame] | 4679 | Tcl_WideInt N = -1; |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4680 | if( objc!=1 && objc!=2 ){ |
| 4681 | Tcl_WrongNumArgs(interp, 1, objv, "?N?"); |
| 4682 | return TCL_ERROR; |
| 4683 | } |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4684 | if( objc==2 ){ |
drh | f82ccf6 | 2010-09-15 17:54:31 +0000 | [diff] [blame] | 4685 | if( Tcl_GetWideIntFromObj(interp, objv[1], &N) ) return TCL_ERROR; |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4686 | } |
drh | f82ccf6 | 2010-09-15 17:54:31 +0000 | [diff] [blame] | 4687 | amt = sqlite3_soft_heap_limit64(N); |
| 4688 | Tcl_SetObjResult(interp, Tcl_NewWideIntObj(amt)); |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 4689 | return TCL_OK; |
| 4690 | } |
| 4691 | |
| 4692 | /* |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 4693 | ** Usage: sqlite3_thread_cleanup |
| 4694 | ** |
| 4695 | ** Call the sqlite3_thread_cleanup API. |
| 4696 | */ |
| 4697 | static int test_thread_cleanup( |
| 4698 | void * clientData, |
| 4699 | Tcl_Interp *interp, |
| 4700 | int objc, |
| 4701 | Tcl_Obj *CONST objv[] |
| 4702 | ){ |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 4703 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 4704 | sqlite3_thread_cleanup(); |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 4705 | #endif |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 4706 | return TCL_OK; |
| 4707 | } |
| 4708 | |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 4709 | /* |
drh | c6ba55f | 2007-04-05 17:36:18 +0000 | [diff] [blame] | 4710 | ** Usage: sqlite3_pager_refcounts DB |
| 4711 | ** |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4712 | ** Return a list of numbers which are the PagerRefcount for all |
| 4713 | ** pagers on each database connection. |
drh | c6ba55f | 2007-04-05 17:36:18 +0000 | [diff] [blame] | 4714 | */ |
| 4715 | static int test_pager_refcounts( |
| 4716 | void * clientData, |
| 4717 | Tcl_Interp *interp, |
| 4718 | int objc, |
| 4719 | Tcl_Obj *CONST objv[] |
| 4720 | ){ |
| 4721 | sqlite3 *db; |
| 4722 | int i; |
| 4723 | int v, *a; |
| 4724 | Tcl_Obj *pResult; |
| 4725 | |
| 4726 | if( objc!=2 ){ |
| 4727 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4728 | Tcl_GetStringFromObj(objv[0], 0), " DB", 0); |
drh | c6ba55f | 2007-04-05 17:36:18 +0000 | [diff] [blame] | 4729 | return TCL_ERROR; |
| 4730 | } |
| 4731 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 4732 | pResult = Tcl_NewObj(); |
| 4733 | for(i=0; i<db->nDb; i++){ |
| 4734 | if( db->aDb[i].pBt==0 ){ |
| 4735 | v = -1; |
| 4736 | }else{ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 4737 | sqlite3_mutex_enter(db->mutex); |
drh | c6ba55f | 2007-04-05 17:36:18 +0000 | [diff] [blame] | 4738 | a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt)); |
| 4739 | v = a[0]; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 4740 | sqlite3_mutex_leave(db->mutex); |
drh | c6ba55f | 2007-04-05 17:36:18 +0000 | [diff] [blame] | 4741 | } |
| 4742 | Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v)); |
| 4743 | } |
| 4744 | Tcl_SetObjResult(interp, pResult); |
| 4745 | return TCL_OK; |
| 4746 | } |
| 4747 | |
| 4748 | |
| 4749 | /* |
drh | 80788d8 | 2006-09-02 14:50:23 +0000 | [diff] [blame] | 4750 | ** tclcmd: working_64bit_int |
| 4751 | ** |
| 4752 | ** Some TCL builds (ex: cygwin) do not support 64-bit integers. This |
| 4753 | ** leads to a number of test failures. The present command checks the |
| 4754 | ** TCL build to see whether or not it supports 64-bit integers. It |
| 4755 | ** returns TRUE if it does and FALSE if not. |
| 4756 | ** |
| 4757 | ** This command is used to warn users that their TCL build is defective |
| 4758 | ** and that the errors they are seeing in the test scripts might be |
| 4759 | ** a result of their defective TCL rather than problems in SQLite. |
| 4760 | */ |
| 4761 | static int working_64bit_int( |
| 4762 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 4763 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 4764 | int objc, /* Number of arguments */ |
| 4765 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 4766 | ){ |
| 4767 | Tcl_Obj *pTestObj; |
| 4768 | int working = 0; |
| 4769 | |
| 4770 | pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890); |
| 4771 | working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0; |
| 4772 | Tcl_DecrRefCount(pTestObj); |
| 4773 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working)); |
| 4774 | return TCL_OK; |
| 4775 | } |
| 4776 | |
| 4777 | |
| 4778 | /* |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 4779 | ** tclcmd: vfs_unlink_test |
| 4780 | ** |
| 4781 | ** This TCL command unregisters the primary VFS and then registers |
| 4782 | ** it back again. This is used to test the ability to register a |
| 4783 | ** VFS when none are previously registered, and the ability to |
| 4784 | ** unregister the only available VFS. Ticket #2738 |
| 4785 | */ |
| 4786 | static int vfs_unlink_test( |
| 4787 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 4788 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 4789 | int objc, /* Number of arguments */ |
| 4790 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 4791 | ){ |
| 4792 | int i; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4793 | sqlite3_vfs *pMain; |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 4794 | sqlite3_vfs *apVfs[20]; |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4795 | sqlite3_vfs one, two; |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 4796 | |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4797 | sqlite3_vfs_unregister(0); /* Unregister of NULL is harmless */ |
| 4798 | one.zName = "__one"; |
| 4799 | two.zName = "__two"; |
| 4800 | |
| 4801 | /* Calling sqlite3_vfs_register with 2nd argument of 0 does not |
| 4802 | ** change the default VFS |
| 4803 | */ |
| 4804 | pMain = sqlite3_vfs_find(0); |
| 4805 | sqlite3_vfs_register(&one, 0); |
| 4806 | assert( pMain==0 || pMain==sqlite3_vfs_find(0) ); |
| 4807 | sqlite3_vfs_register(&two, 0); |
| 4808 | assert( pMain==0 || pMain==sqlite3_vfs_find(0) ); |
| 4809 | |
| 4810 | /* We can find a VFS by its name */ |
| 4811 | assert( sqlite3_vfs_find("__one")==&one ); |
| 4812 | assert( sqlite3_vfs_find("__two")==&two ); |
| 4813 | |
| 4814 | /* Calling sqlite_vfs_register with non-zero second parameter changes the |
| 4815 | ** default VFS, even if the 1st parameter is an existig VFS that is |
| 4816 | ** previously registered as the non-default. |
| 4817 | */ |
| 4818 | sqlite3_vfs_register(&one, 1); |
| 4819 | assert( sqlite3_vfs_find("__one")==&one ); |
| 4820 | assert( sqlite3_vfs_find("__two")==&two ); |
| 4821 | assert( sqlite3_vfs_find(0)==&one ); |
| 4822 | sqlite3_vfs_register(&two, 1); |
| 4823 | assert( sqlite3_vfs_find("__one")==&one ); |
| 4824 | assert( sqlite3_vfs_find("__two")==&two ); |
| 4825 | assert( sqlite3_vfs_find(0)==&two ); |
| 4826 | if( pMain ){ |
| 4827 | sqlite3_vfs_register(pMain, 1); |
| 4828 | assert( sqlite3_vfs_find("__one")==&one ); |
| 4829 | assert( sqlite3_vfs_find("__two")==&two ); |
| 4830 | assert( sqlite3_vfs_find(0)==pMain ); |
| 4831 | } |
| 4832 | |
| 4833 | /* Unlink the default VFS. Repeat until there are no more VFSes |
| 4834 | ** registered. |
| 4835 | */ |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 4836 | for(i=0; i<sizeof(apVfs)/sizeof(apVfs[0]); i++){ |
| 4837 | apVfs[i] = sqlite3_vfs_find(0); |
| 4838 | if( apVfs[i] ){ |
| 4839 | assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) ); |
| 4840 | sqlite3_vfs_unregister(apVfs[i]); |
| 4841 | assert( 0==sqlite3_vfs_find(apVfs[i]->zName) ); |
| 4842 | } |
| 4843 | } |
| 4844 | assert( 0==sqlite3_vfs_find(0) ); |
mlcreech | 1f04533 | 2008-04-08 03:07:54 +0000 | [diff] [blame] | 4845 | |
| 4846 | /* Register the main VFS as non-default (will be made default, since |
| 4847 | ** it'll be the only one in existence). |
| 4848 | */ |
| 4849 | sqlite3_vfs_register(pMain, 0); |
| 4850 | assert( sqlite3_vfs_find(0)==pMain ); |
| 4851 | |
| 4852 | /* Un-register the main VFS again to restore an empty VFS list */ |
| 4853 | sqlite3_vfs_unregister(pMain); |
| 4854 | assert( 0==sqlite3_vfs_find(0) ); |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4855 | |
| 4856 | /* Relink all VFSes in reverse order. */ |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 4857 | for(i=sizeof(apVfs)/sizeof(apVfs[0])-1; i>=0; i--){ |
| 4858 | if( apVfs[i] ){ |
| 4859 | sqlite3_vfs_register(apVfs[i], 1); |
| 4860 | assert( apVfs[i]==sqlite3_vfs_find(0) ); |
| 4861 | assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) ); |
| 4862 | } |
| 4863 | } |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 4864 | |
| 4865 | /* Unregister out sample VFSes. */ |
| 4866 | sqlite3_vfs_unregister(&one); |
| 4867 | sqlite3_vfs_unregister(&two); |
| 4868 | |
| 4869 | /* Unregistering a VFS that is not currently registered is harmless */ |
| 4870 | sqlite3_vfs_unregister(&one); |
| 4871 | sqlite3_vfs_unregister(&two); |
| 4872 | assert( sqlite3_vfs_find("__one")==0 ); |
| 4873 | assert( sqlite3_vfs_find("__two")==0 ); |
| 4874 | |
| 4875 | /* We should be left with the original default VFS back as the |
| 4876 | ** original */ |
| 4877 | assert( sqlite3_vfs_find(0)==pMain ); |
| 4878 | |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 4879 | return TCL_OK; |
| 4880 | } |
| 4881 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 4882 | /* |
drh | c8d7567 | 2008-07-08 02:12:37 +0000 | [diff] [blame] | 4883 | ** tclcmd: vfs_initfail_test |
| 4884 | ** |
| 4885 | ** This TCL command attempts to vfs_find and vfs_register when the |
| 4886 | ** sqlite3_initialize() interface is failing. All calls should fail. |
| 4887 | */ |
| 4888 | static int vfs_initfail_test( |
| 4889 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 4890 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 4891 | int objc, /* Number of arguments */ |
| 4892 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 4893 | ){ |
| 4894 | sqlite3_vfs one; |
| 4895 | one.zName = "__one"; |
| 4896 | |
| 4897 | if( sqlite3_vfs_find(0) ) return TCL_ERROR; |
| 4898 | sqlite3_vfs_register(&one, 0); |
| 4899 | if( sqlite3_vfs_find(0) ) return TCL_ERROR; |
| 4900 | sqlite3_vfs_register(&one, 1); |
| 4901 | if( sqlite3_vfs_find(0) ) return TCL_ERROR; |
| 4902 | return TCL_OK; |
| 4903 | } |
| 4904 | |
| 4905 | /* |
drh | a282097 | 2008-07-07 13:31:58 +0000 | [diff] [blame] | 4906 | ** Saved VFSes |
| 4907 | */ |
| 4908 | static sqlite3_vfs *apVfs[20]; |
| 4909 | static int nVfs = 0; |
| 4910 | |
| 4911 | /* |
| 4912 | ** tclcmd: vfs_unregister_all |
| 4913 | ** |
| 4914 | ** Unregister all VFSes. |
| 4915 | */ |
| 4916 | static int vfs_unregister_all( |
| 4917 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 4918 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 4919 | int objc, /* Number of arguments */ |
| 4920 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 4921 | ){ |
| 4922 | int i; |
| 4923 | for(i=0; i<ArraySize(apVfs); i++){ |
| 4924 | apVfs[i] = sqlite3_vfs_find(0); |
| 4925 | if( apVfs[i]==0 ) break; |
| 4926 | sqlite3_vfs_unregister(apVfs[i]); |
| 4927 | } |
| 4928 | nVfs = i; |
| 4929 | return TCL_OK; |
| 4930 | } |
| 4931 | /* |
| 4932 | ** tclcmd: vfs_reregister_all |
| 4933 | ** |
| 4934 | ** Restore all VFSes that were removed using vfs_unregister_all |
| 4935 | */ |
| 4936 | static int vfs_reregister_all( |
| 4937 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 4938 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 4939 | int objc, /* Number of arguments */ |
| 4940 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 4941 | ){ |
| 4942 | int i; |
| 4943 | for(i=0; i<nVfs; i++){ |
| 4944 | sqlite3_vfs_register(apVfs[i], i==0); |
| 4945 | } |
| 4946 | return TCL_OK; |
| 4947 | } |
| 4948 | |
| 4949 | |
| 4950 | /* |
drh | 5517625 | 2008-01-22 14:50:16 +0000 | [diff] [blame] | 4951 | ** tclcmd: file_control_test DB |
| 4952 | ** |
| 4953 | ** This TCL command runs the sqlite3_file_control interface and |
| 4954 | ** verifies correct operation of the same. |
| 4955 | */ |
| 4956 | static int file_control_test( |
| 4957 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 4958 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 4959 | int objc, /* Number of arguments */ |
| 4960 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 4961 | ){ |
| 4962 | int iArg = 0; |
| 4963 | sqlite3 *db; |
| 4964 | int rc; |
| 4965 | |
| 4966 | if( objc!=2 ){ |
| 4967 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4968 | Tcl_GetStringFromObj(objv[0], 0), " DB", 0); |
| 4969 | return TCL_ERROR; |
| 4970 | } |
| 4971 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 4972 | rc = sqlite3_file_control(db, 0, 0, &iArg); |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 4973 | assert( rc==SQLITE_NOTFOUND ); |
drh | 5517625 | 2008-01-22 14:50:16 +0000 | [diff] [blame] | 4974 | rc = sqlite3_file_control(db, "notadatabase", SQLITE_FCNTL_LOCKSTATE, &iArg); |
| 4975 | assert( rc==SQLITE_ERROR ); |
| 4976 | rc = sqlite3_file_control(db, "main", -1, &iArg); |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 4977 | assert( rc==SQLITE_NOTFOUND ); |
drh | 5517625 | 2008-01-22 14:50:16 +0000 | [diff] [blame] | 4978 | rc = sqlite3_file_control(db, "temp", -1, &iArg); |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 4979 | assert( rc==SQLITE_NOTFOUND || rc==SQLITE_ERROR ); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4980 | |
| 4981 | return TCL_OK; |
| 4982 | } |
| 4983 | |
| 4984 | |
| 4985 | /* |
| 4986 | ** tclcmd: file_control_lasterrno_test DB |
| 4987 | ** |
| 4988 | ** This TCL command runs the sqlite3_file_control interface and |
| 4989 | ** verifies correct operation of the SQLITE_LAST_ERRNO verb. |
| 4990 | */ |
| 4991 | static int file_control_lasterrno_test( |
| 4992 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 4993 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 4994 | int objc, /* Number of arguments */ |
| 4995 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 4996 | ){ |
| 4997 | int iArg = 0; |
| 4998 | sqlite3 *db; |
| 4999 | int rc; |
| 5000 | |
| 5001 | if( objc!=2 ){ |
| 5002 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5003 | Tcl_GetStringFromObj(objv[0], 0), " DB", 0); |
| 5004 | return TCL_ERROR; |
| 5005 | } |
shane | 9db299f | 2009-01-30 05:59:10 +0000 | [diff] [blame] | 5006 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 5007 | return TCL_ERROR; |
| 5008 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5009 | rc = sqlite3_file_control(db, NULL, SQLITE_LAST_ERRNO, &iArg); |
shane | 9db299f | 2009-01-30 05:59:10 +0000 | [diff] [blame] | 5010 | if( rc ){ |
| 5011 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 5012 | return TCL_ERROR; |
| 5013 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5014 | if( iArg!=0 ) { |
| 5015 | Tcl_AppendResult(interp, "Unexpected non-zero errno: ", |
| 5016 | Tcl_GetStringFromObj(Tcl_NewIntObj(iArg), 0), " ", 0); |
| 5017 | return TCL_ERROR; |
| 5018 | } |
drh | 5517625 | 2008-01-22 14:50:16 +0000 | [diff] [blame] | 5019 | return TCL_OK; |
| 5020 | } |
| 5021 | |
| 5022 | /* |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 5023 | ** tclcmd: file_control_chunksize_test DB DBNAME SIZE |
| 5024 | ** |
| 5025 | ** This TCL command runs the sqlite3_file_control interface and |
| 5026 | ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and |
| 5027 | ** SQLITE_SET_LOCKPROXYFILE verbs. |
| 5028 | */ |
| 5029 | static int file_control_chunksize_test( |
| 5030 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5031 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5032 | int objc, /* Number of arguments */ |
| 5033 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5034 | ){ |
| 5035 | int nSize; /* New chunk size */ |
| 5036 | char *zDb; /* Db name ("main", "temp" etc.) */ |
| 5037 | sqlite3 *db; /* Database handle */ |
| 5038 | int rc; /* file_control() return code */ |
| 5039 | |
| 5040 | if( objc!=4 ){ |
| 5041 | Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE"); |
| 5042 | return TCL_ERROR; |
| 5043 | } |
| 5044 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) |
| 5045 | || Tcl_GetIntFromObj(interp, objv[3], &nSize) |
| 5046 | ){ |
| 5047 | return TCL_ERROR; |
| 5048 | } |
| 5049 | zDb = Tcl_GetString(objv[2]); |
| 5050 | if( zDb[0]=='\0' ) zDb = NULL; |
| 5051 | |
| 5052 | rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_CHUNK_SIZE, (void *)&nSize); |
| 5053 | if( rc ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 5054 | Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 5055 | return TCL_ERROR; |
| 5056 | } |
| 5057 | return TCL_OK; |
| 5058 | } |
| 5059 | |
| 5060 | /* |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 5061 | ** tclcmd: file_control_sizehint_test DB DBNAME SIZE |
| 5062 | ** |
drh | fdd7f71 | 2011-08-13 10:47:51 +0000 | [diff] [blame] | 5063 | ** This TCL command runs the sqlite3_file_control interface |
| 5064 | ** with SQLITE_FCNTL_SIZE_HINT |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 5065 | */ |
| 5066 | static int file_control_sizehint_test( |
| 5067 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5068 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5069 | int objc, /* Number of arguments */ |
| 5070 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5071 | ){ |
drh | b3f787f | 2012-09-29 14:45:54 +0000 | [diff] [blame] | 5072 | Tcl_WideInt nSize; /* Hinted size */ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 5073 | char *zDb; /* Db name ("main", "temp" etc.) */ |
| 5074 | sqlite3 *db; /* Database handle */ |
| 5075 | int rc; /* file_control() return code */ |
| 5076 | |
| 5077 | if( objc!=4 ){ |
| 5078 | Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE"); |
| 5079 | return TCL_ERROR; |
| 5080 | } |
| 5081 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) |
| 5082 | || Tcl_GetWideIntFromObj(interp, objv[3], &nSize) |
| 5083 | ){ |
| 5084 | return TCL_ERROR; |
| 5085 | } |
| 5086 | zDb = Tcl_GetString(objv[2]); |
| 5087 | if( zDb[0]=='\0' ) zDb = NULL; |
| 5088 | |
| 5089 | rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_SIZE_HINT, (void *)&nSize); |
| 5090 | if( rc ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 5091 | Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 5092 | return TCL_ERROR; |
| 5093 | } |
| 5094 | return TCL_OK; |
| 5095 | } |
| 5096 | |
| 5097 | /* |
drh | ad24581 | 2010-06-01 00:28:42 +0000 | [diff] [blame] | 5098 | ** tclcmd: file_control_lockproxy_test DB PWD |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5099 | ** |
| 5100 | ** This TCL command runs the sqlite3_file_control interface and |
| 5101 | ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and |
| 5102 | ** SQLITE_SET_LOCKPROXYFILE verbs. |
| 5103 | */ |
| 5104 | static int file_control_lockproxy_test( |
| 5105 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5106 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5107 | int objc, /* Number of arguments */ |
| 5108 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5109 | ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5110 | sqlite3 *db; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5111 | |
drh | ad24581 | 2010-06-01 00:28:42 +0000 | [diff] [blame] | 5112 | if( objc!=3 ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5113 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
drh | ad24581 | 2010-06-01 00:28:42 +0000 | [diff] [blame] | 5114 | Tcl_GetStringFromObj(objv[0], 0), " DB PWD", 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5115 | return TCL_ERROR; |
| 5116 | } |
shane | 9db299f | 2009-01-30 05:59:10 +0000 | [diff] [blame] | 5117 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 5118 | return TCL_ERROR; |
| 5119 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5120 | |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 5121 | #if !defined(SQLITE_ENABLE_LOCKING_STYLE) |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5122 | # if defined(__APPLE__) |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 5123 | # define SQLITE_ENABLE_LOCKING_STYLE 1 |
| 5124 | # else |
| 5125 | # define SQLITE_ENABLE_LOCKING_STYLE 0 |
| 5126 | # endif |
| 5127 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5128 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5129 | { |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5130 | char *testPath; |
drh | 103fe74 | 2008-12-11 02:56:07 +0000 | [diff] [blame] | 5131 | int rc; |
drh | caffb1a | 2012-01-30 18:00:31 +0000 | [diff] [blame] | 5132 | int nPwd; |
| 5133 | const char *zPwd; |
drh | ad24581 | 2010-06-01 00:28:42 +0000 | [diff] [blame] | 5134 | char proxyPath[400]; |
| 5135 | |
drh | caffb1a | 2012-01-30 18:00:31 +0000 | [diff] [blame] | 5136 | zPwd = Tcl_GetStringFromObj(objv[2], &nPwd); |
drh | ad24581 | 2010-06-01 00:28:42 +0000 | [diff] [blame] | 5137 | if( sizeof(proxyPath)<nPwd+20 ){ |
| 5138 | Tcl_AppendResult(interp, "PWD too big", (void*)0); |
| 5139 | return TCL_ERROR; |
| 5140 | } |
| 5141 | sprintf(proxyPath, "%s/test.proxy", zPwd); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5142 | rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath); |
| 5143 | if( rc ){ |
shane | 9db299f | 2009-01-30 05:59:10 +0000 | [diff] [blame] | 5144 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 5145 | return TCL_ERROR; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5146 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5147 | rc = sqlite3_file_control(db, NULL, SQLITE_GET_LOCKPROXYFILE, &testPath); |
shane | 9db299f | 2009-01-30 05:59:10 +0000 | [diff] [blame] | 5148 | if( strncmp(proxyPath,testPath,11) ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5149 | Tcl_AppendResult(interp, "Lock proxy file did not match the " |
| 5150 | "previously assigned value", 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5151 | return TCL_ERROR; |
| 5152 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5153 | if( rc ){ |
| 5154 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 5155 | return TCL_ERROR; |
| 5156 | } |
| 5157 | rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath); |
| 5158 | if( rc ){ |
| 5159 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 5160 | return TCL_ERROR; |
| 5161 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5162 | } |
| 5163 | #endif |
| 5164 | return TCL_OK; |
| 5165 | } |
| 5166 | |
drh | d0cdf01 | 2011-07-13 16:03:46 +0000 | [diff] [blame] | 5167 | /* |
| 5168 | ** tclcmd: file_control_win32_av_retry DB NRETRY DELAY |
| 5169 | ** |
| 5170 | ** This TCL command runs the sqlite3_file_control interface with |
| 5171 | ** the SQLITE_FCNTL_WIN32_AV_RETRY opcode. |
| 5172 | */ |
| 5173 | static int file_control_win32_av_retry( |
| 5174 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5175 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5176 | int objc, /* Number of arguments */ |
| 5177 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5178 | ){ |
| 5179 | sqlite3 *db; |
| 5180 | int rc; |
| 5181 | int a[2]; |
| 5182 | char z[100]; |
| 5183 | |
| 5184 | if( objc!=4 ){ |
| 5185 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5186 | Tcl_GetStringFromObj(objv[0], 0), " DB NRETRY DELAY", 0); |
| 5187 | return TCL_ERROR; |
| 5188 | } |
| 5189 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 5190 | return TCL_ERROR; |
| 5191 | } |
| 5192 | if( Tcl_GetIntFromObj(interp, objv[2], &a[0]) ) return TCL_ERROR; |
| 5193 | if( Tcl_GetIntFromObj(interp, objv[3], &a[1]) ) return TCL_ERROR; |
| 5194 | rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_AV_RETRY, (void*)a); |
| 5195 | sqlite3_snprintf(sizeof(z), z, "%d %d %d", rc, a[0], a[1]); |
| 5196 | Tcl_AppendResult(interp, z, (char*)0); |
| 5197 | return TCL_OK; |
| 5198 | } |
| 5199 | |
drh | 253cea5 | 2011-07-26 16:23:25 +0000 | [diff] [blame] | 5200 | /* |
| 5201 | ** tclcmd: file_control_persist_wal DB PERSIST-FLAG |
| 5202 | ** |
| 5203 | ** This TCL command runs the sqlite3_file_control interface with |
| 5204 | ** the SQLITE_FCNTL_PERSIST_WAL opcode. |
| 5205 | */ |
| 5206 | static int file_control_persist_wal( |
| 5207 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5208 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5209 | int objc, /* Number of arguments */ |
| 5210 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5211 | ){ |
| 5212 | sqlite3 *db; |
| 5213 | int rc; |
| 5214 | int bPersist; |
| 5215 | char z[100]; |
| 5216 | |
| 5217 | if( objc!=3 ){ |
| 5218 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5219 | Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0); |
| 5220 | return TCL_ERROR; |
| 5221 | } |
| 5222 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 5223 | return TCL_ERROR; |
| 5224 | } |
| 5225 | if( Tcl_GetIntFromObj(interp, objv[2], &bPersist) ) return TCL_ERROR; |
| 5226 | rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_PERSIST_WAL, (void*)&bPersist); |
| 5227 | sqlite3_snprintf(sizeof(z), z, "%d %d", rc, bPersist); |
| 5228 | Tcl_AppendResult(interp, z, (char*)0); |
| 5229 | return TCL_OK; |
| 5230 | } |
| 5231 | |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 5232 | /* |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 5233 | ** tclcmd: file_control_powersafe_overwrite DB PSOW-FLAG |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 5234 | ** |
| 5235 | ** This TCL command runs the sqlite3_file_control interface with |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 5236 | ** the SQLITE_FCNTL_POWERSAFE_OVERWRITE opcode. |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 5237 | */ |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 5238 | static int file_control_powersafe_overwrite( |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 5239 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5240 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5241 | int objc, /* Number of arguments */ |
| 5242 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5243 | ){ |
| 5244 | sqlite3 *db; |
| 5245 | int rc; |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 5246 | int b; |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 5247 | char z[100]; |
| 5248 | |
| 5249 | if( objc!=3 ){ |
| 5250 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5251 | Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0); |
| 5252 | return TCL_ERROR; |
| 5253 | } |
| 5254 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 5255 | return TCL_ERROR; |
| 5256 | } |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 5257 | if( Tcl_GetIntFromObj(interp, objv[2], &b) ) return TCL_ERROR; |
| 5258 | rc = sqlite3_file_control(db,NULL,SQLITE_FCNTL_POWERSAFE_OVERWRITE,(void*)&b); |
| 5259 | sqlite3_snprintf(sizeof(z), z, "%d %d", rc, b); |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 5260 | Tcl_AppendResult(interp, z, (char*)0); |
| 5261 | return TCL_OK; |
| 5262 | } |
| 5263 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5264 | |
| 5265 | /* |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 5266 | ** tclcmd: file_control_vfsname DB ?AUXDB? |
| 5267 | ** |
| 5268 | ** Return a string that describes the stack of VFSes. |
| 5269 | */ |
| 5270 | static int file_control_vfsname( |
| 5271 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5272 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5273 | int objc, /* Number of arguments */ |
| 5274 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5275 | ){ |
| 5276 | sqlite3 *db; |
| 5277 | const char *zDbName = "main"; |
| 5278 | char *zVfsName = 0; |
| 5279 | |
| 5280 | if( objc!=2 && objc!=3 ){ |
| 5281 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5282 | Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0); |
| 5283 | return TCL_ERROR; |
| 5284 | } |
| 5285 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 5286 | return TCL_ERROR; |
| 5287 | } |
| 5288 | if( objc==3 ){ |
| 5289 | zDbName = Tcl_GetString(objv[2]); |
| 5290 | } |
| 5291 | sqlite3_file_control(db, zDbName, SQLITE_FCNTL_VFSNAME,(void*)&zVfsName); |
| 5292 | Tcl_AppendResult(interp, zVfsName, (char*)0); |
| 5293 | sqlite3_free(zVfsName); |
| 5294 | return TCL_OK; |
| 5295 | } |
| 5296 | |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 5297 | /* |
| 5298 | ** tclcmd: file_control_tempfilename DB ?AUXDB? |
| 5299 | ** |
| 5300 | ** Return a string that is a temporary filename |
| 5301 | */ |
| 5302 | static int file_control_tempfilename( |
| 5303 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5304 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5305 | int objc, /* Number of arguments */ |
| 5306 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5307 | ){ |
| 5308 | sqlite3 *db; |
| 5309 | const char *zDbName = "main"; |
| 5310 | char *zTName = 0; |
| 5311 | |
| 5312 | if( objc!=2 && objc!=3 ){ |
| 5313 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5314 | Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0); |
| 5315 | return TCL_ERROR; |
| 5316 | } |
| 5317 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 5318 | return TCL_ERROR; |
| 5319 | } |
| 5320 | if( objc==3 ){ |
| 5321 | zDbName = Tcl_GetString(objv[2]); |
| 5322 | } |
| 5323 | sqlite3_file_control(db, zDbName, SQLITE_FCNTL_TEMPFILENAME, (void*)&zTName); |
| 5324 | Tcl_AppendResult(interp, zTName, (char*)0); |
| 5325 | sqlite3_free(zTName); |
| 5326 | return TCL_OK; |
| 5327 | } |
| 5328 | |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 5329 | |
| 5330 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5331 | ** tclcmd: sqlite3_vfs_list |
| 5332 | ** |
| 5333 | ** Return a tcl list containing the names of all registered vfs's. |
| 5334 | */ |
| 5335 | static int vfs_list( |
| 5336 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5337 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5338 | int objc, /* Number of arguments */ |
| 5339 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5340 | ){ |
| 5341 | sqlite3_vfs *pVfs; |
| 5342 | Tcl_Obj *pRet = Tcl_NewObj(); |
| 5343 | if( objc!=1 ){ |
| 5344 | Tcl_WrongNumArgs(interp, 1, objv, ""); |
| 5345 | return TCL_ERROR; |
| 5346 | } |
| 5347 | for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ |
| 5348 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(pVfs->zName, -1)); |
| 5349 | } |
| 5350 | Tcl_SetObjResult(interp, pRet); |
| 5351 | return TCL_OK; |
| 5352 | } |
| 5353 | |
| 5354 | /* |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 5355 | ** tclcmd: sqlite3_limit DB ID VALUE |
| 5356 | ** |
| 5357 | ** This TCL command runs the sqlite3_limit interface and |
| 5358 | ** verifies correct operation of the same. |
| 5359 | */ |
| 5360 | static int test_limit( |
| 5361 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5362 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5363 | int objc, /* Number of arguments */ |
| 5364 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5365 | ){ |
| 5366 | sqlite3 *db; |
| 5367 | int rc; |
| 5368 | static const struct { |
| 5369 | char *zName; |
| 5370 | int id; |
| 5371 | } aId[] = { |
| 5372 | { "SQLITE_LIMIT_LENGTH", SQLITE_LIMIT_LENGTH }, |
| 5373 | { "SQLITE_LIMIT_SQL_LENGTH", SQLITE_LIMIT_SQL_LENGTH }, |
| 5374 | { "SQLITE_LIMIT_COLUMN", SQLITE_LIMIT_COLUMN }, |
| 5375 | { "SQLITE_LIMIT_EXPR_DEPTH", SQLITE_LIMIT_EXPR_DEPTH }, |
| 5376 | { "SQLITE_LIMIT_COMPOUND_SELECT", SQLITE_LIMIT_COMPOUND_SELECT }, |
| 5377 | { "SQLITE_LIMIT_VDBE_OP", SQLITE_LIMIT_VDBE_OP }, |
| 5378 | { "SQLITE_LIMIT_FUNCTION_ARG", SQLITE_LIMIT_FUNCTION_ARG }, |
| 5379 | { "SQLITE_LIMIT_ATTACHED", SQLITE_LIMIT_ATTACHED }, |
| 5380 | { "SQLITE_LIMIT_LIKE_PATTERN_LENGTH", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, |
| 5381 | { "SQLITE_LIMIT_VARIABLE_NUMBER", SQLITE_LIMIT_VARIABLE_NUMBER }, |
drh | 417168a | 2009-09-07 18:14:02 +0000 | [diff] [blame] | 5382 | { "SQLITE_LIMIT_TRIGGER_DEPTH", SQLITE_LIMIT_TRIGGER_DEPTH }, |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 5383 | |
| 5384 | /* Out of range test cases */ |
| 5385 | { "SQLITE_LIMIT_TOOSMALL", -1, }, |
drh | 417168a | 2009-09-07 18:14:02 +0000 | [diff] [blame] | 5386 | { "SQLITE_LIMIT_TOOBIG", SQLITE_LIMIT_TRIGGER_DEPTH+1 }, |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 5387 | }; |
| 5388 | int i, id; |
| 5389 | int val; |
| 5390 | const char *zId; |
| 5391 | |
| 5392 | if( objc!=4 ){ |
| 5393 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5394 | Tcl_GetStringFromObj(objv[0], 0), " DB ID VALUE", 0); |
| 5395 | return TCL_ERROR; |
| 5396 | } |
| 5397 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 5398 | zId = Tcl_GetString(objv[2]); |
| 5399 | for(i=0; i<sizeof(aId)/sizeof(aId[0]); i++){ |
| 5400 | if( strcmp(zId, aId[i].zName)==0 ){ |
| 5401 | id = aId[i].id; |
| 5402 | break; |
| 5403 | } |
| 5404 | } |
| 5405 | if( i>=sizeof(aId)/sizeof(aId[0]) ){ |
| 5406 | Tcl_AppendResult(interp, "unknown limit type: ", zId, (char*)0); |
| 5407 | return TCL_ERROR; |
| 5408 | } |
| 5409 | if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR; |
| 5410 | rc = sqlite3_limit(db, id, val); |
| 5411 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 5412 | return TCL_OK; |
| 5413 | } |
| 5414 | |
| 5415 | /* |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 5416 | ** tclcmd: save_prng_state |
drh | a282097 | 2008-07-07 13:31:58 +0000 | [diff] [blame] | 5417 | ** |
| 5418 | ** Save the state of the pseudo-random number generator. |
| 5419 | ** At the same time, verify that sqlite3_test_control works even when |
| 5420 | ** called with an out-of-range opcode. |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 5421 | */ |
| 5422 | static int save_prng_state( |
| 5423 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5424 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5425 | int objc, /* Number of arguments */ |
| 5426 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5427 | ){ |
drh | a282097 | 2008-07-07 13:31:58 +0000 | [diff] [blame] | 5428 | int rc = sqlite3_test_control(9999); |
| 5429 | assert( rc==0 ); |
| 5430 | rc = sqlite3_test_control(-1); |
| 5431 | assert( rc==0 ); |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 5432 | sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SAVE); |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 5433 | return TCL_OK; |
| 5434 | } |
| 5435 | /* |
| 5436 | ** tclcmd: restore_prng_state |
| 5437 | */ |
| 5438 | static int restore_prng_state( |
| 5439 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5440 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5441 | int objc, /* Number of arguments */ |
| 5442 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5443 | ){ |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 5444 | sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESTORE); |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 5445 | return TCL_OK; |
| 5446 | } |
| 5447 | /* |
| 5448 | ** tclcmd: reset_prng_state |
| 5449 | */ |
| 5450 | static int reset_prng_state( |
| 5451 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5452 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5453 | int objc, /* Number of arguments */ |
| 5454 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5455 | ){ |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 5456 | sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESET); |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 5457 | return TCL_OK; |
| 5458 | } |
| 5459 | |
danielk1977 | 062d4cb | 2008-08-29 09:10:02 +0000 | [diff] [blame] | 5460 | /* |
| 5461 | ** tclcmd: pcache_stats |
| 5462 | */ |
| 5463 | static int test_pcache_stats( |
| 5464 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 5465 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5466 | int objc, /* Number of arguments */ |
| 5467 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5468 | ){ |
| 5469 | int nMin; |
| 5470 | int nMax; |
| 5471 | int nCurrent; |
| 5472 | int nRecyclable; |
| 5473 | Tcl_Obj *pRet; |
| 5474 | |
| 5475 | sqlite3PcacheStats(&nCurrent, &nMax, &nMin, &nRecyclable); |
| 5476 | |
| 5477 | pRet = Tcl_NewObj(); |
| 5478 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("current", -1)); |
| 5479 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCurrent)); |
| 5480 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("max", -1)); |
| 5481 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMax)); |
| 5482 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("min", -1)); |
| 5483 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMin)); |
| 5484 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("recyclable", -1)); |
| 5485 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nRecyclable)); |
| 5486 | |
| 5487 | Tcl_SetObjResult(interp, pRet); |
| 5488 | |
| 5489 | return TCL_OK; |
| 5490 | } |
| 5491 | |
drh | 69910da | 2009-03-27 12:32:54 +0000 | [diff] [blame] | 5492 | #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 5493 | static void test_unlock_notify_cb(void **aArg, int nArg){ |
| 5494 | int ii; |
| 5495 | for(ii=0; ii<nArg; ii++){ |
| 5496 | Tcl_EvalEx((Tcl_Interp *)aArg[ii], "unlock_notify", -1, TCL_EVAL_GLOBAL); |
| 5497 | } |
| 5498 | } |
drh | 69910da | 2009-03-27 12:32:54 +0000 | [diff] [blame] | 5499 | #endif /* SQLITE_ENABLE_UNLOCK_NOTIFY */ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 5500 | |
| 5501 | /* |
| 5502 | ** tclcmd: sqlite3_unlock_notify db |
| 5503 | */ |
| 5504 | #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY |
| 5505 | static int test_unlock_notify( |
| 5506 | ClientData clientData, /* Unused */ |
| 5507 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5508 | int objc, /* Number of arguments */ |
| 5509 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5510 | ){ |
| 5511 | sqlite3 *db; |
| 5512 | int rc; |
| 5513 | |
| 5514 | if( objc!=2 ){ |
| 5515 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 5516 | return TCL_ERROR; |
| 5517 | } |
| 5518 | |
| 5519 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 5520 | return TCL_ERROR; |
| 5521 | } |
| 5522 | rc = sqlite3_unlock_notify(db, test_unlock_notify_cb, (void *)interp); |
| 5523 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
| 5524 | return TCL_OK; |
| 5525 | } |
| 5526 | #endif |
| 5527 | |
dan | 87c1fe1 | 2010-05-03 12:14:15 +0000 | [diff] [blame] | 5528 | /* |
| 5529 | ** tclcmd: sqlite3_wal_checkpoint db ?NAME? |
| 5530 | */ |
| 5531 | static int test_wal_checkpoint( |
| 5532 | ClientData clientData, /* Unused */ |
| 5533 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5534 | int objc, /* Number of arguments */ |
| 5535 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5536 | ){ |
| 5537 | char *zDb = 0; |
| 5538 | sqlite3 *db; |
| 5539 | int rc; |
| 5540 | |
| 5541 | if( objc!=3 && objc!=2 ){ |
| 5542 | Tcl_WrongNumArgs(interp, 1, objv, "DB ?NAME?"); |
| 5543 | return TCL_ERROR; |
| 5544 | } |
| 5545 | |
| 5546 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 5547 | return TCL_ERROR; |
| 5548 | } |
| 5549 | if( objc==3 ){ |
| 5550 | zDb = Tcl_GetString(objv[2]); |
| 5551 | } |
| 5552 | rc = sqlite3_wal_checkpoint(db, zDb); |
| 5553 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
| 5554 | return TCL_OK; |
| 5555 | } |
| 5556 | |
dan | eb8763d | 2010-08-17 14:52:22 +0000 | [diff] [blame] | 5557 | /* |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 5558 | ** tclcmd: sqlite3_wal_checkpoint_v2 db MODE ?NAME? |
| 5559 | ** |
| 5560 | ** This command calls the wal_checkpoint_v2() function with the specified |
| 5561 | ** mode argument (passive, full or restart). If present, the database name |
| 5562 | ** NAME is passed as the second argument to wal_checkpoint_v2(). If it the |
| 5563 | ** NAME argument is not present, a NULL pointer is passed instead. |
| 5564 | ** |
| 5565 | ** If wal_checkpoint_v2() returns any value other than SQLITE_BUSY or |
| 5566 | ** SQLITE_OK, then this command returns TCL_ERROR. The Tcl result is set |
| 5567 | ** to the error message obtained from sqlite3_errmsg(). |
| 5568 | ** |
| 5569 | ** Otherwise, this command returns a list of three integers. The first integer |
| 5570 | ** is 1 if SQLITE_BUSY was returned, or 0 otherwise. The following two integers |
drh | f7b5496 | 2013-05-28 12:11:54 +0000 | [diff] [blame] | 5571 | ** are the values returned via the output parameters by wal_checkpoint_v2() - |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 5572 | ** the number of frames in the log and the number of frames in the log |
| 5573 | ** that have been checkpointed. |
| 5574 | */ |
| 5575 | static int test_wal_checkpoint_v2( |
| 5576 | ClientData clientData, /* Unused */ |
| 5577 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5578 | int objc, /* Number of arguments */ |
| 5579 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5580 | ){ |
| 5581 | char *zDb = 0; |
| 5582 | sqlite3 *db; |
| 5583 | int rc; |
| 5584 | |
| 5585 | int eMode; |
| 5586 | int nLog = -555; |
| 5587 | int nCkpt = -555; |
| 5588 | Tcl_Obj *pRet; |
| 5589 | |
| 5590 | const char * aMode[] = { "passive", "full", "restart", 0 }; |
| 5591 | assert( SQLITE_CHECKPOINT_PASSIVE==0 ); |
| 5592 | assert( SQLITE_CHECKPOINT_FULL==1 ); |
| 5593 | assert( SQLITE_CHECKPOINT_RESTART==2 ); |
| 5594 | |
| 5595 | if( objc!=3 && objc!=4 ){ |
| 5596 | Tcl_WrongNumArgs(interp, 1, objv, "DB MODE ?NAME?"); |
| 5597 | return TCL_ERROR; |
| 5598 | } |
| 5599 | |
| 5600 | if( objc==4 ){ |
| 5601 | zDb = Tcl_GetString(objv[3]); |
| 5602 | } |
| 5603 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) |
| 5604 | || Tcl_GetIndexFromObj(interp, objv[2], aMode, "mode", 0, &eMode) |
| 5605 | ){ |
| 5606 | return TCL_ERROR; |
| 5607 | } |
| 5608 | |
| 5609 | rc = sqlite3_wal_checkpoint_v2(db, zDb, eMode, &nLog, &nCkpt); |
| 5610 | if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ |
| 5611 | Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE); |
| 5612 | return TCL_ERROR; |
| 5613 | } |
| 5614 | |
| 5615 | pRet = Tcl_NewObj(); |
| 5616 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(rc==SQLITE_BUSY?1:0)); |
| 5617 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nLog)); |
| 5618 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCkpt)); |
| 5619 | Tcl_SetObjResult(interp, pRet); |
| 5620 | |
| 5621 | return TCL_OK; |
| 5622 | } |
| 5623 | |
| 5624 | /* |
dan | eb8763d | 2010-08-17 14:52:22 +0000 | [diff] [blame] | 5625 | ** tclcmd: test_sqlite3_log ?SCRIPT? |
| 5626 | */ |
| 5627 | static struct LogCallback { |
| 5628 | Tcl_Interp *pInterp; |
| 5629 | Tcl_Obj *pObj; |
| 5630 | } logcallback = {0, 0}; |
| 5631 | static void xLogcallback(void *unused, int err, char *zMsg){ |
| 5632 | Tcl_Obj *pNew = Tcl_DuplicateObj(logcallback.pObj); |
| 5633 | Tcl_IncrRefCount(pNew); |
| 5634 | Tcl_ListObjAppendElement( |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 5635 | 0, pNew, Tcl_NewStringObj(sqlite3ErrName(err), -1) |
dan | eb8763d | 2010-08-17 14:52:22 +0000 | [diff] [blame] | 5636 | ); |
| 5637 | Tcl_ListObjAppendElement(0, pNew, Tcl_NewStringObj(zMsg, -1)); |
| 5638 | Tcl_EvalObjEx(logcallback.pInterp, pNew, TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT); |
| 5639 | Tcl_DecrRefCount(pNew); |
| 5640 | } |
| 5641 | static int test_sqlite3_log( |
| 5642 | ClientData clientData, |
| 5643 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 5644 | int objc, /* Number of arguments */ |
| 5645 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 5646 | ){ |
| 5647 | if( objc>2 ){ |
| 5648 | Tcl_WrongNumArgs(interp, 1, objv, "SCRIPT"); |
| 5649 | return TCL_ERROR; |
| 5650 | } |
| 5651 | if( logcallback.pObj ){ |
| 5652 | Tcl_DecrRefCount(logcallback.pObj); |
| 5653 | logcallback.pObj = 0; |
| 5654 | logcallback.pInterp = 0; |
| 5655 | sqlite3_config(SQLITE_CONFIG_LOG, 0, 0); |
| 5656 | } |
| 5657 | if( objc>1 ){ |
| 5658 | logcallback.pObj = objv[1]; |
| 5659 | Tcl_IncrRefCount(logcallback.pObj); |
| 5660 | logcallback.pInterp = interp; |
| 5661 | sqlite3_config(SQLITE_CONFIG_LOG, xLogcallback, 0); |
| 5662 | } |
| 5663 | return TCL_OK; |
| 5664 | } |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 5665 | |
| 5666 | /* |
drh | a2c8a95 | 2009-10-13 18:38:34 +0000 | [diff] [blame] | 5667 | ** tcl_objproc COMMANDNAME ARGS... |
| 5668 | ** |
| 5669 | ** Run a TCL command using its objProc interface. Throw an error if |
| 5670 | ** the command has no objProc interface. |
| 5671 | */ |
| 5672 | static int runAsObjProc( |
| 5673 | void * clientData, |
| 5674 | Tcl_Interp *interp, |
| 5675 | int objc, |
| 5676 | Tcl_Obj *CONST objv[] |
| 5677 | ){ |
| 5678 | Tcl_CmdInfo cmdInfo; |
| 5679 | if( objc<2 ){ |
| 5680 | Tcl_WrongNumArgs(interp, 1, objv, "COMMAND ..."); |
| 5681 | return TCL_ERROR; |
| 5682 | } |
| 5683 | if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ |
| 5684 | Tcl_AppendResult(interp, "command not found: ", |
| 5685 | Tcl_GetString(objv[1]), (char*)0); |
| 5686 | return TCL_ERROR; |
| 5687 | } |
| 5688 | if( cmdInfo.objProc==0 ){ |
| 5689 | Tcl_AppendResult(interp, "command has no objProc: ", |
| 5690 | Tcl_GetString(objv[1]), (char*)0); |
| 5691 | return TCL_ERROR; |
| 5692 | } |
| 5693 | return cmdInfo.objProc(cmdInfo.objClientData, interp, objc-1, objv+1); |
| 5694 | } |
| 5695 | |
dan | 91da6b8 | 2010-11-15 14:51:33 +0000 | [diff] [blame] | 5696 | #ifndef SQLITE_OMIT_EXPLAIN |
| 5697 | /* |
| 5698 | ** WARNING: The following function, printExplainQueryPlan() is an exact |
| 5699 | ** copy of example code from eqp.in (eqp.html). If this code is modified, |
| 5700 | ** then the documentation copy needs to be modified as well. |
| 5701 | */ |
| 5702 | /* |
| 5703 | ** Argument pStmt is a prepared SQL statement. This function compiles |
| 5704 | ** an EXPLAIN QUERY PLAN command to report on the prepared statement, |
| 5705 | ** and prints the report to stdout using printf(). |
| 5706 | */ |
| 5707 | int printExplainQueryPlan(sqlite3_stmt *pStmt){ |
| 5708 | const char *zSql; /* Input SQL */ |
| 5709 | char *zExplain; /* SQL with EXPLAIN QUERY PLAN prepended */ |
| 5710 | sqlite3_stmt *pExplain; /* Compiled EXPLAIN QUERY PLAN command */ |
| 5711 | int rc; /* Return code from sqlite3_prepare_v2() */ |
| 5712 | |
| 5713 | zSql = sqlite3_sql(pStmt); |
| 5714 | if( zSql==0 ) return SQLITE_ERROR; |
| 5715 | |
| 5716 | zExplain = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zSql); |
| 5717 | if( zExplain==0 ) return SQLITE_NOMEM; |
| 5718 | |
| 5719 | rc = sqlite3_prepare_v2(sqlite3_db_handle(pStmt), zExplain, -1, &pExplain, 0); |
| 5720 | sqlite3_free(zExplain); |
| 5721 | if( rc!=SQLITE_OK ) return rc; |
| 5722 | |
| 5723 | while( SQLITE_ROW==sqlite3_step(pExplain) ){ |
| 5724 | int iSelectid = sqlite3_column_int(pExplain, 0); |
| 5725 | int iOrder = sqlite3_column_int(pExplain, 1); |
| 5726 | int iFrom = sqlite3_column_int(pExplain, 2); |
| 5727 | const char *zDetail = (const char *)sqlite3_column_text(pExplain, 3); |
| 5728 | |
| 5729 | printf("%d %d %d %s\n", iSelectid, iOrder, iFrom, zDetail); |
| 5730 | } |
| 5731 | |
| 5732 | return sqlite3_finalize(pExplain); |
| 5733 | } |
| 5734 | |
| 5735 | static int test_print_eqp( |
| 5736 | void * clientData, |
| 5737 | Tcl_Interp *interp, |
| 5738 | int objc, |
| 5739 | Tcl_Obj *CONST objv[] |
| 5740 | ){ |
| 5741 | int rc; |
| 5742 | sqlite3_stmt *pStmt; |
| 5743 | |
| 5744 | if( objc!=2 ){ |
| 5745 | Tcl_WrongNumArgs(interp, 1, objv, "STMT"); |
| 5746 | return TCL_ERROR; |
| 5747 | } |
| 5748 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 5749 | rc = printExplainQueryPlan(pStmt); |
shaneh | 7c5d8fb | 2011-06-22 14:21:31 +0000 | [diff] [blame] | 5750 | /* This is needed on Windows so that a test case using this |
| 5751 | ** function can open a read pipe and get the output of |
| 5752 | ** printExplainQueryPlan() immediately. |
| 5753 | */ |
| 5754 | fflush(stdout); |
dan | 91da6b8 | 2010-11-15 14:51:33 +0000 | [diff] [blame] | 5755 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); |
| 5756 | return TCL_OK; |
| 5757 | } |
| 5758 | #endif /* SQLITE_OMIT_EXPLAIN */ |
drh | a2c8a95 | 2009-10-13 18:38:34 +0000 | [diff] [blame] | 5759 | |
| 5760 | /* |
dan | c17d696 | 2011-06-21 12:47:30 +0000 | [diff] [blame] | 5761 | ** sqlite3_test_control VERB ARGS... |
| 5762 | */ |
| 5763 | static int test_test_control( |
| 5764 | void * clientData, |
| 5765 | Tcl_Interp *interp, |
| 5766 | int objc, |
| 5767 | Tcl_Obj *CONST objv[] |
| 5768 | ){ |
| 5769 | struct Verb { |
| 5770 | const char *zName; |
| 5771 | int i; |
| 5772 | } aVerb[] = { |
| 5773 | { "SQLITE_TESTCTRL_LOCALTIME_FAULT", SQLITE_TESTCTRL_LOCALTIME_FAULT }, |
| 5774 | }; |
| 5775 | int iVerb; |
| 5776 | int iFlag; |
| 5777 | int rc; |
| 5778 | |
| 5779 | if( objc<2 ){ |
| 5780 | Tcl_WrongNumArgs(interp, 1, objv, "VERB ARGS..."); |
| 5781 | return TCL_ERROR; |
| 5782 | } |
| 5783 | |
| 5784 | rc = Tcl_GetIndexFromObjStruct( |
| 5785 | interp, objv[1], aVerb, sizeof(aVerb[0]), "VERB", 0, &iVerb |
| 5786 | ); |
| 5787 | if( rc!=TCL_OK ) return rc; |
| 5788 | |
| 5789 | iFlag = aVerb[iVerb].i; |
| 5790 | switch( iFlag ){ |
| 5791 | case SQLITE_TESTCTRL_LOCALTIME_FAULT: { |
| 5792 | int val; |
| 5793 | if( objc!=3 ){ |
| 5794 | Tcl_WrongNumArgs(interp, 2, objv, "ONOFF"); |
| 5795 | return TCL_ERROR; |
| 5796 | } |
| 5797 | if( Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; |
| 5798 | sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, val); |
| 5799 | break; |
| 5800 | } |
| 5801 | } |
| 5802 | |
| 5803 | Tcl_ResetResult(interp); |
| 5804 | return TCL_OK; |
| 5805 | } |
| 5806 | |
mistachkin | daf9a5a | 2013-03-23 09:56:39 +0000 | [diff] [blame] | 5807 | #if SQLITE_OS_UNIX |
dan | a72014f | 2013-03-16 20:19:21 +0000 | [diff] [blame] | 5808 | #include <sys/time.h> |
| 5809 | #include <sys/resource.h> |
| 5810 | |
| 5811 | static int test_getrusage( |
| 5812 | void * clientData, |
| 5813 | Tcl_Interp *interp, |
| 5814 | int objc, |
| 5815 | Tcl_Obj *CONST objv[] |
| 5816 | ){ |
| 5817 | char buf[1024]; |
| 5818 | struct rusage r; |
| 5819 | memset(&r, 0, sizeof(r)); |
| 5820 | getrusage(RUSAGE_SELF, &r); |
| 5821 | |
| 5822 | sprintf(buf, "ru_utime=%d.%06d ru_stime=%d.%06d ru_minflt=%d ru_majflt=%d", |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 5823 | (int)r.ru_utime.tv_sec, (int)r.ru_utime.tv_usec, |
| 5824 | (int)r.ru_stime.tv_sec, (int)r.ru_stime.tv_usec, |
| 5825 | (int)r.ru_minflt, (int)r.ru_majflt |
dan | a72014f | 2013-03-16 20:19:21 +0000 | [diff] [blame] | 5826 | ); |
| 5827 | Tcl_SetObjResult(interp, Tcl_NewStringObj(buf, -1)); |
| 5828 | return TCL_OK; |
| 5829 | } |
mistachkin | daf9a5a | 2013-03-23 09:56:39 +0000 | [diff] [blame] | 5830 | #endif |
dan | a72014f | 2013-03-16 20:19:21 +0000 | [diff] [blame] | 5831 | |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 5832 | #if SQLITE_OS_WIN |
| 5833 | /* |
| 5834 | ** Information passed from the main thread into the windows file locker |
| 5835 | ** background thread. |
| 5836 | */ |
| 5837 | struct win32FileLocker { |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 5838 | char *evName; /* Name of event to signal thread startup */ |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 5839 | HANDLE h; /* Handle of the file to be locked */ |
| 5840 | int delay1; /* Delay before locking */ |
| 5841 | int delay2; /* Delay before unlocking */ |
| 5842 | int ok; /* Finished ok */ |
| 5843 | int err; /* True if an error occurs */ |
| 5844 | }; |
| 5845 | #endif |
| 5846 | |
| 5847 | |
| 5848 | #if SQLITE_OS_WIN |
drh | 7da5fcb | 2012-03-30 14:59:43 +0000 | [diff] [blame] | 5849 | #include <process.h> |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 5850 | /* |
| 5851 | ** The background thread that does file locking. |
| 5852 | */ |
| 5853 | static void win32_file_locker(void *pAppData){ |
| 5854 | struct win32FileLocker *p = (struct win32FileLocker*)pAppData; |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 5855 | if( p->evName ){ |
| 5856 | HANDLE ev = OpenEvent(EVENT_MODIFY_STATE, FALSE, p->evName); |
| 5857 | if ( ev ){ |
| 5858 | SetEvent(ev); |
| 5859 | CloseHandle(ev); |
| 5860 | } |
| 5861 | } |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 5862 | if( p->delay1 ) Sleep(p->delay1); |
| 5863 | if( LockFile(p->h, 0, 0, 100000000, 0) ){ |
| 5864 | Sleep(p->delay2); |
| 5865 | UnlockFile(p->h, 0, 0, 100000000, 0); |
| 5866 | p->ok = 1; |
| 5867 | }else{ |
| 5868 | p->err = 1; |
| 5869 | } |
| 5870 | CloseHandle(p->h); |
| 5871 | p->h = 0; |
| 5872 | p->delay1 = 0; |
| 5873 | p->delay2 = 0; |
| 5874 | } |
| 5875 | #endif |
| 5876 | |
| 5877 | #if SQLITE_OS_WIN |
| 5878 | /* |
| 5879 | ** lock_win32_file FILENAME DELAY1 DELAY2 |
| 5880 | ** |
| 5881 | ** Get an exclusive manditory lock on file for DELAY2 milliseconds. |
| 5882 | ** Wait DELAY1 milliseconds before acquiring the lock. |
| 5883 | */ |
| 5884 | static int win32_file_lock( |
| 5885 | void * clientData, |
| 5886 | Tcl_Interp *interp, |
| 5887 | int objc, |
| 5888 | Tcl_Obj *CONST objv[] |
| 5889 | ){ |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 5890 | static struct win32FileLocker x = { "win32_file_lock", 0, 0, 0, 0, 0 }; |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 5891 | const char *zFilename; |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 5892 | char zBuf[200]; |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 5893 | int retry = 0; |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 5894 | HANDLE ev; |
| 5895 | DWORD wResult; |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 5896 | |
| 5897 | if( objc!=4 && objc!=1 ){ |
| 5898 | Tcl_WrongNumArgs(interp, 1, objv, "FILENAME DELAY1 DELAY2"); |
| 5899 | return TCL_ERROR; |
| 5900 | } |
| 5901 | if( objc==1 ){ |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 5902 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%d %d %d %d %d", |
| 5903 | x.ok, x.err, x.delay1, x.delay2, x.h); |
| 5904 | Tcl_AppendResult(interp, zBuf, (char*)0); |
| 5905 | return TCL_OK; |
| 5906 | } |
drh | d0cdf01 | 2011-07-13 16:03:46 +0000 | [diff] [blame] | 5907 | while( x.h && retry<30 ){ |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 5908 | retry++; |
| 5909 | Sleep(100); |
| 5910 | } |
| 5911 | if( x.h ){ |
| 5912 | Tcl_AppendResult(interp, "busy", (char*)0); |
| 5913 | return TCL_ERROR; |
| 5914 | } |
| 5915 | if( Tcl_GetIntFromObj(interp, objv[2], &x.delay1) ) return TCL_ERROR; |
| 5916 | if( Tcl_GetIntFromObj(interp, objv[3], &x.delay2) ) return TCL_ERROR; |
| 5917 | zFilename = Tcl_GetString(objv[1]); |
| 5918 | x.h = CreateFile(zFilename, GENERIC_READ|GENERIC_WRITE, |
| 5919 | FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_ALWAYS, |
| 5920 | FILE_ATTRIBUTE_NORMAL, 0); |
| 5921 | if( !x.h ){ |
| 5922 | Tcl_AppendResult(interp, "cannot open file: ", zFilename, (char*)0); |
| 5923 | return TCL_ERROR; |
| 5924 | } |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 5925 | ev = CreateEvent(NULL, TRUE, FALSE, x.evName); |
| 5926 | if ( !ev ){ |
| 5927 | Tcl_AppendResult(interp, "cannot create event: ", x.evName, (char*)0); |
| 5928 | return TCL_ERROR; |
| 5929 | } |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 5930 | _beginthread(win32_file_locker, 0, (void*)&x); |
| 5931 | Sleep(0); |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 5932 | if ( (wResult = WaitForSingleObject(ev, 10000))!=WAIT_OBJECT_0 ){ |
| 5933 | sqlite3_snprintf(sizeof(zBuf), zBuf, "0x%x", wResult); |
| 5934 | Tcl_AppendResult(interp, "wait failed: ", zBuf, (char*)0); |
| 5935 | CloseHandle(ev); |
| 5936 | return TCL_ERROR; |
| 5937 | } |
| 5938 | CloseHandle(ev); |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 5939 | return TCL_OK; |
| 5940 | } |
mistachkin | 3741827 | 2013-08-28 05:49:39 +0000 | [diff] [blame] | 5941 | |
| 5942 | /* |
| 5943 | ** exists_win32_path PATH |
| 5944 | ** |
| 5945 | ** Returns non-zero if the specified path exists, whose fully qualified name |
mistachkin | 3259fe7 | 2013-08-28 17:59:38 +0000 | [diff] [blame] | 5946 | ** may exceed 260 characters if it is prefixed with "\\?\". |
mistachkin | 3741827 | 2013-08-28 05:49:39 +0000 | [diff] [blame] | 5947 | */ |
| 5948 | static int win32_exists_path( |
| 5949 | void *clientData, |
| 5950 | Tcl_Interp *interp, |
| 5951 | int objc, |
| 5952 | Tcl_Obj *CONST objv[] |
| 5953 | ){ |
| 5954 | if( objc!=2 ){ |
| 5955 | Tcl_WrongNumArgs(interp, 1, objv, "PATH"); |
| 5956 | return TCL_ERROR; |
| 5957 | } |
| 5958 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj( |
| 5959 | GetFileAttributesW( Tcl_GetUnicode(objv[1]))!=INVALID_FILE_ATTRIBUTES )); |
| 5960 | return TCL_OK; |
| 5961 | } |
| 5962 | |
| 5963 | /* |
| 5964 | ** find_win32_file PATTERN |
| 5965 | ** |
| 5966 | ** Returns a list of entries in a directory that match the specified pattern, |
| 5967 | ** whose fully qualified name may exceed 248 characters if it is prefixed with |
| 5968 | ** "\\?\". |
| 5969 | */ |
| 5970 | static int win32_find_file( |
| 5971 | void *clientData, |
| 5972 | Tcl_Interp *interp, |
| 5973 | int objc, |
| 5974 | Tcl_Obj *CONST objv[] |
| 5975 | ){ |
| 5976 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 5977 | WIN32_FIND_DATAW findData; |
| 5978 | Tcl_Obj *listObj; |
| 5979 | DWORD lastErrno; |
| 5980 | if( objc!=2 ){ |
| 5981 | Tcl_WrongNumArgs(interp, 1, objv, "PATTERN"); |
| 5982 | return TCL_ERROR; |
| 5983 | } |
| 5984 | hFindFile = FindFirstFileW(Tcl_GetUnicode(objv[1]), &findData); |
| 5985 | if( hFindFile==INVALID_HANDLE_VALUE ){ |
| 5986 | Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); |
| 5987 | return TCL_ERROR; |
| 5988 | } |
| 5989 | listObj = Tcl_NewObj(); |
| 5990 | Tcl_IncrRefCount(listObj); |
| 5991 | do { |
| 5992 | Tcl_ListObjAppendElement(interp, listObj, Tcl_NewUnicodeObj( |
| 5993 | findData.cFileName, -1)); |
| 5994 | Tcl_ListObjAppendElement(interp, listObj, Tcl_NewWideIntObj( |
| 5995 | findData.dwFileAttributes)); |
| 5996 | } while( FindNextFileW(hFindFile, &findData) ); |
| 5997 | lastErrno = GetLastError(); |
| 5998 | if( lastErrno!=NO_ERROR && lastErrno!=ERROR_NO_MORE_FILES ){ |
| 5999 | FindClose(hFindFile); |
| 6000 | Tcl_DecrRefCount(listObj); |
| 6001 | Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); |
| 6002 | return TCL_ERROR; |
| 6003 | } |
| 6004 | FindClose(hFindFile); |
| 6005 | Tcl_SetObjResult(interp, listObj); |
| 6006 | return TCL_OK; |
| 6007 | } |
| 6008 | |
| 6009 | /* |
| 6010 | ** delete_win32_file FILENAME |
| 6011 | ** |
mistachkin | 3259fe7 | 2013-08-28 17:59:38 +0000 | [diff] [blame] | 6012 | ** Deletes the specified file, whose fully qualified name may exceed 260 |
mistachkin | 3741827 | 2013-08-28 05:49:39 +0000 | [diff] [blame] | 6013 | ** characters if it is prefixed with "\\?\". |
| 6014 | */ |
| 6015 | static int win32_delete_file( |
| 6016 | void *clientData, |
| 6017 | Tcl_Interp *interp, |
| 6018 | int objc, |
| 6019 | Tcl_Obj *CONST objv[] |
| 6020 | ){ |
| 6021 | if( objc!=2 ){ |
| 6022 | Tcl_WrongNumArgs(interp, 1, objv, "FILENAME"); |
| 6023 | return TCL_ERROR; |
| 6024 | } |
| 6025 | if( !DeleteFileW(Tcl_GetUnicode(objv[1])) ){ |
| 6026 | Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); |
| 6027 | return TCL_ERROR; |
| 6028 | } |
| 6029 | Tcl_ResetResult(interp); |
| 6030 | return TCL_OK; |
| 6031 | } |
| 6032 | |
| 6033 | /* |
| 6034 | ** make_win32_dir DIRECTORY |
| 6035 | ** |
| 6036 | ** Creates the specified directory, whose fully qualified name may exceed 248 |
| 6037 | ** characters if it is prefixed with "\\?\". |
| 6038 | */ |
| 6039 | static int win32_mkdir( |
| 6040 | void *clientData, |
| 6041 | Tcl_Interp *interp, |
| 6042 | int objc, |
| 6043 | Tcl_Obj *CONST objv[] |
| 6044 | ){ |
| 6045 | if( objc!=2 ){ |
| 6046 | Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY"); |
| 6047 | return TCL_ERROR; |
| 6048 | } |
| 6049 | if( !CreateDirectoryW(Tcl_GetUnicode(objv[1]), NULL) ){ |
| 6050 | Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); |
| 6051 | return TCL_ERROR; |
| 6052 | } |
| 6053 | Tcl_ResetResult(interp); |
| 6054 | return TCL_OK; |
| 6055 | } |
| 6056 | |
| 6057 | /* |
| 6058 | ** remove_win32_dir DIRECTORY |
| 6059 | ** |
| 6060 | ** Removes the specified directory, whose fully qualified name may exceed 248 |
| 6061 | ** characters if it is prefixed with "\\?\". |
| 6062 | */ |
| 6063 | static int win32_rmdir( |
| 6064 | void *clientData, |
| 6065 | Tcl_Interp *interp, |
| 6066 | int objc, |
| 6067 | Tcl_Obj *CONST objv[] |
| 6068 | ){ |
| 6069 | if( objc!=2 ){ |
| 6070 | Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY"); |
| 6071 | return TCL_ERROR; |
| 6072 | } |
| 6073 | if( !RemoveDirectoryW(Tcl_GetUnicode(objv[1])) ){ |
| 6074 | Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); |
| 6075 | return TCL_ERROR; |
| 6076 | } |
| 6077 | Tcl_ResetResult(interp); |
| 6078 | return TCL_OK; |
| 6079 | } |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 6080 | #endif |
dan | c17d696 | 2011-06-21 12:47:30 +0000 | [diff] [blame] | 6081 | |
drh | d0cdf01 | 2011-07-13 16:03:46 +0000 | [diff] [blame] | 6082 | |
dan | c17d696 | 2011-06-21 12:47:30 +0000 | [diff] [blame] | 6083 | /* |
drh | f58ee7f | 2010-12-06 21:06:09 +0000 | [diff] [blame] | 6084 | ** optimization_control DB OPT BOOLEAN |
| 6085 | ** |
| 6086 | ** Enable or disable query optimizations using the sqlite3_test_control() |
| 6087 | ** interface. Disable if BOOLEAN is false and enable if BOOLEAN is true. |
| 6088 | ** OPT is the name of the optimization to be disabled. |
| 6089 | */ |
| 6090 | static int optimization_control( |
| 6091 | void * clientData, |
| 6092 | Tcl_Interp *interp, |
| 6093 | int objc, |
| 6094 | Tcl_Obj *CONST objv[] |
| 6095 | ){ |
| 6096 | int i; |
| 6097 | sqlite3 *db; |
| 6098 | const char *zOpt; |
| 6099 | int onoff; |
drh | fc30b04 | 2012-08-20 16:08:29 +0000 | [diff] [blame] | 6100 | int mask = 0; |
drh | f58ee7f | 2010-12-06 21:06:09 +0000 | [diff] [blame] | 6101 | static const struct { |
| 6102 | const char *zOptName; |
| 6103 | int mask; |
| 6104 | } aOpt[] = { |
drh | 9d5a579 | 2013-06-28 13:43:33 +0000 | [diff] [blame] | 6105 | { "all", SQLITE_AllOpts }, |
| 6106 | { "none", 0 }, |
| 6107 | { "query-flattener", SQLITE_QueryFlattener }, |
| 6108 | { "column-cache", SQLITE_ColumnCache }, |
| 6109 | { "groupby-order", SQLITE_GroupByOrder }, |
| 6110 | { "factor-constants", SQLITE_FactorOutConst }, |
| 6111 | { "real-as-int", SQLITE_IdxRealAsInt }, |
| 6112 | { "distinct-opt", SQLITE_DistinctOpt }, |
| 6113 | { "cover-idx-scan", SQLITE_CoverIdxScan }, |
| 6114 | { "order-by-idx-join", SQLITE_OrderByIdxJoin }, |
| 6115 | { "transitive", SQLITE_Transitive }, |
| 6116 | { "subquery-coroutine", SQLITE_SubqCoroutine }, |
| 6117 | { "omit-noop-join", SQLITE_OmitNoopJoin }, |
drh | 40aa936 | 2013-06-28 17:29:25 +0000 | [diff] [blame] | 6118 | { "stat3", SQLITE_Stat3 }, |
drh | f58ee7f | 2010-12-06 21:06:09 +0000 | [diff] [blame] | 6119 | }; |
| 6120 | |
| 6121 | if( objc!=4 ){ |
| 6122 | Tcl_WrongNumArgs(interp, 1, objv, "DB OPT BOOLEAN"); |
| 6123 | return TCL_ERROR; |
| 6124 | } |
| 6125 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 6126 | if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ) return TCL_ERROR; |
| 6127 | zOpt = Tcl_GetString(objv[2]); |
| 6128 | for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){ |
| 6129 | if( strcmp(zOpt, aOpt[i].zOptName)==0 ){ |
| 6130 | mask = aOpt[i].mask; |
| 6131 | break; |
| 6132 | } |
| 6133 | } |
| 6134 | if( onoff ) mask = ~mask; |
| 6135 | if( i>=sizeof(aOpt)/sizeof(aOpt[0]) ){ |
| 6136 | Tcl_AppendResult(interp, "unknown optimization - should be one of:", |
| 6137 | (char*)0); |
| 6138 | for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){ |
drh | 9d5a579 | 2013-06-28 13:43:33 +0000 | [diff] [blame] | 6139 | Tcl_AppendResult(interp, " ", aOpt[i].zOptName, (char*)0); |
drh | f58ee7f | 2010-12-06 21:06:09 +0000 | [diff] [blame] | 6140 | } |
| 6141 | return TCL_ERROR; |
| 6142 | } |
| 6143 | sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, mask); |
| 6144 | return TCL_OK; |
| 6145 | } |
| 6146 | |
drh | 248f2be | 2013-04-23 20:10:13 +0000 | [diff] [blame] | 6147 | typedef struct sqlite3_api_routines sqlite3_api_routines; |
| 6148 | /* |
drh | ea41dc4 | 2013-04-25 19:31:33 +0000 | [diff] [blame] | 6149 | ** load_static_extension DB NAME ... |
drh | 248f2be | 2013-04-23 20:10:13 +0000 | [diff] [blame] | 6150 | ** |
drh | ea41dc4 | 2013-04-25 19:31:33 +0000 | [diff] [blame] | 6151 | ** Load one or more statically linked extensions. |
drh | 248f2be | 2013-04-23 20:10:13 +0000 | [diff] [blame] | 6152 | */ |
| 6153 | static int tclLoadStaticExtensionCmd( |
| 6154 | void * clientData, |
| 6155 | Tcl_Interp *interp, |
| 6156 | int objc, |
| 6157 | Tcl_Obj *CONST objv[] |
| 6158 | ){ |
drh | 8416fc7 | 2013-04-25 16:42:55 +0000 | [diff] [blame] | 6159 | extern int sqlite3_amatch_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 6160 | extern int sqlite3_closure_init(sqlite3*,char**,const sqlite3_api_routines*); |
drh | e50db1c | 2013-04-25 14:31:46 +0000 | [diff] [blame] | 6161 | extern int sqlite3_fuzzer_init(sqlite3*,char**,const sqlite3_api_routines*); |
drh | 8416fc7 | 2013-04-25 16:42:55 +0000 | [diff] [blame] | 6162 | extern int sqlite3_ieee_init(sqlite3*,char**,const sqlite3_api_routines*); |
drh | ea41dc4 | 2013-04-25 19:31:33 +0000 | [diff] [blame] | 6163 | extern int sqlite3_nextchar_init(sqlite3*,char**,const sqlite3_api_routines*); |
drh | def3367 | 2013-05-28 20:25:54 +0000 | [diff] [blame] | 6164 | extern int sqlite3_percentile_init(sqlite3*,char**,const sqlite3_api_routines*); |
drh | 248f2be | 2013-04-23 20:10:13 +0000 | [diff] [blame] | 6165 | extern int sqlite3_regexp_init(sqlite3*,char**,const sqlite3_api_routines*); |
drh | b7045ab | 2013-04-25 14:59:01 +0000 | [diff] [blame] | 6166 | extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*); |
drh | 5f8cdac | 2013-10-14 21:14:42 +0000 | [diff] [blame] | 6167 | extern int sqlite3_totype_init(sqlite3*,char**,const sqlite3_api_routines*); |
drh | 24b6422 | 2013-04-25 11:58:36 +0000 | [diff] [blame] | 6168 | extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*); |
drh | 248f2be | 2013-04-23 20:10:13 +0000 | [diff] [blame] | 6169 | static const struct { |
| 6170 | const char *zExtName; |
| 6171 | int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*); |
| 6172 | } aExtension[] = { |
drh | 8416fc7 | 2013-04-25 16:42:55 +0000 | [diff] [blame] | 6173 | { "amatch", sqlite3_amatch_init }, |
| 6174 | { "closure", sqlite3_closure_init }, |
drh | e50db1c | 2013-04-25 14:31:46 +0000 | [diff] [blame] | 6175 | { "fuzzer", sqlite3_fuzzer_init }, |
drh | 8416fc7 | 2013-04-25 16:42:55 +0000 | [diff] [blame] | 6176 | { "ieee754", sqlite3_ieee_init }, |
drh | ea41dc4 | 2013-04-25 19:31:33 +0000 | [diff] [blame] | 6177 | { "nextchar", sqlite3_nextchar_init }, |
drh | def3367 | 2013-05-28 20:25:54 +0000 | [diff] [blame] | 6178 | { "percentile", sqlite3_percentile_init }, |
drh | 24b6422 | 2013-04-25 11:58:36 +0000 | [diff] [blame] | 6179 | { "regexp", sqlite3_regexp_init }, |
drh | b7045ab | 2013-04-25 14:59:01 +0000 | [diff] [blame] | 6180 | { "spellfix", sqlite3_spellfix_init }, |
drh | 5f8cdac | 2013-10-14 21:14:42 +0000 | [diff] [blame] | 6181 | { "totype", sqlite3_totype_init }, |
drh | 24b6422 | 2013-04-25 11:58:36 +0000 | [diff] [blame] | 6182 | { "wholenumber", sqlite3_wholenumber_init }, |
drh | 248f2be | 2013-04-23 20:10:13 +0000 | [diff] [blame] | 6183 | }; |
| 6184 | sqlite3 *db; |
| 6185 | const char *zName; |
drh | ea41dc4 | 2013-04-25 19:31:33 +0000 | [diff] [blame] | 6186 | int i, j, rc; |
drh | 248f2be | 2013-04-23 20:10:13 +0000 | [diff] [blame] | 6187 | char *zErrMsg = 0; |
drh | ea41dc4 | 2013-04-25 19:31:33 +0000 | [diff] [blame] | 6188 | if( objc<3 ){ |
| 6189 | Tcl_WrongNumArgs(interp, 1, objv, "DB NAME ..."); |
drh | 248f2be | 2013-04-23 20:10:13 +0000 | [diff] [blame] | 6190 | return TCL_ERROR; |
| 6191 | } |
| 6192 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
drh | ea41dc4 | 2013-04-25 19:31:33 +0000 | [diff] [blame] | 6193 | for(j=2; j<objc; j++){ |
| 6194 | zName = Tcl_GetString(objv[j]); |
| 6195 | for(i=0; i<ArraySize(aExtension); i++){ |
| 6196 | if( strcmp(zName, aExtension[i].zExtName)==0 ) break; |
| 6197 | } |
| 6198 | if( i>=ArraySize(aExtension) ){ |
| 6199 | Tcl_AppendResult(interp, "no such extension: ", zName, (char*)0); |
| 6200 | return TCL_ERROR; |
| 6201 | } |
| 6202 | rc = aExtension[i].pInit(db, &zErrMsg, 0); |
| 6203 | if( rc!=SQLITE_OK || zErrMsg ){ |
| 6204 | Tcl_AppendResult(interp, "initialization of ", zName, " failed: ", zErrMsg, |
| 6205 | (char*)0); |
| 6206 | sqlite3_free(zErrMsg); |
| 6207 | return TCL_ERROR; |
| 6208 | } |
drh | 248f2be | 2013-04-23 20:10:13 +0000 | [diff] [blame] | 6209 | } |
| 6210 | return TCL_OK; |
| 6211 | } |
| 6212 | |
| 6213 | |
drh | f58ee7f | 2010-12-06 21:06:09 +0000 | [diff] [blame] | 6214 | /* |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 6215 | ** Register commands with the TCL interpreter. |
| 6216 | */ |
| 6217 | int Sqlitetest1_Init(Tcl_Interp *interp){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 6218 | extern int sqlite3_search_count; |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 6219 | extern int sqlite3_found_count; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 6220 | extern int sqlite3_interrupt_count; |
| 6221 | extern int sqlite3_open_file_count; |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 6222 | extern int sqlite3_sort_count; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 6223 | extern int sqlite3_current_time; |
drh | 84a2bf6 | 2010-03-05 13:41:06 +0000 | [diff] [blame] | 6224 | #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6225 | extern int sqlite3_hostid_num; |
pweilbacher | aabbed2 | 2008-11-21 23:35:02 +0000 | [diff] [blame] | 6226 | #endif |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 6227 | extern int sqlite3_max_blobsize; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6228 | extern int sqlite3BtreeSharedCacheReport(void*, |
| 6229 | Tcl_Interp*,int,Tcl_Obj*CONST*); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 6230 | static struct { |
| 6231 | char *zName; |
| 6232 | Tcl_CmdProc *xProc; |
| 6233 | } aCmd[] = { |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 6234 | { "db_enter", (Tcl_CmdProc*)db_enter }, |
| 6235 | { "db_leave", (Tcl_CmdProc*)db_leave }, |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 6236 | { "sqlite3_mprintf_int", (Tcl_CmdProc*)sqlite3_mprintf_int }, |
drh | e970767 | 2004-06-25 01:10:48 +0000 | [diff] [blame] | 6237 | { "sqlite3_mprintf_int64", (Tcl_CmdProc*)sqlite3_mprintf_int64 }, |
drh | c5cad1e | 2009-02-01 00:21:09 +0000 | [diff] [blame] | 6238 | { "sqlite3_mprintf_long", (Tcl_CmdProc*)sqlite3_mprintf_long }, |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 6239 | { "sqlite3_mprintf_str", (Tcl_CmdProc*)sqlite3_mprintf_str }, |
drh | b3738b6 | 2007-03-31 15:02:49 +0000 | [diff] [blame] | 6240 | { "sqlite3_snprintf_str", (Tcl_CmdProc*)sqlite3_snprintf_str }, |
drh | e29b1a0 | 2004-07-17 21:56:09 +0000 | [diff] [blame] | 6241 | { "sqlite3_mprintf_stronly", (Tcl_CmdProc*)sqlite3_mprintf_stronly}, |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 6242 | { "sqlite3_mprintf_double", (Tcl_CmdProc*)sqlite3_mprintf_double }, |
| 6243 | { "sqlite3_mprintf_scaled", (Tcl_CmdProc*)sqlite3_mprintf_scaled }, |
drh | 6378285 | 2005-08-30 19:30:59 +0000 | [diff] [blame] | 6244 | { "sqlite3_mprintf_hexdouble", (Tcl_CmdProc*)sqlite3_mprintf_hexdouble}, |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 6245 | { "sqlite3_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z }, |
drh | 05a8298 | 2006-03-19 13:00:25 +0000 | [diff] [blame] | 6246 | { "sqlite3_mprintf_n_test", (Tcl_CmdProc*)test_mprintf_n }, |
drh | 6885390 | 2007-05-07 11:24:30 +0000 | [diff] [blame] | 6247 | { "sqlite3_snprintf_int", (Tcl_CmdProc*)test_snprintf_int }, |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 6248 | { "sqlite3_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid }, |
| 6249 | { "sqlite3_exec_printf", (Tcl_CmdProc*)test_exec_printf }, |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 6250 | { "sqlite3_exec_hex", (Tcl_CmdProc*)test_exec_hex }, |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 6251 | { "sqlite3_exec", (Tcl_CmdProc*)test_exec }, |
| 6252 | { "sqlite3_exec_nr", (Tcl_CmdProc*)test_exec_nr }, |
shane | 8225f5a | 2008-07-31 02:05:04 +0000 | [diff] [blame] | 6253 | #ifndef SQLITE_OMIT_GET_TABLE |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 6254 | { "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf }, |
shane | 8225f5a | 2008-07-31 02:05:04 +0000 | [diff] [blame] | 6255 | #endif |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 6256 | { "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close }, |
dan | 617dc86 | 2013-05-16 11:57:28 +0000 | [diff] [blame] | 6257 | { "sqlite3_close_v2", (Tcl_CmdProc*)sqlite_test_close_v2 }, |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 6258 | { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function }, |
| 6259 | { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate }, |
| 6260 | { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func }, |
| 6261 | { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort }, |
drh | 25d6543 | 2004-07-22 15:02:25 +0000 | [diff] [blame] | 6262 | { "sqlite_bind", (Tcl_CmdProc*)test_bind }, |
| 6263 | { "breakpoint", (Tcl_CmdProc*)test_breakpoint }, |
| 6264 | { "sqlite3_key", (Tcl_CmdProc*)test_key }, |
| 6265 | { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey }, |
drh | cacb208 | 2005-01-11 15:28:33 +0000 | [diff] [blame] | 6266 | { "sqlite_set_magic", (Tcl_CmdProc*)sqlite_set_magic }, |
drh | c5cdca6 | 2005-01-11 16:54:14 +0000 | [diff] [blame] | 6267 | { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt }, |
drh | 3e1d8e6 | 2005-05-26 16:23:34 +0000 | [diff] [blame] | 6268 | { "sqlite_delete_function", (Tcl_CmdProc*)delete_function }, |
| 6269 | { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation }, |
| 6270 | { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit }, |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 6271 | { "sqlite3_stack_used", (Tcl_CmdProc*)test_stack_used }, |
drh | 3086765 | 2006-07-06 10:59:57 +0000 | [diff] [blame] | 6272 | { "sqlite3_busy_timeout", (Tcl_CmdProc*)test_busy_timeout }, |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 6273 | { "printf", (Tcl_CmdProc*)test_printf }, |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 6274 | { "sqlite3IoTrace", (Tcl_CmdProc*)test_io_trace }, |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 6275 | }; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 6276 | static struct { |
| 6277 | char *zName; |
| 6278 | Tcl_ObjCmdProc *xProc; |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 6279 | void *clientData; |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 6280 | } aObjCmd[] = { |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 6281 | { "sqlite3_connection_pointer", get_sqlite_pointer, 0 }, |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 6282 | { "sqlite3_bind_int", test_bind_int, 0 }, |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 6283 | { "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 }, |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 6284 | { "sqlite3_bind_int64", test_bind_int64, 0 }, |
| 6285 | { "sqlite3_bind_double", test_bind_double, 0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 6286 | { "sqlite3_bind_null", test_bind_null ,0 }, |
| 6287 | { "sqlite3_bind_text", test_bind_text ,0 }, |
| 6288 | { "sqlite3_bind_text16", test_bind_text16 ,0 }, |
| 6289 | { "sqlite3_bind_blob", test_bind_blob ,0 }, |
drh | 75f6a03 | 2004-07-15 14:15:00 +0000 | [diff] [blame] | 6290 | { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0}, |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 6291 | { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0}, |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 6292 | { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0}, |
danielk1977 | 600dd0b | 2005-01-20 01:14:23 +0000 | [diff] [blame] | 6293 | { "sqlite3_clear_bindings", test_clear_bindings, 0}, |
drh | f9cb7f5 | 2006-06-27 20:06:44 +0000 | [diff] [blame] | 6294 | { "sqlite3_sleep", test_sleep, 0}, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 6295 | { "sqlite3_errcode", test_errcode ,0 }, |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 6296 | { "sqlite3_extended_errcode", test_ex_errcode ,0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 6297 | { "sqlite3_errmsg", test_errmsg ,0 }, |
| 6298 | { "sqlite3_errmsg16", test_errmsg16 ,0 }, |
| 6299 | { "sqlite3_open", test_open ,0 }, |
| 6300 | { "sqlite3_open16", test_open16 ,0 }, |
dan | 286ab7c | 2011-05-06 18:34:54 +0000 | [diff] [blame] | 6301 | { "sqlite3_open_v2", test_open_v2 ,0 }, |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 6302 | { "sqlite3_complete16", test_complete16 ,0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 6303 | |
| 6304 | { "sqlite3_prepare", test_prepare ,0 }, |
| 6305 | { "sqlite3_prepare16", test_prepare16 ,0 }, |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 6306 | { "sqlite3_prepare_v2", test_prepare_v2 ,0 }, |
drh | 4837f53 | 2008-05-23 14:49:49 +0000 | [diff] [blame] | 6307 | { "sqlite3_prepare_tkt3134", test_prepare_tkt3134, 0}, |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 6308 | { "sqlite3_prepare16_v2", test_prepare16_v2 ,0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 6309 | { "sqlite3_finalize", test_finalize ,0 }, |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 6310 | { "sqlite3_stmt_status", test_stmt_status ,0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 6311 | { "sqlite3_reset", test_reset ,0 }, |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 6312 | { "sqlite3_expired", test_expired ,0 }, |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 6313 | { "sqlite3_transfer_bindings", test_transfer_bind ,0 }, |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 6314 | { "sqlite3_changes", test_changes ,0 }, |
| 6315 | { "sqlite3_step", test_step ,0 }, |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 6316 | { "sqlite3_sql", test_sql ,0 }, |
drh | bb5a9c3 | 2008-06-19 02:52:25 +0000 | [diff] [blame] | 6317 | { "sqlite3_next_stmt", test_next_stmt ,0 }, |
drh | f03d9cc | 2010-11-16 23:10:25 +0000 | [diff] [blame] | 6318 | { "sqlite3_stmt_readonly", test_stmt_readonly ,0 }, |
drh | 2fb6693 | 2011-11-25 17:21:47 +0000 | [diff] [blame] | 6319 | { "sqlite3_stmt_busy", test_stmt_busy ,0 }, |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 6320 | { "uses_stmt_journal", uses_stmt_journal ,0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 6321 | |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 6322 | { "sqlite3_release_memory", test_release_memory, 0}, |
drh | 09419b4 | 2011-11-16 19:29:17 +0000 | [diff] [blame] | 6323 | { "sqlite3_db_release_memory", test_db_release_memory, 0}, |
drh | 283829c | 2011-11-17 00:56:20 +0000 | [diff] [blame] | 6324 | { "sqlite3_db_filename", test_db_filename, 0}, |
drh | 421377e | 2012-03-15 21:28:54 +0000 | [diff] [blame] | 6325 | { "sqlite3_db_readonly", test_db_readonly, 0}, |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 6326 | { "sqlite3_soft_heap_limit", test_soft_heap_limit, 0}, |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 6327 | { "sqlite3_thread_cleanup", test_thread_cleanup, 0}, |
drh | c6ba55f | 2007-04-05 17:36:18 +0000 | [diff] [blame] | 6328 | { "sqlite3_pager_refcounts", test_pager_refcounts, 0}, |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 6329 | |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 6330 | { "sqlite3_load_extension", test_load_extension, 0}, |
| 6331 | { "sqlite3_enable_load_extension", test_enable_load, 0}, |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 6332 | { "sqlite3_extended_result_codes", test_extended_result_codes, 0}, |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 6333 | { "sqlite3_limit", test_limit, 0}, |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 6334 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 6335 | { "save_prng_state", save_prng_state, 0 }, |
| 6336 | { "restore_prng_state", restore_prng_state, 0 }, |
| 6337 | { "reset_prng_state", reset_prng_state, 0 }, |
drh | f58ee7f | 2010-12-06 21:06:09 +0000 | [diff] [blame] | 6338 | { "optimization_control", optimization_control,0}, |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 6339 | #if SQLITE_OS_WIN |
| 6340 | { "lock_win32_file", win32_file_lock, 0 }, |
mistachkin | 3741827 | 2013-08-28 05:49:39 +0000 | [diff] [blame] | 6341 | { "exists_win32_path", win32_exists_path, 0 }, |
| 6342 | { "find_win32_file", win32_find_file, 0 }, |
| 6343 | { "delete_win32_file", win32_delete_file, 0 }, |
| 6344 | { "make_win32_dir", win32_mkdir, 0 }, |
| 6345 | { "remove_win32_dir", win32_rmdir, 0 }, |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 6346 | #endif |
drh | a2c8a95 | 2009-10-13 18:38:34 +0000 | [diff] [blame] | 6347 | { "tcl_objproc", runAsObjProc, 0 }, |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 6348 | |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 6349 | /* sqlite3_column_*() API */ |
| 6350 | { "sqlite3_column_count", test_column_count ,0 }, |
| 6351 | { "sqlite3_data_count", test_data_count ,0 }, |
| 6352 | { "sqlite3_column_type", test_column_type ,0 }, |
danielk1977 | ea61b2c | 2004-05-27 01:49:51 +0000 | [diff] [blame] | 6353 | { "sqlite3_column_blob", test_column_blob ,0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 6354 | { "sqlite3_column_double", test_column_double ,0 }, |
| 6355 | { "sqlite3_column_int64", test_column_int64 ,0 }, |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 6356 | { "sqlite3_column_text", test_stmt_utf8, (void*)sqlite3_column_text }, |
| 6357 | { "sqlite3_column_name", test_stmt_utf8, (void*)sqlite3_column_name }, |
| 6358 | { "sqlite3_column_int", test_stmt_int, (void*)sqlite3_column_int }, |
| 6359 | { "sqlite3_column_bytes", test_stmt_int, (void*)sqlite3_column_bytes}, |
drh | 3f91357 | 2008-03-22 01:07:17 +0000 | [diff] [blame] | 6360 | #ifndef SQLITE_OMIT_DECLTYPE |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 6361 | { "sqlite3_column_decltype",test_stmt_utf8,(void*)sqlite3_column_decltype}, |
drh | 3f91357 | 2008-03-22 01:07:17 +0000 | [diff] [blame] | 6362 | #endif |
danielk1977 | 4b1ae99 | 2006-02-10 03:06:10 +0000 | [diff] [blame] | 6363 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 6364 | { "sqlite3_column_database_name",test_stmt_utf8,(void*)sqlite3_column_database_name}, |
| 6365 | { "sqlite3_column_table_name",test_stmt_utf8,(void*)sqlite3_column_table_name}, |
| 6366 | { "sqlite3_column_origin_name",test_stmt_utf8,(void*)sqlite3_column_origin_name}, |
danielk1977 | 4b1ae99 | 2006-02-10 03:06:10 +0000 | [diff] [blame] | 6367 | #endif |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 6368 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 6369 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 6370 | { "sqlite3_column_bytes16", test_stmt_int, (void*)sqlite3_column_bytes16 }, |
| 6371 | { "sqlite3_column_text16", test_stmt_utf16, (void*)sqlite3_column_text16}, |
| 6372 | { "sqlite3_column_name16", test_stmt_utf16, (void*)sqlite3_column_name16}, |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 6373 | { "add_alignment_test_collations", add_alignment_test_collations, 0 }, |
drh | 3f91357 | 2008-03-22 01:07:17 +0000 | [diff] [blame] | 6374 | #ifndef SQLITE_OMIT_DECLTYPE |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 6375 | { "sqlite3_column_decltype16",test_stmt_utf16,(void*)sqlite3_column_decltype16}, |
drh | 3f91357 | 2008-03-22 01:07:17 +0000 | [diff] [blame] | 6376 | #endif |
danielk1977 | 4b1ae99 | 2006-02-10 03:06:10 +0000 | [diff] [blame] | 6377 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 6378 | {"sqlite3_column_database_name16", |
drh | 7da5fcb | 2012-03-30 14:59:43 +0000 | [diff] [blame] | 6379 | test_stmt_utf16, (void*)sqlite3_column_database_name16}, |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 6380 | {"sqlite3_column_table_name16", test_stmt_utf16, (void*)sqlite3_column_table_name16}, |
| 6381 | {"sqlite3_column_origin_name16", test_stmt_utf16, (void*)sqlite3_column_origin_name16}, |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 6382 | #endif |
danielk1977 | 4b1ae99 | 2006-02-10 03:06:10 +0000 | [diff] [blame] | 6383 | #endif |
danielk1977 | a393c03 | 2007-05-07 14:58:53 +0000 | [diff] [blame] | 6384 | { "sqlite3_create_collation_v2", test_create_collation_v2, 0 }, |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 6385 | { "sqlite3_global_recover", test_global_recover, 0 }, |
| 6386 | { "working_64bit_int", working_64bit_int, 0 }, |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 6387 | { "vfs_unlink_test", vfs_unlink_test, 0 }, |
drh | c8d7567 | 2008-07-08 02:12:37 +0000 | [diff] [blame] | 6388 | { "vfs_initfail_test", vfs_initfail_test, 0 }, |
drh | a282097 | 2008-07-07 13:31:58 +0000 | [diff] [blame] | 6389 | { "vfs_unregister_all", vfs_unregister_all, 0 }, |
| 6390 | { "vfs_reregister_all", vfs_reregister_all, 0 }, |
drh | 5517625 | 2008-01-22 14:50:16 +0000 | [diff] [blame] | 6391 | { "file_control_test", file_control_test, 0 }, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6392 | { "file_control_lasterrno_test", file_control_lasterrno_test, 0 }, |
| 6393 | { "file_control_lockproxy_test", file_control_lockproxy_test, 0 }, |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 6394 | { "file_control_chunksize_test", file_control_chunksize_test, 0 }, |
drh | d0cdf01 | 2011-07-13 16:03:46 +0000 | [diff] [blame] | 6395 | { "file_control_sizehint_test", file_control_sizehint_test, 0 }, |
| 6396 | { "file_control_win32_av_retry", file_control_win32_av_retry, 0 }, |
drh | 253cea5 | 2011-07-26 16:23:25 +0000 | [diff] [blame] | 6397 | { "file_control_persist_wal", file_control_persist_wal, 0 }, |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 6398 | { "file_control_powersafe_overwrite",file_control_powersafe_overwrite,0}, |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 6399 | { "file_control_vfsname", file_control_vfsname, 0 }, |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 6400 | { "file_control_tempfilename", file_control_tempfilename, 0 }, |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6401 | { "sqlite3_vfs_list", vfs_list, 0 }, |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 6402 | { "sqlite3_create_function_v2", test_create_function_v2, 0 }, |
danielk1977 | 04f2e68 | 2004-05-27 01:04:07 +0000 | [diff] [blame] | 6403 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 6404 | /* Functions from os.h */ |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 6405 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 6406 | { "add_test_collate", test_collate, 0 }, |
| 6407 | { "add_test_collate_needed", test_collate_needed, 0 }, |
| 6408 | { "add_test_function", test_function, 0 }, |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 6409 | #endif |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 6410 | { "sqlite3_test_errstr", test_errstr, 0 }, |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 6411 | { "tcl_variable_type", tcl_variable_type, 0 }, |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6412 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 6413 | { "sqlite3_enable_shared_cache", test_enable_shared, 0 }, |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6414 | { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0}, |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6415 | #endif |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 6416 | { "sqlite3_libversion_number", test_libversion_number, 0 }, |
danielk1977 | deb802c | 2006-02-09 13:43:28 +0000 | [diff] [blame] | 6417 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 6418 | { "sqlite3_table_column_metadata", test_table_column_metadata, 0 }, |
| 6419 | #endif |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6420 | #ifndef SQLITE_OMIT_INCRBLOB |
dan | 61c7f59 | 2010-10-26 18:42:52 +0000 | [diff] [blame] | 6421 | { "sqlite3_blob_read", test_blob_read, 0 }, |
| 6422 | { "sqlite3_blob_write", test_blob_write, 0 }, |
dan | 4e76cc3 | 2010-10-20 18:56:04 +0000 | [diff] [blame] | 6423 | { "sqlite3_blob_reopen", test_blob_reopen, 0 }, |
dan | 61c7f59 | 2010-10-26 18:42:52 +0000 | [diff] [blame] | 6424 | { "sqlite3_blob_bytes", test_blob_bytes, 0 }, |
| 6425 | { "sqlite3_blob_close", test_blob_close, 0 }, |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6426 | #endif |
danielk1977 | 062d4cb | 2008-08-29 09:10:02 +0000 | [diff] [blame] | 6427 | { "pcache_stats", test_pcache_stats, 0 }, |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 6428 | #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY |
| 6429 | { "sqlite3_unlock_notify", test_unlock_notify, 0 }, |
| 6430 | #endif |
dan | 91da6b8 | 2010-11-15 14:51:33 +0000 | [diff] [blame] | 6431 | { "sqlite3_wal_checkpoint", test_wal_checkpoint, 0 }, |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 6432 | { "sqlite3_wal_checkpoint_v2",test_wal_checkpoint_v2, 0 }, |
dan | 91da6b8 | 2010-11-15 14:51:33 +0000 | [diff] [blame] | 6433 | { "test_sqlite3_log", test_sqlite3_log, 0 }, |
shaneh | bb20134 | 2011-02-09 19:55:20 +0000 | [diff] [blame] | 6434 | #ifndef SQLITE_OMIT_EXPLAIN |
dan | 91da6b8 | 2010-11-15 14:51:33 +0000 | [diff] [blame] | 6435 | { "print_explain_query_plan", test_print_eqp, 0 }, |
shaneh | bb20134 | 2011-02-09 19:55:20 +0000 | [diff] [blame] | 6436 | #endif |
dan | c17d696 | 2011-06-21 12:47:30 +0000 | [diff] [blame] | 6437 | { "sqlite3_test_control", test_test_control }, |
mistachkin | daf9a5a | 2013-03-23 09:56:39 +0000 | [diff] [blame] | 6438 | #if SQLITE_OS_UNIX |
dan | a72014f | 2013-03-16 20:19:21 +0000 | [diff] [blame] | 6439 | { "getrusage", test_getrusage }, |
mistachkin | daf9a5a | 2013-03-23 09:56:39 +0000 | [diff] [blame] | 6440 | #endif |
drh | 248f2be | 2013-04-23 20:10:13 +0000 | [diff] [blame] | 6441 | { "load_static_extension", tclLoadStaticExtensionCmd }, |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 6442 | }; |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 6443 | static int bitmask_size = sizeof(Bitmask)*8; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 6444 | int i; |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 6445 | extern int sqlite3_sync_count, sqlite3_fullsync_count; |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 6446 | extern int sqlite3_opentemp_count; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 6447 | extern int sqlite3_like_count; |
drh | dd73521 | 2007-02-24 13:53:05 +0000 | [diff] [blame] | 6448 | extern int sqlite3_xferopt_count; |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6449 | extern int sqlite3_pager_readdb_count; |
| 6450 | extern int sqlite3_pager_writedb_count; |
| 6451 | extern int sqlite3_pager_writej_count; |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 6452 | #if SQLITE_OS_WIN |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 6453 | extern int sqlite3_os_type; |
| 6454 | #endif |
drh | 8b3d990 | 2005-08-19 00:14:42 +0000 | [diff] [blame] | 6455 | #ifdef SQLITE_DEBUG |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 6456 | extern int sqlite3WhereTrace; |
| 6457 | extern int sqlite3OSTrace; |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 6458 | extern int sqlite3WalTrace; |
drh | 549c8b6 | 2005-09-19 13:15:23 +0000 | [diff] [blame] | 6459 | #endif |
| 6460 | #ifdef SQLITE_TEST |
danielk1977 | 33e8903 | 2008-12-17 15:18:17 +0000 | [diff] [blame] | 6461 | #ifdef SQLITE_ENABLE_FTS3 |
| 6462 | extern int sqlite3_fts3_enable_parentheses; |
| 6463 | #endif |
drh | 48083ce | 2005-09-19 12:37:27 +0000 | [diff] [blame] | 6464 | #endif |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 6465 | |
| 6466 | for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ |
| 6467 | Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); |
| 6468 | } |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 6469 | for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 6470 | Tcl_CreateObjCommand(interp, aObjCmd[i].zName, |
| 6471 | aObjCmd[i].xProc, aObjCmd[i].clientData, 0); |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 6472 | } |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 6473 | Tcl_LinkVar(interp, "sqlite_search_count", |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 6474 | (char*)&sqlite3_search_count, TCL_LINK_INT); |
dan | 0ff297e | 2009-09-25 17:03:14 +0000 | [diff] [blame] | 6475 | Tcl_LinkVar(interp, "sqlite_found_count", |
| 6476 | (char*)&sqlite3_found_count, TCL_LINK_INT); |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 6477 | Tcl_LinkVar(interp, "sqlite_sort_count", |
| 6478 | (char*)&sqlite3_sort_count, TCL_LINK_INT); |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 6479 | Tcl_LinkVar(interp, "sqlite3_max_blobsize", |
| 6480 | (char*)&sqlite3_max_blobsize, TCL_LINK_INT); |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 6481 | Tcl_LinkVar(interp, "sqlite_like_count", |
| 6482 | (char*)&sqlite3_like_count, TCL_LINK_INT); |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 6483 | Tcl_LinkVar(interp, "sqlite_interrupt_count", |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 6484 | (char*)&sqlite3_interrupt_count, TCL_LINK_INT); |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 6485 | Tcl_LinkVar(interp, "sqlite_open_file_count", |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 6486 | (char*)&sqlite3_open_file_count, TCL_LINK_INT); |
danielk1977 | 6490beb | 2004-05-11 06:17:21 +0000 | [diff] [blame] | 6487 | Tcl_LinkVar(interp, "sqlite_current_time", |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 6488 | (char*)&sqlite3_current_time, TCL_LINK_INT); |
drh | 84a2bf6 | 2010-03-05 13:41:06 +0000 | [diff] [blame] | 6489 | #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6490 | Tcl_LinkVar(interp, "sqlite_hostid_num", |
| 6491 | (char*)&sqlite3_hostid_num, TCL_LINK_INT); |
pweilbacher | aabbed2 | 2008-11-21 23:35:02 +0000 | [diff] [blame] | 6492 | #endif |
drh | dd73521 | 2007-02-24 13:53:05 +0000 | [diff] [blame] | 6493 | Tcl_LinkVar(interp, "sqlite3_xferopt_count", |
| 6494 | (char*)&sqlite3_xferopt_count, TCL_LINK_INT); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6495 | Tcl_LinkVar(interp, "sqlite3_pager_readdb_count", |
| 6496 | (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT); |
| 6497 | Tcl_LinkVar(interp, "sqlite3_pager_writedb_count", |
| 6498 | (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT); |
| 6499 | Tcl_LinkVar(interp, "sqlite3_pager_writej_count", |
| 6500 | (char*)&sqlite3_pager_writej_count, TCL_LINK_INT); |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 6501 | #ifndef SQLITE_OMIT_UTF16 |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 6502 | Tcl_LinkVar(interp, "unaligned_string_counter", |
| 6503 | (char*)&unaligned_string_counter, TCL_LINK_INT); |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 6504 | #endif |
drh | 268803a | 2005-12-14 20:11:30 +0000 | [diff] [blame] | 6505 | #ifndef SQLITE_OMIT_UTF16 |
| 6506 | Tcl_LinkVar(interp, "sqlite_last_needed_collation", |
| 6507 | (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY); |
| 6508 | #endif |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 6509 | #if SQLITE_OS_WIN |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 6510 | Tcl_LinkVar(interp, "sqlite_os_type", |
| 6511 | (char*)&sqlite3_os_type, TCL_LINK_INT); |
| 6512 | #endif |
drh | 549c8b6 | 2005-09-19 13:15:23 +0000 | [diff] [blame] | 6513 | #ifdef SQLITE_TEST |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 6514 | { |
| 6515 | static const char *query_plan = "*** OBSOLETE VARIABLE ***"; |
| 6516 | Tcl_LinkVar(interp, "sqlite_query_plan", |
| 6517 | (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY); |
| 6518 | } |
drh | 549c8b6 | 2005-09-19 13:15:23 +0000 | [diff] [blame] | 6519 | #endif |
drh | 8b3d990 | 2005-08-19 00:14:42 +0000 | [diff] [blame] | 6520 | #ifdef SQLITE_DEBUG |
drh | 48083ce | 2005-09-19 12:37:27 +0000 | [diff] [blame] | 6521 | Tcl_LinkVar(interp, "sqlite_where_trace", |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 6522 | (char*)&sqlite3WhereTrace, TCL_LINK_INT); |
drh | 73be501 | 2007-08-08 12:11:21 +0000 | [diff] [blame] | 6523 | Tcl_LinkVar(interp, "sqlite_os_trace", |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 6524 | (char*)&sqlite3OSTrace, TCL_LINK_INT); |
dan | 38e1a27 | 2010-06-28 11:23:09 +0000 | [diff] [blame] | 6525 | #ifndef SQLITE_OMIT_WAL |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 6526 | Tcl_LinkVar(interp, "sqlite_wal_trace", |
| 6527 | (char*)&sqlite3WalTrace, TCL_LINK_INT); |
drh | 8b3d990 | 2005-08-19 00:14:42 +0000 | [diff] [blame] | 6528 | #endif |
dan | 38e1a27 | 2010-06-28 11:23:09 +0000 | [diff] [blame] | 6529 | #endif |
danielk1977 | cbe21be | 2005-06-07 07:58:48 +0000 | [diff] [blame] | 6530 | #ifndef SQLITE_OMIT_DISKIO |
drh | af6df11 | 2005-06-07 02:12:30 +0000 | [diff] [blame] | 6531 | Tcl_LinkVar(interp, "sqlite_opentemp_count", |
| 6532 | (char*)&sqlite3_opentemp_count, TCL_LINK_INT); |
danielk1977 | cbe21be | 2005-06-07 07:58:48 +0000 | [diff] [blame] | 6533 | #endif |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 6534 | Tcl_LinkVar(interp, "sqlite_static_bind_value", |
| 6535 | (char*)&sqlite_static_bind_value, TCL_LINK_STRING); |
drh | f031381 | 2006-09-04 15:53:53 +0000 | [diff] [blame] | 6536 | Tcl_LinkVar(interp, "sqlite_static_bind_nbyte", |
| 6537 | (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT); |
drh | ab3f9fe | 2004-08-14 17:10:10 +0000 | [diff] [blame] | 6538 | Tcl_LinkVar(interp, "sqlite_temp_directory", |
drh | effd02b | 2004-08-29 23:42:13 +0000 | [diff] [blame] | 6539 | (char*)&sqlite3_temp_directory, TCL_LINK_STRING); |
mistachkin | a112d14 | 2012-03-14 00:44:01 +0000 | [diff] [blame] | 6540 | Tcl_LinkVar(interp, "sqlite_data_directory", |
| 6541 | (char*)&sqlite3_data_directory, TCL_LINK_STRING); |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 6542 | Tcl_LinkVar(interp, "bitmask_size", |
| 6543 | (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY); |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 6544 | Tcl_LinkVar(interp, "sqlite_sync_count", |
| 6545 | (char*)&sqlite3_sync_count, TCL_LINK_INT); |
| 6546 | Tcl_LinkVar(interp, "sqlite_fullsync_count", |
| 6547 | (char*)&sqlite3_fullsync_count, TCL_LINK_INT); |
drh | d1fa7bc | 2009-01-10 13:24:50 +0000 | [diff] [blame] | 6548 | #if defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_TEST) |
danielk1977 | 33e8903 | 2008-12-17 15:18:17 +0000 | [diff] [blame] | 6549 | Tcl_LinkVar(interp, "sqlite_fts3_enable_parentheses", |
| 6550 | (char*)&sqlite3_fts3_enable_parentheses, TCL_LINK_INT); |
| 6551 | #endif |
drh | d1bf351 | 2001-04-07 15:24:33 +0000 | [diff] [blame] | 6552 | return TCL_OK; |
| 6553 | } |