Add an experimental API for retrieving the SQL source from a compiled statement: sqlite3_sql(). Ticket #2769. (CVS 4543)

FossilOrigin-Name: d31f1e0d74a871d66cf7d3ef35faae5171d5cbc3
diff --git a/manifest b/manifest
index 547d93a..a60caff 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Modify\sthe\sTcl\sinterface\sto\suse\ssqlite3_prepare_v2().\s(CVS\s4542)
-D 2007-11-13T10:30:25
+C Add\san\sexperimental\sAPI\sfor\sretrieving\sthe\sSQL\ssource\sfrom\sa\scompiled\sstatement:\ssqlite3_sql().\sTicket\s#2769.\s(CVS\s4543)
+D 2007-11-14T06:48:48
 F Makefile.arm-wince-mingw32ce-gcc ac5f7b2cef0cd850d6f755ba6ee4ab961b1fadf7
 F Makefile.in 30c7e3ba426ddb253b8ef037d1873425da6009a8
 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -126,18 +126,18 @@
 F src/pager.h d783e7f184afdc33adff37ba58d4e029bd8793b3
 F src/parse.y 9640f5b930b1d0bfd05d7428da6f762305567cbb
 F src/pragma.c cb1486e76dbcad757968afc4083d3472032e62b5
-F src/prepare.c ab2374102d8367ad336d160a06cef9a8c96d8964
+F src/prepare.c 5dd06102c4c538fcbb9c71d35e505abb9fcbd269
 F src/printf.c 96c8d55315a13fc53cb3754cb15046f3ff891ea2
 F src/random.c 4a22746501bf36b0a088c66e38dde5daba6a35da
 F src/select.c aef87a179e287c4ab864cb927c1e95f5ee66bf45
 F src/server.c 087b92a39d883e3fa113cae259d64e4c7438bc96
 F src/shell.c 5b950381f6fb030f123fcd41ae3fdf431c9b0689
-F src/sqlite.h.in 3844177d389d58bd4a8be4ee81eefb01a084ed72
+F src/sqlite.h.in a8ab7453e036656658e263a4542c6b27db0537cb
 F src/sqlite3ext.h a93f59cdee3638dc0c9c086f80df743a4e68c3cb
 F src/sqliteInt.h 4e6fdeb5630ead97bcec60b941e7a72203c64b9e
 F src/sqliteLimit.h 15ffe2116746c27ace2b428a26a4fcd6dba6fa65
 F src/table.c 1aeb9eab57b4235db86fe15a35dec76fb445a9c4
-F src/tclsqlite.c c5953aedd61f0c0cb9f75dcd371e2ae10f899813
+F src/tclsqlite.c 9923abeffc9b3d7dad58e92b319661521f60debf
 F src/test1.c 0e6fe5449ef9e7289bcaf904e9e3a2ea20650b32
 F src/test2.c 77b34303883b9d722c65a6879bb0163a400e3789
 F src/test3.c 73c1fd55d1ece61f295a6b9204fd97a139de86ae
@@ -167,10 +167,10 @@
 F src/util.c 05f31144bbd3f1a24f4139ae029c42545cb72624
 F src/vacuum.c a5e51c77370c1a6445e86d42abfc43867cdd482d
 F src/vdbe.c 791d056da2c264c2cfed6e2150852926845875e5
-F src/vdbe.h 856360de71d4bdda71fbb534087668667924e41a
+F src/vdbe.h 79e09ff13b85457abe437d9814454534ebbc1fe3
 F src/vdbeInt.h 630145b9bfaa19190ab491f52658a7db550f2247
 F src/vdbeapi.c eecea7fa87e20664acf270f17c6f397421e9344b
-F src/vdbeaux.c 4b6fb200f3f35fb5983f39ee8527025a372aaa88
+F src/vdbeaux.c ffc2610c0d29a6e7b5c1d5dfea2ad406f7f9aff1
 F src/vdbeblob.c 82f51cdf9b0c0af729732fde48c824e498c0a1ca
 F src/vdbefifo.c 334c838c8f42d61a94813d136019ee566b5dc2f6
 F src/vdbemem.c 123994fcd344993d2fb050a83b91b341bbbd08b4
@@ -587,7 +587,7 @@
 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
 F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b
 F www/whentouse.tcl fc46eae081251c3c181bd79c5faef8195d7991a5
-P 8ad0ab8cb374bd34e47af9e71b2aad9dd9af0d1b
-R 7f804fc953f25759b567beb020ffc09d
+P 7bb00c7df672cb8988121f30e3bbdea9ad951603
+R 5c4df6521dfe4d4463c8657cb223a380
 U danielk1977
-Z de3fed23e51ed80655b9fa7a173fd027
+Z d57498e72dddc8893fb5fcae7c38aca8
diff --git a/manifest.uuid b/manifest.uuid
index d55f803..e7299dd 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-7bb00c7df672cb8988121f30e3bbdea9ad951603
\ No newline at end of file
+d31f1e0d74a871d66cf7d3ef35faae5171d5cbc3
\ No newline at end of file
diff --git a/src/prepare.c b/src/prepare.c
index ea6a4f4..28621fa 100644
--- a/src/prepare.c
+++ b/src/prepare.c
@@ -13,7 +13,7 @@
 ** interface, and routines that contribute to loading the database schema
 ** from disk.
 **
-** $Id: prepare.c,v 1.63 2007/11/13 10:30:26 danielk1977 Exp $
+** $Id: prepare.c,v 1.64 2007/11/14 06:48:48 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -629,7 +629,7 @@
   sqlite3 *db;
 
   assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
