Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge from edp963/davinci dev-0.3 #3

Merged
merged 16 commits into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: get count and execute sql add 'with fragment' support
  • Loading branch information
RichardShan committed Dec 3, 2019
commit 31aa0748e5e1258936b24364b4093b797112fb75
2 changes: 1 addition & 1 deletion server/src/main/java/edp/core/consts/Consts.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public class Consts {
public static final String REG_SENSITIVE_SQL = "drop\\s|alter\\s|grant\\s|insert\\s|replace\\s|delete\\s|truncate\\s|update\\s|remove\\s";
public static final Pattern PATTERN_SENSITIVE_SQL = Pattern.compile(REG_SENSITIVE_SQL);

private static final String REG_WITH_SQL_FRAGMENT = "((?i)WITH[\\s\\S]+(?i)AS\\s*\\([\\s\\S]+\\))\\s*(?i)SELECT";
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);

/**
Expand Down
7 changes: 5 additions & 2 deletions server/src/main/java/edp/core/utils/SqlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import edp.core.model.*;
import edp.davinci.core.enums.LogNameEnum;
import edp.davinci.core.enums.SqlColumnEnum;
import edp.davinci.core.utils.SqlParseUtils;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Alias;
Expand Down Expand Up @@ -296,14 +297,15 @@ private Map<String, Object> getResultObjectMap(Set<String> excludeColumns, Resul
}

public static String getCountSql(String sql) {
String countSql = String.format(Consts.QUERY_COUNT_SQL, sql);
try {
Select select = (Select) CCJSqlParserUtil.parse(sql);
PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
plainSelect.setOrderByElements(null);
return String.format(QUERY_COUNT_SQL, select.toString());
countSql = String.format(QUERY_COUNT_SQL, select.toString());
} catch (JSQLParserException e) {
}
return String.format(Consts.QUERY_COUNT_SQL, sql);
return SqlParseUtils.rebuildSqlWithFragment(countSql);
}


Expand All @@ -328,6 +330,7 @@ public static Set<String> getQueryFromsAndJoins(String sql) {
columnPrefixExtractor(columnPrefixs, plainSelect);
}
} catch (JSQLParserException e) {
log.warn("Get table name or alias Error: {}", e.getCause().getMessage());
}
return columnPrefixs;
}
Expand Down
22 changes: 12 additions & 10 deletions server/src/main/java/edp/davinci/core/utils/SqlParseUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.stringtemplate.v4.ST;

Expand All @@ -61,6 +62,7 @@ public class SqlParseUtils {
private static final String QUERY_WHERE_TRUE = "1=1";
private static final String QUERY_WHERE_FALSE = "1=0";

@Autowired
private DacChannelUtil dacChannelUtil;

/**
Expand Down Expand Up @@ -93,7 +95,7 @@ public SqlEntity parseSql(String sqlStr, List<SqlVariable> variables, String sql

// 解析参数
if (!CollectionUtils.isEmpty(variables)) {
ExecutorService executorService = Executors.newFixedThreadPool(8);
ExecutorService executorService = Executors.newFixedThreadPool(4);
try {
CountDownLatch countDownLatch = new CountDownLatch(variables.size());
List<Future> futures = new ArrayList<>(variables.size());
Expand Down Expand Up @@ -140,7 +142,6 @@ public SqlEntity parseSql(String sqlStr, List<SqlVariable> variables, String sql


public List<String> getAuthVarValue(SqlVariable variable, String email) {

SqlVariableChannel channel = variable.getChannel();
if (null == channel) {
return SqlVariableValueTypeEnum.getValues(variable.getValueType(), variable.getDefaultValues(),
Expand Down Expand Up @@ -241,7 +242,6 @@ public List<String> getSqls(String sql, boolean isQuery) {
if (split.length > 0) {
list = new ArrayList<>();
for (String sqlStr : split) {
sqlStr = rebuildSqlWithFragment(sqlStr.trim());
boolean select = sqlStr.toLowerCase().startsWith(SELECT) || sqlStr.toLowerCase().startsWith(WITH);
if (isQuery) {
if (select) {
Expand All @@ -257,18 +257,20 @@ public List<String> getSqls(String sql, boolean isQuery) {
return list;
}

private static String rebuildSqlWithFragment(String sql) {
public 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 (withFragment.length() > 6) {
int lastSelectIndex = withFragment.length() - 6;
sql = sql.replace(withFragment, withFragment.substring(lastSelectIndex));
withFragment = withFragment.substring(0, lastSelectIndex);
if (!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 + SPACE + sql;
sql = sql.replaceAll(SPACE + "{2,}", SPACE);
}
sql = withFragment + SPACE + sql;
sql = sql.replaceAll(SPACE + "{2,}", SPACE);
}
}
return sql;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import edp.core.utils.CollectionUtils;
import edp.core.utils.SqlUtils;
import edp.davinci.core.enums.ActionEnum;
import edp.davinci.core.utils.SqlParseUtils;
import edp.davinci.dto.cronJobDto.MsgMailExcel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
Expand Down Expand Up @@ -67,6 +68,7 @@ public T call() throws Exception {
template.setFetchSize(500);

String sql = context.getQuerySql().get(context.getQuerySql().size() - 1);
sql = SqlParseUtils.rebuildSqlWithFragment(sql);
Set<String> queryFromsAndJoins = SqlUtils.getQueryFromsAndJoins(sql);
if (context.getCustomLogger() != null) {
context.getCustomLogger().info("Task ({}) -- {} start query", context.getTaskKey(), context.getName());
Expand Down Expand Up @@ -138,6 +140,7 @@ private void buildQueryColumn(JdbcTemplate template) {
template.setMaxRows(1);
String sql = context.getQuerySql().get(context.getQuerySql().size() - 1);
sql = String.format(QUERY_META_SQL, sql);
sql = SqlParseUtils.rebuildSqlWithFragment(sql);
Set<String> queryFromsAndJoins = SqlUtils.getQueryFromsAndJoins(sql);
template.query(sql, rs -> {
ResultSetMetaData metaData = rs.getMetaData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ public PaginateWithQueryColumns executeSql(ViewExecuteSql executeSql, User user)
}
if (!CollectionUtils.isEmpty(querySqlList)) {
for (String sql : querySqlList) {
sql = SqlParseUtils.rebuildSqlWithFragment(sql);
paginateWithQueryColumns = sqlUtils.syncQuery4Paginate(sql, null, null, null, executeSql.getLimit(), null);
}
}
Expand Down Expand Up @@ -618,7 +619,7 @@ public PaginateWithQueryColumns getResultDataList(boolean isMaintainer, ViewWith

for (String sql : querySqlList) {
paginate = sqlUtils.syncQuery4Paginate(
sql,
SqlParseUtils.rebuildSqlWithFragment(sql),
executeParam.getPageNo(),
executeParam.getPageSize(),
executeParam.getTotalCount(),
Expand Down Expand Up @@ -717,7 +718,7 @@ public List<Map<String, Object>> getDistinctValueData(boolean isMaintainer, View
}
List<Map<String, Object>> list = null;
for (String sql : querySqlList) {
list = sqlUtils.query4List(sql, -1);
list = sqlUtils.query4List(SqlParseUtils.rebuildSqlWithFragment(sql), -1);
}

if (null != param.getCache() && param.getCache() && param.getExpired().longValue() > 0L) {
Expand Down