Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.apache.ibatis.jdbc.SelectBuilder Maven / Gradle / Ivy
package org.apache.ibatis.jdbc;
import java.util.ArrayList;
import java.util.List;
public class SelectBuilder {
private static final String AND = ") \nAND (";
private static final String OR = ") \nOR (";
private static final ThreadLocal localSQL = new ThreadLocal();
public static void BEGIN() {
RESET();
}
public static void RESET() {
localSQL.set(new SelectSQL());
}
public static void SELECT(String columns) {
sql().select.add(columns);
}
public static void FROM(String table) {
sql().from.add(table);
}
public static void JOIN(String join) {
sql().join.add(join);
}
public static void INNER_JOIN(String join) {
sql().innerJoin.add(join);
}
public static void LEFT_OUTER_JOIN(String join) {
sql().leftOuterJoin.add(join);
}
public static void RIGHT_OUTER_JOIN(String join) {
sql().rightOuterJoin.add(join);
}
public static void OUTER_JOIN(String join) {
sql().outerJoin.add(join);
}
public static void WHERE(String conditions) {
sql().where.add(conditions);
sql().lastList = sql().where;
}
public static void OR() {
sql().lastList.add(OR);
}
public static void AND() {
sql().lastList.add(AND);
}
public static void GROUP_BY(String columns) {
sql().groupBy.add(columns);
}
public static void HAVING(String conditions) {
sql().having.add(conditions);
sql().lastList = sql().having;
}
public static void ORDER_BY(String columns) {
sql().orderBy.add(columns);
}
public static String SQL() {
try {
StringBuilder builder = new StringBuilder();
selectClause(builder, "SELECT", sql().select, "", "", ", ");
selectClause(builder, "FROM", sql().from, "", "", ", ");
selectClause(builder, "JOIN", sql().join, "", "", "JOIN");
selectClause(builder, "INNER JOIN", sql().innerJoin, "", "", "\nINNER JOIN ");
selectClause(builder, "OUTER JOIN", sql().outerJoin, "", "", "\nOUTER JOIN ");
selectClause(builder, "LEFT OUTER JOIN", sql().leftOuterJoin, "", "", "\nLEFT OUTER JOIN ");
selectClause(builder, "RIGHT OUTER JOIN", sql().rightOuterJoin, "", "", "\nRIGHT OUTER JOIN ");
selectClause(builder, "WHERE", sql().where, "(", ")", " AND ");
selectClause(builder, "GROUP BY", sql().groupBy, "", "", ", ");
selectClause(builder, "HAVING", sql().having, "(", ")", " AND ");
selectClause(builder, "ORDER BY", sql().orderBy, "", "", ", ");
return builder.toString();
} finally {
RESET();
}
}
private static void selectClause(StringBuilder builder, String keyword, List parts, String open, String close, String conjunction) {
if (!parts.isEmpty()) {
if (builder.length() > 0) builder.append("\n");
builder.append(keyword);
builder.append(" ");
builder.append(open);
String last = "________";
for (int i = 0, n = parts.size(); i < n; i++) {
String part = parts.get(i);
if (i > 0 && !part.equals(AND) && !part.equals(OR) && !last.equals(AND) && !last.equals(OR)) {
builder.append(conjunction);
}
builder.append(part);
last = part;
}
builder.append(close);
}
}
private static SelectSQL sql() {
SelectSQL selectSQL = localSQL.get();
if (selectSQL == null) {
RESET();
selectSQL = localSQL.get();
}
return selectSQL;
}
private static class SelectSQL {
List select = new ArrayList();
List from = new ArrayList();
List join = new ArrayList();
List innerJoin = new ArrayList();
List outerJoin = new ArrayList();
List leftOuterJoin = new ArrayList();
List rightOuterJoin = new ArrayList();
List where = new ArrayList();
List having = new ArrayList();
List groupBy = new ArrayList();
List orderBy = new ArrayList();
List lastList = new ArrayList();
}
}