mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
refactor: optimize oracle dialect
This commit is contained in:
parent
dbc2098185
commit
3058dc1f83
@ -32,12 +32,6 @@
|
|||||||
<version>${mybatis-flex.version}</version>
|
<version>${mybatis-flex.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.oracle.ojdbc</groupId>
|
|
||||||
<artifactId>ojdbc8</artifactId>
|
|
||||||
<version>19.3.0.0</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
|
|
||||||
<!--使用 enjoy 模板引擎-->
|
<!--使用 enjoy 模板引擎-->
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -66,6 +60,14 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.oracle.ojdbc</groupId>
|
||||||
|
<artifactId>ojdbc8</artifactId>
|
||||||
|
<version>19.3.0.0</version>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.zaxxer</groupId>
|
<groupId>com.zaxxer</groupId>
|
||||||
<artifactId>HikariCP</artifactId>
|
<artifactId>HikariCP</artifactId>
|
||||||
@ -75,6 +77,15 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>druid</artifactId>
|
||||||
|
<version>1.2.18</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
@ -97,6 +108,8 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@ -15,19 +15,19 @@
|
|||||||
*/
|
*/
|
||||||
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.entity.Table;
|
import com.mybatisflex.codegen.entity.Table;
|
||||||
import com.mybatisflex.core.util.ClassUtil;
|
import com.mybatisflex.core.util.ClassUtil;
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
|
|
||||||
import com.zaxxer.hikari.pool.HikariProxyConnection;
|
import com.zaxxer.hikari.pool.HikariProxyConnection;
|
||||||
|
import oracle.jdbc.driver.OracleConnection;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
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;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.function.Predicate;
|
|
||||||
import oracle.jdbc.driver.OracleConnection;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 方言接口。
|
* 方言接口。
|
||||||
@ -70,22 +70,25 @@ public interface IDialect {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ResultSet forRemarks(String schema, Table table, DatabaseMetaData dbMeta, Connection conn) throws SQLException {
|
protected ResultSet forRemarks(String schema, Table table, DatabaseMetaData dbMeta, Connection conn) throws SQLException {
|
||||||
HikariProxyConnection hikariProxyConnection = (HikariProxyConnection) conn;
|
if (conn instanceof OracleConnection){
|
||||||
Field delegate = ClassUtil.getFirstField(HikariProxyConnection.class, new Predicate<Field>() {
|
((OracleConnection) conn).setRemarksReporting(true);
|
||||||
@Override
|
return dbMeta.getColumns(conn.getCatalog(), StringUtil.isNotBlank(schema) ? schema : dbMeta.getUserName(), table.getName(), null);
|
||||||
public boolean test(Field field) {
|
}else if ("com.zaxxer.hikari.pool.HikariProxyConnection".equals(conn.getClass().getName())){
|
||||||
return field.getName().equals("delegate");
|
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;
|
||||||
delegate.setAccessible(true);
|
}
|
||||||
OracleConnection oc;
|
|
||||||
|
private Connection getOriginalConn(Class<?> clazz,String attr,Connection conn){
|
||||||
|
Field delegate = ClassUtil.getFirstField(clazz, field -> field.getName().equals(attr));
|
||||||
try {
|
try {
|
||||||
oc = (OracleConnection) delegate.get(hikariProxyConnection);
|
delegate.setAccessible(true);
|
||||||
|
return (Connection) delegate.get(conn);
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
oc.setRemarksReporting(true);
|
|
||||||
return dbMeta.getColumns(oc.getCatalog(), StringUtil.isNotBlank(schema) ? schema : dbMeta.getUserName(), table.getName(), null);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -23,7 +23,6 @@ import com.mybatisflex.codegen.config.TableConfig;
|
|||||||
import com.mybatisflex.codegen.config.TableDefConfig;
|
import com.mybatisflex.codegen.config.TableDefConfig;
|
||||||
import com.mybatisflex.spring.service.impl.CacheableServiceImpl;
|
import com.mybatisflex.spring.service.impl.CacheableServiceImpl;
|
||||||
import com.zaxxer.hikari.HikariDataSource;
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.util.function.UnaryOperator;
|
import java.util.function.UnaryOperator;
|
||||||
|
|
||||||
@ -149,7 +148,7 @@ public class GeneratorTest {
|
|||||||
generator.generate();
|
generator.generate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
// @Test
|
||||||
public void testCodeGen3() {
|
public void testCodeGen3() {
|
||||||
//配置数据源
|
//配置数据源
|
||||||
HikariDataSource dataSource = new HikariDataSource();
|
HikariDataSource dataSource = new HikariDataSource();
|
||||||
|
|||||||
@ -16,22 +16,30 @@
|
|||||||
|
|
||||||
package com.mybatisflex.codegen.test;
|
package com.mybatisflex.codegen.test;
|
||||||
|
|
||||||
|
import com.alibaba.druid.pool.DruidDataSource;
|
||||||
|
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||||
import com.mybatisflex.codegen.Generator;
|
import com.mybatisflex.codegen.Generator;
|
||||||
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.zaxxer.hikari.HikariDataSource;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
|
|
||||||
public class SqliteGeneratorTest {
|
public class SqliteGeneratorTest {
|
||||||
|
|
||||||
// @Test
|
// @Test
|
||||||
public void testGenerator3() {
|
public void testGenerator3() {
|
||||||
|
|
||||||
//配置数据源
|
//配置数据源
|
||||||
HikariDataSource dataSource = new HikariDataSource();
|
// HikariDataSource dataSource = new HikariDataSource();
|
||||||
dataSource.setJdbcUrl("jdbc:sqlite:sample.db");
|
// dataSource.setJdbcUrl("jdbc:sqlite:sample.db");
|
||||||
|
// //dataSource.setUsername("root");
|
||||||
|
// //dataSource.setPassword("123456");
|
||||||
|
|
||||||
|
DruidDataSource dataSource = new DruidDataSource();
|
||||||
|
dataSource.setUrl("jdbc:sqlite:sample.db");
|
||||||
//dataSource.setUsername("root");
|
//dataSource.setUsername("root");
|
||||||
//dataSource.setPassword("123456");
|
//dataSource.setPassword("123456");
|
||||||
|
|
||||||
@ -62,7 +70,7 @@ public class SqliteGeneratorTest {
|
|||||||
generator.generate();
|
generator.generate();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createTestTable(HikariDataSource dataSource) {
|
private void createTestTable(DataSource dataSource) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Connection connection = dataSource.getConnection();
|
Connection connection = dataSource.getConnection();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user