drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 5c4d970 | 2001-08-20 00:33:58 +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 | 5c4d970 | 2001-08-20 00:33:58 +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 | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** Code for testing the pager.c module in SQLite. This code |
| 13 | ** is not included in the SQLite library. It is used for automated |
| 14 | ** testing of the SQLite library. |
| 15 | ** |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 16 | ** $Id: test2.c,v 1.24 2004/07/22 01:19:35 drh Exp $ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 17 | */ |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 18 | #include "os.h" |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 19 | #include "sqliteInt.h" |
| 20 | #include "pager.h" |
| 21 | #include "tcl.h" |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | /* |
| 26 | ** Interpret an SQLite error number |
| 27 | */ |
| 28 | static char *errorName(int rc){ |
| 29 | char *zName; |
| 30 | switch( rc ){ |
| 31 | case SQLITE_OK: zName = "SQLITE_OK"; break; |
| 32 | case SQLITE_ERROR: zName = "SQLITE_ERROR"; break; |
| 33 | case SQLITE_INTERNAL: zName = "SQLITE_INTERNAL"; break; |
| 34 | case SQLITE_PERM: zName = "SQLITE_PERM"; break; |
| 35 | case SQLITE_ABORT: zName = "SQLITE_ABORT"; break; |
| 36 | case SQLITE_BUSY: zName = "SQLITE_BUSY"; break; |
| 37 | case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break; |
| 38 | case SQLITE_READONLY: zName = "SQLITE_READONLY"; break; |
| 39 | case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break; |
| 40 | case SQLITE_IOERR: zName = "SQLITE_IOERR"; break; |
| 41 | case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break; |
| 42 | case SQLITE_NOTFOUND: zName = "SQLITE_NOTFOUND"; break; |
| 43 | case SQLITE_FULL: zName = "SQLITE_FULL"; break; |
| 44 | case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break; |
| 45 | case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break; |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 46 | case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break; |
| 47 | case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break; |
| 48 | case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break; |
| 49 | case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break; |
| 50 | case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break; |
| 51 | case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break; |
drh | 8766c34 | 2002-11-09 00:33:15 +0000 | [diff] [blame] | 52 | case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break; |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 53 | default: zName = "SQLITE_Unknown"; break; |
| 54 | } |
| 55 | return zName; |
| 56 | } |
| 57 | |
| 58 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 59 | ** Page size and reserved size used for testing. |
| 60 | */ |
| 61 | static int test_pagesize = 1024; |
| 62 | |
| 63 | /* |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 64 | ** Usage: pager_open FILENAME N-PAGE |
| 65 | ** |
| 66 | ** Open a new pager |
| 67 | */ |
| 68 | static int pager_open( |
| 69 | void *NotUsed, |
| 70 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 71 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 72 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 73 | ){ |
| 74 | Pager *pPager; |
| 75 | int nPage; |
| 76 | int rc; |
| 77 | char zBuf[100]; |
| 78 | if( argc!=3 ){ |
| 79 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 80 | " FILENAME N-PAGE\"", 0); |
| 81 | return TCL_ERROR; |
| 82 | } |
| 83 | if( Tcl_GetInt(interp, argv[2], &nPage) ) return TCL_ERROR; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 84 | rc = sqlite3pager_open(&pPager, argv[1], 0, 1); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 85 | if( rc!=SQLITE_OK ){ |
| 86 | Tcl_AppendResult(interp, errorName(rc), 0); |
| 87 | return TCL_ERROR; |
| 88 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 89 | sqlite3pager_set_cachesize(pPager, nPage); |
| 90 | sqlite3pager_set_pagesize(pPager, test_pagesize); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 91 | sprintf(zBuf,"0x%x",(int)pPager); |
| 92 | Tcl_AppendResult(interp, zBuf, 0); |
| 93 | return TCL_OK; |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | ** Usage: pager_close ID |
| 98 | ** |
| 99 | ** Close the given pager. |
| 100 | */ |
| 101 | static int pager_close( |
| 102 | void *NotUsed, |
| 103 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 104 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 105 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 106 | ){ |
| 107 | Pager *pPager; |
| 108 | int rc; |
| 109 | if( argc!=2 ){ |
| 110 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 111 | " ID\"", 0); |
| 112 | return TCL_ERROR; |
| 113 | } |
| 114 | if( Tcl_GetInt(interp, argv[1], (int*)&pPager) ) return TCL_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 115 | rc = sqlite3pager_close(pPager); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 116 | if( rc!=SQLITE_OK ){ |
| 117 | Tcl_AppendResult(interp, errorName(rc), 0); |
| 118 | return TCL_ERROR; |
| 119 | } |
| 120 | return TCL_OK; |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | ** Usage: pager_rollback ID |
| 125 | ** |
| 126 | ** Rollback changes |
| 127 | */ |
| 128 | static int pager_rollback( |
| 129 | void *NotUsed, |
| 130 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 131 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 132 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 133 | ){ |
| 134 | Pager *pPager; |
| 135 | int rc; |
| 136 | if( argc!=2 ){ |
| 137 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 138 | " ID\"", 0); |
| 139 | return TCL_ERROR; |
| 140 | } |
| 141 | if( Tcl_GetInt(interp, argv[1], (int*)&pPager) ) return TCL_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 142 | rc = sqlite3pager_rollback(pPager); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 143 | if( rc!=SQLITE_OK ){ |
| 144 | Tcl_AppendResult(interp, errorName(rc), 0); |
| 145 | return TCL_ERROR; |
| 146 | } |
| 147 | return TCL_OK; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | ** Usage: pager_commit ID |
| 152 | ** |
| 153 | ** Commit all changes |
| 154 | */ |
| 155 | static int pager_commit( |
| 156 | void *NotUsed, |
| 157 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 158 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 159 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 160 | ){ |
| 161 | Pager *pPager; |
| 162 | int rc; |
| 163 | if( argc!=2 ){ |
| 164 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 165 | " ID\"", 0); |
| 166 | return TCL_ERROR; |
| 167 | } |
| 168 | if( Tcl_GetInt(interp, argv[1], (int*)&pPager) ) return TCL_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 169 | rc = sqlite3pager_commit(pPager); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 170 | if( rc!=SQLITE_OK ){ |
| 171 | Tcl_AppendResult(interp, errorName(rc), 0); |
| 172 | return TCL_ERROR; |
| 173 | } |
| 174 | return TCL_OK; |
| 175 | } |
| 176 | |
| 177 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 178 | ** Usage: pager_stmt_begin ID |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 179 | ** |
| 180 | ** Start a new checkpoint. |
| 181 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 182 | static int pager_stmt_begin( |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 183 | void *NotUsed, |
| 184 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 185 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 186 | const char **argv /* Text of each argument */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 187 | ){ |
| 188 | Pager *pPager; |
| 189 | int rc; |
| 190 | if( argc!=2 ){ |
| 191 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 192 | " ID\"", 0); |
| 193 | return TCL_ERROR; |
| 194 | } |
| 195 | if( Tcl_GetInt(interp, argv[1], (int*)&pPager) ) return TCL_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 196 | rc = sqlite3pager_stmt_begin(pPager); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 197 | if( rc!=SQLITE_OK ){ |
| 198 | Tcl_AppendResult(interp, errorName(rc), 0); |
| 199 | return TCL_ERROR; |
| 200 | } |
| 201 | return TCL_OK; |
| 202 | } |
| 203 | |
| 204 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 205 | ** Usage: pager_stmt_rollback ID |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 206 | ** |
| 207 | ** Rollback changes to a checkpoint |
| 208 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 209 | static int pager_stmt_rollback( |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 210 | void *NotUsed, |
| 211 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 212 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 213 | const char **argv /* Text of each argument */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 214 | ){ |
| 215 | Pager *pPager; |
| 216 | int rc; |
| 217 | if( argc!=2 ){ |
| 218 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 219 | " ID\"", 0); |
| 220 | return TCL_ERROR; |
| 221 | } |
| 222 | if( Tcl_GetInt(interp, argv[1], (int*)&pPager) ) return TCL_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 223 | rc = sqlite3pager_stmt_rollback(pPager); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 224 | if( rc!=SQLITE_OK ){ |
| 225 | Tcl_AppendResult(interp, errorName(rc), 0); |
| 226 | return TCL_ERROR; |
| 227 | } |
| 228 | return TCL_OK; |
| 229 | } |
| 230 | |
| 231 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 232 | ** Usage: pager_stmt_commit ID |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 233 | ** |
| 234 | ** Commit changes to a checkpoint |
| 235 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 236 | static int pager_stmt_commit( |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 237 | void *NotUsed, |
| 238 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 239 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 240 | const char **argv /* Text of each argument */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 241 | ){ |
| 242 | Pager *pPager; |
| 243 | int rc; |
| 244 | if( argc!=2 ){ |
| 245 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 246 | " ID\"", 0); |
| 247 | return TCL_ERROR; |
| 248 | } |
| 249 | if( Tcl_GetInt(interp, argv[1], (int*)&pPager) ) return TCL_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 250 | rc = sqlite3pager_stmt_commit(pPager); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 251 | if( rc!=SQLITE_OK ){ |
| 252 | Tcl_AppendResult(interp, errorName(rc), 0); |
| 253 | return TCL_ERROR; |
| 254 | } |
| 255 | return TCL_OK; |
| 256 | } |
| 257 | |
| 258 | /* |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 259 | ** Usage: pager_stats ID |
| 260 | ** |
| 261 | ** Return pager statistics. |
| 262 | */ |
| 263 | static int pager_stats( |
| 264 | void *NotUsed, |
| 265 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 266 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 267 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 268 | ){ |
| 269 | Pager *pPager; |
| 270 | int i, *a; |
| 271 | if( argc!=2 ){ |
| 272 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 273 | " ID\"", 0); |
| 274 | return TCL_ERROR; |
| 275 | } |
| 276 | if( Tcl_GetInt(interp, argv[1], (int*)&pPager) ) return TCL_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 277 | a = sqlite3pager_stats(pPager); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 278 | for(i=0; i<9; i++){ |
| 279 | static char *zName[] = { |
| 280 | "ref", "page", "max", "size", "state", "err", |
| 281 | "hit", "miss", "ovfl", |
| 282 | }; |
| 283 | char zBuf[100]; |
| 284 | Tcl_AppendElement(interp, zName[i]); |
| 285 | sprintf(zBuf,"%d",a[i]); |
| 286 | Tcl_AppendElement(interp, zBuf); |
| 287 | } |
| 288 | return TCL_OK; |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | ** Usage: pager_pagecount ID |
| 293 | ** |
| 294 | ** Return the size of the database file. |
| 295 | */ |
| 296 | static int pager_pagecount( |
| 297 | void *NotUsed, |
| 298 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 299 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 300 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 301 | ){ |
| 302 | Pager *pPager; |
| 303 | char zBuf[100]; |
| 304 | if( argc!=2 ){ |
| 305 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 306 | " ID\"", 0); |
| 307 | return TCL_ERROR; |
| 308 | } |
| 309 | if( Tcl_GetInt(interp, argv[1], (int*)&pPager) ) return TCL_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 310 | sprintf(zBuf,"%d",sqlite3pager_pagecount(pPager)); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 311 | Tcl_AppendResult(interp, zBuf, 0); |
| 312 | return TCL_OK; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | ** Usage: page_get ID PGNO |
| 317 | ** |
| 318 | ** Return a pointer to a page from the database. |
| 319 | */ |
| 320 | static int page_get( |
| 321 | void *NotUsed, |
| 322 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 323 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 324 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 325 | ){ |
| 326 | Pager *pPager; |
| 327 | char zBuf[100]; |
| 328 | void *pPage; |
| 329 | int pgno; |
| 330 | int rc; |
| 331 | if( argc!=3 ){ |
| 332 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 333 | " ID PGNO\"", 0); |
| 334 | return TCL_ERROR; |
| 335 | } |
| 336 | if( Tcl_GetInt(interp, argv[1], (int*)&pPager) ) return TCL_ERROR; |
| 337 | if( Tcl_GetInt(interp, argv[2], &pgno) ) return TCL_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 338 | rc = sqlite3pager_get(pPager, pgno, &pPage); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 339 | if( rc!=SQLITE_OK ){ |
| 340 | Tcl_AppendResult(interp, errorName(rc), 0); |
| 341 | return TCL_ERROR; |
| 342 | } |
| 343 | sprintf(zBuf,"0x%x",(int)pPage); |
| 344 | Tcl_AppendResult(interp, zBuf, 0); |
| 345 | return TCL_OK; |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | ** Usage: page_lookup ID PGNO |
| 350 | ** |
| 351 | ** Return a pointer to a page if the page is already in cache. |
| 352 | ** If not in cache, return an empty string. |
| 353 | */ |
| 354 | static int page_lookup( |
| 355 | void *NotUsed, |
| 356 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 357 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 358 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 359 | ){ |
| 360 | Pager *pPager; |
| 361 | char zBuf[100]; |
| 362 | void *pPage; |
| 363 | int pgno; |
| 364 | if( argc!=3 ){ |
| 365 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 366 | " ID PGNO\"", 0); |
| 367 | return TCL_ERROR; |
| 368 | } |
| 369 | if( Tcl_GetInt(interp, argv[1], (int*)&pPager) ) return TCL_ERROR; |
| 370 | if( Tcl_GetInt(interp, argv[2], &pgno) ) return TCL_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 371 | pPage = sqlite3pager_lookup(pPager, pgno); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 372 | if( pPage ){ |
| 373 | sprintf(zBuf,"0x%x",(int)pPage); |
| 374 | Tcl_AppendResult(interp, zBuf, 0); |
| 375 | } |
| 376 | return TCL_OK; |
| 377 | } |
| 378 | |
| 379 | /* |
| 380 | ** Usage: page_unref PAGE |
| 381 | ** |
| 382 | ** Drop a pointer to a page. |
| 383 | */ |
| 384 | static int page_unref( |
| 385 | void *NotUsed, |
| 386 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 387 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 388 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 389 | ){ |
| 390 | void *pPage; |
| 391 | int rc; |
| 392 | if( argc!=2 ){ |
| 393 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 394 | " PAGE\"", 0); |
| 395 | return TCL_ERROR; |
| 396 | } |
| 397 | if( Tcl_GetInt(interp, argv[1], (int*)&pPage) ) return TCL_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 398 | rc = sqlite3pager_unref(pPage); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 399 | if( rc!=SQLITE_OK ){ |
| 400 | Tcl_AppendResult(interp, errorName(rc), 0); |
| 401 | return TCL_ERROR; |
| 402 | } |
| 403 | return TCL_OK; |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | ** Usage: page_read PAGE |
| 408 | ** |
| 409 | ** Return the content of a page |
| 410 | */ |
| 411 | static int page_read( |
| 412 | void *NotUsed, |
| 413 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 414 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 415 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 416 | ){ |
| 417 | char zBuf[100]; |
| 418 | void *pPage; |
| 419 | if( argc!=2 ){ |
| 420 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 421 | " PAGE\"", 0); |
| 422 | return TCL_ERROR; |
| 423 | } |
| 424 | if( Tcl_GetInt(interp, argv[1], (int*)&pPage) ) return TCL_ERROR; |
| 425 | memcpy(zBuf, pPage, sizeof(zBuf)); |
| 426 | Tcl_AppendResult(interp, zBuf, 0); |
| 427 | return TCL_OK; |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | ** Usage: page_number PAGE |
| 432 | ** |
| 433 | ** Return the page number for a page. |
| 434 | */ |
| 435 | static int page_number( |
| 436 | void *NotUsed, |
| 437 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 438 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 439 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 440 | ){ |
| 441 | char zBuf[100]; |
| 442 | void *pPage; |
| 443 | if( argc!=2 ){ |
| 444 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 445 | " PAGE\"", 0); |
| 446 | return TCL_ERROR; |
| 447 | } |
| 448 | if( Tcl_GetInt(interp, argv[1], (int*)&pPage) ) return TCL_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 449 | sprintf(zBuf, "%d", sqlite3pager_pagenumber(pPage)); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 450 | Tcl_AppendResult(interp, zBuf, 0); |
| 451 | return TCL_OK; |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | ** Usage: page_write PAGE DATA |
| 456 | ** |
| 457 | ** Write something into a page. |
| 458 | */ |
| 459 | static int page_write( |
| 460 | void *NotUsed, |
| 461 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 462 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 463 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 464 | ){ |
| 465 | void *pPage; |
| 466 | int rc; |
| 467 | if( argc!=3 ){ |
| 468 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 469 | " PAGE DATA\"", 0); |
| 470 | return TCL_ERROR; |
| 471 | } |
| 472 | if( Tcl_GetInt(interp, argv[1], (int*)&pPage) ) return TCL_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 473 | rc = sqlite3pager_write(pPage); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 474 | if( rc!=SQLITE_OK ){ |
| 475 | Tcl_AppendResult(interp, errorName(rc), 0); |
| 476 | return TCL_ERROR; |
| 477 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 478 | strncpy((char*)pPage, argv[2], test_pagesize-1); |
| 479 | ((char*)pPage)[test_pagesize-1] = 0; |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 480 | return TCL_OK; |
| 481 | } |
| 482 | |
| 483 | /* |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 484 | ** Usage: fake_big_file N FILENAME |
| 485 | ** |
| 486 | ** Write a few bytes at the N megabyte point of FILENAME. This will |
| 487 | ** create a large file. If the file was a valid SQLite database, then |
| 488 | ** the next time the database is opened, SQLite will begin allocating |
| 489 | ** new pages after N. If N is 2096 or bigger, this will test the |
| 490 | ** ability of SQLite to write to large files. |
| 491 | */ |
| 492 | static int fake_big_file( |
| 493 | void *NotUsed, |
| 494 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 495 | int argc, /* Number of arguments */ |
| 496 | const char **argv /* Text of each argument */ |
| 497 | ){ |
| 498 | int rc; |
| 499 | int n; |
| 500 | off_t offset; |
| 501 | OsFile fd; |
| 502 | int readOnly = 0; |
| 503 | if( argc!=3 ){ |
| 504 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 505 | " N-MEGABYTES FILE\"", 0); |
| 506 | return TCL_ERROR; |
| 507 | } |
| 508 | if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 509 | memset(&fd, 0, sizeof(fd)); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 510 | rc = sqlite3OsOpenReadWrite(argv[2], &fd, &readOnly); |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 511 | if( rc ){ |
| 512 | Tcl_AppendResult(interp, "open failed: ", errorName(rc), 0); |
| 513 | return TCL_ERROR; |
| 514 | } |
| 515 | offset = n; |
| 516 | offset *= 1024*1024; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 517 | rc = sqlite3OsSeek(&fd, offset); |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 518 | if( rc ){ |
| 519 | Tcl_AppendResult(interp, "seek failed: ", errorName(rc), 0); |
| 520 | return TCL_ERROR; |
| 521 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 522 | rc = sqlite3OsWrite(&fd, "Hello, World!", 14); |
| 523 | sqlite3OsClose(&fd); |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 524 | if( rc ){ |
| 525 | Tcl_AppendResult(interp, "write failed: ", errorName(rc), 0); |
| 526 | return TCL_ERROR; |
| 527 | } |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 528 | return TCL_OK; |
| 529 | } |
| 530 | |
| 531 | /* |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 532 | ** Register commands with the TCL interpreter. |
| 533 | */ |
| 534 | int Sqlitetest2_Init(Tcl_Interp *interp){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 535 | extern int sqlite3_io_error_pending; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 536 | static struct { |
| 537 | char *zName; |
| 538 | Tcl_CmdProc *xProc; |
| 539 | } aCmd[] = { |
| 540 | { "pager_open", (Tcl_CmdProc*)pager_open }, |
| 541 | { "pager_close", (Tcl_CmdProc*)pager_close }, |
| 542 | { "pager_commit", (Tcl_CmdProc*)pager_commit }, |
| 543 | { "pager_rollback", (Tcl_CmdProc*)pager_rollback }, |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 544 | { "pager_stmt_begin", (Tcl_CmdProc*)pager_stmt_begin }, |
| 545 | { "pager_stmt_commit", (Tcl_CmdProc*)pager_stmt_commit }, |
| 546 | { "pager_stmt_rollback", (Tcl_CmdProc*)pager_stmt_rollback }, |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 547 | { "pager_stats", (Tcl_CmdProc*)pager_stats }, |
| 548 | { "pager_pagecount", (Tcl_CmdProc*)pager_pagecount }, |
| 549 | { "page_get", (Tcl_CmdProc*)page_get }, |
| 550 | { "page_lookup", (Tcl_CmdProc*)page_lookup }, |
| 551 | { "page_unref", (Tcl_CmdProc*)page_unref }, |
| 552 | { "page_read", (Tcl_CmdProc*)page_read }, |
| 553 | { "page_write", (Tcl_CmdProc*)page_write }, |
| 554 | { "page_number", (Tcl_CmdProc*)page_number }, |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 555 | { "fake_big_file", (Tcl_CmdProc*)fake_big_file }, |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 556 | }; |
| 557 | int i; |
| 558 | for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ |
| 559 | Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); |
| 560 | } |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 561 | Tcl_LinkVar(interp, "sqlite_io_error_pending", |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 562 | (char*)&sqlite3_io_error_pending, TCL_LINK_INT); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 563 | Tcl_LinkVar(interp, "pager_pagesize", |
| 564 | (char*)&test_pagesize, TCL_LINK_INT); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 565 | return TCL_OK; |
| 566 | } |