mirror of
https://github.com/dataease/dataease.git
synced 2026-05-18 09:48:10 +08:00
feat: 支持 SQL Server
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
package io.dataease.datasource.constants;
|
||||
|
||||
public enum DatasourceTypes {
|
||||
mysql
|
||||
mysql, sqlServer
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package io.dataease.datasource.dto;
|
||||
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class JdbcDTO {
|
||||
private String host;
|
||||
private Integer port;
|
||||
private String username;
|
||||
private String password;
|
||||
private String dataBase;
|
||||
private String dataSourceType = "jdbc";
|
||||
}
|
||||
@@ -6,20 +6,11 @@ import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class MysqlConfigrationDTO {
|
||||
private String host;
|
||||
private Integer port;
|
||||
private String username;
|
||||
private String password;
|
||||
private String jdbc;
|
||||
private String dataBase;
|
||||
public class MysqlConfigrationDTO extends JdbcDTO {
|
||||
|
||||
private String driver = "com.mysql.cj.jdbc.Driver";
|
||||
|
||||
public String getJdbc(){
|
||||
if(StringUtils.isNotEmpty(jdbc)){
|
||||
return jdbc;
|
||||
}else {
|
||||
return "jdbc:mysql://HOSTNAME:PORT/DATABASE".replace("HOSTNAME", host).replace("PORT", port.toString()).replace("DATABASE", dataBase);
|
||||
}
|
||||
return "jdbc:mysql://HOSTNAME:PORT/DATABASE".replace("HOSTNAME", getHost()).replace("PORT", getPort().toString()).replace("DATABASE", getDataBase());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package io.dataease.datasource.dto;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class SqlServerConfigration extends JdbcDTO {
|
||||
private String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
|
||||
|
||||
public String getJdbc(){
|
||||
return "jdbc:sqlserver://HOSTNAME:PORT;DatabaseName=DATABASE".replace("HOSTNAME", getHost()).replace("PORT", getPort().toString()).replace("DATABASE", getDataBase());
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package io.dataease.datasource.provider;
|
||||
import com.google.gson.Gson;
|
||||
import io.dataease.datasource.constants.DatasourceTypes;
|
||||
import io.dataease.datasource.dto.MysqlConfigrationDTO;
|
||||
import io.dataease.datasource.dto.SqlServerConfigration;
|
||||
import io.dataease.datasource.dto.TableFiled;
|
||||
import io.dataease.datasource.request.DatasourceRequest;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -13,13 +14,14 @@ import java.util.*;
|
||||
@Service("jdbc")
|
||||
public class JdbcProvider extends DatasourceProvider{
|
||||
|
||||
|
||||
@Override
|
||||
public List<String[]> getData(DatasourceRequest datasourceRequest) throws Exception {
|
||||
List<String[]> list = new LinkedList<>();
|
||||
try (
|
||||
Connection connection = getConnection(datasourceRequest);
|
||||
Statement stat = connection.createStatement();
|
||||
ResultSet rs = stat.executeQuery(datasourceRequest.getQuery())
|
||||
Connection connection = getConnection(datasourceRequest);
|
||||
Statement stat = connection.createStatement();
|
||||
ResultSet rs = stat.executeQuery(datasourceRequest.getQuery())
|
||||
) {
|
||||
ResultSetMetaData metaData = rs.getMetaData();
|
||||
int columnCount = metaData.getColumnCount();
|
||||
@@ -49,11 +51,7 @@ public class JdbcProvider extends DatasourceProvider{
|
||||
@Override
|
||||
public List<String> getTables(DatasourceRequest datasourceRequest) throws Exception {
|
||||
List<String> tables = new ArrayList<>();
|
||||
|
||||
String queryStr = "show tables";
|
||||
if(StringUtils.isNotEmpty(datasourceRequest.getQuery())){
|
||||
queryStr = datasourceRequest.getQuery();
|
||||
}
|
||||
String queryStr = getTablesSql(datasourceRequest);
|
||||
try (Connection con = getConnection(datasourceRequest); Statement ps = con.createStatement()) {
|
||||
ResultSet resultSet = ps.executeQuery(queryStr);
|
||||
while (resultSet.next()){
|
||||
@@ -74,7 +72,7 @@ public class JdbcProvider extends DatasourceProvider{
|
||||
DatabaseMetaData databaseMetaData = connection.getMetaData();
|
||||
ResultSet resultSet = databaseMetaData.getColumns(null, "%", datasourceRequest.getTable().toUpperCase(), "%");
|
||||
while (resultSet.next()) {
|
||||
String tableName=resultSet.getString("TABLE_NAME");
|
||||
String tableName = resultSet.getString("TABLE_NAME");
|
||||
String database = resultSet.getString("TABLE_CAT");
|
||||
if(tableName.equals(datasourceRequest.getTable()) && database.equalsIgnoreCase(getDatabase(datasourceRequest))){
|
||||
TableFiled tableFiled = new TableFiled();
|
||||
@@ -100,7 +98,7 @@ public class JdbcProvider extends DatasourceProvider{
|
||||
|
||||
@Override
|
||||
public void test(DatasourceRequest datasourceRequest) throws Exception {
|
||||
String queryStr = "show tables";
|
||||
String queryStr = getTablesSql(datasourceRequest);
|
||||
try (Connection con = getConnection(datasourceRequest); Statement ps = con.createStatement()) {
|
||||
ResultSet resultSet = ps.executeQuery(queryStr);
|
||||
} catch (Exception e) {
|
||||
@@ -122,6 +120,13 @@ public class JdbcProvider extends DatasourceProvider{
|
||||
driver = mysqlConfigrationDTO.getDriver();
|
||||
jdbcurl = mysqlConfigrationDTO.getJdbc();
|
||||
break;
|
||||
case sqlServer:
|
||||
SqlServerConfigration sqlServerConfigration= new Gson().fromJson(datasourceRequest.getDatasource().getConfiguration(), SqlServerConfigration.class);
|
||||
username = sqlServerConfigration.getUsername();
|
||||
password = sqlServerConfigration.getPassword();
|
||||
driver = sqlServerConfigration.getDriver();
|
||||
jdbcurl = sqlServerConfigration.getJdbc();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -141,45 +146,23 @@ public class JdbcProvider extends DatasourceProvider{
|
||||
case mysql:
|
||||
MysqlConfigrationDTO mysqlConfigrationDTO = new Gson().fromJson(datasourceRequest.getDatasource().getConfiguration(), MysqlConfigrationDTO.class);
|
||||
return mysqlConfigrationDTO.getDataBase();
|
||||
case sqlServer:
|
||||
SqlServerConfigration sqlServerConfigration = new Gson().fromJson(datasourceRequest.getDatasource().getConfiguration(), SqlServerConfigration.class);
|
||||
return sqlServerConfigration.getDataBase();
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static String getSchema(Connection conn) throws Exception {
|
||||
String schema;
|
||||
schema = conn.getMetaData().getUserName();
|
||||
System.out.println(schema);
|
||||
if ((schema == null) || (schema.length() == 0)) {
|
||||
throw new Exception("ORACLE数据库模式不允许为空");
|
||||
}
|
||||
return schema.toUpperCase().toString();
|
||||
}
|
||||
|
||||
private static String changeDbType(String dbType) {
|
||||
dbType = dbType.toUpperCase();
|
||||
switch(dbType){
|
||||
case "VARCHAR":
|
||||
case "VARCHAR2":
|
||||
case "CHAR":
|
||||
return "1";
|
||||
case "NUMBER":
|
||||
case "DECIMAL":
|
||||
return "4";
|
||||
case "INT":
|
||||
case "SMALLINT":
|
||||
case "INTEGER":
|
||||
return "2";
|
||||
case "BIGINT":
|
||||
return "6";
|
||||
case "DATETIME":
|
||||
case "TIMESTAMP":
|
||||
case "DATE":
|
||||
return "7";
|
||||
private String getTablesSql(DatasourceRequest datasourceRequest){
|
||||
DatasourceTypes datasourceType = DatasourceTypes.valueOf(datasourceRequest.getDatasource().getType());
|
||||
switch (datasourceType){
|
||||
case mysql:
|
||||
return "show tables;";
|
||||
case sqlServer:
|
||||
return "SELECT TABLE_NAME FROM fit2cloud2.INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE';";
|
||||
default:
|
||||
return "1";
|
||||
return "show tables;";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -19,12 +19,12 @@ public class ProviderFactory implements ApplicationContextAware {
|
||||
}
|
||||
|
||||
public static DatasourceProvider getProvider(String type){
|
||||
System.out.println(type);
|
||||
DatasourceTypes datasourceType = DatasourceTypes.valueOf(type);
|
||||
System.out.println(datasourceType.name());
|
||||
switch (datasourceType){
|
||||
case mysql:
|
||||
return context.getBean("jdbc", DatasourceProvider.class);
|
||||
case sqlServer:
|
||||
return context.getBean("jdbc", DatasourceProvider.class);
|
||||
default:
|
||||
return context.getBean("jdbc", DatasourceProvider.class);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import io.dataease.base.domain.Datasource;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package io.dataease.datasource.service;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import io.dataease.base.domain.*;
|
||||
import io.dataease.base.mapper.*;
|
||||
import io.dataease.commons.exception.DEException;
|
||||
import io.dataease.datasource.dto.MysqlConfigrationDTO;
|
||||
import io.dataease.datasource.provider.DatasourceProvider;
|
||||
import io.dataease.datasource.provider.JdbcProvider;
|
||||
import io.dataease.datasource.provider.ProviderFactory;
|
||||
import io.dataease.datasource.request.DatasourceRequest;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
@@ -15,10 +12,8 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
|
||||
Reference in New Issue
Block a user