From 4960a715a0856f4a5717dfc36799ff17654a852d Mon Sep 17 00:00:00 2001 From: Piotr Sarna Date: Tue, 15 Nov 2022 12:01:40 +0100 Subject: [PATCH] shell: add .walmethodslist The command is heavily inspired by .vfslist and lists all the registered custom WAL methods. --- src/main.c | 5 +++++ src/pager.c | 9 +++++++++ src/pager.h | 3 +++ src/shell.c.in | 18 ++++++++++++++++++ src/sqlite.h.in | 13 +++++++++++++ src/wal.c | 8 ++++++++ 6 files changed, 56 insertions(+) diff --git a/src/main.c b/src/main.c index 98711e3493..0c4db6e857 100644 --- a/src/main.c +++ b/src/main.c @@ -3975,6 +3975,11 @@ int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){ }else if( op==SQLITE_FCNTL_DATA_VERSION ){ *(unsigned int*)pArg = sqlite3PagerDataVersion(pPager); rc = SQLITE_OK; +#ifndef SQLITE_OMIT_WAL + }else if( op==SQLITE_FCNTL_WAL_METHODS_POINTER ){ + *(libsql_wal_methods**)pArg = sqlite3PagerWalMethods(pPager); + rc = SQLITE_OK; +#endif }else if( op==SQLITE_FCNTL_RESERVE_BYTES ){ int iNew = *(int*)pArg; *(int*)pArg = sqlite3BtreeGetRequestedReserve(pBtree); diff --git a/src/pager.c b/src/pager.c index 53769e5895..07b665def2 100644 --- a/src/pager.c +++ b/src/pager.c @@ -7050,6 +7050,15 @@ sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){ return pPager->pVfs; } +#ifndef SQLITE_OMIT_WAL +/* +** Return the WAL methods structure for the pager. +*/ +libsql_wal_methods *sqlite3PagerWalMethods(Pager *pPager){ + return pPager->pWalMethods; +} +#endif + /* ** Return the file handle for the database file associated ** with the pager. This might return NULL if the file has diff --git a/src/pager.h b/src/pager.h index d2634fe34d..3fabb70afe 100644 --- a/src/pager.h +++ b/src/pager.h @@ -214,6 +214,9 @@ u32 sqlite3PagerDataVersion(Pager*); int sqlite3PagerMemUsed(Pager*); const char *sqlite3PagerFilename(const Pager*, int); sqlite3_vfs *sqlite3PagerVfs(Pager*); +#ifndef SQLITE_OMIT_WAL +libsql_wal_methods *sqlite3PagerWalMethods(Pager*); +#endif sqlite3_file *sqlite3PagerFile(Pager*); sqlite3_file *sqlite3PagerJrnlFile(Pager*); const char *sqlite3PagerJournalname(Pager*); diff --git a/src/shell.c.in b/src/shell.c.in index c682fb878f..788166560b 100644 --- a/src/shell.c.in +++ b/src/shell.c.in @@ -10836,6 +10836,24 @@ static int do_meta_command(char *zLine, ShellState *p){ } }else +#ifndef SQLITE_OMIT_WAL + if( c=='w' && cli_strncmp(azArg[0], "walmethodslist", n)==0 ){ + libsql_wal_methods *pWal; + libsql_wal_methods *pCurrent = 0; + if( p->db ){ + sqlite3_file_control(p->db, "main", SQLITE_FCNTL_WAL_METHODS_POINTER, &pCurrent); + } + for(pWal=libsql_wal_methods_find(0); pWal; pWal=libsql_wal_methods_next(pWal)){ + + utf8_printf(p->out, "wal.zName = \"%s\"%s\n", libsql_wal_methods_name(pWal), + pWal==pCurrent ? " <--- CURRENT" : ""); + if( libsql_wal_methods_next(pWal) ){ + raw_printf(p->out, "-----------------------------------\n"); + } + } + }else +#endif + if( c=='w' && cli_strncmp(azArg[0], "wheretrace", n)==0 ){ unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff; sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x); diff --git a/src/sqlite.h.in b/src/sqlite.h.in index b45953a1b1..c0bc26fb7b 100644 --- a/src/sqlite.h.in +++ b/src/sqlite.h.in @@ -1012,6 +1012,13 @@ struct sqlite3_io_methods { ** ^When there are multiple VFS shims in the stack, this opcode finds the ** upper-most shim only. ** +**
  • [[SQLITE_FCNTL_WAL_METHODS_POINTER]] +** ^The [SQLITE_FCNTL_WAL_METHODS_POINTER] opcode finds a pointer to the top-level +** WAL methods currently in use. ^(The argument X in +** sqlite3_file_control(db,SQLITE_FCNTL_WAL_METHODS_POINTER,X) must be +** of type "[libsql_wal_methods] **". This opcodes will set *X +** to a pointer to the top-level WAL methods.)^ +** **
  • [[SQLITE_FCNTL_PRAGMA]] ** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA] ** file control is sent to the open [sqlite3_file] object corresponding @@ -1242,6 +1249,9 @@ struct sqlite3_io_methods { #define SQLITE_FCNTL_EXTERNAL_READER 40 #define SQLITE_FCNTL_CKSM_FILE 41 +// libSQL extension +#define SQLITE_FCNTL_WAL_METHODS_POINTER 129 + /* deprecated names */ #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE @@ -7733,6 +7743,9 @@ libsql_wal_methods *libsql_wal_methods_find(const char *zName); int libsql_wal_methods_register(libsql_wal_methods*); int libsql_wal_methods_unregister(libsql_wal_methods*); +libsql_wal_methods *libsql_wal_methods_next(libsql_wal_methods *w); +const char *libsql_wal_methods_name(libsql_wal_methods *w); + /* ** CAPI3REF: Mutexes ** diff --git a/src/wal.c b/src/wal.c index bf658cac9f..e7d190b756 100644 --- a/src/wal.c +++ b/src/wal.c @@ -4209,4 +4209,12 @@ int libsql_wal_methods_unregister(libsql_wal_methods *pWalMethods) { return SQLITE_OK; } +libsql_wal_methods *libsql_wal_methods_next(libsql_wal_methods *w) { + return w->pNext; +} + +const char *libsql_wal_methods_name(libsql_wal_methods *w) { + return w->zName; +} + #endif /* #ifndef SQLITE_OMIT_WAL */