use of net.sf.jsqlparser.expression.Expression in project dbeaver by serge-rider.
the class SQLQueryTransformerCount method transformQuery.
@Override
public SQLQuery transformQuery(SQLDataSource dataSource, SQLQuery query) throws DBException {
try {
Statement statement = CCJSqlParserUtil.parse(query.getQuery());
if (statement instanceof Select && ((Select) statement).getSelectBody() instanceof PlainSelect) {
PlainSelect select = (PlainSelect) ((Select) statement).getSelectBody();
List<SelectItem> selectItems = new ArrayList<>();
Function countFunc = new Function();
countFunc.setName("count");
countFunc.setParameters(new ExpressionList(Collections.<Expression>singletonList(new Column("*"))));
SelectItem countItem = new SelectExpressionItem(countFunc);
selectItems.add(countItem);
select.setSelectItems(selectItems);
return new SQLQuery(dataSource, select.toString(), query, false);
} else {
throw new DBException("Query [" + query.getQuery() + "] can't be modified");
}
} catch (JSQLParserException e) {
throw new DBException("Can't transform query to SELECT count(*)", e);
}
}
use of net.sf.jsqlparser.expression.Expression in project dbeaver by serge-rider.
the class SQLSemanticProcessor method patchSelectQuery.
private static boolean patchSelectQuery(DBPDataSource dataSource, PlainSelect select, DBDDataFilter filter) throws JSQLParserException {
// WHERE
if (filter.hasConditions()) {
for (DBDAttributeConstraint co : filter.getConstraints()) {
if (co.hasCondition()) {
Table table = getConstraintTable(select, co);
if (table != null) {
if (table.getAlias() != null) {
co.setEntityAlias(table.getAlias().getName());
} else {
co.setEntityAlias(table.getName());
}
} else {
co.setEntityAlias(null);
}
}
}
StringBuilder whereString = new StringBuilder();
SQLUtils.appendConditionString(filter, dataSource, null, whereString, true);
String condString = whereString.toString();
addWhereToSelect(select, condString);
}
// ORDER
if (filter.hasOrdering()) {
List<OrderByElement> orderByElements = select.getOrderByElements();
if (orderByElements == null) {
orderByElements = new ArrayList<>();
select.setOrderByElements(orderByElements);
}
for (DBDAttributeConstraint co : filter.getOrderConstraints()) {
Expression orderExpr = getConstraintExpression(select, co);
OrderByElement element = new OrderByElement();
element.setExpression(orderExpr);
if (co.isOrderDescending()) {
element.setAsc(false);
element.setAscDescPresent(true);
}
orderByElements.add(element);
}
}
return true;
}
use of net.sf.jsqlparser.expression.Expression in project dbeaver by dbeaver.
the class SQLSemanticProcessor method patchSelectQuery.
private static boolean patchSelectQuery(DBPDataSource dataSource, PlainSelect select, DBDDataFilter filter) throws JSQLParserException {
// WHERE
if (filter.hasConditions()) {
for (DBDAttributeConstraint co : filter.getConstraints()) {
if (co.hasCondition()) {
Table table = getConstraintTable(select, co);
if (table != null) {
if (table.getAlias() != null) {
co.setEntityAlias(table.getAlias().getName());
} else {
co.setEntityAlias(table.getName());
}
} else {
co.setEntityAlias(null);
}
}
}
StringBuilder whereString = new StringBuilder();
SQLUtils.appendConditionString(filter, dataSource, null, whereString, true);
String condString = whereString.toString();
addWhereToSelect(select, condString);
}
// ORDER
if (filter.hasOrdering()) {
List<OrderByElement> orderByElements = select.getOrderByElements();
if (orderByElements == null) {
orderByElements = new ArrayList<>();
select.setOrderByElements(orderByElements);
}
for (DBDAttributeConstraint co : filter.getOrderConstraints()) {
Expression orderExpr = getConstraintExpression(dataSource, select, co);
OrderByElement element = new OrderByElement();
element.setExpression(orderExpr);
if (co.isOrderDescending()) {
element.setAsc(false);
element.setAscDescPresent(true);
}
orderByElements.add(element);
}
}
return true;
}
use of net.sf.jsqlparser.expression.Expression in project xwiki-platform by xwiki.
the class EscapeLikeParametersQuery method modifyStatement.
/**
* Handle the case of MySQL: in MySQL a '\' character is a special escape character. In addition we often
* use '\' in Entity References. For example to find nested pages in a page with a dot would result in
* something like "LIKE '.%.a\.b.%'" which wouldn't work on MySQL. Thus we need to replace the default
* escape character with another one. To be safe we verify that the statement doesn't already specify an ESCAPE
* term.
*/
private String modifyStatement(String statementString) throws JSQLParserException {
Statement statement = CCJSqlParserUtil.parse(statementString);
if (statement instanceof Select) {
Select select = (Select) statement;
SelectBody selectBody = select.getSelectBody();
if (selectBody instanceof PlainSelect) {
PlainSelect plainSelect = (PlainSelect) selectBody;
Expression where = plainSelect.getWhere();
where.accept(new XWikiExpressionVisitor());
}
}
return statement.toString();
}
use of net.sf.jsqlparser.expression.Expression in project herddb by diennea.
the class SQLAggregator method createOutputColumns.
private static Column[] createOutputColumns(List<SelectItem> selectItems, DataScanner wrapped) throws StatementExecutionException {
Column[] columns = new Column[selectItems.size()];
int i = 0;
for (SelectItem item : selectItems) {
boolean done = false;
if (item instanceof SelectExpressionItem) {
SelectExpressionItem sei = (SelectExpressionItem) item;
Expression expression = sei.getExpression();
if (expression instanceof Function) {
Function f = (Function) expression;
String fieldName = f.toString();
if (sei.getAlias() != null && sei.getAlias().getName() != null) {
fieldName = sei.getAlias().getName();
}
if (fieldName == null) {
fieldName = "field" + i;
}
Column aggregated = BuiltinFunctions.toAggregatedOutputColumn(fieldName, f);
if (aggregated != null) {
columns[i] = aggregated;
done = true;
}
} else if (expression instanceof net.sf.jsqlparser.schema.Column) {
net.sf.jsqlparser.schema.Column c = (net.sf.jsqlparser.schema.Column) expression;
String name = c.getColumnName();
boolean found = false;
for (Column co : wrapped.getSchema()) {
if (co.name.equals(name)) {
columns[i] = co;
found = true;
break;
}
}
if (!found) {
throw new StatementExecutionException("cannot find column " + name + " is upstream scanner");
}
done = true;
} else {
throw new StatementExecutionException("unhandled aggregate query selectable item:" + expression);
}
}
i++;
if (!done) {
throw new StatementExecutionException("unhandled aggregate function " + item);
}
}
return columns;
}
Aggregations