From: drh Date: Thu, 7 May 2015 14:41:56 +0000 (+0000) Subject: Enhance the dbstat virtual table with the ability to analyze ATTACHed X-Git-Tag: version-3.8.10.1~6 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=857df26b856d7017607dc0e3f6bf5f409d606653;p=thirdparty%2Fsqlite.git Enhance the dbstat virtual table with the ability to analyze ATTACHed databases. FossilOrigin-Name: 25ec09400b753fcb10a2aae57eb43dbf0548b7ca --- diff --git a/manifest b/manifest index b7d0b1b60e..0cbe50bfcb 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Version\s3.8.10 -D 2015-05-07T11:53:08.287 +C Enhance\sthe\sdbstat\svirtual\stable\swith\sthe\sability\sto\sanalyze\sATTACHed\ndatabases. +D 2015-05-07T14:41:56.372 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 08728ecbeddca339c77bfd564d3484b523dffdb1 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -181,7 +181,7 @@ F src/callback.c 7b44ce59674338ad48b0e84e7b72f935ea4f68b0 F src/complete.c a5cf5b4b56390cfb7b8636e8f7ddef90258dd575 F src/ctime.c 98f89724adc891a1a4c655bee04e33e716e05887 F src/date.c e4d50b3283696836ec1036b695ead9a19e37a5ac -F src/dbstat.c 1eacd310212b5ae59b7be645a06de8f8bbe0b5d6 +F src/dbstat.c a9c0550fe90b765e1ac38760e1822bb7a1dfe857 F src/delete.c 37964e6c1d73ff49cbea9ff690c9605fb15f600e F src/expr.c 3fb2ab3ab69d15b4b75ae53fceb4e317f64cb306 F src/fault.c 160a0c015b6c2629d3899ed2daf63d75754a32bb @@ -902,7 +902,7 @@ F test/speed4p.test 0e51908951677de5a969b723e03a27a1c45db38b F test/speedtest1.c 2b416dca3a155fcaa849540b2e7fc1df12896c23 F test/spellfix.test 24f676831acddd2f4056a598fd731a72c6311f49 F test/sqllimits1.test e05786eaed7950ff6a2d00031d001d8a26131e68 -F test/stat.test 76fd746b85459e812a0193410fb599f0531f22de +F test/stat.test 8de91498c99f5298b303f70f1d1f3b9557af91bf F test/statfault.test f525a7bf633e50afd027700e9a486090684b1ac1 F test/stmt.test 25d64e3dbf9a3ce89558667d7f39d966fe2a71b9 F test/subquery.test d7268d193dd33d5505df965399d3a594e76ae13f @@ -1256,10 +1256,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P 5f2539da8cb9df99029ab4ab7023804722697673 -R 014968d54fc9fdc23120a39f420a5f8f -T +bgcolor * #d0c0ff -T +sym-release * -T +sym-version-3.8.10 * +P cf975957b9ae671f34bb65f049acf351e650d437 +R e2036c834faa435fbd4ece874ab9ac24 U drh -Z 6270ca72fe7be4337004b813fe656c7c +Z cef1b7912e822bf40819db2b70a8eba6 diff --git a/manifest.uuid b/manifest.uuid index 8e4d2fce57..dde28b002b 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -cf975957b9ae671f34bb65f049acf351e650d437 \ No newline at end of file +25ec09400b753fcb10a2aae57eb43dbf0548b7ca \ No newline at end of file diff --git a/src/dbstat.c b/src/dbstat.c index fb5a52b791..8351926dcc 100644 --- a/src/dbstat.c +++ b/src/dbstat.c @@ -122,6 +122,7 @@ struct StatCursor { struct StatTable { sqlite3_vtab base; sqlite3 *db; + int iDb; /* Index of database to analyze */ }; #ifndef get2byte @@ -140,7 +141,17 @@ static int statConnect( ){ StatTable *pTab = 0; int rc = SQLITE_OK; + int iDb; + if( argc>=4 ){ + iDb = sqlite3FindDbName(db, argv[3]); + if( iDb<0 ){ + *pzErr = sqlite3_mprintf("no such database: %s", argv[3]); + return SQLITE_ERROR; + } + }else{ + iDb = 0; + } rc = sqlite3_declare_vtab(db, VTAB_SCHEMA); if( rc==SQLITE_OK ){ pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable)); @@ -151,6 +162,7 @@ static int statConnect( if( rc==SQLITE_OK ){ memset(pTab, 0, sizeof(StatTable)); pTab->db = db; + pTab->iDb = iDb; } *ppVtab = (sqlite3_vtab*)pTab; @@ -205,16 +217,22 @@ static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ if( pCsr==0 ){ rc = SQLITE_NOMEM; }else{ + char *zSql; memset(pCsr, 0, sizeof(StatCursor)); pCsr->base.pVtab = pVTab; - rc = sqlite3_prepare_v2(pTab->db, + zSql = sqlite3_mprintf( "SELECT 'sqlite_master' AS name, 1 AS rootpage, 'table' AS type" " UNION ALL " - "SELECT name, rootpage, type FROM sqlite_master WHERE rootpage!=0" - " ORDER BY name", -1, - &pCsr->pStmt, 0 - ); + "SELECT name, rootpage, type" + " FROM \"%w\".sqlite_master WHERE rootpage!=0" + " ORDER BY name", pTab->db->aDb[pTab->iDb].zName); + if( zSql==0 ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0); + sqlite3_free(zSql); + } if( rc!=SQLITE_OK ){ sqlite3_free(pCsr); pCsr = 0; @@ -380,7 +398,7 @@ static int statDecodePage(Btree *pBt, StatPage *p){ */ static void statSizeAndOffset(StatCursor *pCsr){ StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab; - Btree *pBt = pTab->db->aDb[0].pBt; + Btree *pBt = pTab->db->aDb[pTab->iDb].pBt; Pager *pPager = sqlite3BtreePager(pBt); sqlite3_file *fd; sqlite3_int64 x[2]; @@ -394,7 +412,7 @@ static void statSizeAndOffset(StatCursor *pCsr){ */ fd = sqlite3PagerFile(pPager); x[0] = pCsr->iPageno; - if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){ + if( fd->pMethods!=0 && sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){ pCsr->iOffset = x[0]; pCsr->szPage = (int)x[1]; } @@ -408,7 +426,7 @@ static int statNext(sqlite3_vtab_cursor *pCursor){ int nPayload; StatCursor *pCsr = (StatCursor *)pCursor; StatTable *pTab = (StatTable *)pCursor->pVtab; - Btree *pBt = pTab->db->aDb[0].pBt; + Btree *pBt = pTab->db->aDb[pTab->iDb].pBt; Pager *pPager = sqlite3BtreePager(pBt); sqlite3_free(pCsr->zPath); diff --git a/test/stat.test b/test/stat.test index f0447e4509..57c1b9eae1 100644 --- a/test/stat.test +++ b/test/stat.test @@ -166,8 +166,10 @@ sqlite3 db test.db register_dbstat_vtab db do_execsql_test stat-5.1 { PRAGMA auto_vacuum = OFF; - CREATE VIRTUAL TABLE temp.stat USING dbstat; - CREATE TABLE t1(x); + CREATE TABLE tx(y); + ATTACH ':memory:' AS aux1; + CREATE VIRTUAL TABLE temp.stat USING dbstat(aux1); + CREATE TABLE aux1.t1(x); INSERT INTO t1 VALUES(zeroblob(1513)); INSERT INTO t1 VALUES(zeroblob(1514)); SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload @@ -178,4 +180,8 @@ do_execsql_test stat-5.1 { t1 /001+000000 4 overflow 0 1020 0 0 \ ] +do_catchsql_test stat-6.1 { + CREATE VIRTUAL TABLE temp.s2 USING dbstat(mainx); +} {1 {no such database: mainx}} + finish_test