mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
feat: optimize dialect for codegen
This commit is contained in:
parent
2754004055
commit
ea83639365
@ -25,10 +25,9 @@ import java.util.Map;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认方言抽象类。
|
* 默认方言抽象类。
|
||||||
*
|
|
||||||
* @author michael
|
* @author michael
|
||||||
*/
|
*/
|
||||||
public abstract class JdbcDialect implements IDialect {
|
public abstract class AbstractJdbcDialect implements IDialect {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void buildTableColumns(String schemaName, Table table, GlobalConfig globalConfig, DatabaseMetaData dbMeta, Connection conn) throws SQLException {
|
public void buildTableColumns(String schemaName, Table table, GlobalConfig globalConfig, DatabaseMetaData dbMeta, Connection conn) throws SQLException {
|
||||||
@ -79,15 +78,6 @@ public abstract class JdbcDialect implements IDialect {
|
|||||||
return dbMeta.getTables(conn.getCatalog(), schema, null, types);
|
return dbMeta.getTables(conn.getCatalog(), schema, null, types);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 构建查询所有数据的 SQL 语句。
|
|
||||||
*
|
|
||||||
* @param schema 模式
|
|
||||||
* @param tableName 表名
|
|
||||||
* @return 全量查询 SQL 语句
|
|
||||||
*/
|
|
||||||
abstract String forBuildColumnsSql(String schema, String tableName);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构建 remarks 的 ResultSet
|
* 构建 remarks 的 ResultSet
|
||||||
@ -104,4 +94,16 @@ public abstract class JdbcDialect implements IDialect {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建查询所有数据的 SQL 语句。
|
||||||
|
*
|
||||||
|
* @param schema 模式
|
||||||
|
* @param tableName 表名
|
||||||
|
* @return 全量查询 SQL 语句
|
||||||
|
*/
|
||||||
|
protected abstract String forBuildColumnsSql(String schema, String tableName);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -15,15 +15,13 @@
|
|||||||
*/
|
*/
|
||||||
package com.mybatisflex.codegen.dialect;
|
package com.mybatisflex.codegen.dialect;
|
||||||
|
|
||||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
|
||||||
import com.mybatisflex.codegen.config.GlobalConfig;
|
import com.mybatisflex.codegen.config.GlobalConfig;
|
||||||
|
import com.mybatisflex.codegen.dialect.impl.DefaultJdbcDialect;
|
||||||
|
import com.mybatisflex.codegen.dialect.impl.MySqlJdbcDialect;
|
||||||
|
import com.mybatisflex.codegen.dialect.impl.OracleJdbcDialect;
|
||||||
|
import com.mybatisflex.codegen.dialect.impl.SqliteDialect;
|
||||||
import com.mybatisflex.codegen.entity.Table;
|
import com.mybatisflex.codegen.entity.Table;
|
||||||
import com.mybatisflex.core.util.ClassUtil;
|
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
|
||||||
import com.zaxxer.hikari.pool.HikariProxyConnection;
|
|
||||||
import oracle.jdbc.driver.OracleConnection;
|
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DatabaseMetaData;
|
import java.sql.DatabaseMetaData;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
@ -31,66 +29,25 @@ import java.sql.SQLException;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 方言接口。
|
* 方言接口。
|
||||||
|
* @author michael
|
||||||
|
* @author Suomm
|
||||||
*/
|
*/
|
||||||
public interface IDialect {
|
public interface IDialect {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认方言。
|
* 默认方言。
|
||||||
*/
|
*/
|
||||||
IDialect DEFAULT = new JdbcDialect() {
|
IDialect DEFAULT = new DefaultJdbcDialect();
|
||||||
@Override
|
|
||||||
String forBuildColumnsSql(String schema, String tableName) {
|
|
||||||
return "SELECT * FROM " + (StringUtil.isNotBlank(schema) ? schema + "." : "") + tableName + " WHERE 1 = 2";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MySQL 方言。
|
* MySQL 方言。
|
||||||
*/
|
*/
|
||||||
IDialect MYSQL = new JdbcDialect() {
|
IDialect MYSQL = new MySqlJdbcDialect();
|
||||||
@Override
|
|
||||||
String forBuildColumnsSql(String schema, String tableName) {
|
|
||||||
return "SELECT * FROM `" + (StringUtil.isNotBlank(schema) ? schema + "`.`" : "") + tableName + "` WHERE 1 = 2";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Oracle 方言。
|
* Oracle 方言。
|
||||||
*/
|
*/
|
||||||
IDialect ORACLE = new JdbcDialect() {
|
IDialect ORACLE = new OracleJdbcDialect();
|
||||||
@Override
|
|
||||||
public String forBuildColumnsSql(String schema, String tableName) {
|
|
||||||
return "SELECT * FROM \"" + (StringUtil.isNotBlank(schema) ? schema + "\".\"" : "") + tableName + "\" WHERE rownum < 1";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String schema, String[] types) throws SQLException {
|
|
||||||
return dbMeta.getTables(conn.getCatalog(), StringUtil.isNotBlank(schema) ? schema : dbMeta.getUserName(), null, types);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected ResultSet forRemarks(String schema, Table table, DatabaseMetaData dbMeta, Connection conn) throws SQLException {
|
|
||||||
if (conn instanceof OracleConnection) {
|
|
||||||
((OracleConnection) conn).setRemarksReporting(true);
|
|
||||||
return dbMeta.getColumns(conn.getCatalog(), StringUtil.isNotBlank(schema) ? schema : dbMeta.getUserName(), table.getName(), null);
|
|
||||||
} else if ("com.zaxxer.hikari.pool.HikariProxyConnection".equals(conn.getClass().getName())) {
|
|
||||||
return forRemarks(schema, table, dbMeta, getOriginalConn(HikariProxyConnection.class, "delegate", conn));
|
|
||||||
} else if ("com.alibaba.druid.pool.DruidPooledConnection".equals(conn.getClass().getName())) {
|
|
||||||
return forRemarks(schema, table, dbMeta, getOriginalConn(DruidPooledConnection.class, "conn", conn));
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Connection getOriginalConn(Class<?> clazz, String attr, Connection conn) {
|
|
||||||
Field delegate = ClassUtil.getFirstField(clazz, field -> field.getName().equals(attr));
|
|
||||||
try {
|
|
||||||
delegate.setAccessible(true);
|
|
||||||
return (Connection) delegate.get(conn);
|
|
||||||
} catch (IllegalAccessException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sqlite 方言。
|
* Sqlite 方言。
|
||||||
|
|||||||
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* 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.impl;
|
||||||
|
|
||||||
|
import com.mybatisflex.codegen.dialect.AbstractJdbcDialect;
|
||||||
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author michael
|
||||||
|
*/
|
||||||
|
public class DefaultJdbcDialect extends AbstractJdbcDialect {
|
||||||
|
@Override
|
||||||
|
protected String forBuildColumnsSql(String schema, String tableName) {
|
||||||
|
return "SELECT * FROM " + (StringUtil.isNotBlank(schema) ? schema + "." : "") + tableName + " WHERE 1 = 2";
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* 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.impl;
|
||||||
|
|
||||||
|
import com.mybatisflex.codegen.dialect.AbstractJdbcDialect;
|
||||||
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author michael
|
||||||
|
*/
|
||||||
|
public class MySqlJdbcDialect extends AbstractJdbcDialect {
|
||||||
|
@Override
|
||||||
|
protected String forBuildColumnsSql(String schema, String tableName) {
|
||||||
|
return "SELECT * FROM `" + (StringUtil.isNotBlank(schema) ? schema + "`.`" : "") + tableName + "` WHERE 1 = 2";
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
* 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.impl;
|
||||||
|
|
||||||
|
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||||
|
import com.mybatisflex.codegen.dialect.AbstractJdbcDialect;
|
||||||
|
import com.mybatisflex.codegen.entity.Table;
|
||||||
|
import com.mybatisflex.core.util.ClassUtil;
|
||||||
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
|
import com.zaxxer.hikari.pool.HikariProxyConnection;
|
||||||
|
import oracle.jdbc.driver.OracleConnection;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DatabaseMetaData;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author michael
|
||||||
|
*/
|
||||||
|
public class OracleJdbcDialect extends AbstractJdbcDialect {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String forBuildColumnsSql(String schema, String tableName) {
|
||||||
|
return "SELECT * FROM \"" + (StringUtil.isNotBlank(schema) ? schema + "\".\"" : "") + tableName + "\" WHERE rownum < 1";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String schema, String[] types) throws SQLException {
|
||||||
|
return dbMeta.getTables(conn.getCatalog(), StringUtil.isNotBlank(schema) ? schema : dbMeta.getUserName(), null, types);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ResultSet forRemarks(String schema, Table table, DatabaseMetaData dbMeta, Connection conn) throws SQLException {
|
||||||
|
if (conn instanceof OracleConnection) {
|
||||||
|
((OracleConnection) conn).setRemarksReporting(true);
|
||||||
|
return dbMeta.getColumns(conn.getCatalog(), StringUtil.isNotBlank(schema) ? schema : dbMeta.getUserName(), table.getName(), null);
|
||||||
|
} else if ("com.zaxxer.hikari.pool.HikariProxyConnection".equals(conn.getClass().getName())) {
|
||||||
|
return forRemarks(schema, table, dbMeta, getOriginalConn(HikariProxyConnection.class, "delegate", conn));
|
||||||
|
} else if ("com.alibaba.druid.pool.DruidPooledConnection".equals(conn.getClass().getName())) {
|
||||||
|
return forRemarks(schema, table, dbMeta, getOriginalConn(DruidPooledConnection.class, "conn", conn));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Connection getOriginalConn(Class<?> clazz, String attr, Connection conn) {
|
||||||
|
Field delegate = ClassUtil.getFirstField(clazz, field -> field.getName().equals(attr));
|
||||||
|
try {
|
||||||
|
delegate.setAccessible(true);
|
||||||
|
return (Connection) delegate.get(conn);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -13,9 +13,10 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package com.mybatisflex.codegen.dialect;
|
package com.mybatisflex.codegen.dialect.impl;
|
||||||
|
|
||||||
import com.mybatisflex.codegen.config.GlobalConfig;
|
import com.mybatisflex.codegen.config.GlobalConfig;
|
||||||
|
import com.mybatisflex.codegen.dialect.IDialect;
|
||||||
import com.mybatisflex.codegen.entity.Column;
|
import com.mybatisflex.codegen.entity.Column;
|
||||||
import com.mybatisflex.codegen.entity.Table;
|
import com.mybatisflex.codegen.entity.Table;
|
||||||
|
|
||||||
@ -25,6 +26,7 @@ import java.sql.*;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Sqlite 方言实现。
|
* Sqlite 方言实现。
|
||||||
|
* @author michael
|
||||||
*/
|
*/
|
||||||
public class SqliteDialect implements IDialect {
|
public class SqliteDialect implements IDialect {
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user