Unverified Commit a55de799 authored by gaoyan's avatar gaoyan Committed by GitHub

fix sql annotations replace bug (#1882)

* fix sql annotations replace bug * fix sql annotations replace bug * add testRemoveNote unit test
parent cb1cae2b
/*
*
* 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.dinky.utils;
import org.assertj.core.api.Assertions;
import org.junit.Ignore;
import org.junit.Test;
/** DirUtilTest */
@Ignore
public class SqlUtilTest {
@Test
public void testRemoveNote() {
String testSql =
"/**\n"
+ "test1\n"
+ "*/\n"
+ "//test2\n"
+ "-- test3\n"
+ "--test4\n"
+ "select 1 --test5\n"
+ " from test # test9\n"
+ " where '1' <> '-- ::.' //test6\n"
+ " and 1=1 --test7\n"
+ " and 'zz' <> null; /**test8*/";
String removedNoteSql = SqlUtil.removeNote(testSql);
Assertions.assertThat(removedNoteSql).isNotNull();
Assertions.assertThat(removedNoteSql).isNotEqualTo(testSql);
}
}
...@@ -23,6 +23,7 @@ import org.dinky.assertion.Asserts; ...@@ -23,6 +23,7 @@ import org.dinky.assertion.Asserts;
import org.dinky.model.SystemConfiguration; import org.dinky.model.SystemConfiguration;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern;
/** /**
* SqlUtil * SqlUtil
...@@ -52,12 +53,14 @@ public class SqlUtil { ...@@ -52,12 +53,14 @@ public class SqlUtil {
} }
public static String removeNote(String sql) { public static String removeNote(String sql) {
if (Asserts.isNotNullString(sql)) { if (Asserts.isNotNullString(sql)) {
sql = // Remove the special-space characters
sql.replaceAll("\u00A0", " ") sql = sql.replaceAll("\u00A0", " ").replaceAll("[\r\n]+", "\n");
.replaceAll("[\r\n]+", "\n") // Remove annotations Support '--aa' , '/**aaa*/' , '//aa' , '#aaa'
.replaceAll("--([^'\n]{0,}('[^'\n]{0,}'){0,1}[^'\n]{0,}){0,}", "") Pattern p = Pattern.compile("(?ms)('(?:''|[^'])*')|--.*?$|//.*?$|/\\*.*?\\*/|#.*?$|");
.trim(); String presult = p.matcher(sql).replaceAll("$1");
return presult.trim();
} }
return sql; return sql;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment