From 1c6e3eda5cc6fc5ae3c0610ef273f1621760883f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Tue, 28 Mar 2023 15:03:24 +0800 Subject: [PATCH] add SqliteDialect for codegen module --- mybatis-flex-codegen/pom.xml | 10 ++ .../com/mybatisflex/codegen/Generator.java | 61 ++---------- .../mybatisflex/codegen/dialect/IDialect.java | 16 +-- .../codegen/dialect/JdbcDialect.java | 74 ++++++++++++++ .../codegen/dialect/SqliteDialect.java | 97 +++++++++++++++++++ .../mybatisflex/codegen/entity/Column.java | 4 - .../com/mybatisflex/codegen/entity/Table.java | 18 ++++ .../codegen/test/SqliteGeneratorTest.java | 64 ++++++++++++ 8 files changed, 278 insertions(+), 66 deletions(-) create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcDialect.java create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/SqliteDialect.java create mode 100644 mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/SqliteGeneratorTest.java diff --git a/mybatis-flex-codegen/pom.xml b/mybatis-flex-codegen/pom.xml index 91e5df15..c6ebeb92 100644 --- a/mybatis-flex-codegen/pom.xml +++ b/mybatis-flex-codegen/pom.xml @@ -39,6 +39,7 @@ + mysql mysql-connector-java @@ -48,6 +49,15 @@ + + org.xerial + sqlite-jdbc + 3.41.2.1 + compile + true + + + com.zaxxer HikariCP diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/Generator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/Generator.java index c0223190..e9530649 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/Generator.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/Generator.java @@ -17,17 +17,17 @@ package com.mybatisflex.codegen; import com.mybatisflex.codegen.config.GlobalConfig; import com.mybatisflex.codegen.dialect.IDialect; -import com.mybatisflex.codegen.entity.Column; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.template.ITemplate; import javax.sql.DataSource; import java.io.File; -import java.sql.*; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; public class Generator { @@ -101,56 +101,6 @@ public class Generator { } - private void buildTableColumns(Table table) throws SQLException { - - Map columnRemarks = buildColumnRemarks(table); - - String sql = dialect.forBuildColumns(table.getName()); - try (Statement stm = conn.createStatement(); ResultSet rs = stm.executeQuery(sql)) { - - ResultSetMetaData columnMetaData = rs.getMetaData(); - int columnCount = columnMetaData.getColumnCount(); - - for (int i = 1; i <= columnCount; i++) { - Column column = new Column(); - column.setName(columnMetaData.getColumnName(i)); - column.setPropertyType(columnMetaData.getColumnClassName(i)); - column.setAutoIncrement(columnMetaData.isAutoIncrement(i)); - - //主键 - if (table.getPrimaryKeys() != null && table.getPrimaryKeys().contains(column.getName())) { - column.setPrimaryKey(true); - } - - //注释 - column.setRemarks(columnRemarks.get(column.getName())); - - column.setColumnConfig(globalConfig.getColumnConfig(table.getName(), column.getName())); - table.addColumn(column); - } - } - } - - - private Map buildColumnRemarks(Table table) throws SQLException { - Map columnRemarks = new HashMap<>(); - ResultSet colRs = null; - try { - colRs = dbMeta.getColumns(conn.getCatalog(), null, table.getName(), null); - while (colRs.next()) { - columnRemarks.put(colRs.getString("COLUMN_NAME"), colRs.getString("REMARKS")); - } - } catch (Exception e) { - System.err.println("无法获取字段的备注内容:" + e.getMessage()); - } finally { - if (colRs != null) { - colRs.close(); - } - } - return columnRemarks; - } - - private List buildTables() throws SQLException { List
tables = new ArrayList<>(); try (ResultSet rs = getTablesResultSet()) { @@ -171,7 +121,8 @@ public class Generator { buildPrimaryKey(table); - buildTableColumns(table); + + dialect.buildTableColumns(table, globalConfig, dbMeta, conn); tables.add(table); } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/IDialect.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/IDialect.java index b43d740b..2a4ed77f 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/IDialect.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/IDialect.java @@ -15,6 +15,9 @@ */ package com.mybatisflex.codegen.dialect; +import com.mybatisflex.codegen.config.GlobalConfig; +import com.mybatisflex.codegen.entity.Table; + import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.ResultSet; @@ -22,9 +25,9 @@ import java.sql.SQLException; public interface IDialect { - IDialect MYSQL = new IDialect() { + IDialect MYSQL = new JdbcDialect() { @Override - public String forBuildColumns(String tableName) { + String forBuildColumnsSql(String tableName) { return "SELECT * FROM `" + tableName + "` WHERE 1 = 2"; } @@ -35,10 +38,9 @@ public interface IDialect { }; - - IDialect ORACLE = new IDialect() { + IDialect ORACLE = new JdbcDialect() { @Override - public String forBuildColumns(String tableName) { + public String forBuildColumnsSql(String tableName) { return "SELECT * FROM \"" +tableName+ "\" WHERE rownum < 1"; } @@ -48,10 +50,10 @@ public interface IDialect { } }; + IDialect SQLITE = new SqliteDialect(); - - String forBuildColumns(String tableName); + void buildTableColumns( Table table, GlobalConfig globalConfig,DatabaseMetaData dbMeta, Connection conn) throws SQLException; ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String[] types) throws SQLException; } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcDialect.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcDialect.java new file mode 100644 index 00000000..e72df39c --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcDialect.java @@ -0,0 +1,74 @@ +/** + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 + *

+ * 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 com.mybatisflex.codegen.dialect; + +import com.mybatisflex.codegen.config.GlobalConfig; +import com.mybatisflex.codegen.entity.Column; +import com.mybatisflex.codegen.entity.Table; + +import java.sql.*; +import java.util.HashMap; +import java.util.Map; + +public abstract class JdbcDialect implements IDialect { + + @Override + public void buildTableColumns(Table table,GlobalConfig globalConfig, DatabaseMetaData dbMeta, Connection conn) throws SQLException { + Map columnRemarks = buildColumnRemarks(table,dbMeta,conn); + + String sql = forBuildColumnsSql(table.getName()); + try (Statement stm = conn.createStatement(); ResultSet rs = stm.executeQuery(sql)) { + + ResultSetMetaData columnMetaData = rs.getMetaData(); + int columnCount = columnMetaData.getColumnCount(); + + for (int i = 1; i <= columnCount; i++) { + Column column = new Column(); + column.setName(columnMetaData.getColumnName(i)); + column.setPropertyType(columnMetaData.getColumnClassName(i)); + column.setAutoIncrement(columnMetaData.isAutoIncrement(i)); + + //注释 + column.setRemarks(columnRemarks.get(column.getName())); + + table.addColumn(column); + } + } + } + + + private Map buildColumnRemarks(Table table,DatabaseMetaData dbMeta, Connection conn) throws SQLException { + Map columnRemarks = new HashMap<>(); + ResultSet colRs = null; + try { + colRs = dbMeta.getColumns(conn.getCatalog(), null, table.getName(), null); + while (colRs.next()) { + columnRemarks.put(colRs.getString("COLUMN_NAME"), colRs.getString("REMARKS")); + } + } catch (Exception e) { + System.err.println("无法获取字段的备注内容:" + e.getMessage()); + } finally { + if (colRs != null) { + colRs.close(); + } + } + return columnRemarks; + } + + + + abstract String forBuildColumnsSql(String tableName); +} diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/SqliteDialect.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/SqliteDialect.java new file mode 100644 index 00000000..20ddc91c --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/SqliteDialect.java @@ -0,0 +1,97 @@ +/** + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 + *

+ * 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 com.mybatisflex.codegen.dialect; + +import com.mybatisflex.codegen.config.GlobalConfig; +import com.mybatisflex.codegen.entity.Column; +import com.mybatisflex.codegen.entity.Table; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.sql.*; + +public class SqliteDialect implements IDialect { + + @Override + public void buildTableColumns(Table table, GlobalConfig globalConfig, DatabaseMetaData dbMeta, Connection conn) throws SQLException { + Statement statement = conn.createStatement(); + ResultSet rs = statement.executeQuery("pragma table_info(" + table.getName() + ")"); + + //cid name type notnull dflt_value pk + //--- --------- ------- ------- ---------- -- + //0 EventId INTEGER 0 1 + //1 EventName 0 0 + //2 StartDate 0 0 + //3 EndDate 0 0 + + while (rs.next()) { + Column column = new Column(); + column.setName(rs.getString(2)); + + String type = rs.getString(3); + column.setPropertyType(type2ClassName(type)); + + table.addColumn(column); + } + } + + + @Override + public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String[] types) throws SQLException { + return dbMeta.getTables(conn.getCatalog(), null, null, types); + } + + + private String type2ClassName(String type) { + int indexOf = type.indexOf("("); + if (indexOf > 0) { + type = type.substring(0, indexOf); + } + type = type.toLowerCase(); + switch (type) { + case "integer": + case "int": + case "int2": + case "int8": + case "tinyint": + case "smallint": + case "mediumint": + return Integer.class.getName(); + case "bigint": + case "unsigned bigint": + return BigInteger.class.getName(); + case "character": + case "varchar": + case "nchar": + case "nvarchar": + case "varying character": + case "native character": + case "text": + case "clob": + return String.class.getName(); + case "double": + case "numeric": + case "real": + return Double.class.getName(); + case "float": + return Float.class.getName(); + case "decimal": + return BigDecimal.class.getName(); + default: + return String.class.getName(); + } + } +} diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java index 096a1450..28b29876 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java @@ -51,10 +51,6 @@ public class Column { return property; } - public void setProperty(String property) { - this.property = property; - } - public String getPropertyType() { return propertyType; } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java index 9962763a..9d744aa0 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java @@ -19,6 +19,7 @@ import com.mybatisflex.codegen.config.GlobalConfig; import com.mybatisflex.codegen.config.TableConfig; import com.mybatisflex.core.util.StringUtil; +import java.math.BigInteger; import java.util.*; import java.util.stream.Collectors; @@ -73,6 +74,23 @@ public class Table { } public void addColumn(Column column) { + + //主键 + if (primaryKeys != null && primaryKeys.contains(column.getName())) { + column.setPrimaryKey(true); + if (column.getAutoIncrement() == null){ + if (column.getPropertyType().equals(Integer.class.getName()) || column.getPropertyType().equals(BigInteger.class.getName())){ + column.setAutoIncrement(true); + } + } + } + + if (column.getAutoIncrement() == null){ + column.setAutoIncrement(false); + } + + column.setColumnConfig(globalConfig.getColumnConfig(name, column.getName())); + columns.add(column); } diff --git a/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/SqliteGeneratorTest.java b/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/SqliteGeneratorTest.java new file mode 100644 index 00000000..f000df90 --- /dev/null +++ b/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/SqliteGeneratorTest.java @@ -0,0 +1,64 @@ +package com.mybatisflex.codegen.test; + +import com.mybatisflex.codegen.Generator; +import com.mybatisflex.codegen.config.GlobalConfig; +import com.mybatisflex.codegen.dialect.IDialect; +import com.zaxxer.hikari.HikariDataSource; +import org.junit.Test; + +import java.sql.Connection; +import java.sql.Statement; + +public class SqliteGeneratorTest { + + @Test + public void testGenerator3() { + + //配置数据源 + HikariDataSource dataSource = new HikariDataSource(); + dataSource.setJdbcUrl("jdbc:sqlite:sample.db"); + //dataSource.setUsername("root"); + //dataSource.setPassword("123456"); + + createTestTable(dataSource); + + + GlobalConfig globalConfig = new GlobalConfig(); + globalConfig.setSourceDir(System.getProperty("user.dir") + "/src/test/java"); + + //设置只生成哪些表 + globalConfig.addGenerateTable("person"); + + //设置 entity 的包名 + globalConfig.setEntityPackage("com.test.entity"); + + //是否生成 mapper 类,默认为 false + globalConfig.setMapperGenerateEnable(true); + globalConfig.setEntityWithLombok(true); + + //设置 mapper 类的包名 + globalConfig.setMapperPackage("com.test.mapper"); + + + Generator generator = new Generator(dataSource, globalConfig, IDialect.SQLITE); + + //开始生成代码 + generator.generate(); + } + + private void createTestTable(HikariDataSource dataSource) { + + try { + Connection connection = dataSource.getConnection(); + Statement statement = connection.createStatement(); + statement.setQueryTimeout(30); // set timeout to 30 sec. + + statement.executeUpdate("drop table if exists person"); + statement.executeUpdate("create table person (id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING, mobile varchar(20))"); + + connection.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } +}