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 btree.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" |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 17 | #include "btreeInt.h" |
mistachkin | 52b1dbb | 2016-07-28 14:37:04 +0000 | [diff] [blame] | 18 | #if defined(INCLUDE_SQLITE_TCL_H) |
| 19 | # include "sqlite_tcl.h" |
| 20 | #else |
| 21 | # include "tcl.h" |
| 22 | #endif |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 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 | a2451e2 | 2007-08-20 23:50:24 +0000 | [diff] [blame] | 29 | ** A bogus sqlite3 connection structure for use in the btree |
| 30 | ** tests. |
| 31 | */ |
| 32 | static sqlite3 sDb; |
| 33 | static int nRefSqlite3 = 0; |
| 34 | |
| 35 | /* |
drh | 75c014c | 2010-08-30 15:02:28 +0000 | [diff] [blame] | 36 | ** Usage: btree_open FILENAME NCACHE |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 37 | ** |
| 38 | ** Open a new database |
| 39 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 40 | static int SQLITE_TCLAPI btree_open( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 41 | void *NotUsed, |
| 42 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 43 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 44 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 45 | ){ |
| 46 | Btree *pBt; |
drh | 75c014c | 2010-08-30 15:02:28 +0000 | [diff] [blame] | 47 | int rc, nCache; |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 48 | char zBuf[100]; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 49 | int n; |
| 50 | char *zFilename; |
drh | 75c014c | 2010-08-30 15:02:28 +0000 | [diff] [blame] | 51 | if( argc!=3 ){ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 52 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 53 | " FILENAME NCACHE FLAGS\"", 0); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 54 | return TCL_ERROR; |
| 55 | } |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 56 | if( Tcl_GetInt(interp, argv[2], &nCache) ) return TCL_ERROR; |
drh | a2451e2 | 2007-08-20 23:50:24 +0000 | [diff] [blame] | 57 | nRefSqlite3++; |
| 58 | if( nRefSqlite3==1 ){ |
| 59 | sDb.pVfs = sqlite3_vfs_find(0); |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 60 | sDb.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 61 | sqlite3_mutex_enter(sDb.mutex); |
drh | a2451e2 | 2007-08-20 23:50:24 +0000 | [diff] [blame] | 62 | } |
drh | 83cc139 | 2012-04-19 18:04:28 +0000 | [diff] [blame] | 63 | n = (int)strlen(argv[1]); |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 64 | zFilename = sqlite3_malloc( n+2 ); |
| 65 | if( zFilename==0 ) return TCL_ERROR; |
| 66 | memcpy(zFilename, argv[1], n+1); |
| 67 | zFilename[n+1] = 0; |
| 68 | rc = sqlite3BtreeOpen(sDb.pVfs, zFilename, &sDb, &pBt, 0, |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 69 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB); |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 70 | sqlite3_free(zFilename); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 71 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 72 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 73 | return TCL_ERROR; |
| 74 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 75 | sqlite3BtreeSetCacheSize(pBt, nCache); |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 76 | sqlite3_snprintf(sizeof(zBuf), zBuf,"%p", pBt); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 77 | Tcl_AppendResult(interp, zBuf, 0); |
| 78 | return TCL_OK; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | ** Usage: btree_close ID |
| 83 | ** |
| 84 | ** Close the given database. |
| 85 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 86 | static int SQLITE_TCLAPI btree_close( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 87 | void *NotUsed, |
| 88 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 89 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 90 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 91 | ){ |
| 92 | Btree *pBt; |
| 93 | int rc; |
| 94 | if( argc!=2 ){ |
| 95 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 96 | " ID\"", 0); |
| 97 | return TCL_ERROR; |
| 98 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 99 | pBt = sqlite3TestTextToPtr(argv[1]); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 100 | rc = sqlite3BtreeClose(pBt); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 101 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 102 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 103 | return TCL_ERROR; |
| 104 | } |
drh | a2451e2 | 2007-08-20 23:50:24 +0000 | [diff] [blame] | 105 | nRefSqlite3--; |
| 106 | if( nRefSqlite3==0 ){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 107 | sqlite3_mutex_leave(sDb.mutex); |
drh | a2451e2 | 2007-08-20 23:50:24 +0000 | [diff] [blame] | 108 | sqlite3_mutex_free(sDb.mutex); |
| 109 | sDb.mutex = 0; |
drh | a2451e2 | 2007-08-20 23:50:24 +0000 | [diff] [blame] | 110 | sDb.pVfs = 0; |
| 111 | } |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 112 | return TCL_OK; |
| 113 | } |
| 114 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 115 | |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 116 | /* |
| 117 | ** Usage: btree_begin_transaction ID |
| 118 | ** |
| 119 | ** Start a new transaction |
| 120 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 121 | static int SQLITE_TCLAPI btree_begin_transaction( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 122 | void *NotUsed, |
| 123 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 124 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 125 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 126 | ){ |
| 127 | Btree *pBt; |
| 128 | int rc; |
| 129 | if( argc!=2 ){ |
| 130 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 131 | " ID\"", 0); |
| 132 | return TCL_ERROR; |
| 133 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 134 | pBt = sqlite3TestTextToPtr(argv[1]); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 135 | sqlite3BtreeEnter(pBt); |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 136 | rc = sqlite3BtreeBeginTrans(pBt, 1); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 137 | sqlite3BtreeLeave(pBt); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 138 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 139 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 140 | return TCL_ERROR; |
| 141 | } |
| 142 | return TCL_OK; |
| 143 | } |
| 144 | |
| 145 | /* |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 146 | ** Usage: btree_pager_stats ID |
| 147 | ** |
| 148 | ** Returns pager statistics |
| 149 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 150 | static int SQLITE_TCLAPI btree_pager_stats( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 151 | void *NotUsed, |
| 152 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 153 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 154 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 155 | ){ |
| 156 | Btree *pBt; |
| 157 | int i; |
| 158 | int *a; |
| 159 | |
| 160 | if( argc!=2 ){ |
| 161 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 162 | " ID\"", 0); |
| 163 | return TCL_ERROR; |
| 164 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 165 | pBt = sqlite3TestTextToPtr(argv[1]); |
danielk1977 | f3c6265 | 2007-08-30 08:27:39 +0000 | [diff] [blame] | 166 | |
| 167 | /* Normally in this file, with a b-tree handle opened using the |
| 168 | ** [btree_open] command it is safe to call sqlite3BtreeEnter() directly. |
| 169 | ** But this function is sometimes called with a btree handle obtained |
| 170 | ** from an open SQLite connection (using [btree_from_db]). In this case |
| 171 | ** we need to obtain the mutex for the controlling SQLite handle before |
| 172 | ** it is safe to call sqlite3BtreeEnter(). |
| 173 | */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 174 | sqlite3_mutex_enter(pBt->db->mutex); |
danielk1977 | f3c6265 | 2007-08-30 08:27:39 +0000 | [diff] [blame] | 175 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 176 | sqlite3BtreeEnter(pBt); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 177 | a = sqlite3PagerStats(sqlite3BtreePager(pBt)); |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 178 | for(i=0; i<11; i++){ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 179 | static char *zName[] = { |
| 180 | "ref", "page", "max", "size", "state", "err", |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 181 | "hit", "miss", "ovfl", "read", "write" |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 182 | }; |
| 183 | char zBuf[100]; |
| 184 | Tcl_AppendElement(interp, zName[i]); |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 185 | sqlite3_snprintf(sizeof(zBuf), zBuf,"%d",a[i]); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 186 | Tcl_AppendElement(interp, zBuf); |
| 187 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 188 | sqlite3BtreeLeave(pBt); |
danielk1977 | f3c6265 | 2007-08-30 08:27:39 +0000 | [diff] [blame] | 189 | |
| 190 | /* Release the mutex on the SQLite handle that controls this b-tree */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 191 | sqlite3_mutex_leave(pBt->db->mutex); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 192 | return TCL_OK; |
| 193 | } |
| 194 | |
| 195 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 196 | ** Usage: btree_cursor ID TABLENUM WRITEABLE |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 197 | ** |
| 198 | ** Create a new cursor. Return the ID for the cursor. |
| 199 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 200 | static int SQLITE_TCLAPI btree_cursor( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 201 | void *NotUsed, |
| 202 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 203 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 204 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 205 | ){ |
| 206 | Btree *pBt; |
| 207 | int iTable; |
| 208 | BtCursor *pCur; |
dan | 93a696f | 2010-01-07 11:27:30 +0000 | [diff] [blame] | 209 | int rc = SQLITE_OK; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 210 | int wrFlag; |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 211 | char zBuf[30]; |
| 212 | |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 213 | if( argc!=4 ){ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 214 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 215 | " ID TABLENUM WRITEABLE\"", 0); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 216 | return TCL_ERROR; |
| 217 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 218 | pBt = sqlite3TestTextToPtr(argv[1]); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 219 | if( Tcl_GetInt(interp, argv[2], &iTable) ) return TCL_ERROR; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 220 | if( Tcl_GetBoolean(interp, argv[3], &wrFlag) ) return TCL_ERROR; |
dan | fd261ec | 2015-10-22 20:54:33 +0000 | [diff] [blame] | 221 | if( wrFlag ) wrFlag = BTREE_WRCSR; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 222 | pCur = (BtCursor *)ckalloc(sqlite3BtreeCursorSize()); |
| 223 | memset(pCur, 0, sqlite3BtreeCursorSize()); |
dan | 6a75c8a | 2015-10-30 20:54:25 +0000 | [diff] [blame] | 224 | sqlite3_mutex_enter(pBt->db->mutex); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 225 | sqlite3BtreeEnter(pBt); |
dan | 93a696f | 2010-01-07 11:27:30 +0000 | [diff] [blame] | 226 | #ifndef SQLITE_OMIT_SHARED_CACHE |
dan | 6b51364 | 2015-10-26 16:31:18 +0000 | [diff] [blame] | 227 | rc = sqlite3BtreeLockTable(pBt, iTable, !!wrFlag); |
dan | 93a696f | 2010-01-07 11:27:30 +0000 | [diff] [blame] | 228 | #endif |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 229 | if( rc==SQLITE_OK ){ |
| 230 | rc = sqlite3BtreeCursor(pBt, iTable, wrFlag, 0, pCur); |
| 231 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 232 | sqlite3BtreeLeave(pBt); |
dan | 6a75c8a | 2015-10-30 20:54:25 +0000 | [diff] [blame] | 233 | sqlite3_mutex_leave(pBt->db->mutex); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 234 | if( rc ){ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 235 | ckfree((char *)pCur); |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 236 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 237 | return TCL_ERROR; |
| 238 | } |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 239 | sqlite3_snprintf(sizeof(zBuf), zBuf,"%p", pCur); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 240 | Tcl_AppendResult(interp, zBuf, 0); |
| 241 | return SQLITE_OK; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | ** Usage: btree_close_cursor ID |
| 246 | ** |
| 247 | ** Close a cursor opened using btree_cursor. |
| 248 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 249 | static int SQLITE_TCLAPI btree_close_cursor( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 250 | void *NotUsed, |
| 251 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 252 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 253 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 254 | ){ |
| 255 | BtCursor *pCur; |
| 256 | int rc; |
| 257 | |
| 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 | pCur = sqlite3TestTextToPtr(argv[1]); |
drh | 2eb22af | 2016-09-10 19:51:40 +0000 | [diff] [blame] | 264 | #if SQLITE_THREADSAFE>0 |
| 265 | { |
| 266 | Btree *pBt = pCur->pBtree; |
| 267 | sqlite3_mutex_enter(pBt->db->mutex); |
| 268 | sqlite3BtreeEnter(pBt); |
| 269 | rc = sqlite3BtreeCloseCursor(pCur); |
| 270 | sqlite3BtreeLeave(pBt); |
| 271 | sqlite3_mutex_leave(pBt->db->mutex); |
| 272 | } |
| 273 | #else |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 274 | rc = sqlite3BtreeCloseCursor(pCur); |
drh | 2eb22af | 2016-09-10 19:51:40 +0000 | [diff] [blame] | 275 | #endif |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 276 | ckfree((char *)pCur); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 277 | if( rc ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 278 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 279 | return TCL_ERROR; |
| 280 | } |
| 281 | return SQLITE_OK; |
| 282 | } |
| 283 | |
| 284 | /* |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 285 | ** Usage: btree_next ID |
| 286 | ** |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 287 | ** Move the cursor to the next entry in the table. Return 0 on success |
| 288 | ** or 1 if the cursor was already on the last entry in the table or if |
| 289 | ** the table is empty. |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 290 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 291 | static int SQLITE_TCLAPI btree_next( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 292 | void *NotUsed, |
| 293 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 294 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 295 | const char **argv /* Text of each argument */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 296 | ){ |
| 297 | BtCursor *pCur; |
| 298 | int rc; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 299 | int res = 0; |
| 300 | char zBuf[100]; |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 301 | |
| 302 | if( argc!=2 ){ |
| 303 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 304 | " ID\"", 0); |
| 305 | return TCL_ERROR; |
| 306 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 307 | pCur = sqlite3TestTextToPtr(argv[1]); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 308 | sqlite3BtreeEnter(pCur->pBtree); |
drh | 4fc0fb4 | 2004-05-07 02:26:28 +0000 | [diff] [blame] | 309 | rc = sqlite3BtreeNext(pCur, &res); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 310 | sqlite3BtreeLeave(pCur->pBtree); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 311 | if( rc ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 312 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 313 | return TCL_ERROR; |
| 314 | } |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 315 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",res); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 316 | Tcl_AppendResult(interp, zBuf, 0); |
| 317 | return SQLITE_OK; |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | ** Usage: btree_first ID |
| 322 | ** |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 323 | ** Move the cursor to the first entry in the table. Return 0 if the |
| 324 | ** cursor was left point to something and 1 if the table is empty. |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 325 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 326 | static int SQLITE_TCLAPI btree_first( |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 327 | void *NotUsed, |
| 328 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 329 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 330 | const char **argv /* Text of each argument */ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 331 | ){ |
| 332 | BtCursor *pCur; |
| 333 | int rc; |
| 334 | int res = 0; |
| 335 | char zBuf[100]; |
| 336 | |
| 337 | if( argc!=2 ){ |
| 338 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 339 | " ID\"", 0); |
| 340 | return TCL_ERROR; |
| 341 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 342 | pCur = sqlite3TestTextToPtr(argv[1]); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 343 | sqlite3BtreeEnter(pCur->pBtree); |
drh | 4fc0fb4 | 2004-05-07 02:26:28 +0000 | [diff] [blame] | 344 | rc = sqlite3BtreeFirst(pCur, &res); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 345 | sqlite3BtreeLeave(pCur->pBtree); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 346 | if( rc ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 +0000 | [diff] [blame] | 347 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 348 | return TCL_ERROR; |
| 349 | } |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 350 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",res); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 351 | Tcl_AppendResult(interp, zBuf, 0); |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 352 | return SQLITE_OK; |
| 353 | } |
| 354 | |
| 355 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 356 | ** Usage: btree_eof ID |
| 357 | ** |
| 358 | ** Return TRUE if the given cursor is not pointing at a valid entry. |
| 359 | ** Return FALSE if the cursor does point to a valid entry. |
| 360 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 361 | static int SQLITE_TCLAPI btree_eof( |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 362 | void *NotUsed, |
| 363 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 364 | int argc, /* Number of arguments */ |
| 365 | const char **argv /* Text of each argument */ |
| 366 | ){ |
| 367 | BtCursor *pCur; |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 368 | int rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 369 | char zBuf[50]; |
| 370 | |
| 371 | if( argc!=2 ){ |
| 372 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 373 | " ID\"", 0); |
| 374 | return TCL_ERROR; |
| 375 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 376 | pCur = sqlite3TestTextToPtr(argv[1]); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 377 | sqlite3BtreeEnter(pCur->pBtree); |
| 378 | rc = sqlite3BtreeEof(pCur); |
| 379 | sqlite3BtreeLeave(pCur->pBtree); |
| 380 | sqlite3_snprintf(sizeof(zBuf),zBuf, "%d", rc); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 381 | Tcl_AppendResult(interp, zBuf, 0); |
| 382 | return SQLITE_OK; |
| 383 | } |
| 384 | |
| 385 | /* |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 386 | ** Usage: btree_payload_size ID |
| 387 | ** |
| 388 | ** Return the number of bytes of payload |
| 389 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 390 | static int SQLITE_TCLAPI btree_payload_size( |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 391 | void *NotUsed, |
| 392 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 393 | int argc, /* Number of arguments */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 394 | const char **argv /* Text of each argument */ |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 395 | ){ |
| 396 | BtCursor *pCur; |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 397 | u32 n; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 398 | char zBuf[50]; |
| 399 | |
| 400 | if( argc!=2 ){ |
| 401 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 402 | " ID\"", 0); |
| 403 | return TCL_ERROR; |
| 404 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 405 | pCur = sqlite3TestTextToPtr(argv[1]); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 406 | sqlite3BtreeEnter(pCur->pBtree); |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 407 | n = sqlite3BtreePayloadSize(pCur); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 408 | sqlite3BtreeLeave(pCur->pBtree); |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 409 | sqlite3_snprintf(sizeof(zBuf),zBuf, "%u", n); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 410 | Tcl_AppendResult(interp, zBuf, 0); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 411 | return SQLITE_OK; |
| 412 | } |
| 413 | |
| 414 | /* |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 415 | ** usage: varint_test START MULTIPLIER COUNT INCREMENT |
| 416 | ** |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 417 | ** This command tests the putVarint() and getVarint() |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 418 | ** routines, both for accuracy and for speed. |
| 419 | ** |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 420 | ** An integer is written using putVarint() and read back with |
| 421 | ** getVarint() and varified to be unchanged. This repeats COUNT |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 422 | ** times. The first integer is START*MULTIPLIER. Each iteration |
| 423 | ** increases the integer by INCREMENT. |
| 424 | ** |
| 425 | ** This command returns nothing if it works. It returns an error message |
| 426 | ** if something goes wrong. |
| 427 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 428 | static int SQLITE_TCLAPI btree_varint_test( |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 429 | void *NotUsed, |
| 430 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 431 | int argc, /* Number of arguments */ |
| 432 | const char **argv /* Text of each argument */ |
| 433 | ){ |
| 434 | u32 start, mult, count, incr; |
| 435 | u64 in, out; |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 436 | int n1, n2, i, j; |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 437 | unsigned char zBuf[100]; |
| 438 | if( argc!=5 ){ |
| 439 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 440 | " START MULTIPLIER COUNT INCREMENT\"", 0); |
| 441 | return TCL_ERROR; |
| 442 | } |
| 443 | if( Tcl_GetInt(interp, argv[1], (int*)&start) ) return TCL_ERROR; |
| 444 | if( Tcl_GetInt(interp, argv[2], (int*)&mult) ) return TCL_ERROR; |
| 445 | if( Tcl_GetInt(interp, argv[3], (int*)&count) ) return TCL_ERROR; |
| 446 | if( Tcl_GetInt(interp, argv[4], (int*)&incr) ) return TCL_ERROR; |
| 447 | in = start; |
| 448 | in *= mult; |
drh | 7da5fcb | 2012-03-30 14:59:43 +0000 | [diff] [blame] | 449 | for(i=0; i<(int)count; i++){ |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 450 | char zErr[200]; |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 451 | n1 = putVarint(zBuf, in); |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 452 | if( n1>9 || n1<1 ){ |
drh | 65545b5 | 2015-01-19 00:35:53 +0000 | [diff] [blame] | 453 | sqlite3_snprintf(sizeof(zErr), zErr, |
| 454 | "putVarint returned %d - should be between 1 and 9", n1); |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 455 | Tcl_AppendResult(interp, zErr, 0); |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 456 | return TCL_ERROR; |
| 457 | } |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 458 | n2 = getVarint(zBuf, &out); |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 459 | if( n1!=n2 ){ |
drh | 65545b5 | 2015-01-19 00:35:53 +0000 | [diff] [blame] | 460 | sqlite3_snprintf(sizeof(zErr), zErr, |
| 461 | "putVarint returned %d and getVarint returned %d", n1, n2); |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 462 | Tcl_AppendResult(interp, zErr, 0); |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 463 | return TCL_ERROR; |
| 464 | } |
| 465 | if( in!=out ){ |
drh | 65545b5 | 2015-01-19 00:35:53 +0000 | [diff] [blame] | 466 | sqlite3_snprintf(sizeof(zErr), zErr, |
| 467 | "Wrote 0x%016llx and got back 0x%016llx", in, out); |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 468 | Tcl_AppendResult(interp, zErr, 0); |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 469 | return TCL_ERROR; |
| 470 | } |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 471 | if( (in & 0xffffffff)==in ){ |
| 472 | u32 out32; |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 473 | n2 = getVarint32(zBuf, out32); |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 474 | out = out32; |
| 475 | if( n1!=n2 ){ |
drh | 65545b5 | 2015-01-19 00:35:53 +0000 | [diff] [blame] | 476 | sqlite3_snprintf(sizeof(zErr), zErr, |
| 477 | "putVarint returned %d and GetVarint32 returned %d", |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 478 | n1, n2); |
| 479 | Tcl_AppendResult(interp, zErr, 0); |
| 480 | return TCL_ERROR; |
| 481 | } |
| 482 | if( in!=out ){ |
drh | 65545b5 | 2015-01-19 00:35:53 +0000 | [diff] [blame] | 483 | sqlite3_snprintf(sizeof(zErr), zErr, |
| 484 | "Wrote 0x%016llx and got back 0x%016llx from GetVarint32", |
drh | 9d213ef | 2004-06-30 04:02:11 +0000 | [diff] [blame] | 485 | in, out); |
| 486 | Tcl_AppendResult(interp, zErr, 0); |
| 487 | return TCL_ERROR; |
| 488 | } |
| 489 | } |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 490 | |
| 491 | /* In order to get realistic timings, run getVarint 19 more times. |
| 492 | ** This is because getVarint is called about 20 times more often |
| 493 | ** than putVarint. |
| 494 | */ |
| 495 | for(j=0; j<19; j++){ |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 496 | getVarint(zBuf, &out); |
drh | d8820e8 | 2004-05-18 15:57:42 +0000 | [diff] [blame] | 497 | } |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 498 | in += incr; |
| 499 | } |
| 500 | return TCL_OK; |
| 501 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 502 | |
| 503 | /* |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 504 | ** usage: btree_from_db DB-HANDLE |
| 505 | ** |
| 506 | ** This command returns the btree handle for the main database associated |
| 507 | ** with the database-handle passed as the argument. Example usage: |
| 508 | ** |
| 509 | ** sqlite3 db test.db |
| 510 | ** set bt [btree_from_db db] |
| 511 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 512 | static int SQLITE_TCLAPI btree_from_db( |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 513 | void *NotUsed, |
| 514 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 515 | int argc, /* Number of arguments */ |
| 516 | const char **argv /* Text of each argument */ |
| 517 | ){ |
| 518 | char zBuf[100]; |
| 519 | Tcl_CmdInfo info; |
| 520 | sqlite3 *db; |
| 521 | Btree *pBt; |
danielk1977 | 63c64f3 | 2007-05-17 14:45:12 +0000 | [diff] [blame] | 522 | int iDb = 0; |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 523 | |
danielk1977 | 63c64f3 | 2007-05-17 14:45:12 +0000 | [diff] [blame] | 524 | if( argc!=2 && argc!=3 ){ |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 525 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
danielk1977 | 63c64f3 | 2007-05-17 14:45:12 +0000 | [diff] [blame] | 526 | " DB-HANDLE ?N?\"", 0); |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 527 | return TCL_ERROR; |
| 528 | } |
| 529 | |
| 530 | if( 1!=Tcl_GetCommandInfo(interp, argv[1], &info) ){ |
| 531 | Tcl_AppendResult(interp, "No such db-handle: \"", argv[1], "\"", 0); |
| 532 | return TCL_ERROR; |
| 533 | } |
danielk1977 | 63c64f3 | 2007-05-17 14:45:12 +0000 | [diff] [blame] | 534 | if( argc==3 ){ |
| 535 | iDb = atoi(argv[2]); |
| 536 | } |
| 537 | |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 538 | db = *((sqlite3 **)info.objClientData); |
| 539 | assert( db ); |
| 540 | |
danielk1977 | 63c64f3 | 2007-05-17 14:45:12 +0000 | [diff] [blame] | 541 | pBt = db->aDb[iDb].pBt; |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 542 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%p", pBt); |
| 543 | Tcl_SetResult(interp, zBuf, TCL_VOLATILE); |
| 544 | return TCL_OK; |
| 545 | } |
| 546 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 547 | /* |
| 548 | ** Usage: btree_ismemdb ID |
| 549 | ** |
dan | 9131ab9 | 2016-04-06 18:20:51 +0000 | [diff] [blame] | 550 | ** Return true if the B-Tree is currently stored entirely in memory. |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 551 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 552 | static int SQLITE_TCLAPI btree_ismemdb( |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 553 | void *NotUsed, |
| 554 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 555 | int argc, /* Number of arguments */ |
| 556 | const char **argv /* Text of each argument */ |
| 557 | ){ |
| 558 | Btree *pBt; |
| 559 | int res; |
dan | 9131ab9 | 2016-04-06 18:20:51 +0000 | [diff] [blame] | 560 | sqlite3_file *pFile; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 561 | |
| 562 | if( argc!=2 ){ |
| 563 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 564 | " ID\"", 0); |
| 565 | return TCL_ERROR; |
| 566 | } |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 567 | pBt = sqlite3TestTextToPtr(argv[1]); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 568 | sqlite3_mutex_enter(pBt->db->mutex); |
| 569 | sqlite3BtreeEnter(pBt); |
dan | 9131ab9 | 2016-04-06 18:20:51 +0000 | [diff] [blame] | 570 | pFile = sqlite3PagerFile(sqlite3BtreePager(pBt)); |
| 571 | res = (pFile->pMethods==0); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 572 | sqlite3BtreeLeave(pBt); |
| 573 | sqlite3_mutex_leave(pBt->db->mutex); |
| 574 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(res)); |
| 575 | return SQLITE_OK; |
| 576 | } |
| 577 | |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 578 | /* |
| 579 | ** usage: btree_set_cache_size ID NCACHE |
| 580 | ** |
| 581 | ** Set the size of the cache used by btree $ID. |
| 582 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 583 | static int SQLITE_TCLAPI btree_set_cache_size( |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 584 | void *NotUsed, |
| 585 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 586 | int argc, /* Number of arguments */ |
| 587 | const char **argv /* Text of each argument */ |
| 588 | ){ |
| 589 | int nCache; |
| 590 | Btree *pBt; |
| 591 | |
| 592 | if( argc!=3 ){ |
| 593 | Tcl_AppendResult( |
| 594 | interp, "wrong # args: should be \"", argv[0], " BT NCACHE\"", 0); |
| 595 | return TCL_ERROR; |
| 596 | } |
| 597 | pBt = sqlite3TestTextToPtr(argv[1]); |
| 598 | if( Tcl_GetInt(interp, argv[2], &nCache) ) return TCL_ERROR; |
| 599 | |
| 600 | sqlite3_mutex_enter(pBt->db->mutex); |
| 601 | sqlite3BtreeEnter(pBt); |
| 602 | sqlite3BtreeSetCacheSize(pBt, nCache); |
| 603 | sqlite3BtreeLeave(pBt); |
| 604 | sqlite3_mutex_leave(pBt->db->mutex); |
| 605 | return TCL_OK; |
| 606 | } |
| 607 | |
dan | 6b51364 | 2015-10-26 16:31:18 +0000 | [diff] [blame] | 608 | /* |
| 609 | ** usage: btree_insert CSR ?KEY? VALUE |
| 610 | ** |
| 611 | ** Set the size of the cache used by btree $ID. |
| 612 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 613 | static int SQLITE_TCLAPI btree_insert( |
dan | 6b51364 | 2015-10-26 16:31:18 +0000 | [diff] [blame] | 614 | ClientData clientData, |
| 615 | Tcl_Interp *interp, |
| 616 | int objc, |
| 617 | Tcl_Obj *const objv[] |
| 618 | ){ |
| 619 | BtCursor *pCur; |
| 620 | int rc; |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 621 | BtreePayload x; |
dan | 6b51364 | 2015-10-26 16:31:18 +0000 | [diff] [blame] | 622 | |
| 623 | if( objc!=4 && objc!=3 ){ |
| 624 | Tcl_WrongNumArgs(interp, 1, objv, "?-intkey? CSR KEY VALUE"); |
| 625 | return TCL_ERROR; |
| 626 | } |
| 627 | |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 628 | memset(&x, 0, sizeof(x)); |
dan | 6b51364 | 2015-10-26 16:31:18 +0000 | [diff] [blame] | 629 | if( objc==4 ){ |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 630 | if( Tcl_GetIntFromObj(interp, objv[2], &rc) ) return TCL_ERROR; |
| 631 | x.nKey = rc; |
| 632 | x.pData = (void*)Tcl_GetByteArrayFromObj(objv[3], &x.nData); |
dan | 6b51364 | 2015-10-26 16:31:18 +0000 | [diff] [blame] | 633 | }else{ |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 634 | x.pKey = (void*)Tcl_GetByteArrayFromObj(objv[2], &rc); |
| 635 | x.nKey = rc; |
dan | 6b51364 | 2015-10-26 16:31:18 +0000 | [diff] [blame] | 636 | } |
| 637 | pCur = (BtCursor*)sqlite3TestTextToPtr(Tcl_GetString(objv[1])); |
| 638 | |
dan | 6a75c8a | 2015-10-30 20:54:25 +0000 | [diff] [blame] | 639 | sqlite3_mutex_enter(pCur->pBtree->db->mutex); |
dan | 6b51364 | 2015-10-26 16:31:18 +0000 | [diff] [blame] | 640 | sqlite3BtreeEnter(pCur->pBtree); |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 641 | rc = sqlite3BtreeInsert(pCur, &x, 0, 0); |
dan | 6b51364 | 2015-10-26 16:31:18 +0000 | [diff] [blame] | 642 | sqlite3BtreeLeave(pCur->pBtree); |
dan | 6a75c8a | 2015-10-30 20:54:25 +0000 | [diff] [blame] | 643 | sqlite3_mutex_leave(pCur->pBtree->db->mutex); |
dan | 6b51364 | 2015-10-26 16:31:18 +0000 | [diff] [blame] | 644 | |
| 645 | Tcl_ResetResult(interp); |
| 646 | if( rc ){ |
| 647 | Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); |
| 648 | return TCL_ERROR; |
| 649 | } |
| 650 | return TCL_OK; |
| 651 | } |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 652 | |
danielk1977 | 1ad7f64 | 2005-01-20 05:24:32 +0000 | [diff] [blame] | 653 | |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 654 | /* |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 655 | ** Register commands with the TCL interpreter. |
| 656 | */ |
| 657 | int Sqlitetest3_Init(Tcl_Interp *interp){ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 658 | static struct { |
| 659 | char *zName; |
| 660 | Tcl_CmdProc *xProc; |
| 661 | } aCmd[] = { |
| 662 | { "btree_open", (Tcl_CmdProc*)btree_open }, |
| 663 | { "btree_close", (Tcl_CmdProc*)btree_close }, |
| 664 | { "btree_begin_transaction", (Tcl_CmdProc*)btree_begin_transaction }, |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 665 | { "btree_pager_stats", (Tcl_CmdProc*)btree_pager_stats }, |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 666 | { "btree_cursor", (Tcl_CmdProc*)btree_cursor }, |
| 667 | { "btree_close_cursor", (Tcl_CmdProc*)btree_close_cursor }, |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 668 | { "btree_next", (Tcl_CmdProc*)btree_next }, |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 669 | { "btree_eof", (Tcl_CmdProc*)btree_eof }, |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 670 | { "btree_payload_size", (Tcl_CmdProc*)btree_payload_size }, |
| 671 | { "btree_first", (Tcl_CmdProc*)btree_first }, |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 672 | { "btree_varint_test", (Tcl_CmdProc*)btree_varint_test }, |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 673 | { "btree_from_db", (Tcl_CmdProc*)btree_from_db }, |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 674 | { "btree_ismemdb", (Tcl_CmdProc*)btree_ismemdb }, |
| 675 | { "btree_set_cache_size", (Tcl_CmdProc*)btree_set_cache_size } |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 676 | }; |
| 677 | int i; |
| 678 | |
| 679 | for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ |
| 680 | Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); |
| 681 | } |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 682 | |
dan | 6b51364 | 2015-10-26 16:31:18 +0000 | [diff] [blame] | 683 | Tcl_CreateObjCommand(interp, "btree_insert", btree_insert, 0, 0); |
| 684 | |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 685 | return TCL_OK; |
| 686 | } |