Search in sources :

Example 21 with Table

use of net.sf.jsqlparser.schema.Table in project dbeaver by dbeaver.

the class SQLSemanticProcessor method getConstraintTable.

/**
 * Extract alias (or source table name) for specified constraint from SQL select.
 * Searches in FROM and JOIN
 */
@Nullable
public static Table getConstraintTable(PlainSelect select, DBDAttributeConstraint constraint) {
    String constrTable;
    DBSAttributeBase ca = constraint.getAttribute();
    if (ca instanceof DBDAttributeBinding) {
        constrTable = ((DBDAttributeBinding) ca).getMetaAttribute().getEntityName();
    } else if (ca instanceof DBSEntityAttribute) {
        constrTable = ((DBSEntityAttribute) ca).getParentObject().getName();
    } else {
        return null;
    }
    if (constrTable == null) {
        return null;
    }
    FromItem fromItem = select.getFromItem();
    Table table = findTableInFrom(fromItem, constrTable);
    if (table == null) {
        // Maybe it is a join
        if (!CommonUtils.isEmpty(select.getJoins())) {
            for (Join join : select.getJoins()) {
                table = findTableInFrom(join.getRightItem(), constrTable);
                if (table != null) {
                    break;
                }
            }
        }
    }
    return table;
}
Also used : Table(net.sf.jsqlparser.schema.Table) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) DBSAttributeBase(org.jkiss.dbeaver.model.struct.DBSAttributeBase) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) Nullable(org.jkiss.code.Nullable)

Example 22 with Table

use of net.sf.jsqlparser.schema.Table in project dbeaver by serge-rider.

the class SQLSemanticProcessor method getConstraintExpression.

private static Expression getConstraintExpression(PlainSelect select, DBDAttributeConstraint co) throws JSQLParserException {
    Expression orderExpr;
    String attrName = co.getAttribute().getName();
    if (attrName.isEmpty()) {
        orderExpr = new LongValue(co.getAttribute().getOrdinalPosition() + 1);
    } 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);
        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;
}
Also used : Table(net.sf.jsqlparser.schema.Table) Expression(net.sf.jsqlparser.expression.Expression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) Column(net.sf.jsqlparser.schema.Column) LongValue(net.sf.jsqlparser.expression.LongValue)

Example 23 with Table

use of net.sf.jsqlparser.schema.Table in project dbeaver by dbeaver.

the class SQLSemanticProcessor method getConstraintExpression.

private static Expression getConstraintExpression(DBPDataSource dataSource, PlainSelect select, DBDAttributeConstraint co) throws JSQLParserException {
    Expression orderExpr;
    String attrName = DBUtils.getQuotedIdentifier(dataSource, co.getAttribute().getName());
    if (attrName.isEmpty()) {
        orderExpr = new LongValue(co.getAttribute().getOrdinalPosition() + 1);
    } 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);
        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;
}
Also used : Table(net.sf.jsqlparser.schema.Table) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) Expression(net.sf.jsqlparser.expression.Expression) Column(net.sf.jsqlparser.schema.Column) LongValue(net.sf.jsqlparser.expression.LongValue)

Example 24 with Table

use of net.sf.jsqlparser.schema.Table in project JSqlParser by JSQLParser.

the class CreateViewDeParserTest method testUseExtrnalExpressionDeparser.

/**
 * Test of deParse method, of class CreateViewDeParser.
 */
@Test
public void testUseExtrnalExpressionDeparser() throws JSQLParserException {
    StringBuilder b = new StringBuilder();
    SelectDeParser selectDeParser = new SelectDeParser();
    selectDeParser.setBuffer(b);
    ExpressionDeParser expressionDeParser = new ExpressionDeParser(selectDeParser, b) {

        @Override
        public void visit(Column tableColumn) {
            final Table table = tableColumn.getTable();
            String tableName = null;
            if (table != null) {
                if (table.getAlias() != null) {
                    tableName = table.getAlias().getName();
                } else {
                    tableName = table.getFullyQualifiedName();
                }
            }
            if (tableName != null && !tableName.isEmpty()) {
                getBuffer().append("\"").append(tableName).append("\"").append(".");
            }
            getBuffer().append("\"").append(tableColumn.getColumnName()).append("\"");
        }
    };
    selectDeParser.setExpressionVisitor(expressionDeParser);
    CreateViewDeParser instance = new CreateViewDeParser(b, selectDeParser);
    CreateView vc = (CreateView) CCJSqlParserUtil.parse("CREATE VIEW test AS SELECT a, b FROM mytable");
    instance.deParse(vc);
    assertEquals("CREATE VIEW test AS SELECT a, b FROM mytable", vc.toString());
    assertEquals("CREATE VIEW test AS SELECT \"a\", \"b\" FROM mytable", instance.getBuffer().toString());
}
Also used : Table(net.sf.jsqlparser.schema.Table) Column(net.sf.jsqlparser.schema.Column) CreateView(net.sf.jsqlparser.statement.create.view.CreateView) Test(org.junit.Test)

Example 25 with Table

use of net.sf.jsqlparser.schema.Table in project JSqlParser by JSQLParser.

the class StatementDeParserTest method shouldUseProvidedDeParsersWhenDeParsingReplaceWithoutItemsList.

@Test
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
public void shouldUseProvidedDeParsersWhenDeParsingReplaceWithoutItemsList() {
    Replace replace = new Replace();
    Table table = new Table();
    List<Column> columns = new ArrayList<Column>();
    List<Expression> expressions = new ArrayList<Expression>();
    Column column1 = new Column();
    Column column2 = new Column();
    Expression expression1 = mock(Expression.class);
    Expression expression2 = mock(Expression.class);
    replace.setTable(table);
    replace.setColumns(columns);
    replace.setExpressions(expressions);
    columns.add(column1);
    columns.add(column2);
    expressions.add(expression1);
    expressions.add(expression2);
    statementDeParser.visit(replace);
    then(expression1).should().accept(expressionDeParser);
    then(expression2).should().accept(expressionDeParser);
}
Also used : Replace(net.sf.jsqlparser.statement.replace.Replace) Table(net.sf.jsqlparser.schema.Table) Column(net.sf.jsqlparser.schema.Column) Expression(net.sf.jsqlparser.expression.Expression) ArrayList(java.util.ArrayList) 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