mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 09:08:24 +08:00
add SqliteDialect for codegen module
This commit is contained in:
parent
b18b629fec
commit
1c6e3eda5c
@ -39,6 +39,7 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- for test only-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>mysql</groupId>
|
<groupId>mysql</groupId>
|
||||||
<artifactId>mysql-connector-java</artifactId>
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
@ -48,6 +49,15 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.xerial</groupId>
|
||||||
|
<artifactId>sqlite-jdbc</artifactId>
|
||||||
|
<version>3.41.2.1</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.zaxxer</groupId>
|
<groupId>com.zaxxer</groupId>
|
||||||
<artifactId>HikariCP</artifactId>
|
<artifactId>HikariCP</artifactId>
|
||||||
|
|||||||
@ -17,17 +17,17 @@ package com.mybatisflex.codegen;
|
|||||||
|
|
||||||
import com.mybatisflex.codegen.config.GlobalConfig;
|
import com.mybatisflex.codegen.config.GlobalConfig;
|
||||||
import com.mybatisflex.codegen.dialect.IDialect;
|
import com.mybatisflex.codegen.dialect.IDialect;
|
||||||
import com.mybatisflex.codegen.entity.Column;
|
|
||||||
import com.mybatisflex.codegen.entity.Table;
|
import com.mybatisflex.codegen.entity.Table;
|
||||||
import com.mybatisflex.codegen.template.ITemplate;
|
import com.mybatisflex.codegen.template.ITemplate;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.io.File;
|
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.ArrayList;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class Generator {
|
public class Generator {
|
||||||
|
|
||||||
@ -101,56 +101,6 @@ public class Generator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void buildTableColumns(Table table) throws SQLException {
|
|
||||||
|
|
||||||
Map<String, String> 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<String, String> buildColumnRemarks(Table table) throws SQLException {
|
|
||||||
Map<String, String> 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<Table> buildTables() throws SQLException {
|
private List<Table> buildTables() throws SQLException {
|
||||||
List<Table> tables = new ArrayList<>();
|
List<Table> tables = new ArrayList<>();
|
||||||
try (ResultSet rs = getTablesResultSet()) {
|
try (ResultSet rs = getTablesResultSet()) {
|
||||||
@ -171,7 +121,8 @@ public class Generator {
|
|||||||
|
|
||||||
|
|
||||||
buildPrimaryKey(table);
|
buildPrimaryKey(table);
|
||||||
buildTableColumns(table);
|
|
||||||
|
dialect.buildTableColumns(table, globalConfig, dbMeta, conn);
|
||||||
|
|
||||||
tables.add(table);
|
tables.add(table);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,6 +15,9 @@
|
|||||||
*/
|
*/
|
||||||
package com.mybatisflex.codegen.dialect;
|
package com.mybatisflex.codegen.dialect;
|
||||||
|
|
||||||
|
import com.mybatisflex.codegen.config.GlobalConfig;
|
||||||
|
import com.mybatisflex.codegen.entity.Table;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DatabaseMetaData;
|
import java.sql.DatabaseMetaData;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
@ -22,9 +25,9 @@ import java.sql.SQLException;
|
|||||||
|
|
||||||
public interface IDialect {
|
public interface IDialect {
|
||||||
|
|
||||||
IDialect MYSQL = new IDialect() {
|
IDialect MYSQL = new JdbcDialect() {
|
||||||
@Override
|
@Override
|
||||||
public String forBuildColumns(String tableName) {
|
String forBuildColumnsSql(String tableName) {
|
||||||
return "SELECT * FROM `" + tableName + "` WHERE 1 = 2";
|
return "SELECT * FROM `" + tableName + "` WHERE 1 = 2";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,10 +38,9 @@ public interface IDialect {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
IDialect ORACLE = new JdbcDialect() {
|
||||||
IDialect ORACLE = new IDialect() {
|
|
||||||
@Override
|
@Override
|
||||||
public String forBuildColumns(String tableName) {
|
public String forBuildColumnsSql(String tableName) {
|
||||||
return "SELECT * FROM \"" +tableName+ "\" WHERE rownum < 1";
|
return "SELECT * FROM \"" +tableName+ "\" WHERE rownum < 1";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,10 +50,10 @@ public interface IDialect {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
IDialect SQLITE = new SqliteDialect();
|
||||||
|
|
||||||
|
|
||||||
|
void buildTableColumns( Table table, GlobalConfig globalConfig,DatabaseMetaData dbMeta, Connection conn) throws SQLException;
|
||||||
String forBuildColumns(String tableName);
|
|
||||||
|
|
||||||
ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String[] types) throws SQLException;
|
ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String[] types) throws SQLException;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,74 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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>
|
||||||
|
* http://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.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<String, String> 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<String, String> buildColumnRemarks(Table table,DatabaseMetaData dbMeta, Connection conn) throws SQLException {
|
||||||
|
Map<String, String> 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);
|
||||||
|
}
|
||||||
@ -0,0 +1,97 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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>
|
||||||
|
* http://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.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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -51,10 +51,6 @@ public class Column {
|
|||||||
return property;
|
return property;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setProperty(String property) {
|
|
||||||
this.property = property;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPropertyType() {
|
public String getPropertyType() {
|
||||||
return propertyType;
|
return propertyType;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,6 +19,7 @@ import com.mybatisflex.codegen.config.GlobalConfig;
|
|||||||
import com.mybatisflex.codegen.config.TableConfig;
|
import com.mybatisflex.codegen.config.TableConfig;
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -73,6 +74,23 @@ public class Table {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addColumn(Column column) {
|
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);
|
columns.add(column);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user