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