blob: 0de19469a44818ca958589dae3b0722f97d41f34 [file] [log] [blame]
drh5c4d9702001-08-20 00:33:58 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh5c4d9702001-08-20 00:33:58 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh5c4d9702001-08-20 00:33:58 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drh5c4d9702001-08-20 00:33:58 +000010**
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.
drh5c4d9702001-08-20 00:33:58 +000015*/
16#include "sqliteInt.h"
danielk1977a1644fd2007-08-29 12:31:25 +000017#include "btreeInt.h"
mistachkin52b1dbb2016-07-28 14:37:04 +000018#if defined(INCLUDE_SQLITE_TCL_H)
19# include "sqlite_tcl.h"
20#else
21# include "tcl.h"
22#endif
drh5c4d9702001-08-20 00:33:58 +000023#include <stdlib.h>
24#include <string.h>
25
mistachkine84d8d32013-04-29 03:09:10 +000026extern const char *sqlite3ErrName(int);
drh5c4d9702001-08-20 00:33:58 +000027
28/*
drha2451e22007-08-20 23:50:24 +000029** A bogus sqlite3 connection structure for use in the btree
30** tests.
31*/
32static sqlite3 sDb;
33static int nRefSqlite3 = 0;
34
35/*
drh75c014c2010-08-30 15:02:28 +000036** Usage: btree_open FILENAME NCACHE
drh5c4d9702001-08-20 00:33:58 +000037**
38** Open a new database
39*/
mistachkin7617e4a2016-07-28 17:11:20 +000040static int SQLITE_TCLAPI btree_open(
drh5c4d9702001-08-20 00:33:58 +000041 void *NotUsed,
42 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
43 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +000044 const char **argv /* Text of each argument */
drh5c4d9702001-08-20 00:33:58 +000045){
46 Btree *pBt;
drh75c014c2010-08-30 15:02:28 +000047 int rc, nCache;
drh5c4d9702001-08-20 00:33:58 +000048 char zBuf[100];
drhc02a43a2012-01-10 23:18:38 +000049 int n;
50 char *zFilename;
drh75c014c2010-08-30 15:02:28 +000051 if( argc!=3 ){
drh5c4d9702001-08-20 00:33:58 +000052 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
drh23e11ca2004-05-04 17:27:28 +000053 " FILENAME NCACHE FLAGS\"", 0);
drh5c4d9702001-08-20 00:33:58 +000054 return TCL_ERROR;
55 }
drh23e11ca2004-05-04 17:27:28 +000056 if( Tcl_GetInt(interp, argv[2], &nCache) ) return TCL_ERROR;
drha2451e22007-08-20 23:50:24 +000057 nRefSqlite3++;
58 if( nRefSqlite3==1 ){
59 sDb.pVfs = sqlite3_vfs_find(0);
danielk197759f8c082008-06-18 17:09:10 +000060 sDb.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
drh27641702007-08-22 02:56:42 +000061 sqlite3_mutex_enter(sDb.mutex);
drha2451e22007-08-20 23:50:24 +000062 }
drh83cc1392012-04-19 18:04:28 +000063 n = (int)strlen(argv[1]);
drhc02a43a2012-01-10 23:18:38 +000064 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,
drh33f4e022007-09-03 15:19:34 +000069 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB);
drhc02a43a2012-01-10 23:18:38 +000070 sqlite3_free(zFilename);
drh5c4d9702001-08-20 00:33:58 +000071 if( rc!=SQLITE_OK ){
mistachkine84d8d32013-04-29 03:09:10 +000072 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
drh5c4d9702001-08-20 00:33:58 +000073 return TCL_ERROR;
74 }
drh90f5ecb2004-07-22 01:19:35 +000075 sqlite3BtreeSetCacheSize(pBt, nCache);
drhfe63d1c2004-09-08 20:13:04 +000076 sqlite3_snprintf(sizeof(zBuf), zBuf,"%p", pBt);
drh5c4d9702001-08-20 00:33:58 +000077 Tcl_AppendResult(interp, zBuf, 0);
78 return TCL_OK;
79}
80
81/*
82** Usage: btree_close ID
83**
84** Close the given database.
85*/
mistachkin7617e4a2016-07-28 17:11:20 +000086static int SQLITE_TCLAPI btree_close(
drh5c4d9702001-08-20 00:33:58 +000087 void *NotUsed,
88 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
89 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +000090 const char **argv /* Text of each argument */
drh5c4d9702001-08-20 00:33:58 +000091){
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 }
drhe8f52c52008-07-12 14:52:20 +000099 pBt = sqlite3TestTextToPtr(argv[1]);
drh23e11ca2004-05-04 17:27:28 +0000100 rc = sqlite3BtreeClose(pBt);
drh5c4d9702001-08-20 00:33:58 +0000101 if( rc!=SQLITE_OK ){
mistachkine84d8d32013-04-29 03:09:10 +0000102 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
drh5c4d9702001-08-20 00:33:58 +0000103 return TCL_ERROR;
104 }
drha2451e22007-08-20 23:50:24 +0000105 nRefSqlite3--;
106 if( nRefSqlite3==0 ){
drh27641702007-08-22 02:56:42 +0000107 sqlite3_mutex_leave(sDb.mutex);
drha2451e22007-08-20 23:50:24 +0000108 sqlite3_mutex_free(sDb.mutex);
109 sDb.mutex = 0;
drha2451e22007-08-20 23:50:24 +0000110 sDb.pVfs = 0;
111 }
drh5c4d9702001-08-20 00:33:58 +0000112 return TCL_OK;
113}
114
drh27641702007-08-22 02:56:42 +0000115
drh5c4d9702001-08-20 00:33:58 +0000116/*
117** Usage: btree_begin_transaction ID
118**
119** Start a new transaction
120*/
mistachkin7617e4a2016-07-28 17:11:20 +0000121static int SQLITE_TCLAPI btree_begin_transaction(
drh5c4d9702001-08-20 00:33:58 +0000122 void *NotUsed,
123 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
124 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +0000125 const char **argv /* Text of each argument */
drh5c4d9702001-08-20 00:33:58 +0000126){
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 }
drhe8f52c52008-07-12 14:52:20 +0000134 pBt = sqlite3TestTextToPtr(argv[1]);
drhff0587c2007-08-29 17:43:19 +0000135 sqlite3BtreeEnter(pBt);
danielk197740b38dc2004-06-26 08:38:24 +0000136 rc = sqlite3BtreeBeginTrans(pBt, 1);
drhff0587c2007-08-29 17:43:19 +0000137 sqlite3BtreeLeave(pBt);
drh5c4d9702001-08-20 00:33:58 +0000138 if( rc!=SQLITE_OK ){
mistachkine84d8d32013-04-29 03:09:10 +0000139 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
drh5c4d9702001-08-20 00:33:58 +0000140 return TCL_ERROR;
141 }
142 return TCL_OK;
143}
144
145/*
drh5c4d9702001-08-20 00:33:58 +0000146** Usage: btree_pager_stats ID
147**
148** Returns pager statistics
149*/
mistachkin7617e4a2016-07-28 17:11:20 +0000150static int SQLITE_TCLAPI btree_pager_stats(
drh5c4d9702001-08-20 00:33:58 +0000151 void *NotUsed,
152 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
153 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +0000154 const char **argv /* Text of each argument */
drh5c4d9702001-08-20 00:33:58 +0000155){
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 }
drhe8f52c52008-07-12 14:52:20 +0000165 pBt = sqlite3TestTextToPtr(argv[1]);
danielk1977f3c62652007-08-30 08:27:39 +0000166
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 */
drhe5fe6902007-12-07 18:55:28 +0000174 sqlite3_mutex_enter(pBt->db->mutex);
danielk1977f3c62652007-08-30 08:27:39 +0000175
drh27641702007-08-22 02:56:42 +0000176 sqlite3BtreeEnter(pBt);
danielk19773b8a05f2007-03-19 17:44:26 +0000177 a = sqlite3PagerStats(sqlite3BtreePager(pBt));
danielk197742741be2005-01-08 12:42:39 +0000178 for(i=0; i<11; i++){
drh5c4d9702001-08-20 00:33:58 +0000179 static char *zName[] = {
180 "ref", "page", "max", "size", "state", "err",
danielk197742741be2005-01-08 12:42:39 +0000181 "hit", "miss", "ovfl", "read", "write"
drh5c4d9702001-08-20 00:33:58 +0000182 };
183 char zBuf[100];
184 Tcl_AppendElement(interp, zName[i]);
drhfe63d1c2004-09-08 20:13:04 +0000185 sqlite3_snprintf(sizeof(zBuf), zBuf,"%d",a[i]);
drh5c4d9702001-08-20 00:33:58 +0000186 Tcl_AppendElement(interp, zBuf);
187 }
drh27641702007-08-22 02:56:42 +0000188 sqlite3BtreeLeave(pBt);
danielk1977f3c62652007-08-30 08:27:39 +0000189
190 /* Release the mutex on the SQLite handle that controls this b-tree */
drhe5fe6902007-12-07 18:55:28 +0000191 sqlite3_mutex_leave(pBt->db->mutex);
drh5c4d9702001-08-20 00:33:58 +0000192 return TCL_OK;
193}
194
195/*
drhecdc7532001-09-23 02:35:53 +0000196** Usage: btree_cursor ID TABLENUM WRITEABLE
drh5c4d9702001-08-20 00:33:58 +0000197**
198** Create a new cursor. Return the ID for the cursor.
199*/
mistachkin7617e4a2016-07-28 17:11:20 +0000200static int SQLITE_TCLAPI btree_cursor(
drh5c4d9702001-08-20 00:33:58 +0000201 void *NotUsed,
202 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
203 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +0000204 const char **argv /* Text of each argument */
drh5c4d9702001-08-20 00:33:58 +0000205){
206 Btree *pBt;
207 int iTable;
208 BtCursor *pCur;
dan93a696f2010-01-07 11:27:30 +0000209 int rc = SQLITE_OK;
drhecdc7532001-09-23 02:35:53 +0000210 int wrFlag;
drh5c4d9702001-08-20 00:33:58 +0000211 char zBuf[30];
212
drhecdc7532001-09-23 02:35:53 +0000213 if( argc!=4 ){
drh5c4d9702001-08-20 00:33:58 +0000214 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
drhecdc7532001-09-23 02:35:53 +0000215 " ID TABLENUM WRITEABLE\"", 0);
drh5c4d9702001-08-20 00:33:58 +0000216 return TCL_ERROR;
217 }
drhe8f52c52008-07-12 14:52:20 +0000218 pBt = sqlite3TestTextToPtr(argv[1]);
drh5c4d9702001-08-20 00:33:58 +0000219 if( Tcl_GetInt(interp, argv[2], &iTable) ) return TCL_ERROR;
drhecdc7532001-09-23 02:35:53 +0000220 if( Tcl_GetBoolean(interp, argv[3], &wrFlag) ) return TCL_ERROR;
danfd261ec2015-10-22 20:54:33 +0000221 if( wrFlag ) wrFlag = BTREE_WRCSR;
danielk1977cd3e8f72008-03-25 09:47:35 +0000222 pCur = (BtCursor *)ckalloc(sqlite3BtreeCursorSize());
223 memset(pCur, 0, sqlite3BtreeCursorSize());
dan6a75c8a2015-10-30 20:54:25 +0000224 sqlite3_mutex_enter(pBt->db->mutex);
drhff0587c2007-08-29 17:43:19 +0000225 sqlite3BtreeEnter(pBt);
dan93a696f2010-01-07 11:27:30 +0000226#ifndef SQLITE_OMIT_SHARED_CACHE
dan6b513642015-10-26 16:31:18 +0000227 rc = sqlite3BtreeLockTable(pBt, iTable, !!wrFlag);
dan93a696f2010-01-07 11:27:30 +0000228#endif
danielk197796d48e92009-06-29 06:00:37 +0000229 if( rc==SQLITE_OK ){
230 rc = sqlite3BtreeCursor(pBt, iTable, wrFlag, 0, pCur);
231 }
drhff0587c2007-08-29 17:43:19 +0000232 sqlite3BtreeLeave(pBt);
dan6a75c8a2015-10-30 20:54:25 +0000233 sqlite3_mutex_leave(pBt->db->mutex);
drh5c4d9702001-08-20 00:33:58 +0000234 if( rc ){
danielk1977cd3e8f72008-03-25 09:47:35 +0000235 ckfree((char *)pCur);
mistachkine84d8d32013-04-29 03:09:10 +0000236 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
drh5c4d9702001-08-20 00:33:58 +0000237 return TCL_ERROR;
238 }
drhfe63d1c2004-09-08 20:13:04 +0000239 sqlite3_snprintf(sizeof(zBuf), zBuf,"%p", pCur);
drh5c4d9702001-08-20 00:33:58 +0000240 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*/
mistachkin7617e4a2016-07-28 17:11:20 +0000249static int SQLITE_TCLAPI btree_close_cursor(
drh5c4d9702001-08-20 00:33:58 +0000250 void *NotUsed,
251 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
252 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +0000253 const char **argv /* Text of each argument */
drh5c4d9702001-08-20 00:33:58 +0000254){
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 }
drhe8f52c52008-07-12 14:52:20 +0000263 pCur = sqlite3TestTextToPtr(argv[1]);
drh2eb22af2016-09-10 19:51:40 +0000264#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
drh23e11ca2004-05-04 17:27:28 +0000274 rc = sqlite3BtreeCloseCursor(pCur);
drh2eb22af2016-09-10 19:51:40 +0000275#endif
danielk1977cd3e8f72008-03-25 09:47:35 +0000276 ckfree((char *)pCur);
drh5c4d9702001-08-20 00:33:58 +0000277 if( rc ){
mistachkine84d8d32013-04-29 03:09:10 +0000278 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
drh5c4d9702001-08-20 00:33:58 +0000279 return TCL_ERROR;
280 }
281 return SQLITE_OK;
282}
283
284/*
drh5c4d9702001-08-20 00:33:58 +0000285** Usage: btree_next ID
286**
drh2dcc9aa2002-12-04 13:40:25 +0000287** 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.
drh5c4d9702001-08-20 00:33:58 +0000290*/
mistachkin7617e4a2016-07-28 17:11:20 +0000291static int SQLITE_TCLAPI btree_next(
drh5c4d9702001-08-20 00:33:58 +0000292 void *NotUsed,
293 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
294 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +0000295 const char **argv /* Text of each argument */
drh5c4d9702001-08-20 00:33:58 +0000296){
297 BtCursor *pCur;
298 int rc;
drh0de8c112002-07-06 16:32:14 +0000299 int res = 0;
300 char zBuf[100];
drh5c4d9702001-08-20 00:33:58 +0000301
302 if( argc!=2 ){
303 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
304 " ID\"", 0);
305 return TCL_ERROR;
306 }
drhe8f52c52008-07-12 14:52:20 +0000307 pCur = sqlite3TestTextToPtr(argv[1]);
drhff0587c2007-08-29 17:43:19 +0000308 sqlite3BtreeEnter(pCur->pBtree);
drh2ab792e2017-05-30 18:34:07 +0000309 rc = sqlite3BtreeNext(pCur, 0);
310 if( rc==SQLITE_DONE ){
311 res = 1;
312 rc = SQLITE_OK;
313 }
drhff0587c2007-08-29 17:43:19 +0000314 sqlite3BtreeLeave(pCur->pBtree);
drh5c4d9702001-08-20 00:33:58 +0000315 if( rc ){
mistachkine84d8d32013-04-29 03:09:10 +0000316 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
drh5c4d9702001-08-20 00:33:58 +0000317 return TCL_ERROR;
318 }
drhfe63d1c2004-09-08 20:13:04 +0000319 sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",res);
drh0de8c112002-07-06 16:32:14 +0000320 Tcl_AppendResult(interp, zBuf, 0);
321 return SQLITE_OK;
322}
323
324/*
325** Usage: btree_first ID
326**
drh2dcc9aa2002-12-04 13:40:25 +0000327** Move the cursor to the first entry in the table. Return 0 if the
328** cursor was left point to something and 1 if the table is empty.
drh0de8c112002-07-06 16:32:14 +0000329*/
mistachkin7617e4a2016-07-28 17:11:20 +0000330static int SQLITE_TCLAPI btree_first(
drh0de8c112002-07-06 16:32:14 +0000331 void *NotUsed,
332 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
333 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +0000334 const char **argv /* Text of each argument */
drh0de8c112002-07-06 16:32:14 +0000335){
336 BtCursor *pCur;
337 int rc;
338 int res = 0;
339 char zBuf[100];
340
341 if( argc!=2 ){
342 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
343 " ID\"", 0);
344 return TCL_ERROR;
345 }
drhe8f52c52008-07-12 14:52:20 +0000346 pCur = sqlite3TestTextToPtr(argv[1]);
drhff0587c2007-08-29 17:43:19 +0000347 sqlite3BtreeEnter(pCur->pBtree);
drh4fc0fb42004-05-07 02:26:28 +0000348 rc = sqlite3BtreeFirst(pCur, &res);
drhff0587c2007-08-29 17:43:19 +0000349 sqlite3BtreeLeave(pCur->pBtree);
drh0de8c112002-07-06 16:32:14 +0000350 if( rc ){
mistachkine84d8d32013-04-29 03:09:10 +0000351 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
drh0de8c112002-07-06 16:32:14 +0000352 return TCL_ERROR;
353 }
drhfe63d1c2004-09-08 20:13:04 +0000354 sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",res);
drh0de8c112002-07-06 16:32:14 +0000355 Tcl_AppendResult(interp, zBuf, 0);
drh5c4d9702001-08-20 00:33:58 +0000356 return SQLITE_OK;
357}
358
359/*
drhc39e0002004-05-07 23:50:57 +0000360** Usage: btree_eof ID
361**
362** Return TRUE if the given cursor is not pointing at a valid entry.
363** Return FALSE if the cursor does point to a valid entry.
364*/
mistachkin7617e4a2016-07-28 17:11:20 +0000365static int SQLITE_TCLAPI btree_eof(
drhc39e0002004-05-07 23:50:57 +0000366 void *NotUsed,
367 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
368 int argc, /* Number of arguments */
369 const char **argv /* Text of each argument */
370){
371 BtCursor *pCur;
drhff0587c2007-08-29 17:43:19 +0000372 int rc;
drhc39e0002004-05-07 23:50:57 +0000373 char zBuf[50];
374
375 if( argc!=2 ){
376 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
377 " ID\"", 0);
378 return TCL_ERROR;
379 }
drhe8f52c52008-07-12 14:52:20 +0000380 pCur = sqlite3TestTextToPtr(argv[1]);
drhff0587c2007-08-29 17:43:19 +0000381 sqlite3BtreeEnter(pCur->pBtree);
382 rc = sqlite3BtreeEof(pCur);
383 sqlite3BtreeLeave(pCur->pBtree);
384 sqlite3_snprintf(sizeof(zBuf),zBuf, "%d", rc);
drhc39e0002004-05-07 23:50:57 +0000385 Tcl_AppendResult(interp, zBuf, 0);
386 return SQLITE_OK;
387}
388
389/*
drh0de8c112002-07-06 16:32:14 +0000390** Usage: btree_payload_size ID
391**
392** Return the number of bytes of payload
393*/
mistachkin7617e4a2016-07-28 17:11:20 +0000394static int SQLITE_TCLAPI btree_payload_size(
drh0de8c112002-07-06 16:32:14 +0000395 void *NotUsed,
396 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
397 int argc, /* Number of arguments */
drhc2eef3b2002-08-31 18:53:06 +0000398 const char **argv /* Text of each argument */
drh0de8c112002-07-06 16:32:14 +0000399){
400 BtCursor *pCur;
drha7c90c42016-06-04 20:37:10 +0000401 u32 n;
drh0de8c112002-07-06 16:32:14 +0000402 char zBuf[50];
403
404 if( argc!=2 ){
405 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
406 " ID\"", 0);
407 return TCL_ERROR;
408 }
drhe8f52c52008-07-12 14:52:20 +0000409 pCur = sqlite3TestTextToPtr(argv[1]);
drhff0587c2007-08-29 17:43:19 +0000410 sqlite3BtreeEnter(pCur->pBtree);
drha7c90c42016-06-04 20:37:10 +0000411 n = sqlite3BtreePayloadSize(pCur);
drhff0587c2007-08-29 17:43:19 +0000412 sqlite3BtreeLeave(pCur->pBtree);
drha7c90c42016-06-04 20:37:10 +0000413 sqlite3_snprintf(sizeof(zBuf),zBuf, "%u", n);
drh0de8c112002-07-06 16:32:14 +0000414 Tcl_AppendResult(interp, zBuf, 0);
drh0de8c112002-07-06 16:32:14 +0000415 return SQLITE_OK;
416}
417
418/*
drh6d2fb152004-05-14 16:50:06 +0000419** usage: varint_test START MULTIPLIER COUNT INCREMENT
420**
shane3f8d5cf2008-04-24 19:15:09 +0000421** This command tests the putVarint() and getVarint()
drh6d2fb152004-05-14 16:50:06 +0000422** routines, both for accuracy and for speed.
423**
shane3f8d5cf2008-04-24 19:15:09 +0000424** An integer is written using putVarint() and read back with
425** getVarint() and varified to be unchanged. This repeats COUNT
drh6d2fb152004-05-14 16:50:06 +0000426** times. The first integer is START*MULTIPLIER. Each iteration
427** increases the integer by INCREMENT.
428**
429** This command returns nothing if it works. It returns an error message
430** if something goes wrong.
431*/
mistachkin7617e4a2016-07-28 17:11:20 +0000432static int SQLITE_TCLAPI btree_varint_test(
drh6d2fb152004-05-14 16:50:06 +0000433 void *NotUsed,
434 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
435 int argc, /* Number of arguments */
436 const char **argv /* Text of each argument */
437){
438 u32 start, mult, count, incr;
439 u64 in, out;
drhd8820e82004-05-18 15:57:42 +0000440 int n1, n2, i, j;
drh6d2fb152004-05-14 16:50:06 +0000441 unsigned char zBuf[100];
442 if( argc!=5 ){
443 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
444 " START MULTIPLIER COUNT INCREMENT\"", 0);
445 return TCL_ERROR;
446 }
447 if( Tcl_GetInt(interp, argv[1], (int*)&start) ) return TCL_ERROR;
448 if( Tcl_GetInt(interp, argv[2], (int*)&mult) ) return TCL_ERROR;
449 if( Tcl_GetInt(interp, argv[3], (int*)&count) ) return TCL_ERROR;
450 if( Tcl_GetInt(interp, argv[4], (int*)&incr) ) return TCL_ERROR;
451 in = start;
452 in *= mult;
drh7da5fcb2012-03-30 14:59:43 +0000453 for(i=0; i<(int)count; i++){
drhd8820e82004-05-18 15:57:42 +0000454 char zErr[200];
shane3f8d5cf2008-04-24 19:15:09 +0000455 n1 = putVarint(zBuf, in);
drh6d2fb152004-05-14 16:50:06 +0000456 if( n1>9 || n1<1 ){
drh65545b52015-01-19 00:35:53 +0000457 sqlite3_snprintf(sizeof(zErr), zErr,
458 "putVarint returned %d - should be between 1 and 9", n1);
drhd8820e82004-05-18 15:57:42 +0000459 Tcl_AppendResult(interp, zErr, 0);
drh6d2fb152004-05-14 16:50:06 +0000460 return TCL_ERROR;
461 }
shane3f8d5cf2008-04-24 19:15:09 +0000462 n2 = getVarint(zBuf, &out);
drh6d2fb152004-05-14 16:50:06 +0000463 if( n1!=n2 ){
drh65545b52015-01-19 00:35:53 +0000464 sqlite3_snprintf(sizeof(zErr), zErr,
465 "putVarint returned %d and getVarint returned %d", n1, n2);
drhd8820e82004-05-18 15:57:42 +0000466 Tcl_AppendResult(interp, zErr, 0);
drh6d2fb152004-05-14 16:50:06 +0000467 return TCL_ERROR;
468 }
469 if( in!=out ){
drh65545b52015-01-19 00:35:53 +0000470 sqlite3_snprintf(sizeof(zErr), zErr,
471 "Wrote 0x%016llx and got back 0x%016llx", in, out);
drhd8820e82004-05-18 15:57:42 +0000472 Tcl_AppendResult(interp, zErr, 0);
drh6d2fb152004-05-14 16:50:06 +0000473 return TCL_ERROR;
474 }
drh9d213ef2004-06-30 04:02:11 +0000475 if( (in & 0xffffffff)==in ){
476 u32 out32;
shane3f8d5cf2008-04-24 19:15:09 +0000477 n2 = getVarint32(zBuf, out32);
drh9d213ef2004-06-30 04:02:11 +0000478 out = out32;
479 if( n1!=n2 ){
drh65545b52015-01-19 00:35:53 +0000480 sqlite3_snprintf(sizeof(zErr), zErr,
481 "putVarint returned %d and GetVarint32 returned %d",
drh9d213ef2004-06-30 04:02:11 +0000482 n1, n2);
483 Tcl_AppendResult(interp, zErr, 0);
484 return TCL_ERROR;
485 }
486 if( in!=out ){
drh65545b52015-01-19 00:35:53 +0000487 sqlite3_snprintf(sizeof(zErr), zErr,
488 "Wrote 0x%016llx and got back 0x%016llx from GetVarint32",
drh9d213ef2004-06-30 04:02:11 +0000489 in, out);
490 Tcl_AppendResult(interp, zErr, 0);
491 return TCL_ERROR;
492 }
493 }
drhd8820e82004-05-18 15:57:42 +0000494
495 /* In order to get realistic timings, run getVarint 19 more times.
496 ** This is because getVarint is called about 20 times more often
497 ** than putVarint.
498 */
499 for(j=0; j<19; j++){
shane3f8d5cf2008-04-24 19:15:09 +0000500 getVarint(zBuf, &out);
drhd8820e82004-05-18 15:57:42 +0000501 }
drh6d2fb152004-05-14 16:50:06 +0000502 in += incr;
503 }
504 return TCL_OK;
505}
drh9b171272004-05-08 02:03:22 +0000506
507/*
danielk197742741be2005-01-08 12:42:39 +0000508** usage: btree_from_db DB-HANDLE
509**
510** This command returns the btree handle for the main database associated
511** with the database-handle passed as the argument. Example usage:
512**
513** sqlite3 db test.db
514** set bt [btree_from_db db]
515*/
mistachkin7617e4a2016-07-28 17:11:20 +0000516static int SQLITE_TCLAPI btree_from_db(
danielk197742741be2005-01-08 12:42:39 +0000517 void *NotUsed,
518 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
519 int argc, /* Number of arguments */
520 const char **argv /* Text of each argument */
521){
522 char zBuf[100];
523 Tcl_CmdInfo info;
524 sqlite3 *db;
525 Btree *pBt;
danielk197763c64f32007-05-17 14:45:12 +0000526 int iDb = 0;
danielk197742741be2005-01-08 12:42:39 +0000527
danielk197763c64f32007-05-17 14:45:12 +0000528 if( argc!=2 && argc!=3 ){
danielk197742741be2005-01-08 12:42:39 +0000529 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
danielk197763c64f32007-05-17 14:45:12 +0000530 " DB-HANDLE ?N?\"", 0);
danielk197742741be2005-01-08 12:42:39 +0000531 return TCL_ERROR;
532 }
533
534 if( 1!=Tcl_GetCommandInfo(interp, argv[1], &info) ){
535 Tcl_AppendResult(interp, "No such db-handle: \"", argv[1], "\"", 0);
536 return TCL_ERROR;
537 }
danielk197763c64f32007-05-17 14:45:12 +0000538 if( argc==3 ){
539 iDb = atoi(argv[2]);
540 }
541
danielk197742741be2005-01-08 12:42:39 +0000542 db = *((sqlite3 **)info.objClientData);
543 assert( db );
544
danielk197763c64f32007-05-17 14:45:12 +0000545 pBt = db->aDb[iDb].pBt;
danielk197742741be2005-01-08 12:42:39 +0000546 sqlite3_snprintf(sizeof(zBuf), zBuf, "%p", pBt);
547 Tcl_SetResult(interp, zBuf, TCL_VOLATILE);
548 return TCL_OK;
549}
550
danielk197717b90b52008-06-06 11:11:25 +0000551/*
552** Usage: btree_ismemdb ID
553**
dan9131ab92016-04-06 18:20:51 +0000554** Return true if the B-Tree is currently stored entirely in memory.
danielk197717b90b52008-06-06 11:11:25 +0000555*/
mistachkin7617e4a2016-07-28 17:11:20 +0000556static int SQLITE_TCLAPI btree_ismemdb(
danielk197717b90b52008-06-06 11:11:25 +0000557 void *NotUsed,
558 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
559 int argc, /* Number of arguments */
560 const char **argv /* Text of each argument */
561){
562 Btree *pBt;
563 int res;
dan9131ab92016-04-06 18:20:51 +0000564 sqlite3_file *pFile;
danielk197717b90b52008-06-06 11:11:25 +0000565
566 if( argc!=2 ){
567 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
568 " ID\"", 0);
569 return TCL_ERROR;
570 }
drhe8f52c52008-07-12 14:52:20 +0000571 pBt = sqlite3TestTextToPtr(argv[1]);
danielk197717b90b52008-06-06 11:11:25 +0000572 sqlite3_mutex_enter(pBt->db->mutex);
573 sqlite3BtreeEnter(pBt);
dan9131ab92016-04-06 18:20:51 +0000574 pFile = sqlite3PagerFile(sqlite3BtreePager(pBt));
575 res = (pFile->pMethods==0);
danielk197717b90b52008-06-06 11:11:25 +0000576 sqlite3BtreeLeave(pBt);
577 sqlite3_mutex_leave(pBt->db->mutex);
578 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(res));
579 return SQLITE_OK;
580}
581
danielk197730548662009-07-09 05:07:37 +0000582/*
583** usage: btree_set_cache_size ID NCACHE
584**
585** Set the size of the cache used by btree $ID.
586*/
mistachkin7617e4a2016-07-28 17:11:20 +0000587static int SQLITE_TCLAPI btree_set_cache_size(
danielk197730548662009-07-09 05:07:37 +0000588 void *NotUsed,
589 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
590 int argc, /* Number of arguments */
591 const char **argv /* Text of each argument */
592){
593 int nCache;
594 Btree *pBt;
595
596 if( argc!=3 ){
597 Tcl_AppendResult(
598 interp, "wrong # args: should be \"", argv[0], " BT NCACHE\"", 0);
599 return TCL_ERROR;
600 }
601 pBt = sqlite3TestTextToPtr(argv[1]);
602 if( Tcl_GetInt(interp, argv[2], &nCache) ) return TCL_ERROR;
603
604 sqlite3_mutex_enter(pBt->db->mutex);
605 sqlite3BtreeEnter(pBt);
606 sqlite3BtreeSetCacheSize(pBt, nCache);
607 sqlite3BtreeLeave(pBt);
608 sqlite3_mutex_leave(pBt->db->mutex);
609 return TCL_OK;
610}
611
dan6b513642015-10-26 16:31:18 +0000612/*
613** usage: btree_insert CSR ?KEY? VALUE
614**
615** Set the size of the cache used by btree $ID.
616*/
mistachkin7617e4a2016-07-28 17:11:20 +0000617static int SQLITE_TCLAPI btree_insert(
dan6b513642015-10-26 16:31:18 +0000618 ClientData clientData,
619 Tcl_Interp *interp,
620 int objc,
621 Tcl_Obj *const objv[]
622){
623 BtCursor *pCur;
624 int rc;
drh8eeb4462016-05-21 20:03:42 +0000625 BtreePayload x;
dan6b513642015-10-26 16:31:18 +0000626
627 if( objc!=4 && objc!=3 ){
628 Tcl_WrongNumArgs(interp, 1, objv, "?-intkey? CSR KEY VALUE");
629 return TCL_ERROR;
630 }
631
drh8eeb4462016-05-21 20:03:42 +0000632 memset(&x, 0, sizeof(x));
dan6b513642015-10-26 16:31:18 +0000633 if( objc==4 ){
drh8eeb4462016-05-21 20:03:42 +0000634 if( Tcl_GetIntFromObj(interp, objv[2], &rc) ) return TCL_ERROR;
635 x.nKey = rc;
636 x.pData = (void*)Tcl_GetByteArrayFromObj(objv[3], &x.nData);
dan6b513642015-10-26 16:31:18 +0000637 }else{
drh8eeb4462016-05-21 20:03:42 +0000638 x.pKey = (void*)Tcl_GetByteArrayFromObj(objv[2], &rc);
639 x.nKey = rc;
dan6b513642015-10-26 16:31:18 +0000640 }
641 pCur = (BtCursor*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
642
dan6a75c8a2015-10-30 20:54:25 +0000643 sqlite3_mutex_enter(pCur->pBtree->db->mutex);
dan6b513642015-10-26 16:31:18 +0000644 sqlite3BtreeEnter(pCur->pBtree);
drh8eeb4462016-05-21 20:03:42 +0000645 rc = sqlite3BtreeInsert(pCur, &x, 0, 0);
dan6b513642015-10-26 16:31:18 +0000646 sqlite3BtreeLeave(pCur->pBtree);
dan6a75c8a2015-10-30 20:54:25 +0000647 sqlite3_mutex_leave(pCur->pBtree->db->mutex);
dan6b513642015-10-26 16:31:18 +0000648
649 Tcl_ResetResult(interp);
650 if( rc ){
651 Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
652 return TCL_ERROR;
653 }
654 return TCL_OK;
655}
danielk197730548662009-07-09 05:07:37 +0000656
danielk19771ad7f642005-01-20 05:24:32 +0000657
danielk197742741be2005-01-08 12:42:39 +0000658/*
drh5c4d9702001-08-20 00:33:58 +0000659** Register commands with the TCL interpreter.
660*/
661int Sqlitetest3_Init(Tcl_Interp *interp){
drhc2eef3b2002-08-31 18:53:06 +0000662 static struct {
663 char *zName;
664 Tcl_CmdProc *xProc;
665 } aCmd[] = {
666 { "btree_open", (Tcl_CmdProc*)btree_open },
667 { "btree_close", (Tcl_CmdProc*)btree_close },
668 { "btree_begin_transaction", (Tcl_CmdProc*)btree_begin_transaction },
drhc2eef3b2002-08-31 18:53:06 +0000669 { "btree_pager_stats", (Tcl_CmdProc*)btree_pager_stats },
drhc2eef3b2002-08-31 18:53:06 +0000670 { "btree_cursor", (Tcl_CmdProc*)btree_cursor },
671 { "btree_close_cursor", (Tcl_CmdProc*)btree_close_cursor },
drhc2eef3b2002-08-31 18:53:06 +0000672 { "btree_next", (Tcl_CmdProc*)btree_next },
drhc39e0002004-05-07 23:50:57 +0000673 { "btree_eof", (Tcl_CmdProc*)btree_eof },
drhc2eef3b2002-08-31 18:53:06 +0000674 { "btree_payload_size", (Tcl_CmdProc*)btree_payload_size },
675 { "btree_first", (Tcl_CmdProc*)btree_first },
drh6d2fb152004-05-14 16:50:06 +0000676 { "btree_varint_test", (Tcl_CmdProc*)btree_varint_test },
danielk197742741be2005-01-08 12:42:39 +0000677 { "btree_from_db", (Tcl_CmdProc*)btree_from_db },
danielk197730548662009-07-09 05:07:37 +0000678 { "btree_ismemdb", (Tcl_CmdProc*)btree_ismemdb },
679 { "btree_set_cache_size", (Tcl_CmdProc*)btree_set_cache_size }
drhc2eef3b2002-08-31 18:53:06 +0000680 };
681 int i;
682
683 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
684 Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
685 }
danielk1977312d6b32004-06-29 13:18:23 +0000686
dan6b513642015-10-26 16:31:18 +0000687 Tcl_CreateObjCommand(interp, "btree_insert", btree_insert, 0, 0);
688
drh5c4d9702001-08-20 00:33:58 +0000689 return TCL_OK;
690}