Search in sources :

Example 1 with Table

use of net.sf.jsqlparser.schema.Table 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;
}
Also used : Table(net.sf.jsqlparser.schema.Table) DBDAttributeConstraint(org.jkiss.dbeaver.model.data.DBDAttributeConstraint) Expression(net.sf.jsqlparser.expression.Expression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression)

Example 2 with Table

use of net.sf.jsqlparser.schema.Table 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;
}
Also used : Table(net.sf.jsqlparser.schema.Table) DBDAttributeConstraint(org.jkiss.dbeaver.model.data.DBDAttributeConstraint) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) Expression(net.sf.jsqlparser.expression.Expression)

Example 3 with Table

use of net.sf.jsqlparser.schema.Table 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 4 with Table

use of net.sf.jsqlparser.schema.Table 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 5 with Table

use of net.sf.jsqlparser.schema.Table 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)

Aggregations

Table (net.sf.jsqlparser.schema.Table)42 Column (net.sf.jsqlparser.schema.Column)18 Expression (net.sf.jsqlparser.expression.Expression)14 Test (org.junit.Test)13 Select (net.sf.jsqlparser.statement.select.Select)11 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)9 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)8 ArrayList (java.util.ArrayList)6 Nullable (org.jkiss.code.Nullable)6 Statement (net.sf.jsqlparser.statement.Statement)5 DBException (org.jkiss.dbeaver.DBException)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 java.util (java.util)4 List (java.util.List)4 Matcher (java.util.regex.Matcher)4 Pattern (java.util.regex.Pattern)4 PatternSyntaxException (java.util.regex.PatternSyntaxException)4 LongValue (net.sf.jsqlparser.expression.LongValue)4 Delete (net.sf.jsqlparser.statement.delete.Delete)4 Insert (net.sf.jsqlparser.statement.insert.Insert)4