Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override AbstractJdbcDriver method getTable The default method takes … #3148

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions dinky-common/src/main/java/org/dinky/data/model/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.beans.Transient;
import java.io.Serializable;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -56,15 +57,26 @@ public class Table implements Serializable, Comparable<Table>, Cloneable {
private Long rows;
private Date createTime;
private Date updateTime;
/** 表类型 */
/**
* 表类型
*/
private TableType tableType = TableType.SINGLE_DATABASE_AND_TABLE;
/** 分库或分表对应的表名 */
/**
* 分库或分表对应的表名
*/
private List<String> schemaTableNameList;

private List<Column> columns;

public Table() {}

public Table(List<Column> columns, String databaseName, String tableName, String tableComment) throws SQLException {
this.name = tableName;
this.schema = databaseName;
this.comment = tableComment;
this.columns = columns;
}

public Table(String name, String schema, List<Column> columns) {
this.name = name;
this.schema = schema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.dinky.utils.TextUtil;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand Down Expand Up @@ -79,9 +80,8 @@
public abstract class AbstractJdbcDriver extends AbstractDriver<AbstractJdbcConfig> {

protected ThreadLocal<Connection> conn = new ThreadLocal<>();

private DruidDataSource dataSource;
protected String validationQuery = "select 1";
private DruidDataSource dataSource;

abstract String getDriverClass();

Expand Down Expand Up @@ -292,6 +292,23 @@ public List<Table> listTables(String schemaName) {
return tableList;
}

@Override
public Table getTable(String schemaName, String tableName) {
try {
DatabaseMetaData metaData = conn.get().getMetaData();
ResultSet tables = metaData.getTables(schemaName, null, tableName, new String[] {"TABLE"});
while (tables.next()) {
String tableComment = tables.getString("REMARKS");
List<Column> columns = listColumns(schemaName, tableName);
return new Table(columns, schemaName, tableName, tableComment);
}
} catch (SQLException e) {
log.error("GetTable error:", e);
throw new RuntimeException(e);
}
return null;
}

@Override
public List<Column> listColumns(String schemaName, String tableName) {
List<Column> columns = new ArrayList<>();
Expand Down Expand Up @@ -516,8 +533,11 @@ public int executeUpdate(String sql) throws Exception {
}

/**
* 标准sql where与order语法都是相同的 不同数据库limit语句不一样,需要单独交由driver去处理,例如oracle 通过{@query(String sql,
* Integer limit)}去截断返回数据,但是在大量数据情况下会导致数据库负载过高。
* 标准sql where与order语法都是相同的
* 不同数据库limit语句不一样,需要单独交由driver去处理,例如oracle
* 通过{@query(String sql,
* Integer
* limit)}去截断返回数据,但是在大量数据情况下会导致数据库负载过高。
*/
@Override
public StringBuilder genQueryOption(QueryData queryData) {
Expand Down Expand Up @@ -614,7 +634,6 @@ public JdbcSelectResult query(String sql, Integer limit) {

/**
* 如果执行多条语句返回最后一条语句执行结果
*
*/
@Override
public JdbcSelectResult executeSql(String sql, Integer limit) {
Expand Down
Loading