com.github.mengxianun.core.ActionUtil Maven / Gradle / Ivy
package com.github.mengxianun.core;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.text.RandomStringGenerator;
import com.github.mengxianun.core.item.ColumnItem;
import com.github.mengxianun.core.item.TableItem;
import com.github.mengxianun.core.schema.Column;
import com.github.mengxianun.core.schema.Table;
import com.google.common.base.Strings;
public class ActionUtil {
private ActionUtil() {
throw new IllegalStateException("Utility class");
}
public static TableItem createTableItem(Table table) {
return new TableItem(table, createTableAlias(table, true), false);
}
public static TableItem createTableItem(Table table, boolean isAlias) {
return new TableItem(table, createTableAlias(table, isAlias), false);
}
public static TableItem createTableItem(Table table, String alias) {
return new TableItem(table, alias, true);
}
public static List createColumnItems(TableItem tableItem, boolean isAlias) {
List columnItems = new ArrayList<>();
Table table = tableItem.getTable();
String expression = tableItem.getExpression();
if (table != null) {
List columns = table.getColumns();
for (Column column : columns) {
columnItems.add(new ColumnItem(column, isAlias ? createRandomAlias() : null, false, tableItem));
}
} else if (!Strings.isNullOrEmpty(expression)) {
columnItems.add(new ColumnItem(SQLBuilder.COLUMN_ALL));
}
return columnItems;
}
public static String createTableAlias(Table table) {
return createTableAlias(table, true);
}
public static String createTableAlias(Table table, boolean isAlias) {
String alias = null;
if (isAlias) {
alias = createRandomAlias();
if (table != null) {
alias += "_" + table.getName();
}
}
return alias;
}
public static String createRandomAlias() {
return createRandomString(6);
}
public static String createRandomString(int length) {
RandomStringGenerator generator = new RandomStringGenerator.Builder().withinRange('a', 'z').build();
return generator.generate(length);
}
}