Merge pull request #6914 from dataease/pr@dev@fixSql

Pr@dev@fix sql
This commit is contained in:
taojinlong 2023-11-28 23:03:49 -06:00 committed by GitHub
commit 70effc90fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 29 deletions

View File

@ -1115,7 +1115,22 @@ public class DataSetTableService {
}
}
private String handlePlainSelect(PlainSelect plainSelect, Select statementSelect, String dsType) throws Exception {
private void handleSelectItems(PlainSelect plainSelect, String dsType) throws Exception{
List<SelectItem> selectItems = new ArrayList<>();
for (SelectItem selectItem : plainSelect.getSelectItems()) {
SelectExpressionItem selectExpressionItem = (SelectExpressionItem) selectItem;
if (selectExpressionItem.getExpression() instanceof SubSelect) {
SubSelect subSelect = (SubSelect) selectExpressionItem.getExpression();
Select select = (Select) CCJSqlParserUtil.parse (removeVariables(subSelect.getSelectBody().toString(), dsType));
subSelect.setSelectBody(select.getSelectBody());
((SelectExpressionItem) selectItem).setExpression(subSelect);
}
selectItems.add(selectItem);
}
plainSelect.setSelectItems(selectItems);
}
private void handleFromItems(PlainSelect plainSelect, String dsType) throws Exception{
FromItem fromItem = plainSelect.getFromItem();
if (fromItem instanceof SubSelect) {
SelectBody selectBody = ((SubSelect) fromItem).getSelectBody();
@ -1123,7 +1138,9 @@ public class DataSetTableService {
Select subSelectTmp = (Select) CCJSqlParserUtil.parse(removeVariables(selectBody.toString(), dsType));
subSelect.setSelectBody(subSelectTmp.getSelectBody());
if (dsType.equals(DatasourceTypes.oracle.getType())) {
subSelect.setAlias(new Alias(fromItem.getAlias().toString(), false));
if(fromItem.getAlias() != null){
subSelect.setAlias(new Alias(fromItem.getAlias().toString(), false));
}
} else {
if (fromItem.getAlias() == null) {
throw new Exception("Failed to parse sql, Every derived table must have its own alias");
@ -1132,6 +1149,8 @@ public class DataSetTableService {
}
plainSelect.setFromItem(subSelect);
}
}
private void handleJoins(PlainSelect plainSelect, String dsType) throws Exception{
List<Join> joins = plainSelect.getJoins();
if (joins != null) {
List<Join> joinsList = new ArrayList<>();
@ -1157,27 +1176,29 @@ public class DataSetTableService {
}
plainSelect.setJoins(joinsList);
}
Expression expr = plainSelect.getWhere();
if (expr == null) {
return handleWith(plainSelect, statementSelect, dsType);
}
StringBuilder stringBuilder = new StringBuilder();
BinaryExpression binaryExpression = null;
try {
binaryExpression = (BinaryExpression) expr;
} catch (Exception e) {
}
if (binaryExpression != null) {
if (!(binaryExpression.getLeftExpression() instanceof BinaryExpression) && !(binaryExpression.getLeftExpression() instanceof InExpression) && hasVariable(binaryExpression.getRightExpression().toString())) {
stringBuilder.append(SubstitutedSql);
} else {
expr.accept(getExpressionDeParser(stringBuilder));
}
} else {
expr.accept(getExpressionDeParser(stringBuilder));
}
plainSelect.setWhere(CCJSqlParserUtil.parseCondExpression(stringBuilder.toString()));
return handleWith(plainSelect, statementSelect, dsType);
}
private String handleWhere(PlainSelect plainSelect, Select statementSelect, String dsType) throws Exception{
Expression expr = plainSelect.getWhere();
if (expr == null) {
return handleWith(plainSelect, statementSelect, dsType);
}
StringBuilder stringBuilder = new StringBuilder();
BinaryExpression binaryExpression = null;
try {
binaryExpression = (BinaryExpression) expr;
} catch (Exception e) {
}
if (binaryExpression != null) {
if (!(binaryExpression.getLeftExpression() instanceof BinaryExpression) && !(binaryExpression.getLeftExpression() instanceof InExpression) && hasVariable(binaryExpression.getRightExpression().toString())) {
stringBuilder.append(SubstitutedSql);
} else {
expr.accept(getExpressionDeParser(stringBuilder));
}
} else {
expr.accept(getExpressionDeParser(stringBuilder));
}
plainSelect.setWhere(CCJSqlParserUtil.parseCondExpression(stringBuilder.toString()));
return handleWith(plainSelect, statementSelect, dsType);
}
private String handleWith(PlainSelect plainSelect, Select select, String dsType) throws Exception {
@ -1198,6 +1219,15 @@ public class DataSetTableService {
return builder.toString();
}
private String handlePlainSelect(PlainSelect plainSelect, Select statementSelect, String dsType) throws Exception {
handleSelectItems(plainSelect, dsType);
handleFromItems(plainSelect, dsType);
handleJoins(plainSelect, dsType);
return handleWhere(plainSelect, statementSelect, dsType);
}
public Map<String, Object> getDBPreview(DataSetTableRequest dataSetTableRequest) throws Exception {
Datasource ds = datasourceMapper.selectByPrimaryKey(dataSetTableRequest.getDataSourceId());
if (ds == null) {

View File

@ -1945,7 +1945,7 @@ export default {
please_input_max_pool_size: 'Please enter the maximum number of connections',
please_input_max_idle_time: 'Please enter the maximum idle (seconds)',
please_input_acquire_increment: 'Please enter the growth number',
please_input_query_timeout: 'Please enter query timeout',
please_input_query_timeout: 'Please enter query timeoutno less then zero',
please_input_connect_timeout: 'Please enter the connection timeout (seconds)',
no_less_then_0: 'Parameters in advanced settings cannot be less than zero',
port_no_less_then_0: 'Port cannot be less than zero',

View File

@ -1937,7 +1937,7 @@ export default {
please_input_max_pool_size: '請輸入最大連接數',
please_input_max_idle_time: '請輸入最大空閑(秒)',
please_input_acquire_increment: '請輸入增長數',
please_input_query_timeout: '請輸入查詢超時',
please_input_query_timeout: '請輸入查詢超時,不小于零',
please_input_connect_timeout: '請輸輸入連接超時(秒)',
no_less_then_0: '高級設置中的參數不能小於零',
port_no_less_then_0: '端口不能小於零',

View File

@ -1937,7 +1937,7 @@ export default {
please_input_max_pool_size: '请输入最大连接数',
please_input_max_idle_time: '请输入最大空闲(秒)',
please_input_acquire_increment: '请输入增长数',
please_input_query_timeout: '请输入查询超时',
please_input_query_timeout: '请输入查询超时,不小于零',
please_input_connect_timeout: '请输入连接超时(秒)',
no_less_then_0: '高级设置中的参数不能小于零',
port_no_less_then_0: '端口不能小于零',

View File

@ -999,8 +999,7 @@ export default {
{
required: true,
validator: this.isNumber,
message: i18n.t('datasource.please_input_query_timeout'),
trigger: 'blur'
trigger: ['blur', 'change']
}
],
dataPath: [
@ -1161,6 +1160,7 @@ export default {
},
isNumber(rule, value, callback) {
console.log(value)
console.log(!value)
if (!value) {
callback(new Error(i18n.t('datasource.please_input_query_timeout')))
return
@ -1168,8 +1168,9 @@ export default {
let isNumber = false
var reg = /^\d+$/;
isNumber = reg.test(value);
console.log(!isNumber)
if (!isNumber) {
callback(new Error(i18n.t('chart.value_error')))
callback(new Error(i18n.t('datasource.please_input_query_timeout')))
return
}
callback()