Browse Source

postgresql关键字处理.

聂秋秋 5 years ago
parent
commit
d05beff2bf

+ 4 - 2
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/TableField.java

@@ -130,8 +130,10 @@ public class TableField {
      * @since 3.3.2
      */
     public String getAnnotationColumnName() {
-        if (columnName.startsWith("\"")) {
-            return String.format("\\%s\\", columnName);
+        if (keyWords) {
+            if (columnName.startsWith("\"")) {
+                return String.format("\\\"%s\\\"", name);
+            }
         }
         return columnName;
     }

+ 147 - 0
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/keywords/PostgreSqlKeyWordsHandler.java

@@ -0,0 +1,147 @@
+/*
+ * Copyright (c) 2011-2020, baomidou (jobob@qq.com).
+ * <p>
+ * Licensed 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
+ * <p>
+ * https://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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 com.baomidou.mybatisplus.generator.keywords;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * postgresql关键字处理
+ * https://www.postgresql.org/docs/11/sql-keywords-appendix.html
+ *
+ * @author nieqiurong 2020/5/9.
+ * @since 3.3.2
+ */
+public class PostgreSqlKeyWordsHandler extends BaseKeyWordsHandler {
+    
+    private static List<String> KEY_WORDS = new ArrayList<>(Arrays.asList(
+        "ALL",
+        "ANALYSE",
+        "ANALYZE",
+        "AND",
+        "ANY",
+        "ARRAY",
+        "AS",
+        "ASC",
+        "ASYMMETRIC",
+        "AUTHORIZATION",
+        "BINARY",
+        "BOTH",
+        "CASE",
+        "CAST",
+        "CHECK",
+        "COLLATE",
+        "COLLATION",
+        "COLUMN",
+        "CONCURRENTLY",
+        "CONSTRAINT",
+        "CREATE",
+        "CROSS",
+        "CURRENT_CATALOG",
+        "CURRENT_DATE",
+        "CURRENT_ROLE",
+        "CURRENT_SCHEMA",
+        "CURRENT_TIME",
+        "CURRENT_TIMESTAMP",
+        "CURRENT_USER",
+        "DEFAULT",
+        "DEFERRABLE",
+        "DESC",
+        "DISTINCT",
+        "DO",
+        "ELSE",
+        "END",
+        "EXCEPT",
+        "FALSE",
+        "FETCH",
+        "FOR",
+        "FOREIGN",
+        "FREEZE",
+        "FROM",
+        "FULL",
+        "GRANT",
+        "GROUP",
+        "HAVING",
+        "ILIKE",
+        "IN",
+        "INITIALLY",
+        "INNER",
+        "INTERSECT",
+        "INTO",
+        "IS",
+        "ISNULL",
+        "JOIN",
+        "LATERAL",
+        "LEADING",
+        "LEFT",
+        "LIKE",
+        "LIMIT",
+        "LOCALTIME",
+        "LOCALTIMESTAMP",
+        "NATURAL",
+        "NOT",
+        "NOTNULL",
+        "NULL",
+        "OFFSET",
+        "ON",
+        "ONLY",
+        "OR",
+        "ORDER",
+        "OUTER",
+        "OVERLAPS",
+        "PLACING",
+        "PRIMARY",
+        "REFERENCES",
+        "RETURNING",
+        "RIGHT",
+        "SELECT",
+        "SESSION_USER",
+        "SIMILAR",
+        "SOME",
+        "SYMMETRIC",
+        "TABLE",
+        "TABLESAMPLE",
+        "THEN",
+        "TO",
+        "TRAILING",
+        "TRUE",
+        "UNION",
+        "UNIQUE",
+        "USER",
+        "USING",
+        "VARIADIC",
+        "VERBOSE",
+        "WHEN",
+        "WHERE",
+        "WINDOW",
+        "WITH"
+    ));
+    
+    public PostgreSqlKeyWordsHandler() {
+        super(KEY_WORDS);
+    }
+    
+    public PostgreSqlKeyWordsHandler(List<String> keyWords) {
+        super(keyWords);
+    }
+    
+    @Override
+    public String formatStyle() {
+        return "\"%s\"";
+    }
+    
+}

+ 22 - 0
mybatis-plus-generator/src/test/java/com/baomidou/mybatisplus/test/generator/keywords/PostgreSqlKeyWordsHandlerTest.java

@@ -0,0 +1,22 @@
+package com.baomidou.mybatisplus.test.generator.keywords;
+
+import com.baomidou.mybatisplus.generator.keywords.PostgreSqlKeyWordsHandler;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * @author nieqiurong 2020/5/9.
+ */
+class PostgreSqlKeyWordsHandlerTest {
+    
+    @Test
+    void test() {
+        PostgreSqlKeyWordsHandler keyWordsHandler = new PostgreSqlKeyWordsHandler();
+        Assertions.assertTrue(keyWordsHandler.isKeyWords("with"));
+        Assertions.assertTrue(keyWordsHandler.isKeyWords("WITH"));
+        Assertions.assertFalse(keyWordsHandler.isKeyWords("system"));
+        Assertions.assertFalse(keyWordsHandler.isKeyWords("SYSTEM"));
+        Assertions.assertEquals(keyWordsHandler.formatColumn("with"), "\"with\"");
+    }
+    
+}