gu.sql2java.SqlFormatters Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sql2java-manager Show documentation
Show all versions of sql2java-manager Show documentation
sql2java manager class package for accessing database
package gu.sql2java;
import static com.google.common.base.Strings.isNullOrEmpty;
import static com.google.common.base.Preconditions.checkArgument;
import java.util.Map;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap;
import gu.sql2java.mysql.MysqlSqlFormatter;
import gu.sql2java.phoenix.PhonenixSqlFormatter;
/**
* @author guyadong
* @since 3.14.0
*/
public class SqlFormatters implements Constant{
private static Map sqlFormatters =
ImmutableMap.of(
PRODUCT_NAME_PHOENIX,new PhonenixSqlFormatter(),
PRODUCT_NAME_MYSQL,new MysqlSqlFormatter());
private static SqlFormatter DEFAULT_INSTANCE= new SqlFormatterAdapter();
public static SqlFormatter formatterOf(String engine){
return sqlFormatters.getOrDefault(engine, DEFAULT_INSTANCE);
}
public static class SqlFormatterAdapter implements SqlFormatter {
@Override
public String tablename(String tablename) {
return tablename;
}
@Override
public String columname(String columname) {
return columname;
}
@Override
public String alias(String alias) {
return alias;
}
}
public static class QuoteFormatter extends SqlFormatterAdapter{
private final String quote;
public QuoteFormatter(String quote) {
super();
checkArgument(!isNullOrEmpty(quote),"quote is null or empty");
this.quote = quote;
}
/**
* 如果输入字符串{@code input}以{@code quote}开头和结尾返回{@code true},否则返回{@code false}
* @param input
*/
protected boolean isQuoteWith(String input){
if(null != input){
return input.startsWith(quote) && input.endsWith(quote);
}
return false;
}
protected String quoteIf(String input, Predicate and) {
if(null != input && !isQuoteWith(input) && and.apply(input)){
return quote + input + quote;
}
return input;
}
@Override
public String alias(String alias) {
return columname(alias);
}
}
}