-  zSql = sqlite3VdbeGetSql(p);
+  zSql = sqlite3_sql((sqlite3_stmt *)p);
   if( zSql==0 ){
     return 0;
   }
diff --git a/src/sqlite.h.in b/src/sqlite.h.in
index cb1adf2..a169b8c 100644
--- a/src/sqlite.h.in
+++ b/src/sqlite.h.in
@@ -30,7 +30,7 @@
 ** the version number) and changes its name to "sqlite3.h" as
 ** part of the build process.
 **
-** @(#) $Id: sqlite.h.in,v 1.269 2007/11/05 17:54:17 drh Exp $
+** @(#) $Id: sqlite.h.in,v 1.270 2007/11/14 06:48:48 danielk1977 Exp $
 */
 #ifndef _SQLITE3_H_
 #define _SQLITE3_H_
@@ -1595,6 +1595,23 @@
 );
 
 /*
+** Retrieve the original SQL statement associated with a compiled statement
+** in UTF-8 encoding.
+**
+** If the compiled SQL statement passed as an argument was compiled using
+** either sqlite3_prepare_v2 or sqlite3_prepare16_v2, then this function
+** returns a pointer to a nul-terminated string containing a copy of
+** the original SQL statement. The pointer is valid until the statement
+** is deleted using sqlite3_finalize().
+**
+** If the statement was compiled using either of the legacy interfaces 
+** sqlite3_prepare() or sqlite3_prepare16(), this function returns NULL.
+** 
+****** EXPERIMENTAL - subject to change without notice **************
+*/
+const char *sqlite3_sql(sqlite3_stmt *pStmt);
+
+/*
 ** CAPI3REF:  Dynamically Typed Value Object
 **
 ** SQLite uses dynamic typing for the values it stores.  Values can 
diff --git a/src/tclsqlite.c b/src/tclsqlite.c
index 6a94341..a151bb6 100644
--- a/src/tclsqlite.c
+++ b/src/tclsqlite.c
@@ -12,7 +12,7 @@
 ** A TCL Interface to SQLite.  Append this file to sqlite3.c and
 ** compile the whole thing to build a TCL-enabled version of SQLite.
 **
-** $Id: tclsqlite.c,v 1.206 2007/11/13 10:30:26 danielk1977 Exp $
+** $Id: tclsqlite.c,v 1.207 2007/11/14 06:48:48 danielk1977 Exp $
 */
 #include "tcl.h"
 #include <errno.h>
@@ -85,7 +85,7 @@
   SqlPreparedStmt *pPrev;  /* Previous on the list */
   sqlite3_stmt *pStmt;     /* The prepared statement */
   int nSql;                /* chars in zSql[] */
-  char zSql[1];            /* Text of the SQL statement */
+  const char *zSql;        /* Text of the SQL statement */
 };
 
 typedef struct IncrblobChannel IncrblobChannel;
@@ -1813,12 +1813,13 @@
         */
         if( pPreStmt==0 ){
           len = zLeft - zSql;
-          pPreStmt = (SqlPreparedStmt*)Tcl_Alloc( sizeof(*pPreStmt) + len );
+          pPreStmt = (SqlPreparedStmt*)Tcl_Alloc( sizeof(*pPreStmt) );
           if( pPreStmt==0 ) return TCL_ERROR;
           pPreStmt->pStmt = pStmt;
           pPreStmt->nSql = len;
-          memcpy(pPreStmt->zSql, zSql, len);
-          pPreStmt->zSql[len] = 0;
+          pPreStmt->zSql = sqlite3_sql(pStmt);
+          assert( strlen(pPreStmt->zSql)==len );
+          assert( 0==memcmp(pPreStmt->zSql, zSql, len) );
         }
 
         /* Add the prepared statement to the beginning of the cache list
diff --git a/src/vdbe.h b/src/vdbe.h
index c228076..eee9db2 100644
--- a/src/vdbe.h
+++ b/src/vdbe.h
@@ -15,7 +15,7 @@
 ** or VDBE.  The VDBE implements an abstract machine that runs a
 ** simple program to access and modify the underlying database.
 **
-** $Id: vdbe.h,v 1.114 2007/10/23 15:39:45 drh Exp $
+** $Id: vdbe.h,v 1.115 2007/11/14 06:48:48 danielk1977 Exp $
 */
 #ifndef _SQLITE_VDBE_H_
 #define _SQLITE_VDBE_H_
@@ -140,7 +140,6 @@
 void sqlite3VdbeCountChanges(Vdbe*);
 sqlite3 *sqlite3VdbeDb(Vdbe*);
 void sqlite3VdbeSetSql(Vdbe*, const char *z, int n);
-const char *sqlite3VdbeGetSql(Vdbe*);
 void sqlite3VdbeSwap(Vdbe*,Vdbe*);
 
 #ifndef NDEBUG
diff --git a/src/vdbeaux.c b/src/vdbeaux.c
index 14f6637..a0a7874 100644
--- a/src/vdbeaux.c
+++ b/src/vdbeaux.c
@@ -60,8 +60,8 @@
 /*
 ** Return the SQL associated with a prepared statement
 */
-const char *sqlite3VdbeGetSql(Vdbe *p){
-  return p->zSql;
+const char *sqlite3_sql(sqlite3_stmt *pStmt){
+  return ((Vdbe *)pStmt)->zSql;
 }
 
 /*