From 083abc330d1b4b415603519a63a5985135ea1e22 Mon Sep 17 00:00:00 2001 From: hexinyu <1213652135@qq.com> Date: Tue, 15 Nov 2022 15:23:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0opengauss=E4=BF=9D=E7=95=99?= =?UTF-8?q?=E5=AD=97=E7=9A=84=E8=BF=81=E7=A7=BB=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=8F=8C=E5=BC=95=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mysql/MySqlToOpenGaussOutputVisitor.java | 13 ++++++++---- .../sqltranslator/SqlTranslateTest.java | 6 ++++++ .../expect/hasReservedWord_statement.sql | 20 +++++++++++++++++++ .../mysql/input/hasReservedWord_statement.sql | 8 ++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 src/test/resources/dialect/mysql/expect/hasReservedWord_statement.sql create mode 100644 src/test/resources/dialect/mysql/input/hasReservedWord_statement.sql diff --git a/src/main/java/org/opengauss/sqltranslator/dialect/mysql/MySqlToOpenGaussOutputVisitor.java b/src/main/java/org/opengauss/sqltranslator/dialect/mysql/MySqlToOpenGaussOutputVisitor.java index e95ff62..a5d0d54 100644 --- a/src/main/java/org/opengauss/sqltranslator/dialect/mysql/MySqlToOpenGaussOutputVisitor.java +++ b/src/main/java/org/opengauss/sqltranslator/dialect/mysql/MySqlToOpenGaussOutputVisitor.java @@ -39,7 +39,7 @@ public class MySqlToOpenGaussOutputVisitor extends MySqlOutputVisitor { private static final HashSet commonSchemaPrivilegeSet = new HashSet<>(); private static final HashSet tablePrivilegeSet = new HashSet<>(); private static final HashSet routinePrivilegeSet = new HashSet<>(); - + private static final HashSet reservedwordSet = new HashSet<>(); static { incompatiblePrivilegeSet.add("PROXY"); incompatiblePrivilegeSet.add("TRIGGER"); @@ -71,7 +71,7 @@ public class MySqlToOpenGaussOutputVisitor extends MySqlOutputVisitor { routinePrivilegeSet.add("ALTER"); routinePrivilegeSet.add("ALL"); routinePrivilegeSet.add("ALL PRIVILEGES"); - + reservedwordSet.add("number"); } private final StringBuilder sb = (StringBuilder) appender; @@ -1962,7 +1962,6 @@ public class MySqlToOpenGaussOutputVisitor extends MySqlOutputVisitor { if (nameHash == Constants.TIME || nameHash == Constants.TIMESTAMP) { this.print0(this.ucase ? " WITHOUT TIME ZONE" : " without time zone"); } - /* index by 貌似没得翻译 */ this.parameterized = parameterized; } @@ -2319,7 +2318,7 @@ public class MySqlToOpenGaussOutputVisitor extends MySqlOutputVisitor { this.appender.append(text.substring(1, text.length() - 1)); this.appender.append(this.quote); } else { - if (hasUpper(text)) { + if (hasUpper(text) || hasReservedword(text)) { this.appender.append(this.quote); this.appender.append(text); this.appender.append(this.quote); @@ -2332,6 +2331,12 @@ public class MySqlToOpenGaussOutputVisitor extends MySqlOutputVisitor { } } + public static boolean hasReservedword(String str) { + if (reservedwordSet.contains(str)) + return true; + else + return false; + } public static boolean hasUpper(String str) { for (int i = 0; i < str.length(); i++) { char c0 = str.charAt(i); diff --git a/src/test/java/org/opengauss/sqltranslator/SqlTranslateTest.java b/src/test/java/org/opengauss/sqltranslator/SqlTranslateTest.java index 5fe126f..c5d7928 100644 --- a/src/test/java/org/opengauss/sqltranslator/SqlTranslateTest.java +++ b/src/test/java/org/opengauss/sqltranslator/SqlTranslateTest.java @@ -235,4 +235,10 @@ public class SqlTranslateTest { String[] sqlContents = execFile("revoke_statement.sql"); assertEquals(sqlContents[1], sqlContents[2]); } + + @Test + public void test_hasReservedWord() throws IOException { + String[] sqlContents = execFile("hasReservedWord_statement.sql"); + assertEquals(sqlContents[1], sqlContents[2]); + } } diff --git a/src/test/resources/dialect/mysql/expect/hasReservedWord_statement.sql b/src/test/resources/dialect/mysql/expect/hasReservedWord_statement.sql new file mode 100644 index 0000000..27a0902 --- /dev/null +++ b/src/test/resources/dialect/mysql/expect/hasReservedWord_statement.sql @@ -0,0 +1,20 @@ +CREATE PROCEDURE test_chameleon_procedure ( + IN "number" INTEGER +) +AS +BEGIN + INSERT + INTO test_chameleon_table_1 + VALUES ("number", 'test'); +END; +/ +CREATE PROCEDURE test_chameleon_procedure ( + IN "NUMBER" INTEGER +) +AS +BEGIN + INSERT + INTO test_chameleon_table_1 + VALUES ("NUMBER", 'test'); +END; +/ \ No newline at end of file diff --git a/src/test/resources/dialect/mysql/input/hasReservedWord_statement.sql b/src/test/resources/dialect/mysql/input/hasReservedWord_statement.sql new file mode 100644 index 0000000..a6563bc --- /dev/null +++ b/src/test/resources/dialect/mysql/input/hasReservedWord_statement.sql @@ -0,0 +1,8 @@ +create procedure test_chameleon_procedure(in number int) +begin +insert into test_chameleon_table_1 values(number,"test"); +end; +create procedure test_chameleon_procedure(in NUMBER int) +begin +insert into test_chameleon_table_1 values(NUMBER,"test"); +end; \ No newline at end of file -- Gitee