This is an automated email from the ASF dual-hosted git repository.

thomasm pushed a commit to branch OAK-10527
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git


The following commit(s) were added to refs/heads/OAK-10527 by this push:
     new c7061755b2 OAK-10527 Improve readability of the explain query output
c7061755b2 is described below

commit c7061755b2d721a9cef86a1633c526378f2e9922
Author: Thomas Mueller <[email protected]>
AuthorDate: Fri Nov 3 15:20:25 2023 +0100

    OAK-10527 Improve readability of the explain query output
---
 .../jackrabbit/oak/query/QueryFormatter.java       | 20 +++++++++++---
 .../jackrabbit/oak}/query/QueryFormatterTest.java  | 31 +++++++++++++++++++---
 2 files changed, 44 insertions(+), 7 deletions(-)

diff --git 
a/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java 
b/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java
index 7f73da0f1e..f3a474bd32 100644
--- a/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java
@@ -86,9 +86,16 @@ public class QueryFormatter {
                     // skip
                 }
             } else if (c =='[') {
-                buff.insert(i + 1, "\n  ");
-                i += 3;
-            } else if (c <= ' ') {
+                if (i + 1 < buff.length() && buff.charAt(i + 1) > ' ') {
+                    buff.insert(i + 1, "\n  ");
+                    i += 3;
+                }
+            } else if (c == '\n') {
+                // already formatted
+                while (++i < buff.length() && buff.charAt(i) == ' ') {
+                    // skip
+                }
+            } else if (c == ' ') {
                 String sub = buff.substring(i, Math.min(i + 10, 
buff.length()));
                 if (sub.startsWith(" and ")
                         || sub.startsWith(" or ")
@@ -112,7 +119,12 @@ public class QueryFormatter {
                 while (++i < buff.length() && buff.charAt(i) != c) {
                     // skip
                 }
-            } else if (c <= ' ') {
+            } else if (c == '\n') {
+                // already formatted
+                while (++i < buff.length() && buff.charAt(i) == ' ') {
+                    // skip
+                }
+            } else if (c == ' ') {
                 String sub = buff.substring(i, Math.min(i + 10, 
buff.length()));
                 if (startsWithIgnoreCase(sub, " and ")
                         || startsWithIgnoreCase(sub, " or ")
diff --git 
a/oak-lucene/src/test/java/org/apache/jackrabbit/oak/jcr/query/QueryFormatterTest.java
 
b/oak-core/src/test/java/org/apache/jackrabbit/oak/query/QueryFormatterTest.java
similarity index 86%
rename from 
oak-lucene/src/test/java/org/apache/jackrabbit/oak/jcr/query/QueryFormatterTest.java
rename to 
oak-core/src/test/java/org/apache/jackrabbit/oak/query/QueryFormatterTest.java
index bd7417bf89..8dc8fd0cdc 100644
--- 
a/oak-lucene/src/test/java/org/apache/jackrabbit/oak/jcr/query/QueryFormatterTest.java
+++ 
b/oak-core/src/test/java/org/apache/jackrabbit/oak/query/QueryFormatterTest.java
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.jackrabbit.oak.jcr.query;
+package org.apache.jackrabbit.oak.query;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -142,12 +142,37 @@ public class QueryFormatterTest {
                 + "  order by @z",
                 QueryFormatter.format("/jcr:root//*[@x=' and '' and ' or @y=\" 
or \"] order by @z", null));
         assertEquals(
-                "/jcr:root//*[\n"
-                + "  ",
+                "/jcr:root//*[",
                 QueryFormatter.format("/jcr:root//*[", null));
         assertEquals(
                 "/jcr:root//*[\n"
                 + "  @a='",
                 QueryFormatter.format("/jcr:root//*[@a='", null));
     }
+
+    @Test
+    public void alreadyFormatted() {
+        assertEquals("/jcr:root//*[\n"
+                + "  @a=1\n"
+                + "  and @b=2\n"
+                + "  or @c=3]\n"
+                + "  order by @d\n"
+                + "  option(traversal ok)",
+                QueryFormatter.format(
+                "/jcr:root//*[\n"
+                + "  @a=1\n"
+                + "  and @b=2\n"
+                + "  or @c=3]\n"
+                + "  order by @d\n"
+                + "  option(traversal ok)", null));
+        assertEquals(
+                "select \" from  \"\" union \"\n"
+                + "  from ...\n"
+                + "  option(...)",
+                QueryFormatter.format(
+                "select \" from  \"\" union \"\n"
+                        + "  from ...\n"
+                        + "  option(...)",
+                null));
+    }
 }

Reply via email to