feat: 获取表的colume属性
This commit is contained in:
parent
edeba4bb6a
commit
6d8158ffbe
@ -2,43 +2,25 @@ package io.dataease.datasource.provider;
|
|||||||
|
|
||||||
import io.dataease.base.domain.Datasource;
|
import io.dataease.base.domain.Datasource;
|
||||||
import io.dataease.datasource.dto.TableFiled;
|
import io.dataease.datasource.dto.TableFiled;
|
||||||
|
import io.dataease.datasource.request.DatasourceRequest;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public abstract class DatasourceProvider {
|
public abstract class DatasourceProvider {
|
||||||
|
|
||||||
protected String query;
|
|
||||||
private int resultLimit = 30000;
|
private int resultLimit = 30000;
|
||||||
protected Datasource datasource;
|
|
||||||
|
|
||||||
|
abstract public List<String[]> getData(DatasourceRequest datasourceRequest) throws Exception;
|
||||||
|
|
||||||
public String getQuery() {
|
abstract public List<String> getTables(DatasourceRequest datasourceRequest) throws Exception;
|
||||||
return query;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setQuery(String query) {
|
public List<TableFiled> getTableFileds(DatasourceRequest datasourceRequest) throws Exception{
|
||||||
this.query = query;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Datasource getDatasource() {
|
|
||||||
return datasource;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDatasource(Datasource datasource) {
|
|
||||||
this.datasource = datasource;
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract public List<String[]> getData() throws Exception;
|
|
||||||
|
|
||||||
abstract public List<String> getTables() throws Exception;
|
|
||||||
|
|
||||||
public List<TableFiled> getTableFileds(String table) throws Exception{
|
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
};
|
};
|
||||||
|
|
||||||
public void test() throws Exception {
|
public void test(DatasourceRequest datasourceRequest) throws Exception {
|
||||||
getData();
|
getData(datasourceRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,25 +4,22 @@ import com.google.gson.Gson;
|
|||||||
import io.dataease.datasource.constants.DatasourceTypes;
|
import io.dataease.datasource.constants.DatasourceTypes;
|
||||||
import io.dataease.datasource.dto.MysqlConfigrationDTO;
|
import io.dataease.datasource.dto.MysqlConfigrationDTO;
|
||||||
import io.dataease.datasource.dto.TableFiled;
|
import io.dataease.datasource.dto.TableFiled;
|
||||||
|
import io.dataease.datasource.request.DatasourceRequest;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import io.dataease.datasource.constants.DatasourceTypes.*;
|
|
||||||
|
|
||||||
|
|
||||||
@Service("jdbc")
|
@Service("jdbc")
|
||||||
public class JdbcProvider extends DatasourceProvider{
|
public class JdbcProvider extends DatasourceProvider{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String[]> getData() throws Exception {
|
public List<String[]> getData(DatasourceRequest datasourceRequest) throws Exception {
|
||||||
List<String[]> list = new LinkedList<>();
|
List<String[]> list = new LinkedList<>();
|
||||||
try (
|
try (
|
||||||
Connection connection = getConnection();
|
Connection connection = getConnection(datasourceRequest);
|
||||||
Statement stat = connection.createStatement();
|
Statement stat = connection.createStatement();
|
||||||
ResultSet rs = stat.executeQuery(getQuery())
|
ResultSet rs = stat.executeQuery(datasourceRequest.getQuery())
|
||||||
) {
|
) {
|
||||||
ResultSetMetaData metaData = rs.getMetaData();
|
ResultSetMetaData metaData = rs.getMetaData();
|
||||||
int columnCount = metaData.getColumnCount();
|
int columnCount = metaData.getColumnCount();
|
||||||
@ -50,11 +47,14 @@ public class JdbcProvider extends DatasourceProvider{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getTables() throws Exception {
|
public List<String> getTables(DatasourceRequest datasourceRequest) throws Exception {
|
||||||
List<String> tables = new ArrayList<>();
|
List<String> tables = new ArrayList<>();
|
||||||
|
|
||||||
String queryStr = "show tables";
|
String queryStr = "show tables";
|
||||||
try (Connection con = getConnection(); Statement ps = con.createStatement()) {
|
if(StringUtils.isNotEmpty(datasourceRequest.getQuery())){
|
||||||
|
queryStr = datasourceRequest.getQuery();
|
||||||
|
}
|
||||||
|
try (Connection con = getConnection(datasourceRequest); Statement ps = con.createStatement()) {
|
||||||
ResultSet resultSet = ps.executeQuery(queryStr);
|
ResultSet resultSet = ps.executeQuery(queryStr);
|
||||||
while (resultSet.next()){
|
while (resultSet.next()){
|
||||||
tables.add(resultSet.getString(1));
|
tables.add(resultSet.getString(1));
|
||||||
@ -66,17 +66,17 @@ public class JdbcProvider extends DatasourceProvider{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<TableFiled> getTableFileds(String table) throws Exception{
|
public List<TableFiled> getTableFileds(DatasourceRequest datasourceRequest) throws Exception{
|
||||||
List<TableFiled> list = new LinkedList<>();
|
List<TableFiled> list = new LinkedList<>();
|
||||||
try (
|
try (
|
||||||
Connection connection = getConnection();
|
Connection connection = getConnection(datasourceRequest);
|
||||||
) {
|
) {
|
||||||
DatabaseMetaData databaseMetaData = connection.getMetaData();
|
DatabaseMetaData databaseMetaData = connection.getMetaData();
|
||||||
ResultSet resultSet = databaseMetaData.getColumns(null, "%", table.toUpperCase(), "%");
|
ResultSet resultSet = databaseMetaData.getColumns(null, "%", datasourceRequest.getTable().toUpperCase(), "%");
|
||||||
while (resultSet.next()) {
|
while (resultSet.next()) {
|
||||||
String tableName=resultSet.getString("TABLE_NAME");
|
String tableName=resultSet.getString("TABLE_NAME");
|
||||||
String database = resultSet.getString("TABLE_CAT");
|
String database = resultSet.getString("TABLE_CAT");
|
||||||
if(tableName.equals(table) && database.equalsIgnoreCase(getDatabase())){
|
if(tableName.equals(datasourceRequest.getTable()) && database.equalsIgnoreCase(getDatabase(datasourceRequest))){
|
||||||
TableFiled tableFiled = new TableFiled();
|
TableFiled tableFiled = new TableFiled();
|
||||||
String colName = resultSet.getString("COLUMN_NAME");
|
String colName = resultSet.getString("COLUMN_NAME");
|
||||||
tableFiled.setFieldName(colName);
|
tableFiled.setFieldName(colName);
|
||||||
@ -99,24 +99,24 @@ public class JdbcProvider extends DatasourceProvider{
|
|||||||
};
|
};
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void test() throws Exception {
|
public void test(DatasourceRequest datasourceRequest) throws Exception {
|
||||||
String queryStr = "show tables";
|
String queryStr = "show tables";
|
||||||
try (Connection con = getConnection(); Statement ps = con.createStatement()) {
|
try (Connection con = getConnection(datasourceRequest); Statement ps = con.createStatement()) {
|
||||||
ResultSet resultSet = ps.executeQuery(queryStr);
|
ResultSet resultSet = ps.executeQuery(queryStr);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new Exception("ERROR: " + e.getMessage(), e);
|
throw new Exception("ERROR: " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Connection getConnection() throws Exception {
|
private Connection getConnection(DatasourceRequest datasourceRequest) throws Exception {
|
||||||
String username = null;
|
String username = null;
|
||||||
String password = null;
|
String password = null;
|
||||||
String driver = null;
|
String driver = null;
|
||||||
String jdbcurl = null;
|
String jdbcurl = null;
|
||||||
DatasourceTypes datasourceType = DatasourceTypes.valueOf(getDatasource().getType());
|
DatasourceTypes datasourceType = DatasourceTypes.valueOf(datasourceRequest.getDatasource().getType());
|
||||||
switch (datasourceType){
|
switch (datasourceType){
|
||||||
case mysql:
|
case mysql:
|
||||||
MysqlConfigrationDTO mysqlConfigrationDTO = new Gson().fromJson(getDatasource().getConfiguration(), MysqlConfigrationDTO.class);
|
MysqlConfigrationDTO mysqlConfigrationDTO = new Gson().fromJson(datasourceRequest.getDatasource().getConfiguration(), MysqlConfigrationDTO.class);
|
||||||
username = mysqlConfigrationDTO.getUsername();
|
username = mysqlConfigrationDTO.getUsername();
|
||||||
password = mysqlConfigrationDTO.getPassword();
|
password = mysqlConfigrationDTO.getPassword();
|
||||||
driver = mysqlConfigrationDTO.getDriver();
|
driver = mysqlConfigrationDTO.getDriver();
|
||||||
@ -135,11 +135,11 @@ public class JdbcProvider extends DatasourceProvider{
|
|||||||
return DriverManager.getConnection(jdbcurl, props);
|
return DriverManager.getConnection(jdbcurl, props);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getDatabase(){
|
private String getDatabase(DatasourceRequest datasourceRequest){
|
||||||
DatasourceTypes datasourceType = DatasourceTypes.valueOf(getDatasource().getType());
|
DatasourceTypes datasourceType = DatasourceTypes.valueOf(datasourceRequest.getDatasource().getType());
|
||||||
switch (datasourceType) {
|
switch (datasourceType) {
|
||||||
case mysql:
|
case mysql:
|
||||||
MysqlConfigrationDTO mysqlConfigrationDTO = new Gson().fromJson(getDatasource().getConfiguration(), MysqlConfigrationDTO.class);
|
MysqlConfigrationDTO mysqlConfigrationDTO = new Gson().fromJson(datasourceRequest.getDatasource().getConfiguration(), MysqlConfigrationDTO.class);
|
||||||
return mysqlConfigrationDTO.getDataBase();
|
return mysqlConfigrationDTO.getDataBase();
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@ -0,0 +1,16 @@
|
|||||||
|
package io.dataease.datasource.request;
|
||||||
|
|
||||||
|
import io.dataease.base.domain.Datasource;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class DatasourceRequest {
|
||||||
|
protected String query;
|
||||||
|
protected String table;
|
||||||
|
protected Datasource datasource;
|
||||||
|
|
||||||
|
}
|
||||||
@ -8,6 +8,7 @@ import io.dataease.datasource.dto.MysqlConfigrationDTO;
|
|||||||
import io.dataease.datasource.provider.DatasourceProvider;
|
import io.dataease.datasource.provider.DatasourceProvider;
|
||||||
import io.dataease.datasource.provider.JdbcProvider;
|
import io.dataease.datasource.provider.JdbcProvider;
|
||||||
import io.dataease.datasource.provider.ProviderFactory;
|
import io.dataease.datasource.provider.ProviderFactory;
|
||||||
|
import io.dataease.datasource.request.DatasourceRequest;
|
||||||
import org.apache.commons.collections.CollectionUtils;
|
import org.apache.commons.collections.CollectionUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -65,8 +66,9 @@ public class DatasourceService {
|
|||||||
|
|
||||||
public void validate(Datasource datasource)throws Exception {
|
public void validate(Datasource datasource)throws Exception {
|
||||||
DatasourceProvider datasourceProvider = ProviderFactory.getProvider(datasource.getType());
|
DatasourceProvider datasourceProvider = ProviderFactory.getProvider(datasource.getType());
|
||||||
datasourceProvider.setDatasource(datasource);
|
DatasourceRequest datasourceRequest = new DatasourceRequest();
|
||||||
datasourceProvider.test();
|
datasourceRequest.setDatasource(datasource);
|
||||||
|
datasourceProvider.test(datasourceRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user