jdaugherty commented on code in PR #16631: URL: https://github.com/apache/grails-core/pull/16631#discussion_r4365605734
########## grails-datamapping-core/src/main/groovy/org/apache/grails/data/core/querytail/H2SqlSyntax.groovy: ########## @@ -0,0 +1,65 @@ +/* + * 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.grails.datastore.gorm.jdbc.sqltail; + +/** + * 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. + */ +final class H2SqlSyntax extends StandardSqlSyntax { + + H2SqlSyntax() { + super("H2"); + } + + @Override + public int endOfComment(String sql, int index) { + return sql.startsWith("//", index) ? endOfLine(sql, index) : super.endOfComment(sql, index); + } + + @Override + public int endOfValue(String sql, int index) { + int end = endOfDollarQuoted(sql, index, false); + return end != NONE ? end : super.endOfValue(sql, index); + } + + @Override + public int endOfQuotedName(String sql, int index) { + char c = sql.charAt(index); + if (c == '`') { + return endOfQuoted(sql, index, '`', false, true); + } + if (c == '[' && !subscript(sql, index)) { + return endOfQuoted(sql, index, ']', false, false); + } + return super.endOfQuotedName(sql, index); + } + + @Override + protected boolean nestedComments() { + return true; + } + + private boolean subscript(String sql, int index) { Review Comment: Fixed in 67ba541f11. H2 now reads a bracket that follows whitespace both ways, as a name in the SQL Server mode and as the punctuation an array literal opens with after `ARRAY`, however far before it the keyword stands. A name agrees with itself and is recorded; an array literal, whose elements are values, does not and the statement is withheld. `QueryTailRedactorSpec` asserts `select ARRAY ['example-secret'] from t`, the same with a block comment and with a line comment between `ARRAY` and `[`, and `select [it's] from t` are all withheld, and the never-survives list for H2 holds the literal in those positions, including `ARRAY ['a', 'secret'][1]`. ########## grails-datamapping-core/src/main/groovy/org/apache/grails/data/core/querytail/H2SqlSyntax.groovy: ########## @@ -0,0 +1,70 @@ +/* + * 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. + */ +@CompileStatic +@PackageScope +final class H2SqlSyntax extends StandardSqlSyntax { + + H2SqlSyntax() { + super('H2') + } + + @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)) { Review Comment: Fixed in 67ba541f11. H2 now reads a bracket that follows whitespace both ways, as a name in the SQL Server mode and as the punctuation an array literal opens with after `ARRAY`, however far before it the keyword stands. A name agrees with itself and is recorded; an array literal, whose elements are values, does not and the statement is withheld. `QueryTailRedactorSpec` asserts `select ARRAY ['example-secret'] from t`, the same with a block comment and with a line comment between `ARRAY` and `[`, and `select [it's] from t` are all withheld, and the never-survives list for H2 holds the literal in those positions, including `ARRAY ['a', 'secret'][1]`. -- 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]
