Search in sources :

Example 1 with Column

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);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) Statement(net.sf.jsqlparser.statement.Statement) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) JSQLParserException(net.sf.jsqlparser.JSQLParserException) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) ArrayList(java.util.ArrayList) SQLQuery(org.jkiss.dbeaver.model.sql.SQLQuery) Function(net.sf.jsqlparser.expression.Function) Expression(net.sf.jsqlparser.expression.Expression) Column(net.sf.jsqlparser.schema.Column) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList)

Example 2 with Column

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());
}
Also used : Table(net.sf.jsqlparser.schema.Table) Column(net.sf.jsqlparser.schema.Column) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) Join(net.sf.jsqlparser.statement.select.Join) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo) Test(org.junit.Test)

Example 3 with Column

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());
}
Also used : Table(net.sf.jsqlparser.schema.Table) Column(net.sf.jsqlparser.schema.Column) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) Test(org.junit.Test)

Example 4 with Column

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());
}
Also used : Table(net.sf.jsqlparser.schema.Table) Column(net.sf.jsqlparser.schema.Column) Expression(net.sf.jsqlparser.expression.Expression) Alias(net.sf.jsqlparser.expression.Alias) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) Join(net.sf.jsqlparser.statement.select.Join) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo) Test(org.junit.Test)

Example 5 with Column

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);
}
Also used : Column(net.sf.jsqlparser.schema.Column) Expression(net.sf.jsqlparser.expression.Expression) ArrayList(java.util.ArrayList) Select(net.sf.jsqlparser.statement.select.Select) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement) SelectBody(net.sf.jsqlparser.statement.select.SelectBody) Update(net.sf.jsqlparser.statement.update.Update) Test(org.junit.Test)

Aggregations

Column (net.sf.jsqlparser.schema.Column)42 Test (org.junit.Test)19 Expression (net.sf.jsqlparser.expression.Expression)18 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)18 Select (net.sf.jsqlparser.statement.select.Select)18 Table (net.sf.jsqlparser.schema.Table)17 ArrayList (java.util.ArrayList)9 SelectExpressionItem (net.sf.jsqlparser.statement.select.SelectExpressionItem)8 SelectItem (net.sf.jsqlparser.statement.select.SelectItem)8 OrderByElement (net.sf.jsqlparser.statement.select.OrderByElement)6 Statement (net.sf.jsqlparser.statement.Statement)5 SelectBody (net.sf.jsqlparser.statement.select.SelectBody)5 LongValue (net.sf.jsqlparser.expression.LongValue)4 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)4 SimpleNode (net.sf.jsqlparser.parser.SimpleNode)4 SelectVisitorAdapter (net.sf.jsqlparser.statement.select.SelectVisitorAdapter)4 WriteBuilder (com.google.cloud.spanner.Mutation.WriteBuilder)3 ExpressionVisitorAdapter (net.sf.jsqlparser.expression.ExpressionVisitorAdapter)3 CloudSpannerSQLException (nl.topicus.jdbc.exception.CloudSpannerSQLException)3 SQLException (java.sql.SQLException)2