PageRenderTime 81ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/testfixture/src/test1_c.cs

https://bitbucket.org/eumario/csharp-sqlite
C# | 6843 lines | 4352 code | 479 blank | 2012 comment | 946 complexity | bf9b03885ff23f6ac2f249711e2306e7 MD5 | raw file
  1. using System;
  2. using System.Diagnostics;
  3. using System.Text;
  4. using i64 = System.Int64;
  5. using u32 = System.UInt32;
  6. namespace Community.CsharpSqlite
  7. {
  8. #if TCLSH
  9. using tcl.lang;
  10. using ClientData = System.Object;
  11. using sqlite3_int64 = System.Int64;
  12. using sqlite3_stmt = Sqlite3.Vdbe;
  13. using sqlite3_u3264 = System.UInt64;
  14. using sqlite3_value = Sqlite3.Mem;
  15. using Tcl_Interp = tcl.lang.Interp;
  16. using Tcl_Obj = tcl.lang.TclObject;
  17. public partial class Sqlite3
  18. {
  19. /*
  20. ** 2001 September 15
  21. **
  22. ** The author disclaims copyright to this source code. In place of
  23. ** a legal notice, here is a blessing:
  24. **
  25. ** May you do good and not evil.
  26. ** May you find forgiveness for yourself and forgive others.
  27. ** May you share freely, never taking more than you give.
  28. **
  29. *************************************************************************
  30. ** Code for testing all sorts of SQLite interfaces. This code
  31. ** is not included in the SQLite library. It is used for automated
  32. ** testing of the SQLite library.
  33. *************************************************************************
  34. ** Included in SQLite3 port to C#-SQLite; 2008 Noah B Hart
  35. ** C#-SQLite is an independent reimplementation of the SQLite software library
  36. **
  37. ** SQLITE_SOURCE_ID: 2011-06-23 19:49:22 4374b7e83ea0a3fbc3691f9c0c936272862f32f2
  38. **
  39. *************************************************************************
  40. */
  41. //#include "sqliteInt.h"
  42. //#include "tcl.h"
  43. //#include <stdlib.h>
  44. //#include <string.h>
  45. /*
  46. ** This is a copy of the first part of the SqliteDb structure in
  47. ** tclsqlite.c. We need it here so that the get_sqlite_pointer routine
  48. ** can extract the sqlite3* pointer from an existing Tcl SQLite
  49. ** connection.
  50. */
  51. //struct SqliteDb {
  52. // sqlite3 db=null;
  53. //};
  54. /*
  55. ** Convert text generated by the "%p" conversion format back into
  56. ** a pointer.
  57. */
  58. static int testHexToInt( int h )
  59. {
  60. if ( h >= '0' && h <= '9' )
  61. {
  62. return h - '0';
  63. }
  64. else if ( h >= 'a' && h <= 'f' )
  65. {
  66. return h - 'a' + 10;
  67. }
  68. else
  69. {
  70. Debug.Assert( h >= 'A' && h <= 'F' );
  71. return h - 'A' + 10;
  72. }
  73. }
  74. static object sqlite3TestTextToPtr( Tcl_Interp interp, string z )
  75. {
  76. //object p ;
  77. //var v = new u64[1];
  78. //u32 v2;
  79. //int zIndex = 0;
  80. //if ( z[0] == '0' && z[1] == 'x' )
  81. //{
  82. // zIndex += 2;
  83. //}
  84. //v[0] = 0;
  85. //while ( zIndex < z.Length )* z )
  86. //{
  87. // v[0] = ( v[0] << 4 ) + (ulong)testHexToInt( z[zIndex] );
  88. // zIndex++;
  89. //}
  90. //if ( sizeof( object ) == sizeof( u64 ) )
  91. //{
  92. // Marshal.Copy( v, 0, (IntPtr)p, 1 );// memcpy( &p, v, sizeof( p ) );
  93. //}
  94. //else
  95. //{
  96. // Debug.Assert( sizeof( p ) == sizeof( v2 ) );
  97. // v2 = (u32)v;
  98. // memcpy( &p, v2, sizeof( p ) );
  99. //}
  100. WrappedCommand cmdInfo = new WrappedCommand();
  101. if ( TCL.Tcl_GetCommandInfo( interp, z, out cmdInfo ) || cmdInfo == null )
  102. {
  103. return null;
  104. }
  105. else
  106. {
  107. return cmdInfo.objClientData;
  108. }
  109. }
  110. /*
  111. ** A TCL command that returns the address of the sqlite* pointer
  112. ** for an sqlite connection instance. Bad things happen if the
  113. ** input is not an sqlite connection.
  114. */
  115. static int get_sqlite_pointer(
  116. object clientdata,
  117. Tcl_Interp interp,
  118. int objc,
  119. Tcl_Obj[] objv
  120. )
  121. {
  122. SqliteDb p;
  123. WrappedCommand cmdInfo = null;
  124. //string zBuf ;//[100];
  125. if ( objc != 2 )
  126. {
  127. TCL.Tcl_WrongNumArgs( interp, 1, objv, "SQLITE-CONNECTION" );
  128. return TCL.TCL_ERROR;
  129. }
  130. if ( TCL.Tcl_GetCommandInfo( interp, objv[1].ToString(), out cmdInfo ) )
  131. {
  132. TCL.Tcl_AppendResult( interp, "command not found: ",
  133. TCL.Tcl_GetString( objv[1] ), null );
  134. return TCL.TCL_ERROR;
  135. }
  136. //p = (SqliteDb)cmdInfo.objclientdata;
  137. //zBuf = p.db.GetHashCode().ToString();
  138. //sqlite3_snprintf( zBuf, "%p", p.db );
  139. //if( strncmp(zBuf,"0x",2) ){
  140. // sqlite3_snprintf(zBuf, "0x%p", p.db);
  141. //}
  142. //TCL.Tcl_AppendResult(interp, zBuf,null );
  143. TCL.Tcl_AppendResult( interp, objv[1].ToString() );
  144. return TCL.TCL_OK;
  145. }
  146. /*
  147. ** Decode a pointer to an sqlite3 object.
  148. */
  149. static int getDbPointer( Tcl_Interp interp, string zA, out sqlite3 ppDb )
  150. {
  151. SqliteDb p;
  152. WrappedCommand cmdInfo = new WrappedCommand();
  153. if ( !TCL.Tcl_GetCommandInfo( interp, zA, out cmdInfo ) )
  154. {
  155. if ( cmdInfo == null )
  156. {
  157. ppDb = new sqlite3();
  158. }
  159. else
  160. {
  161. p = (SqliteDb)cmdInfo.objClientData;
  162. ppDb = p.db;
  163. }
  164. }
  165. else
  166. {
  167. ppDb = null;
  168. }
  169. return TCL.TCL_OK;
  170. }
  171. static string sqlite3TestErrorName( int rc )
  172. {
  173. string zName = "";
  174. switch ( rc )
  175. {
  176. case SQLITE_OK:
  177. zName = "SQLITE_OK";
  178. break;
  179. case SQLITE_ERROR:
  180. zName = "SQLITE_ERROR";
  181. break;
  182. case SQLITE_INTERNAL:
  183. zName = "SQLITE_INTERNAL";
  184. break;
  185. case SQLITE_PERM:
  186. zName = "SQLITE_PERM";
  187. break;
  188. case SQLITE_ABORT:
  189. zName = "SQLITE_ABORT";
  190. break;
  191. case SQLITE_BUSY:
  192. zName = "SQLITE_BUSY";
  193. break;
  194. case SQLITE_LOCKED:
  195. zName = "SQLITE_LOCKED";
  196. break;
  197. case SQLITE_LOCKED_SHAREDCACHE:
  198. zName = "SQLITE_LOCKED_SHAREDCACHE";
  199. break;
  200. case SQLITE_NOMEM:
  201. zName = "SQLITE_NOMEM";
  202. break;
  203. case SQLITE_READONLY:
  204. zName = "SQLITE_READONLY";
  205. break;
  206. case SQLITE_INTERRUPT:
  207. zName = "SQLITE_INTERRUPT";
  208. break;
  209. case SQLITE_IOERR:
  210. zName = "SQLITE_IOERR";
  211. break;
  212. case SQLITE_CORRUPT:
  213. zName = "SQLITE_CORRUPT";
  214. break;
  215. case SQLITE_NOTFOUND:
  216. zName = "SQLITE_NOTFOUND";
  217. break;
  218. case SQLITE_FULL:
  219. zName = "SQLITE_FULL";
  220. break;
  221. case SQLITE_CANTOPEN:
  222. zName = "SQLITE_CANTOPEN";
  223. break;
  224. case SQLITE_PROTOCOL:
  225. zName = "SQLITE_PROTOCOL";
  226. break;
  227. case SQLITE_EMPTY:
  228. zName = "SQLITE_EMPTY";
  229. break;
  230. case SQLITE_SCHEMA:
  231. zName = "SQLITE_SCHEMA";
  232. break;
  233. case SQLITE_TOOBIG:
  234. zName = "SQLITE_TOOBIG";
  235. break;
  236. case SQLITE_CONSTRAINT:
  237. zName = "SQLITE_CONSTRAINT";
  238. break;
  239. case SQLITE_MISMATCH:
  240. zName = "SQLITE_MISMATCH";
  241. break;
  242. case SQLITE_MISUSE:
  243. zName = "SQLITE_MISUSE";
  244. break;
  245. case SQLITE_NOLFS:
  246. zName = "SQLITE_NOLFS";
  247. break;
  248. case SQLITE_AUTH:
  249. zName = "SQLITE_AUTH";
  250. break;
  251. case SQLITE_FORMAT:
  252. zName = "SQLITE_FORMAT";
  253. break;
  254. case SQLITE_RANGE:
  255. zName = "SQLITE_RANGE";
  256. break;
  257. case SQLITE_NOTADB:
  258. zName = "SQLITE_NOTADB";
  259. break;
  260. case SQLITE_ROW:
  261. zName = "SQLITE_ROW";
  262. break;
  263. case SQLITE_DONE:
  264. zName = "SQLITE_DONE";
  265. break;
  266. case SQLITE_IOERR_READ:
  267. zName = "SQLITE_IOERR_READ";
  268. break;
  269. case SQLITE_IOERR_SHORT_READ:
  270. zName = "SQLITE_IOERR_SHORT_READ";
  271. break;
  272. case SQLITE_IOERR_WRITE:
  273. zName = "SQLITE_IOERR_WRITE";
  274. break;
  275. case SQLITE_IOERR_FSYNC:
  276. zName = "SQLITE_IOERR_FSYNC";
  277. break;
  278. case SQLITE_IOERR_DIR_FSYNC:
  279. zName = "SQLITE_IOERR_DIR_FSYNC";
  280. break;
  281. case SQLITE_IOERR_TRUNCATE:
  282. zName = "SQLITE_IOERR_TRUNCATE";
  283. break;
  284. case SQLITE_IOERR_FSTAT:
  285. zName = "SQLITE_IOERR_FSTAT";
  286. break;
  287. case SQLITE_IOERR_UNLOCK:
  288. zName = "SQLITE_IOERR_UNLOCK";
  289. break;
  290. case SQLITE_IOERR_RDLOCK:
  291. zName = "SQLITE_IOERR_RDLOCK";
  292. break;
  293. case SQLITE_IOERR_DELETE:
  294. zName = "SQLITE_IOERR_DELETE";
  295. break;
  296. case SQLITE_IOERR_BLOCKED:
  297. zName = "SQLITE_IOERR_BLOCKED";
  298. break;
  299. case SQLITE_IOERR_NOMEM:
  300. zName = "SQLITE_IOERR_NOMEM";
  301. break;
  302. case SQLITE_IOERR_ACCESS:
  303. zName = "SQLITE_IOERR_ACCESS";
  304. break;
  305. case SQLITE_IOERR_CHECKRESERVEDLOCK:
  306. zName = "SQLITE_IOERR_CHECKRESERVEDLOCK";
  307. break;
  308. case SQLITE_IOERR_LOCK:
  309. zName = "SQLITE_IOERR_LOCK";
  310. break;
  311. case SQLITE_CORRUPT_VTAB:
  312. zName = "SQLITE_CORRUPT_VTAB";
  313. break;
  314. case SQLITE_READONLY_RECOVERY:
  315. zName = "SQLITE_READONLY_RECOVERY";
  316. break;
  317. case SQLITE_READONLY_CANTLOCK:
  318. zName = "SQLITE_READONLY_CANTLOCK";
  319. break;
  320. default:
  321. zName = "SQLITE_Unknown";
  322. break;
  323. }
  324. return zName;
  325. }
  326. //#define t1ErrorName sqlite3TestErrorName
  327. static string t1ErrorName( int i )
  328. {
  329. return sqlite3TestErrorName( i );
  330. }
  331. /*
  332. ** Convert an sqlite3_stmt* into an sqlite3*. This depends on the
  333. ** fact that the sqlite3* is the first field in the Vdbe structure.
  334. */
  335. //#define StmtToDb(X) sqlite3_db_handle(X)
  336. static sqlite3 StmtToDb( Vdbe v )
  337. {
  338. return sqlite3_db_handle( v );
  339. }
  340. /*
  341. ** Check a return value to make sure it agrees with the results
  342. ** from sqlite3_errcode.
  343. */
  344. static int sqlite3TestErrCode( Tcl_Interp interp, sqlite3 db, int rc )
  345. {
  346. if ( sqlite3_threadsafe() == 0 && rc != SQLITE_MISUSE && rc != SQLITE_OK
  347. && sqlite3_errcode( db ) != rc )
  348. {
  349. StringBuilder zBuf = new StringBuilder( 200 );//char zBuf[200];
  350. int r2 = sqlite3_errcode( db );
  351. sqlite3_snprintf( 200, zBuf, "error code %s (%d) does not match sqlite3_errcode %s (%d)",
  352. sqlite3TestErrorName( rc ), rc, sqlite3TestErrorName( r2 ), r2 );//t1ErrorName( rc ), rc, t1ErrorName( r2 ), r2 );
  353. TCL.Tcl_ResetResult( interp );
  354. TCL.Tcl_AppendResult( interp, zBuf.ToString() );
  355. return 1;
  356. }
  357. return 0;
  358. }
  359. /*
  360. ** Decode a pointer to an sqlite3_stmt object.
  361. */
  362. static int getStmtPointer(
  363. Tcl_Interp interp,
  364. string zArg,
  365. out sqlite3_stmt ppStmt
  366. )
  367. {
  368. ppStmt = (sqlite3_stmt)sqlite3TestTextToPtr( interp, zArg );
  369. WrappedCommand cmdInfo = new WrappedCommand();
  370. TCL.Tcl_GetCommandInfo( interp, zArg, out cmdInfo );
  371. ppStmt = cmdInfo == null ? null : (sqlite3_stmt)cmdInfo.objClientData;
  372. return TCL.TCL_OK;
  373. }
  374. /*
  375. ** Generate a text representation of a pointer that can be understood
  376. ** by the getDbPointer and getVmPointer routines above.
  377. **
  378. ** The problem is, on some machines (Solaris) if you do a printf with
  379. ** "%p" you cannot turn around and do a scanf with the same "%p" and
  380. ** get your pointer back. You have to prepend a "0x" before it will
  381. ** work. Or at least that is what is reported to me (drh). But this
  382. ** behavior varies from machine to machine. The solution used her is
  383. ** to test the string right after it is generated to see if it can be
  384. ** understood by scanf, and if not, try prepending an "0x" to see if
  385. ** that helps. If nothing works, a fatal error is generated.
  386. */
  387. /*
  388. ** Decode a pointer to an sqlite3_stmt object.
  389. */
  390. static int sqlite3TestMakePointerStr( Tcl_Interp interp, StringBuilder zPtr, object p )
  391. {
  392. sqlite3_snprintf( 100, zPtr, "->%p", p );
  393. if ( TCL.Tcl_CreateCommandPointer( interp, zPtr, p ) )
  394. {
  395. return TCL.TCL_ERROR;
  396. }
  397. return TCL.TCL_OK;
  398. }
  399. /*
  400. ** The callback routine for sqlite3_exec_printf().
  401. */
  402. static int exec_printf_cb( object pArg, sqlite3_int64 argc, object p2, object p3 )
  403. {
  404. string[] name = (string[])p3;
  405. string[] argv = (string[])p2;
  406. TclObject str = (TclObject)pArg;
  407. int i;
  408. if ( TCL.Tcl_DStringLength( str ) == 0 )
  409. {
  410. for ( i = 0; i < argc; i++ )
  411. {
  412. TCL.Tcl_DStringAppendElement( str, name[i] != null ? name[i] + " " : "NULL " );
  413. }
  414. }
  415. string beginbrace = "", endbrace = "";
  416. for ( i = 0; i < argc; i++ )
  417. {
  418. if ( argc > 1 )
  419. {
  420. if ( Util.scanElement( null, argv[i].ToString() ) != 0 )
  421. {
  422. beginbrace = "{";
  423. endbrace = "}";
  424. }
  425. else
  426. {
  427. beginbrace = "";
  428. endbrace = "";
  429. }
  430. }
  431. TCL.Tcl_DStringAppendElement( str, argv[i] != null ? beginbrace + argv[i] + endbrace + ( i < argc - 1 ? " " : "" ) : "NULL" );
  432. }
  433. return 0;
  434. }
  435. /*
  436. ** The I/O tracing callback.
  437. */
  438. #if !(SQLITE_OMIT_TRACE) && TRACE
  439. //static FILE *iotrace_file = 0;
  440. //static void io_trace_callback(string zFormat, ...){
  441. // va_list ap;
  442. // va_start(ap, zFormat);
  443. // vfprintf(iotrace_file, zFormat, ap);
  444. // va_end(ap);
  445. // fflush(iotrace_file);
  446. //}
  447. #endif
  448. /*
  449. ** Usage: io_trace FILENAME
  450. **
  451. ** Turn I/O tracing on or off. If FILENAME is not an empty string,
  452. ** I/O tracing begins going into FILENAME. If FILENAME is an empty
  453. ** string, I/O tracing is turned off.
  454. */
  455. //static int test_io_trace(
  456. // object NotUsed,
  457. // Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  458. // int argc, /* Number of arguments */
  459. // Tcl_Obj[] argv /* Text of each argument */
  460. //){
  461. #if !(SQLITE_OMIT_TRACE) && (TRACE)
  462. // if( argc!=2 ){
  463. // TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  464. // " FILENAME\"", 0);
  465. // return TCL.TCL_ERROR;
  466. // }
  467. // if( iotrace_file ){
  468. // if( iotrace_file!=stdout && iotrace_file!=stderr ){
  469. // fclose(iotrace_file);
  470. // }
  471. // iotrace_file = 0;
  472. // sqlite3IoTrace = 0;
  473. // }
  474. // if( argv[1][0] ){
  475. // if( strcmp(argv[1],"stdout")==0 ){
  476. // iotrace_file = stdout;
  477. // }else if( strcmp(argv[1],"stderr")==0 ){
  478. // iotrace_file = stderr;
  479. // }else{
  480. // iotrace_file = fopen(argv[1], "w");
  481. // }
  482. // sqlite3IoTrace = io_trace_callback;
  483. // }
  484. #endif
  485. // return TCL.TCL_OK;
  486. //}
  487. /*
  488. ** Usage: sqlite3_exec_printf DB FORMAT STRING
  489. **
  490. ** Invoke the sqlite3_exec_printf() interface using the open database
  491. ** DB. The SQL is the string FORMAT. The format string should contain
  492. ** one %s or %q. STRING is the value inserted into %s or %q.
  493. */
  494. static int test_exec_printf(
  495. object NotUsed,
  496. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  497. int argc, /* Number of arguments */
  498. Tcl_Obj[] argv /* Text of each argument */
  499. )
  500. {
  501. sqlite3 db = null;
  502. TclObject str = null;
  503. int rc;
  504. string zErr = "";
  505. string zSql = "";
  506. StringBuilder zBuf = new StringBuilder( 30 );
  507. if ( argc != 4 )
  508. {
  509. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  510. " DB FORMAT STRING" );
  511. return TCL.TCL_ERROR;
  512. }
  513. if ( getDbPointer( interp, argv[1].ToString(), out db ) != 0 )
  514. return TCL.TCL_ERROR;
  515. TCL.Tcl_DStringInit( out str );
  516. zSql = sqlite3_mprintf( argv[2].ToString(), argv[3].ToString() );
  517. rc = sqlite3_exec( db, zSql, (dxCallback)exec_printf_cb, str, ref zErr );
  518. sqlite3DbFree( db, ref zSql );
  519. sqlite3_snprintf( 30, zBuf, "%d", rc );
  520. TCL.Tcl_AppendElement( interp, zBuf );
  521. TCL.Tcl_AppendElement( interp, rc == SQLITE_OK ? str.ToString() : zErr ); //TCL.Tcl_DStringValue(ref str)
  522. TCL.Tcl_DStringFree( ref str );
  523. if ( zErr != null )
  524. sqlite3DbFree( db, ref zErr );
  525. if ( sqlite3TestErrCode( interp, db, rc ) != 0 )
  526. return TCL.TCL_ERROR;
  527. return TCL.TCL_OK;
  528. }
  529. /*
  530. ** Usage: sqlite3_exec_hex DB HEX
  531. **
  532. ** Invoke the sqlite3_exec() on a string that is obtained by translating
  533. ** HEX into ASCII. Most characters are translated as is. %HH becomes
  534. ** a hex character.
  535. */
  536. static int test_exec_hex(
  537. object NotUsed,
  538. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  539. int argc, /* Number of arguments */
  540. Tcl_Obj[] argv /* Text of each argument */
  541. )
  542. {
  543. sqlite3 db = null;
  544. TclObject str = null;
  545. int rc, i, j;
  546. string zErr = "";
  547. string zHex;
  548. StringBuilder zSql = new StringBuilder( 500 );
  549. string zBuf = "";
  550. if ( argc != 3 )
  551. {
  552. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  553. " DB HEX" );
  554. return TCL.TCL_ERROR;
  555. }
  556. if ( getDbPointer( interp, argv[1].ToString(), out db ) != 0 )
  557. return TCL.TCL_ERROR;
  558. zHex = argv[2].ToString();
  559. for ( i = j = 0; j < zHex.Length && zHex[j] != 0; i++, j++ )
  560. {
  561. if ( zHex[j] == '%' && zHex[j + 2] != 0 && zHex[j + 2] != 0 )
  562. {
  563. zSql.Append( (char)( ( testHexToInt( zHex[j + 1] ) << 4 ) + testHexToInt( zHex[j + 2] ) ) );
  564. j += 2;
  565. }
  566. else
  567. {
  568. zSql.Append( zHex[j] );
  569. }
  570. }
  571. //zSql[i] = '\0';
  572. TCL.Tcl_DStringInit( out str );
  573. rc = sqlite3_exec( db, zSql.ToString(), (dxCallback)exec_printf_cb, str, ref zErr );
  574. zBuf = rc.ToString();// sprintf( zBuf, "%d", rc );
  575. TCL.Tcl_AppendElement( interp, zBuf );
  576. TCL.Tcl_AppendElement( interp, rc == SQLITE_OK ? str.ToString() : zErr );
  577. TCL.Tcl_DStringFree( ref str );
  578. // //sqlite3_free(ref zErr);
  579. if ( sqlite3TestErrCode( interp, db, rc ) != 0 )
  580. return TCL.TCL_ERROR;
  581. return TCL.TCL_OK;
  582. }
  583. /*
  584. ** Usage: db_enter DB
  585. ** db_leave DB
  586. **
  587. ** Enter or leave the mutex on a database connection.
  588. */
  589. static int db_enter(
  590. object NotUsed,
  591. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  592. int argc, /* Number of arguments */
  593. Tcl_Obj[] argv /* Text of each argument */
  594. )
  595. {
  596. sqlite3 db = null;
  597. if ( argc != 2 )
  598. {
  599. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  600. " DB" );
  601. return TCL.TCL_ERROR;
  602. }
  603. if ( getDbPointer( interp, argv[1].ToString(), out db ) != 0 )
  604. return TCL.TCL_ERROR;
  605. sqlite3_mutex_enter( db.mutex );
  606. return TCL.TCL_OK;
  607. }
  608. static int db_leave(
  609. object NotUsed,
  610. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  611. int argc, /* Number of arguments */
  612. Tcl_Obj[] argv /* Text of each argument */
  613. )
  614. {
  615. sqlite3 db = null;
  616. if ( argc != 2 )
  617. {
  618. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  619. " DB" );
  620. return TCL.TCL_ERROR;
  621. }
  622. if ( getDbPointer( interp, argv[1].ToString(), out db ) != 0 )
  623. return TCL.TCL_ERROR;
  624. sqlite3_mutex_leave( db.mutex );
  625. return TCL.TCL_OK;
  626. }
  627. /*
  628. ** Usage: sqlite3_exec DB SQL
  629. **
  630. ** Invoke the sqlite3_exec interface using the open database DB
  631. */
  632. static int test_exec(
  633. object NotUsed,
  634. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  635. int argc, /* Number of arguments */
  636. Tcl_Obj[] argv /* Text of each argument */
  637. )
  638. {
  639. sqlite3 db = null;
  640. TclObject str = TclString.newInstance( "" );
  641. int rc;
  642. string zErr = "";
  643. string zSql;
  644. int i, j;
  645. StringBuilder zBuf = new StringBuilder( 30 );
  646. if ( argc != 3 )
  647. {
  648. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  649. " DB SQL" );
  650. return TCL.TCL_ERROR;
  651. }
  652. if ( getDbPointer( interp, argv[1].ToString(), out db ) != 0 )
  653. return TCL.TCL_ERROR;
  654. TCL.Tcl_DStringInit( out str );
  655. zSql = sqlite3_mprintf( "%s", argv[2].ToString() );
  656. StringBuilder sb = new StringBuilder( zSql.Length );
  657. for ( i = 0; i < zSql.Length; i++ )
  658. {
  659. if ( zSql[i] == '%' )
  660. {
  661. sb.Append( (char)( ( testHexToInt( zSql[i + 1] ) << 4 ) + testHexToInt( zSql[i + 2] ) ) );
  662. i += 2;
  663. }
  664. else
  665. sb.Append( zSql[i] );
  666. }
  667. ////zSql[j] = 0;
  668. rc = sqlite3_exec( db, sb.ToString(), exec_printf_cb, str, ref zErr );
  669. sqlite3DbFree( db, ref zSql );
  670. sqlite3_snprintf( 30, zBuf, "%d", rc );
  671. TCL.Tcl_AppendElement( interp, zBuf );
  672. TCL.Tcl_AppendElement( interp, rc == SQLITE_OK ? str.ToString() : zErr );
  673. //TCL.Tcl_DStringFree(&str);
  674. if ( zErr != "" )
  675. sqlite3DbFree( db, ref zErr );
  676. if ( sqlite3TestErrCode( interp, db, rc ) != 0 )
  677. return TCL.TCL_ERROR;
  678. return TCL.TCL_OK;
  679. }
  680. /*
  681. ** Usage: sqlite3_exec_nr DB SQL
  682. **
  683. ** Invoke the sqlite3_exec interface using the open database DB. Discard
  684. ** all results
  685. */
  686. static int test_exec_nr(
  687. object NotUsed,
  688. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  689. int argc, /* Number of arguments */
  690. Tcl_Obj[] argv /* Text of each argument */
  691. )
  692. {
  693. sqlite3 db = null;
  694. int rc;
  695. string zErr = "";
  696. if ( argc != 3 )
  697. {
  698. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  699. " DB SQL" );
  700. return TCL.TCL_ERROR;
  701. }
  702. if ( getDbPointer( interp, argv[1].ToString(), out db ) != 0 )
  703. return TCL.TCL_ERROR;
  704. rc = sqlite3_exec( db, argv[2].ToString(), null, null, ref zErr );
  705. if ( sqlite3TestErrCode( interp, db, rc ) != 0 )
  706. return TCL.TCL_ERROR;
  707. return TCL.TCL_OK;
  708. }
  709. /*
  710. ** Usage: sqlite3_mprintf_z_test SEPARATOR ARG0 ARG1 ...
  711. **
  712. ** Test the %z format of sqlite_mprintf(). Use multiple mprintf() calls to
  713. ** concatenate arg0 through argn using separator as the separator.
  714. ** Return the result.
  715. */
  716. static int test_mprintf_z(
  717. object NotUsed,
  718. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  719. int argc, /* Number of arguments */
  720. Tcl_Obj[] argv /* Text of each argument */
  721. )
  722. {
  723. string zResult = "";
  724. int i;
  725. for ( i = 2; i < argc && ( i == 2 || zResult != "" ); i++ )
  726. {
  727. zResult = sqlite3_mprintf( "%z%s%s", zResult, argv[1].ToString(), argv[i].ToString() );
  728. }
  729. TCL.Tcl_AppendResult( interp, zResult );
  730. //sqlite3DbFree( db, zResult );
  731. return TCL.TCL_OK;
  732. }
  733. /*
  734. ** Usage: sqlite3_mprintf_n_test STRING
  735. **
  736. ** Test the %n format of sqlite_mprintf(). Return the length of the
  737. ** input string.
  738. */
  739. static int test_mprintf_n(
  740. object NotUsed,
  741. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  742. int argc, /* Number of arguments */
  743. Tcl_Obj[] argv /* Text of each argument */
  744. )
  745. {
  746. string zStr;
  747. int n = 0;
  748. zStr = sqlite3_mprintf( "%s%n", argv[1].ToString() );
  749. n = zStr.Length;
  750. //sqlite3DbFree( db, zStr );
  751. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewIntObj( n ) );
  752. return TCL.TCL_OK;
  753. }
  754. /*
  755. ** Usage: sqlite3_snprintf_int SIZE FORMAT INT
  756. **
  757. ** Test the of sqlite3_snprintf() routine. SIZE is the size of the
  758. ** output buffer in bytes. The maximum size is 100. FORMAT is the
  759. ** format string. INT is a single integer argument. The FORMAT
  760. ** string must require no more than this one integer argument. If
  761. ** You pass in a format string that requires more than one argument,
  762. ** bad things will happen.
  763. */
  764. static int test_snprintf_int(
  765. object NotUsed,
  766. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  767. int argc, /* Number of arguments */
  768. Tcl_Obj[] argv /* Text of each argument */
  769. )
  770. {
  771. StringBuilder zStr = new StringBuilder( 100 );
  772. int n = atoi( argv[1].ToString() );
  773. string zFormat = argv[2].ToString();
  774. int a1 = atoi( argv[3].ToString() );
  775. if ( n > zStr.Capacity )
  776. n = zStr.Capacity;// sizeof( zStr );
  777. zStr = new StringBuilder( "abcdefghijklmnopqrstuvwxyz" );
  778. sqlite3_snprintf( n, zStr, zFormat, a1 );
  779. TCL.Tcl_AppendResult( interp, zStr );
  780. return TCL.TCL_OK;
  781. }
  782. #if !SQLITE_OMIT_GET_TABLE
  783. /*
  784. ** Usage: sqlite3_get_table_printf DB FORMAT STRING ?--no-counts?
  785. **
  786. ** Invoke the sqlite3_get_table_printf() interface using the open database
  787. ** DB. The SQL is the string FORMAT. The format string should contain
  788. ** one %s or %q. STRING is the value inserted into %s or %q.
  789. */
  790. static int test_get_table_printf(
  791. object NotUsed,
  792. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  793. int argc, /* Number of arguments */
  794. Tcl_Obj[] argv /* Text of each argument */
  795. ){
  796. sqlite3 db=null;
  797. TCL.Tcl_DString str;
  798. int rc;
  799. string zErr = 0;
  800. int nRow, nCol;
  801. char **aResult;
  802. int i;
  803. char zBuf[30];
  804. string zSql;
  805. int resCount = -1;
  806. if( argc==5 ){
  807. if( TCL.Tcl_GetInt(interp, argv[4], out resCount) ) return TCL.TCL_ERROR;
  808. }
  809. if( argc!=4 && argc!=5 ){
  810. TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  811. " DB FORMAT STRING ?COUNT?", 0);
  812. return TCL.TCL_ERROR;
  813. }
  814. if( getDbPointer(interp, argv[1].ToString(), out db) !=0) return TCL.TCL_ERROR;
  815. TCL.Tcl_DStringInit(&str);
  816. zSql = sqlite3_mprintf(argv[2],argv[3]);
  817. if( argc==5 ){
  818. rc = sqlite3_get_table(db, zSql, aResult, 0, 0, zErr);
  819. }else{
  820. rc = sqlite3_get_table(db, zSql, aResult, nRow, nCol, zErr);
  821. resCount = (nRow+1)*nCol;
  822. }
  823. sqlite3DbFree(db,zSql);
  824. sqlite3_snprintf(zBuf, "%d", rc);
  825. TCL.Tcl_AppendElement(interp, zBuf);
  826. if( rc==SQLITE_OK ){
  827. if( argc==4 ){
  828. sqlite3_snprintf(zBuf, "%d", nRow);
  829. TCL.Tcl_AppendElement(interp, zBuf);
  830. sqlite3_snprintf(zBuf, "%d", nCol);
  831. TCL.Tcl_AppendElement(interp, zBuf);
  832. }
  833. for(i=0; i<resCount; i++){
  834. TCL.Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL");
  835. }
  836. }else{
  837. TCL.Tcl_AppendElement(interp, zErr);
  838. }
  839. //sqlite3_free_table(aResult);
  840. if( zErr ) sqlite3DbFree(db,zErr);
  841. if( sqlite3TestErrCode(interp, db, rc) ) return TCL.TCL_ERROR;
  842. return TCL.TCL_OK;
  843. }
  844. #endif //* SQLITE_OMIT_GET_TABLE*/
  845. /*
  846. ** Usage: sqlite3_last_insert_rowid DB
  847. **
  848. ** Returns the integer ROWID of the most recent insert.
  849. */
  850. //static int test_last_rowid(
  851. // object NotUsed,
  852. // Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  853. // int argc, /* Number of arguments */
  854. // Tcl_Obj[] argv /* Text of each argument */
  855. //){
  856. // sqlite3 db=null;
  857. // char zBuf[30];
  858. // if( argc!=2 ){
  859. // TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"");
  860. // return TCL.TCL_ERROR;
  861. // }
  862. // if( getDbPointer(interp, argv[1].ToString(), out db) !=0) return TCL.TCL_ERROR;
  863. // sqlite3_snprintf(zBuf, "%lld", sqlite3_last_insert_rowid(db));
  864. // TCL.Tcl_AppendResult(interp, zBuf);
  865. // return SQLITE_OK;
  866. //}
  867. /*
  868. ** Usage: sqlite3_key DB KEY
  869. **
  870. ** Set the codec key.
  871. */
  872. static int test_key(
  873. object NotUsed,
  874. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  875. int argc, /* Number of arguments */
  876. Tcl_Obj[] argv /* Text of each argument */
  877. )
  878. {
  879. sqlite3 db = null;
  880. string zKey;
  881. int nKey;
  882. if ( argc != 3 )
  883. {
  884. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  885. " FILENAME\"" );
  886. return TCL.TCL_ERROR;
  887. }
  888. if ( getDbPointer( interp, argv[1].ToString(), out db ) != 0 )
  889. return TCL.TCL_ERROR;
  890. zKey = argv[2].ToString();
  891. nKey = zKey.Length;
  892. #if SQLITE_HAS_CODEC
  893. sqlite3_key( db, zKey, nKey );
  894. #endif
  895. return TCL.TCL_OK;
  896. }
  897. /*
  898. ** Usage: sqlite3_rekey DB KEY
  899. **
  900. ** Change the codec key.
  901. */
  902. static int test_rekey(
  903. object NotUsed,
  904. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  905. int argc, /* Number of arguments */
  906. Tcl_Obj[] argv /* Text of each argument */
  907. )
  908. {
  909. sqlite3 db = null;
  910. string zKey;
  911. int nKey;
  912. if ( argc != 3 )
  913. {
  914. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  915. " FILENAME\"" );
  916. return TCL.TCL_ERROR;
  917. }
  918. if ( getDbPointer( interp, argv[1].ToString(), out db ) != 0 )
  919. return TCL.TCL_ERROR;
  920. zKey = argv[2].ToString();
  921. nKey = zKey.Length;
  922. #if SQLITE_HAS_CODEC
  923. sqlite3_rekey( db, zKey, nKey );
  924. #endif
  925. return TCL.TCL_OK;
  926. }
  927. /*
  928. ** Usage: sqlite3_close DB
  929. **
  930. ** Closes the database opened by sqlite3_open.
  931. */
  932. static int sqlite_test_close(
  933. object NotUsed,
  934. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  935. int argc, /* Number of arguments */
  936. Tcl_Obj[] argv /* Text of each argument */
  937. )
  938. {
  939. sqlite3 db = null;
  940. int rc;
  941. if ( argc != 2 )
  942. {
  943. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  944. " FILENAME\"" );
  945. return TCL.TCL_ERROR;
  946. }
  947. if ( getDbPointer( interp, argv[1].ToString(), out db ) != 0 )
  948. return TCL.TCL_ERROR;
  949. rc = sqlite3_close( db );
  950. TCL.Tcl_SetResult( interp, t1ErrorName( rc ), TCL.TCL_STATIC );
  951. return TCL.TCL_OK;
  952. }
  953. /*
  954. ** Implementation of the x_coalesce() function.
  955. ** Return the first argument non-NULL argument.
  956. */
  957. static void t1_ifnullFunc(
  958. sqlite3_context context,
  959. int argc,
  960. sqlite3_value[] argv
  961. )
  962. {
  963. int i;
  964. for ( i = 0; i < argc; i++ )
  965. {
  966. if ( SQLITE_NULL != sqlite3_value_type( argv[i] ) )
  967. {
  968. int n = sqlite3_value_bytes( argv[i] );
  969. sqlite3_result_text( context, sqlite3_value_text( argv[i] ),
  970. n, SQLITE_TRANSIENT );
  971. break;
  972. }
  973. }
  974. }
  975. /*
  976. ** These are test functions. hex8() interprets its argument as
  977. ** UTF8 and returns a hex encoding. hex16le() interprets its argument
  978. ** as UTF16le and returns a hex encoding.
  979. */
  980. static void hex8Func( sqlite3_context p, int argc, sqlite3_value[] argv )
  981. {
  982. string z;
  983. int i;
  984. StringBuilder zBuf = new StringBuilder( 200 );
  985. z = sqlite3_value_text( argv[0] );
  986. StringBuilder zTemp = new StringBuilder( 200 );
  987. for ( i = 0; i < zBuf.Capacity / 2 - 2 && i < argv[0].n; i++ )
  988. {
  989. sqlite3_snprintf( 4, zTemp, "%02x", z[i] & 0xff );
  990. zBuf.Append( zTemp );
  991. }
  992. //zBuf[i*2] = 0;
  993. sqlite3_result_text( p, zBuf, -1, SQLITE_TRANSIENT );
  994. }
  995. #if !SQLITE_OMIT_UTF16
  996. static void hex16Func(sqlite3_context p, int argc, sqlite3_value[] argv){
  997. Debugger.Break (); //TODO --
  998. // const unsigned short int *z;
  999. // int i;
  1000. // char zBuf[400];
  1001. // z = sqlite3_value_text16(argv[0]);
  1002. // for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){
  1003. // sqlite3_snprintf(&zBuf[i*4], "%04x", z[i]&0xff);
  1004. // }
  1005. // zBuf[i*4] = 0;
  1006. // sqlite3_result_text(p, (char)zBuf, -1, SQLITE_TRANSIENT);
  1007. }
  1008. #endif
  1009. /*
  1010. ** A structure into which to accumulate text.
  1011. */
  1012. struct dstr
  1013. {
  1014. public int nAlloc; /* Space allocated */
  1015. public int nUsed; /* Space used */
  1016. public StringBuilder z; /* The space */
  1017. };
  1018. /*
  1019. ** Append text to a dstr
  1020. */
  1021. static void dstrAppend( dstr p, string z, int divider )
  1022. {
  1023. int n = z.Length;// strlen( z );
  1024. // if( p.nUsed + n + 2 > p.nAlloc ){
  1025. // string zNew;
  1026. p.nAlloc = p.nAlloc * 2 + n + 200;
  1027. p.z.Capacity = p.nAlloc;
  1028. // zNew = sqlite3_realloc(p.z, p.nAlloc);
  1029. // if( zNew==0 ){
  1030. // sqlite3DbFree(db,p.z);
  1031. // memset(p, 0, sizeof(*p));
  1032. // return;
  1033. // }
  1034. // p.z = zNew;
  1035. // }
  1036. // if( divider && p.nUsed>0 ){
  1037. // p.z[p.nUsed++] = divider;
  1038. // }
  1039. // memcpy(p.z[p.nUsed], z, n+1);
  1040. p.nUsed += n;
  1041. p.z.Append( divider + z );
  1042. }
  1043. /*
  1044. ** Invoked for each callback from sqlite3ExecFunc
  1045. */
  1046. static int execFuncCallback( object pData, sqlite3_int64 argc, object _argv, object NotUsed )
  1047. {
  1048. Tcl_Obj[] argv = (Tcl_Obj[])_argv;
  1049. dstr p = (dstr)pData;
  1050. int i;
  1051. for ( i = 0; i < argc; i++ )
  1052. {
  1053. if ( argv[i] == null )
  1054. {
  1055. dstrAppend( p, "NULL", ' ' );
  1056. }
  1057. else
  1058. {
  1059. dstrAppend( p, argv[i].ToString(), ' ' );
  1060. }
  1061. }
  1062. return 0;
  1063. }
  1064. /*
  1065. ** Implementation of the x_sqlite_exec() function. This function takes
  1066. ** a single argument and attempts to execute that argument as SQL code.
  1067. ** This is illegal and should set the SQLITE_MISUSE flag on the database.
  1068. **
  1069. ** 2004-Jan-07: We have changed this to make it legal to call sqlite3_exec()
  1070. ** from within a function call.
  1071. **
  1072. ** This routine simulates the effect of having two threads attempt to
  1073. ** use the same database at the same time.
  1074. */
  1075. static void sqlite3ExecFunc(
  1076. sqlite3_context context,
  1077. int argc,
  1078. sqlite3_value[] argv
  1079. )
  1080. {
  1081. dstr x = new dstr();
  1082. //memset(&x, 0, sizeof(x));
  1083. string sDummy = "";
  1084. sqlite3_exec( (sqlite3)sqlite3_context_db_handle( context ),
  1085. sqlite3_value_text( argv[0] ),
  1086. (dxCallback)execFuncCallback, (object)x, ref sDummy );
  1087. sqlite3_result_text( context, x.z, x.nUsed, SQLITE_TRANSIENT );
  1088. x.z = null;// sqlite3DbFree( db, ref x.z );
  1089. }
  1090. /*
  1091. ** Implementation of tkt2213func(), a scalar function that takes exactly
  1092. ** one argument. It has two interesting features:
  1093. **
  1094. ** * It calls sqlite3_value_text() 3 times on the argument sqlite3_value*.
  1095. ** If the three pointers returned are not the same an SQL error is raised.
  1096. **
  1097. ** * Otherwise it returns a copy of the text representation of its
  1098. ** argument in such a way as the VDBE representation is a Mem* cell
  1099. ** with the MEM_Term flag clear.
  1100. **
  1101. ** Ticket #2213 can therefore be tested by evaluating the following
  1102. ** SQL expression:
  1103. **
  1104. ** tkt2213func(tkt2213func('a string'));
  1105. */
  1106. static void tkt2213Function(
  1107. sqlite3_context context,
  1108. int argc,
  1109. sqlite3_value[] argv
  1110. )
  1111. {
  1112. int nText;
  1113. string zText1;
  1114. string zText2;
  1115. string zText3;
  1116. nText = sqlite3_value_bytes( argv[0] );
  1117. zText1 = sqlite3_value_text( argv[0] );
  1118. zText2 = sqlite3_value_text( argv[0] );
  1119. zText3 = sqlite3_value_text( argv[0] );
  1120. if ( zText1 != zText2 || zText2 != zText3 )
  1121. {
  1122. sqlite3_result_error( context, "tkt2213 is not fixed", -1 );
  1123. }
  1124. else
  1125. {
  1126. //string zCopy = (char )sqlite3Malloc(nText);
  1127. //memcpy(zCopy, zText1, nText);
  1128. sqlite3_result_text( context, zText1, nText, null ); //sqlite3_free );
  1129. }
  1130. }
  1131. /*
  1132. ** The following SQL function takes 4 arguments. The 2nd and
  1133. ** 4th argument must be one of these strings: 'text', 'text16',
  1134. ** or 'blob' corresponding to API functions
  1135. **
  1136. ** sqlite3_value_text()
  1137. ** sqlite3_value_text16()
  1138. ** sqlite3_value_blob()
  1139. **
  1140. ** The third argument is a string, either 'bytes' or 'bytes16' or 'noop',
  1141. ** corresponding to APIs:
  1142. **
  1143. ** sqlite3_value_bytes()
  1144. ** sqlite3_value_bytes16()
  1145. ** noop
  1146. **
  1147. ** The APIs designated by the 2nd through 4th arguments are applied
  1148. ** to the first argument in order. If the pointers returned by the
  1149. ** second and fourth are different, this routine returns 1. Otherwise,
  1150. ** this routine returns 0.
  1151. **
  1152. ** This function is used to test to see when returned pointers from
  1153. ** the _text(), _text16() and _blob() APIs become invalidated.
  1154. */
  1155. static void ptrChngFunction(
  1156. sqlite3_context context,
  1157. int argc,
  1158. sqlite3_value[] argv
  1159. )
  1160. {
  1161. sqlite3_result_int( context, 0 );
  1162. return;
  1163. //Debugger.Break(); //TODO --
  1164. //string p1 = "", p2 = "";
  1165. //string zCmd;
  1166. //if ( argc != 4 )
  1167. // return;
  1168. //zCmd = sqlite3_value_text( argv[1] );
  1169. //if ( zCmd == null )
  1170. // return;
  1171. // if( strcmp(zCmd,"text")==0 ){
  1172. // p1 = (const void)sqlite3_value_text(argv[0]);
  1173. //#if !SQLITE_OMIT_UTF16
  1174. // }else if( strcmp(zCmd, "text16")==0 ){
  1175. // p1 = (const void)sqlite3_value_text16(argv[0]);
  1176. //#endif
  1177. // }else if( strcmp(zCmd, "blob")==0 ){
  1178. // p1 = (const void)sqlite3_value_blob(argv[0]);
  1179. // }else{
  1180. // return;
  1181. // }
  1182. // zCmd = (const char)sqlite3_value_text(argv[2]);
  1183. // if( zCmd==0 ) return;
  1184. // if( strcmp(zCmd,"bytes")==0 ){
  1185. // sqlite3_value_bytes(argv[0]);
  1186. //#if !SQLITE_OMIT_UTF16
  1187. // }else if( strcmp(zCmd, "bytes16")==0 ){
  1188. // sqlite3_value_bytes16(argv[0]);
  1189. //#endif
  1190. // }else if( strcmp(zCmd, "noop")==0 ){
  1191. // /* do nothing */
  1192. // }else{
  1193. // return;
  1194. // }
  1195. // zCmd = (const char)sqlite3_value_text(argv[3]);
  1196. // if( zCmd==0 ) return;
  1197. // if( strcmp(zCmd,"text")==0 ){
  1198. // p2 = (const void)sqlite3_value_text(argv[0]);
  1199. //#if !SQLITE_OMIT_UTF16
  1200. // }else if( strcmp(zCmd, "text16")==0 ){
  1201. // p2 = (const void)sqlite3_value_text16(argv[0]);
  1202. //#endif
  1203. // }else if( strcmp(zCmd, "blob")==0 ){
  1204. // p2 = (const void)sqlite3_value_blob(argv[0]);
  1205. // }else{
  1206. // return;
  1207. // }
  1208. //sqlite3_result_int( context, p1 != p2 ? 1 : 0 );
  1209. }
  1210. /*
  1211. ** Usage: sqlite_test_create_function DB
  1212. **
  1213. ** Call the sqlite3_create_function API on the given database in order
  1214. ** to create a function named "x_coalesce". This function does the same thing
  1215. ** as the "coalesce" function. This function also registers an SQL function
  1216. ** named "x_sqlite_exec" that invokes sqlite3_exec(). Invoking sqlite3_exec()
  1217. ** in this way is illegal recursion and should raise an SQLITE_MISUSE error.
  1218. ** The effect is similar to trying to use the same database connection from
  1219. ** two threads at the same time.
  1220. **
  1221. ** The original motivation for this routine was to be able to call the
  1222. ** sqlite3_create_function function while a query is in progress in order
  1223. ** to test the SQLITE_MISUSE detection logic.
  1224. */
  1225. static int test_create_function(
  1226. object NotUsed,
  1227. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  1228. int argc, /* Number of arguments */
  1229. Tcl_Obj[] argv /* Text of each argument */
  1230. )
  1231. {
  1232. int rc;
  1233. sqlite3 db = null;
  1234. if ( argc != 2 )
  1235. {
  1236. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  1237. " DB\"" );
  1238. return TCL.TCL_ERROR;
  1239. }
  1240. if ( getDbPointer( interp, argv[1].ToString(), out db ) != 0 )
  1241. return TCL.TCL_ERROR;
  1242. rc = sqlite3_create_function( db, "x_coalesce", -1, SQLITE_ANY, 0,
  1243. t1_ifnullFunc, null, null );
  1244. if ( rc == SQLITE_OK )
  1245. {
  1246. rc = sqlite3_create_function( db, "hex8", 1, SQLITE_ANY, 0,
  1247. hex8Func, null, null );
  1248. }
  1249. #if !SQLITE_OMIT_UTF16
  1250. if( rc==SQLITE_OK ){
  1251. rc = sqlite3_create_function(db, "hex16", 1, SQLITE_ANY, null, hex16Func, null,null);
  1252. }
  1253. #endif
  1254. if ( rc == SQLITE_OK )
  1255. {
  1256. rc = sqlite3_create_function( db, "tkt2213func", 1, SQLITE_ANY, 0,
  1257. tkt2213Function, null, null );
  1258. }
  1259. if ( rc == SQLITE_OK )
  1260. {
  1261. rc = sqlite3_create_function( db, "pointer_change", 4, SQLITE_ANY, 0,
  1262. ptrChngFunction, null, null );
  1263. }
  1264. #if !SQLITE_OMIT_UTF16
  1265. /* Use the sqlite3_create_function16() API here. Mainly for fun, but also
  1266. ** because it is not tested anywhere else. */
  1267. if( rc==SQLITE_OK ){
  1268. string zUtf16;
  1269. sqlite3_value pVal;
  1270. sqlite3_mutex_enter(db.mutex);
  1271. pVal = sqlite3ValueNew(db);
  1272. sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC);
  1273. zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
  1274. if( db.mallocFailed !=0{
  1275. rc = SQLITE_NOMEM;
  1276. }else{
  1277. rc = sqlite3_create_function16(db, zUtf16, 1, SQLITE_UTF16, db, sqlite3ExecFunc,null,null );
  1278. }
  1279. sqlite3ValueFree(ref pVal);
  1280. sqlite3_mutex_leave(db.mutex);
  1281. }
  1282. #endif
  1283. if ( sqlite3TestErrCode( interp, db, rc ) != 0 )
  1284. return TCL.TCL_ERROR;
  1285. TCL.Tcl_SetResult( interp, sqlite3TestErrorName( rc ), 0 );
  1286. return TCL.TCL_OK;
  1287. }
  1288. /*
  1289. ** Routines to implement the x_count() aggregate function.
  1290. **
  1291. ** x_count() counts the number of non-null arguments. But there are
  1292. ** some twists for testing purposes.
  1293. **
  1294. ** If the argument to x_count() is 40 then a UTF-8 error is reported
  1295. ** on the step function. If x_count(41) is seen, then a UTF-16 error
  1296. ** is reported on the step function. If the total count is 42, then
  1297. ** a UTF-8 error is reported on the finalize function.
  1298. */
  1299. //typedef struct t1CountCtx t1CountCtx;
  1300. static void t1CountStep(
  1301. sqlite3_context context,
  1302. int argc,
  1303. sqlite3_value[] argv
  1304. )
  1305. {
  1306. SumCtx p;
  1307. Mem pMem = sqlite3_aggregate_context( context, 1 );//sizeof(*p));
  1308. if ( pMem._SumCtx == null )
  1309. pMem._SumCtx = new SumCtx();
  1310. p = pMem._SumCtx;
  1311. if ( p.Context == null )
  1312. p.Context = pMem;
  1313. if ( ( argc == 0 || SQLITE_NULL != sqlite3_value_type( argv[0] ) ) && p != null )
  1314. {
  1315. p.cnt++;
  1316. }
  1317. if ( argc > 0 )
  1318. {
  1319. int v = sqlite3_value_int( argv[0] );
  1320. if ( v == 40 )
  1321. {
  1322. sqlite3_result_error( context, "value of 40 handed to x_count", -1 );
  1323. #if !SQLITE_OMIT_UTF16
  1324. }else if( v==41 ){
  1325. Debugger.Break (); // TODO --
  1326. //const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0};
  1327. //sqlite3_result_error16(context, zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1);
  1328. #endif
  1329. }
  1330. }
  1331. }
  1332. static void t1CountFinalize( sqlite3_context context )
  1333. {
  1334. SumCtx p;
  1335. Mem pMem = sqlite3_aggregate_context( context, 0 );//sizeof(*p));
  1336. p = pMem._SumCtx;
  1337. if ( p != null )
  1338. {
  1339. if ( p.cnt == 42 )
  1340. {
  1341. sqlite3_result_error( context, "x_count totals to 42", -1 );
  1342. }
  1343. else
  1344. {
  1345. sqlite3_result_int( context, p != null ? (int)p.cnt : 0 );
  1346. }
  1347. }
  1348. }
  1349. #if !SQLITE_OMIT_DEPRECATED
  1350. static void legacyCountStep(
  1351. sqlite3_context context,
  1352. int argc,
  1353. sqlite3_value[] argv
  1354. )
  1355. {
  1356. /* no-op */
  1357. }
  1358. static void legacyCountFinalize( sqlite3_context context )
  1359. {
  1360. sqlite3_result_int( context, sqlite3_aggregate_count( context ) );
  1361. }
  1362. #endif
  1363. /*
  1364. ** Usage: sqlite3_create_aggregate DB
  1365. **
  1366. ** Call the sqlite3_create_function API on the given database in order
  1367. ** to create a function named "x_count". This function is similar
  1368. ** to the built-in count() function, with a few special quirks
  1369. ** for testing the sqlite3_result_error() APIs.
  1370. **
  1371. ** The original motivation for this routine was to be able to call the
  1372. ** sqlite3_create_aggregate function while a query is in progress in order
  1373. ** to test the SQLITE_MISUSE detection logic. See misuse.test.
  1374. **
  1375. ** This routine was later extended to test the use of sqlite3_result_error()
  1376. ** within aggregate functions.
  1377. **
  1378. ** Later: It is now also extended to register the aggregate function
  1379. ** "legacy_count()" with the supplied database handle. This is used
  1380. ** to test the deprecated sqlite3_aggregate_count() API.
  1381. */
  1382. static int test_create_aggregate(
  1383. object NotUsed,
  1384. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  1385. int argc, /* Number of arguments */
  1386. Tcl_Obj[] argv /* Text of each argument */
  1387. )
  1388. {
  1389. sqlite3 db = new sqlite3();
  1390. int rc;
  1391. if ( argc != 2 )
  1392. {
  1393. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0].ToString(),
  1394. " FILENAME\"" );
  1395. return TCL.TCL_ERROR;
  1396. }
  1397. if ( getDbPointer( interp, argv[1].ToString(), out db ) != 0 )
  1398. return TCL.TCL_ERROR;
  1399. rc = sqlite3_create_function( db, "x_count", 0, SQLITE_UTF8, 0, null,
  1400. t1CountStep, t1CountFinalize );
  1401. if ( rc == SQLITE_OK )
  1402. {
  1403. rc = sqlite3_create_function( db, "x_count", 1, SQLITE_UTF8, 0, null,
  1404. t1CountStep, t1CountFinalize );
  1405. }
  1406. #if !SQLITE_OMIT_DEPRECATED
  1407. if ( rc == SQLITE_OK )
  1408. {
  1409. rc = sqlite3_create_function( db, "legacy_count", 0, SQLITE_ANY, 0, null,
  1410. legacyCountStep, legacyCountFinalize
  1411. );
  1412. }
  1413. #endif
  1414. if ( sqlite3TestErrCode( interp, db, rc ) != 0 )
  1415. return TCL.TCL_ERROR;
  1416. TCL.Tcl_SetResult( interp, t1ErrorName( rc ), 0 );
  1417. return TCL.TCL_OK;
  1418. }
  1419. /*
  1420. ** Usage: printf TEXT
  1421. **
  1422. ** Send output to printf. Use this rather than puts to merge the output
  1423. ** in the correct sequence with debugging printfs inserted into C code.
  1424. ** Puts uses a separate buffer and debugging statements will be out of
  1425. ** sequence if it is used.
  1426. */
  1427. //static int test_printf(
  1428. // object NotUsed,
  1429. // Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  1430. // int argc, /* Number of arguments */
  1431. // Tcl_Obj[] argv /* Text of each argument */
  1432. //){
  1433. // if( argc!=2 ){
  1434. // TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1435. // " TEXT\"");
  1436. // return TCL.TCL_ERROR;
  1437. // }
  1438. // printf("%s\n", argv[1]);
  1439. // return TCL.TCL_OK;
  1440. //}
  1441. /*
  1442. ** Usage: sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER
  1443. **
  1444. ** Call mprintf with three integer arguments
  1445. */
  1446. static int sqlite3_mprintf_int(
  1447. object NotUsed,
  1448. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  1449. int argc, /* Number of arguments */
  1450. Tcl_Obj[] argv /* Text of each argument */
  1451. )
  1452. {
  1453. long[] a = new long[3];
  1454. int i;
  1455. string z;
  1456. if ( argc != 5 )
  1457. {
  1458. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  1459. " FORMAT INT INT INT\"" );
  1460. return TCL.TCL_ERROR;
  1461. }
  1462. for ( i = 2; i < 5; i++ )
  1463. {
  1464. if ( TCL.Tcl_GetLong( interp, argv[i], out a[i - 2] ) )
  1465. return TCL.TCL_ERROR;
  1466. }
  1467. z = sqlite3_mprintf( argv[1].ToString(), a[0], a[1], a[2] );
  1468. TCL.Tcl_AppendResult( interp, z );
  1469. //sqlite3DbFree(db,z);
  1470. return TCL.TCL_OK;
  1471. }
  1472. /*
  1473. ** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER
  1474. **
  1475. ** Call mprintf with three 64-bit integer arguments
  1476. */
  1477. static int sqlite3_mprintf_int64(
  1478. object NotUsed,
  1479. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  1480. int argc, /* Number of arguments */
  1481. Tcl_Obj[] argv /* Text of each argument */
  1482. )
  1483. {
  1484. int i;
  1485. sqlite3_int64[] a = new sqlite3_int64[3];
  1486. string z;
  1487. if ( argc != 5 )
  1488. {
  1489. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  1490. " FORMAT INT INT INT\"" );
  1491. return TCL.TCL_ERROR;
  1492. }
  1493. for ( i = 2; i < 5; i++ )
  1494. {
  1495. if ( sqlite3Atoi64( argv[i].ToString(), ref a[i - 2], argv[i].ToString().Length, SQLITE_UTF8 ) != 0 )
  1496. {
  1497. TCL.Tcl_AppendResult( interp, "argument is not a valid 64-bit integer" );
  1498. return TCL.TCL_ERROR;
  1499. }
  1500. }
  1501. z = sqlite3_mprintf( argv[1].ToString(), a[0], a[1], a[2] );
  1502. TCL.Tcl_AppendResult( interp, z );
  1503. //sqlite3DbFree(db,z);
  1504. return TCL.TCL_OK;
  1505. }
  1506. /*
  1507. ** Usage: sqlite3_mprintf_long FORMAT INTEGER INTEGER INTEGER
  1508. **
  1509. ** Call mprintf with three long integer arguments. This might be the
  1510. ** same as sqlite3_mprintf_int or sqlite3_mprintf_int64, depending on
  1511. ** platform.
  1512. */
  1513. static int sqlite3_mprintf_long(
  1514. object NotUsed,
  1515. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  1516. int argc, /* Number of arguments */
  1517. Tcl_Obj[] argv /* Text of each argument */
  1518. )
  1519. {
  1520. int i;
  1521. long[] a = new long[3];
  1522. long[] b = new long[3];
  1523. string z;
  1524. if ( argc != 5 )
  1525. {
  1526. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  1527. " FORMAT INT INT INT\"" );
  1528. return TCL.TCL_ERROR;
  1529. }
  1530. for ( i = 2; i < 5; i++ )
  1531. {
  1532. if ( TCL.Tcl_GetLong( interp, argv[i], out b[i - 2] ) )
  1533. return TCL.TCL_ERROR;
  1534. a[i - 2] = b[i - 2];
  1535. //a[i-2] &= (((u64)1)<<(sizeof(int)*8))-1;
  1536. }
  1537. z = sqlite3_mprintf( argv[1].ToString(), a[0], a[1], a[2] );
  1538. TCL.Tcl_AppendResult( interp, z );
  1539. ////sqlite3_free(z);
  1540. return TCL.TCL_OK;
  1541. }
  1542. /*
  1543. ** Usage: sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING
  1544. **
  1545. ** Call mprintf with two integer arguments and one string argument
  1546. */
  1547. static int sqlite3_mprintf_str(
  1548. object NotUsed,
  1549. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  1550. int argc, /* Number of arguments */
  1551. Tcl_Obj[] argv /* Text of each argument */
  1552. )
  1553. {
  1554. long[] a = new long[3];
  1555. int i;
  1556. string z;
  1557. if ( argc < 4 || argc > 5 )
  1558. {
  1559. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  1560. " FORMAT INT INT ?STRING?\"" );
  1561. return TCL.TCL_ERROR;
  1562. }
  1563. for ( i = 2; i < 4; i++ )
  1564. {
  1565. if ( TCL.Tcl_GetLong( interp, argv[i], out a[i - 2] ) )
  1566. return TCL.TCL_ERROR;
  1567. }
  1568. z = sqlite3_mprintf( argv[1].ToString(), a[0], a[1], argc > 4 ? argv[4].ToString() : null );
  1569. TCL.Tcl_AppendResult( interp, z );
  1570. //sqlite3DbFree(db,z);
  1571. return TCL.TCL_OK;
  1572. }
  1573. /*
  1574. ** Usage: sqlite3_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING
  1575. **
  1576. ** Call mprintf with two integer arguments and one string argument
  1577. */
  1578. static int sqlite3_snprintf_str(
  1579. object NotUsed,
  1580. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  1581. int argc, /* Number of arguments */
  1582. Tcl_Obj[] argv /* Text of each argument */
  1583. )
  1584. {
  1585. long[] a = new long[3];
  1586. int i;
  1587. int n = 0;
  1588. StringBuilder z;
  1589. if ( argc < 5 || argc > 6 )
  1590. {
  1591. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  1592. " INT FORMAT INT INT ?STRING?\"" );
  1593. return TCL.TCL_ERROR;
  1594. }
  1595. if ( TCL.Tcl_GetInt( interp, argv[1], out n ) )
  1596. return TCL.TCL_ERROR;
  1597. if ( n < 0 )
  1598. {
  1599. TCL.Tcl_AppendResult( interp, "N must be non-negative" );
  1600. return TCL.TCL_ERROR;
  1601. }
  1602. for ( i = 3; i < 5; i++ )
  1603. {
  1604. if ( TCL.Tcl_GetLong( interp, argv[i], out a[i - 3] ) )
  1605. return TCL.TCL_ERROR;
  1606. }
  1607. z = new StringBuilder( n + 1 );//sqlite3Malloc( n+1 );
  1608. sqlite3_snprintf( n, z, argv[2].ToString(), a[0], a[1], argc > 4 ? argv[5].ToString() : null );
  1609. TCL.Tcl_AppendResult( interp, z );
  1610. //sqlite3DbFree(db,z);
  1611. return TCL.TCL_OK;
  1612. }
  1613. /*
  1614. ** Usage: sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE
  1615. **
  1616. ** Call mprintf with two integer arguments and one double argument
  1617. */
  1618. static int sqlite3_mprintf_double(
  1619. object NotUsed,
  1620. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  1621. int argc, /* Number of arguments */
  1622. Tcl_Obj[] argv /* Text of each argument */
  1623. )
  1624. {
  1625. long[] a = new long[3];
  1626. int i;
  1627. double r = 0;
  1628. string z;
  1629. if ( argc != 5 )
  1630. {
  1631. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  1632. " FORMAT INT INT DOUBLE\"" );
  1633. return TCL.TCL_ERROR;
  1634. }
  1635. for ( i = 2; i < 4; i++ )
  1636. {
  1637. if ( TCL.Tcl_GetLong( interp, argv[i], out a[i - 2] ) )
  1638. return TCL.TCL_ERROR;
  1639. }
  1640. if ( TCL.Tcl_GetDouble( interp, argv[4], out r ) )
  1641. return TCL.TCL_ERROR;
  1642. z = sqlite3_mprintf( argv[1].ToString(), a[0], a[1], r );
  1643. TCL.Tcl_AppendResult( interp, z );
  1644. //sqlite3DbFree(db,z);
  1645. return TCL.TCL_OK;
  1646. }
  1647. /*
  1648. ** Usage: sqlite3_mprintf_scaled FORMAT DOUBLE DOUBLE
  1649. **
  1650. ** Call mprintf with a single double argument which is the product of the
  1651. ** two arguments given above. This is used to generate overflow and underflow
  1652. ** doubles to test that they are converted properly.
  1653. */
  1654. static int sqlite3_mprintf_scaled(
  1655. object NotUsed,
  1656. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  1657. int argc, /* Number of arguments */
  1658. Tcl_Obj[] argv /* Text of each argument */
  1659. )
  1660. {
  1661. int i;
  1662. double[] r = new double[2];
  1663. string z;
  1664. if ( argc != 4 )
  1665. {
  1666. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  1667. " FORMAT DOUBLE DOUBLE\"" );
  1668. return TCL.TCL_ERROR;
  1669. }
  1670. for ( i = 2; i < 4; i++ )
  1671. {
  1672. if ( TCL.Tcl_GetDouble( interp, argv[i], out r[i - 2] ) )
  1673. return TCL.TCL_ERROR;
  1674. }
  1675. z = sqlite3_mprintf( argv[1].ToString(), r[0] * r[1] );
  1676. TCL.Tcl_AppendResult( interp, z );
  1677. //sqlite3DbFree(db,z);
  1678. return TCL.TCL_OK;
  1679. }
  1680. /*
  1681. ** Usage: sqlite3_mprintf_stronly FORMAT STRING
  1682. **
  1683. ** Call mprintf with a single double argument which is the product of the
  1684. ** two arguments given above. This is used to generate overflow and underflow
  1685. ** doubles to test that they are converted properly.
  1686. */
  1687. static int sqlite3_mprintf_stronly(
  1688. object NotUsed,
  1689. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  1690. int argc, /* Number of arguments */
  1691. Tcl_Obj[] argv /* Text of each argument */
  1692. )
  1693. {
  1694. string z;
  1695. if ( argc != 3 )
  1696. {
  1697. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  1698. " FORMAT STRING\"" );
  1699. return TCL.TCL_ERROR;
  1700. }
  1701. z = sqlite3_mprintf( argv[1].ToString(), argv[2].ToString() );
  1702. TCL.Tcl_AppendResult( interp, z );
  1703. //sqlite3DbFree( db, z );
  1704. return TCL.TCL_OK;
  1705. }
  1706. /*
  1707. ** Usage: sqlite3_mprintf_hexdouble FORMAT HEX
  1708. **
  1709. ** Call mprintf with a single double argument which is derived from the
  1710. ** hexadecimal encoding of an IEEE double.
  1711. */
  1712. static int sqlite3_mprintf_hexdouble(
  1713. object NotUsed,
  1714. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  1715. int argc, /* Number of arguments */
  1716. Tcl_Obj[] argv /* Text of each argument */
  1717. )
  1718. {
  1719. string z;
  1720. double r;
  1721. u32 x1, x2;
  1722. i64 d = 0;
  1723. if ( argc != 3 )
  1724. {
  1725. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  1726. " FORMAT STRING\"" );
  1727. return TCL.TCL_ERROR;
  1728. }
  1729. //if( sscanf(argv[2].ToString(), "%08x%08x", ref x2, ref x1)!=2 ){
  1730. if ( argv[2].ToString().Length != 16
  1731. || !u32.TryParse( argv[2].ToString().Substring( 0, 8 ), System.Globalization.NumberStyles.HexNumber, null, out x2 )
  1732. || !u32.TryParse( argv[2].ToString().Substring( 8, 8 ), System.Globalization.NumberStyles.HexNumber, null, out x1 )
  1733. )
  1734. {
  1735. TCL.Tcl_AppendResult( interp, "2nd argument should be 16-characters of hex" );
  1736. return TCL.TCL_ERROR;
  1737. }
  1738. d = x2;
  1739. d = ( d << 32 ) + x1;
  1740. #if WINDOWS_PHONE
  1741. r = BitConverter.ToDouble(BitConverter.GetBytes((long)d), 0);
  1742. #else
  1743. r = BitConverter.Int64BitsToDouble( d );// memcpy( &r, d, sizeof( r ) );
  1744. #endif
  1745. z = sqlite3_mprintf( argv[1].ToString(), r );
  1746. TCL.Tcl_AppendResult( interp, z );
  1747. //sqlite3DbFree(db,z);
  1748. return TCL.TCL_OK;
  1749. }
  1750. /*
  1751. ** Usage: sqlite3_enable_shared_cache ?BOOLEAN?
  1752. **
  1753. */
  1754. #if !SQLITE_OMIT_SHARED_CACHE
  1755. static int test_enable_shared(
  1756. object clientdata, /* Pointer to sqlite3_enable_XXX function */
  1757. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  1758. int objc, /* Number of arguments */
  1759. Tcl_Obj[] objv /* Command arguments */
  1760. )
  1761. {
  1762. int rc;
  1763. bool enable = false;
  1764. int ret = 0;
  1765. if ( objc != 2 && objc != 1 )
  1766. {
  1767. TCL.Tcl_WrongNumArgs( interp, 1, objv, "?BOOLEAN?" );
  1768. return TCL.TCL_ERROR;
  1769. }
  1770. ret = sqlite3GlobalConfig.sharedCacheEnabled ? 1 : 0;
  1771. if ( objc == 2 )
  1772. {
  1773. if ( TCL.Tcl_GetBooleanFromObj( interp, objv[1], out enable ) )
  1774. {
  1775. return TCL.TCL_ERROR;
  1776. }
  1777. rc = sqlite3_enable_shared_cache( enable );
  1778. if ( rc != SQLITE_OK )
  1779. {
  1780. TCL.Tcl_SetResult( interp, sqlite3ErrStr( rc ), TCL.TCL_STATIC );
  1781. return TCL.TCL_ERROR;
  1782. }
  1783. }
  1784. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewBooleanObj( ret ) );
  1785. return TCL.TCL_OK;
  1786. }
  1787. #endif
  1788. /*
  1789. ** Usage: sqlite3_extended_result_codes DB BOOLEAN
  1790. **
  1791. */
  1792. static int test_extended_result_codes(
  1793. object clientdata, /* Pointer to sqlite3_enable_XXX function */
  1794. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  1795. int objc, /* Number of arguments */
  1796. Tcl_Obj[] objv /* Command arguments */
  1797. )
  1798. {
  1799. bool enable = false;
  1800. sqlite3 db = null;
  1801. if ( objc != 3 )
  1802. {
  1803. TCL.Tcl_WrongNumArgs( interp, 1, objv, "DB BOOLEAN" );
  1804. return TCL.TCL_ERROR;
  1805. }
  1806. if ( getDbPointer( interp, objv[1].ToString(), out db ) != 0 )
  1807. return TCL.TCL_ERROR;
  1808. if ( TCL.Tcl_GetBooleanFromObj( interp, objv[2], out enable ) )
  1809. return TCL.TCL_ERROR;
  1810. sqlite3_extended_result_codes( db, enable );
  1811. return TCL.TCL_OK;
  1812. }
  1813. /*
  1814. ** Usage: sqlite3_libversion_number
  1815. **
  1816. */
  1817. static int test_libversion_number(
  1818. object clientdata, /* Pointer to sqlite3_enable_XXX function */
  1819. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  1820. int objc, /* Number of arguments */
  1821. Tcl_Obj[] objv /* Command arguments */
  1822. )
  1823. {
  1824. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewIntObj( sqlite3_libversion_number() ) );
  1825. return TCL.TCL_OK;
  1826. }
  1827. /*
  1828. ** Usage: sqlite3_table_column_metadata DB dbname tblname colname
  1829. **
  1830. */
  1831. #if SQLITE_ENABLE_COLUMN_METADATA
  1832. static int test_table_column_metadata(
  1833. object clientdata, /* Pointer to sqlite3_enable_XXX function */
  1834. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  1835. int objc, /* Number of arguments */
  1836. Tcl_Obj[] objv /* Command arguments */
  1837. )
  1838. {
  1839. sqlite3 db = null;
  1840. string zDb = null;
  1841. string zTbl = null;
  1842. string zCol = null;
  1843. int rc;
  1844. Tcl_Obj pRet;
  1845. string zDatatype = null;
  1846. string zCollseq = null;
  1847. int notnull = 0;
  1848. int primarykey = 0;
  1849. int autoincrement = 0;
  1850. if ( objc != 5 )
  1851. {
  1852. TCL.Tcl_WrongNumArgs( interp, 1, objv, "DB dbname tblname colname" );
  1853. return TCL.TCL_ERROR;
  1854. }
  1855. if ( getDbPointer( interp, TCL.Tcl_GetString( objv[1] ), out db ) != 0 )
  1856. return TCL.TCL_ERROR;
  1857. zDb = TCL.Tcl_GetString( objv[2] );
  1858. zTbl = TCL.Tcl_GetString( objv[3] );
  1859. zCol = TCL.Tcl_GetString( objv[4] );
  1860. if ( zDb.Length == 0 )
  1861. zDb = null;
  1862. rc = sqlite3_table_column_metadata( db, zDb, zTbl, zCol,
  1863. ref zDatatype, ref zCollseq, ref notnull, ref primarykey, ref autoincrement );
  1864. if ( rc != SQLITE_OK )
  1865. {
  1866. TCL.Tcl_AppendResult( interp, sqlite3_errmsg( db ) );
  1867. return TCL.TCL_ERROR;
  1868. }
  1869. pRet = TCL.Tcl_NewObj();
  1870. TCL.Tcl_ListObjAppendElement( null, pRet, TCL.Tcl_NewStringObj( zDatatype, -1 ) );
  1871. TCL.Tcl_ListObjAppendElement( null, pRet, TCL.Tcl_NewStringObj( zCollseq, -1 ) );
  1872. TCL.Tcl_ListObjAppendElement( null, pRet, TCL.Tcl_NewIntObj( notnull ) );
  1873. TCL.Tcl_ListObjAppendElement( null, pRet, TCL.Tcl_NewIntObj( primarykey ) );
  1874. TCL.Tcl_ListObjAppendElement( null, pRet, TCL.Tcl_NewIntObj( autoincrement ) );
  1875. TCL.Tcl_SetObjResult( interp, pRet );
  1876. return TCL.TCL_OK;
  1877. }
  1878. #endif
  1879. #if !SQLITE_OMIT_INCRBLOB
  1880. static int blobHandleFromObj(
  1881. Tcl_Interp interp,
  1882. Tcl_Obj *pObj,
  1883. sqlite3_blob **ppBlob
  1884. ){
  1885. string z;
  1886. int n;
  1887. z = TCL.Tcl_GetStringFromObj(pObj, &n);
  1888. if( n==0 ){
  1889. *ppBlob = 0;
  1890. }else{
  1891. int notUsed;
  1892. TCL.Tcl_Channel channel;
  1893. ClientData instanceData;
  1894. channel = TCL.Tcl_GetChannel(interp, z, &notUsed);
  1895. if( null==channel ) return TCL.TCL_ERROR;
  1896. TCL.Tcl_Flush(channel);
  1897. TCL.Tcl_Seek(channel, 0, SEEK_SET);
  1898. instanceData = TCL.Tcl_GetChannelInstanceData(channel);
  1899. *ppBlob = *((sqlite3_blob *)instanceData);
  1900. }
  1901. return TCL.TCL_OK;
  1902. }
  1903. /*
  1904. ** sqlite3_blob_bytes CHANNEL
  1905. */
  1906. static int test_blob_bytes(
  1907. ClientData clientData, /* Not used */
  1908. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  1909. int objc, /* Number of arguments */
  1910. Tcl_Obj[] objv /* Command arguments */
  1911. ){
  1912. sqlite3_blob *pBlob;
  1913. int nByte;
  1914. if( objc!=2 ){
  1915. TCL.Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL");
  1916. return TCL.TCL_ERROR;
  1917. }
  1918. if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL.TCL_ERROR;
  1919. nByte = sqlite3_blob_bytes(pBlob);
  1920. TCL.Tcl_SetObjResult(interp, TCL.Tcl_NewIntObj(nByte));
  1921. return TCL.TCL_OK;
  1922. }
  1923. /*
  1924. ** sqlite3_blob_close CHANNEL
  1925. */
  1926. static int test_blob_close(
  1927. ClientData clientData, /* Not used */
  1928. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  1929. int objc, /* Number of arguments */
  1930. Tcl_Obj[] objv /* Command arguments */
  1931. ){
  1932. sqlite3_blob *pBlob;
  1933. if( objc!=2 ){
  1934. TCL.Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL");
  1935. return TCL.TCL_ERROR;
  1936. }
  1937. if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL.TCL_ERROR;
  1938. sqlite3_blob_close(pBlob);
  1939. return TCL.TCL_OK;
  1940. }
  1941. /*
  1942. ** sqlite3_blob_read CHANNEL OFFSET N
  1943. **
  1944. ** This command is used to test the sqlite3_blob_read() in ways that
  1945. ** the Tcl channel interface does not. The first argument should
  1946. ** be the name of a valid channel created by the [incrblob] method
  1947. ** of a database handle. This function calls sqlite3_blob_read()
  1948. ** to read N bytes from offset OFFSET from the underlying SQLite
  1949. ** blob handle.
  1950. **
  1951. ** On success, a byte-array object containing the read data is
  1952. ** returned. On failure, the interpreter result is set to the
  1953. ** text representation of the returned error code (i.e. "SQLITE_NOMEM")
  1954. ** and a Tcl exception is thrown.
  1955. */
  1956. static int test_blob_read(
  1957. ClientData clientData, /* Not used */
  1958. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  1959. int objc, /* Number of arguments */
  1960. Tcl_Obj[] objv /* Command arguments */
  1961. ){
  1962. sqlite3_blob *pBlob;
  1963. int nByte;
  1964. int iOffset;
  1965. unsigned string zBuf = 0;
  1966. int rc;
  1967. if( objc!=4 ){
  1968. TCL.Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET N");
  1969. return TCL.TCL_ERROR;
  1970. }
  1971. if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL.TCL_ERROR;
  1972. if( TCL_OK!=TCL.Tcl_GetIntFromObj(interp, objv[2], &iOffset)
  1973. || TCL_OK!=TCL.Tcl_GetIntFromObj(interp, objv[3], &nByte)
  1974. ){
  1975. return TCL.TCL_ERROR;
  1976. }
  1977. if( nByte>0 ){
  1978. zBuf = (unsigned char )TCL.Tcl_Alloc(nByte);
  1979. }
  1980. rc = sqlite3_blob_read(pBlob, zBuf, nByte, iOffset);
  1981. if( rc==SQLITE_OK ){
  1982. TCL.Tcl_SetObjResult(interp, TCL.Tcl_NewByteArrayObj(zBuf, nByte));
  1983. }else{
  1984. TCL.Tcl_SetResult(interp, (char )sqlite3TestErrorName(rc), TCL.TCL_VOLATILE);
  1985. }
  1986. TCL.Tcl_Free((char )zBuf);
  1987. return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
  1988. }
  1989. /*
  1990. ** sqlite3_blob_write CHANNEL OFFSET DATA ?NDATA?
  1991. **
  1992. ** This command is used to test the sqlite3_blob_write() in ways that
  1993. ** the Tcl channel interface does not. The first argument should
  1994. ** be the name of a valid channel created by the [incrblob] method
  1995. ** of a database handle. This function calls sqlite3_blob_write()
  1996. ** to write the DATA byte-array to the underlying SQLite blob handle.
  1997. ** at offset OFFSET.
  1998. **
  1999. ** On success, an empty string is returned. On failure, the interpreter
  2000. ** result is set to the text representation of the returned error code
  2001. ** (i.e. "SQLITE_NOMEM") and a Tcl exception is thrown.
  2002. */
  2003. static int test_blob_write(
  2004. ClientData clientData, /* Not used */
  2005. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  2006. int objc, /* Number of arguments */
  2007. Tcl_Obj[] objv /* Command arguments */
  2008. ){
  2009. sqlite3_blob *pBlob;
  2010. int iOffset;
  2011. int rc;
  2012. unsigned string zBuf;
  2013. int nBuf;
  2014. if( objc!=4 && objc!=5 ){
  2015. TCL.Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET DATA ?NDATA?");
  2016. return TCL.TCL_ERROR;
  2017. }
  2018. if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL.TCL_ERROR;
  2019. if( TCL_OK!=TCL.Tcl_GetIntFromObj(interp, objv[2], &iOffset) ){
  2020. return TCL.TCL_ERROR;
  2021. }
  2022. zBuf = TCL.Tcl_GetByteArrayFromObj(objv[3], &nBuf);
  2023. if( objc==5 && TCL.Tcl_GetIntFromObj(interp, objv[4], &nBuf) ){
  2024. return TCL.TCL_ERROR;
  2025. }
  2026. rc = sqlite3_blob_write(pBlob, zBuf, nBuf, iOffset);
  2027. if( rc!=SQLITE_OK ){
  2028. TCL.Tcl_SetResult(interp, (char )sqlite3TestErrorName(rc), TCL.TCL_VOLATILE);
  2029. }
  2030. return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
  2031. }
  2032. static int test_blob_reopen(
  2033. ClientData clientData, /* Not used */
  2034. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  2035. int objc, /* Number of arguments */
  2036. Tcl_Obj[] objv /* Command arguments */
  2037. ){
  2038. TCL.Tcl_WideInt iRowid;
  2039. sqlite3_blob *pBlob;
  2040. int rc;
  2041. if( objc!=3 ){
  2042. TCL.Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL ROWID");
  2043. return TCL.TCL_ERROR;
  2044. }
  2045. if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL.TCL_ERROR;
  2046. if( TCL.Tcl_GetWideIntFromObj(interp, objv[2], &iRowid) ) return TCL.TCL_ERROR;
  2047. rc = sqlite3_blob_reopen(pBlob, iRowid);
  2048. if( rc!=SQLITE_OK ){
  2049. TCL.Tcl_SetResult(interp, (char )sqlite3TestErrorName(rc), TCL.TCL_VOLATILE);
  2050. }
  2051. return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
  2052. }
  2053. #endif
  2054. /*
  2055. ** Usage: sqlite3_create_collation_v2 DB-HANDLE NAME CMP-PROC DEL-PROC
  2056. **
  2057. ** This Tcl proc is used for testing the experimental
  2058. ** sqlite3_create_collation_v2() interface.
  2059. */
  2060. public struct TestCollationX
  2061. {
  2062. public Tcl_Interp interp;
  2063. public Tcl_Obj pCmp;
  2064. public Tcl_Obj pDel;
  2065. };
  2066. //typedef struct TestCollationX TestCollationX;
  2067. static void testCreateCollationDel( ref object pCtx )
  2068. {
  2069. TestCollationX p = (TestCollationX)pCtx;
  2070. int rc = TCL.Tcl_EvalObjEx( p.interp, p.pDel, TCL.TCL_EVAL_DIRECT | TCL.TCL_EVAL_GLOBAL );
  2071. if ( rc != TCL.TCL_OK )
  2072. {
  2073. TCL.Tcl_BackgroundError( p.interp );
  2074. }
  2075. TCL.Tcl_DecrRefCount( ref p.pCmp );
  2076. TCL.Tcl_DecrRefCount( ref p.pDel );
  2077. //sqlite3Free(ref p);
  2078. }
  2079. static int testCreateCollationCmp(
  2080. object pCtx,
  2081. int nLeft,
  2082. string zLeft,
  2083. int nRight,
  2084. string zRight
  2085. )
  2086. {
  2087. TestCollationX p = (TestCollationX)pCtx;
  2088. Tcl_Obj pScript = TCL.Tcl_DuplicateObj( p.pCmp );
  2089. int iRes = 0;
  2090. TCL.Tcl_IncrRefCount( pScript );
  2091. TCL.Tcl_ListObjAppendElement( null, pScript, TCL.Tcl_NewStringObj( zLeft, nLeft ) );
  2092. TCL.Tcl_ListObjAppendElement( null, pScript, TCL.Tcl_NewStringObj( zRight, nRight ) );
  2093. if ( TCL.TCL_OK != TCL.Tcl_EvalObjEx( p.interp, pScript, TCL.TCL_EVAL_DIRECT | TCL.TCL_EVAL_GLOBAL )
  2094. || ( TCL.Tcl_GetIntFromObj( p.interp, TCL.Tcl_GetObjResult( p.interp ), out iRes ) != TCL.TCL_OK )
  2095. )
  2096. {
  2097. TCL.Tcl_BackgroundError( p.interp );
  2098. }
  2099. TCL.Tcl_DecrRefCount( ref pScript );
  2100. return iRes;
  2101. }
  2102. static int test_create_collation_v2(
  2103. object clientdata, /* Not used */
  2104. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  2105. int objc, /* Number of arguments */
  2106. Tcl_Obj[] objv /* Command arguments */
  2107. )
  2108. {
  2109. TestCollationX p;
  2110. sqlite3 db = null;
  2111. int rc;
  2112. if ( objc != 5 )
  2113. {
  2114. TCL.Tcl_WrongNumArgs( interp, 1, objv, "DB-HANDLE NAME CMP-PROC DEL-PROC" );
  2115. return TCL.TCL_ERROR;
  2116. }
  2117. if ( getDbPointer( interp, TCL.Tcl_GetString( objv[1] ), out db ) != 0 )
  2118. return TCL.TCL_ERROR;
  2119. p = new TestCollationX(); //(TestCollationX )sqlite3Malloc(sizeof(TestCollationX));
  2120. p.pCmp = objv[3];
  2121. p.pDel = objv[4];
  2122. p.interp = interp;
  2123. TCL.Tcl_IncrRefCount( p.pCmp );
  2124. TCL.Tcl_IncrRefCount( p.pDel );
  2125. rc = sqlite3_create_collation_v2( db, TCL.Tcl_GetString( objv[2] ), 16,
  2126. p, (dxCompare)testCreateCollationCmp, testCreateCollationDel
  2127. );
  2128. if ( rc != SQLITE_MISUSE )
  2129. {
  2130. TCL.Tcl_AppendResult( interp, "sqlite3_create_collate_v2() failed to detect " +
  2131. "an invalid encoding" );
  2132. return TCL.TCL_ERROR;
  2133. }
  2134. rc = sqlite3_create_collation_v2( db, TCL.Tcl_GetString( objv[2] ), SQLITE_UTF8,
  2135. p, (dxCompare)testCreateCollationCmp, testCreateCollationDel
  2136. );
  2137. return TCL.TCL_OK;
  2138. }
  2139. /*
  2140. ** USAGE: sqlite3_create_function_v2 DB NAME NARG ENC ?SWITCHES?
  2141. **
  2142. ** Available switches are:
  2143. **
  2144. ** -func SCRIPT
  2145. ** -step SCRIPT
  2146. ** -final SCRIPT
  2147. ** -destroy SCRIPT
  2148. */
  2149. //typedef struct CreateFunctionV2 CreateFunctionV2;
  2150. public class CreateFunctionV2
  2151. {
  2152. public Tcl_Interp interp;
  2153. public Tcl_Obj pFunc; /* Script for function invocation */
  2154. public Tcl_Obj pStep; /* Script for agg. step invocation */
  2155. public Tcl_Obj pFinal; /* Script for agg. finalization invocation */
  2156. public Tcl_Obj pDestroy; /* Destructor script */
  2157. };
  2158. static void cf2Func( sqlite3_context ctx, int nArg, sqlite3_value[] aArg )
  2159. {
  2160. }
  2161. static void cf2Step( sqlite3_context ctx, int nArg, sqlite3_value[] aArg )
  2162. {
  2163. }
  2164. static void cf2Final( sqlite3_context ctx )
  2165. {
  2166. }
  2167. static void cf2Destroy( object pUser )
  2168. {
  2169. CreateFunctionV2 p = (CreateFunctionV2)pUser;
  2170. if ( p.interp != null && p.pDestroy != null )
  2171. {
  2172. int rc = TCL.Tcl_EvalObjEx( p.interp, p.pDestroy, 0 );
  2173. if ( rc != TCL.TCL_OK )
  2174. TCL.Tcl_BackgroundError( p.interp );
  2175. }
  2176. if ( p.pFunc != null )
  2177. TCL.Tcl_DecrRefCount( ref p.pFunc );
  2178. if ( p.pStep != null )
  2179. TCL.Tcl_DecrRefCount( ref p.pStep );
  2180. if ( p.pFinal != null )
  2181. TCL.Tcl_DecrRefCount( ref p.pFinal );
  2182. if ( p.pDestroy != null )
  2183. TCL.Tcl_DecrRefCount( ref p.pDestroy );
  2184. //sqlite3_free( p);
  2185. }
  2186. public class EncTable
  2187. {
  2188. public string zEnc;
  2189. public int enc;
  2190. public EncTable( string zEnc, int enc )
  2191. {
  2192. this.zEnc = zEnc;
  2193. this.enc = enc;
  2194. }
  2195. }
  2196. static int test_create_function_v2(
  2197. ClientData clientData, /* Not used */
  2198. Tcl_Interp interp, /* The invoking TCL interpreter */
  2199. int objc, /* Number of arguments */
  2200. Tcl_Obj[] objv /* Command arguments */
  2201. )
  2202. {
  2203. sqlite3 db = null;
  2204. string zFunc;
  2205. int nArg = 0;
  2206. int enc = 0;
  2207. CreateFunctionV2 p;
  2208. int i;
  2209. int rc;
  2210. EncTable[] aEnc = {
  2211. new EncTable("utf8", SQLITE_UTF8 ),
  2212. new EncTable("utf16", SQLITE_UTF16 ),
  2213. new EncTable("utf16le", SQLITE_UTF16LE ),
  2214. new EncTable("utf16be", SQLITE_UTF16BE ),
  2215. new EncTable("any", SQLITE_ANY ),
  2216. new EncTable("0", 0 )
  2217. };
  2218. if ( objc < 5 || ( objc % 2 ) == 0 )
  2219. {
  2220. TCL.Tcl_WrongNumArgs( interp, 1, objv, "DB NAME NARG ENC SWITCHES..." );
  2221. return TCL.TCL_ERROR;
  2222. }
  2223. if ( getDbPointer( interp, TCL.Tcl_GetString( objv[1] ), out db ) != 0 )
  2224. return TCL.TCL_ERROR;
  2225. zFunc = TCL.Tcl_GetString( objv[2] );
  2226. if ( TCL.Tcl_GetIntFromObj( interp, objv[3], out nArg ) != 0 )
  2227. return TCL.TCL_ERROR;
  2228. //if ( TCL.Tcl_GetIndexFromObjStruct( interp, objv[4], aEnc,//sizeof(aEnc[0]),
  2229. // "encoding", 0, ref enc)
  2230. //)
  2231. int iEnc;
  2232. for ( iEnc = 0; iEnc < aEnc.Length && ( aEnc[iEnc].zEnc != objv[4].ToString() ); iEnc++ )
  2233. {
  2234. }
  2235. if ( iEnc >= aEnc.Length )
  2236. return TCL.TCL_ERROR;
  2237. enc = aEnc[iEnc].enc;
  2238. p = new CreateFunctionV2();//sqlite3_malloc( sizeof( CreateFunctionV2 ) );
  2239. //Debug.Assert( p );
  2240. //memset(p, 0, sizeof(CreateFunctionV2));
  2241. p.interp = interp;
  2242. for ( i = 5; i < objc; i += 2 )
  2243. {
  2244. int iSwitch = 0;
  2245. string[] azSwitch = { "-func", "-step", "-final", "-destroy", "" };
  2246. if ( TCL.Tcl_GetIndexFromObj( interp, objv[i], azSwitch, "switch", 0, out iSwitch ) )
  2247. {
  2248. //sqlite3_free(p);
  2249. return TCL.TCL_ERROR;
  2250. }
  2251. switch ( iSwitch )
  2252. {
  2253. case 0:
  2254. p.pFunc = objv[i + 1];
  2255. break;
  2256. case 1:
  2257. p.pStep = objv[i + 1];
  2258. break;
  2259. case 2:
  2260. p.pFinal = objv[i + 1];
  2261. break;
  2262. case 3:
  2263. p.pDestroy = objv[i + 1];
  2264. break;
  2265. }
  2266. }
  2267. if ( p.pFunc != null )
  2268. p.pFunc = TCL.Tcl_DuplicateObj( p.pFunc );
  2269. if ( p.pStep != null )
  2270. p.pStep = TCL.Tcl_DuplicateObj( p.pStep );
  2271. if ( p.pFinal != null )
  2272. p.pFinal = TCL.Tcl_DuplicateObj( p.pFinal );
  2273. if ( p.pDestroy != null )
  2274. p.pDestroy = TCL.Tcl_DuplicateObj( p.pDestroy );
  2275. if ( p.pFunc != null )
  2276. TCL.Tcl_IncrRefCount( p.pFunc );
  2277. if ( p.pStep != null )
  2278. TCL.Tcl_IncrRefCount( p.pStep );
  2279. if ( p.pFinal != null )
  2280. TCL.Tcl_IncrRefCount( p.pFinal );
  2281. if ( p.pDestroy != null )
  2282. TCL.Tcl_IncrRefCount( p.pDestroy );
  2283. rc = sqlite3_create_function_v2( db, zFunc, nArg, enc, p,
  2284. ( p.pFunc != null ? (dxFunc)cf2Func : (dxFunc)null ),
  2285. ( p.pStep != null ? (dxStep)cf2Step : (dxStep)null ),
  2286. ( p.pFinal != null ? (dxFinal)cf2Final : (dxFinal)null ),
  2287. (dxFDestroy)cf2Destroy
  2288. );
  2289. if ( rc != SQLITE_OK )
  2290. {
  2291. TCL.Tcl_ResetResult( interp );
  2292. TCL.Tcl_AppendResult( interp, sqlite3TestErrorName( rc ) );
  2293. return TCL.TCL_ERROR;
  2294. }
  2295. return TCL.TCL_OK;
  2296. }
  2297. /*
  2298. ** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC?
  2299. */
  2300. //static int test_load_extension(
  2301. // object clientdata, /* Not used */
  2302. // Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  2303. // int objc, /* Number of arguments */
  2304. // Tcl_Obj[] objv /* Command arguments */
  2305. //){
  2306. // TCL.Tcl_CmdInfo cmdInfo;
  2307. // sqlite3 db=null;
  2308. // int rc;
  2309. // string zDb;
  2310. // string zFile;
  2311. // string zProc = 0;
  2312. // string zErr = 0;
  2313. // if( objc!=4 && objc!=3 ){
  2314. // TCL.Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE FILE ?PROC?");
  2315. // return TCL.TCL_ERROR;
  2316. // }
  2317. // zDb = TCL.Tcl_GetString(objv[1]);
  2318. // zFile = TCL.Tcl_GetString(objv[2]);
  2319. // if( objc==4 ){
  2320. // zProc = TCL.Tcl_GetString(objv[3]);
  2321. // }
  2322. // /* Extract the C database handle from the Tcl command name */
  2323. // if( null==TCL.Tcl_GetCommandInfo(interp, zDb, cmdInfo) ){
  2324. // TCL.Tcl_AppendResult(interp, "command not found: ", zDb);
  2325. // return TCL.TCL_ERROR;
  2326. // }
  2327. // db = ((struct SqliteDb)cmdInfo.objclientdata).db;
  2328. // Debug.Assert(db);
  2329. // /* Call the underlying C function. If an error occurs, set rc to
  2330. // ** TCL.TCL_ERROR and load any error string into the interpreter. If no
  2331. // ** error occurs, set rc to TCL.TCL_OK.
  2332. // */
  2333. #if SQLITE_OMIT_LOAD_EXTENSION
  2334. // rc = SQLITE_ERROR;
  2335. // zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()");
  2336. #else
  2337. // rc = sqlite3_load_extension(db, zFile, zProc, zErr);
  2338. #endif
  2339. // if( rc!=SQLITE_OK ){
  2340. // TCL.Tcl_SetResult(interp, zErr ? zErr : "", TCL.Tcl_VOLATILE);
  2341. // rc = TCL.TCL_ERROR;
  2342. // }else{
  2343. // rc = TCL.TCL_OK;
  2344. // }
  2345. // sqlite3DbFree(db,zErr);
  2346. // return rc;
  2347. //}
  2348. /*
  2349. ** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF
  2350. */
  2351. //static int test_enable_load(
  2352. // object clientdata, /* Not used */
  2353. // Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  2354. // int objc, /* Number of arguments */
  2355. // Tcl_Obj[] objv /* Command arguments */
  2356. //){
  2357. // TCL.Tcl_CmdInfo cmdInfo;
  2358. // sqlite3 db=null;
  2359. // string zDb;
  2360. // int onoff;
  2361. // if( objc!=3 ){
  2362. // TCL.Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF");
  2363. // return TCL.TCL_ERROR;
  2364. // }
  2365. // zDb = TCL.Tcl_GetString(objv[1]);
  2366. // /* Extract the C database handle from the Tcl command name */
  2367. // if( null==TCL.Tcl_GetCommandInfo(interp, zDb, cmdInfo) ){
  2368. // TCL.Tcl_AppendResult(interp, "command not found: ", zDb);
  2369. // return TCL.TCL_ERROR;
  2370. // }
  2371. // db = ((struct SqliteDb)cmdInfo.objclientdata).db;
  2372. // Debug.Assert(db);
  2373. // /* Get the onoff parameter */
  2374. // if( TCL.Tcl_GetBooleanFromObj(interp, objv[2], out onoff) ){
  2375. // return TCL.TCL_ERROR;
  2376. // }
  2377. #if SQLITE_OMIT_LOAD_EXTENSION
  2378. // TCL.Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()");
  2379. // return TCL.TCL_ERROR;
  2380. #else
  2381. // sqlite3_enable_load_extension(db, onoff);
  2382. // return TCL.TCL_OK;
  2383. #endif
  2384. //}
  2385. /*
  2386. ** Usage: sqlite_abort
  2387. **
  2388. ** Shutdown the process immediately. This is not a clean shutdown.
  2389. ** This command is used to test the recoverability of a database in
  2390. ** the event of a program crash.
  2391. */
  2392. //static int sqlite_abort(
  2393. // object NotUsed,
  2394. // Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  2395. // int argc, /* Number of arguments */
  2396. // Tcl_Obj[] argv /* Text of each argument */
  2397. //){
  2398. //#if defined(_MSC_VER)
  2399. // /* We do this, otherwise the test will halt with a popup message
  2400. // * that we have to click away before the test will continue.
  2401. // */
  2402. // _set_abort_behavior( 0, _CALL_REPORTFAULT );
  2403. //#endif
  2404. // exit(255);
  2405. // Debug.Assert( interp==0 ); /* This will always fail */
  2406. // return TCL.TCL_OK;
  2407. //}
  2408. /*
  2409. ** The following routine is a user-defined SQL function whose purpose
  2410. ** is to test the sqlite_set_result() API.
  2411. */
  2412. static void testFunc( sqlite3_context context, int argc, sqlite3_value[] argv )
  2413. {
  2414. int ARGV = 0;
  2415. while ( argc >= 2 )
  2416. {
  2417. string zArg0 = sqlite3_value_text( argv[ARGV] );
  2418. if ( zArg0 != null )
  2419. {
  2420. if ( zArg0.Equals( "int", StringComparison.InvariantCultureIgnoreCase ) )
  2421. {
  2422. sqlite3_result_int( context, sqlite3_value_int( argv[ARGV + 1] ) );
  2423. }
  2424. else if ( zArg0.Equals( "int64", StringComparison.InvariantCultureIgnoreCase ) )
  2425. {
  2426. sqlite3_result_int64( context, sqlite3_value_int64( argv[ARGV + 1] ) );
  2427. }
  2428. else if ( zArg0.Equals( "string", StringComparison.InvariantCultureIgnoreCase ) )
  2429. {
  2430. sqlite3_result_text( context, sqlite3_value_text( argv[ARGV + 1] ), -1,
  2431. SQLITE_TRANSIENT );
  2432. }
  2433. else if ( zArg0.Equals( "double", StringComparison.InvariantCultureIgnoreCase ) )
  2434. {
  2435. sqlite3_result_double( context, sqlite3_value_double( argv[ARGV + 1] ) );
  2436. }
  2437. else if ( zArg0.Equals( "null", StringComparison.InvariantCultureIgnoreCase ) )
  2438. {
  2439. sqlite3_result_null( context );
  2440. }
  2441. else if ( zArg0.Equals( "value", StringComparison.InvariantCultureIgnoreCase ) )
  2442. {
  2443. sqlite3_result_value( context, argv[sqlite3_value_int( argv[ARGV + 1] )] );
  2444. }
  2445. else
  2446. {
  2447. goto error_out;
  2448. }
  2449. }
  2450. else
  2451. {
  2452. goto error_out;
  2453. }
  2454. argc -= 2;
  2455. ARGV += 2;
  2456. }
  2457. return;
  2458. error_out:
  2459. sqlite3_result_error( context, "first argument should be one of: " +
  2460. "int int64 string double null value", -1 );
  2461. }
  2462. /*
  2463. ** Usage: sqlite_register_test_function DB NAME
  2464. **
  2465. ** Register the test SQL function on the database DB under the name NAME.
  2466. */
  2467. static int test_register_func(
  2468. object NotUsed,
  2469. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  2470. int argc, /* Number of arguments */
  2471. Tcl_Obj[] argv /* Text of each argument */
  2472. )
  2473. {
  2474. sqlite3 db = null;
  2475. int rc;
  2476. if ( argc != 3 )
  2477. {
  2478. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  2479. " DB FUNCTION-NAME" );
  2480. return TCL.TCL_ERROR;
  2481. }
  2482. if ( getDbPointer( interp, argv[1].ToString(), out db ) != 0 )
  2483. return TCL.TCL_ERROR;
  2484. rc = sqlite3_create_function( db, argv[2].ToString(), -1, SQLITE_UTF8, 0,
  2485. testFunc, null, null );
  2486. if ( rc != 0 )
  2487. {
  2488. TCL.Tcl_AppendResult( interp, sqlite3ErrStr( rc ) );
  2489. return TCL.TCL_ERROR;
  2490. }
  2491. if ( sqlite3TestErrCode( interp, db, rc ) != 0 )
  2492. return TCL.TCL_ERROR;
  2493. return TCL.TCL_OK;
  2494. }
  2495. /*
  2496. ** Usage: sqlite3_finalize STMT
  2497. **
  2498. ** Finalize a statement handle.
  2499. */
  2500. static int test_finalize(
  2501. object clientdata,
  2502. Tcl_Interp interp,
  2503. int objc,
  2504. Tcl_Obj[] objv
  2505. )
  2506. {
  2507. sqlite3_stmt pStmt = null;
  2508. int rc;
  2509. sqlite3 db = null;
  2510. if ( objc != 2 )
  2511. {
  2512. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  2513. TCL.Tcl_GetStringFromObj( objv[0], 0 ), " <STMT>" );
  2514. return TCL.TCL_ERROR;
  2515. }
  2516. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  2517. return TCL.TCL_ERROR;
  2518. if ( pStmt != null )
  2519. {
  2520. db = sqlite3_db_handle( pStmt );// StmtToDb( pStmt );
  2521. }
  2522. rc = sqlite3_finalize( pStmt );
  2523. TCL.Tcl_SetResult( interp, sqlite3TestErrorName( rc ), TCL.TCL_STATIC );//t1ErrorName( rc ), TCL.TCL_STATIC );
  2524. if ( db != null && sqlite3TestErrCode( interp, db, rc ) != 0 )
  2525. return TCL.TCL_ERROR;
  2526. return TCL.TCL_OK;
  2527. }
  2528. /*
  2529. ** Usage: sqlite3_stmt_status STMT CODE RESETFLAG
  2530. **
  2531. ** Get the value of a status counter from a statement.
  2532. */
  2533. static int test_stmt_status(
  2534. object clientdata,
  2535. Tcl_Interp interp,
  2536. int objc,
  2537. Tcl_Obj[] objv
  2538. )
  2539. {
  2540. int iValue;
  2541. int i, op = 0;
  2542. bool resetFlag = false;
  2543. string zOpName;
  2544. sqlite3_stmt pStmt = null;
  2545. //struct _aOp{
  2546. // string zName;
  2547. // int op;
  2548. //}
  2549. _aOp[] aOp = {
  2550. new _aOp( "SQLITE_STMTSTATUS_FULLSCAN_STEP", SQLITE_STMTSTATUS_FULLSCAN_STEP ),
  2551. new _aOp( "SQLITE_STMTSTATUS_SORT", SQLITE_STMTSTATUS_SORT ),
  2552. new _aOp( "SQLITE_STMTSTATUS_AUTOINDEX", SQLITE_STMTSTATUS_AUTOINDEX )
  2553. };
  2554. if ( objc != 4 )
  2555. {
  2556. TCL.Tcl_WrongNumArgs( interp, 1, objv, "STMT PARAMETER RESETFLAG" );
  2557. return TCL.TCL_ERROR;
  2558. }
  2559. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  2560. return TCL.TCL_ERROR;
  2561. zOpName = TCL.Tcl_GetString( objv[2] );
  2562. for ( i = 0; i < ArraySize( aOp ); i++ )
  2563. {
  2564. if ( aOp[i].zName == zOpName )
  2565. {
  2566. op = aOp[i].op;
  2567. break;
  2568. }
  2569. }
  2570. if ( i >= ArraySize( aOp ) )
  2571. {
  2572. if ( TCL.Tcl_GetIntFromObj( interp, objv[2], out op ) != 0 )
  2573. return TCL.TCL_ERROR;
  2574. }
  2575. if ( TCL.Tcl_GetBooleanFromObj( interp, objv[3], out resetFlag ) )
  2576. return TCL.TCL_ERROR;
  2577. iValue = sqlite3_stmt_status( pStmt, op, resetFlag ? 1 : 0 );
  2578. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewIntObj( iValue ) );
  2579. return TCL.TCL_OK;
  2580. }
  2581. /*
  2582. ** Usage: sqlite3_next_stmt DB STMT
  2583. **
  2584. ** Return the next statment in sequence after STMT.
  2585. */
  2586. static int test_next_stmt(
  2587. object clientdata,
  2588. Tcl_Interp interp,
  2589. int objc,
  2590. Tcl_Obj[] objv
  2591. )
  2592. {
  2593. sqlite3_stmt pStmt = null;
  2594. sqlite3 db = null;
  2595. StringBuilder zBuf = new StringBuilder( 50 );
  2596. if ( objc != 3 )
  2597. {
  2598. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  2599. TCL.Tcl_GetStringFromObj( objv[0], 0 ), " DB STMT" );
  2600. return TCL.TCL_ERROR;
  2601. }
  2602. if ( getDbPointer( interp, TCL.Tcl_GetString( objv[1] ), out db ) != 0 )
  2603. return TCL.TCL_ERROR;
  2604. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[2] ), out pStmt ) != 0 )
  2605. return TCL.TCL_ERROR;
  2606. pStmt = sqlite3_next_stmt( db, pStmt );
  2607. if ( pStmt != null )
  2608. {
  2609. if ( sqlite3TestMakePointerStr( interp, zBuf, pStmt ) != 0 )
  2610. return TCL.TCL_ERROR;
  2611. TCL.Tcl_AppendResult( interp, zBuf );
  2612. }
  2613. return TCL.TCL_OK;
  2614. }
  2615. /*
  2616. ** Usage: sqlite3_stmt_readonly STMT
  2617. **
  2618. ** Return true if STMT is a NULL pointer or a pointer to a statement
  2619. ** that is guaranteed to leave the database unmodified.
  2620. */
  2621. static int test_stmt_readonly(
  2622. object clientData,
  2623. Tcl_Interp interp,
  2624. int objc,
  2625. Tcl_Obj[] objv
  2626. )
  2627. {
  2628. sqlite3_stmt pStmt = null;
  2629. int rc;
  2630. if ( objc != 2 )
  2631. {
  2632. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  2633. TCL.Tcl_GetStringFromObj( objv[0], 0 ), " STMT", 0 );
  2634. return TCL.TCL_ERROR;
  2635. }
  2636. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  2637. return TCL.TCL_ERROR;
  2638. rc = sqlite3_stmt_readonly( pStmt ) ? 1 : 0;
  2639. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewBooleanObj( rc ) );
  2640. return TCL.TCL_OK;
  2641. }
  2642. /*
  2643. ** Usage: uses_stmt_journal STMT
  2644. **
  2645. ** Return true if STMT uses a statement journal.
  2646. */
  2647. static int uses_stmt_journal(
  2648. object clientdata,
  2649. Tcl_Interp interp,
  2650. int objc,
  2651. Tcl_Obj[] objv
  2652. )
  2653. {
  2654. sqlite3_stmt pStmt = null;
  2655. int rc;
  2656. if ( objc != 2 )
  2657. {
  2658. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  2659. TCL.Tcl_GetStringFromObj( objv[0], 0 ), " STMT", 0 );
  2660. return TCL.TCL_ERROR;
  2661. }
  2662. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  2663. return TCL.TCL_ERROR;
  2664. rc = sqlite3_stmt_readonly( pStmt ) ? 1 : 0;
  2665. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewBooleanObj( pStmt.usesStmtJournal ? 1 : 0 ) );
  2666. return TCL.TCL_OK;
  2667. }
  2668. /*
  2669. ** Usage: sqlite3_reset STMT
  2670. **
  2671. ** Reset a statement handle.
  2672. */
  2673. static int test_reset(
  2674. object clientdata,
  2675. Tcl_Interp interp,
  2676. int objc,
  2677. Tcl_Obj[] objv
  2678. )
  2679. {
  2680. sqlite3_stmt pStmt = null;
  2681. int rc;
  2682. if ( objc != 2 )
  2683. {
  2684. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  2685. TCL.Tcl_GetStringFromObj( objv[0], 0 ), " <STMT>" );
  2686. return TCL.TCL_ERROR;
  2687. }
  2688. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  2689. return TCL.TCL_ERROR;
  2690. rc = sqlite3_reset( pStmt );
  2691. if ( pStmt != null && sqlite3TestErrCode( interp, StmtToDb( pStmt ), rc ) != 0 )
  2692. {
  2693. return TCL.TCL_ERROR;
  2694. }
  2695. TCL.Tcl_SetResult( interp, t1ErrorName( rc ), TCL.TCL_STATIC );
  2696. /*
  2697. if( rc !=0){
  2698. return TCL.TCL_ERROR;
  2699. }
  2700. */
  2701. return TCL.TCL_OK;
  2702. }
  2703. /*
  2704. ** Usage: sqlite3_expired STMT
  2705. **
  2706. ** Return TRUE if a recompilation of the statement is recommended.
  2707. */
  2708. static int test_expired(
  2709. object clientdata,
  2710. Tcl_Interp interp,
  2711. int objc,
  2712. Tcl_Obj[] objv
  2713. )
  2714. {
  2715. #if !SQLITE_OMIT_DEPRECATED
  2716. sqlite3_stmt pStmt = null;
  2717. if ( objc != 2 )
  2718. {
  2719. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  2720. TCL.Tcl_GetStringFromObj( objv[0], 0 ), " <STMT>" );
  2721. return TCL.TCL_ERROR;
  2722. }
  2723. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 ) return TCL.TCL_ERROR;
  2724. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewBooleanObj( sqlite3_expired( pStmt ) ) );
  2725. #endif
  2726. TCL.Tcl_SetResult( interp, "0", 0 );
  2727. return TCL.TCL_OK;
  2728. }
  2729. /*
  2730. ** Usage: sqlite3TransferBindings FROMSTMT TOSTMT
  2731. **
  2732. ** Transfer all bindings from FROMSTMT over to TOSTMT
  2733. */
  2734. static int test_transfer_bind(
  2735. object clientdata,
  2736. Tcl_Interp interp,
  2737. int objc,
  2738. Tcl_Obj[] objv
  2739. )
  2740. {
  2741. #if !SQLITE_OMIT_DEPRECATED
  2742. sqlite3_stmt pStmt1 = null, pStmt2 = null;
  2743. if ( objc != 3 )
  2744. {
  2745. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  2746. TCL.Tcl_GetStringFromObj( objv[0], 0 ), " FROM-STMT TO-STMT" );
  2747. return TCL.TCL_ERROR;
  2748. }
  2749. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt1 ) != 0 ) return TCL.TCL_ERROR;
  2750. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[2] ), out pStmt2 ) != 0 ) return TCL.TCL_ERROR;
  2751. TCL.Tcl_SetObjResult( interp,
  2752. TCL.Tcl_NewIntObj( pStmt1.nVar == pStmt2.nVar ? sqlite3TransferBindings( pStmt1, pStmt2 ) : TCL.TCL_ERROR ) );
  2753. #endif
  2754. return TCL.TCL_OK;
  2755. }
  2756. /*
  2757. ** Usage: sqlite3_changes DB
  2758. **
  2759. ** Return the number of changes made to the database by the last SQL
  2760. ** execution.
  2761. */
  2762. static int test_changes(
  2763. object clientdata,
  2764. Tcl_Interp interp,
  2765. int objc,
  2766. Tcl_Obj[] objv
  2767. )
  2768. {
  2769. sqlite3 db = null;
  2770. if ( objc != 2 )
  2771. {
  2772. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  2773. TCL.Tcl_GetString( objv[0] ), " DB" );
  2774. return TCL.TCL_ERROR;
  2775. }
  2776. if ( getDbPointer( interp, TCL.Tcl_GetString( objv[1] ), out db ) != 0 )
  2777. return TCL.TCL_ERROR;
  2778. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewIntObj( sqlite3_changes( db ) ) );
  2779. return TCL.TCL_OK;
  2780. }
  2781. /*
  2782. ** This is the "static_bind_value" that variables are bound to when
  2783. ** the FLAG option of sqlite3_bind is "static"
  2784. */
  2785. //static string sqlite_static_bind_value = "";
  2786. //static int sqlite_static_bind_nbyte = 0;
  2787. /*
  2788. ** Usage: sqlite3_bind VM IDX VALUE FLAGS
  2789. **
  2790. ** Sets the value of the IDX-th occurance of "?" in the original SQL
  2791. ** string. VALUE is the new value. If FLAGS=="null" then VALUE is
  2792. ** ignored and the value is set to NULL. If FLAGS=="static" then
  2793. ** the value is set to the value of a static variable named
  2794. ** "sqlite_static_bind_value". If FLAGS=="normal" then a copy
  2795. ** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored
  2796. ** an a 10-byte blob "abc\000xyz\000pq" is inserted.
  2797. */
  2798. static int test_bind(
  2799. object NotUsed,
  2800. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  2801. int argc, /* Number of arguments */
  2802. Tcl_Obj[] argv /* Text of each argument */
  2803. )
  2804. {
  2805. sqlite3_stmt pStmt = null;
  2806. int rc;
  2807. int idx = 0;
  2808. if ( argc != 5 )
  2809. {
  2810. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  2811. " VM IDX VALUE (null|static|normal)\"" );
  2812. return TCL.TCL_ERROR;
  2813. }
  2814. if ( getStmtPointer( interp, argv[1].ToString(), out pStmt ) != 0 )
  2815. return TCL.TCL_ERROR;
  2816. if ( TCL.Tcl_GetInt( interp, argv[2], out idx ) )
  2817. return TCL.TCL_ERROR;
  2818. if ( argv[4].ToString() == "null" )
  2819. {
  2820. rc = sqlite3_bind_null( pStmt, idx );
  2821. }
  2822. else if ( argv[4].ToString() == "static" )
  2823. {
  2824. rc = sqlite3_bind_text( pStmt, idx, sqlite_static_bind_value.sValue, -1, null );
  2825. }
  2826. else if ( argv[4].ToString() == "static-nbytes" )
  2827. {
  2828. rc = sqlite3_bind_text( pStmt, idx, sqlite_static_bind_value.sValue,
  2829. sqlite_static_bind_nbyte.iValue, null );
  2830. }
  2831. else if ( argv[4].ToString() == "normal" )
  2832. {
  2833. rc = sqlite3_bind_text( pStmt, idx, argv[3].ToString(), -1, SQLITE_TRANSIENT );
  2834. }
  2835. else if ( argv[4].ToString() == "blob10" )
  2836. {
  2837. rc = sqlite3_bind_text( pStmt, idx, "abc\0xyz\0pq", 10, SQLITE_STATIC );
  2838. }
  2839. else
  2840. {
  2841. TCL.Tcl_AppendResult( interp, "4th argument should be " +
  2842. "\"null\" or \"static\" or \"normal\"" );
  2843. return TCL.TCL_ERROR;
  2844. }
  2845. if ( sqlite3TestErrCode( interp, StmtToDb( pStmt ), rc ) != 0 )
  2846. return TCL.TCL_ERROR;
  2847. if ( rc != 0 )
  2848. {
  2849. StringBuilder zBuf = new StringBuilder( 50 );
  2850. sqlite3_snprintf( 50, zBuf, "(%d) ", rc );
  2851. TCL.Tcl_SetResult( interp, zBuf + sqlite3ErrStr( rc ), 0 );
  2852. return TCL.TCL_ERROR;
  2853. }
  2854. return TCL.TCL_OK;
  2855. }
  2856. #if !SQLITE_OMIT_UTF16
  2857. /*
  2858. ** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be>
  2859. **
  2860. ** This function is used to test that SQLite selects the correct collation
  2861. ** sequence callback when multiple versions (for different text encodings)
  2862. ** are available.
  2863. **
  2864. ** Calling this routine registers the collation sequence "test_collate"
  2865. ** with database handle <db>. The second argument must be a list of three
  2866. ** boolean values. If the first is true, then a version of test_collate is
  2867. ** registered for UTF-8, if the second is true, a version is registered for
  2868. ** UTF-16le, if the third is true, a UTF-16be version is available.
  2869. ** Previous versions of test_collate are deleted.
  2870. **
  2871. ** The collation sequence test_collate is implemented by calling the
  2872. ** following TCL script:
  2873. **
  2874. ** "test_collate <enc> <lhs> <rhs>"
  2875. **
  2876. ** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8.
  2877. ** The <enc> parameter is the encoding of the collation function that
  2878. ** SQLite selected to call. The TCL test script implements the
  2879. ** "test_collate" proc.
  2880. **
  2881. ** Note that this will only work with one intepreter at a time, as the
  2882. ** interp pointer to use when evaluating the TCL script is stored in
  2883. ** pTestCollateInterp.
  2884. */
  2885. //static Tcl_Interp * pTestCollateInterp;
  2886. //static int test_collate_func(
  2887. // object pCtx,,
  2888. // int nA, string zA,
  2889. // int nB, string zB
  2890. //){
  2891. // Tcl_Interp *i = pTestCollateInterp;
  2892. // int encin = SQLITE_PTR_TO_INT(pCtx);
  2893. // int res;
  2894. // int n;
  2895. // sqlite3_value pVal;
  2896. // Tcl_Obj pX;
  2897. // pX = TCL.Tcl_NewStringObj("test_collate", -1);
  2898. // TCL.Tcl_IncrRefCount(pX);
  2899. // switch( encin ){
  2900. // case SQLITE_UTF8:
  2901. // TCL.Tcl_ListObjAppendElement(i,pX,TCL.Tcl_NewStringObj("UTF-8",-1));
  2902. // break;
  2903. // case SQLITE_UTF16LE:
  2904. // TCL.Tcl_ListObjAppendElement(i,pX,TCL.Tcl_NewStringObj("UTF-16LE",-1));
  2905. // break;
  2906. // case SQLITE_UTF16BE:
  2907. // TCL.Tcl_ListObjAppendElement(i,pX,TCL.Tcl_NewStringObj("UTF-16BE",-1));
  2908. // break;
  2909. // default:
  2910. // Debug.Assert(false);
  2911. // }
  2912. //sqlite3BeginBenignMalloc();
  2913. // pVal = sqlite3ValueNew(0);
  2914. // if( pVal ){
  2915. // sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC);
  2916. // n = sqlite3_value_bytes(pVal);
  2917. // TCL.Tcl_ListObjAppendElement(i,pX,
  2918. // TCL.Tcl_NewStringObj((char)sqlite3_value_text(pVal),n));
  2919. // sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC);
  2920. // n = sqlite3_value_bytes(pVal);
  2921. // TCL.Tcl_ListObjAppendElement(i,pX,
  2922. // TCL.Tcl_NewStringObj((char)sqlite3_value_text(pVal),n));
  2923. // sqlite3ValueFree(pVal);
  2924. //}
  2925. //sqlite3EndBenignMalloc();
  2926. // TCL.Tcl_EvalObjEx(i, pX, 0);
  2927. // TCL.Tcl_DecrRefCount(pX);
  2928. // TCL.Tcl_GetIntFromObj(i, TCL.Tcl_GetObjResult(i), res);
  2929. // return res;
  2930. //}
  2931. //static int test_collate(
  2932. // object clientdata,
  2933. // Tcl_Interp interp,
  2934. // int objc,
  2935. // Tcl_Obj[] objv
  2936. //){
  2937. // sqlite3 db=null;
  2938. // int val;
  2939. // sqlite3_value pVal;
  2940. // int rc;
  2941. // if( objc!=5 ) goto bad_args;
  2942. // pTestCollateInterp = interp;
  2943. // if( getDbPointer(interp, TCL.Tcl_GetString(objv[1]), out db) !=0) return TCL.TCL_ERROR;
  2944. // if( TCL.TCL_OK!=TCL.Tcl_GetBooleanFromObj(interp, objv[2], out val) ) return TCL.TCL_ERROR;
  2945. // rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8,
  2946. // (void )SQLITE_UTF8, val?test_collate_func:0);
  2947. // if( rc==SQLITE_OK ){
  2948. // string zUtf16;
  2949. // if( TCL.TCL_OK!=TCL.Tcl_GetBooleanFromObj(interp, objv[3], out val) ) return TCL.TCL_ERROR;
  2950. // rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE,
  2951. // (void )SQLITE_UTF16LE, val?test_collate_func:0);
  2952. // if( TCL.TCL_OK!=TCL.Tcl_GetBooleanFromObj(interp, objv[4], out val) ) return TCL.TCL_ERROR;
  2953. //#if FALSE
  2954. // if( sqlite3_iMallocFail>0 ){
  2955. // sqlite3_iMallocFail++;
  2956. // }
  2957. //#endif
  2958. // sqlite3_mutex_enter(db.mutex);
  2959. // pVal = sqlite3ValueNew(db);
  2960. // sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC);
  2961. // zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
  2962. // if( db.mallocFailed !=0{
  2963. // rc = SQLITE_NOMEM;
  2964. // }else{
  2965. // rc = sqlite3_create_collation16(db, zUtf16, SQLITE_UTF16BE,
  2966. // (void )SQLITE_UTF16BE, val?test_collate_func:0);
  2967. // }
  2968. // sqlite3ValueFree(pVal);
  2969. // sqlite3_mutex_leave(db.mutex);
  2970. // }
  2971. // if( sqlite3TestErrCode(interp, db, rc) ) return TCL.TCL_ERROR;
  2972. // if( rc!=SQLITE_OK ){
  2973. // TCL.Tcl_AppendResult(interp, sqlite3TestErrorName(rc));
  2974. // return TCL.TCL_ERROR;
  2975. // }
  2976. // return TCL.TCL_OK;
  2977. //bad_args:
  2978. // TCL.Tcl_AppendResult(interp, "wrong # args: should be \"",
  2979. // TCL.Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
  2980. // return TCL.TCL_ERROR;
  2981. //}
  2982. /*
  2983. ** When the collation needed callback is invoked, record the name of
  2984. ** the requested collating function here. The recorded name is linked
  2985. ** to a TCL variable and used to make sure that the requested collation
  2986. ** name is correct.
  2987. */
  2988. //static char zNeededCollation[200];
  2989. //static char pzNeededCollation = zNeededCollation;
  2990. /*
  2991. ** Called when a collating sequence is needed. Registered using
  2992. ** sqlite3_collation_needed16().
  2993. */
  2994. //static void test_collate_needed_cb(
  2995. // object pCtx,,
  2996. // sqlite3 db,
  2997. // int eTextRep,
  2998. // const void pName
  2999. //){
  3000. // int enc = ENC(db);
  3001. // int i;
  3002. // string z;
  3003. // for(z = (char)pName, i=0; *z || z[1]; z++){
  3004. // if( *z ) zNeededCollation[i++] = *z;
  3005. // }
  3006. // zNeededCollation[i] = 0;
  3007. // sqlite3_create_collation(
  3008. // db, "test_collate", ENC(db), SQLITE_INT_TO_PTR(enc), test_collate_func);
  3009. //}
  3010. /*
  3011. ** Usage: add_test_collate_needed DB
  3012. */
  3013. //static int test_collate_needed(
  3014. // object clientdata,
  3015. // Tcl_Interp interp,
  3016. // int objc,
  3017. // Tcl_Obj[] objv
  3018. //){
  3019. // sqlite3 db=null;
  3020. // int rc;
  3021. // if( objc!=2 ) goto bad_args;
  3022. // if( getDbPointer(interp, TCL.Tcl_GetString(objv[1]), out db) !=0) return TCL.TCL_ERROR;
  3023. // rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb);
  3024. // zNeededCollation[0] = 0;
  3025. // if( sqlite3TestErrCode(interp, db, rc) ) return TCL.TCL_ERROR;
  3026. // return TCL.TCL_OK;
  3027. //bad_args:
  3028. // TCL.Tcl_WrongNumArgs(interp, 1, objv, "DB");
  3029. // return TCL.TCL_ERROR;
  3030. //}
  3031. /*
  3032. ** tclcmd: add_alignment_test_collations DB
  3033. **
  3034. ** Add two new collating sequences to the database DB
  3035. **
  3036. ** utf16_aligned
  3037. ** utf16_unaligned
  3038. **
  3039. ** Both collating sequences use the same sort order as BINARY.
  3040. ** The only difference is that the utf16_aligned collating
  3041. ** sequence is declared with the SQLITE_UTF16_ALIGNED flag.
  3042. ** Both collating functions increment the unaligned utf16 counter
  3043. ** whenever they see a string that begins on an odd byte boundary.
  3044. */
  3045. //static int unaligned_string_counter = 0;
  3046. //static int alignmentCollFunc(
  3047. // object NotUsed,
  3048. // int nKey1, const void pKey1,
  3049. // int nKey2, const void pKey2
  3050. //){
  3051. // int rc, n;
  3052. // n = nKey1<nKey2 ? nKey1 : nKey2;
  3053. //if( nKey1>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey1))) ) unaligned_string_counter++;
  3054. //if( nKey2>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey2))) ) unaligned_string_counter++;
  3055. // rc = memcmp(pKey1, pKey2, n);
  3056. // if( rc==0 ){
  3057. // rc = nKey1 - nKey2;
  3058. // }
  3059. // return rc;
  3060. //}
  3061. //static int add_alignment_test_collations(
  3062. // object clientdata,
  3063. // Tcl_Interp interp,
  3064. // int objc,
  3065. // Tcl_Obj[] objv
  3066. //){
  3067. // sqlite3 db=null;
  3068. // if( objc>=2 ){
  3069. // if( getDbPointer(interp, TCL.Tcl_GetString(objv[1]), out db) !=0) return TCL.TCL_ERROR;
  3070. // sqlite3_create_collation(db, "utf16_unaligned", SQLITE_UTF16,
  3071. // 0, alignmentCollFunc);
  3072. // sqlite3_create_collation(db, "utf16_aligned", SQLITE_UTF16_ALIGNED,
  3073. // 0, alignmentCollFunc);
  3074. // }
  3075. // return SQLITE_OK;
  3076. //}
  3077. #endif // * !SQLITE_OMIT_UTF16) */
  3078. /*
  3079. ** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be>
  3080. **
  3081. ** This function is used to test that SQLite selects the correct user
  3082. ** function callback when multiple versions (for different text encodings)
  3083. ** are available.
  3084. **
  3085. ** Calling this routine registers up to three versions of the user function
  3086. ** "test_function" with database handle <db>. If the second argument is
  3087. ** true, then a version of test_function is registered for UTF-8, if the
  3088. ** third is true, a version is registered for UTF-16le, if the fourth is
  3089. ** true, a UTF-16be version is available. Previous versions of
  3090. ** test_function are deleted.
  3091. **
  3092. ** The user function is implemented by calling the following TCL script:
  3093. **
  3094. ** "test_function <enc> <arg>"
  3095. **
  3096. ** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the
  3097. ** single argument passed to the SQL function. The value returned by
  3098. ** the TCL script is used as the return value of the SQL function. It
  3099. ** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8
  3100. ** for a UTF-16LE test_function(), and UTF-16LE for an implementation that
  3101. ** prefers UTF-16BE.
  3102. */
  3103. #if !SQLITE_OMIT_UTF16
  3104. //static void test_function_utf8(
  3105. // sqlite3_context pCtx,
  3106. // int nArg,
  3107. // sqlite3_value[] argv
  3108. //){
  3109. // Tcl_Interp interp;
  3110. // Tcl_Obj pX;
  3111. // sqlite3_value pVal;
  3112. // interp = (Tcl_Interp )sqlite3_context_db_handle(pCtx);
  3113. // pX = TCL.Tcl_NewStringObj("test_function", -1);
  3114. // TCL.Tcl_IncrRefCount(pX);
  3115. // TCL.Tcl_ListObjAppendElement(interp, pX, TCL.Tcl_NewStringObj("UTF-8", -1));
  3116. // TCL.Tcl_ListObjAppendElement(interp, pX,
  3117. // TCL.Tcl_NewStringObj((char)sqlite3_value_text(argv[0]), -1));
  3118. // TCL.Tcl_EvalObjEx(interp, pX, 0);
  3119. // TCL.Tcl_DecrRefCount(pX);
  3120. // sqlite3_result_text(pCtx, TCL.Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT);
  3121. // pVal = sqlite3ValueNew(0);
  3122. // sqlite3ValueSetStr(pVal, -1, TCL.Tcl_GetStringResult(interp),
  3123. // SQLITE_UTF8, SQLITE_STATIC);
  3124. // sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal),
  3125. // -1, SQLITE_TRANSIENT);
  3126. // sqlite3ValueFree(pVal);
  3127. //}
  3128. //static void test_function_utf16le(
  3129. // sqlite3_context pCtx,
  3130. // int nArg,
  3131. // sqlite3_value[] argv
  3132. //){
  3133. // Tcl_Interp interp;
  3134. // Tcl_Obj pX;
  3135. // sqlite3_value pVal;
  3136. // interp = (Tcl_Interp )sqlite3_context_db_handle(pCtx);
  3137. // pX = TCL.Tcl_NewStringObj("test_function", -1);
  3138. // TCL.Tcl_IncrRefCount(pX);
  3139. // TCL.Tcl_ListObjAppendElement(interp, pX, TCL.Tcl_NewStringObj("UTF-16LE", -1));
  3140. // TCL.Tcl_ListObjAppendElement(interp, pX,
  3141. // TCL.Tcl_NewStringObj((char)sqlite3_value_text(argv[0]), -1));
  3142. // TCL.Tcl_EvalObjEx(interp, pX, 0);
  3143. // TCL.Tcl_DecrRefCount(pX);
  3144. // pVal = sqlite3ValueNew(0);
  3145. // sqlite3ValueSetStr(pVal, -1, TCL.Tcl_GetStringResult(interp),
  3146. // SQLITE_UTF8, SQLITE_STATIC);
  3147. // sqlite3_result_text(pCtx,(char)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT);
  3148. // sqlite3ValueFree(pVal);
  3149. //}
  3150. //static void test_function_utf16be(
  3151. // sqlite3_context pCtx,
  3152. // int nArg,
  3153. // sqlite3_value[] argv
  3154. //){
  3155. // Tcl_Interp interp;
  3156. // Tcl_Obj pX;
  3157. // sqlite3_value pVal;
  3158. // interp = (Tcl_Interp )sqlite3_context_db_handle(pCtx);
  3159. // pX = TCL.Tcl_NewStringObj("test_function", -1);
  3160. // TCL.Tcl_IncrRefCount(pX);
  3161. // TCL.Tcl_ListObjAppendElement(interp, pX, TCL.Tcl_NewStringObj("UTF-16BE", -1));
  3162. // TCL.Tcl_ListObjAppendElement(interp, pX,
  3163. // TCL.Tcl_NewStringObj((char)sqlite3_value_text(argv[0]), -1));
  3164. // TCL.Tcl_EvalObjEx(interp, pX, 0);
  3165. // TCL.Tcl_DecrRefCount(pX);
  3166. // pVal = sqlite3ValueNew(0);
  3167. // sqlite3ValueSetStr(pVal, -1, TCL.Tcl_GetStringResult(interp),
  3168. // SQLITE_UTF8, SQLITE_STATIC);
  3169. // sqlite3_result_text16(pCtx, sqlite3_value_text16le(pVal),
  3170. // -1, SQLITE_TRANSIENT);
  3171. // sqlite3_result_text16be(pCtx, sqlite3_value_text16le(pVal),
  3172. // -1, SQLITE_TRANSIENT);
  3173. // sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal),
  3174. // -1, SQLITE_TRANSIENT);
  3175. // sqlite3ValueFree(pVal);
  3176. //}
  3177. #endif // * SQLITE_OMIT_UTF16 */
  3178. //static int test_function(
  3179. // object clientdata,
  3180. // Tcl_Interp interp,
  3181. // int objc,
  3182. // Tcl_Obj[] objv
  3183. //){
  3184. #if !SQLITE_OMIT_UTF16
  3185. // sqlite3 db=null;
  3186. // int val;
  3187. // if( objc!=5 ) goto bad_args;
  3188. // if( getDbPointer(interp, TCL.Tcl_GetString(objv[1]), out db) !=0) return TCL.TCL_ERROR;
  3189. // if( TCL.TCL_OK!=TCL.Tcl_GetBooleanFromObj(interp, objv[2], out val) ) return TCL.TCL_ERROR;
  3190. // if( val ){
  3191. // sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8,
  3192. // interp, test_function_utf8, 0, 0);
  3193. // }
  3194. // if( TCL.TCL_OK!=TCL.Tcl_GetBooleanFromObj(interp, objv[3], out val) ) return TCL.TCL_ERROR;
  3195. // if( val ){
  3196. // sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE,
  3197. // interp, test_function_utf16le, 0, 0);
  3198. // }
  3199. // if( TCL.TCL_OK!=TCL.Tcl_GetBooleanFromObj(interp, objv[4], out val) ) return TCL.TCL_ERROR;
  3200. // if( val ){
  3201. // sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE,
  3202. // interp, test_function_utf16be, 0, 0);
  3203. // }
  3204. // return TCL.TCL_OK;
  3205. //bad_args:
  3206. // TCL.Tcl_AppendResult(interp, "wrong # args: should be \"",
  3207. // TCL.Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
  3208. #endif // * SQLITE_OMIT_UTF16 */
  3209. // return TCL.TCL_ERROR;
  3210. //}
  3211. /*
  3212. ** Usage: test_errstr <err code>
  3213. **
  3214. ** Test that the english language string equivalents for sqlite error codes
  3215. ** are sane. The parameter is an integer representing an sqlite error code.
  3216. ** The result is a list of two elements, the string representation of the
  3217. ** error code and the english language explanation.
  3218. */
  3219. static int test_errstr(
  3220. object clientdata,
  3221. Tcl_Interp interp,
  3222. int objc,
  3223. Tcl_Obj[] objv
  3224. )
  3225. {
  3226. string zCode;
  3227. int i;
  3228. if ( objc != 2 )
  3229. {
  3230. TCL.Tcl_WrongNumArgs( interp, 1, objv, "<error code>" );
  3231. }
  3232. zCode = TCL.Tcl_GetString( objv[1] );
  3233. for ( i = 0; i < 200; i++ )
  3234. {
  3235. if ( t1ErrorName( i ).Equals( zCode ) )
  3236. break;
  3237. }
  3238. TCL.Tcl_SetResult( interp, sqlite3ErrStr( i ), 0 );
  3239. return TCL.TCL_OK;
  3240. }
  3241. /*
  3242. ** Usage: breakpoint
  3243. **
  3244. ** This routine exists for one purpose - to provide a place to put a
  3245. ** breakpoint with GDB that can be triggered using TCL code. The use
  3246. ** for this is when a particular test fails on (say) the 1485th iteration.
  3247. ** In the TCL test script, we can add code like this:
  3248. **
  3249. ** if {$i==1485} breakpoint
  3250. **
  3251. ** Then run testfixture in the debugger and wait for the breakpoint to
  3252. ** fire. Then additional breakpoints can be set to trace down the bug.
  3253. */
  3254. static int test_breakpoint(
  3255. object NotUsed,
  3256. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  3257. int argc, /* Number of arguments */
  3258. Tcl_Obj[] argv /* Text of each argument */
  3259. )
  3260. {
  3261. return TCL.TCL_OK; /* Do nothing */
  3262. }
  3263. /*
  3264. ** Usage: sqlite3_bind_zeroblob STMT IDX N
  3265. **
  3266. ** Test the sqlite3_bind_zeroblob interface. STMT is a prepared statement.
  3267. ** IDX is the index of a wildcard in the prepared statement. This command
  3268. ** binds a N-byte zero-filled BLOB to the wildcard.
  3269. */
  3270. static int test_bind_zeroblob(
  3271. object clientdata,
  3272. Tcl_Interp interp,
  3273. int objc,
  3274. Tcl_Obj[] objv
  3275. )
  3276. {
  3277. sqlite3_stmt pStmt = null;
  3278. int idx = 0;
  3279. int n = 0;
  3280. int rc;
  3281. if ( objc != 4 )
  3282. {
  3283. TCL.Tcl_WrongNumArgs( interp, 1, objv, "STMT IDX N" );
  3284. return TCL.TCL_ERROR;
  3285. }
  3286. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  3287. return TCL.TCL_ERROR;
  3288. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[2], out idx ) )
  3289. return TCL.TCL_ERROR;
  3290. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[3], out n ) )
  3291. return TCL.TCL_ERROR;
  3292. rc = sqlite3_bind_zeroblob( pStmt, idx, n );
  3293. if ( sqlite3TestErrCode( interp, StmtToDb( pStmt ), rc ) != 0 )
  3294. return TCL.TCL_ERROR;
  3295. if ( rc != SQLITE_OK )
  3296. {
  3297. return TCL.TCL_ERROR;
  3298. }
  3299. return TCL.TCL_OK;
  3300. }
  3301. /*
  3302. ** Usage: sqlite3_bind_int STMT N VALUE
  3303. **
  3304. ** Test the sqlite3_bind_int interface. STMT is a prepared statement.
  3305. ** N is the index of a wildcard in the prepared statement. This command
  3306. ** binds a 32-bit integer VALUE to that wildcard.
  3307. */
  3308. static int test_bind_int(
  3309. object clientdata,
  3310. Tcl_Interp interp,
  3311. int objc,
  3312. Tcl_Obj[] objv
  3313. )
  3314. {
  3315. sqlite3_stmt pStmt = null;
  3316. int idx = 0;
  3317. int value = 0;
  3318. int rc;
  3319. if ( objc != 4 )
  3320. {
  3321. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  3322. TCL.Tcl_GetStringFromObj( objv[0], 0 ), " STMT N VALUE" );
  3323. return TCL.TCL_ERROR;
  3324. }
  3325. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  3326. return TCL.TCL_ERROR;
  3327. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[2], out idx ) )
  3328. return TCL.TCL_ERROR;
  3329. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[3], out value ) )
  3330. return TCL.TCL_ERROR;
  3331. rc = sqlite3_bind_int( pStmt, idx, value );
  3332. if ( sqlite3TestErrCode( interp, StmtToDb( pStmt ), rc ) != 0 )
  3333. return TCL.TCL_ERROR;
  3334. if ( rc != SQLITE_OK )
  3335. {
  3336. return TCL.TCL_ERROR;
  3337. }
  3338. return TCL.TCL_OK;
  3339. }
  3340. /*
  3341. ** Usage: sqlite3_bind_int64 STMT N VALUE
  3342. **
  3343. ** Test the sqlite3_bind_int64 interface. STMT is a prepared statement.
  3344. ** N is the index of a wildcard in the prepared statement. This command
  3345. ** binds a 64-bit integer VALUE to that wildcard.
  3346. */
  3347. static int test_bind_int64(
  3348. object clientdata,
  3349. Tcl_Interp interp,
  3350. int objc,
  3351. Tcl_Obj[] objv
  3352. )
  3353. {
  3354. sqlite3_stmt pStmt = null;
  3355. int idx = 0;
  3356. i64 value = 0;
  3357. int rc;
  3358. if ( objc != 4 )
  3359. {
  3360. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  3361. TCL.Tcl_GetStringFromObj( objv[0], 0 ), " STMT N VALUE" );
  3362. return TCL.TCL_ERROR;
  3363. }
  3364. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  3365. return TCL.TCL_ERROR;
  3366. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[2], out idx ) )
  3367. return TCL.TCL_ERROR;
  3368. if ( TCL.Tcl_GetWideIntFromObj( interp, objv[3], out value ) )
  3369. return TCL.TCL_ERROR;
  3370. rc = sqlite3_bind_int64( pStmt, idx, value );
  3371. if ( sqlite3TestErrCode( interp, StmtToDb( pStmt ), rc ) != 0 )
  3372. return TCL.TCL_ERROR;
  3373. if ( rc != SQLITE_OK )
  3374. {
  3375. return TCL.TCL_ERROR;
  3376. }
  3377. return TCL.TCL_OK;
  3378. }
  3379. /*
  3380. ** Usage: sqlite3_bind_double STMT N VALUE
  3381. **
  3382. ** Test the sqlite3_bind_double interface. STMT is a prepared statement.
  3383. ** N is the index of a wildcard in the prepared statement. This command
  3384. ** binds a 64-bit integer VALUE to that wildcard.
  3385. */
  3386. class _aSpecialFp
  3387. {
  3388. public string zName; /* Name of the special floating point value */
  3389. public u32 iUpper; /* Upper 32 bits */
  3390. public u32 iLower; /* Lower 32 bits */
  3391. public _aSpecialFp( string zName, u32 iUpper, u32 iLower )
  3392. {
  3393. this.zName = zName;
  3394. this.iUpper = iUpper;
  3395. this.iLower = iLower;
  3396. }
  3397. }
  3398. static int test_bind_double(
  3399. object clientdata,
  3400. Tcl_Interp interp,
  3401. int objc,
  3402. Tcl_Obj[] objv
  3403. )
  3404. {
  3405. sqlite3_stmt pStmt = null;
  3406. ;
  3407. int idx = 0;
  3408. double value = 0;
  3409. int rc;
  3410. string zVal;
  3411. int i;
  3412. _aSpecialFp[] aSpecialFp = new _aSpecialFp[] {
  3413. new _aSpecialFp( "NaN", 0x7fffffff, 0xffffffff ),
  3414. new _aSpecialFp( "SNaN", 0x7ff7ffff, 0xffffffff ),
  3415. new _aSpecialFp( "-NaN", 0xffffffff, 0xffffffff ),
  3416. new _aSpecialFp( "-SNaN", 0xfff7ffff, 0xffffffff ),
  3417. new _aSpecialFp( "+Inf", 0x7ff00000, 0x00000000 ),
  3418. new _aSpecialFp( "-Inf", 0xfff00000, 0x00000000 ),
  3419. new _aSpecialFp( "Epsilon", 0x00000000, 0x00000001 ),
  3420. new _aSpecialFp( "-Epsilon", 0x80000000, 0x00000001 ),
  3421. new _aSpecialFp( "NaN0", 0x7ff80000, 0x00000000 ),
  3422. new _aSpecialFp( "-NaN0", 0xfff80000, 0x00000000 ),
  3423. };
  3424. if ( objc != 4 )
  3425. {
  3426. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  3427. TCL.Tcl_GetStringFromObj( objv[0], 0 ), " STMT N VALUE" );
  3428. return TCL.TCL_ERROR;
  3429. }
  3430. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  3431. return TCL.TCL_ERROR;
  3432. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[2], out idx ) )
  3433. return TCL.TCL_ERROR;
  3434. /* Intercept the string "NaN" and generate a NaN value for it.
  3435. ** All other strings are passed through to TCL.Tcl_GetDoubleFromObj().
  3436. ** TCL.Tcl_GetDoubleFromObj() should understand "NaN" but some versions
  3437. ** contain a bug.
  3438. */
  3439. zVal = TCL.Tcl_GetString( objv[3] );
  3440. for ( i = 0; i < aSpecialFp.Length; i++ )
  3441. {//sizeof(aSpecialFp)/sizeof(aSpecialFp[0]); i++){
  3442. if ( aSpecialFp[i].zName == zVal )
  3443. {
  3444. i64 x;
  3445. x = aSpecialFp[i].iUpper;
  3446. x <<= 32;
  3447. x |= aSpecialFp[i].iLower;
  3448. Debug.Assert( sizeof( double ) == 8 );
  3449. Debug.Assert( sizeof( sqlite3_u3264 ) == 8 );
  3450. #if WINDOWS_PHONE
  3451. value = BitConverter.ToDouble(BitConverter.GetBytes((long)x), 0);
  3452. #else
  3453. value = BitConverter.Int64BitsToDouble( x );//memcpy(&value, x, 8);
  3454. #endif
  3455. //value = Double.NaN;
  3456. break;
  3457. }
  3458. }
  3459. if ( ( i >= aSpecialFp.Length ) && //sizeof(aSpecialFp)/sizeof(aSpecialFp[0]) &&
  3460. TCL.Tcl_GetDoubleFromObj( interp, objv[3], out value ) )
  3461. {
  3462. return TCL.TCL_ERROR;
  3463. }
  3464. rc = sqlite3_bind_double( pStmt, idx, value );
  3465. if ( sqlite3TestErrCode( interp, StmtToDb( pStmt ), rc ) != 0 )
  3466. return TCL.TCL_ERROR;
  3467. if ( rc != SQLITE_OK )
  3468. {
  3469. return TCL.TCL_ERROR;
  3470. }
  3471. return TCL.TCL_OK;
  3472. }
  3473. /*
  3474. ** Usage: sqlite3_bind_null STMT N
  3475. **
  3476. ** Test the sqlite3_bind_null interface. STMT is a prepared statement.
  3477. ** N is the index of a wildcard in the prepared statement. This command
  3478. ** binds a NULL to the wildcard.
  3479. */
  3480. static int test_bind_null(
  3481. object clientdata,
  3482. Tcl_Interp interp,
  3483. int objc,
  3484. Tcl_Obj[] objv
  3485. )
  3486. {
  3487. sqlite3_stmt pStmt = null;
  3488. ;
  3489. int idx = 0;
  3490. int rc;
  3491. if ( objc != 3 )
  3492. {
  3493. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  3494. TCL.Tcl_GetStringFromObj( objv[0], 0 ), " STMT N" );
  3495. return TCL.TCL_ERROR;
  3496. }
  3497. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  3498. return TCL.TCL_ERROR;
  3499. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[2], out idx ) )
  3500. return TCL.TCL_ERROR;
  3501. rc = sqlite3_bind_null( pStmt, idx );
  3502. if ( sqlite3TestErrCode( interp, StmtToDb( pStmt ), rc ) != 0 )
  3503. return TCL.TCL_ERROR;
  3504. if ( rc != SQLITE_OK )
  3505. {
  3506. return TCL.TCL_ERROR;
  3507. }
  3508. return TCL.TCL_OK;
  3509. }
  3510. /*
  3511. ** Usage: sqlite3_bind_text STMT N STRING BYTES
  3512. **
  3513. ** Test the sqlite3_bind_text interface. STMT is a prepared statement.
  3514. ** N is the index of a wildcard in the prepared statement. This command
  3515. ** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes
  3516. ** long.
  3517. */
  3518. static int test_bind_text(
  3519. object clientdata,
  3520. Tcl_Interp interp,
  3521. int objc,
  3522. Tcl_Obj[] objv
  3523. )
  3524. {
  3525. sqlite3_stmt pStmt = null;
  3526. int idx = 0;
  3527. int bytes = 0;
  3528. byte[] value;
  3529. int rc;
  3530. if ( objc != 5 )
  3531. {
  3532. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  3533. TCL.Tcl_GetStringFromObj( objv[0], 0 ), " STMT N VALUE BYTES" );
  3534. return TCL.TCL_ERROR;
  3535. }
  3536. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  3537. return TCL.TCL_ERROR;
  3538. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[2], out idx ) )
  3539. return TCL.TCL_ERROR;
  3540. value = TCL.Tcl_GetByteArrayFromObj( objv[3], out bytes );
  3541. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[4], out bytes ) )
  3542. return TCL.TCL_ERROR;
  3543. rc = sqlite3_bind_text( pStmt, idx, Encoding.UTF8.GetString( value, 0, value.Length ), bytes, SQLITE_TRANSIENT );
  3544. if ( sqlite3TestErrCode( interp, StmtToDb( pStmt ), rc ) != 0 )
  3545. return TCL.TCL_ERROR;
  3546. if ( rc != SQLITE_OK )
  3547. {
  3548. TCL.Tcl_AppendResult( interp, sqlite3TestErrorName( rc ) );
  3549. return TCL.TCL_ERROR;
  3550. }
  3551. return TCL.TCL_OK;
  3552. }
  3553. /*
  3554. ** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES
  3555. **
  3556. ** Test the sqlite3_bind_text16 interface. STMT is a prepared statement.
  3557. ** N is the index of a wildcard in the prepared statement. This command
  3558. ** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes
  3559. ** long.
  3560. */
  3561. static int test_bind_text16(
  3562. object clientdata,
  3563. Tcl_Interp interp,
  3564. int objc,
  3565. Tcl_Obj[] objv
  3566. )
  3567. {
  3568. #if !SQLITE_OMIT_UTF16
  3569. sqlite3_stmt pStmt=null;
  3570. int idx=0;
  3571. int bytes=0;
  3572. string value;
  3573. int rc;
  3574. dxDel xDel = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT);
  3575. Tcl_Obj oStmt = objv[objc-4];
  3576. Tcl_Obj oN = objv[objc-3];
  3577. Tcl_Obj oString = objv[objc-2];
  3578. Tcl_Obj oBytes = objv[objc-1];
  3579. if( objc!=5 && objc!=6){
  3580. TCL.Tcl_AppendResult(interp, "wrong # args: should be \"",
  3581. TCL.Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
  3582. return TCL.TCL_ERROR;
  3583. }
  3584. if( getStmtPointer(interp, TCL.Tcl_GetString(oStmt), out pStmt)!=0 ) return TCL.TCL_ERROR;
  3585. if( TCL.Tcl_GetIntFromObj(interp, oN, out idx) ) return TCL.TCL_ERROR;
  3586. int dummy0 = 0;
  3587. value = Encoding.UTF8.GetString(TCL.Tcl_GetByteArrayFromObj( oString, out dummy0 ));
  3588. if( TCL.Tcl_GetIntFromObj(interp, oBytes, out bytes) ) return TCL.TCL_ERROR;
  3589. rc = sqlite3_bind_text16(pStmt, idx, value, bytes, xDel);
  3590. if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) !=0) return TCL.TCL_ERROR;
  3591. if( rc!=SQLITE_OK ){
  3592. TCL.Tcl_AppendResult(interp, sqlite3TestErrorName(rc));
  3593. return TCL.TCL_ERROR;
  3594. }
  3595. #endif // * SQLITE_OMIT_UTF16 */
  3596. return TCL.TCL_OK;
  3597. }
  3598. /*
  3599. ** Usage: sqlite3_bind_blob ?-static? STMT N DATA BYTES
  3600. **
  3601. ** Test the sqlite3_bind_blob interface. STMT is a prepared statement.
  3602. ** N is the index of a wildcard in the prepared statement. This command
  3603. ** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size.
  3604. */
  3605. static int test_bind_blob(
  3606. object clientdata,
  3607. Tcl_Interp interp,
  3608. int objc,
  3609. Tcl_Obj[] objv
  3610. )
  3611. {
  3612. sqlite3_stmt pStmt = null;
  3613. int idx = 0;
  3614. int bytes = 0;
  3615. byte[] value;
  3616. int rc;
  3617. dxDel xDestructor = SQLITE_TRANSIENT;
  3618. int iObjv = 0;
  3619. if ( objc != 5 && objc != 6 )
  3620. {
  3621. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  3622. TCL.Tcl_GetStringFromObj( objv[0], 0 ), " STMT N DATA BYTES" );
  3623. return TCL.TCL_ERROR;
  3624. }
  3625. if ( objc == 6 )
  3626. {
  3627. xDestructor = SQLITE_STATIC;
  3628. iObjv++;
  3629. }
  3630. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[iObjv + 1] ), out pStmt ) != 0 )
  3631. return TCL.TCL_ERROR;
  3632. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[iObjv + 2], out idx ) )
  3633. return TCL.TCL_ERROR;
  3634. value = Encoding.UTF8.GetBytes( TCL.Tcl_GetString( objv[iObjv + 3] ) );
  3635. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[iObjv + 4], out bytes ) )
  3636. return TCL.TCL_ERROR;
  3637. rc = sqlite3_bind_blob( pStmt, idx, value, bytes, xDestructor );
  3638. if ( sqlite3TestErrCode( interp, StmtToDb( pStmt ), rc ) != 0 )
  3639. return TCL.TCL_ERROR;
  3640. if ( rc != SQLITE_OK )
  3641. {
  3642. return TCL.TCL_ERROR;
  3643. }
  3644. return TCL.TCL_OK;
  3645. }
  3646. /*
  3647. ** Usage: sqlite3_bind_parameter_count STMT
  3648. **
  3649. ** Return the number of wildcards in the given statement.
  3650. */
  3651. static int test_bind_parameter_count(
  3652. object clientdata,
  3653. Tcl_Interp interp,
  3654. int objc,
  3655. Tcl_Obj[] objv
  3656. )
  3657. {
  3658. sqlite3_stmt pStmt = null;
  3659. if ( objc != 2 )
  3660. {
  3661. TCL.Tcl_WrongNumArgs( interp, 1, objv, "STMT" );
  3662. return TCL.TCL_ERROR;
  3663. }
  3664. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  3665. return TCL.TCL_ERROR;
  3666. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewIntObj( sqlite3_bind_parameter_count( pStmt ) ) );
  3667. return TCL.TCL_OK;
  3668. }
  3669. /*
  3670. ** Usage: sqlite3_bind_parameter_name STMT N
  3671. **
  3672. ** Return the name of the Nth wildcard. The first wildcard is 1.
  3673. ** An empty string is returned if N is out of range or if the wildcard
  3674. ** is nameless.
  3675. */
  3676. static int test_bind_parameter_name(
  3677. object clientdata,
  3678. Tcl_Interp interp,
  3679. int objc,
  3680. Tcl_Obj[] objv
  3681. )
  3682. {
  3683. sqlite3_stmt pStmt = null;
  3684. int i = 0;
  3685. if ( objc != 3 )
  3686. {
  3687. TCL.Tcl_WrongNumArgs( interp, 1, objv, "STMT N" );
  3688. return TCL.TCL_ERROR;
  3689. }
  3690. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  3691. return TCL.TCL_ERROR;
  3692. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[2], out i ) )
  3693. return TCL.TCL_ERROR;
  3694. TCL.Tcl_SetObjResult( interp,
  3695. TCL.Tcl_NewStringObj( sqlite3_bind_parameter_name( pStmt, i ), -1 )
  3696. );
  3697. return TCL.TCL_OK;
  3698. }
  3699. /*
  3700. ** Usage: sqlite3_bind_parameter_index STMT NAME
  3701. **
  3702. ** Return the index of the wildcard called NAME. Return 0 if there is
  3703. ** no such wildcard.
  3704. */
  3705. static int test_bind_parameter_index(
  3706. object clientdata,
  3707. Tcl_Interp interp,
  3708. int objc,
  3709. Tcl_Obj[] objv
  3710. )
  3711. {
  3712. sqlite3_stmt pStmt = null;
  3713. if ( objc != 3 )
  3714. {
  3715. TCL.Tcl_WrongNumArgs( interp, 1, objv, "STMT NAME" );
  3716. return TCL.TCL_ERROR;
  3717. }
  3718. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  3719. return TCL.TCL_ERROR;
  3720. TCL.Tcl_SetObjResult( interp,
  3721. TCL.Tcl_NewIntObj(
  3722. sqlite3_bind_parameter_index( pStmt, TCL.Tcl_GetString( objv[2] ) )
  3723. )
  3724. );
  3725. return TCL.TCL_OK;
  3726. }
  3727. /*
  3728. ** Usage: sqlite3_clear_bindings STMT
  3729. **
  3730. */
  3731. static int test_clear_bindings(
  3732. object clientdata,
  3733. Tcl_Interp interp,
  3734. int objc,
  3735. Tcl_Obj[] objv
  3736. )
  3737. {
  3738. sqlite3_stmt pStmt = null;
  3739. if ( objc != 2 )
  3740. {
  3741. TCL.Tcl_WrongNumArgs( interp, 1, objv, "STMT" );
  3742. return TCL.TCL_ERROR;
  3743. }
  3744. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  3745. return TCL.TCL_ERROR;
  3746. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewIntObj( sqlite3_clear_bindings( pStmt ) ) );
  3747. return TCL.TCL_OK;
  3748. }
  3749. /*
  3750. ** Usage: sqlite3_sleep MILLISECONDS
  3751. */
  3752. static int test_sleep(
  3753. object clientdata,
  3754. Tcl_Interp interp,
  3755. int objc,
  3756. Tcl_Obj[] objv
  3757. )
  3758. {
  3759. int ms = 0;
  3760. if ( objc != 2 )
  3761. {
  3762. TCL.Tcl_WrongNumArgs( interp, 1, objv, "MILLISECONDS" );
  3763. return TCL.TCL_ERROR;
  3764. }
  3765. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[1], out ms ) )
  3766. {
  3767. return TCL.TCL_ERROR;
  3768. }
  3769. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewIntObj( sqlite3_sleep( ms ) ) );
  3770. return TCL.TCL_OK;
  3771. }
  3772. /*
  3773. ** Usage: sqlite3_extended_errcode DB
  3774. **
  3775. ** Return the string representation of the most recent sqlite3_* API
  3776. ** error code. e.g. "SQLITE_ERROR".
  3777. */
  3778. static int test_ex_errcode(
  3779. object clientdata,
  3780. Tcl_Interp interp,
  3781. int objc,
  3782. Tcl_Obj[] objv
  3783. )
  3784. {
  3785. sqlite3 db = null;
  3786. int rc;
  3787. if ( objc != 2 )
  3788. {
  3789. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  3790. TCL.Tcl_GetString( objv[0] ), " DB" );
  3791. return TCL.TCL_ERROR;
  3792. }
  3793. if ( getDbPointer( interp, TCL.Tcl_GetString( objv[1] ), out db ) != 0 )
  3794. return TCL.TCL_ERROR;
  3795. rc = sqlite3_extended_errcode( db );
  3796. TCL.Tcl_AppendResult( interp, t1ErrorName( rc ) );
  3797. return TCL.TCL_OK;
  3798. }
  3799. /*
  3800. ** Usage: sqlite3_errcode DB
  3801. **
  3802. ** Return the string representation of the most recent sqlite3_* API
  3803. ** error code. e.g. "SQLITE_ERROR".
  3804. */
  3805. static int test_errcode(
  3806. object clientdata,
  3807. Tcl_Interp interp,
  3808. int objc,
  3809. Tcl_Obj[] objv
  3810. )
  3811. {
  3812. sqlite3 db = null;
  3813. int rc;
  3814. StringBuilder zBuf = new StringBuilder( 50 );
  3815. if ( objc != 2 )
  3816. {
  3817. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  3818. TCL.Tcl_GetString( objv[0] ), " DB" );
  3819. return TCL.TCL_ERROR;
  3820. }
  3821. if ( getDbPointer( interp, TCL.Tcl_GetString( objv[1] ), out db ) != 0 )
  3822. return TCL.TCL_ERROR;
  3823. rc = sqlite3_errcode( db );
  3824. if ( ( rc & 0xff ) == rc )
  3825. {
  3826. zBuf.Length = 0;
  3827. }
  3828. else
  3829. {
  3830. sqlite3_snprintf( 30, zBuf, "+%d", rc >> 8 );
  3831. }
  3832. TCL.Tcl_AppendResult( interp, t1ErrorName( rc ), zBuf );
  3833. return TCL.TCL_OK;
  3834. }
  3835. /*
  3836. ** Usage: sqlite3_errmsg DB
  3837. **
  3838. ** Returns the UTF-8 representation of the error message string for the
  3839. ** most recent sqlite3_* API call.
  3840. */
  3841. static int test_errmsg(
  3842. object clientdata,
  3843. Tcl_Interp interp,
  3844. int objc,
  3845. Tcl_Obj[] objv
  3846. )
  3847. {
  3848. sqlite3 db = null;
  3849. string zErr;
  3850. if ( objc != 2 )
  3851. {
  3852. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  3853. TCL.Tcl_GetString( objv[0] ), " DB" );
  3854. return TCL.TCL_ERROR;
  3855. }
  3856. if ( getDbPointer( interp, TCL.Tcl_GetString( objv[1] ), out db ) != 0 )
  3857. return TCL.TCL_ERROR;
  3858. zErr = sqlite3_errmsg( db );
  3859. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewStringObj( zErr, -1 ) );
  3860. return TCL.TCL_OK;
  3861. }
  3862. /*
  3863. ** Usage: test_errmsg16 DB
  3864. **
  3865. ** Returns the UTF-16 representation of the error message string for the
  3866. ** most recent sqlite3_* API call. This is a byte array object at the TCL
  3867. ** level, and it includes the 0x00 0x00 terminator bytes at the end of the
  3868. ** UTF-16 string.
  3869. */
  3870. //static int test_errmsg16(
  3871. // object clientdata,
  3872. // Tcl_Interp interp,
  3873. // int objc,
  3874. // Tcl_Obj[] objv
  3875. //){
  3876. #if !SQLITE_OMIT_UTF16
  3877. sqlite3 db;
  3878. string zErr;
  3879. string z;
  3880. int bytes = 0;
  3881. if( objc!=2 ){
  3882. TCL.Tcl_AppendResult(interp, "wrong # args: should be \"",
  3883. TCL.Tcl_GetString(objv[0]), " DB", 0);
  3884. return TCL.TCL_ERROR;
  3885. }
  3886. if( getDbPointer(interp, TCL.Tcl_GetString(objv[1]), &db) ) return TCL.TCL_ERROR;
  3887. zErr = sqlite3_errmsg16(db);
  3888. if( zErr ){
  3889. z = zErr;
  3890. for(bytes=0; z[bytes] || z[bytes+1]; bytes+=2){}
  3891. }
  3892. TCL.Tcl_SetObjResult(interp, TCL.Tcl_NewByteArrayObj(zErr, bytes));
  3893. #endif // * SQLITE_OMIT_UTF16 */
  3894. // return TCL.TCL_OK;
  3895. //}
  3896. /*
  3897. ** Usage: sqlite3_prepare DB sql bytes ?tailvar?
  3898. **
  3899. ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
  3900. ** database handle <DB>. The parameter <tailval> is the name of a global
  3901. ** variable that is set to the unused portion of <sql> (if any). A
  3902. ** STMT handle is returned.
  3903. */
  3904. static int test_prepare(
  3905. object clientdata,
  3906. Tcl_Interp interp,
  3907. int objc,
  3908. Tcl_Obj[] objv
  3909. )
  3910. {
  3911. sqlite3 db = null;
  3912. string zSql;
  3913. int bytes = 0;
  3914. string zTail = "";
  3915. sqlite3_stmt pStmt = null;
  3916. StringBuilder zBuf = new StringBuilder( 50 );
  3917. int rc;
  3918. if ( objc != 5 && objc != 4 )
  3919. {
  3920. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  3921. TCL.Tcl_GetString( objv[0] ), " DB sql bytes ?tailvar?" );
  3922. return TCL.TCL_ERROR;
  3923. }
  3924. if ( getDbPointer( interp, TCL.Tcl_GetString( objv[1] ), out db ) != 0 )
  3925. return TCL.TCL_ERROR;
  3926. zSql = TCL.Tcl_GetString( objv[2] );
  3927. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[3], out bytes ) )
  3928. return TCL.TCL_ERROR;
  3929. if ( bytes > zSql.Length )
  3930. bytes = zSql.Length;
  3931. rc = sqlite3_prepare( db, zSql, bytes, ref pStmt, ref zTail );
  3932. TCL.Tcl_ResetResult( interp );
  3933. if ( sqlite3TestErrCode( interp, db, rc ) != 0 )
  3934. return TCL.TCL_ERROR;
  3935. if ( zTail != null && objc >= 5 )
  3936. {
  3937. if ( bytes >= 0 )
  3938. {
  3939. bytes = bytes - zSql.Length - zTail.Length;// ( zTail - zSql );
  3940. }
  3941. if ( zTail.Length < bytes )
  3942. {
  3943. bytes = zTail.Length;
  3944. }
  3945. TCL.Tcl_ObjSetVar2( interp, objv[4], null, TCL.Tcl_NewStringObj( zTail, bytes ), 0 );
  3946. }
  3947. if ( rc != SQLITE_OK )
  3948. {
  3949. Debug.Assert( pStmt == null );
  3950. sqlite3_snprintf( 200, zBuf, "(%d) ", rc );
  3951. TCL.Tcl_SetResult( interp, zBuf + sqlite3_errmsg( db ), 0 );
  3952. return TCL.TCL_ERROR;
  3953. }
  3954. if ( pStmt != null )
  3955. {
  3956. if ( sqlite3TestMakePointerStr( interp, zBuf, pStmt ) != 0 )
  3957. return TCL.TCL_ERROR;
  3958. TCL.Tcl_AppendResult( interp, zBuf, null );
  3959. }
  3960. return TCL.TCL_OK;
  3961. }
  3962. /*
  3963. ** Usage: sqlite3_prepare_v2 DB sql bytes ?tailvar?
  3964. **
  3965. ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
  3966. ** database handle <DB>. The parameter <tailval> is the name of a global
  3967. ** variable that is set to the unused portion of <sql> (if any). A
  3968. ** STMT handle is returned.
  3969. */
  3970. static int test_prepare_v2(
  3971. object clientdata,
  3972. Tcl_Interp interp,
  3973. int objc,
  3974. Tcl_Obj[] objv
  3975. )
  3976. {
  3977. sqlite3 db = null;
  3978. string zSql;
  3979. int bytes = 0;
  3980. string zTail = "";
  3981. sqlite3_stmt pStmt = null;
  3982. StringBuilder zBuf = new StringBuilder( 50 );
  3983. int rc;
  3984. if ( objc != 5 && objc != 4 )
  3985. {
  3986. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  3987. TCL.Tcl_GetString( objv[0] ), " DB sql bytes ?tailvar?", null );
  3988. return TCL.TCL_ERROR;
  3989. }
  3990. if ( getDbPointer( interp, TCL.Tcl_GetString( objv[1] ), out db ) != 0 )
  3991. return TCL.TCL_ERROR;
  3992. zSql = TCL.Tcl_GetString( objv[2] );
  3993. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[3], out bytes ) )
  3994. return TCL.TCL_ERROR;
  3995. rc = sqlite3_prepare_v2( db, zSql, bytes, ref pStmt, ref zTail );
  3996. Debug.Assert( rc == SQLITE_OK || pStmt == null );
  3997. TCL.Tcl_ResetResult( interp );
  3998. if ( sqlite3TestErrCode( interp, db, rc ) != 0 )
  3999. return TCL.TCL_ERROR;
  4000. if ( zTail != null && objc >= 5 )
  4001. {
  4002. if ( bytes >= 0 )
  4003. {
  4004. bytes = bytes - zSql.Length - zTail.Length;// ( zTail - zSql );
  4005. }
  4006. TCL.Tcl_ObjSetVar2( interp, objv[4], null, TCL.Tcl_NewStringObj( zTail, bytes ), 0 );
  4007. }
  4008. if ( rc != SQLITE_OK )
  4009. {
  4010. Debug.Assert( pStmt == null );
  4011. sqlite3_snprintf( 50, zBuf, "(%d) ", rc );
  4012. TCL.Tcl_AppendResult( interp, zBuf, sqlite3_errmsg( db ) );
  4013. return TCL.TCL_ERROR;
  4014. }
  4015. if ( pStmt != null )
  4016. {
  4017. if ( sqlite3TestMakePointerStr( interp, zBuf, pStmt ) != 0 )
  4018. return TCL.TCL_ERROR;
  4019. TCL.Tcl_AppendResult( interp, zBuf );
  4020. }
  4021. return TCL.TCL_OK;
  4022. }
  4023. /*
  4024. ** Usage: sqlite3_prepare_tkt3134 DB
  4025. **
  4026. ** Generate a prepared statement for a zero-byte string as a test
  4027. ** for ticket #3134. The string should be preceeded by a zero byte.
  4028. */
  4029. static int test_prepare_tkt3134(
  4030. object clientdata,
  4031. Tcl_Interp interp,
  4032. int objc,
  4033. Tcl_Obj[] objv
  4034. )
  4035. {
  4036. sqlite3 db = null;
  4037. string zSql = "\000SELECT 1";
  4038. sqlite3_stmt pStmt = null;
  4039. StringBuilder zBuf = new StringBuilder( 50 );
  4040. int rc;
  4041. if ( objc != 2 )
  4042. {
  4043. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  4044. TCL.Tcl_GetString( objv[0] ), " DB sql bytes tailvar" );
  4045. return TCL.TCL_ERROR;
  4046. }
  4047. if ( getDbPointer( interp, TCL.Tcl_GetString( objv[1] ), out db ) != 0 )
  4048. return TCL.TCL_ERROR;
  4049. rc = sqlite3_prepare_v2( db, zSql.Substring( 1 ), 0, ref pStmt, 0 );
  4050. Debug.Assert( rc == SQLITE_OK || pStmt == null );
  4051. if ( sqlite3TestErrCode( interp, db, rc ) != 0 )
  4052. return TCL.TCL_ERROR;
  4053. if ( rc != SQLITE_OK )
  4054. {
  4055. Debug.Assert( pStmt == null );
  4056. zBuf.Length = 0;
  4057. zBuf.Append( rc.ToString() ); //sprintf( zBuf, "(%d) ", rc );
  4058. TCL.Tcl_AppendResult( interp, zBuf, sqlite3_errmsg( db ) );
  4059. return TCL.TCL_ERROR;
  4060. }
  4061. if ( pStmt != null )
  4062. {
  4063. if ( sqlite3TestMakePointerStr( interp, zBuf, pStmt ) != 0 )
  4064. return TCL.TCL_ERROR;
  4065. TCL.Tcl_AppendResult( interp, zBuf );
  4066. }
  4067. return TCL.TCL_OK;
  4068. }
  4069. /*
  4070. ** Usage: sqlite3_prepare16 DB sql bytes tailvar
  4071. **
  4072. ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
  4073. ** database handle <DB>. The parameter <tailval> is the name of a global
  4074. ** variable that is set to the unused portion of <sql> (if any). A
  4075. ** STMT handle is returned.
  4076. */
  4077. static int test_prepare16(
  4078. object clientdata,
  4079. Tcl_Interp interp,
  4080. int objc,
  4081. Tcl_Obj[] objv
  4082. )
  4083. {
  4084. #if !SQLITE_OMIT_UTF16
  4085. sqlite3 db=null;
  4086. string zSql;
  4087. string zTail = 0;
  4088. Tcl_Obj pTail = 0;
  4089. sqlite3_stmt pStmt = null;
  4090. char zBuf[50];
  4091. int rc;
  4092. int bytes; /* The integer specified as arg 3 */
  4093. int objlen; /* The byte-array length of arg 2 */
  4094. if( objc!=5 && objc!=4 ){
  4095. TCL.Tcl_AppendResult(interp, "wrong # args: should be \"",
  4096. TCL.Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
  4097. return TCL.TCL_ERROR;
  4098. }
  4099. if( getDbPointer(interp, TCL.Tcl_GetString(objv[1]), out db) !=0) return TCL.TCL_ERROR;
  4100. zSql = TCL.Tcl_GetByteArrayFromObj(objv[2], out objlen);
  4101. if( TCL.Tcl_GetIntFromObj(interp, objv[3], out bytes) ) return TCL.TCL_ERROR;
  4102. rc = sqlite3_prepare16(db, zSql, bytes, pStmt, objc>=5 ? &zTail : 0);
  4103. if( sqlite3TestErrCode(interp, db, rc) ) return TCL.TCL_ERROR;
  4104. if( rc !=0){
  4105. return TCL.TCL_ERROR;
  4106. }
  4107. if( objc>=5 ){
  4108. if( zTail ){
  4109. objlen = objlen - ((u8 )zTail-(u8 )zSql);
  4110. }else{
  4111. objlen = 0;
  4112. }
  4113. pTail = TCL.Tcl_NewByteArrayObj((u8 )zTail, objlen);
  4114. TCL.Tcl_IncrRefCount(pTail);
  4115. Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
  4116. TCL.Tcl_DecrRefCount(pTail);
  4117. }
  4118. if( pStmt ){
  4119. if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL.TCL_ERROR;
  4120. }
  4121. TCL.Tcl_AppendResult(interp, zBuf);
  4122. #endif // * SQLITE_OMIT_UTF16 */
  4123. return TCL.TCL_OK;
  4124. }
  4125. /*
  4126. ** Usage: sqlite3_prepare16_v2 DB sql bytes tailvar
  4127. **
  4128. ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
  4129. ** database handle <DB>. The parameter <tailval> is the name of a global
  4130. ** variable that is set to the unused portion of <sql> (if any). A
  4131. ** STMT handle is returned.
  4132. */
  4133. //static int test_prepare16_v2(
  4134. // object clientdata,
  4135. // Tcl_Interp interp,
  4136. // int objc,
  4137. // Tcl_Obj[] objv
  4138. //){
  4139. #if !SQLITE_OMIT_UTF16
  4140. // sqlite3 db=null;
  4141. // string zSql;
  4142. // string zTail = 0;
  4143. // Tcl_Obj pTail = 0;
  4144. // sqlite3_stmt pStmt = null;
  4145. // char zBuf[50];
  4146. // int rc;
  4147. // int bytes; /* The integer specified as arg 3 */
  4148. // int objlen; /* The byte-array length of arg 2 */
  4149. // if( objc!=5 && objc!=4 ){
  4150. // TCL.Tcl_AppendResult(interp, "wrong # args: should be \"",
  4151. // TCL.Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
  4152. // return TCL.TCL_ERROR;
  4153. // }
  4154. // if( getDbPointer(interp, TCL.Tcl_GetString(objv[1]), out db) !=0) return TCL.TCL_ERROR;
  4155. // zSql = TCL.Tcl_GetByteArrayFromObj(objv[2], out objlen);
  4156. // if( TCL.Tcl_GetIntFromObj(interp, objv[3], out bytes) ) return TCL.TCL_ERROR;
  4157. // rc = sqlite3_prepare16_v2(db, zSql, bytes, pStmt,objc>=5 ? &zTail : 0);
  4158. // if( sqlite3TestErrCode(interp, db, rc) ) return TCL.TCL_ERROR;
  4159. // if( rc !=0){
  4160. // return TCL.TCL_ERROR;
  4161. // }
  4162. if( objc>=5 ){
  4163. if( zTail ){
  4164. objlen = objlen - ((u8 )zTail-(u8 )zSql);
  4165. }else{
  4166. objlen = 0;
  4167. }
  4168. pTail = TCL.Tcl_NewByteArrayObj((u8 )zTail, objlen);
  4169. TCL.Tcl_IncrRefCount(pTail);
  4170. Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
  4171. TCL.Tcl_DecrRefCount(pTail);
  4172. // }
  4173. // if( pStmt ){
  4174. // if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL.TCL_ERROR;
  4175. // }
  4176. // TCL.Tcl_AppendResult(interp, zBuf);
  4177. #endif // * SQLITE_OMIT_UTF16 */
  4178. // return TCL.TCL_OK;
  4179. //}
  4180. /*
  4181. ** Usage: sqlite3_open filename ?options-list?
  4182. */
  4183. static int test_open(
  4184. object clientdata,
  4185. Tcl_Interp interp,
  4186. int objc,
  4187. Tcl_Obj[] objv
  4188. )
  4189. {
  4190. string zFilename;
  4191. SqliteDb db = new SqliteDb();
  4192. int rc;
  4193. StringBuilder zBuf = new StringBuilder( 100 );
  4194. if ( objc != 3 && objc != 2 && objc != 1 )
  4195. {
  4196. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  4197. TCL.Tcl_GetString( objv[0] ), " filename options-list" );
  4198. return TCL.TCL_ERROR;
  4199. }
  4200. zFilename = objc > 1 ? TCL.Tcl_GetString( objv[1] ) : null;
  4201. rc = sqlite3_open( zFilename, out db.db );
  4202. if ( sqlite3TestMakePointerStr( interp, zBuf, db ) != TCL.TCL_OK )
  4203. return TCL.TCL_ERROR;
  4204. TCL.Tcl_AppendResult( interp, zBuf );
  4205. return TCL.TCL_OK;
  4206. }
  4207. class OpenFlag
  4208. {
  4209. public string zFlag;
  4210. public int flag;
  4211. public OpenFlag( string zFlag, int flag )
  4212. {
  4213. this.zFlag = zFlag;
  4214. this.flag = flag;
  4215. }
  4216. }
  4217. /*
  4218. ** Usage: sqlite3_open_v2 FILENAME FLAGS VFS
  4219. */
  4220. static int test_open_v2(
  4221. object clientdata,
  4222. Tcl_Interp interp,
  4223. int objc,
  4224. Tcl_Obj[] objv
  4225. )
  4226. {
  4227. string zFilename;
  4228. string zVfs;
  4229. int flags = 0;
  4230. sqlite3 db = null;
  4231. int rc;
  4232. StringBuilder zBuf = new StringBuilder( 100 );
  4233. int nFlag = 0;
  4234. Tcl_Obj[] apFlag = null;
  4235. int i;
  4236. if ( objc != 4 )
  4237. {
  4238. TCL.Tcl_WrongNumArgs( interp, 1, objv, "FILENAME FLAGS VFS" );
  4239. return TCL.TCL_ERROR;
  4240. }
  4241. zFilename = TCL.Tcl_GetString( objv[1] );
  4242. zVfs = TCL.Tcl_GetString( objv[3] );
  4243. if ( zVfs[0] == 0x00 )
  4244. zVfs = null;
  4245. rc = TCL.Tcl_ListObjGetElements( interp, objv[2], out nFlag, out apFlag ) ? TCL.TCL_OK : 1;
  4246. if ( rc != TCL.TCL_OK )
  4247. return rc;
  4248. for ( i = 0; i < nFlag; i++ )
  4249. {
  4250. int iFlag;
  4251. OpenFlag[] aFlag = new OpenFlag[] {
  4252. new OpenFlag( "SQLITE_OPEN_READONLY", SQLITE_OPEN_READONLY ),
  4253. new OpenFlag( "SQLITE_OPEN_READWRITE", SQLITE_OPEN_READWRITE ),
  4254. new OpenFlag( "SQLITE_OPEN_CREATE", SQLITE_OPEN_CREATE ),
  4255. new OpenFlag( "SQLITE_OPEN_DELETEONCLOSE", SQLITE_OPEN_DELETEONCLOSE ),
  4256. new OpenFlag( "SQLITE_OPEN_EXCLUSIVE", SQLITE_OPEN_EXCLUSIVE ),
  4257. new OpenFlag( "SQLITE_OPEN_AUTOPROXY", SQLITE_OPEN_AUTOPROXY ),
  4258. new OpenFlag( "SQLITE_OPEN_MAIN_DB", SQLITE_OPEN_MAIN_DB ),
  4259. new OpenFlag( "SQLITE_OPEN_TEMP_DB", SQLITE_OPEN_TEMP_DB ),
  4260. new OpenFlag( "SQLITE_OPEN_TRANSIENT_DB", SQLITE_OPEN_TRANSIENT_DB ),
  4261. new OpenFlag( "SQLITE_OPEN_MAIN_JOURNAL", SQLITE_OPEN_MAIN_JOURNAL ),
  4262. new OpenFlag( "SQLITE_OPEN_TEMP_JOURNAL", SQLITE_OPEN_TEMP_JOURNAL ),
  4263. new OpenFlag( "SQLITE_OPEN_SUBJOURNAL", SQLITE_OPEN_SUBJOURNAL ),
  4264. new OpenFlag( "SQLITE_OPEN_MASTER_JOURNAL", SQLITE_OPEN_MASTER_JOURNAL ),
  4265. new OpenFlag( "SQLITE_OPEN_NOMUTEX", SQLITE_OPEN_NOMUTEX ),
  4266. new OpenFlag( "SQLITE_OPEN_FULLMUTEX", SQLITE_OPEN_FULLMUTEX ),
  4267. new OpenFlag( "SQLITE_OPEN_SHAREDCACHE", SQLITE_OPEN_SHAREDCACHE ),
  4268. new OpenFlag( "SQLITE_OPEN_PRIVATECACHE", SQLITE_OPEN_PRIVATECACHE ),
  4269. new OpenFlag( "SQLITE_OPEN_WAL", SQLITE_OPEN_WAL ),
  4270. new OpenFlag( "SQLITE_OPEN_URI", SQLITE_OPEN_URI ),
  4271. new OpenFlag( null, 0 )
  4272. };
  4273. //rc = TCL.Tcl_GetIndexFromObjStruct( interp, apFlag[i], aFlag, sizeof( aFlag[0] ),
  4274. // "flag", 0, ref iFlag );
  4275. for ( iFlag = 0; iFlag < aFlag.Length && ( aFlag[iFlag].zFlag != objv[4].ToString() ); iFlag++ )
  4276. {
  4277. }
  4278. if ( iFlag >= aFlag.Length )
  4279. return TCL.TCL_ERROR;
  4280. flags |= aFlag[iFlag].flag;
  4281. }
  4282. rc = sqlite3_open_v2( zFilename, out db, flags, zVfs );
  4283. if ( sqlite3TestMakePointerStr( interp, zBuf, db ) != 0 )
  4284. return TCL.TCL_ERROR;
  4285. TCL.Tcl_AppendResult( interp, zBuf, 0 );
  4286. return TCL.TCL_OK;
  4287. }
  4288. /*
  4289. ** Usage: sqlite3_open16 filename options
  4290. */
  4291. //static int test_open16(
  4292. // object clientdata,
  4293. // Tcl_Interp interp,
  4294. // int objc,
  4295. // Tcl_Obj[] objv
  4296. //){
  4297. #if !SQLITE_OMIT_UTF16
  4298. // string zFilename;
  4299. // sqlite3 db=null;
  4300. // int rc;
  4301. // char zBuf[100];
  4302. // if( objc!=3 ){
  4303. // TCL.Tcl_AppendResult(interp, "wrong # args: should be \"",
  4304. // TCL.Tcl_GetString(objv[0]), " filename options-list", 0);
  4305. // return TCL.TCL_ERROR;
  4306. // }
  4307. // zFilename = TCL.Tcl_GetByteArrayFromObj(objv[1], 0);
  4308. // rc = sqlite3_open16(zFilename, ref db);
  4309. // if ( sqlite3TestMakePointerStr( interp, zBuf, db ) != TCL.TCL_OK ) return TCL.TCL_ERROR;
  4310. // TCL.Tcl_AppendResult(interp, zBuf);
  4311. #endif // * SQLITE_OMIT_UTF16 */
  4312. // return TCL.TCL_OK;
  4313. //}
  4314. /*
  4315. ** Usage: sqlite3_complete16 <UTF-16 string>
  4316. **
  4317. ** Return 1 if the supplied argument is a complete SQL statement, or zero
  4318. ** otherwise.
  4319. */
  4320. //static int test_complete16(
  4321. // object clientdata,
  4322. // Tcl_Interp interp,
  4323. // int objc,
  4324. // Tcl_Obj[] objv
  4325. //){
  4326. #if !SQLITE_OMIT_COMPLETE && !SQLITE_OMIT_UTF16
  4327. // string zBuf;
  4328. // if( objc!=2 ){
  4329. // TCL.Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>");
  4330. // return TCL.TCL_ERROR;
  4331. // }
  4332. // zBuf = (char)TCL.Tcl_GetByteArrayFromObj(objv[1], 0);
  4333. // TCL.Tcl_SetObjResult(interp, TCL.Tcl_NewIntObj(sqlite3_complete16(zBuf)));
  4334. #endif // * SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */
  4335. // return TCL.TCL_OK;
  4336. //}
  4337. /*
  4338. ** Usage: sqlite3_step STMT
  4339. **
  4340. ** Advance the statement to the next row.
  4341. */
  4342. static int test_step(
  4343. object clientdata,
  4344. Tcl_Interp interp,
  4345. int objc,
  4346. Tcl_Obj[] objv
  4347. )
  4348. {
  4349. sqlite3_stmt pStmt = null;
  4350. int rc;
  4351. if ( objc != 2 )
  4352. {
  4353. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  4354. TCL.Tcl_GetString( objv[0] ), " STMT" );
  4355. return TCL.TCL_ERROR;
  4356. }
  4357. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  4358. return TCL.TCL_ERROR;
  4359. rc = sqlite3_step( pStmt );
  4360. /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL.TCL_ERROR; */
  4361. TCL.Tcl_SetResult( interp, t1ErrorName( rc ), 0 );
  4362. return TCL.TCL_OK;
  4363. }
  4364. static int test_sql(
  4365. object clientdata,
  4366. Tcl_Interp interp,
  4367. int objc,
  4368. Tcl_Obj[] objv
  4369. )
  4370. {
  4371. sqlite3_stmt pStmt = null;
  4372. if ( objc != 2 )
  4373. {
  4374. TCL.Tcl_WrongNumArgs( interp, 1, objv, "STMT" );
  4375. return TCL.TCL_ERROR;
  4376. }
  4377. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  4378. return TCL.TCL_ERROR;
  4379. TCL.Tcl_SetResult( interp, sqlite3_sql( pStmt ), TCL.TCL_VOLATILE );
  4380. return TCL.TCL_OK;
  4381. }
  4382. /*
  4383. ** Usage: sqlite3_column_count STMT
  4384. **
  4385. ** Return the number of columns returned by the sql statement STMT.
  4386. */
  4387. static int test_column_count(
  4388. object clientdata,
  4389. Tcl_Interp interp,
  4390. int objc,
  4391. Tcl_Obj[] objv
  4392. )
  4393. {
  4394. sqlite3_stmt pStmt = null;
  4395. if ( objc != 2 )
  4396. {
  4397. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  4398. TCL.Tcl_GetString( objv[0] ), " STMT column" );
  4399. return TCL.TCL_ERROR;
  4400. }
  4401. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  4402. return TCL.TCL_ERROR;
  4403. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewIntObj( sqlite3_column_count( pStmt ) ) );
  4404. return TCL.TCL_OK;
  4405. }
  4406. /*
  4407. ** Usage: sqlite3_column_type STMT column
  4408. **
  4409. ** Return the type of the data in column 'column' of the current row.
  4410. */
  4411. static int test_column_type(
  4412. object clientdata,
  4413. Tcl_Interp interp,
  4414. int objc,
  4415. Tcl_Obj[] objv
  4416. )
  4417. {
  4418. sqlite3_stmt pStmt = null;
  4419. int col = 0;
  4420. int tp;
  4421. if ( objc != 3 )
  4422. {
  4423. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  4424. TCL.Tcl_GetString( objv[0] ), " STMT column" );
  4425. return TCL.TCL_ERROR;
  4426. }
  4427. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  4428. return TCL.TCL_ERROR;
  4429. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[2], out col ) )
  4430. return TCL.TCL_ERROR;
  4431. tp = sqlite3_column_type( pStmt, col );
  4432. switch ( tp )
  4433. {
  4434. case SQLITE_INTEGER:
  4435. TCL.Tcl_SetResult( interp, "INTEGER", TCL.TCL_STATIC );
  4436. break;
  4437. case SQLITE_NULL:
  4438. TCL.Tcl_SetResult( interp, "NULL", TCL.TCL_STATIC );
  4439. break;
  4440. case SQLITE_FLOAT:
  4441. TCL.Tcl_SetResult( interp, "FLOAT", TCL.TCL_STATIC );
  4442. break;
  4443. case SQLITE_TEXT:
  4444. TCL.Tcl_SetResult( interp, "TEXT", TCL.TCL_STATIC );
  4445. break;
  4446. case SQLITE_BLOB:
  4447. TCL.Tcl_SetResult( interp, "BLOB", TCL.TCL_STATIC );
  4448. break;
  4449. default:
  4450. Debugger.Break();
  4451. break;
  4452. }
  4453. return TCL.TCL_OK;
  4454. }
  4455. /*
  4456. ** Usage: sqlite3_column_int64 STMT column
  4457. **
  4458. ** Return the data in column 'column' of the current row cast as an
  4459. ** wide (64-bit) integer.
  4460. */
  4461. static int test_column_int64(
  4462. object clientdata,
  4463. Tcl_Interp interp,
  4464. int objc,
  4465. Tcl_Obj[] objv
  4466. )
  4467. {
  4468. sqlite3_stmt pStmt = new sqlite3_stmt();
  4469. int col = 0;
  4470. i64 iVal;
  4471. if ( objc != 3 )
  4472. {
  4473. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  4474. TCL.Tcl_GetString( objv[0] ), " STMT column" );
  4475. return TCL.TCL_ERROR;
  4476. }
  4477. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != TCL.TCL_OK )
  4478. return TCL.TCL_ERROR;
  4479. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[2], out col ) )
  4480. return TCL.TCL_ERROR;
  4481. iVal = sqlite3_column_int64( pStmt, col );
  4482. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewWideIntObj( iVal ) );
  4483. return TCL.TCL_OK;
  4484. }
  4485. /*
  4486. ** Usage: sqlite3_column_blob STMT column
  4487. */
  4488. static int test_column_blob(
  4489. object clientdata,
  4490. Tcl_Interp interp,
  4491. int objc,
  4492. Tcl_Obj[] objv
  4493. )
  4494. {
  4495. sqlite3_stmt pStmt = new sqlite3_stmt();
  4496. int col = 0;
  4497. int len;
  4498. byte[] pBlob;
  4499. if ( objc != 3 )
  4500. {
  4501. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  4502. TCL.Tcl_GetString( objv[0] ), " STMT column" );
  4503. return TCL.TCL_ERROR;
  4504. }
  4505. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != TCL.TCL_OK )
  4506. return TCL.TCL_ERROR;
  4507. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[2], out col ) )
  4508. return TCL.TCL_ERROR;
  4509. len = sqlite3_column_bytes( pStmt, col );
  4510. pBlob = sqlite3_column_blob( pStmt, col );
  4511. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewByteArrayObj( pBlob, len ) );
  4512. return TCL.TCL_OK;
  4513. }
  4514. /*
  4515. ** Usage: sqlite3_column_double STMT column
  4516. **
  4517. ** Return the data in column 'column' of the current row cast as a double.
  4518. */
  4519. static int test_column_double(
  4520. object clientdata,
  4521. Tcl_Interp interp,
  4522. int objc,
  4523. Tcl_Obj[] objv
  4524. )
  4525. {
  4526. sqlite3_stmt pStmt = new sqlite3_stmt();
  4527. int col = 0;
  4528. double rVal;
  4529. if ( objc != 3 )
  4530. {
  4531. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  4532. TCL.Tcl_GetString( objv[0] ), " STMT column" );
  4533. return TCL.TCL_ERROR;
  4534. }
  4535. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != TCL.TCL_OK )
  4536. return TCL.TCL_ERROR;
  4537. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[2], out col ) )
  4538. return TCL.TCL_ERROR;
  4539. rVal = sqlite3_column_double( pStmt, col );
  4540. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewDoubleObj( rVal ) );
  4541. return TCL.TCL_OK;
  4542. }
  4543. /*
  4544. ** Usage: sqlite3_data_count STMT
  4545. **
  4546. ** Return the number of columns returned by the sql statement STMT.
  4547. */
  4548. static int test_data_count(
  4549. object clientdata,
  4550. Tcl_Interp interp,
  4551. int objc,
  4552. Tcl_Obj[] objv
  4553. )
  4554. {
  4555. sqlite3_stmt pStmt = null;
  4556. if ( objc != 2 )
  4557. {
  4558. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  4559. TCL.Tcl_GetString( objv[0] ), " STMT column" );
  4560. return TCL.TCL_ERROR;
  4561. }
  4562. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != TCL.TCL_OK )
  4563. return TCL.TCL_ERROR;
  4564. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewIntObj( sqlite3_data_count( pStmt ) ) );
  4565. return TCL.TCL_OK;
  4566. }
  4567. /*
  4568. ** Usage: sqlite3_column_text STMT column
  4569. **
  4570. ** Usage: sqlite3_column_decltype STMT column
  4571. **
  4572. ** Usage: sqlite3_column_name STMT column
  4573. */
  4574. static int test_stmt_utf8(
  4575. object clientdata, /* Pointer to SQLite API function to be invoke */
  4576. Tcl_Interp interp,
  4577. int objc,
  4578. Tcl_Obj[] objv
  4579. )
  4580. {
  4581. sqlite3_stmt pStmt = null;
  4582. int col = 0;
  4583. dxColumn xFunc;//string (*xFunc)(sqlite3_stmt*, int);
  4584. string zRet;
  4585. xFunc = (dxColumn)clientdata; //(string ()(sqlite3_stmt*, int))clientData;
  4586. if ( objc != 3 )
  4587. {
  4588. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  4589. TCL.Tcl_GetString( objv[0] ), " STMT column" );
  4590. return TCL.TCL_ERROR;
  4591. }
  4592. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  4593. return TCL.TCL_ERROR;
  4594. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[2], out col ) )
  4595. return TCL.TCL_ERROR;
  4596. zRet = (string)xFunc( pStmt, col );
  4597. if ( zRet != null )
  4598. {
  4599. TCL.Tcl_SetResult( interp, zRet, 0 );
  4600. }
  4601. return TCL.TCL_OK;
  4602. }
  4603. static int test_global_recover(
  4604. object clientdata,
  4605. Tcl_Interp interp,
  4606. int objc,
  4607. Tcl_Obj[] objv
  4608. )
  4609. {
  4610. #if !SQLITE_OMIT_DEPRECATED
  4611. int rc;
  4612. if ( objc != 1 )
  4613. {
  4614. TCL.Tcl_WrongNumArgs( interp, 1, objv, "" );
  4615. return TCL.TCL_ERROR;
  4616. }
  4617. rc = sqlite3_global_recover();
  4618. TCL.Tcl_SetResult( interp, t1ErrorName( rc ), TCL.TCL_STATIC );
  4619. #else
  4620. TCL.Tcl_SetResult( interp, t1ErrorName( SQLITE_OK ), TCL.TCL_STATIC );
  4621. #endif
  4622. return TCL.TCL_OK;
  4623. }
  4624. /*
  4625. ** Usage: sqlite3_column_text STMT column
  4626. **
  4627. ** Usage: sqlite3_column_decltype STMT column
  4628. **
  4629. ** Usage: sqlite3_column_name STMT column
  4630. */
  4631. //static int test_stmt_utf16(
  4632. // object clientdata, /* Pointer to SQLite API function to be invoked */
  4633. // Tcl_Interp interp,
  4634. // int objc,
  4635. // Tcl_Obj[] objv
  4636. //){
  4637. #if !SQLITE_OMIT_UTF16
  4638. // sqlite3_stmt pStmt;
  4639. // int col;
  4640. // Tcl_Obj pRet;
  4641. // string zName16;
  4642. // const void *(*xFunc)(sqlite3_stmt*, int);
  4643. xFunc = (dxColumn)clientdata; //(string ()(sqlite3_stmt*, int))clientData;
  4644. // if( objc!=3 ){
  4645. // TCL.Tcl_AppendResult(interp, "wrong # args: should be \"",
  4646. // TCL.Tcl_GetString(objv[0]), " STMT column", 0);
  4647. // return TCL.TCL_ERROR;
  4648. // }
  4649. // if( getStmtPointer(interp, TCL.Tcl_GetString(objv[1]), pStmt) ) return TCL.TCL_ERROR;
  4650. // if( TCL.Tcl_GetIntFromObj(interp, objv[2], out col) ) return TCL.TCL_ERROR;
  4651. // zName16 = xFunc(pStmt, col);
  4652. // if( zName16 ){
  4653. int n;
  4654. string z = zName16;
  4655. for(n=0; z[n] || z[n+1]; n+=2){}
  4656. pRet = TCL.Tcl_NewByteArrayObj(zName16, n+2);
  4657. // TCL.Tcl_SetObjResult(interp, pRet);
  4658. // }
  4659. #endif // * SQLITE_OMIT_UTF16 */
  4660. // return TCL.TCL_OK;
  4661. //}
  4662. /*
  4663. ** Usage: sqlite3_column_int STMT column
  4664. **
  4665. ** Usage: sqlite3_column_bytes STMT column
  4666. **
  4667. ** Usage: sqlite3_column_bytes16 STMT column
  4668. **
  4669. */
  4670. static int test_stmt_int(
  4671. object clientdata, /* Pointer to SQLite API function to be invoked */
  4672. Tcl_Interp interp,
  4673. int objc,
  4674. Tcl_Obj[] objv
  4675. )
  4676. {
  4677. sqlite3_stmt pStmt = null;
  4678. int col = 0;
  4679. dxColumn_I xFunc;//(sqlite3_stmt*, int) ;
  4680. xFunc = (dxColumn_I)clientdata; //(int ()(sqlite3_stmt*, int))clientData;
  4681. if ( objc != 3 )
  4682. {
  4683. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  4684. TCL.Tcl_GetString( objv[0] ), " STMT column" );
  4685. return TCL.TCL_ERROR;
  4686. }
  4687. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  4688. return TCL.TCL_ERROR;
  4689. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[2], out col ) )
  4690. return TCL.TCL_ERROR;
  4691. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewIntObj( xFunc( pStmt, col ) ) );
  4692. return TCL.TCL_OK;
  4693. }
  4694. /*
  4695. ** Usage: sqlite_set_magic DB MAGIC-NUMBER
  4696. **
  4697. ** Set the db.magic value. This is used to test error recovery logic.
  4698. */
  4699. static int sqlite_set_magic(
  4700. object clientdata, /* Pointer to SQLite API function to be invoked */
  4701. Tcl_Interp interp,
  4702. int argc,
  4703. Tcl_Obj[] argv
  4704. )
  4705. {
  4706. sqlite3 db = null;
  4707. if ( argc != 3 )
  4708. {
  4709. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  4710. " DB MAGIC" );
  4711. return TCL.TCL_ERROR;
  4712. }
  4713. if ( getDbPointer( interp, argv[1].ToString(), out db ) != 0 )
  4714. return TCL.TCL_ERROR;
  4715. if ( argv[2].ToString() == "SQLITE_MAGIC_OPEN" )
  4716. {
  4717. db.magic = SQLITE_MAGIC_OPEN;
  4718. }
  4719. else if ( argv[2].ToString() == "SQLITE_MAGIC_CLOSED" )
  4720. {
  4721. db.magic = SQLITE_MAGIC_CLOSED;
  4722. }
  4723. else if ( argv[2].ToString() == "SQLITE_MAGIC_BUSY" )
  4724. {
  4725. db.magic = SQLITE_MAGIC_BUSY;
  4726. }
  4727. else if ( argv[2].ToString() == "SQLITE_MAGIC_ERROR" )
  4728. {
  4729. db.magic = SQLITE_MAGIC_ERROR;
  4730. }
  4731. else if ( TCL.Tcl_GetInt( interp, argv[2], out db.magic ) )
  4732. {
  4733. return TCL.TCL_ERROR;
  4734. }
  4735. return TCL.TCL_OK;
  4736. }
  4737. /*
  4738. ** Usage: sqlite3_interrupt DB
  4739. **
  4740. ** Trigger an interrupt on DB
  4741. */
  4742. static int test_interrupt(
  4743. object clientdata,
  4744. Tcl_Interp interp,
  4745. int argc,
  4746. Tcl_Obj[] argv )
  4747. {
  4748. sqlite3 db = null;
  4749. if ( argc != 2 )
  4750. {
  4751. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0], " DB" );
  4752. return TCL.TCL_ERROR;
  4753. }
  4754. if ( getDbPointer( interp, argv[1].ToString(), out db ) != 0 )
  4755. return TCL.TCL_ERROR;
  4756. sqlite3_interrupt( db );
  4757. return TCL.TCL_OK;
  4758. }
  4759. //static u8 *sqlite3_stack_baseline = 0;
  4760. /*
  4761. ** Fill the stack with a known bitpattern.
  4762. */
  4763. //static void prepStack(void){
  4764. // int i;
  4765. // u32 bigBuf[65536];
  4766. // for(i=0; i<sizeof(bigBuf); i++) bigBuf[i] = 0xdeadbeef;
  4767. // sqlite3_stack_baseline = (u8)&bigBuf[65536];
  4768. //}
  4769. /*
  4770. ** Get the current stack depth. Used for debugging only.
  4771. */
  4772. //u64 sqlite3StackDepth(void){
  4773. // u8 x;
  4774. // return (u64)(sqlite3_stack_baseline - &x);
  4775. //}
  4776. /*
  4777. ** Usage: sqlite3_stack_used DB SQL
  4778. **
  4779. ** Try to measure the amount of stack space used by a call to sqlite3_exec
  4780. */
  4781. //static int test_stack_used(
  4782. // object clientdata,
  4783. // Tcl_Interp interp,
  4784. // int argc,
  4785. // char **argv
  4786. //){
  4787. // sqlite3 db=null;
  4788. // int i;
  4789. // if( argc!=3 ){
  4790. // TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  4791. // " DB SQL", 0);
  4792. // return TCL.TCL_ERROR;
  4793. // }
  4794. // if( getDbPointer(interp, argv[1].ToString(), out db) !=0) return TCL.TCL_ERROR;
  4795. // prepStack();
  4796. // (void)sqlite3_exec(db, argv[2], 0, 0, 0);
  4797. // for(i=65535; i>=0 && ((u32)sqlite3_stack_baseline)[-i]==0xdeadbeef; i--){}
  4798. // TCL.Tcl_SetObjResult(interp, TCL.Tcl_NewIntObj(i*4));
  4799. // return TCL.TCL_OK;
  4800. //}
  4801. /*
  4802. ** Usage: sqlite_delete_function DB function-name
  4803. **
  4804. ** Delete the user function 'function-name' from database handle DB. It
  4805. ** is assumed that the user function was created as UTF8, any number of
  4806. ** arguments (the way the TCL interface does it).
  4807. */
  4808. static int delete_function(
  4809. object clientdata,
  4810. Tcl_Interp interp,
  4811. int argc,
  4812. Tcl_Obj[] argv
  4813. )
  4814. {
  4815. int rc;
  4816. sqlite3 db = null;
  4817. if ( argc != 3 )
  4818. {
  4819. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  4820. " DB function-name", null );
  4821. return TCL.TCL_ERROR;
  4822. }
  4823. if ( getDbPointer( interp, argv[1].ToString(), out db ) != 0 )
  4824. return TCL.TCL_ERROR;
  4825. rc = sqlite3_create_function( db, argv[2].ToString(), -1, SQLITE_UTF8, null, null, null, null );
  4826. TCL.Tcl_SetResult( interp, t1ErrorName( rc ), TCL.TCL_STATIC );
  4827. return TCL.TCL_OK;
  4828. }
  4829. /*
  4830. ** Usage: sqlite_delete_collation DB collation-name
  4831. **
  4832. ** Delete the collation sequence 'collation-name' from database handle
  4833. ** DB. It is assumed that the collation sequence was created as UTF8 (the
  4834. ** way the TCL interface does it).
  4835. */
  4836. static int delete_collation(
  4837. object clientdata,
  4838. Tcl_Interp interp,
  4839. int argc,
  4840. Tcl_Obj[] argv
  4841. )
  4842. {
  4843. int rc;
  4844. sqlite3 db = null;
  4845. if ( argc != 3 )
  4846. {
  4847. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  4848. " DB function-name", null );
  4849. return TCL.TCL_ERROR;
  4850. }
  4851. if ( getDbPointer( interp, argv[1].ToString(), out db ) != 0 )
  4852. return TCL.TCL_ERROR;
  4853. rc = sqlite3_create_collation( db, argv[2].ToString(), SQLITE_UTF8, null, null );
  4854. TCL.Tcl_SetResult( interp, t1ErrorName( rc ), TCL.TCL_STATIC );
  4855. return TCL.TCL_OK;
  4856. }
  4857. /*
  4858. ** Usage: sqlite3_get_autocommit DB
  4859. **
  4860. ** Return true if the database DB is currently in auto-commit mode.
  4861. ** Return false if not.
  4862. */
  4863. static int get_autocommit(
  4864. object clientdata,
  4865. Tcl_Interp interp,
  4866. int argc,
  4867. Tcl_Obj[] argv
  4868. )
  4869. {
  4870. StringBuilder zBuf = new StringBuilder( 30 );
  4871. sqlite3 db = null;
  4872. if ( argc != 2 )
  4873. {
  4874. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  4875. " DB" );
  4876. return TCL.TCL_ERROR;
  4877. }
  4878. if ( getDbPointer( interp, argv[1].ToString(), out db ) != 0 )
  4879. return TCL.TCL_ERROR;
  4880. sqlite3_snprintf( 30, zBuf, "%d", sqlite3_get_autocommit( db ) );
  4881. TCL.Tcl_AppendResult( interp, zBuf );
  4882. return TCL.TCL_OK;
  4883. }
  4884. /*
  4885. ** Usage: sqlite3_busy_timeout DB MS
  4886. **
  4887. ** Set the busy timeout. This is more easily done using the timeout
  4888. ** method of the TCL interface. But we need a way to test the case
  4889. ** where it returns SQLITE_MISUSE.
  4890. */
  4891. static int test_busy_timeout(
  4892. object clientdata,
  4893. Tcl_Interp interp,
  4894. int argc,
  4895. Tcl_Obj[] argv
  4896. )
  4897. {
  4898. int rc, ms = 0;
  4899. sqlite3 db = null;
  4900. if ( argc != 3 )
  4901. {
  4902. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
  4903. " DB" );
  4904. return TCL.TCL_ERROR;
  4905. }
  4906. if ( getDbPointer( interp, argv[1].ToString(), out db ) != 0 )
  4907. return TCL.TCL_ERROR;
  4908. if ( TCL.Tcl_GetInt( interp, argv[2], out ms ) )
  4909. return TCL.TCL_ERROR;
  4910. rc = sqlite3_busy_timeout( db, ms );
  4911. TCL.Tcl_AppendResult( interp, sqlite3TestErrorName( rc ) );
  4912. return TCL.TCL_OK;
  4913. }
  4914. /*
  4915. ** Usage: tcl_variable_type VARIABLENAME
  4916. **
  4917. ** Return the name of the internal representation for the
  4918. ** value of the given variable.
  4919. */
  4920. static int tcl_variable_type(
  4921. object clientdata,
  4922. Tcl_Interp interp,
  4923. int objc,
  4924. Tcl_Obj[] objv
  4925. )
  4926. {
  4927. Tcl_Obj pVar;
  4928. if ( objc != 2 )
  4929. {
  4930. TCL.Tcl_WrongNumArgs( interp, 1, objv, "VARIABLE" );
  4931. return TCL.TCL_ERROR;
  4932. }
  4933. pVar = TCL.Tcl_GetVar2Ex( interp, TCL.Tcl_GetString( objv[1] ), null, (TCL.VarFlag)TCL.TCL_LEAVE_ERR_MSG );
  4934. if ( pVar == null )
  4935. return TCL.TCL_ERROR;
  4936. if ( pVar.typePtr != "" )
  4937. {
  4938. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewStringObj( pVar.typePtr, -1 ) );
  4939. }
  4940. return TCL.TCL_OK;
  4941. }
  4942. /*
  4943. ** Usage: sqlite3_release_memory ?N?
  4944. **
  4945. ** Attempt to release memory currently held but not actually required.
  4946. ** The integer N is the number of bytes we are trying to release. The
  4947. ** return value is the amount of memory actually released.
  4948. */
  4949. static int test_release_memory(
  4950. object clientdata,
  4951. Tcl_Interp interp,
  4952. int objc,
  4953. Tcl_Obj[] objv
  4954. )
  4955. {
  4956. #if SQLITE_ENABLE_MEMORY_MANAGEMENT && !SQLITE_OMIT_DISKIO
  4957. int N;
  4958. int amt;
  4959. if( objc!=1 && objc!=2 ){
  4960. TCL.Tcl_WrongNumArgs(interp, 1, objv, "?N?");
  4961. return TCL.TCL_ERROR;
  4962. }
  4963. if( objc==2 ){
  4964. if( TCL.Tcl_GetIntFromObj(interp, objv[1], out N) ) return TCL.TCL_ERROR;
  4965. }else{
  4966. N = -1;
  4967. }
  4968. amt = sqlite3_release_memory(N);
  4969. TCL.Tcl_SetObjResult(interp, TCL.Tcl_NewIntObj(amt));
  4970. #endif
  4971. return TCL.TCL_OK;
  4972. }
  4973. /*
  4974. ** Usage: sqlite3_soft_heap_limit ?N?
  4975. **
  4976. ** Query or set the soft heap limit for the current thread. The
  4977. ** limit is only changed if the N is present. The previous limit
  4978. ** is returned.
  4979. */
  4980. static int test_soft_heap_limit(
  4981. object clientdata,
  4982. Tcl_Interp interp,
  4983. int objc,
  4984. Tcl_Obj[] objv
  4985. )
  4986. {
  4987. sqlite3_int64 amt;
  4988. sqlite3_int64 N = -1;
  4989. if ( objc != 1 && objc != 2 )
  4990. {
  4991. TCL.Tcl_WrongNumArgs( interp, 1, objv, "?N?" );
  4992. return TCL.TCL_ERROR;
  4993. }
  4994. if ( objc == 2 )
  4995. {
  4996. if ( TCL.Tcl_GetWideIntFromObj( interp, objv[1], out N ) )
  4997. return TCL.TCL_ERROR;
  4998. }
  4999. amt = sqlite3_soft_heap_limit64( N );
  5000. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewWideIntObj( amt ) );
  5001. return TCL.TCL_OK;
  5002. }
  5003. /*
  5004. ** Usage: sqlite3_thread_cleanup
  5005. **
  5006. ** Call the sqlite3_thread_cleanup API.
  5007. */
  5008. //static int test_thread_cleanup(
  5009. // object clientdata,
  5010. // Tcl_Interp interp,
  5011. // int objc,
  5012. // Tcl_Obj[] objv
  5013. //){
  5014. // sqlite3_thread_cleanup();
  5015. // return TCL.TCL_OK;
  5016. //}
  5017. /*
  5018. ** Usage: sqlite3_pager_refcounts DB
  5019. **
  5020. ** Return a list of numbers which are the PagerRefcount for all
  5021. ** pagers on each database connection.
  5022. */
  5023. //static int test_pager_refcounts(
  5024. // object clientdata,
  5025. // Tcl_Interp interp,
  5026. // int objc,
  5027. // Tcl_Obj[] objv
  5028. //){
  5029. // sqlite3 db=null;
  5030. // int i;
  5031. // int v, *a;
  5032. // Tcl_Obj pResult;
  5033. // if( objc!=2 ){
  5034. // TCL.Tcl_AppendResult(interp, "wrong # args: should be \"",
  5035. // TCL.Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
  5036. // return TCL.TCL_ERROR;
  5037. // }
  5038. // if( getDbPointer(interp, TCL.Tcl_GetString(objv[1]), out db) !=0) return TCL.TCL_ERROR;
  5039. // pResult = TCL.Tcl_NewObj();
  5040. // for(i=0; i<db.nDb; i++){
  5041. // if( db.aDb[i].pBt==null ){
  5042. // v = -1;
  5043. // }else{
  5044. // sqlite3_mutex_enter(db.mutex);
  5045. // a = sqlite3PagerStats(sqlite3BtreePager(db.aDb[i].pBt));
  5046. // v = a[0];
  5047. // sqlite3_mutex_leave(db.mutex);
  5048. // }
  5049. // TCL.Tcl_ListObjAppendElement(0, pResult, TCL.Tcl_NewIntObj(v));
  5050. // }
  5051. // TCL.Tcl_SetObjResult(interp, pResult);
  5052. // return TCL.TCL_OK;
  5053. //}
  5054. /*
  5055. ** tclcmd: working_64bit_int
  5056. **
  5057. ** Some TCL builds (ex: cygwin) do not support 64-bit integers. This
  5058. ** leads to a number of test failures. The present command checks the
  5059. ** TCL build to see whether or not it supports 64-bit integers. It
  5060. ** returns TRUE if it does and FALSE if not.
  5061. **
  5062. ** This command is used to warn users that their TCL build is defective
  5063. ** and that the errors they are seeing in the test scripts might be
  5064. ** a result of their defective TCL rather than problems in SQLite.
  5065. */
  5066. static int working_64bit_int(
  5067. object clientdata, /* Pointer to sqlite3_enable_XXX function */
  5068. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  5069. int objc, /* Number of arguments */
  5070. Tcl_Obj[] objv /* Command arguments */
  5071. )
  5072. {
  5073. Tcl_Obj pTestObj;
  5074. int working = 0;
  5075. pTestObj = TCL.Tcl_NewWideIntObj( 1000000 * (i64)1234567890 );
  5076. working = ( TCL.Tcl_GetString( pTestObj ) == "1234567890000000" ) ? 1 : 0;
  5077. TCL.Tcl_DecrRefCount( ref pTestObj );
  5078. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewBooleanObj( working ) );
  5079. return TCL.TCL_OK;
  5080. }
  5081. /*
  5082. ** tclcmd: vfs_unlink_test
  5083. **
  5084. ** This TCL command unregisters the primary VFS and then registers
  5085. ** it back again. This is used to test the ability to register a
  5086. ** VFS when none are previously registered, and the ability to
  5087. ** unregister the only available VFS. Ticket #2738
  5088. */
  5089. static int vfs_unlink_test(
  5090. object clientdata, /* Pointer to sqlite3_enable_XXX function */
  5091. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  5092. int objc, /* Number of arguments */
  5093. Tcl_Obj[] objv /* Command arguments */
  5094. )
  5095. {
  5096. int i;
  5097. sqlite3_vfs pMain;
  5098. sqlite3_vfs one = new sqlite3_vfs();
  5099. sqlite3_vfs two = new sqlite3_vfs();
  5100. sqlite3_vfs_unregister( null ); /* Unregister of NULL is harmless */
  5101. one.zName = "__one";
  5102. two.zName = "__two";
  5103. /* Calling sqlite3_vfs_register with 2nd argument of 0 does not
  5104. ** change the default VFS
  5105. */
  5106. pMain = sqlite3_vfs_find( null );
  5107. sqlite3_vfs_register( one, 0 );
  5108. Debug.Assert( pMain == null || pMain == sqlite3_vfs_find( null ) );
  5109. sqlite3_vfs_register( two, 0 );
  5110. Debug.Assert( pMain == null || pMain == sqlite3_vfs_find( null ) );
  5111. /* We can find a VFS by its name */
  5112. Debug.Assert( sqlite3_vfs_find( "__one" ) == one );
  5113. Debug.Assert( sqlite3_vfs_find( "__two" ) == two );
  5114. /* Calling sqlite_vfs_register with non-zero second parameter changes the
  5115. ** default VFS, even if the 1st parameter is an existig VFS that is
  5116. ** previously registered as the non-default.
  5117. */
  5118. sqlite3_vfs_register( one, 1 );
  5119. Debug.Assert( sqlite3_vfs_find( "__one" ) == one );
  5120. Debug.Assert( sqlite3_vfs_find( "__two" ) == two );
  5121. Debug.Assert( sqlite3_vfs_find( null ) == one );
  5122. sqlite3_vfs_register( two, 1 );
  5123. Debug.Assert( sqlite3_vfs_find( "__one" ) == one );
  5124. Debug.Assert( sqlite3_vfs_find( "__two" ) == two );
  5125. Debug.Assert( sqlite3_vfs_find( null ) == two );
  5126. if ( pMain != null )
  5127. {
  5128. sqlite3_vfs_register( pMain, 1 );
  5129. Debug.Assert( sqlite3_vfs_find( "__one" ) == one );
  5130. Debug.Assert( sqlite3_vfs_find( "__two" ) == two );
  5131. Debug.Assert( sqlite3_vfs_find( null ) == pMain );
  5132. }
  5133. /* Unlink the default VFS. Repeat until there are no more VFSes
  5134. ** registered.
  5135. */
  5136. for ( i = 0; i < apVfs.Length; i++ )
  5137. {//sizeof(apVfs)/sizeof(apVfs[0]); i++){
  5138. apVfs[i] = sqlite3_vfs_find( null );
  5139. if ( apVfs[i] != null )
  5140. {
  5141. Debug.Assert( apVfs[i] == sqlite3_vfs_find( apVfs[i].zName ) );
  5142. sqlite3_vfs_unregister( apVfs[i] );
  5143. Debug.Assert( null == sqlite3_vfs_find( apVfs[i].zName ) );
  5144. }
  5145. }
  5146. Debug.Assert( null == sqlite3_vfs_find( null ) );
  5147. /* Register the main VFS as non-default (will be made default, since
  5148. ** it'll be the only one in existence).
  5149. */
  5150. sqlite3_vfs_register( pMain, 0 );
  5151. Debug.Assert( sqlite3_vfs_find( null ) == pMain );
  5152. /* Un-register the main VFS again to restore an empty VFS list */
  5153. sqlite3_vfs_unregister( pMain );
  5154. Debug.Assert( null == sqlite3_vfs_find( null ) );
  5155. /* Relink all VFSes in reverse order. */
  5156. for ( i = apVfs.Length - 1; i >= 0; i-- )
  5157. {//sizeof(apVfs)/sizeof(apVfs[0])-1; i>=0; i--){
  5158. if ( apVfs[i] != null )
  5159. {
  5160. sqlite3_vfs_register( apVfs[i], 1 );
  5161. Debug.Assert( apVfs[i] == sqlite3_vfs_find( null ) );
  5162. Debug.Assert( apVfs[i] == sqlite3_vfs_find( apVfs[i].zName ) );
  5163. }
  5164. }
  5165. /* Unregister out sample VFSes. */
  5166. sqlite3_vfs_unregister( one );
  5167. sqlite3_vfs_unregister( two );
  5168. /* Unregistering a VFS that is not currently registered is harmless */
  5169. sqlite3_vfs_unregister( one );
  5170. sqlite3_vfs_unregister( two );
  5171. Debug.Assert( sqlite3_vfs_find( "__one" ) == null );
  5172. Debug.Assert( sqlite3_vfs_find( "__two" ) == null );
  5173. /* We should be left with the original default VFS back as the
  5174. ** original */
  5175. Debug.Assert( sqlite3_vfs_find( null ) == pMain );
  5176. return TCL.TCL_OK;
  5177. }
  5178. /*
  5179. ** tclcmd: vfs_initfail_test
  5180. **
  5181. ** This TCL command attempts to vfs_find and vfs_register when the
  5182. ** sqlite3_initialize() interface is failing. All calls should fail.
  5183. */
  5184. //static int vfs_initfail_test(
  5185. // ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  5186. // Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  5187. // int objc, /* Number of arguments */
  5188. // Tcl_Obj[] objv /* Command arguments */
  5189. //){
  5190. // sqlite3_vfs one;
  5191. // one.zName = "__one";
  5192. // if( sqlite3_vfs_find(0) ) return TCL.TCL_ERROR;
  5193. // sqlite3_vfs_register(&one, 0);
  5194. // if( sqlite3_vfs_find(0) ) return TCL.TCL_ERROR;
  5195. // sqlite3_vfs_register(&one, 1);
  5196. // if( sqlite3_vfs_find(0) ) return TCL.TCL_ERROR;
  5197. // return TCL.TCL_OK;
  5198. //}
  5199. /*
  5200. ** Saved VFSes
  5201. */
  5202. static sqlite3_vfs[] apVfs = new sqlite3_vfs[20];
  5203. static int nVfs = 0;
  5204. /*
  5205. ** tclcmd: vfs_unregister_all
  5206. **
  5207. ** Unregister all VFSes.
  5208. */
  5209. static int vfs_unregister_all(
  5210. object clientdata, /* Pointer to sqlite3_enable_XXX function */
  5211. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  5212. int objc, /* Number of arguments */
  5213. Tcl_Obj[] objv /* Command arguments */
  5214. )
  5215. {
  5216. int i;
  5217. for ( i = 0; i < apVfs.Length; i++ )
  5218. {
  5219. apVfs[i] = sqlite3_vfs_find( null );
  5220. if ( apVfs[i] == null )
  5221. break;
  5222. sqlite3_vfs_unregister( apVfs[i] );
  5223. }
  5224. nVfs = i;
  5225. return TCL.TCL_OK;
  5226. }
  5227. /*
  5228. ** tclcmd: vfs_reregister_all
  5229. **
  5230. ** Restore all VFSes that were removed using vfs_unregister_all
  5231. */
  5232. static int vfs_reregister_all(
  5233. object clientdata, /* Pointer to sqlite3_enable_XXX function */
  5234. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  5235. int objc, /* Number of arguments */
  5236. Tcl_Obj[] objv /* Command arguments */
  5237. )
  5238. {
  5239. int i;
  5240. for ( i = 0; i < nVfs; i++ )
  5241. {
  5242. sqlite3_vfs_register( apVfs[i], i == 0 ? 1 : 0 );
  5243. }
  5244. return TCL.TCL_OK;
  5245. }
  5246. /*
  5247. ** tclcmd: file_control_test DB
  5248. **
  5249. ** This TCL command runs the sqlite3_file_control interface and
  5250. ** verifies correct operation of the same.
  5251. */
  5252. static int file_control_test(
  5253. object clientdata, /* Pointer to sqlite3_enable_XXX function */
  5254. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  5255. int objc, /* Number of arguments */
  5256. Tcl_Obj[] objv /* Command arguments */
  5257. )
  5258. {
  5259. sqlite3_int64 iArg = 0;
  5260. sqlite3 db = null;
  5261. int rc;
  5262. if ( objc != 2 )
  5263. {
  5264. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  5265. TCL.Tcl_GetStringFromObj( objv[0], 0 ), " DB" );
  5266. return TCL.TCL_ERROR;
  5267. }
  5268. if ( getDbPointer( interp, TCL.Tcl_GetString( objv[1] ), out db ) != 0 )
  5269. return TCL.TCL_ERROR;
  5270. rc = sqlite3_file_control( db, null, 0, ref iArg );
  5271. Debug.Assert( rc == SQLITE_NOTFOUND );
  5272. rc = sqlite3_file_control( db, "notadatabase", SQLITE_FCNTL_LOCKSTATE, ref iArg );
  5273. Debug.Assert( rc == SQLITE_ERROR );
  5274. rc = sqlite3_file_control( db, "main", -1, ref iArg );
  5275. Debug.Assert( rc == SQLITE_NOTFOUND );
  5276. rc = sqlite3_file_control( db, "temp", -1, ref iArg );
  5277. Debug.Assert( rc == SQLITE_NOTFOUND || rc == SQLITE_ERROR );
  5278. return TCL.TCL_OK;
  5279. }
  5280. /*
  5281. ** tclcmd: file_control_lasterrno_test DB
  5282. **
  5283. ** This TCL command runs the sqlite3_file_control interface and
  5284. ** verifies correct operation of the SQLITE_LAST_ERRNO verb.
  5285. */
  5286. static int file_control_lasterrno_test(
  5287. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  5288. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  5289. int objc, /* Number of arguments */
  5290. Tcl_Obj[] objv /* Command arguments */
  5291. )
  5292. {
  5293. sqlite3_int64 iArg = 0;
  5294. sqlite3 db = null;
  5295. int rc;
  5296. if ( objc != 2 )
  5297. {
  5298. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  5299. TCL.Tcl_GetStringFromObj( objv[0], 0 ), " DB" );
  5300. return TCL.TCL_ERROR;
  5301. }
  5302. if ( getDbPointer( interp, TCL.Tcl_GetString( objv[1] ), out db ) != 0 )
  5303. {
  5304. return TCL.TCL_ERROR;
  5305. }
  5306. rc = sqlite3_file_control( db, null, SQLITE_LAST_ERRNO, ref iArg );
  5307. if ( rc != 0 )
  5308. {
  5309. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewIntObj( rc ) );
  5310. return TCL.TCL_ERROR;
  5311. }
  5312. if ( iArg != 0 )
  5313. {
  5314. TCL.Tcl_AppendResult( interp, "Unexpected non-zero errno: ", iArg.ToString(), "" );
  5315. //TCL.Tcl_GetStringFromObj(TCL.Tcl_NewIntObj(iArg), 0), " ", 0);
  5316. return TCL.TCL_ERROR;
  5317. }
  5318. return TCL.TCL_OK;
  5319. }
  5320. /*
  5321. ** tclcmd: file_control_chunksize_test DB DBNAME SIZE
  5322. **
  5323. ** This TCL command runs the sqlite3_file_control interface and
  5324. ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
  5325. ** SQLITE_SET_LOCKPROXYFILE verbs.
  5326. */
  5327. static int file_control_chunksize_test(
  5328. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  5329. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  5330. int objc, /* Number of arguments */
  5331. Tcl_Obj[] objv /* Command arguments */
  5332. )
  5333. {
  5334. int nSize = 0; /* New chunk size */
  5335. string zDb; /* Db name ("main", "temp" etc.) */
  5336. sqlite3 db = null; /* Database handle */
  5337. int rc; /* file_control() return code */
  5338. if ( objc != 4 )
  5339. {
  5340. TCL.Tcl_WrongNumArgs( interp, 1, objv, "DB DBNAME SIZE" );
  5341. return TCL.TCL_ERROR;
  5342. }
  5343. if ( getDbPointer( interp, TCL.Tcl_GetString( objv[1] ), out db ) != 0
  5344. || TCL.Tcl_GetIntFromObj( interp, objv[3], out nSize ) != 0
  5345. )
  5346. {
  5347. return TCL.TCL_ERROR;
  5348. }
  5349. zDb = TCL.Tcl_GetString( objv[2] );
  5350. if ( zDb == "" )
  5351. zDb = null;
  5352. i64 iSize = 0;
  5353. rc = sqlite3_file_control( db, zDb, SQLITE_FCNTL_CHUNK_SIZE, ref iSize );
  5354. nSize = (int)iSize;
  5355. if ( rc != 0 )
  5356. {
  5357. TCL.Tcl_SetResult( interp, sqlite3TestErrorName( rc ), TCL.TCL_STATIC );
  5358. return TCL.TCL_ERROR;
  5359. }
  5360. return TCL.TCL_OK;
  5361. }
  5362. /*
  5363. ** tclcmd: file_control_sizehint_test DB DBNAME SIZE
  5364. **
  5365. ** This TCL command runs the sqlite3_file_control interface and
  5366. ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
  5367. ** SQLITE_SET_LOCKPROXYFILE verbs.
  5368. */
  5369. static int file_control_sizehint_test(
  5370. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  5371. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  5372. int objc, /* Number of arguments */
  5373. Tcl_Obj[] objv /* Command arguments */
  5374. )
  5375. {
  5376. sqlite3_int64 nSize = 0; /* Hinted size */
  5377. string zDb; /* Db name ("main", "temp" etc.) */
  5378. sqlite3 db = null; /* Database handle */
  5379. int rc; /* file_control() return code */
  5380. if ( objc != 4 )
  5381. {
  5382. TCL.Tcl_WrongNumArgs( interp, 1, objv, "DB DBNAME SIZE" );
  5383. return TCL.TCL_ERROR;
  5384. }
  5385. if ( getDbPointer( interp, TCL.Tcl_GetString( objv[1] ), out db ) != 0
  5386. || TCL.Tcl_GetWideIntFromObj( interp, objv[3], out nSize )
  5387. )
  5388. {
  5389. return TCL.TCL_ERROR;
  5390. }
  5391. zDb = TCL.Tcl_GetString( objv[2] );
  5392. if ( zDb[0] == '\0' )
  5393. zDb = null;
  5394. rc = sqlite3_file_control( db, zDb, SQLITE_FCNTL_SIZE_HINT, ref nSize );
  5395. if ( rc != 0 )
  5396. {
  5397. TCL.Tcl_SetResult( interp, sqlite3TestErrorName( rc ), TCL.TCL_STATIC );
  5398. return TCL.TCL_ERROR;
  5399. }
  5400. return TCL.TCL_OK;
  5401. }
  5402. /*
  5403. ** tclcmd: file_control_lockproxy_test DB PWD
  5404. **
  5405. ** This TCL command runs the sqlite3_file_control interface and
  5406. ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
  5407. ** SQLITE_SET_LOCKPROXYFILE verbs.
  5408. */
  5409. static int file_control_lockproxy_test(
  5410. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  5411. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  5412. int objc, /* Number of arguments */
  5413. Tcl_Obj[] objv /* Command arguments */
  5414. )
  5415. {
  5416. sqlite3 db = null;
  5417. string zPwd;
  5418. int nPwd = 0;
  5419. if ( objc != 3 )
  5420. {
  5421. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  5422. TCL.Tcl_GetStringFromObj( objv[0], 0 ), " DB PWD" );
  5423. return TCL.TCL_ERROR;
  5424. }
  5425. if ( getDbPointer( interp, TCL.Tcl_GetString( objv[1] ), out db ) != 0 )
  5426. {
  5427. return TCL.TCL_ERROR;
  5428. }
  5429. zPwd = TCL.Tcl_GetStringFromObj( objv[2], out nPwd );
  5430. //#if !(SQLITE_ENABLE_LOCKING_STYLE) && (__APPLE__)
  5431. //{
  5432. // char *testPath;
  5433. // int rc;
  5434. // char proxyPath[400];
  5435. // if( sizeof(proxyPath)<nPwd+20 ){
  5436. // TCL.Tcl_AppendResult(interp, "PWD too big", (void)0);
  5437. // return TCL.TCL_ERROR;
  5438. // }
  5439. // sprintf(proxyPath, "%s/test.proxy", zPwd);
  5440. // rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath);
  5441. // if( rc ){
  5442. // TCL.Tcl_SetObjResult(interp, TCL.Tcl_NewIntObj(rc));
  5443. // return TCL.TCL_ERROR;
  5444. // }
  5445. // rc = sqlite3_file_control(db, NULL, SQLITE_GET_LOCKPROXYFILE, &testPath);
  5446. // if( strncmp(proxyPath,testPath,11) ){
  5447. // TCL.Tcl_AppendResult(interp, "Lock proxy file did not match the "
  5448. // "previously assigned value", 0);
  5449. // return TCL.TCL_ERROR;
  5450. // }
  5451. // if( rc ){
  5452. // TCL.Tcl_SetObjResult(interp, TCL.Tcl_NewIntObj(rc));
  5453. // return TCL.TCL_ERROR;
  5454. // }
  5455. // rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath);
  5456. // if( rc ){
  5457. // TCL.Tcl_SetObjResult(interp, TCL.Tcl_NewIntObj(rc));
  5458. // return TCL.TCL_ERROR;
  5459. // }
  5460. //}
  5461. //#endif
  5462. return TCL.TCL_OK;
  5463. }
  5464. /*
  5465. ** tclcmd: sqlite3_vfs_list
  5466. **
  5467. ** Return a tcl list containing the names of all registered vfs's.
  5468. */
  5469. //static int vfs_list(
  5470. // ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  5471. // Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  5472. // int objc, /* Number of arguments */
  5473. // Tcl_Obj[] objv /* Command arguments */
  5474. //){
  5475. // sqlite3_vfs pVfs;
  5476. // Tcl_Obj pRet = TCL.Tcl_NewObj();
  5477. // if( objc!=1 ){
  5478. // TCL.Tcl_WrongNumArgs(interp, 1, objv, "");
  5479. // return TCL.TCL_ERROR;
  5480. // }
  5481. // for(pVfs=sqlite3_vfs_find(0); pVfs!=null; pVfs=pVfs.pNext){
  5482. // TCL.Tcl_ListObjAppendElement(interp, pRet, TCL.Tcl_NewStringObj(pVfs.zName, -1));
  5483. // }
  5484. // TCL.Tcl_SetObjResult(interp, pRet);
  5485. // return TCL.TCL_OK;
  5486. //}
  5487. /*
  5488. ** tclcmd: sqlite3_limit DB ID VALUE
  5489. **
  5490. ** This TCL command runs the sqlite3_limit interface and
  5491. ** verifies correct operation of the same.
  5492. */
  5493. struct _aID
  5494. {
  5495. public string zName;
  5496. public int id;
  5497. public _aID( string zName, int id )
  5498. {
  5499. this.zName = zName;
  5500. this.id = id;
  5501. }
  5502. }
  5503. static int test_limit(
  5504. object clientdata, /* Pointer to sqlite3_enable_XXX function */
  5505. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  5506. int objc, /* Number of arguments */
  5507. Tcl_Obj[] objv /* Command arguments */
  5508. )
  5509. {
  5510. sqlite3 db = null;
  5511. int rc = 0;
  5512. _aID[] aId = new _aID[] {
  5513. new _aID( "SQLITE_LIMIT_LENGTH", SQLITE_LIMIT_LENGTH ),
  5514. new _aID( "SQLITE_LIMIT_SQL_LENGTH", SQLITE_LIMIT_SQL_LENGTH ),
  5515. new _aID( "SQLITE_LIMIT_COLUMN", SQLITE_LIMIT_COLUMN ),
  5516. new _aID( "SQLITE_LIMIT_EXPR_DEPTH", SQLITE_LIMIT_EXPR_DEPTH ),
  5517. new _aID( "SQLITE_LIMIT_COMPOUND_SELECT", SQLITE_LIMIT_COMPOUND_SELECT ),
  5518. new _aID( "SQLITE_LIMIT_VDBE_OP", SQLITE_LIMIT_VDBE_OP ),
  5519. new _aID( "SQLITE_LIMIT_FUNCTION_ARG", SQLITE_LIMIT_FUNCTION_ARG ),
  5520. new _aID( "SQLITE_LIMIT_ATTACHED", SQLITE_LIMIT_ATTACHED ),
  5521. new _aID( "SQLITE_LIMIT_LIKE_PATTERN_LENGTH", SQLITE_LIMIT_LIKE_PATTERN_LENGTH ),
  5522. new _aID( "SQLITE_LIMIT_VARIABLE_NUMBER", SQLITE_LIMIT_VARIABLE_NUMBER ),
  5523. new _aID( "SQLITE_LIMIT_TRIGGER_DEPTH", SQLITE_LIMIT_TRIGGER_DEPTH ),
  5524. /* Out of range test cases */
  5525. new _aID( "SQLITE_LIMIT_TOOSMALL", -1 ),
  5526. new _aID( "SQLITE_LIMIT_TOOBIG", SQLITE_LIMIT_TRIGGER_DEPTH+1 ),
  5527. };
  5528. int i, id = 0;
  5529. int val = 0;
  5530. string zId;
  5531. if ( objc != 4 )
  5532. {
  5533. TCL.Tcl_AppendResult( interp, "wrong # args: should be \"",
  5534. TCL.Tcl_GetStringFromObj( objv[0], 0 ), " DB ID VALUE" );
  5535. return TCL.TCL_ERROR;
  5536. }
  5537. if ( getDbPointer( interp, TCL.Tcl_GetString( objv[1] ), out db ) != 0 )
  5538. return TCL.TCL_ERROR;
  5539. zId = TCL.Tcl_GetString( objv[2] );
  5540. for ( i = 0; i < aId.Length; i++ )
  5541. {
  5542. if ( zId == aId[i].zName )
  5543. {
  5544. id = aId[i].id;
  5545. break;
  5546. }
  5547. }
  5548. if ( i >= aId.Length )
  5549. {
  5550. TCL.Tcl_AppendResult( interp, "unknown limit type: ", zId, '\0' );
  5551. return TCL.TCL_ERROR;
  5552. }
  5553. if ( TCL.TCL_OK != TCL.Tcl_GetIntFromObj( interp, objv[3], out val ) )
  5554. return TCL.TCL_ERROR;
  5555. rc = sqlite3_limit( db, id, val );
  5556. TCL.Tcl_SetObjResult( interp, TCL.Tcl_NewIntObj( rc ) );
  5557. return TCL.TCL_OK;
  5558. }
  5559. /*
  5560. ** tclcmd: save_prng_state
  5561. **
  5562. ** Save the state of the pseudo-random number generator.
  5563. ** At the same time, verify that sqlite3_test_control works even when
  5564. ** called with an out-of-range opcode.
  5565. */
  5566. static int save_prng_state(
  5567. object clientdata, /* Pointer to sqlite3_enable_XXX function */
  5568. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  5569. int objc, /* Number of arguments */
  5570. Tcl_Obj[] objv /* Command arguments */
  5571. )
  5572. {
  5573. int rc = sqlite3_test_control( 9999 );
  5574. Debug.Assert( rc == 0 );
  5575. rc = sqlite3_test_control( -1 );
  5576. Debug.Assert( rc == 0 );
  5577. sqlite3_test_control( SQLITE_TESTCTRL_PRNG_SAVE );
  5578. return TCL.TCL_OK;
  5579. }
  5580. /*
  5581. ** tclcmd: restore_prng_state
  5582. */
  5583. static int restore_prng_state(
  5584. object clientdata, /* Pointer to sqlite3_enable_XXX function */
  5585. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  5586. int objc, /* Number of arguments */
  5587. Tcl_Obj[] objv /* Command arguments */
  5588. )
  5589. {
  5590. sqlite3_test_control( SQLITE_TESTCTRL_PRNG_RESTORE );
  5591. return TCL.TCL_OK;
  5592. }
  5593. /*
  5594. ** tclcmd: reset_prng_state
  5595. */
  5596. static int reset_prng_state(
  5597. object clientdata, /* Pointer to sqlite3_enable_XXX function */
  5598. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  5599. int objc, /* Number of arguments */
  5600. Tcl_Obj[] objv /* Command arguments */
  5601. )
  5602. {
  5603. sqlite3_test_control( SQLITE_TESTCTRL_PRNG_RESET );
  5604. return TCL.TCL_OK;
  5605. }
  5606. /*
  5607. ** tclcmd: pcache_stats
  5608. */
  5609. static int test_pcache_stats(
  5610. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  5611. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  5612. int objc, /* Number of arguments */
  5613. Tcl_Obj[] objv /* Command arguments */
  5614. )
  5615. {
  5616. int nMin;
  5617. int nMax;
  5618. int nCurrent;
  5619. int nRecyclable;
  5620. Tcl_Obj pRet;
  5621. sqlite3PcacheStats( out nCurrent, out nMax, out nMin, out nRecyclable );
  5622. pRet = TCL.Tcl_NewObj();
  5623. TCL.Tcl_ListObjAppendElement( interp, pRet, TCL.Tcl_NewStringObj( "current", -1 ) );
  5624. TCL.Tcl_ListObjAppendElement( interp, pRet, TCL.Tcl_NewIntObj( nCurrent ) );
  5625. TCL.Tcl_ListObjAppendElement( interp, pRet, TCL.Tcl_NewStringObj( "max", -1 ) );
  5626. TCL.Tcl_ListObjAppendElement( interp, pRet, TCL.Tcl_NewIntObj( nMax ) );
  5627. TCL.Tcl_ListObjAppendElement( interp, pRet, TCL.Tcl_NewStringObj( "min", -1 ) );
  5628. TCL.Tcl_ListObjAppendElement( interp, pRet, TCL.Tcl_NewIntObj( nMin ) );
  5629. TCL.Tcl_ListObjAppendElement( interp, pRet, TCL.Tcl_NewStringObj( "recyclable", -1 ) );
  5630. TCL.Tcl_ListObjAppendElement( interp, pRet, TCL.Tcl_NewIntObj( nRecyclable ) );
  5631. TCL.Tcl_SetObjResult( interp, pRet );
  5632. return TCL.TCL_OK;
  5633. }
  5634. public class _aObjCmd
  5635. {
  5636. public string zName;
  5637. public Interp.dxObjCmdProc xProc;
  5638. public object clientData;
  5639. public _aObjCmd( string zName, Interp.dxObjCmdProc xProc )
  5640. {
  5641. this.zName = zName;
  5642. this.xProc = xProc;
  5643. this.clientData = null;
  5644. }
  5645. public _aObjCmd( string zName, Interp.dxObjCmdProc xProc, object clientdata )
  5646. {
  5647. this.zName = zName;
  5648. this.xProc = xProc;
  5649. this.clientData = clientdata.GetType().Name == "Int32" && (int)clientdata == 0 ? null : clientdata;
  5650. }
  5651. }
  5652. #if SQLITE_ENABLE_UNLOCK_NOTIFY
  5653. static void test_unlock_notify_cb(void **aArg, int nArg){
  5654. int ii;
  5655. for(ii=0; ii<nArg; ii++){
  5656. TCL.Tcl_EvalEx((Tcl_Interp )aArg[ii], "unlock_notify", -1, TCL.TCL_EVAL_GLOBAL);
  5657. }
  5658. }
  5659. #endif //* SQLITE_ENABLE_UNLOCK_NOTIFY */
  5660. /*
  5661. ** tclcmd: sqlite3_unlock_notify db
  5662. */
  5663. #if SQLITE_ENABLE_UNLOCK_NOTIFY
  5664. static int test_unlock_notify(
  5665. ClientData clientData, /* Unused */
  5666. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  5667. int objc, /* Number of arguments */
  5668. Tcl_Obj[] objv /* Command arguments */
  5669. ){
  5670. sqlite3 db;
  5671. int rc;
  5672. if( objc!=2 ){
  5673. TCL.Tcl_WrongNumArgs(interp, 1, objv, "DB");
  5674. return TCL.TCL_ERROR;
  5675. }
  5676. if( getDbPointer(interp, TCL.Tcl_GetString(objv[1]), &db) ){
  5677. return TCL.TCL_ERROR;
  5678. }
  5679. rc = sqlite3_unlock_notify(db, test_unlock_notify_cb, (void )interp);
  5680. TCL.Tcl_SetResult(interp, (char )t1ErrorName(rc), TCL.TCL_STATIC);
  5681. return TCL.TCL_OK;
  5682. }
  5683. #endif
  5684. /*
  5685. ** tclcmd: sqlite3_wal_checkpoint db ?NAME?
  5686. */
  5687. static int test_wal_checkpoint(
  5688. ClientData clientData, /* Unused */
  5689. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  5690. int objc, /* Number of arguments */
  5691. Tcl_Obj[] objv /* Command arguments */
  5692. )
  5693. {
  5694. string zDb = "";
  5695. sqlite3 db = null;
  5696. int rc;
  5697. if ( objc != 3 && objc != 2 )
  5698. {
  5699. TCL.Tcl_WrongNumArgs( interp, 1, objv, "DB ?NAME?" );
  5700. return TCL.TCL_ERROR;
  5701. }
  5702. if ( getDbPointer( interp, TCL.Tcl_GetString( objv[1] ), out db ) != 0 )
  5703. {
  5704. return TCL.TCL_ERROR;
  5705. }
  5706. if ( objc == 3 )
  5707. {
  5708. zDb = TCL.Tcl_GetString( objv[2] );
  5709. }
  5710. rc = sqlite3_wal_checkpoint( db, zDb );
  5711. TCL.Tcl_SetResult( interp, t1ErrorName( rc ), TCL.TCL_STATIC );
  5712. return TCL.TCL_OK;
  5713. }
  5714. /*
  5715. ** tclcmd: sqlite3_wal_checkpoint_v2 db MODE ?NAME?
  5716. **
  5717. ** This command calls the wal_checkpoint_v2() function with the specified
  5718. ** mode argument (passive, full or restart). If present, the database name
  5719. ** NAME is passed as the second argument to wal_checkpoint_v2(). If it the
  5720. ** NAME argument is not present, a NULL pointer is passed instead.
  5721. **
  5722. ** If wal_checkpoint_v2() returns any value other than SQLITE_BUSY or
  5723. ** SQLITE_OK, then this command returns TCL_ERROR. The Tcl result is set
  5724. ** to the error message obtained from sqlite3_errmsg().
  5725. **
  5726. ** Otherwise, this command returns a list of three integers. The first integer
  5727. ** is 1 if SQLITE_BUSY was returned, or 0 otherwise. The following two integers
  5728. ** are the values returned via the output paramaters by wal_checkpoint_v2() -
  5729. ** the number of frames in the log and the number of frames in the log
  5730. ** that have been checkpointed.
  5731. */
  5732. static int test_wal_checkpoint_v2(
  5733. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  5734. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  5735. int objc, /* Number of arguments */
  5736. Tcl_Obj[] objv /* Command arguments */
  5737. )
  5738. {
  5739. string zDb = "";
  5740. sqlite3 db = null;
  5741. int rc;
  5742. int eMode = 0;
  5743. int nLog = -555;
  5744. int nCkpt = -555;
  5745. Tcl_Obj pRet;
  5746. string[] aMode = new string[] { "passive", "full", "restart" };
  5747. Debug.Assert( SQLITE_CHECKPOINT_PASSIVE == 0 );
  5748. Debug.Assert( SQLITE_CHECKPOINT_FULL == 1 );
  5749. Debug.Assert( SQLITE_CHECKPOINT_RESTART == 2 );
  5750. if ( objc != 3 && objc != 4 )
  5751. {
  5752. TCL.Tcl_WrongNumArgs( interp, 1, objv, "DB MODE ?NAME?" );
  5753. return TCL.TCL_ERROR;
  5754. }
  5755. if ( objc == 4 )
  5756. {
  5757. zDb = TCL.Tcl_GetString( objv[3] );
  5758. }
  5759. if ( getDbPointer( interp, TCL.Tcl_GetString( objv[1] ), out db ) != 0
  5760. || TCL.Tcl_GetIndexFromObj( interp, objv[2], aMode, "mode", 0, out eMode )
  5761. )
  5762. {
  5763. return TCL.TCL_ERROR;
  5764. }
  5765. rc = sqlite3_wal_checkpoint_v2( db, zDb, eMode, out nLog, out nCkpt );
  5766. if ( rc != SQLITE_OK && rc != SQLITE_BUSY )
  5767. {
  5768. TCL.Tcl_SetResult( interp, sqlite3_errmsg( db ), TCL.TCL_VOLATILE );
  5769. return TCL.TCL_ERROR;
  5770. }
  5771. pRet = TCL.Tcl_NewObj();
  5772. TCL.Tcl_ListObjAppendElement( interp, pRet, TCL.Tcl_NewIntObj( rc == SQLITE_BUSY ? 1 : 0 ) );
  5773. TCL.Tcl_ListObjAppendElement( interp, pRet, TCL.Tcl_NewIntObj( nLog ) );
  5774. TCL.Tcl_ListObjAppendElement( interp, pRet, TCL.Tcl_NewIntObj( nCkpt ) );
  5775. TCL.Tcl_SetObjResult( interp, pRet );
  5776. return TCL.TCL_OK;
  5777. }
  5778. /*
  5779. ** tclcmd: test_sqlite3_log ?SCRIPT?
  5780. */
  5781. struct LogCallback
  5782. {
  5783. public Tcl_Interp pInterp;
  5784. public Tcl_Obj pObj;
  5785. }
  5786. static LogCallback logcallback = new LogCallback();
  5787. static void xLogcallback( object unused, int err, string zMsg )
  5788. {
  5789. Tcl_Obj pNew = TCL.Tcl_DuplicateObj( logcallback.pObj );
  5790. TCL.Tcl_IncrRefCount( pNew );
  5791. TCL.Tcl_ListObjAppendElement(
  5792. null, pNew, TCL.Tcl_NewStringObj( sqlite3TestErrorName( err ), -1 )
  5793. );
  5794. TCL.Tcl_ListObjAppendElement( null, pNew, TCL.Tcl_NewStringObj( zMsg, -1 ) );
  5795. TCL.Tcl_EvalObjEx( logcallback.pInterp, pNew, TCL.TCL_EVAL_GLOBAL | TCL.TCL_EVAL_DIRECT );
  5796. TCL.Tcl_DecrRefCount( ref pNew );
  5797. }
  5798. static int test_sqlite3_log(
  5799. ClientData clientData,
  5800. Tcl_Interp interp, /* The TCL interpreter that invoked this command */
  5801. int objc, /* Number of arguments */
  5802. Tcl_Obj[] objv /* Command arguments */
  5803. )
  5804. {
  5805. if ( objc > 2 )
  5806. {
  5807. TCL.Tcl_WrongNumArgs( interp, 1, objv, "SCRIPT" );
  5808. return TCL.TCL_ERROR;
  5809. }
  5810. if ( logcallback.pObj != null )
  5811. {
  5812. TCL.Tcl_DecrRefCount( ref logcallback.pObj );
  5813. logcallback.pObj = null;
  5814. logcallback.pInterp = null;
  5815. sqlite3_config( SQLITE_CONFIG_LOG, 0, 0 );
  5816. }
  5817. if ( objc > 1 )
  5818. {
  5819. logcallback.pObj = objv[1];
  5820. TCL.Tcl_IncrRefCount( logcallback.pObj );
  5821. logcallback.pInterp = interp;
  5822. sqlite3_config( SQLITE_CONFIG_LOG, (dxLogcallback)xLogcallback, 0 );
  5823. }
  5824. return TCL.TCL_OK;
  5825. }
  5826. /*
  5827. ** tcl_objproc COMMANDNAME ARGS...
  5828. **
  5829. ** Run a TCL command using its objProc interface. Throw an error if
  5830. ** the command has no objProc interface.
  5831. */
  5832. //static int runAsObjProc(
  5833. // void * clientData,
  5834. // Tcl_Interp interp,
  5835. // int objc,
  5836. // Tcl_Obj[] objv
  5837. //){
  5838. // TCL.Tcl_CmdInfo cmdInfo;
  5839. // if( objc<2 ){
  5840. // TCL.Tcl_WrongNumArgs(interp, 1, objv, "COMMAND ...");
  5841. // return TCL.TCL_ERROR;
  5842. // }
  5843. // if( null==TCL.Tcl_GetCommandInfo(interp, TCL.Tcl_GetString(objv[1]), &cmdInfo) ){
  5844. // TCL.Tcl_AppendResult(interp, "command not found: ",
  5845. // TCL.Tcl_GetString(objv[1]), (char)0);
  5846. // return TCL.TCL_ERROR;
  5847. // }
  5848. // if( cmdInfo.objProc==0 ){
  5849. // TCL.Tcl_AppendResult(interp, "command has no objProc: ",
  5850. // TCL.Tcl_GetString(objv[1]), (char)0);
  5851. // return TCL.TCL_ERROR;
  5852. // }
  5853. // return cmdInfo.objProc(cmdInfo.objClientData, interp, objc-1, objv+1);
  5854. //}
  5855. /*
  5856. ** Register commands with the TCL interpreter.
  5857. */
  5858. #if !SQLITE_OMIT_EXPLAIN
  5859. /*
  5860. ** WARNING: The following function, printExplainQueryPlan() is an exact
  5861. ** copy of example code from eqp.in (eqp.html). If this code is modified,
  5862. ** then the documentation copy needs to be modified as well.
  5863. */
  5864. /*
  5865. ** Argument pStmt is a prepared SQL statement. This function compiles
  5866. ** an EXPLAIN QUERY PLAN command to report on the prepared statement,
  5867. ** and prints the report to stdout using printf().
  5868. */
  5869. static int printExplainQueryPlan( sqlite3_stmt pStmt )
  5870. {
  5871. string zSql; /* Input SQL */
  5872. string zExplain; /* SQL with EXPLAIN QUERY PLAN prepended */
  5873. sqlite3_stmt pExplain = null; /* Compiled EXPLAIN QUERY PLAN command */
  5874. int rc; /* Return code from sqlite3_prepare_v2() */
  5875. zSql = sqlite3_sql( pStmt );
  5876. if ( zSql == null )
  5877. return SQLITE_ERROR;
  5878. zExplain = sqlite3_mprintf( "EXPLAIN QUERY PLAN %s", zSql );
  5879. //if ( zExplain == null )
  5880. // return SQLITE_NOMEM;
  5881. rc = sqlite3_prepare_v2( sqlite3_db_handle( pStmt ), zExplain, -1, ref pExplain, 0 );
  5882. //sqlite3_free(zExplain);
  5883. if ( rc != SQLITE_OK )
  5884. return rc;
  5885. while ( SQLITE_ROW == sqlite3_step( pExplain ) )
  5886. {
  5887. int iSelectid = sqlite3_column_int( pExplain, 0 );
  5888. int iOrder = sqlite3_column_int( pExplain, 1 );
  5889. int iFrom = sqlite3_column_int( pExplain, 2 );
  5890. string zDetail = sqlite3_column_text( pExplain, 3 );
  5891. printf( "%d %d %d %s\n", iSelectid, iOrder, iFrom, zDetail );
  5892. }
  5893. return sqlite3_finalize( pExplain );
  5894. }
  5895. static int test_print_eqp(
  5896. object clientData,
  5897. Tcl_Interp interp,
  5898. int objc,
  5899. Tcl_Obj[] objv
  5900. )
  5901. {
  5902. int rc;
  5903. sqlite3_stmt pStmt = null;
  5904. if ( objc != 2 )
  5905. {
  5906. TCL.Tcl_WrongNumArgs( interp, 1, objv, "STMT" );
  5907. return TCL.TCL_ERROR;
  5908. }
  5909. if ( getStmtPointer( interp, TCL.Tcl_GetString( objv[1] ), out pStmt ) != 0 )
  5910. return TCL.TCL_ERROR;
  5911. rc = printExplainQueryPlan( pStmt );
  5912. /* This is needed on Windows so that a test case using this
  5913. ** function can open a read pipe and get the output of
  5914. ** printExplainQueryPlan() immediately.
  5915. */
  5916. //fflush( stdout );
  5917. TCL.Tcl_SetResult( interp, t1ErrorName( rc ), 0 );
  5918. return TCL.TCL_OK;
  5919. }
  5920. #endif //* SQLITE_OMIT_EXPLAIN */
  5921. class Verb
  5922. {
  5923. public string zName;
  5924. public int i;
  5925. public Verb( string zName, int i )
  5926. {
  5927. this.zName = zName;
  5928. this.i = i;
  5929. }
  5930. }
  5931. /*
  5932. ** sqlite3_test_control VERB ARGS...
  5933. */
  5934. static int test_test_control(
  5935. object clientData,
  5936. Tcl_Interp interp,
  5937. int objc,
  5938. Tcl_Obj[] objv
  5939. )
  5940. {
  5941. Verb[] aVerb = new Verb[] {
  5942. new Verb( "SQLITE_TESTCTRL_LOCALTIME_FAULT", SQLITE_TESTCTRL_LOCALTIME_FAULT ),
  5943. };
  5944. int iVerb;
  5945. int iFlag;
  5946. int rc;
  5947. if ( objc < 2 )
  5948. {
  5949. TCL.Tcl_WrongNumArgs( interp, 1, objv, "VERB ARGS..." );
  5950. return TCL.TCL_ERROR;
  5951. }
  5952. //rc = TCL.Tcl_GetIndexFromObjStruct(
  5953. // interp, objv[1], aVerb, sizeof(aVerb[0]), "VERB", 0, &iVerb
  5954. //);
  5955. for ( iVerb = 0; iVerb < aVerb.Length && ( aVerb[iVerb].zName != objv[1].ToString() ); iVerb++ )
  5956. {
  5957. }
  5958. if ( iVerb >= aVerb.Length )
  5959. return TCL.TCL_ERROR;
  5960. iFlag = aVerb[iVerb].i;
  5961. switch ( iFlag )
  5962. {
  5963. case SQLITE_TESTCTRL_LOCALTIME_FAULT:
  5964. {
  5965. bool val = false;
  5966. if ( objc != 3 )
  5967. {
  5968. TCL.Tcl_WrongNumArgs( interp, 2, objv, "ONOFF" );
  5969. return TCL.TCL_ERROR;
  5970. }
  5971. if ( TCL.Tcl_GetBooleanFromObj( interp, objv[2], out val ) )
  5972. return TCL.TCL_ERROR;
  5973. sqlite3_test_control( SQLITE_TESTCTRL_LOCALTIME_FAULT, val );
  5974. break;
  5975. }
  5976. }
  5977. TCL.Tcl_ResetResult( interp );
  5978. return TCL.TCL_OK;
  5979. }
  5980. /*
  5981. ** optimization_control DB OPT BOOLEAN
  5982. **
  5983. ** Enable or disable query optimizations using the sqlite3_test_control()
  5984. ** interface. Disable if BOOLEAN is false and enable if BOOLEAN is true.
  5985. ** OPT is the name of the optimization to be disabled.
  5986. */
  5987. public class _aOpt
  5988. {
  5989. public string zOptName;
  5990. public int mask;
  5991. public _aOpt( string zOptName, int mask )
  5992. {
  5993. this.zOptName = zOptName;
  5994. this.mask = mask;
  5995. }
  5996. }
  5997. static int optimization_control(
  5998. object clientData,
  5999. Tcl_Interp interp,
  6000. int objc,
  6001. Tcl_Obj[] objv
  6002. )
  6003. {
  6004. int i;
  6005. sqlite3 db = null;
  6006. string zOpt;
  6007. bool onoff = false;
  6008. int mask = 0;
  6009. _aOpt[] aOpt = {
  6010. new _aOpt( "all", SQLITE_OptMask ),
  6011. new _aOpt( "query-flattener", SQLITE_QueryFlattener ),
  6012. new _aOpt( "column-cache", SQLITE_ColumnCache ),
  6013. new _aOpt( "index-sort", SQLITE_IndexSort ),
  6014. new _aOpt( "index-search", SQLITE_IndexSearch ),
  6015. new _aOpt( "index-cover", SQLITE_IndexCover ),
  6016. new _aOpt( "groupby-order", SQLITE_GroupByOrder ),
  6017. new _aOpt( "factor-constants", SQLITE_FactorOutConst ),
  6018. new _aOpt( "real-as-int", SQLITE_IdxRealAsInt ),
  6019. };
  6020. if ( objc != 4 )
  6021. {
  6022. TCL.Tcl_WrongNumArgs( interp, 1, objv, "DB OPT BOOLEAN" );
  6023. return TCL.TCL_ERROR;
  6024. }
  6025. if ( getDbPointer( interp, TCL.Tcl_GetString( objv[1] ), out db ) != 0 )
  6026. return TCL.TCL_ERROR;
  6027. if ( TCL.Tcl_GetBooleanFromObj( interp, objv[3], out onoff ) )
  6028. return TCL.TCL_ERROR;
  6029. zOpt = TCL.Tcl_GetString( objv[2] );
  6030. for ( i = 0; i < aOpt.Length; i++ )//sizeof(aOpt)/sizeof(aOpt[0]); i++)
  6031. {
  6032. if ( zOpt == aOpt[i].zOptName )
  6033. {
  6034. mask = aOpt[i].mask;
  6035. break;
  6036. }
  6037. }
  6038. if ( onoff )
  6039. mask = ~mask;
  6040. if ( i >= aOpt.Length )//sizeof(aOpt)/sizeof(aOpt[0]) )
  6041. {
  6042. TCL.Tcl_AppendResult( interp, "unknown optimization - should be one of:",
  6043. null );
  6044. for ( i = 0; i < aOpt.Length; i++ )//sizeof(aOpt)/sizeof(aOpt[0]); i++)
  6045. {
  6046. TCL.Tcl_AppendResult( interp, " ", aOpt[i].zOptName );
  6047. }
  6048. return TCL.TCL_ERROR;
  6049. }
  6050. sqlite3_test_control( SQLITE_TESTCTRL_OPTIMIZATIONS, db, mask );
  6051. return TCL.TCL_OK;
  6052. }
  6053. static Var.SQLITE3_GETSET bitmask_size = new Var.SQLITE3_GETSET( "bitmask_size" );
  6054. #if SQLITE_OS_UNIX && (__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
  6055. extern int sqlite3_hostid_num;
  6056. #endif
  6057. static Var.SQLITE3_GETSET sqlite_static_bind_nbyte = new Var.SQLITE3_GETSET( "static_bind_nbyte" );
  6058. static Var.SQLITE3_GETSET sqlite_static_bind_value = new Var.SQLITE3_GETSET( "static_bind_value" );
  6059. public static int Sqlitetest1_Init( Tcl_Interp interp )
  6060. {
  6061. // extern int sqlite3_search_count;
  6062. // extern int sqlite3_found_count;
  6063. // extern int sqlite3_interrupt_count;
  6064. // extern int sqlite3_sort_count;
  6065. // extern int sqlite3_current_time;
  6066. // extern int sqlite3_max_blobsize;
  6067. // extern int sqlite3BtreeSharedCacheReport(void*,
  6068. // Tcl_Interp *,int,Tcl_Obj*CONST);
  6069. // static struct {
  6070. // string zName;
  6071. // TCL.Tcl_CmdProc *xProc;
  6072. _aCmd[] aCmd = new _aCmd[] {
  6073. new _aCmd( "db_enter", db_enter ),
  6074. new _aCmd( "db_leave", db_leave ),
  6075. new _aCmd( "sqlite3_mprintf_int", sqlite3_mprintf_int ),
  6076. new _aCmd( "sqlite3_mprintf_int64", sqlite3_mprintf_int64 ),
  6077. new _aCmd( "sqlite3_mprintf_long", sqlite3_mprintf_long ),
  6078. new _aCmd( "sqlite3_mprintf_str", sqlite3_mprintf_str ),
  6079. new _aCmd( "sqlite3_snprintf_str", sqlite3_snprintf_str ),
  6080. new _aCmd( "sqlite3_mprintf_stronly", sqlite3_mprintf_stronly),
  6081. new _aCmd( "sqlite3_mprintf_double", sqlite3_mprintf_double ),
  6082. new _aCmd( "sqlite3_mprintf_scaled", sqlite3_mprintf_scaled ),
  6083. new _aCmd( "sqlite3_mprintf_hexdouble", sqlite3_mprintf_hexdouble),
  6084. new _aCmd( "sqlite3_mprintf_z_test", test_mprintf_z ),
  6085. new _aCmd( "sqlite3_mprintf_n_test", test_mprintf_n ),
  6086. new _aCmd( "sqlite3_snprintf_int", test_snprintf_int ),
  6087. // new _aCmd( "sqlite3_last_insert_rowid", test_last_rowid ),
  6088. new _aCmd( "sqlite3_exec_printf", test_exec_printf ),
  6089. new _aCmd( "sqlite3_exec_hex", test_exec_hex ),
  6090. new _aCmd( "sqlite3_exec", test_exec ),
  6091. new _aCmd( "sqlite3_exec_nr", test_exec_nr ),
  6092. #if !SQLITE_OMIT_GET_TABLE
  6093. new _aCmd( "sqlite3_get_table_printf", test_get_table_printf ),
  6094. #endif
  6095. new _aCmd( "sqlite3_close", sqlite_test_close ),
  6096. new _aCmd( "sqlite3_create_function", test_create_function ),
  6097. new _aCmd( "sqlite3_create_aggregate", test_create_aggregate ),
  6098. new _aCmd( "sqlite_register_test_function", test_register_func ),
  6099. // new _aCmd( "sqlite_abort", sqlite_abort ),
  6100. new _aCmd( "sqlite_bind", test_bind ),
  6101. new _aCmd( "breakpoint", test_breakpoint ),
  6102. new _aCmd( "sqlite3_key", test_key ),
  6103. new _aCmd( "sqlite3_rekey", test_rekey ),
  6104. new _aCmd( "sqlite_set_magic", sqlite_set_magic ),
  6105. new _aCmd( "sqlite3_interrupt", test_interrupt ),
  6106. new _aCmd( "sqlite_delete_function", delete_function ),
  6107. new _aCmd( "sqlite_delete_collation", delete_collation ),
  6108. new _aCmd( "sqlite3_get_autocommit", get_autocommit ),
  6109. // new _aCmd( "sqlite3_stack_used", test_stack_used ),
  6110. new _aCmd( "sqlite3_busy_timeout", test_busy_timeout ),
  6111. // new _aCmd( "printf", test_printf ),
  6112. // new _aCmd( "sqlite3IoTrace", test_io_trace ),
  6113. };
  6114. // static struct {
  6115. // string zName;
  6116. // Tcl_ObjCmdProc *xProc;
  6117. // void *object;
  6118. _aObjCmd[] aObjCmd = new _aObjCmd[]{
  6119. new _aObjCmd( "sqlite3_connection_pointer", get_sqlite_pointer, 0 ),
  6120. new _aObjCmd( "sqlite3_bind_int", test_bind_int, 0 ),
  6121. new _aObjCmd( "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 ),
  6122. new _aObjCmd( "sqlite3_bind_int64", test_bind_int64, 0 ),
  6123. new _aObjCmd( "sqlite3_bind_double", test_bind_double, 0 ),
  6124. new _aObjCmd( "sqlite3_bind_null", test_bind_null ,0 ),
  6125. new _aObjCmd( "sqlite3_bind_text", test_bind_text ,0 ),
  6126. new _aObjCmd( "sqlite3_bind_text16", test_bind_text16 ,0 ),
  6127. new _aObjCmd( "sqlite3_bind_blob", test_bind_blob ,0 ),
  6128. new _aObjCmd( "sqlite3_bind_parameter_count", test_bind_parameter_count, 0),
  6129. new _aObjCmd( "sqlite3_bind_parameter_name", test_bind_parameter_name, 0),
  6130. new _aObjCmd( "sqlite3_bind_parameter_index", test_bind_parameter_index, 0),
  6131. new _aObjCmd( "sqlite3_clear_bindings", test_clear_bindings, 0),
  6132. new _aObjCmd( "sqlite3_sleep", test_sleep, 0),
  6133. new _aObjCmd( "sqlite3_errcode", test_errcode ,0 ),
  6134. new _aObjCmd( "sqlite3_extended_errcode", test_ex_errcode ,0 ),
  6135. new _aObjCmd( "sqlite3_errmsg", test_errmsg ,0 ),
  6136. // new _aObjCmd( "sqlite3_errmsg16", test_errmsg16 ,0 ),
  6137. new _aObjCmd( "sqlite3_open", test_open ,0 ),
  6138. // new _aObjCmd( "sqlite3_open16", test_open16 ,0 ),
  6139. new _aObjCmd( "sqlite3_open_v2", test_open_v2 ,0 ),
  6140. // new _aObjCmd( "sqlite3_complete16", test_complete16 ,0 ),
  6141. new _aObjCmd( "sqlite3_prepare", test_prepare ,0 ),
  6142. new _aObjCmd( "sqlite3_prepare16", test_prepare16 ,0 ),
  6143. new _aObjCmd( "sqlite3_prepare_v2", test_prepare_v2 ,0 ),
  6144. new _aObjCmd( "sqlite3_prepare_tkt3134", test_prepare_tkt3134, 0),
  6145. // new _aObjCmd( "sqlite3_prepare16_v2", test_prepare16_v2 ,0 ),
  6146. new _aObjCmd( "sqlite3_finalize", test_finalize ,0 ),
  6147. new _aObjCmd( "sqlite3_reset", test_reset ,0 ),
  6148. new _aObjCmd( "sqlite3_expired", test_expired ,0 ),
  6149. new _aObjCmd( "sqlite3_transfer_bindings", test_transfer_bind ,0 ),
  6150. new _aObjCmd( "sqlite3_changes", test_changes ,0 ),
  6151. new _aObjCmd( "sqlite3_step", test_step ,0 ),
  6152. // { "sqlite3_sql", test_sql ,0 },
  6153. new _aObjCmd( "sqlite3_next_stmt", test_next_stmt ,0 ),
  6154. new _aObjCmd( "sqlite3_stmt_readonly", test_stmt_readonly ,0 ),
  6155. new _aObjCmd( "uses_stmt_journal", uses_stmt_journal ,0 ),
  6156. new _aObjCmd( "sqlite3_release_memory", test_release_memory, 0),
  6157. new _aObjCmd( "sqlite3_soft_heap_limit", test_soft_heap_limit, 0),
  6158. // new _aObjCmd( "sqlite3_thread_cleanup", test_thread_cleanup, 0),
  6159. // new _aObjCmd( "sqlite3_pager_refcounts", test_pager_refcounts, 0),
  6160. // new _aObjCmd( "sqlite3_load_extension", test_load_extension, 0),
  6161. // new _aObjCmd( "sqlite3_enable_load_extension", test_enable_load, 0),
  6162. new _aObjCmd( "sqlite3_extended_result_codes", test_extended_result_codes, 0),
  6163. new _aObjCmd( "sqlite3_limit", test_limit, 0),
  6164. new _aObjCmd( "save_prng_state", save_prng_state, 0 ),
  6165. new _aObjCmd( "restore_prng_state", restore_prng_state, 0 ),
  6166. new _aObjCmd( "reset_prng_state", reset_prng_state, 0 ),
  6167. new _aObjCmd( "optimization_control", optimization_control,0),
  6168. // { "tcl_objproc", runAsObjProc, 0 },
  6169. // /* sqlite3_column_*() API */
  6170. new _aObjCmd( "sqlite3_column_count", test_column_count ,0 ),
  6171. new _aObjCmd( "sqlite3_data_count", test_data_count ,0 ),
  6172. new _aObjCmd( "sqlite3_column_type", test_column_type ,0 ),
  6173. new _aObjCmd( "sqlite3_column_blob", test_column_blob ,0 ),
  6174. new _aObjCmd( "sqlite3_column_double", test_column_double ,0 ),
  6175. new _aObjCmd( "sqlite3_column_int64", test_column_int64 ,0 ),
  6176. new _aObjCmd( "sqlite3_column_text", test_stmt_utf8, (dxColumn)sqlite3_column_text ),
  6177. new _aObjCmd( "sqlite3_column_name", test_stmt_utf8, (dxColumn)sqlite3_column_name ),
  6178. new _aObjCmd( "sqlite3_column_int", test_stmt_int, (dxColumn_I)sqlite3_column_int ),
  6179. new _aObjCmd( "sqlite3_column_bytes", test_stmt_int, (dxColumn_I)sqlite3_column_bytes ),
  6180. #if !SQLITE_OMIT_DECLTYPE
  6181. new _aObjCmd( "sqlite3_column_decltype", test_stmt_utf8, (dxColumn) sqlite3_column_decltype ),
  6182. #endif
  6183. #if SQLITE_ENABLE_COLUMN_METADATA
  6184. new _aObjCmd( "sqlite3_column_database_name", test_stmt_utf8, (dxColumn)sqlite3_column_database_name),
  6185. new _aObjCmd( "sqlite3_column_table_name", test_stmt_utf8, (dxColumn)sqlite3_column_table_name),
  6186. new _aObjCmd( "sqlite3_column_origin_name", test_stmt_utf8, (dxColumn)sqlite3_column_origin_name),
  6187. #endif
  6188. #if !SQLITE_OMIT_UTF16
  6189. // { "sqlite3_column_bytes16", test_stmt_int, sqlite3_column_bytes16 ),
  6190. // { "sqlite3_column_text16", test_stmt_utf16, sqlite3_column_text16 ),
  6191. // { "sqlite3_column_decltype16", test_stmt_utf16, sqlite3_column_decltype16),
  6192. // { "sqlite3_column_name16", test_stmt_utf16, sqlite3_column_name16 ),
  6193. // { "add_alignment_test_collations", add_alignment_test_collations, 0 ),
  6194. #if SQLITE_ENABLE_COLUMN_METADATA
  6195. //{"sqlite3_column_database_name16",
  6196. // test_stmt_utf16, sqlite3_column_database_name16),
  6197. //{"sqlite3_column_table_name16", test_stmt_utf16, sqlite3_column_table_name16),
  6198. //{"sqlite3_column_origin_name16", test_stmt_utf16, sqlite3_column_origin_name16),
  6199. #endif
  6200. #endif
  6201. new _aObjCmd( "sqlite3_create_collation_v2", test_create_collation_v2, 0 ),
  6202. new _aObjCmd( "sqlite3_global_recover", test_global_recover, 0 ),
  6203. new _aObjCmd( "working_64bit_int", working_64bit_int, 0 ),
  6204. new _aObjCmd( "vfs_unlink_test", vfs_unlink_test, 0 ),
  6205. //{ "vfs_initfail_test", vfs_initfail_test, 0 },
  6206. new _aObjCmd( "vfs_unregister_all", vfs_unregister_all, 0 ),
  6207. new _aObjCmd("vfs_reregister_all", vfs_reregister_all, 0 ),
  6208. new _aObjCmd( "file_control_test", file_control_test, 0 ),
  6209. new _aObjCmd("file_control_lasterrno_test", file_control_lasterrno_test, 0 ),
  6210. new _aObjCmd("file_control_lockproxy_test", file_control_lockproxy_test, 0 ),
  6211. new _aObjCmd("file_control_chunksize_test", file_control_chunksize_test, 0 ),
  6212. new _aObjCmd("file_control_sizehint_test", file_control_sizehint_test, 0),
  6213. //new _aObjCmd( "sqlite3_vfs_list", vfs_list, 0 ),
  6214. new _aObjCmd( "sqlite3_create_function_v2", test_create_function_v2, 0 ),
  6215. // /* Functions from os.h */
  6216. #if !SQLITE_OMIT_UTF16
  6217. // { "add_test_collate", test_collate, 0 ),
  6218. // { "add_test_collate_needed", test_collate_needed, 0 ),
  6219. // { "add_test_function", test_function, 0 ),
  6220. #endif
  6221. new _aObjCmd( "sqlite3_test_errstr", test_errstr, 0 ),
  6222. new _aObjCmd( "tcl_variable_type", tcl_variable_type, 0 ),
  6223. #if !SQLITE_OMIT_SHARED_CACHE
  6224. new _aObjCmd( "sqlite3_enable_shared_cache", test_enable_shared, 0 ),
  6225. //{ "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0),
  6226. #endif
  6227. new _aObjCmd( "sqlite3_libversion_number", test_libversion_number, 0 ),
  6228. #if SQLITE_ENABLE_COLUMN_METADATA
  6229. new _aObjCmd( "sqlite3_table_column_metadata", test_table_column_metadata, 0 ),
  6230. #endif
  6231. #if !SQLITE_OMIT_INCRBLOB
  6232. // new _aObjCmd( "sqlite3_blob_read", test_blob_read, 0 ),
  6233. // new _aObjCmd( "sqlite3_blob_write", test_blob_write, 0 ),
  6234. //{ "sqlite3_blob_reopen", test_blob_reopen, 0 },
  6235. //{ "sqlite3_blob_bytes", test_blob_bytes, 0 },
  6236. //{ "sqlite3_blob_close", test_blob_close, 0 },
  6237. #endif
  6238. new _aObjCmd( "pcache_stats", test_pcache_stats, 0 ),
  6239. #if SQLITE_ENABLE_UNLOCK_NOTIFY
  6240. { "sqlite3_unlock_notify", test_unlock_notify, 0 },
  6241. #endif
  6242. new _aObjCmd( "sqlite3_wal_checkpoint", test_wal_checkpoint, 0 ),
  6243. new _aObjCmd( "sqlite3_wal_checkpoint_v2", test_wal_checkpoint_v2, 0 ),
  6244. new _aObjCmd( "test_sqlite3_log", test_sqlite3_log, 0 ),
  6245. #if !SQLITE_OMIT_EXPLAIN
  6246. new _aObjCmd( "print_explain_query_plan", test_print_eqp, 0 ),
  6247. #endif
  6248. new _aObjCmd( "sqlite3_test_control", test_test_control ),
  6249. };
  6250. bitmask_size.iValue = BMS;
  6251. int i;
  6252. // extern int sqlite3_sync_count, sqlite3_fullsync_count;
  6253. // extern int sqlite3_opentemp_count;
  6254. // extern int sqlite3_like_count;
  6255. // extern int sqlite3_xferopt_count;
  6256. // extern int sqlite3_pager_readdb_count;
  6257. // extern int sqlite3_pager_writedb_count;
  6258. // extern int sqlite3_pager_writej_count;
  6259. #if SQLITE_OS_WIN
  6260. // extern int sqlite3_os_type;
  6261. #endif
  6262. #if SQLITE_DEBUG
  6263. // extern int sqlite3WhereTrace;
  6264. // extern int sqlite3OSTrace;
  6265. // extern int sqlite3VdbeAddopTrace;
  6266. // extern int sqlite3WalTrace;
  6267. #endif
  6268. #if SQLITE_TEST
  6269. // extern char sqlite3_query_plan[];
  6270. // static char *query_plan = sqlite3_query_plan;
  6271. #if SQLITE_ENABLE_FTS3
  6272. extern int sqlite3_fts3_enable_parentheses;
  6273. #endif
  6274. #endif
  6275. for ( i = 0; i < aCmd.Length; i++ )
  6276. {//sizeof(aCmd)/sizeof(aCmd[0]); i++){
  6277. TCL.Tcl_CreateCommand( interp, aCmd[i].zName, aCmd[i].xProc, null, null );
  6278. }
  6279. for ( i = 0; i < aObjCmd.Length; i++ )
  6280. {// i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
  6281. TCL.Tcl_CreateObjCommand( interp, aObjCmd[i].zName,
  6282. (Interp.dxObjCmdProc)aObjCmd[i].xProc, (object)aObjCmd[i].clientData, null );
  6283. }
  6284. TCL.Tcl_LinkVar( interp, "sqlite_search_count",
  6285. sqlite3_search_count, VarFlags.SQLITE3_LINK_INT );
  6286. TCL.Tcl_LinkVar( interp, "sqlite_found_count",
  6287. sqlite3_found_count, VarFlags.SQLITE3_LINK_INT );
  6288. TCL.Tcl_LinkVar( interp, "sqlite_sort_count",
  6289. sqlite3_sort_count, VarFlags.SQLITE3_LINK_INT );
  6290. TCL.Tcl_LinkVar( interp, "sqlite3_max_blobsize",
  6291. sqlite3_max_blobsize, VarFlags.SQLITE3_LINK_INT );
  6292. TCL.Tcl_LinkVar( interp, "sqlite_like_count",
  6293. sqlite3_like_count, VarFlags.SQLITE3_LINK_INT );
  6294. TCL.Tcl_LinkVar( interp, "sqlite_interrupt_count",
  6295. sqlite3_interrupt_count, VarFlags.SQLITE3_LINK_INT );
  6296. TCL.Tcl_LinkVar( interp, "sqlite_open_file_count",
  6297. sqlite3_open_file_count, VarFlags.SQLITE3_LINK_INT );
  6298. TCL.Tcl_LinkVar( interp, "sqlite_current_time",
  6299. sqlite3_current_time, VarFlags.SQLITE3_LINK_INT );
  6300. #if SQLITE_OS_UNIX && (__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
  6301. TCL.Tcl_LinkVar(interp, "sqlite_hostid_num",
  6302. (char)&sqlite3_hostid_num, TCL.Tcl_LINK_INT);
  6303. #endif
  6304. TCL.Tcl_LinkVar( interp, "sqlite3_xferopt_count",
  6305. sqlite3_xferopt_count, VarFlags.SQLITE3_LINK_INT );
  6306. TCL.Tcl_LinkVar( interp, "sqlite3_pager_readdb_count",
  6307. sqlite3_pager_readdb_count, VarFlags.SQLITE3_LINK_INT );
  6308. TCL.Tcl_LinkVar( interp, "sqlite3_pager_writedb_count",
  6309. sqlite3_pager_writedb_count, VarFlags.SQLITE3_LINK_INT );
  6310. TCL.Tcl_LinkVar( interp, "sqlite3_pager_writej_count",
  6311. sqlite3_pager_writej_count, VarFlags.SQLITE3_LINK_INT );
  6312. #if !SQLITE_OMIT_UTF16
  6313. // TCL.Tcl_LinkVar(interp, "unaligned_string_counter",
  6314. // (char)&unaligned_string_counter, VarFlag.SQLITE3_LINK_INT);
  6315. #endif
  6316. #if !SQLITE_OMIT_UTF16
  6317. // TCL.Tcl_LinkVar(interp, "sqlite_last_needed_collation",
  6318. // (char)&pzNeededCollation, VarFlag.TCL_LINK_STRING|VarFlag.TCL_LINK_READ_ONLY);
  6319. #endif
  6320. #if SQLITE_OS_WIN
  6321. // TCL.Tcl_LinkVar(interp, "sqlite_os_type",
  6322. // (char)&sqlite3_os_type, VarFlag.SQLITE3_LINK_INT);
  6323. #endif
  6324. #if SQLITE_TEST
  6325. TCL.Tcl_LinkVar( interp, "sqlite_query_plan",
  6326. sqlite3_query_plan, VarFlags.SQLITE3_LINK_STRING | VarFlags.SQLITE3_LINK_READ_ONLY );
  6327. #endif
  6328. #if SQLITE_DEBUG
  6329. // TCL.Tcl_LinkVar(interp, "sqlite_addop_trace",
  6330. // (char)&sqlite3VdbeAddopTrace, VarFlag.SQLITE3_LINK_INT);
  6331. // TCL.Tcl_LinkVar(interp, "sqlite_where_trace",
  6332. // (char)&sqlite3WhereTrace, VarFlag.SQLITE3_LINK_INT);
  6333. // TCL.Tcl_LinkVar(interp, "sqlite_os_trace",
  6334. // (char)&sqlite3OSTrace, VarFlag.SQLITE3_LINK_INT);
  6335. #if !SQLITE_OMIT_WAL
  6336. TCL.Tcl_LinkVar((interp, "sqlite_wal_trace",
  6337. (char)&sqlite3WalTrace, VarFlag.SQLITE3_LINK_INT);
  6338. #endif
  6339. #endif
  6340. #if !SQLITE_OMIT_DISKIO
  6341. TCL.Tcl_LinkVar( interp, "sqlite_opentemp_count",
  6342. sqlite3_opentemp_count, VarFlags.SQLITE3_LINK_INT );
  6343. #endif
  6344. TCL.Tcl_LinkVar( interp, "sqlite_static_bind_value",
  6345. sqlite_static_bind_value, VarFlags.SQLITE3_LINK_STRING );
  6346. TCL.Tcl_LinkVar( interp, "sqlite_static_bind_nbyte",
  6347. sqlite_static_bind_nbyte, VarFlags.SQLITE3_LINK_INT );
  6348. // TCL.Tcl_LinkVar(interp, "sqlite_temp_directory",
  6349. // (char)&sqlite3_temp_directory, VarFlag.TCL_LINK_STRING);
  6350. TCL.Tcl_LinkVar( interp, "bitmask_size",
  6351. bitmask_size, VarFlags.SQLITE3_LINK_INT | VarFlags.SQLITE3_LINK_READ_ONLY );
  6352. TCL.Tcl_LinkVar( interp, "sqlite_sync_count",
  6353. sqlite3_sync_count, VarFlags.SQLITE3_LINK_INT );
  6354. TCL.Tcl_LinkVar( interp, "sqlite_fullsync_count",
  6355. sqlite3_fullsync_count, VarFlags.SQLITE3_LINK_INT );
  6356. #if (SQLITE_ENABLE_FTS3) && (SQLITE_TEST)
  6357. TCL.Tcl_LinkVar(interp, "sqlite_fts3_enable_parentheses",
  6358. (char)&sqlite3_fts3_enable_parentheses, TCL.Tcl_LINK_INT);
  6359. #endif
  6360. return TCL.TCL_OK;
  6361. }
  6362. }
  6363. #endif
  6364. }