strongduanmu commented on code in PR #25574: URL: https://github.com/apache/shardingsphere/pull/25574#discussion_r1190829717
########## test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/external/loader/ExternalMySQLTestParameterLoader.java: ########## @@ -0,0 +1,112 @@ +/* + * 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 + * + * http://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.shardingsphere.test.it.sql.parser.external.loader; + +import org.apache.shardingsphere.test.it.sql.parser.external.ExternalSQLParserTestParameter; +import org.apache.shardingsphere.test.loader.AbstractTestParameterLoader; +import org.apache.shardingsphere.test.loader.strategy.TestParameterLoadStrategy; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; + +/** + * External MySQL SQL parser test parameter loader. + */ +public final class ExternalMySQLTestParameterLoader extends AbstractTestParameterLoader<ExternalSQLParserTestParameter> { + + public ExternalMySQLTestParameterLoader(final TestParameterLoadStrategy loadStrategy) { + super(loadStrategy); + } + + /** + * Create external SQL parser test parameters. + * + * @param sqlCaseFileName SQL case file name + * @param sqlCaseFileContent SQL case file content + * @param resultFileContent result file content + * @param databaseType database type + * @param reportType report type + * @return external SQL parser test parameters + */ + public Collection<ExternalSQLParserTestParameter> createTestParameters(final String sqlCaseFileName, + final List<String> sqlCaseFileContent, + final List<String> resultFileContent, final String databaseType, final String reportType) { + Collection<ExternalSQLParserTestParameter> result = new LinkedList<>(); + List<String> sqlLines = new ArrayList<>(); + int sqlCaseEnum = 1; + String delimiter = ";"; + for (String line : sqlCaseFileContent) { + String trimLine = line.trim(); + if (trimLine.isEmpty() || sqlLines.size() == 0 && isComment(trimLine)) { + continue; + } + if (sqlLines.size() == 0 && trimLine.toUpperCase().startsWith("DELIMITER")) { Review Comment: Please put constant to left condition. ########## test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/external/loader/ExternalMySQLTestParameterLoader.java: ########## @@ -0,0 +1,112 @@ +/* + * 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 + * + * http://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.shardingsphere.test.it.sql.parser.external.loader; + +import org.apache.shardingsphere.test.it.sql.parser.external.ExternalSQLParserTestParameter; +import org.apache.shardingsphere.test.loader.AbstractTestParameterLoader; +import org.apache.shardingsphere.test.loader.strategy.TestParameterLoadStrategy; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; + +/** + * External MySQL SQL parser test parameter loader. + */ +public final class ExternalMySQLTestParameterLoader extends AbstractTestParameterLoader<ExternalSQLParserTestParameter> { + + public ExternalMySQLTestParameterLoader(final TestParameterLoadStrategy loadStrategy) { + super(loadStrategy); + } + + /** + * Create external SQL parser test parameters. + * + * @param sqlCaseFileName SQL case file name + * @param sqlCaseFileContent SQL case file content + * @param resultFileContent result file content + * @param databaseType database type + * @param reportType report type + * @return external SQL parser test parameters + */ + public Collection<ExternalSQLParserTestParameter> createTestParameters(final String sqlCaseFileName, + final List<String> sqlCaseFileContent, + final List<String> resultFileContent, final String databaseType, final String reportType) { + Collection<ExternalSQLParserTestParameter> result = new LinkedList<>(); + List<String> sqlLines = new ArrayList<>(); + int sqlCaseEnum = 1; + String delimiter = ";"; + for (String line : sqlCaseFileContent) { + String trimLine = line.trim(); + if (trimLine.isEmpty() || sqlLines.size() == 0 && isComment(trimLine)) { + continue; + } + if (sqlLines.size() == 0 && trimLine.toUpperCase().startsWith("DELIMITER")) { + delimiter = getNewDelimiter(trimLine, delimiter); + continue; + } + sqlLines.add(trimLine); + if (trimLine.endsWith(delimiter)) { + if (existInResultContent(resultFileContent, sqlLines)) { + String sqlCaseId = sqlCaseFileName + sqlCaseEnum++; + String sql = String.join("\n", sqlLines); + sql = sql.substring(0, sql.length() - delimiter.length()); + result.add(new ExternalSQLParserTestParameter(sqlCaseId, databaseType, sql, reportType)); + } + sqlLines.clear(); + } + } + return result; + } + + private String getNewDelimiter(final String delimiterSQL, final String delimiter) { + String trimSQL = delimiterSQL.trim(); + String newDelimiter = trimSQL + .substring(9, trimSQL.endsWith(delimiter) ? trimSQL.length() - delimiter.length() : trimSQL.length()) Review Comment: Please do not use magic number. ########## test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/external/loader/ExternalMySQLTestParameterLoader.java: ########## @@ -0,0 +1,112 @@ +/* + * 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 + * + * http://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.shardingsphere.test.it.sql.parser.external.loader; + +import org.apache.shardingsphere.test.it.sql.parser.external.ExternalSQLParserTestParameter; +import org.apache.shardingsphere.test.loader.AbstractTestParameterLoader; +import org.apache.shardingsphere.test.loader.strategy.TestParameterLoadStrategy; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; + +/** + * External MySQL SQL parser test parameter loader. + */ +public final class ExternalMySQLTestParameterLoader extends AbstractTestParameterLoader<ExternalSQLParserTestParameter> { + + public ExternalMySQLTestParameterLoader(final TestParameterLoadStrategy loadStrategy) { + super(loadStrategy); + } + + /** + * Create external SQL parser test parameters. + * + * @param sqlCaseFileName SQL case file name + * @param sqlCaseFileContent SQL case file content + * @param resultFileContent result file content + * @param databaseType database type + * @param reportType report type + * @return external SQL parser test parameters + */ + public Collection<ExternalSQLParserTestParameter> createTestParameters(final String sqlCaseFileName, + final List<String> sqlCaseFileContent, + final List<String> resultFileContent, final String databaseType, final String reportType) { + Collection<ExternalSQLParserTestParameter> result = new LinkedList<>(); + List<String> sqlLines = new ArrayList<>(); + int sqlCaseEnum = 1; + String delimiter = ";"; + for (String line : sqlCaseFileContent) { + String trimLine = line.trim(); + if (trimLine.isEmpty() || sqlLines.size() == 0 && isComment(trimLine)) { Review Comment: Please put constant to left condition. ########## test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/external/loader/ExternalMySQLTestParameterLoader.java: ########## @@ -0,0 +1,112 @@ +/* + * 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 + * + * http://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.shardingsphere.test.it.sql.parser.external.loader; + +import org.apache.shardingsphere.test.it.sql.parser.external.ExternalSQLParserTestParameter; +import org.apache.shardingsphere.test.loader.AbstractTestParameterLoader; +import org.apache.shardingsphere.test.loader.strategy.TestParameterLoadStrategy; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; + +/** + * External MySQL SQL parser test parameter loader. + */ +public final class ExternalMySQLTestParameterLoader extends AbstractTestParameterLoader<ExternalSQLParserTestParameter> { + + public ExternalMySQLTestParameterLoader(final TestParameterLoadStrategy loadStrategy) { + super(loadStrategy); + } + + /** + * Create external SQL parser test parameters. + * + * @param sqlCaseFileName SQL case file name + * @param sqlCaseFileContent SQL case file content + * @param resultFileContent result file content + * @param databaseType database type + * @param reportType report type + * @return external SQL parser test parameters + */ + public Collection<ExternalSQLParserTestParameter> createTestParameters(final String sqlCaseFileName, + final List<String> sqlCaseFileContent, + final List<String> resultFileContent, final String databaseType, final String reportType) { + Collection<ExternalSQLParserTestParameter> result = new LinkedList<>(); + List<String> sqlLines = new ArrayList<>(); + int sqlCaseEnum = 1; Review Comment: How about sqlCaseIndex? ########## test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/external/loader/ExternalMySQLTestParameterLoader.java: ########## @@ -0,0 +1,112 @@ +/* + * 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 + * + * http://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.shardingsphere.test.it.sql.parser.external.loader; + +import org.apache.shardingsphere.test.it.sql.parser.external.ExternalSQLParserTestParameter; +import org.apache.shardingsphere.test.loader.AbstractTestParameterLoader; +import org.apache.shardingsphere.test.loader.strategy.TestParameterLoadStrategy; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; + +/** + * External MySQL SQL parser test parameter loader. + */ +public final class ExternalMySQLTestParameterLoader extends AbstractTestParameterLoader<ExternalSQLParserTestParameter> { + + public ExternalMySQLTestParameterLoader(final TestParameterLoadStrategy loadStrategy) { + super(loadStrategy); + } + + /** + * Create external SQL parser test parameters. + * + * @param sqlCaseFileName SQL case file name + * @param sqlCaseFileContent SQL case file content + * @param resultFileContent result file content + * @param databaseType database type + * @param reportType report type + * @return external SQL parser test parameters + */ + public Collection<ExternalSQLParserTestParameter> createTestParameters(final String sqlCaseFileName, + final List<String> sqlCaseFileContent, + final List<String> resultFileContent, final String databaseType, final String reportType) { + Collection<ExternalSQLParserTestParameter> result = new LinkedList<>(); + List<String> sqlLines = new ArrayList<>(); + int sqlCaseEnum = 1; + String delimiter = ";"; + for (String line : sqlCaseFileContent) { + String trimLine = line.trim(); + if (trimLine.isEmpty() || sqlLines.size() == 0 && isComment(trimLine)) { + continue; + } + if (sqlLines.size() == 0 && trimLine.toUpperCase().startsWith("DELIMITER")) { + delimiter = getNewDelimiter(trimLine, delimiter); + continue; + } + sqlLines.add(trimLine); + if (trimLine.endsWith(delimiter)) { + if (existInResultContent(resultFileContent, sqlLines)) { + String sqlCaseId = sqlCaseFileName + sqlCaseEnum++; + String sql = String.join("\n", sqlLines); + sql = sql.substring(0, sql.length() - delimiter.length()); + result.add(new ExternalSQLParserTestParameter(sqlCaseId, databaseType, sql, reportType)); + } + sqlLines.clear(); + } + } + return result; + } + + private String getNewDelimiter(final String delimiterSQL, final String delimiter) { + String trimSQL = delimiterSQL.trim(); + String newDelimiter = trimSQL + .substring(9, trimSQL.endsWith(delimiter) ? trimSQL.length() - delimiter.length() : trimSQL.length()) + .trim(); + if (newDelimiter.startsWith("\"") && newDelimiter.endsWith("\"") || newDelimiter.startsWith("'") && newDelimiter.endsWith("'")) { + newDelimiter = newDelimiter.substring(1, newDelimiter.length() - 1); + } + return newDelimiter.isEmpty() ? delimiter : newDelimiter; + } + + private boolean isComment(final String statement) { + return statement.startsWith("#") || statement.startsWith("/") || statement.startsWith("--") || statement.startsWith(":") || statement.startsWith("\\"); + } + + private boolean existInResultContent(final List<String> resultLines, final List<String> sqlLines) { + int nextLineIndex = searchSQLNextLineIndex(resultLines, sqlLines); + return nextLineIndex != -1 && (nextLineIndex == resultLines.size() || !resultLines.get(nextLineIndex).contains("ERROR")); Review Comment: Please put constant to left condition. -- 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]
