jdaugherty commented on code in PR #16631:
URL: https://github.com/apache/grails-core/pull/16631#discussion_r4366432130


##########
grails-datamapping-core/src/main/groovy/org/apache/grails/data/core/querytail/H2SqlSyntax.groovy:
##########
@@ -0,0 +1,102 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.grails.data.core.querytail
+
+import groovy.transform.CompileStatic
+import groovy.transform.PackageScope
+
+/**
+ * H2: block comments nest, {@code //} starts a line comment, {@code $$} 
quotes a string without a
+ * tag, backticks delimit a name in every compatibility mode, and square 
brackets delimit a name in
+ * the SQL Server mode, where they are never a subscript, which is what a 
bracket after a name, a
+ * closing parenthesis or a closing bracket is in the other modes. A bracket 
after whitespace, or
+ * after a comment, is a name in the SQL Server mode but opens an array 
literal after the
+ * {@code ARRAY} keyword, which may stand any distance before it, so a 
statement with one is read
+ * both ways: a name, which is recorded as written, agrees with itself, while 
an array literal,
+ * whose elements are values, does not and withholds the statement.
+ */
+@CompileStatic
+@PackageScope
+final class H2SqlSyntax extends StandardSqlSyntax {
+
+    private final boolean bracketAfterWhitespaceIsName
+
+    H2SqlSyntax() {
+        this(true)
+    }
+
+    private H2SqlSyntax(boolean bracketAfterWhitespaceIsName) {
+        super('H2')
+        this.bracketAfterWhitespaceIsName = bracketAfterWhitespaceIsName
+    }
+
+    @Override
+    List<QueryTailSyntax> readings(String sql) {
+        if (!hasBracketAfterWhitespace(sql)) {
+            return List.<QueryTailSyntax>of(this)
+        }
+        List.<QueryTailSyntax>of(new H2SqlSyntax(true), new H2SqlSyntax(false))
+    }
+
+    @Override
+    int endOfComment(String sql, int index) {
+        sql.startsWith('//', index) ? endOfLine(sql, index) : 
super.endOfComment(sql, index)
+    }
+
+    @Override
+    int endOfValue(String sql, int index) {
+        int end = endOfDollarQuoted(sql, index, false)
+        end != NONE ? end : super.endOfValue(sql, index)
+    }
+
+    @Override
+    int endOfQuotedName(String sql, int index) {
+        char c = sql.charAt(index)
+        if (c == '`') {
+            return endOfQuoted(sql, index, '`' as char, false, true)
+        }
+        if (c == '[' && !subscript(sql, index) && 
(bracketAfterWhitespaceIsName || !afterWhitespace(sql, index))) {
+            return endOfQuoted(sql, index, ']' as char, false, false)
+        }
+        super.endOfQuotedName(sql, index)
+    }
+
+    @Override
+    protected boolean nestedComments() {
+        true
+    }
+
+    private boolean subscript(String sql, int index) {
+        char previous = previous(sql, index)
+        isNamePart(previous) || previous == ')' || previous == ']'
+    }
+
+    private static boolean afterWhitespace(String sql, int index) {
+        index > 0 && Character.isWhitespace(sql.charAt(index - 1))
+    }
+
+    private static boolean hasBracketAfterWhitespace(String sql) {

Review Comment:
   Fixed in db833afb85 (folded into the feature commit). `H2SqlSyntax` now 
treats the two characters that close a block comment like whitespace before a 
`[`: `afterGap` replaces `afterWhitespace` in both the readings decision and 
`endOfQuotedName`, so `ARRAY/**/[...]` and `ARRAY /* c */[...]` get the name 
and the array-literal reading and are withheld; a line comment ends with a line 
break, which was already whitespace. Covered in `QueryTailRedactorSpec` 
(withheld table and the never-survives list, plus `ARRAY -- c\n[...]`) and 
through the capture API in `QueryTailCaptureSpec`, where the three `ARRAY` 
forms are withheld and `[order] from [user]` is still recorded; the guide 
paragraph names the comment case.



##########
grails-datamapping-core/src/main/groovy/org/apache/grails/data/core/querytail/H2SqlSyntax.groovy:
##########
@@ -0,0 +1,102 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.grails.data.core.querytail
+
+import groovy.transform.CompileStatic
+import groovy.transform.PackageScope
+
+/**
+ * H2: block comments nest, {@code //} starts a line comment, {@code $$} 
quotes a string without a
+ * tag, backticks delimit a name in every compatibility mode, and square 
brackets delimit a name in
+ * the SQL Server mode, where they are never a subscript, which is what a 
bracket after a name, a
+ * closing parenthesis or a closing bracket is in the other modes. A bracket 
after whitespace, or
+ * after a comment, is a name in the SQL Server mode but opens an array 
literal after the
+ * {@code ARRAY} keyword, which may stand any distance before it, so a 
statement with one is read
+ * both ways: a name, which is recorded as written, agrees with itself, while 
an array literal,
+ * whose elements are values, does not and withholds the statement.
+ */
+@CompileStatic
+@PackageScope
+final class H2SqlSyntax extends StandardSqlSyntax {
+
+    private final boolean bracketAfterWhitespaceIsName
+
+    H2SqlSyntax() {
+        this(true)
+    }
+
+    private H2SqlSyntax(boolean bracketAfterWhitespaceIsName) {
+        super('H2')
+        this.bracketAfterWhitespaceIsName = bracketAfterWhitespaceIsName
+    }
+
+    @Override
+    List<QueryTailSyntax> readings(String sql) {
+        if (!hasBracketAfterWhitespace(sql)) {
+            return List.<QueryTailSyntax>of(this)
+        }
+        List.<QueryTailSyntax>of(new H2SqlSyntax(true), new H2SqlSyntax(false))
+    }
+
+    @Override
+    int endOfComment(String sql, int index) {
+        sql.startsWith('//', index) ? endOfLine(sql, index) : 
super.endOfComment(sql, index)
+    }
+
+    @Override
+    int endOfValue(String sql, int index) {
+        int end = endOfDollarQuoted(sql, index, false)
+        end != NONE ? end : super.endOfValue(sql, index)
+    }
+
+    @Override
+    int endOfQuotedName(String sql, int index) {
+        char c = sql.charAt(index)
+        if (c == '`') {
+            return endOfQuoted(sql, index, '`' as char, false, true)
+        }
+        if (c == '[' && !subscript(sql, index) && 
(bracketAfterWhitespaceIsName || !afterWhitespace(sql, index))) {
+            return endOfQuoted(sql, index, ']' as char, false, false)
+        }
+        super.endOfQuotedName(sql, index)
+    }
+
+    @Override
+    protected boolean nestedComments() {
+        true
+    }
+
+    private boolean subscript(String sql, int index) {
+        char previous = previous(sql, index)
+        isNamePart(previous) || previous == ')' || previous == ']'
+    }
+
+    private static boolean afterWhitespace(String sql, int index) {
+        index > 0 && Character.isWhitespace(sql.charAt(index - 1))
+    }
+
+    private static boolean hasBracketAfterWhitespace(String sql) {

Review Comment:
   Fixed in db833afb85 (folded into the feature commit). `H2SqlSyntax` now 
treats the two characters that close a block comment like whitespace before a 
`[`: `afterGap` replaces `afterWhitespace` in both the readings decision and 
`endOfQuotedName`, so `ARRAY/**/[...]` and `ARRAY /* c */[...]` get the name 
and the array-literal reading and are withheld; a line comment ends with a line 
break, which was already whitespace. Covered in `QueryTailRedactorSpec` 
(withheld table and the never-survives list, plus `ARRAY -- c\n[...]`) and 
through the capture API in `QueryTailCaptureSpec`, where the three `ARRAY` 
forms are withheld and `[order] from [user]` is still recorded; the guide 
paragraph names the comment case.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to