use of com.alibaba.druid.sql.ast.statement.SQLSelectStatement in project druid by alibaba.
the class SQLUtils method addSelectItem.
public static void addSelectItem(SQLStatement stmt, SQLExpr expr, String alias, boolean first) {
if (expr == null) {
return;
}
if (stmt instanceof SQLSelectStatement) {
SQLSelectQuery query = ((SQLSelectStatement) stmt).getSelect().getQuery();
if (query instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
addSelectItem(queryBlock, expr, alias, first);
} else {
throw new IllegalArgumentException("add condition not support " + stmt.getClass().getName());
}
return;
}
throw new IllegalArgumentException("add selectItem not support " + stmt.getClass().getName());
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectStatement in project druid by alibaba.
the class PagerUtils method count.
public static String count(String sql, String dbType) {
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
if (stmtList.size() != 1) {
throw new IllegalArgumentException("sql not support count : " + sql);
}
SQLStatement stmt = stmtList.get(0);
if (!(stmt instanceof SQLSelectStatement)) {
throw new IllegalArgumentException("sql not support count : " + sql);
}
SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
return count(selectStmt.getSelect(), dbType);
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectStatement in project druid by alibaba.
the class PagerUtils method createCountUseSubQuery.
private static String createCountUseSubQuery(SQLSelect select, String dbType) {
SQLSelectQueryBlock countSelectQuery = createQueryBlock(dbType);
SQLSelectItem countItem = createCountItem(dbType);
countSelectQuery.getSelectList().add(countItem);
SQLSubqueryTableSource fromSubquery = new SQLSubqueryTableSource(select);
fromSubquery.setAlias("ALIAS_COUNT");
countSelectQuery.setFrom(fromSubquery);
SQLSelect countSelect = new SQLSelect(countSelectQuery);
SQLSelectStatement countStmt = new SQLSelectStatement(countSelect);
return SQLUtils.toSQLString(countStmt, dbType);
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectStatement in project druid by alibaba.
the class PagerUtils method limit.
public static String limit(String sql, String dbType, int offset, int count) {
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
if (stmtList.size() != 1) {
throw new IllegalArgumentException("sql not support count : " + sql);
}
SQLStatement stmt = stmtList.get(0);
if (!(stmt instanceof SQLSelectStatement)) {
throw new IllegalArgumentException("sql not support count : " + sql);
}
SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
return limit(selectStmt.getSelect(), dbType, offset, count);
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectStatement in project druid by alibaba.
the class SQLUtils method addCondition.
public static void addCondition(SQLStatement stmt, SQLBinaryOperator op, SQLExpr condition, boolean left) {
if (stmt instanceof SQLSelectStatement) {
SQLSelectQuery query = ((SQLSelectStatement) stmt).getSelect().getQuery();
if (query instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
SQLExpr newCondition = buildCondition(op, condition, left, queryBlock.getWhere());
queryBlock.setWhere(newCondition);
} else {
throw new IllegalArgumentException("add condition not support " + stmt.getClass().getName());
}
return;
}
if (stmt instanceof SQLDeleteStatement) {
SQLDeleteStatement delete = (SQLDeleteStatement) stmt;
SQLExpr newCondition = buildCondition(op, condition, left, delete.getWhere());
delete.setWhere(newCondition);
return;
}
if (stmt instanceof SQLUpdateStatement) {
SQLUpdateStatement update = (SQLUpdateStatement) stmt;
SQLExpr newCondition = buildCondition(op, condition, left, update.getWhere());
update.setWhere(newCondition);
return;
}
throw new IllegalArgumentException("add condition not support " + stmt.getClass().getName());
}
Aggregations