use of net.sf.jsqlparser.expression.Expression in project dbeaver by serge-rider.
the class SQLSemanticProcessor method getOrderConstraintExpression.
private static Expression getOrderConstraintExpression(DBRProgressMonitor monitor, DBPDataSource dataSource, PlainSelect select, DBDAttributeConstraint co, boolean forceNumeric) throws JSQLParserException, DBException {
Expression orderExpr;
String attrName = DBUtils.getQuotedIdentifier(dataSource, co.getAttributeName());
if (forceNumeric || attrName.isEmpty()) {
orderExpr = new LongValue(co.getOrderPosition());
} else if (CommonUtils.isJavaIdentifier(attrName)) {
// Use column table only if there are multiple source tables (joins)
Table orderTable = CommonUtils.isEmpty(select.getJoins()) ? null : getConstraintTable(select, co);
if (!isValidTableColumn(monitor, dataSource, orderTable, co)) {
orderTable = null;
}
orderExpr = new Column(orderTable, attrName);
} else {
// TODO: set tableAlias for all column references in expression
orderExpr = CCJSqlParserUtil.parseExpression(attrName);
// orderExpr = new CustomExpression(attrName);
// orderExpr = new LongValue(co.getAttribute().getOrdinalPosition() + 1);
}
return orderExpr;
}
use of net.sf.jsqlparser.expression.Expression 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);
}
}
Aggregations