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. |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 15 | */ |
| 16 | #include "sqliteInt.h" |
mistachkin | 52b1dbb | 2016-07-28 14:37:04 +0000 | [diff] [blame] | 17 | #if defined(INCLUDE_SQLITE_TCL_H) |
| 18 | # include "sqlite_tcl.h" |
| 19 | #else |
| 20 | # include "tcl.h" |
| 21 | #endif |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
drh | 3088d59 | 2008-03-21 16:45:47 +0000 | [diff] [blame] | 24 | #include <ctype.h> |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 25 | |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 26 | extern const char *sqlite3ErrName(int); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 27 | |
| 28 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 29 | ** Page size and reserved size used for testing. |
| 30 | */ |
| 31 | static int test_pagesize = 1024; |
| 32 | |
| 33 | /* |
drh | 4775ecd | 2009-07-24 19:01:19 +0000 | [diff] [blame] | 34 | ** Dummy page reinitializer |
| 35 | */ |
| 36 | static void pager_test_reiniter(DbPage *pNotUsed){ |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | /* |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 41 | ** Usage: pager_open FILENAME N-PAGE |
| 42 | ** |
| 43 | ** Open a new pager |
| 44 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 45 | static int SQLITE_TCLAPI pager_open( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 46 | void *NotUsed, |
| 47 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 48 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 49 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 50 | ){ |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 51 | u32 pageSize; |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 52 | Pager *pPager; |
| 53 | int nPage; |
| 54 | int rc; |
| 55 | char zBuf[100]; |
| 56 | if( argc!=3 ){ |
| 57 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 58 | " FILENAME N-PAGE\"", 0); |
| 59 | return TCL_ERROR; |
| 60 | } |
| 61 | if( Tcl_GetInt(interp, argv[2], &nPage) ) return TCL_ERROR; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 62 | rc = sqlite3PagerOpen(sqlite3_vfs_find(0), &pPager, argv[1], 0, 0, |
drh | 4775ecd | 2009-07-24 19:01:19 +0000 | [diff] [blame] | 63 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB, |
| 64 | pager_test_reiniter); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 65 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 66 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 67 | return TCL_ERROR; |
| 68 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 69 | sqlite3PagerSetCachesize(pPager, nPage); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 70 | pageSize = test_pagesize; |
drh | fa9601a | 2009-06-18 17:22:39 +0000 | [diff] [blame] | 71 | sqlite3PagerSetPagesize(pPager, &pageSize, -1); |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 72 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%p",pPager); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 73 | Tcl_AppendResult(interp, zBuf, 0); |
| 74 | return TCL_OK; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | ** Usage: pager_close ID |
| 79 | ** |
| 80 | ** Close the given pager. |
| 81 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 82 | static int SQLITE_TCLAPI pager_close( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 83 | void *NotUsed, |
| 84 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 85 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 86 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 87 | ){ |
| 88 | Pager *pPager; |
| 89 | int rc; |
| 90 | if( argc!=2 ){ |
| 91 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 92 | " ID\"", 0); |
| 93 | return TCL_ERROR; |
| 94 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 95 | pPager = sqlite3TestTextToPtr(argv[1]); |
dan | 7fb8990 | 2016-08-12 16:21:15 +0000 | [diff] [blame] | 96 | rc = sqlite3PagerClose(pPager, 0); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 97 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 98 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 99 | return TCL_ERROR; |
| 100 | } |
| 101 | return TCL_OK; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | ** Usage: pager_rollback ID |
| 106 | ** |
| 107 | ** Rollback changes |
| 108 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 109 | static int SQLITE_TCLAPI pager_rollback( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 110 | void *NotUsed, |
| 111 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 112 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 113 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 114 | ){ |
| 115 | Pager *pPager; |
| 116 | int rc; |
| 117 | if( argc!=2 ){ |
| 118 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 119 | " ID\"", 0); |
| 120 | return TCL_ERROR; |
| 121 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 122 | pPager = sqlite3TestTextToPtr(argv[1]); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 123 | rc = sqlite3PagerRollback(pPager); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 124 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 125 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 126 | return TCL_ERROR; |
| 127 | } |
| 128 | return TCL_OK; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | ** Usage: pager_commit ID |
| 133 | ** |
| 134 | ** Commit all changes |
| 135 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 136 | static int SQLITE_TCLAPI pager_commit( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 137 | void *NotUsed, |
| 138 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 139 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 140 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 141 | ){ |
| 142 | Pager *pPager; |
| 143 | int rc; |
| 144 | if( argc!=2 ){ |
| 145 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 146 | " ID\"", 0); |
| 147 | return TCL_ERROR; |
| 148 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 149 | pPager = sqlite3TestTextToPtr(argv[1]); |
drh | 49b9d33 | 2009-01-02 18:10:42 +0000 | [diff] [blame] | 150 | rc = sqlite3PagerCommitPhaseOne(pPager, 0, 0); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 151 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 152 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 153 | return TCL_ERROR; |
| 154 | } |
| 155 | rc = sqlite3PagerCommitPhaseTwo(pPager); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 156 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 157 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 158 | return TCL_ERROR; |
| 159 | } |
| 160 | return TCL_OK; |
| 161 | } |
| 162 | |
| 163 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 164 | ** Usage: pager_stmt_begin ID |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 165 | ** |
| 166 | ** Start a new checkpoint. |
| 167 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 168 | static int SQLITE_TCLAPI pager_stmt_begin( |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 169 | void *NotUsed, |
| 170 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 171 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 172 | const char **argv /* Text of each argument */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 173 | ){ |
| 174 | Pager *pPager; |
| 175 | int rc; |
| 176 | if( argc!=2 ){ |
| 177 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 178 | " ID\"", 0); |
| 179 | return TCL_ERROR; |
| 180 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 181 | pPager = sqlite3TestTextToPtr(argv[1]); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 182 | rc = sqlite3PagerOpenSavepoint(pPager, 1); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 183 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 184 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 185 | return TCL_ERROR; |
| 186 | } |
| 187 | return TCL_OK; |
| 188 | } |
| 189 | |
| 190 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 191 | ** Usage: pager_stmt_rollback ID |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 192 | ** |
| 193 | ** Rollback changes to a checkpoint |
| 194 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 195 | static int SQLITE_TCLAPI pager_stmt_rollback( |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 196 | void *NotUsed, |
| 197 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 198 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 199 | const char **argv /* Text of each argument */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 200 | ){ |
| 201 | Pager *pPager; |
| 202 | int rc; |
| 203 | if( argc!=2 ){ |
| 204 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 205 | " ID\"", 0); |
| 206 | return TCL_ERROR; |
| 207 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 208 | pPager = sqlite3TestTextToPtr(argv[1]); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 209 | rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, 0); |
| 210 | sqlite3PagerSavepoint(pPager, SAVEPOINT_RELEASE, 0); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 211 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 212 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 213 | return TCL_ERROR; |
| 214 | } |
| 215 | return TCL_OK; |
| 216 | } |
| 217 | |
| 218 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 219 | ** Usage: pager_stmt_commit ID |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 220 | ** |
| 221 | ** Commit changes to a checkpoint |
| 222 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 223 | static int SQLITE_TCLAPI pager_stmt_commit( |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 224 | void *NotUsed, |
| 225 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 226 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 227 | const char **argv /* Text of each argument */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 228 | ){ |
| 229 | Pager *pPager; |
| 230 | int rc; |
| 231 | if( argc!=2 ){ |
| 232 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 233 | " ID\"", 0); |
| 234 | return TCL_ERROR; |
| 235 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 236 | pPager = sqlite3TestTextToPtr(argv[1]); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 237 | rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_RELEASE, 0); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 238 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 239 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 240 | return TCL_ERROR; |
| 241 | } |
| 242 | return TCL_OK; |
| 243 | } |
| 244 | |
| 245 | /* |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 246 | ** Usage: pager_stats ID |
| 247 | ** |
| 248 | ** Return pager statistics. |
| 249 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 250 | static int SQLITE_TCLAPI pager_stats( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 251 | void *NotUsed, |
| 252 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 253 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 254 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 255 | ){ |
| 256 | Pager *pPager; |
| 257 | int i, *a; |
| 258 | if( argc!=2 ){ |
| 259 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 260 | " ID\"", 0); |
| 261 | return TCL_ERROR; |
| 262 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 263 | pPager = sqlite3TestTextToPtr(argv[1]); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 264 | a = sqlite3PagerStats(pPager); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 265 | for(i=0; i<9; i++){ |
| 266 | static char *zName[] = { |
| 267 | "ref", "page", "max", "size", "state", "err", |
| 268 | "hit", "miss", "ovfl", |
| 269 | }; |
| 270 | char zBuf[100]; |
| 271 | Tcl_AppendElement(interp, zName[i]); |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 272 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",a[i]); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 273 | Tcl_AppendElement(interp, zBuf); |
| 274 | } |
| 275 | return TCL_OK; |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | ** Usage: pager_pagecount ID |
| 280 | ** |
| 281 | ** Return the size of the database file. |
| 282 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 283 | static int SQLITE_TCLAPI pager_pagecount( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 284 | void *NotUsed, |
| 285 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 286 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 287 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 288 | ){ |
| 289 | Pager *pPager; |
| 290 | char zBuf[100]; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 291 | int nPage; |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 292 | if( argc!=2 ){ |
| 293 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 294 | " ID\"", 0); |
| 295 | return TCL_ERROR; |
| 296 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 297 | pPager = sqlite3TestTextToPtr(argv[1]); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 298 | sqlite3PagerPagecount(pPager, &nPage); |
| 299 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nPage); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 300 | Tcl_AppendResult(interp, zBuf, 0); |
| 301 | return TCL_OK; |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | ** Usage: page_get ID PGNO |
| 306 | ** |
| 307 | ** Return a pointer to a page from the database. |
| 308 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 309 | static int SQLITE_TCLAPI page_get( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 310 | void *NotUsed, |
| 311 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 312 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 313 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 314 | ){ |
| 315 | Pager *pPager; |
| 316 | char zBuf[100]; |
mistachkin | 27b2f05 | 2015-01-12 19:49:46 +0000 | [diff] [blame] | 317 | DbPage *pPage = 0; |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 318 | int pgno; |
| 319 | int rc; |
| 320 | if( argc!=3 ){ |
| 321 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 322 | " ID PGNO\"", 0); |
| 323 | return TCL_ERROR; |
| 324 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 325 | pPager = sqlite3TestTextToPtr(argv[1]); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 326 | if( Tcl_GetInt(interp, argv[2], &pgno) ) return TCL_ERROR; |
danielk1977 | 89bc4bc | 2009-07-21 19:25:24 +0000 | [diff] [blame] | 327 | rc = sqlite3PagerSharedLock(pPager); |
| 328 | if( rc==SQLITE_OK ){ |
drh | 9584f58 | 2015-11-04 20:22:37 +0000 | [diff] [blame] | 329 | rc = sqlite3PagerGet(pPager, pgno, &pPage, 0); |
danielk1977 | 89bc4bc | 2009-07-21 19:25:24 +0000 | [diff] [blame] | 330 | } |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 331 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 332 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 333 | return TCL_ERROR; |
| 334 | } |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 335 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%p",pPage); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 336 | Tcl_AppendResult(interp, zBuf, 0); |
| 337 | return TCL_OK; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | ** Usage: page_lookup ID PGNO |
| 342 | ** |
| 343 | ** Return a pointer to a page if the page is already in cache. |
| 344 | ** If not in cache, return an empty string. |
| 345 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 346 | static int SQLITE_TCLAPI page_lookup( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 347 | void *NotUsed, |
| 348 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 349 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 350 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 351 | ){ |
| 352 | Pager *pPager; |
| 353 | char zBuf[100]; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 354 | DbPage *pPage; |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 355 | int pgno; |
| 356 | if( argc!=3 ){ |
| 357 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 358 | " ID PGNO\"", 0); |
| 359 | return TCL_ERROR; |
| 360 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 361 | pPager = sqlite3TestTextToPtr(argv[1]); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 362 | if( Tcl_GetInt(interp, argv[2], &pgno) ) return TCL_ERROR; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 363 | pPage = sqlite3PagerLookup(pPager, pgno); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 364 | if( pPage ){ |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 365 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%p",pPage); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 366 | Tcl_AppendResult(interp, zBuf, 0); |
| 367 | } |
| 368 | return TCL_OK; |
| 369 | } |
| 370 | |
| 371 | /* |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 372 | ** Usage: pager_truncate ID PGNO |
| 373 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 374 | static int SQLITE_TCLAPI pager_truncate( |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 375 | void *NotUsed, |
| 376 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 377 | int argc, /* Number of arguments */ |
| 378 | const char **argv /* Text of each argument */ |
| 379 | ){ |
| 380 | Pager *pPager; |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 381 | int pgno; |
| 382 | if( argc!=3 ){ |
| 383 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 384 | " ID PGNO\"", 0); |
| 385 | return TCL_ERROR; |
| 386 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 387 | pPager = sqlite3TestTextToPtr(argv[1]); |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 388 | if( Tcl_GetInt(interp, argv[2], &pgno) ) return TCL_ERROR; |
danielk1977 | f90b726 | 2009-01-07 15:18:20 +0000 | [diff] [blame] | 389 | sqlite3PagerTruncateImage(pPager, pgno); |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 390 | return TCL_OK; |
| 391 | } |
| 392 | |
| 393 | |
| 394 | /* |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 395 | ** Usage: page_unref PAGE |
| 396 | ** |
| 397 | ** Drop a pointer to a page. |
| 398 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 399 | static int SQLITE_TCLAPI page_unref( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 400 | void *NotUsed, |
| 401 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 402 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 403 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 404 | ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 405 | DbPage *pPage; |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 406 | if( argc!=2 ){ |
| 407 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 408 | " PAGE\"", 0); |
| 409 | return TCL_ERROR; |
| 410 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 411 | pPage = (DbPage *)sqlite3TestTextToPtr(argv[1]); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 412 | sqlite3PagerUnref(pPage); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 413 | return TCL_OK; |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | ** Usage: page_read PAGE |
| 418 | ** |
| 419 | ** Return the content of a page |
| 420 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 421 | static int SQLITE_TCLAPI page_read( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 422 | void *NotUsed, |
| 423 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 424 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 425 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 426 | ){ |
| 427 | char zBuf[100]; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 428 | DbPage *pPage; |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 429 | if( argc!=2 ){ |
| 430 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 431 | " PAGE\"", 0); |
| 432 | return TCL_ERROR; |
| 433 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 434 | pPage = sqlite3TestTextToPtr(argv[1]); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 435 | memcpy(zBuf, sqlite3PagerGetData(pPage), sizeof(zBuf)); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 436 | Tcl_AppendResult(interp, zBuf, 0); |
| 437 | return TCL_OK; |
| 438 | } |
| 439 | |
| 440 | /* |
| 441 | ** Usage: page_number PAGE |
| 442 | ** |
| 443 | ** Return the page number for a page. |
| 444 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 445 | static int SQLITE_TCLAPI page_number( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 446 | void *NotUsed, |
| 447 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 448 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 449 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 450 | ){ |
| 451 | char zBuf[100]; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 452 | DbPage *pPage; |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 453 | if( argc!=2 ){ |
| 454 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 455 | " PAGE\"", 0); |
| 456 | return TCL_ERROR; |
| 457 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 458 | pPage = (DbPage *)sqlite3TestTextToPtr(argv[1]); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 459 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", sqlite3PagerPagenumber(pPage)); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 460 | Tcl_AppendResult(interp, zBuf, 0); |
| 461 | return TCL_OK; |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | ** Usage: page_write PAGE DATA |
| 466 | ** |
| 467 | ** Write something into a page. |
| 468 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 469 | static int SQLITE_TCLAPI page_write( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 470 | void *NotUsed, |
| 471 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 472 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 473 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 474 | ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 475 | DbPage *pPage; |
| 476 | char *pData; |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 477 | int rc; |
| 478 | if( argc!=3 ){ |
| 479 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 480 | " PAGE DATA\"", 0); |
| 481 | return TCL_ERROR; |
| 482 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 483 | pPage = (DbPage *)sqlite3TestTextToPtr(argv[1]); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 484 | rc = sqlite3PagerWrite(pPage); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 485 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 486 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 487 | return TCL_ERROR; |
| 488 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 489 | pData = sqlite3PagerGetData(pPage); |
| 490 | strncpy(pData, argv[2], test_pagesize-1); |
| 491 | pData[test_pagesize-1] = 0; |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 492 | return TCL_OK; |
| 493 | } |
| 494 | |
danielk1977 | 44ee5bf | 2005-05-27 09:41:12 +0000 | [diff] [blame] | 495 | #ifndef SQLITE_OMIT_DISKIO |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 496 | /* |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 497 | ** Usage: fake_big_file N FILENAME |
| 498 | ** |
| 499 | ** Write a few bytes at the N megabyte point of FILENAME. This will |
| 500 | ** create a large file. If the file was a valid SQLite database, then |
| 501 | ** the next time the database is opened, SQLite will begin allocating |
| 502 | ** new pages after N. If N is 2096 or bigger, this will test the |
| 503 | ** ability of SQLite to write to large files. |
| 504 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 505 | static int SQLITE_TCLAPI fake_big_file( |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 506 | void *NotUsed, |
| 507 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 508 | int argc, /* Number of arguments */ |
| 509 | const char **argv /* Text of each argument */ |
| 510 | ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 511 | sqlite3_vfs *pVfs; |
| 512 | sqlite3_file *fd = 0; |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 513 | int rc; |
| 514 | int n; |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 515 | i64 offset; |
drh | 69578ac | 2012-01-11 00:38:51 +0000 | [diff] [blame] | 516 | char *zFile; |
| 517 | int nFile; |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 518 | if( argc!=3 ){ |
| 519 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 520 | " N-MEGABYTES FILE\"", 0); |
| 521 | return TCL_ERROR; |
| 522 | } |
| 523 | if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 524 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 525 | pVfs = sqlite3_vfs_find(0); |
drh | 83cc139 | 2012-04-19 18:04:28 +0000 | [diff] [blame] | 526 | nFile = (int)strlen(argv[2]); |
drh | 69578ac | 2012-01-11 00:38:51 +0000 | [diff] [blame] | 527 | zFile = sqlite3_malloc( nFile+2 ); |
| 528 | if( zFile==0 ) return TCL_ERROR; |
| 529 | memcpy(zFile, argv[2], nFile+1); |
| 530 | zFile[nFile+1] = 0; |
| 531 | rc = sqlite3OsOpenMalloc(pVfs, zFile, &fd, |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 532 | (SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB), 0 |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 533 | ); |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 534 | if( rc ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 535 | Tcl_AppendResult(interp, "open failed: ", sqlite3ErrName(rc), 0); |
drh | 69578ac | 2012-01-11 00:38:51 +0000 | [diff] [blame] | 536 | sqlite3_free(zFile); |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 537 | return TCL_ERROR; |
| 538 | } |
| 539 | offset = n; |
| 540 | offset *= 1024*1024; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 541 | rc = sqlite3OsWrite(fd, "Hello, World!", 14, offset); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 542 | sqlite3OsCloseFree(fd); |
drh | 69578ac | 2012-01-11 00:38:51 +0000 | [diff] [blame] | 543 | sqlite3_free(zFile); |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 544 | if( rc ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 545 | Tcl_AppendResult(interp, "write failed: ", sqlite3ErrName(rc), 0); |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 546 | return TCL_ERROR; |
| 547 | } |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 548 | return TCL_OK; |
| 549 | } |
danielk1977 | 44ee5bf | 2005-05-27 09:41:12 +0000 | [diff] [blame] | 550 | #endif |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 551 | |
drh | 3088d59 | 2008-03-21 16:45:47 +0000 | [diff] [blame] | 552 | |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 553 | /* |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 554 | ** test_control_pending_byte PENDING_BYTE |
| 555 | ** |
| 556 | ** Set the PENDING_BYTE using the sqlite3_test_control() interface. |
| 557 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 558 | static int SQLITE_TCLAPI testPendingByte( |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 559 | void *NotUsed, |
| 560 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 561 | int argc, /* Number of arguments */ |
| 562 | const char **argv /* Text of each argument */ |
| 563 | ){ |
| 564 | int pbyte; |
| 565 | int rc; |
| 566 | if( argc!=2 ){ |
| 567 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 568 | " PENDING-BYTE\"", (void*)0); |
dan | 8d6ad1c | 2010-05-04 10:36:20 +0000 | [diff] [blame] | 569 | return TCL_ERROR; |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 570 | } |
| 571 | if( Tcl_GetInt(interp, argv[1], &pbyte) ) return TCL_ERROR; |
| 572 | rc = sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, pbyte); |
| 573 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 574 | return TCL_OK; |
drh | c007f61 | 2014-05-16 14:17:01 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /* |
| 578 | ** The sqlite3FaultSim() callback: |
| 579 | */ |
| 580 | static Tcl_Interp *faultSimInterp = 0; |
| 581 | static int faultSimScriptSize = 0; |
| 582 | static char *faultSimScript; |
| 583 | static int faultSimCallback(int x){ |
| 584 | char zInt[30]; |
| 585 | int i; |
| 586 | int isNeg; |
| 587 | int rc; |
| 588 | if( x==0 ){ |
| 589 | memcpy(faultSimScript+faultSimScriptSize, "0", 2); |
| 590 | }else{ |
| 591 | /* Convert x to text without using any sqlite3 routines */ |
| 592 | if( x<0 ){ |
| 593 | isNeg = 1; |
| 594 | x = -x; |
| 595 | }else{ |
| 596 | isNeg = 0; |
| 597 | } |
| 598 | zInt[sizeof(zInt)-1] = 0; |
| 599 | for(i=sizeof(zInt)-2; i>0 && x>0; i--, x /= 10){ |
| 600 | zInt[i] = (x%10) + '0'; |
| 601 | } |
| 602 | if( isNeg ) zInt[i--] = '-'; |
| 603 | memcpy(faultSimScript+faultSimScriptSize, zInt+i+1, sizeof(zInt)-i); |
| 604 | } |
| 605 | rc = Tcl_Eval(faultSimInterp, faultSimScript); |
| 606 | if( rc ){ |
| 607 | fprintf(stderr, "fault simulator script failed: [%s]", faultSimScript); |
| 608 | rc = SQLITE_ERROR; |
| 609 | }else{ |
| 610 | rc = atoi(Tcl_GetStringResult(faultSimInterp)); |
| 611 | } |
| 612 | Tcl_ResetResult(faultSimInterp); |
| 613 | return rc; |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | ** sqlite3_test_control_fault_install SCRIPT |
| 618 | ** |
| 619 | ** Arrange to invoke SCRIPT with the integer argument to sqlite3FaultSim() |
| 620 | ** appended, whenever sqlite3FaultSim() is called. Or, if SCRIPT is the |
| 621 | ** empty string, cancel the sqlite3FaultSim() callback. |
| 622 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 623 | static int SQLITE_TCLAPI faultInstallCmd( |
drh | c007f61 | 2014-05-16 14:17:01 +0000 | [diff] [blame] | 624 | void *NotUsed, |
| 625 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 626 | int argc, /* Number of arguments */ |
| 627 | const char **argv /* Text of each argument */ |
| 628 | ){ |
| 629 | const char *zScript; |
| 630 | int nScript; |
| 631 | int rc; |
| 632 | if( argc!=1 && argc!=2 ){ |
| 633 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 634 | " SCRIPT\"", (void*)0); |
| 635 | } |
| 636 | zScript = argc==2 ? argv[1] : ""; |
| 637 | nScript = (int)strlen(zScript); |
| 638 | if( faultSimScript ){ |
| 639 | free(faultSimScript); |
| 640 | faultSimScript = 0; |
| 641 | } |
| 642 | if( nScript==0 ){ |
| 643 | rc = sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL, 0); |
| 644 | }else{ |
| 645 | faultSimScript = malloc( nScript+100 ); |
| 646 | if( faultSimScript==0 ){ |
| 647 | Tcl_AppendResult(interp, "out of memory", (void*)0); |
| 648 | return SQLITE_ERROR; |
| 649 | } |
| 650 | memcpy(faultSimScript, zScript, nScript); |
| 651 | faultSimScript[nScript] = ' '; |
| 652 | faultSimScriptSize = nScript+1; |
| 653 | faultSimInterp = interp; |
| 654 | rc = sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL, faultSimCallback); |
| 655 | } |
| 656 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 657 | return SQLITE_OK; |
| 658 | } |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 659 | |
| 660 | /* |
drh | 3088d59 | 2008-03-21 16:45:47 +0000 | [diff] [blame] | 661 | ** sqlite3BitvecBuiltinTest SIZE PROGRAM |
| 662 | ** |
| 663 | ** Invoke the SQLITE_TESTCTRL_BITVEC_TEST operator on test_control. |
| 664 | ** See comments on sqlite3BitvecBuiltinTest() for additional information. |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 665 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 666 | static int SQLITE_TCLAPI testBitvecBuiltinTest( |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 667 | void *NotUsed, |
| 668 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 669 | int argc, /* Number of arguments */ |
| 670 | const char **argv /* Text of each argument */ |
| 671 | ){ |
drh | 3088d59 | 2008-03-21 16:45:47 +0000 | [diff] [blame] | 672 | int sz, rc; |
| 673 | int nProg = 0; |
| 674 | int aProg[100]; |
| 675 | const char *z; |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 676 | if( argc!=3 ){ |
drh | 3088d59 | 2008-03-21 16:45:47 +0000 | [diff] [blame] | 677 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 678 | " SIZE PROGRAM\"", (void*)0); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 679 | } |
drh | 3088d59 | 2008-03-21 16:45:47 +0000 | [diff] [blame] | 680 | if( Tcl_GetInt(interp, argv[1], &sz) ) return TCL_ERROR; |
| 681 | z = argv[2]; |
| 682 | while( nProg<99 && *z ){ |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 683 | while( *z && !sqlite3Isdigit(*z) ){ z++; } |
drh | 3088d59 | 2008-03-21 16:45:47 +0000 | [diff] [blame] | 684 | if( *z==0 ) break; |
| 685 | aProg[nProg++] = atoi(z); |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 686 | while( sqlite3Isdigit(*z) ){ z++; } |
drh | 3088d59 | 2008-03-21 16:45:47 +0000 | [diff] [blame] | 687 | } |
| 688 | aProg[nProg] = 0; |
| 689 | rc = sqlite3_test_control(SQLITE_TESTCTRL_BITVEC_TEST, sz, aProg); |
| 690 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 691 | return TCL_OK; |
| 692 | } |
drh | f5e7bb5 | 2008-02-18 14:47:33 +0000 | [diff] [blame] | 693 | |
| 694 | /* |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 695 | ** Register commands with the TCL interpreter. |
| 696 | */ |
| 697 | int Sqlitetest2_Init(Tcl_Interp *interp){ |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 698 | extern int sqlite3_io_error_persist; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 699 | extern int sqlite3_io_error_pending; |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 700 | extern int sqlite3_io_error_hit; |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 701 | extern int sqlite3_io_error_hardhit; |
drh | 047d483 | 2004-10-01 14:38:02 +0000 | [diff] [blame] | 702 | extern int sqlite3_diskfull_pending; |
drh | f307a4a | 2005-09-09 10:46:19 +0000 | [diff] [blame] | 703 | extern int sqlite3_diskfull; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 704 | static struct { |
| 705 | char *zName; |
| 706 | Tcl_CmdProc *xProc; |
| 707 | } aCmd[] = { |
| 708 | { "pager_open", (Tcl_CmdProc*)pager_open }, |
| 709 | { "pager_close", (Tcl_CmdProc*)pager_close }, |
| 710 | { "pager_commit", (Tcl_CmdProc*)pager_commit }, |
| 711 | { "pager_rollback", (Tcl_CmdProc*)pager_rollback }, |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 712 | { "pager_stmt_begin", (Tcl_CmdProc*)pager_stmt_begin }, |
| 713 | { "pager_stmt_commit", (Tcl_CmdProc*)pager_stmt_commit }, |
| 714 | { "pager_stmt_rollback", (Tcl_CmdProc*)pager_stmt_rollback }, |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 715 | { "pager_stats", (Tcl_CmdProc*)pager_stats }, |
| 716 | { "pager_pagecount", (Tcl_CmdProc*)pager_pagecount }, |
| 717 | { "page_get", (Tcl_CmdProc*)page_get }, |
| 718 | { "page_lookup", (Tcl_CmdProc*)page_lookup }, |
| 719 | { "page_unref", (Tcl_CmdProc*)page_unref }, |
| 720 | { "page_read", (Tcl_CmdProc*)page_read }, |
| 721 | { "page_write", (Tcl_CmdProc*)page_write }, |
| 722 | { "page_number", (Tcl_CmdProc*)page_number }, |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 723 | { "pager_truncate", (Tcl_CmdProc*)pager_truncate }, |
danielk1977 | 44ee5bf | 2005-05-27 09:41:12 +0000 | [diff] [blame] | 724 | #ifndef SQLITE_OMIT_DISKIO |
drh | d0d006e | 2002-12-01 02:00:57 +0000 | [diff] [blame] | 725 | { "fake_big_file", (Tcl_CmdProc*)fake_big_file }, |
danielk1977 | 44ee5bf | 2005-05-27 09:41:12 +0000 | [diff] [blame] | 726 | #endif |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 727 | { "sqlite3BitvecBuiltinTest",(Tcl_CmdProc*)testBitvecBuiltinTest }, |
drh | c007f61 | 2014-05-16 14:17:01 +0000 | [diff] [blame] | 728 | { "sqlite3_test_control_pending_byte", (Tcl_CmdProc*)testPendingByte }, |
| 729 | { "sqlite3_test_control_fault_install", (Tcl_CmdProc*)faultInstallCmd }, |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 730 | }; |
| 731 | int i; |
| 732 | for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ |
| 733 | Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); |
| 734 | } |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 735 | Tcl_LinkVar(interp, "sqlite_io_error_pending", |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 736 | (char*)&sqlite3_io_error_pending, TCL_LINK_INT); |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 737 | Tcl_LinkVar(interp, "sqlite_io_error_persist", |
| 738 | (char*)&sqlite3_io_error_persist, TCL_LINK_INT); |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 739 | Tcl_LinkVar(interp, "sqlite_io_error_hit", |
| 740 | (char*)&sqlite3_io_error_hit, TCL_LINK_INT); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 741 | Tcl_LinkVar(interp, "sqlite_io_error_hardhit", |
| 742 | (char*)&sqlite3_io_error_hardhit, TCL_LINK_INT); |
drh | 047d483 | 2004-10-01 14:38:02 +0000 | [diff] [blame] | 743 | Tcl_LinkVar(interp, "sqlite_diskfull_pending", |
| 744 | (char*)&sqlite3_diskfull_pending, TCL_LINK_INT); |
drh | f307a4a | 2005-09-09 10:46:19 +0000 | [diff] [blame] | 745 | Tcl_LinkVar(interp, "sqlite_diskfull", |
| 746 | (char*)&sqlite3_diskfull, TCL_LINK_INT); |
drh | f83dc1e | 2010-06-03 12:09:52 +0000 | [diff] [blame] | 747 | #ifndef SQLITE_OMIT_WSD |
danielk1977 | fd5f5b6 | 2005-09-16 09:52:29 +0000 | [diff] [blame] | 748 | Tcl_LinkVar(interp, "sqlite_pending_byte", |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 749 | (char*)&sqlite3PendingByte, TCL_LINK_INT | TCL_LINK_READ_ONLY); |
drh | f83dc1e | 2010-06-03 12:09:52 +0000 | [diff] [blame] | 750 | #endif |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 751 | return TCL_OK; |
| 752 | } |