]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Add the -groupbyparent option to the ".fkey_missing_indexes" command.
authordan <dan@noemail.net>
Thu, 15 Dec 2016 06:01:40 +0000 (06:01 +0000)
committerdan <dan@noemail.net>
Thu, 15 Dec 2016 06:01:40 +0000 (06:01 +0000)
FossilOrigin-Name: 976c51b4836dfba2ce9b246334a85bda08ac526f

manifest
manifest.uuid
src/shell.c

index 07bbf2b3e9eede68c963dfb153f710567c19825e..36ecb421028ef672d14ad08b3eb62c9fd0ad6ec4 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Add\sthe\sexperimental\s".fkey_missing_indexes"\scommand\sto\sthe\sshell\stool.\sTo\nidentify\sindexes\sthat\sshould\sbe\screated\son\schild\skeys\sif\sFK\sprocessing\sis\sto\nbe\senabled.
-D 2016-12-14T19:28:27.708
+C Add\sthe\s-groupbyparent\soption\sto\sthe\s".fkey_missing_indexes"\scommand.
+D 2016-12-15T06:01:40.711
 F Makefile.in c194b58fe00c370a48ac6ae6945e92a7781db1c8
 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
 F Makefile.msc b8ca53350ae545e3562403d5da2a69cec79308da
@@ -389,7 +389,7 @@ F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384
 F src/resolve.c bb070cf5f23611c44ab7e4788803684e385fc3fb
 F src/rowset.c 7b7e7e479212e65b723bf40128c7b36dc5afdfac
 F src/select.c 204491a5e09a66380a067943d8651af8bda1d358
-F src/shell.c bf722e55563acd5111fe36bc54ba1669cf9a6b22
+F src/shell.c 331be7c5d82477baff3ab584f1dc679e9676e0ef
 F src/sqlite.h.in e8e2d108d82647f0a812fdb74accf91c1ec08ddc
 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
 F src/sqlite3ext.h 8648034aa702469afb553231677306cc6492a1ae
@@ -1537,10 +1537,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 9cae4c2e300e20304ced0dc8c1415c4922185928
-R 9017289f90148307424d56c20b193c71
-T *branch * fkey-missing-indexes
-T *sym-fkey-missing-indexes *
-T -sym-trunk *
+P 7df23aca1f7c7b769d614d740b3fda3073f46ba9
+R 109aa4e43fd004b2c710d444b0c58b6c
 U dan
-Z 4ebf85f006949e0e6db45fa67eafcbbb
+Z 2faf32103da4e43d54e82bc173d9e560
index 354a93a4cc97577838f51dcad61945e3e0151b20..9760c01f9e7a95b0f8a337513209aef3740cf03a 100644 (file)
@@ -1 +1 @@
-7df23aca1f7c7b769d614d740b3fda3073f46ba9
\ No newline at end of file
+976c51b4836dfba2ce9b246334a85bda08ac526f
\ No newline at end of file
index 9fd2eec8a846d3b906628ab684e204dc9c3877a2..b0bd9c6ece163d775a743bfc92b9a23ac18f3fba 100644 (file)
@@ -3595,7 +3595,10 @@ static int shellFkeyMissingIndexes(
 ){
   sqlite3 *db = pState->db;
   FILE *out = pState->out;
-  int bVerbose = 0;
+  int bVerbose = 0;               /* If -verbose is present */
+  int bGroupByParent = 0;         /* If -groupbyparent is present */
+  int i;
+  const char *zIndent = "";
 
   int rc;
   sqlite3_stmt *pSql = 0;
@@ -3619,14 +3622,27 @@ static int shellFkeyMissingIndexes(
     "  || group_concat(quote(child_col) ||"
     "        fkey_collate_clause(parent, parent_col, child, child_col), ', ')"
     "  || ');'"
+    ", "
+    "     parent "
 
-    "FROM pragma_foreign_key_list AS o GROUP BY child, id"
+    "FROM pragma_foreign_key_list GROUP BY child, id "
+    "ORDER BY (CASE WHEN ? THEN parent ELSE CHILD END)"
   ;
 
-  if( nArg>2 ){
-    raw_printf(stderr, "Usage: .fkey_lint ?verbose-flag?\n");
+  for(i=1; i<nArg; i++){
+    int n = strlen(azArg[i]);
+    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
+      bVerbose = 1;
+    }
+    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
+      bGroupByParent = 1;
+      zIndent = "    ";
+    }
+    else{
+      raw_printf(stderr, "Usage: .fkey_lint ?-verbose? ?-groupbyparent?\n");
+      return SQLITE_ERROR;
+    }
   }
-  if( nArg==2 ) bVerbose = booleanValue(azArg[1]);
 
   /* Register the pragma eponymous virtual tables */
   rc = shellPragmaRegister(db);
@@ -3642,9 +3658,13 @@ static int shellFkeyMissingIndexes(
   if( rc==SQLITE_OK ){
     rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
   }
+  if( rc==SQLITE_OK ){
+    sqlite3_bind_int(pSql, 1, bGroupByParent);
+  }
 
   if( rc==SQLITE_OK ){
     int rc2;
+    char *zPrev = 0;
     while( SQLITE_ROW==sqlite3_step(pSql) ){
       int res = -1;
       sqlite3_stmt *pExplain = 0;
@@ -3653,6 +3673,7 @@ static int shellFkeyMissingIndexes(
       const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
       const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
       const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
+      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
 
       rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
       if( rc!=SQLITE_OK ) break;
@@ -3666,14 +3687,26 @@ static int shellFkeyMissingIndexes(
       if( res<0 ){
         raw_printf(stderr, "Error: internal error");
         break;
-      }else if( res==0 ){
-        raw_printf(out, "%s --> %s\n", zCI, zTarget);
-      }else if( bVerbose ){
-        raw_printf(out, "/* no extra indexes required for %s -> %s */\n", 
-            zFrom, zTarget
-        );
+      }else{
+        if( bGroupByParent 
+        && (bVerbose || res==0)
+        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev)) 
+        ){
+          raw_printf(out, "-- Parent table %s\n", zParent);
+          sqlite3_free(zPrev);
+          zPrev = sqlite3_mprintf("%s", zParent);
+        }
+
+        if( res==0 ){
+          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
+        }else if( bVerbose ){
+          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n", 
+              zIndent, zFrom, zTarget
+          );
+        }
       }
     }
+    sqlite3_free(zPrev);
 
     if( rc!=SQLITE_OK ){
       raw_printf(stderr, "%s\n", sqlite3_errmsg(db));