From: drh <> Date: Mon, 16 Mar 2026 15:35:41 +0000 (+0000) Subject: Rework the ".indexes" command of the CLI. (1) The PATTERN now matches X-Git-Tag: major-release~79 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bf057073c3f101dd67a7ca5fa763f80c09c6389a;p=thirdparty%2Fsqlite.git Rework the ".indexes" command of the CLI. (1) The PATTERN now matches the name of the index itself, not the name of the table containing the index, thus making the pattern actually useful. (2) System-generated indexes (for UNIQUE constraints and similar) are omitted unless the -a|-all or --sys options are added. (3) The --expr option causes only expression indexes to be shown. FossilOrigin-Name: 13e2cad810bfd59a9e18eb38bda1241715b76756ffec7d617e9ce99b89f19284 --- diff --git a/manifest b/manifest index 86a1bf4c10..cbeab1a0b1 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Properly\sfix\stemp\striggers\screated\sas\spart\sof\sFK\sprocessing\sto\stheir\sschemas.\sOtherwise\sthey\smay\sbecome\sconfused\sby\ssimilarly\snamed\schild\stables\sin\sother\sattached\sdatabases.\sFix\sfor\sforum\spost\s[forum:636bd0180a\s|\s636bd0180a]. -D 2026-03-16T11:14:26.814 +C Rework\sthe\s".indexes"\scommand\sof\sthe\sCLI.\s\s(1)\sThe\sPATTERN\snow\smatches\nthe\sname\sof\sthe\sindex\sitself,\snot\sthe\sname\sof\sthe\stable\scontaining\nthe\sindex,\sthus\smaking\sthe\spattern\sactually\suseful.\s\s(2)\s\sSystem-generated\nindexes\s(for\sUNIQUE\sconstraints\sand\ssimilar)\sare\somitted\sunless\nthe\s-a|-all\sor\s--sys\soptions\sare\sadded.\s\s(3)\sThe\s--expr\soption\scauses\nonly\sexpression\sindexes\sto\sbe\sshown. +D 2026-03-16T15:35:41.736 F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea @@ -733,7 +733,7 @@ F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c F src/resolve.c 928ff887f2a7c64275182060d94d06fdddbe32226c569781cf7e7edc6f58d7fd F src/rowset.c 8432130e6c344b3401a8874c3cb49fefe6873fec593294de077afea2dce5ec97 F src/select.c ffe199f025a0dd74670d2a77232bdea364a4d7b36f32c64a6572d39ba6a11576 -F src/shell.c.in 5bfc926b756d5816fcaf56a30c3a363ba09ac09a9f6a5a7eef1b27c8bac8f6c4 +F src/shell.c.in 9b655b63ff8c15f04b23632d648cc2a7285882e6c2126fb9357b5b70e2454a0e F src/sqlite.h.in 4d657846d68a58b028f0c4c331b9d3b4a79306f25c3b0d04fb56060343f73d85 F src/sqlite3.rc 015537e6ac1eec6c7050e17b616c2ffe6f70fca241835a84a4f0d5937383c479 F src/sqlite3ext.h 1b7a0ee438bb5c2896d0609c537e917d8057b3340f6ad004d2de44f03e3d3cca @@ -1615,7 +1615,7 @@ F test/sharedA.test 64bdd21216dda2c6a3bd3475348ccdc108160f34682c97f2f51c19fc0e21 F test/sharedB.test 1a84863d7a2204e0d42f2e1606577c5e92e4473fa37ea0f5bdf829e4bf8ee707 F test/shared_err.test 32634e404a3317eeb94abc7a099c556a346fdb8fb3858dbe222a4cbb8926a939 F test/sharedlock.test 5ede3c37439067c43b0198f580fd374ebf15d304 -F test/shell1.test d714be78c063cfb5f0933dcf2b5d9df62ef57f8eaf8848bbd0026a9d252d4db5 +F test/shell1.test eda2e527435f139224dda67db6bbd2466597408d4fe5883d647d67fa32d88f7c F test/shell2.test dc541d2681503e55466a24d35a4cbf8ca5b90b8fcdef37fc4db07373a67d31d3 F test/shell3.test 91efdd545097a61a1f72cf79c9ad5b49da080f3f10282eaf4c3c272cd1012db2 F test/shell4.test e25580a792b7b54560c3a76b6968bd8189261f38979fe28e6bc6312c5db280db @@ -2192,8 +2192,8 @@ F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee F tool/warnings.sh d924598cf2f55a4ecbc2aeb055c10bd5f48114793e7ba25f9585435da29e7e98 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f F tool/winmain.c 00c8fb88e365c9017db14c73d3c78af62194d9644feaf60e220ab0f411f3604c -P dc78d258745a1350685b37da56f0a2ac2e437b422415d634522dc61d340d06eb -R f4cdffcb37cefef0e7ac6ef7ca48fea4 -U dan -Z d215803567bcaa7157c0e213d8c093a7 +P 80bc5bc07e221f837c28066f0a438f11c8ab6be4c8ba93615439eb1667967003 +R 3ad37f8e404f71b8ecba081b76070ad6 +U drh +Z d82e0cbe9c28448c805d23a2e1afab0e # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 35c327cc26..e063f11647 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -80bc5bc07e221f837c28066f0a438f11c8ab6be4c8ba93615439eb1667967003 +13e2cad810bfd59a9e18eb38bda1241715b76756ffec7d617e9ce99b89f19284 diff --git a/src/shell.c.in b/src/shell.c.in index 1aa6e442de..b7935fbe3d 100644 --- a/src/shell.c.in +++ b/src/shell.c.in @@ -3790,9 +3790,10 @@ static const char *(azHelp[]) = { #ifndef SQLITE_OMIT_TEST_CONTROL ".imposter INDEX TABLE Create imposter table TABLE on index INDEX", #endif - ".indexes ?TABLE? Show names of indexes", - " If TABLE is specified, only show indexes for", - " tables matching TABLE using the LIKE operator.", + ".indexes ?PATTERN? Show names of indexes matching PATTERN", + " -a|--all Also show system-generated indexes", + " --expr Show only expression indexes", + " --sys Show only system-generated indexes", ".intck ?STEPS_PER_UNLOCK? Run an incremental integrity check on the db", #ifdef SQLITE_ENABLE_IOTRACE ",iotrace FILE Enable I/O diagnostic logging to FILE", @@ -9869,6 +9870,92 @@ static int do_meta_command(const char *zLine, ShellState *p){ }else #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */ + if( c=='i' && (cli_strncmp(azArg[0], "indices", n)==0 + || cli_strncmp(azArg[0], "indexes", n)==0) + ){ + sqlite3_str *pSql; + int i; + int allFlag = 0; + int sysFlag = 0; + int exprFlag = 0; + int debugFlag = 0; /* Undocument --debug flag */ + const char *zPattern = 0; + const char *zSep = "WHERE"; + + for(i=1; idb); + sqlite3_str_appendf(pSql, + "SELECT if(t.schema='main',i.name,t.schema||'.'||i.name)\n" + "FROM pragma_table_list t, pragma_index_list(t.name,t.schema) i\n" + ); + if( exprFlag ){ + allFlag = 0; + sqlite3_str_appendf(pSql, + "%s (EXISTS(SELECT 1 FROM pragma_index_xinfo(i.name) WHERE cid=-2)\n" + " OR\n" + " EXISTS(SELECT cid FROM pragma_table_xinfo(t.name) WHERE hidden=2" + " INTERSECT " + " SELECT cid FROM pragma_index_info(i.name)))\n", zSep); + zSep = "AND"; + } + if( sysFlag ){ + sqlite3_str_appendf(pSql, + "%s i.name LIKE 'sqlite__autoindex__%%' ESCAPE '_'\n", zSep); + zSep = "AND"; + }else if( !allFlag ){ + sqlite3_str_appendf(pSql, + "%s i.name NOT LIKE 'sqlite__%%' ESCAPE '_'\n", zSep); + zSep = "AND"; + } + if( zPattern ){ + sqlite3_str_appendf(pSql, "%s i.name LIKE '%%%q%%'\n", zSep, zPattern); + } + sqlite3_str_appendf(pSql, "ORDER BY 1"); + + /* Run the SQL statement in "split" mode. */ + if( debugFlag ){ + cli_printf(stdout,"%s;\n", sqlite3_str_value(pSql)); + }else{ + modePush(p); + modeChange(p, MODE_Split); + shell_exec(p, sqlite3_str_value(pSql), 0); + modePop(p); + } + sqlite3_str_free(pSql); + }else + if( c=='i' && cli_strncmp(azArg[0], "intck", n)==0 ){ i64 iArg = 0; if( nArg==2 ){ @@ -11246,10 +11333,7 @@ static int do_meta_command(const char *zLine, ShellState *p){ } }else - if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0) - || (c=='i' && (cli_strncmp(azArg[0], "indices", n)==0 - || cli_strncmp(azArg[0], "indexes", n)==0) ) - ){ + if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0) ){ sqlite3_stmt *pStmt; sqlite3_str *pSql; const char *zPattern = nArg>1 ? azArg[1] : 0; @@ -11261,15 +11345,6 @@ static int do_meta_command(const char *zLine, ShellState *p){ return shellDatabaseError(p->db); } - if( nArg>2 && c=='i' ){ - /* It is an historical accident that the .indexes command shows an error - ** when called with the wrong number of arguments whereas the .tables - ** command does not. */ - eputz("Usage: .indexes ?LIKE-PATTERN?\n"); - rc = 1; - sqlite3_finalize(pStmt); - goto meta_command_exit; - } pSql = sqlite3_str_new(p->db); while( sqlite3_step(pStmt)==SQLITE_ROW ){ const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); @@ -11283,19 +11358,12 @@ static int do_meta_command(const char *zLine, ShellState *p){ sqlite3_str_appendf(pSql, "SELECT %Q||'.'||name FROM ", zDbName); } sqlite3_str_appendf(pSql, "\"%w\".sqlite_schema", zDbName); - if( c=='t' ){ - sqlite3_str_appendf(pSql, - " WHERE type IN ('table','view')" - " AND name NOT LIKE 'sqlite__%%' ESCAPE '_'" - ); - if( zPattern ){ - sqlite3_str_appendf(pSql," AND name LIKE %Q", zPattern); - } - }else{ - sqlite3_str_appendf(pSql, " WHERE type='index'"); - if( zPattern ){ - sqlite3_str_appendf(pSql," AND tbl_name LIKE %Q", zPattern); - } + sqlite3_str_appendf(pSql, + " WHERE type IN ('table','view')" + " AND name NOT LIKE 'sqlite__%%' ESCAPE '_'" + ); + if( zPattern ){ + sqlite3_str_appendf(pSql," AND name LIKE %Q", zPattern); } } rc = sqlite3_finalize(pStmt); diff --git a/test/shell1.test b/test/shell1.test index 162f5f3e86..4f12bc0964 100644 --- a/test/shell1.test +++ b/test/shell1.test @@ -445,7 +445,8 @@ do_test shell1-3.12.2-legacy { do_test shell1-3.12.3 { # too many arguments catchcmd "test.db" ".indexes FOO BAD" -} {1 {Usage: .indexes ?LIKE-PATTERN?}} +} {1 {line 1: .indexes FOO BAD +line 1: ^--- unknown argument}} # .mode MODE ?TABLE? Set output mode where MODE is one of: # ascii Columns/rows delimited by 0x1F and 0x1E