mirror of
https://github.com/dataease/dataease.git
synced 2026-06-17 13:01:44 +08:00
Merge pull request #142 from dataease/pr@dev@oracle
feat(数据源): 添加oracle 数据源时,指定schema
This commit is contained in:
@@ -61,4 +61,11 @@ public class DatasourceController {
|
||||
public List<DBTableDTO> getTables(@RequestBody Datasource datasource) throws Exception {
|
||||
return datasourceService.getTables(datasource);
|
||||
}
|
||||
|
||||
@PostMapping("/getSchema")
|
||||
public List<String> getSchema(@RequestBody Datasource datasource) throws Exception {
|
||||
return datasourceService.getSchema(datasource);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ public class OracleConfigration extends JdbcDTO {
|
||||
|
||||
private String driver = "oracle.jdbc.driver.OracleDriver";
|
||||
private String connectionType;
|
||||
private String schema;
|
||||
|
||||
public String getJdbc() {
|
||||
// 连接参数先写死,后边要把编码、时区等参数放到数据源的设置中
|
||||
|
||||
@@ -30,4 +30,6 @@ public abstract class DatasourceProvider {
|
||||
abstract public Map<String, List> fetchResultAndField(DatasourceRequest datasourceRequest) throws Exception;
|
||||
|
||||
abstract public void handleDatasource(DatasourceRequest datasourceRequest, String type) throws Exception;
|
||||
|
||||
abstract public List<String> getSchema(DatasourceRequest datasourceRequest) throws Exception;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package io.dataease.datasource.provider;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
||||
import io.dataease.controller.handler.annotation.I18n;
|
||||
import io.dataease.datasource.constants.DatasourceTypes;
|
||||
import io.dataease.datasource.dto.MysqlConfigration;
|
||||
import io.dataease.datasource.dto.OracleConfigration;
|
||||
@@ -9,10 +10,12 @@ import io.dataease.datasource.dto.SqlServerConfigration;
|
||||
import io.dataease.datasource.dto.TableFiled;
|
||||
import io.dataease.datasource.request.DatasourceRequest;
|
||||
import io.dataease.exception.DataEaseException;
|
||||
import io.dataease.i18n.Translator;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.beans.PropertyVetoException;
|
||||
import java.sql.*;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.*;
|
||||
|
||||
@Service("jdbc")
|
||||
@@ -212,6 +215,31 @@ public class JdbcProvider extends DatasourceProvider {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getSchema(DatasourceRequest datasourceRequest) throws Exception {
|
||||
List<String> schemas = new ArrayList<>();
|
||||
String queryStr = getSchemaSql(datasourceRequest);
|
||||
Connection con = null;
|
||||
try {
|
||||
con = getConnection(datasourceRequest);
|
||||
Statement statement = con.createStatement();
|
||||
ResultSet resultSet = statement.executeQuery(queryStr);
|
||||
while (resultSet.next()) {
|
||||
schemas.add(resultSet.getString(1));
|
||||
}
|
||||
resultSet.close();
|
||||
statement.close();
|
||||
return schemas;
|
||||
} catch (Exception e) {
|
||||
DataEaseException.throwException(e);
|
||||
} finally {
|
||||
if(con != null){
|
||||
con.close();
|
||||
}
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TableFiled> getTableFileds(DatasourceRequest datasourceRequest) throws Exception {
|
||||
List<TableFiled> list = new LinkedList<>();
|
||||
@@ -466,7 +494,7 @@ public class JdbcProvider extends DatasourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private String getTablesSql(DatasourceRequest datasourceRequest) {
|
||||
private String getTablesSql(DatasourceRequest datasourceRequest) throws Exception {
|
||||
DatasourceTypes datasourceType = DatasourceTypes.valueOf(datasourceRequest.getDatasource().getType());
|
||||
switch (datasourceType) {
|
||||
case mysql:
|
||||
@@ -477,7 +505,21 @@ public class JdbcProvider extends DatasourceProvider {
|
||||
SqlServerConfigration sqlServerConfigration = new Gson().fromJson(datasourceRequest.getDatasource().getConfiguration(), SqlServerConfigration.class);
|
||||
return "SELECT TABLE_NAME FROM DATABASE.INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE';".replace("DATABASE", sqlServerConfigration.getDataBase());
|
||||
case oracle:
|
||||
return "select TABLE_NAME from USER_TABLES";
|
||||
OracleConfigration oracleConfigration = new Gson().fromJson(datasourceRequest.getDatasource().getConfiguration(), OracleConfigration.class);
|
||||
if(StringUtils.isEmpty(oracleConfigration.getSchema())){
|
||||
throw new Exception(Translator.get("i18n_schema_is_empty"));
|
||||
}
|
||||
return "select table_name, owner from all_tables where owner='" + oracleConfigration.getSchema() + "'";
|
||||
default:
|
||||
return "show tables;";
|
||||
}
|
||||
}
|
||||
|
||||
private String getSchemaSql(DatasourceRequest datasourceRequest) {
|
||||
DatasourceTypes datasourceType = DatasourceTypes.valueOf(datasourceRequest.getDatasource().getType());
|
||||
switch (datasourceType) {
|
||||
case oracle:
|
||||
return "select * from all_users";
|
||||
default:
|
||||
return "show tables;";
|
||||
}
|
||||
|
||||
@@ -120,6 +120,13 @@ public class DatasourceService {
|
||||
datasourceProvider.checkStatus(datasourceRequest);
|
||||
}
|
||||
|
||||
public List<String> getSchema(Datasource datasource) throws Exception {
|
||||
DatasourceProvider datasourceProvider = ProviderFactory.getProvider(datasource.getType());
|
||||
DatasourceRequest datasourceRequest = new DatasourceRequest();
|
||||
datasourceRequest.setDatasource(datasource);
|
||||
return datasourceProvider.getSchema(datasourceRequest);
|
||||
}
|
||||
|
||||
public List<DBTableDTO> getTables(Datasource datasource) throws Exception {
|
||||
Datasource ds = datasourceMapper.selectByPrimaryKey(datasource.getId());
|
||||
DatasourceProvider datasourceProvider = ProviderFactory.getProvider(ds.getType());
|
||||
|
||||
Reference in New Issue
Block a user