use of net.sf.jsqlparser.schema.Column 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.schema.Column in project JSqlParser by JSQLParser.
the class SelectUtilsTest method testAddJoin.
@Test
public void testAddJoin() throws JSQLParserException {
Select select = (Select) CCJSqlParserUtil.parse("select a from mytable");
final EqualsTo equalsTo = new EqualsTo();
equalsTo.setLeftExpression(new Column("a"));
equalsTo.setRightExpression(new Column("b"));
Join addJoin = SelectUtils.addJoin(select, new Table("mytable2"), equalsTo);
addJoin.setLeft(true);
assertEquals("SELECT a FROM mytable LEFT JOIN mytable2 ON a = b", select.toString());
}
use of net.sf.jsqlparser.schema.Column in project JSqlParser by JSQLParser.
the class SelectUtilsTest method testBuildSelectFromTableWithGroupBy.
@Test
public void testBuildSelectFromTableWithGroupBy() {
Select select = SelectUtils.buildSelectFromTable(new Table("mytable"));
SelectUtils.addGroupBy(select, new Column("b"));
assertEquals("SELECT * FROM mytable GROUP BY b", select.toString());
}
use of net.sf.jsqlparser.schema.Column in project JSqlParser by JSQLParser.
the class SelectUtilsTest method testTableAliasIssue311.
@Test
public void testTableAliasIssue311() {
Table table1 = new Table("mytable1");
table1.setAlias(new Alias("tab1"));
Table table2 = new Table("mytable2");
table2.setAlias(new Alias("tab2"));
List<? extends Expression> colunas = Arrays.asList(new Column(table1, "col1"), new Column(table1, "col2"), new Column(table1, "col3"), new Column(table2, "b1"), new Column(table2, "b2"));
Select select = SelectUtils.buildSelectFromTableAndExpressions(table1, colunas.toArray(new Expression[colunas.size()]));
final EqualsTo equalsTo = new EqualsTo();
equalsTo.setLeftExpression(new Column(table1, "col1"));
equalsTo.setRightExpression(new Column(table2, "b1"));
Join addJoin = SelectUtils.addJoin(select, table2, equalsTo);
addJoin.setLeft(true);
assertEquals("SELECT tab1.col1, tab1.col2, tab1.col3, tab2.b1, tab2.b2 FROM mytable1 AS tab1 LEFT JOIN mytable2 AS tab2 ON tab1.col1 = tab2.b1", select.toString());
}
use of net.sf.jsqlparser.schema.Column in project JSqlParser by JSQLParser.
the class StatementDeParserTest method shouldUseProvidedDeParsersWhenDeParsingUpdateUsingSelect.
@Test
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
public void shouldUseProvidedDeParsersWhenDeParsingUpdateUsingSelect() {
Update update = new Update();
List<Column> columns = new ArrayList<Column>();
Select select = new Select();
Expression where = mock(Expression.class);
List<OrderByElement> orderByElements = new ArrayList<OrderByElement>();
Column column1 = new Column();
Column column2 = new Column();
SelectBody selectBody = mock(SelectBody.class);
OrderByElement orderByElement1 = new OrderByElement();
OrderByElement orderByElement2 = new OrderByElement();
Expression orderByElement1Expression = mock(Expression.class);
Expression orderByElement2Expression = mock(Expression.class);
update.setUseSelect(true);
update.setColumns(columns);
update.setSelect(select);
update.setWhere(where);
update.setOrderByElements(orderByElements);
columns.add(column1);
columns.add(column2);
select.setSelectBody(selectBody);
orderByElements.add(orderByElement1);
orderByElements.add(orderByElement2);
orderByElement1.setExpression(orderByElement1Expression);
orderByElement2.setExpression(orderByElement2Expression);
statementDeParser.visit(update);
then(expressionDeParser).should().visit(column1);
then(expressionDeParser).should().visit(column2);
then(selectBody).should().accept(selectDeParser);
then(where).should().accept(expressionDeParser);
then(orderByElement1Expression).should().accept(expressionDeParser);
then(orderByElement2Expression).should().accept(expressionDeParser);
}
Aggregations