mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-11 12:07:42 +00:00
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
package com.tencent.supersonic.common.util;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* * tools functions to file
|
||||
*/
|
||||
public class FileUtils {
|
||||
|
||||
public static Boolean exit(String path) {
|
||||
File file = new File(path);
|
||||
return file.exists();
|
||||
}
|
||||
|
||||
public static long getLastModified(String path) {
|
||||
if (!exit(path)) {
|
||||
return -1;
|
||||
}
|
||||
File file = new File(path);
|
||||
Optional<Long> lastModified = Arrays.stream(file.listFiles()).filter(f -> f.isFile())
|
||||
.map(f -> f.lastModified()).sorted(Collections.reverseOrder()).findFirst();
|
||||
|
||||
if (lastModified.isPresent()) {
|
||||
return lastModified.get();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static File[] getDirFiles(File file) {
|
||||
if (file.isDirectory()) {
|
||||
return file.listFiles();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void scanDirectory(File file, int maxLevel, Map<Integer, List<File>> directories) {
|
||||
if (maxLevel < 0) {
|
||||
return;
|
||||
}
|
||||
if (!file.exists() || !file.isDirectory()) {
|
||||
return;
|
||||
}
|
||||
if (!directories.containsKey(maxLevel)) {
|
||||
directories.put(maxLevel, new ArrayList<>());
|
||||
}
|
||||
for (File f : file.listFiles()) {
|
||||
if (f.isDirectory()) {
|
||||
directories.get(maxLevel).add(f);
|
||||
scanDirectory(f, maxLevel - 1, directories);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<String, Map<String, List<String>>> getTop3Directory(String path) {
|
||||
Map<String, Map<String, List<String>>> result = new HashMap<>();
|
||||
File file = new File(path);
|
||||
if (!file.exists() || !file.isDirectory()) {
|
||||
return result;
|
||||
}
|
||||
Map<Integer, List<File>> directories = new HashMap<>();
|
||||
scanDirectory(file, 2, directories);
|
||||
for (int i = 2; i >= 0; i--) {
|
||||
for (File f : directories.getOrDefault(i, new ArrayList<>())) {
|
||||
if (i == 2) {
|
||||
result.put(f.getName(), new HashMap<>());
|
||||
continue;
|
||||
}
|
||||
if (i == 1 && result.containsKey(f.getParentFile().getName())) {
|
||||
result.get(f.getParentFile().getName()).put(f.getName(), new ArrayList<>());
|
||||
continue;
|
||||
}
|
||||
String parent = f.getParentFile().getParentFile().getName();
|
||||
if (result.containsKey(parent) && result.get(parent).containsKey(f.getParentFile().getName())) {
|
||||
result.get(parent).get(f.getParentFile().getName()).add(f.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,14 @@
|
||||
package com.tencent.supersonic.common.util.jsqlparser;
|
||||
|
||||
import com.tencent.supersonic.common.util.StringUtil;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.jsqlparser.JSQLParserException;
|
||||
import net.sf.jsqlparser.expression.Alias;
|
||||
@@ -28,6 +36,7 @@ import net.sf.jsqlparser.schema.Table;
|
||||
import net.sf.jsqlparser.statement.Statement;
|
||||
import net.sf.jsqlparser.statement.select.Distinct;
|
||||
import net.sf.jsqlparser.statement.select.GroupByElement;
|
||||
import net.sf.jsqlparser.statement.select.Join;
|
||||
import net.sf.jsqlparser.statement.select.LateralView;
|
||||
import net.sf.jsqlparser.statement.select.OrderByElement;
|
||||
import net.sf.jsqlparser.statement.select.ParenthesedSelect;
|
||||
@@ -36,16 +45,10 @@ import net.sf.jsqlparser.statement.select.Select;
|
||||
import net.sf.jsqlparser.statement.select.SelectItem;
|
||||
import net.sf.jsqlparser.statement.select.SelectVisitorAdapter;
|
||||
import net.sf.jsqlparser.statement.select.SetOperationList;
|
||||
import net.sf.jsqlparser.statement.select.WithItem;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Sql Parser Select Helper
|
||||
*/
|
||||
@@ -608,5 +611,52 @@ public class SqlSelectHelper {
|
||||
getColumnFromExpr(expr.getExpression(), columns);
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<String, Set<String>> getFieldsWithSubQuery(String sql) {
|
||||
List<PlainSelect> plainSelects = getPlainSelects(getPlainSelect(sql));
|
||||
Map<String, Set<String>> results = new HashMap<>();
|
||||
for (PlainSelect plainSelect : plainSelects) {
|
||||
getFieldsWithSubQuery(plainSelect, results);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private static void getFieldsWithSubQuery(PlainSelect plainSelect, Map<String, Set<String>> fields) {
|
||||
if (plainSelect.getFromItem() instanceof Table) {
|
||||
boolean isWith = false;
|
||||
if (!CollectionUtils.isEmpty(plainSelect.getWithItemsList())) {
|
||||
for (WithItem withItem : plainSelect.getWithItemsList()) {
|
||||
if (Objects.nonNull(withItem.getSelect())) {
|
||||
getFieldsWithSubQuery(withItem.getSelect().getPlainSelect(), fields);
|
||||
isWith = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isWith) {
|
||||
Table table = (Table) plainSelect.getFromItem();
|
||||
if (!fields.containsKey(table.getFullyQualifiedName())) {
|
||||
fields.put(table.getFullyQualifiedName(), new HashSet<>());
|
||||
}
|
||||
List<String> sqlFields = getFieldsByPlainSelect(plainSelect).stream().map(f -> f.replaceAll("`", ""))
|
||||
.collect(
|
||||
Collectors.toList());
|
||||
fields.get(table.getFullyQualifiedName()).addAll(sqlFields);
|
||||
}
|
||||
}
|
||||
if (plainSelect.getFromItem() instanceof ParenthesedSelect) {
|
||||
ParenthesedSelect parenthesedSelect = (ParenthesedSelect) plainSelect.getFromItem();
|
||||
getFieldsWithSubQuery(parenthesedSelect.getPlainSelect(), fields);
|
||||
if (!CollectionUtils.isEmpty(plainSelect.getJoins())) {
|
||||
for (Join join : plainSelect.getJoins()) {
|
||||
if (join.getRightItem() instanceof ParenthesedSelect) {
|
||||
getFieldsWithSubQuery(((ParenthesedSelect) join.getRightItem()).getPlainSelect(), fields);
|
||||
}
|
||||
if (join.getFromItem() instanceof ParenthesedSelect) {
|
||||
getFieldsWithSubQuery(((ParenthesedSelect) join.getFromItem()).getPlainSelect(), fields);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user