use of net.sf.jsqlparser.expression.Function in project dbeaver by serge-rider.
the class SQLQueryTransformerCount method tryInjectCount.
private SQLQuery tryInjectCount(DBPDataSource dataSource, SQLQuery query) throws DBException {
try {
Statement statement = CCJSqlParserUtil.parse(query.getText());
if (statement instanceof Select && ((Select) statement).getSelectBody() instanceof PlainSelect) {
PlainSelect select = (PlainSelect) ((Select) statement).getSelectBody();
if (select.getHaving() != null) {
throw new DBException("Can't inject COUNT into query with HAVING clause");
}
if (select.getGroupBy() != null && !CommonUtils.isEmpty(select.getGroupBy().getGroupByExpressions())) {
throw new DBException("Can't inject COUNT into query with GROUP BY clause");
}
Distinct selectDistinct = select.getDistinct();
if (selectDistinct != null) {
// Remove distinct
select.setDistinct(null);
}
Function countFunc = new Function();
countFunc.setName("count");
if (selectDistinct != null) {
countFunc.setDistinct(true);
List<Expression> exprs = new ArrayList<>();
for (SelectItem item : select.getSelectItems()) {
if (item instanceof SelectExpressionItem) {
exprs.add(((SelectExpressionItem) item).getExpression());
}
}
if (!exprs.isEmpty()) {
countFunc.setParameters(new ExpressionList(exprs));
}
}
countFunc.setAllColumns(true);
List<SelectItem> selectItems = new ArrayList<>();
selectItems.add(new SelectExpressionItem(countFunc));
select.setSelectItems(selectItems);
select.setOrderByElements(null);
return new SQLQuery(dataSource, select.toString(), query, false);
} else {
throw new DBException("Query [" + query.getText() + "] can't be modified");
}
} catch (JSQLParserException e) {
throw new DBException("Can't transform query to SELECT count(*)", e);
}
}
use of net.sf.jsqlparser.expression.Function in project dbeaver by dbeaver.
the class SQLQueryTransformerCount method tryInjectCount.
private SQLQuery tryInjectCount(DBPDataSource dataSource, SQLQuery query) throws DBException {
try {
Statement statement = CCJSqlParserUtil.parse(query.getText());
if (statement instanceof Select && ((Select) statement).getSelectBody() instanceof PlainSelect) {
PlainSelect select = (PlainSelect) ((Select) statement).getSelectBody();
if (select.getHaving() != null) {
throw new DBException("Can't inject COUNT into query with HAVING clause");
}
if (select.getGroupBy() != null && !CommonUtils.isEmpty(select.getGroupBy().getGroupByExpressions())) {
throw new DBException("Can't inject COUNT into query with GROUP BY clause");
}
Distinct selectDistinct = select.getDistinct();
if (selectDistinct != null) {
// Remove distinct
select.setDistinct(null);
}
Function countFunc = new Function();
countFunc.setName("count");
if (selectDistinct != null) {
countFunc.setDistinct(true);
List<Expression> exprs = new ArrayList<>();
for (SelectItem item : select.getSelectItems()) {
if (item instanceof SelectExpressionItem) {
exprs.add(((SelectExpressionItem) item).getExpression());
}
}
if (!exprs.isEmpty()) {
countFunc.setParameters(new ExpressionList(exprs));
}
}
countFunc.setAllColumns(true);
List<SelectItem> selectItems = new ArrayList<>();
selectItems.add(new SelectExpressionItem(countFunc));
select.setSelectItems(selectItems);
select.setOrderByElements(null);
return new SQLQuery(dataSource, select.toString(), query, false);
} else {
throw new DBException("Query [" + query.getText() + "] can't be modified");
}
} catch (JSQLParserException e) {
throw new DBException("Can't transform query to SELECT count(*)", e);
}
}
use of net.sf.jsqlparser.expression.Function in project herddb by diennea.
the class SQLParserExpressionCompiler method getAggregateFunctionType.
public static int getAggregateFunctionType(Expression exp, OpSchema tableSchema) {
if (exp instanceof net.sf.jsqlparser.expression.CastExpression) {
// SELECT CAST(avg(n) as DOUBLE)
net.sf.jsqlparser.expression.CastExpression c = (net.sf.jsqlparser.expression.CastExpression) exp;
return JSQLParserPlanner.sqlDataTypeToColumnType(c.getType());
}
Function fn = (Function) exp;
String functionNameLowercase = fn.getName().toLowerCase();
switch(functionNameLowercase) {
case COUNT:
return ColumnTypes.LONG;
case SUM:
case SUM0:
case AVG:
case MIN:
case MAX:
checkSupported(fn.getParameters().getExpressions().size() == 1);
final Expression first = fn.getParameters().getExpressions().get(0);
if (first instanceof net.sf.jsqlparser.expression.CastExpression) {
// SELECT AVG(CAST(n) as DOUBLE))
net.sf.jsqlparser.expression.CastExpression c = (net.sf.jsqlparser.expression.CastExpression) first;
return JSQLParserPlanner.sqlDataTypeToColumnType(c.getType());
}
checkSupported(first instanceof net.sf.jsqlparser.schema.Column, first.getClass());
net.sf.jsqlparser.schema.Column cName = (net.sf.jsqlparser.schema.Column) first;
String tableAlias = extractTableName(cName);
ColumnRef col = findColumnInSchema(tableAlias, fixMySqlBackTicks(cName.getColumnName()), tableSchema, new IntHolder());
checkSupported(col != null);
// SUM of INTEGERS is an INTEGER (this is what Calcite does, but it is smarter than this)
return col.type;
default:
throw new HerdDBInternalException(functionNameLowercase);
}
}
Aggregations