Unverified Commit 5a516d25 authored by Licho's avatar Licho Committed by GitHub

refacotr: simpily dinky-executor code (#1526)

* refacotr: simpily dinky-executor code Signed-off-by: 's avatarlicho <lecho.sun@gmail.com> * refactor: spotless:apply * refactor: simpily code Signed-off-by: 's avatarlicho <lecho.sun@gmail.com> Signed-off-by: 's avatarlicho <lecho.sun@gmail.com>
parent 87ba7324
......@@ -252,11 +252,7 @@ public final class SqlManager {
* @return
*/
private boolean isInnerDateVar(String key) {
if (key.startsWith(FlinkSQLConstant.INNER_DATETIME_KEY)) {
return true;
}
return false;
return key.startsWith(FlinkSQLConstant.INNER_DATETIME_KEY);
}
/**
......
......@@ -72,12 +72,12 @@ public abstract class BaseSingleSqlParser {
* 得到解析完毕的Sql语句
**/
public String getParsedSql() {
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
for (SqlSegment sqlSegment : segments) {
sb.append(sqlSegment.getParsedSqlSegment() + "\n");
sb.append(sqlSegment.getParsedSqlSegment()).append("\n");
}
String retval = sb.toString().replaceAll("\n+", "\n");
return retval;
return sb.toString().replaceAll("\n+", "\n");
}
}
......@@ -39,8 +39,6 @@ public class InsertSqlParser extends BaseSingleSqlParser {
}
public String getParsedSql() {
String retval = super.getParsedSql();
retval = retval + ")";
return retval;
return super.getParsedSql() + ")";
}
}
......@@ -24,6 +24,9 @@ import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* SingleSqlParserFactory
*
......@@ -32,9 +35,10 @@ import java.util.regex.Pattern;
*/
public class SingleSqlParserFactory {
protected static final Logger logger = LoggerFactory.getLogger(SingleSqlParserFactory.class);
public static Map<String, List<String>> generateParser(String sql) {
BaseSingleSqlParser tmp = null;
// sql = sql.replace("\n"," ").replaceAll("\\s{1,}", " ") +" ENDOFSQL";
sql = sql.replace("\r\n", " ").replace("\n", " ") + " ENDOFSQL";
if (contains(sql, "(insert\\s+into)(.+)(select)(.+)(from)(.+)")) {
tmp = new InsertSelectSqlParser(sql);
......@@ -50,15 +54,17 @@ public class SingleSqlParserFactory {
tmp = new UpdateSqlParser(sql);
} else if (contains(sql, "(insert\\s+into)(.+)(values)(.+)")) {
tmp = new InsertSqlParser(sql);
// } else if (contains(sql, "(create\\s+table)(.+)")) {
// } else if (contains(sql, "(create\\s+database)(.+)")) {
// } else if (contains(sql, "(show\\s+databases)")) {
// } else if (contains(sql, "(use)(.+)")) {
} else if (contains(sql, "(set)(.+)")) {
tmp = new SetSqlParser(sql);
} else if (contains(sql, "(show\\s+fragment)\\s+(.+)")) {
tmp = new ShowFragmentParser(sql);
}
if (tmp == null) {
logger.error("sql: {} illegal.", sql);
return null;
}
return tmp.splitSql2Segment();
}
......
......@@ -26,6 +26,8 @@ import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.common.collect.Lists;
/**
* SqlSegment
*
......@@ -73,13 +75,7 @@ public class SqlSegment {
* @param bodySplitPattern 用于分割body的正则表达式
**/
public SqlSegment(String segmentRegExp, String bodySplitPattern) {
this.type = "";
this.start = "";
this.body = "";
this.end = "";
this.segmentRegExp = segmentRegExp;
this.bodySplitPattern = bodySplitPattern;
this.bodyPieces = new ArrayList<String>();
this("", segmentRegExp, bodySplitPattern);
}
public SqlSegment(String type, String segmentRegExp, String bodySplitPattern) {
......@@ -103,7 +99,7 @@ public class SqlSegment {
body = matcher.group(2);
end = matcher.group(3);
if (Asserts.isNullString(type)) {
type = start.replace("\n", " ").replaceAll("\\s{1,}", " ").toUpperCase();
type = start.replace("\n", " ").replaceAll("\\s+", " ").toUpperCase();
}
parseBody();
}
......@@ -113,34 +109,25 @@ public class SqlSegment {
* 解析body部分
**/
private void parseBody() {
List<String> ls = new ArrayList<String>();
Pattern p = Pattern.compile(bodySplitPattern, Pattern.CASE_INSENSITIVE);
body = body.trim();
Matcher m = p.matcher(body);
Matcher m = p.matcher(body.trim());
StringBuffer sb = new StringBuffer();
boolean result = m.find();
while (result) {
while (m.find()) {
m.appendReplacement(sb, Crlf);
result = m.find();
}
m.appendTail(sb);
// ls.add(start);
String[] arr = sb.toString().split("[|]");
int arrLength = arr.length;
for (int i = 0; i < arrLength; i++) {
ls.add(arr[i]);
}
bodyPieces = ls;
bodyPieces = Lists.newArrayList(arr);
}
/**
* 取得解析好的Sql片段
**/
public String getParsedSqlSegment() {
StringBuffer sb = new StringBuffer();
sb.append(start + Crlf);
StringBuilder sb = new StringBuilder();
sb.append(start).append(Crlf);
for (String piece : bodyPieces) {
sb.append(piece + Crlf);
sb.append(piece).append(Crlf);
}
return sb.toString();
}
......
......@@ -19,9 +19,7 @@
package org.dinky.parser.check;
import org.dinky.process.context.ProcessContextHolder;
import org.dinky.process.exception.DinkyException;
import org.dinky.process.model.ProcessEntity;
import java.io.File;
import java.util.HashSet;
......@@ -51,7 +49,6 @@ public class AddJarSqlParser {
public static Set<File> getAllFilePath(String[] statements) {
Set<File> fileSet = new HashSet<>();
ProcessEntity process = ProcessContextHolder.getProcess();
patternStatements(statements).stream().map(x -> ReUtil.findAll(ADD_JAR_PATTERN, x, 2).get(0))
.distinct().forEach(path -> {
if (!FileUtil.exist(path)) {
......
......@@ -19,11 +19,6 @@
package org.dinky.trans;
import org.dinky.executor.CustomTableEnvironmentImpl;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -54,16 +49,6 @@ public class AbstractOperation {
this.statement = statement;
}
public boolean checkFunctionExist(CustomTableEnvironmentImpl stEnvironment, String key) {
String[] udfs = stEnvironment.listUserDefinedFunctions();
List<String> udflist = Arrays.asList(udfs);
if (udflist.contains(key.toLowerCase())) {
return true;
} else {
return false;
}
}
public boolean noExecute() {
return true;
}
......
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.trans;
/**
* TODO
*
* @author wenmo
* @since 2021/6/13 19:34
*/
public interface CreateOperation extends Operation {
// void create(CustomTableEnvironmentImpl stEnvironment);
}
......@@ -39,17 +39,25 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Operations {
public static final String SQL_EMPTY_STR = "[\\s\\t\\n\\r]";
private Operations() {
}
private static final Operation[] ALL_OPERATIONS = getAllOperations();
/**
* get all {@link Operation} children ordinary class,
*
* @return all operation class define in project.
*/
private static Operation[] getAllOperations() {
Reflections reflections = new Reflections(Operation.class.getPackage().getName());
Set<Class<?>> operations = reflections.get(Scanners.SubTypes.of(Operation.class).asClass());
return operations.stream().filter(t -> !t.isInterface()).map(t -> {
try {
return t.getConstructor().newInstance();
return (Operation) t.getConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException
| NoSuchMethodException e) {
log.error(String.format("getAllOperations error, class %s, err: %s", t, e));
......@@ -58,31 +66,12 @@ public class Operations {
}).toArray(Operation[]::new);
}
public static SqlType getSqlTypeFromStatements(String statement) {
String[] statements = statement.split(";");
SqlType sqlType = SqlType.UNKNOWN;
for (String item : statements) {
if (item.trim().isEmpty()) {
continue;
}
sqlType = Operations.getOperationType(item);
if (sqlType == SqlType.INSERT || sqlType == SqlType.SELECT) {
return sqlType;
}
}
return sqlType;
}
public static SqlType getOperationType(String sql) {
String sqlTrim = sql.replaceAll("[\\s\\t\\n\\r]", "").trim().toUpperCase();
SqlType type = SqlType.UNKNOWN;
for (SqlType sqlType : SqlType.values()) {
if (sqlTrim.startsWith(sqlType.getType())) {
type = sqlType;
break;
}
}
return type;
String sqlTrim = sql.replaceAll(SQL_EMPTY_STR, "").trim().toUpperCase();
return Arrays.stream(SqlType.values())
.filter(sqlType -> sqlTrim.startsWith(sqlType.getType()))
.findFirst()
.orElse(SqlType.UNKNOWN);
}
public static Operation buildOperation(String statement) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment