dataease-dm/backend/src/main/java/io/dataease/datasource/provider/JdbcProvider.java

186 lines
6.6 KiB
Java
Raw Normal View History

2021-02-20 18:35:09 +08:00
package io.dataease.datasource.provider;
import com.google.gson.Gson;
import io.dataease.datasource.constants.DatasourceTypes;
import io.dataease.datasource.dto.MysqlConfigrationDTO;
2021-02-22 18:06:37 +08:00
import io.dataease.datasource.dto.TableFiled;
2021-02-20 18:35:09 +08:00
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.sql.*;
2021-02-22 18:06:37 +08:00
import java.util.*;
2021-02-20 18:35:09 +08:00
import io.dataease.datasource.constants.DatasourceTypes.*;
@Service("jdbc")
public class JdbcProvider extends DatasourceProvider{
@Override
public List<String[]> getData() throws Exception {
List<String[]> list = new LinkedList<>();
try (
Connection connection = getConnection();
Statement stat = connection.createStatement();
ResultSet rs = stat.executeQuery(getQuery())
) {
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
while (rs.next()) {
String[] row = new String[columnCount];
for (int j = 0; j < columnCount; j++) {
int columType = metaData.getColumnType(j + 1);
switch (columType) {
case java.sql.Types.DATE:
row[j] = rs.getDate(j + 1).toString();
break;
default:
row[j] = rs.getString(j + 1);
break;
}
}
list.add(row);
}
2021-02-22 18:06:37 +08:00
} catch (SQLException e){
throw new Exception("ERROR:" + e.getMessage(), e);
}catch (Exception e) {
2021-02-20 18:35:09 +08:00
throw new Exception("ERROR:" + e.getMessage(), e);
}
return list;
}
@Override
public List<String> getTables() throws Exception {
List<String> tables = new ArrayList<>();
String queryStr = "show tables";
try (Connection con = getConnection(); Statement ps = con.createStatement()) {
ResultSet resultSet = ps.executeQuery(queryStr);
while (resultSet.next()){
tables.add(resultSet.getString(1));
}
} catch (Exception e) {
throw new Exception("ERROR: " + e.getMessage(), e);
}
return tables;
}
2021-02-22 18:06:37 +08:00
@Override
public List<TableFiled> getTableFileds(String table) throws Exception{
List<TableFiled> list = new LinkedList<>();
try (
Connection connection = getConnection();
) {
DatabaseMetaData databaseMetaData = connection.getMetaData();
ResultSet resultSet = databaseMetaData.getColumns(null, "%", table.toUpperCase(), "%");
while (resultSet.next()) {
String tableName=resultSet.getString("TABLE_NAME");
String database = resultSet.getString("TABLE_CAT");
if(tableName.equals(table) && database.equalsIgnoreCase(getDatabase())){
TableFiled tableFiled = new TableFiled();
String colName = resultSet.getString("COLUMN_NAME");
tableFiled.setFieldName(colName);
String remarks = resultSet.getString("REMARKS");
if(remarks == null || remarks.equals("")){
remarks = colName;
}
tableFiled.setRemarks(remarks);
String dbType = resultSet.getString("TYPE_NAME");
tableFiled.setFieldType(dbType);
list.add(tableFiled);
}
}
} catch (SQLException e){
throw new Exception("ERROR:" + e.getMessage(), e);
}catch (Exception e) {
throw new Exception("ERROR:" + e.getMessage(), e);
}
return list;
};
2021-02-20 18:35:09 +08:00
@Override
public void test() throws Exception {
String queryStr = "show tables";
try (Connection con = getConnection(); Statement ps = con.createStatement()) {
ResultSet resultSet = ps.executeQuery(queryStr);
} catch (Exception e) {
throw new Exception("ERROR: " + e.getMessage(), e);
}
}
private Connection getConnection() throws Exception {
String username = null;
String password = null;
String driver = null;
String jdbcurl = null;
2021-02-22 15:30:15 +08:00
DatasourceTypes datasourceType = DatasourceTypes.valueOf(getDatasource().getType());
2021-02-20 18:35:09 +08:00
switch (datasourceType){
case mysql:
2021-02-22 15:30:15 +08:00
MysqlConfigrationDTO mysqlConfigrationDTO = new Gson().fromJson(getDatasource().getConfiguration(), MysqlConfigrationDTO.class);
2021-02-20 18:35:09 +08:00
username = mysqlConfigrationDTO.getUsername();
password = mysqlConfigrationDTO.getPassword();
driver = mysqlConfigrationDTO.getDriver();
jdbcurl = mysqlConfigrationDTO.getJdbc();
break;
default:
break;
}
Class.forName(driver);
Properties props = new Properties();
props.setProperty("user", username);
if (StringUtils.isNotBlank(password)) {
props.setProperty("password", password);
}
return DriverManager.getConnection(jdbcurl, props);
}
2021-02-22 18:06:37 +08:00
private String getDatabase(){
DatasourceTypes datasourceType = DatasourceTypes.valueOf(getDatasource().getType());
switch (datasourceType) {
case mysql:
MysqlConfigrationDTO mysqlConfigrationDTO = new Gson().fromJson(getDatasource().getConfiguration(), MysqlConfigrationDTO.class);
return mysqlConfigrationDTO.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";
default:
return "1";
}
}
2021-02-20 18:35:09 +08:00
}