forked from github/dataease
feat: 支持CTE语法
This commit is contained in:
parent
1ef965357d
commit
129e101453
@ -19,12 +19,16 @@ import java.io.IOException;
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
@Service("jdbc")
|
@Service("jdbc")
|
||||||
public class JdbcProvider extends DatasourceProvider {
|
public class JdbcProvider extends DatasourceProvider {
|
||||||
private static Map<String, DruidDataSource> jdbcConnection = new HashMap<>();
|
private static Map<String, DruidDataSource> jdbcConnection = new HashMap<>();
|
||||||
public ExtendedJdbcClassLoader extendedJdbcClassLoader;
|
public ExtendedJdbcClassLoader extendedJdbcClassLoader;
|
||||||
static private String FILE_PATH = "/opt/dataease/drivers";
|
static private String FILE_PATH = "/opt/dataease/drivers";
|
||||||
|
private static final String REG_WITH_SQL_FRAGMENT = "((?i)WITH[\\s\\S]+(?i)AS?\\s*\\([\\s\\S]+\\))\\s*(?i)SELECT";
|
||||||
|
public static final Pattern WITH_SQL_FRAGMENT = Pattern.compile(REG_WITH_SQL_FRAGMENT);
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() throws Exception{
|
public void init() throws Exception{
|
||||||
@ -63,7 +67,7 @@ public class JdbcProvider extends DatasourceProvider {
|
|||||||
@Override
|
@Override
|
||||||
public List<String[]> getData(DatasourceRequest dsr) throws Exception {
|
public List<String[]> getData(DatasourceRequest dsr) throws Exception {
|
||||||
List<String[]> list = new LinkedList<>();
|
List<String[]> list = new LinkedList<>();
|
||||||
try (Connection connection = getConnectionFromPool(dsr); Statement stat = connection.createStatement(); ResultSet rs = stat.executeQuery(dsr.getQuery())){
|
try (Connection connection = getConnectionFromPool(dsr); Statement stat = connection.createStatement(); ResultSet rs = stat.executeQuery(rebuildSqlWithFragment(dsr.getQuery()) )){
|
||||||
|
|
||||||
list = fetchResult(rs);
|
list = fetchResult(rs);
|
||||||
|
|
||||||
@ -92,7 +96,7 @@ public class JdbcProvider extends DatasourceProvider {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String[]> fetchResult(DatasourceRequest datasourceRequest) throws Exception {
|
public List<String[]> fetchResult(DatasourceRequest datasourceRequest) throws Exception {
|
||||||
try (Connection connection = getConnectionFromPool(datasourceRequest); Statement stat = connection.createStatement(); ResultSet rs = stat.executeQuery(datasourceRequest.getQuery())){
|
try (Connection connection = getConnectionFromPool(datasourceRequest); Statement stat = connection.createStatement(); ResultSet rs = stat.executeQuery(rebuildSqlWithFragment(datasourceRequest.getQuery()))){
|
||||||
return fetchResult(rs);
|
return fetchResult(rs);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
DataEaseException.throwException(e);
|
DataEaseException.throwException(e);
|
||||||
@ -128,7 +132,7 @@ public class JdbcProvider extends DatasourceProvider {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<TableFiled> fetchResultField(DatasourceRequest datasourceRequest) throws Exception {
|
public List<TableFiled> fetchResultField(DatasourceRequest datasourceRequest) throws Exception {
|
||||||
try (Connection connection = getConnectionFromPool(datasourceRequest); Statement stat = connection.createStatement(); ResultSet rs = stat.executeQuery(datasourceRequest.getQuery())){
|
try (Connection connection = getConnectionFromPool(datasourceRequest); Statement stat = connection.createStatement(); ResultSet rs = stat.executeQuery(rebuildSqlWithFragment(datasourceRequest.getQuery()))){
|
||||||
return fetchResultField(rs, datasourceRequest);
|
return fetchResultField(rs, datasourceRequest);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
DataEaseException.throwException(e);
|
DataEaseException.throwException(e);
|
||||||
@ -143,7 +147,7 @@ public class JdbcProvider extends DatasourceProvider {
|
|||||||
Map<String, List> result = new HashMap<>();
|
Map<String, List> result = new HashMap<>();
|
||||||
List<String[]> dataList = new LinkedList<>();
|
List<String[]> dataList = new LinkedList<>();
|
||||||
List<TableFiled> fieldList = new ArrayList<>();
|
List<TableFiled> fieldList = new ArrayList<>();
|
||||||
try (Connection connection = getConnectionFromPool(datasourceRequest); Statement stat = connection.createStatement(); ResultSet rs = stat.executeQuery(datasourceRequest.getQuery())){
|
try (Connection connection = getConnectionFromPool(datasourceRequest); Statement stat = connection.createStatement(); ResultSet rs = stat.executeQuery(rebuildSqlWithFragment(datasourceRequest.getQuery()))){
|
||||||
dataList = fetchResult(rs);
|
dataList = fetchResult(rs);
|
||||||
fieldList = fetchResultField(rs, datasourceRequest);
|
fieldList = fetchResultField(rs, datasourceRequest);
|
||||||
result.put("dataList", dataList);
|
result.put("dataList", dataList);
|
||||||
@ -372,18 +376,21 @@ public class JdbcProvider extends DatasourceProvider {
|
|||||||
MysqlConfiguration mysqlConfiguration = new Gson().fromJson(datasourceRequest.getDatasource().getConfiguration(), MysqlConfiguration.class);
|
MysqlConfiguration mysqlConfiguration = new Gson().fromJson(datasourceRequest.getDatasource().getConfiguration(), MysqlConfiguration.class);
|
||||||
dataSource.setUrl(mysqlConfiguration.getJdbc());
|
dataSource.setUrl(mysqlConfiguration.getJdbc());
|
||||||
dataSource.setDriverClassName(mysqlConfiguration.getDriver());
|
dataSource.setDriverClassName(mysqlConfiguration.getDriver());
|
||||||
|
dataSource.setValidationQuery("select 1");
|
||||||
jdbcConfiguration = mysqlConfiguration;
|
jdbcConfiguration = mysqlConfiguration;
|
||||||
break;
|
break;
|
||||||
case sqlServer:
|
case sqlServer:
|
||||||
SqlServerConfiguration sqlServerConfiguration = new Gson().fromJson(datasourceRequest.getDatasource().getConfiguration(), SqlServerConfiguration.class);
|
SqlServerConfiguration sqlServerConfiguration = new Gson().fromJson(datasourceRequest.getDatasource().getConfiguration(), SqlServerConfiguration.class);
|
||||||
dataSource.setDriverClassName(sqlServerConfiguration.getDriver());
|
dataSource.setDriverClassName(sqlServerConfiguration.getDriver());
|
||||||
dataSource.setUrl(sqlServerConfiguration.getJdbc());
|
dataSource.setUrl(sqlServerConfiguration.getJdbc());
|
||||||
|
dataSource.setValidationQuery("select 1");
|
||||||
jdbcConfiguration = sqlServerConfiguration;
|
jdbcConfiguration = sqlServerConfiguration;
|
||||||
break;
|
break;
|
||||||
case oracle:
|
case oracle:
|
||||||
OracleConfiguration oracleConfiguration = new Gson().fromJson(datasourceRequest.getDatasource().getConfiguration(), OracleConfiguration.class);
|
OracleConfiguration oracleConfiguration = new Gson().fromJson(datasourceRequest.getDatasource().getConfiguration(), OracleConfiguration.class);
|
||||||
dataSource.setDriverClassName(oracleConfiguration.getDriver());
|
dataSource.setDriverClassName(oracleConfiguration.getDriver());
|
||||||
dataSource.setUrl(oracleConfiguration.getJdbc());
|
dataSource.setUrl(oracleConfiguration.getJdbc());
|
||||||
|
dataSource.setValidationQuery("select 1 from dual");
|
||||||
jdbcConfiguration = oracleConfiguration;
|
jdbcConfiguration = oracleConfiguration;
|
||||||
break;
|
break;
|
||||||
case pg:
|
case pg:
|
||||||
@ -407,6 +414,7 @@ public class JdbcProvider extends DatasourceProvider {
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
dataSource.setUsername(jdbcConfiguration.getUsername());
|
dataSource.setUsername(jdbcConfiguration.getUsername());
|
||||||
dataSource.setDriverClassLoader(extendedJdbcClassLoader);
|
dataSource.setDriverClassLoader(extendedJdbcClassLoader);
|
||||||
dataSource.setPassword(jdbcConfiguration.getPassword());
|
dataSource.setPassword(jdbcConfiguration.getPassword());
|
||||||
@ -498,4 +506,23 @@ public class JdbcProvider extends DatasourceProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String rebuildSqlWithFragment(String sql) {
|
||||||
|
if (!sql.toLowerCase().startsWith("with")) {
|
||||||
|
Matcher matcher = WITH_SQL_FRAGMENT.matcher(sql);
|
||||||
|
if (matcher.find()) {
|
||||||
|
String withFragment = matcher.group();
|
||||||
|
if (!com.alibaba.druid.util.StringUtils.isEmpty(withFragment)) {
|
||||||
|
if (withFragment.length() > 6) {
|
||||||
|
int lastSelectIndex = withFragment.length() - 6;
|
||||||
|
sql = sql.replace(withFragment, withFragment.substring(lastSelectIndex));
|
||||||
|
withFragment = withFragment.substring(0, lastSelectIndex);
|
||||||
|
}
|
||||||
|
sql = withFragment + " " + sql;
|
||||||
|
sql = sql.replaceAll(" " + "{2,}", " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sql;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user