Search in sources :

Example 1 with SQLJoinTableSource

use of com.alibaba.druid.sql.ast.statement.SQLJoinTableSource in project Mycat_plus by coderczp.

the class JoinParser method parserTable.

private void parserTable(SQLTableSource table, TableFilter tFilter, boolean isOutJoin) {
    if (table instanceof SQLJoinTableSource) {
        SQLJoinTableSource table1 = (SQLJoinTableSource) table;
        joinType = table1.getJoinType().toString();
        if ((table1.getJoinType() == JoinType.COMMA) || (table1.getJoinType() == JoinType.JOIN) || (table1.getJoinType() == JoinType.INNER_JOIN) || (table1.getJoinType() == JoinType.LEFT_OUTER_JOIN)) {
            tFilter = setTableFilter(tFilter, getTableFilter(table1.getLeft(), isOutJoin));
            if (tableFilter == null) {
                tableFilter = tFilter;
            }
        }
        // parserTable(table1.getLeft());	//SQLExprTableSource
        parserTable(table1.getRight(), tFilter, true);
        // SQLBinaryOpExpr
        SQLExpr expr = table1.getCondition();
        parserJoinKey(expr);
    } else {
        tFilter = setTableFilter(tFilter, getTableFilter(table, isOutJoin));
        LOGGER.info("table " + table.toString() + " Alias:" + table.getAlias() + " Hints:" + table.getHints());
    }
}
Also used : SQLJoinTableSource(com.alibaba.druid.sql.ast.statement.SQLJoinTableSource) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 2 with SQLJoinTableSource

use of com.alibaba.druid.sql.ast.statement.SQLJoinTableSource in project druid by alibaba.

the class SQLTransformUtils method transformOracleToPostgresql.

public static SQLTableSource transformOracleToPostgresql(SQLTableSource x) {
    if (x instanceof OracleSelectTableReference) {
        OracleSelectTableReference xx = (OracleSelectTableReference) x;
        SQLExprTableSource y = new SQLExprTableSource();
        xx.cloneTo(y);
        y.setParent(x.getParent());
        return y;
    }
    if (x instanceof OracleSelectJoin) {
        OracleSelectJoin xx = (OracleSelectJoin) x;
        SQLJoinTableSource y = new SQLJoinTableSource();
        xx.cloneTo(y);
        y.setLeft(transformOracleToPostgresql(y.getLeft()));
        y.setRight(transformOracleToPostgresql(y.getRight()));
        y.setParent(x.getParent());
        return y;
    }
    if (x instanceof OracleSelectSubqueryTableSource) {
        OracleSelectSubqueryTableSource xx = (OracleSelectSubqueryTableSource) x;
        SQLSubqueryTableSource y = new SQLSubqueryTableSource();
        xx.cloneTo(y);
        y.setParent(x.getParent());
        return y;
    }
    if (x instanceof OracleWithSubqueryEntry) {
        SQLWithSubqueryClause.Entry entry = new SQLWithSubqueryClause.Entry();
        ((OracleWithSubqueryEntry) x).cloneTo(entry);
        entry.setParent(x.getParent());
        return entry;
    }
    return x;
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) OracleWithSubqueryEntry(com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleWithSubqueryEntry) SQLWithSubqueryClause(com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause) OracleSelectTableReference(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectTableReference) OracleSelectJoin(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectJoin) OracleSelectSubqueryTableSource(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectSubqueryTableSource) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLJoinTableSource(com.alibaba.druid.sql.ast.statement.SQLJoinTableSource) OracleWithSubqueryEntry(com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleWithSubqueryEntry)

Example 3 with SQLJoinTableSource

use of com.alibaba.druid.sql.ast.statement.SQLJoinTableSource in project druid by alibaba.

the class SQLJoinTest method test_0.

public void test_0() throws Exception {
    SQLSelectStatement stmt = (SQLSelectStatement) SQLUtils.parseStatements("select a.* from t_user a inner join t_group b on a.gid = b.id", JdbcConstants.ORACLE).get(0);
    SQLSelectQueryBlock queryBlock = stmt.getSelect().getQueryBlock();
    assertNotNull(queryBlock);
    SQLJoinTableSource join = (SQLJoinTableSource) queryBlock.getFrom();
    assertTrue(join.match("a", "b"));
}
Also used : SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLJoinTableSource(com.alibaba.druid.sql.ast.statement.SQLJoinTableSource)

Example 4 with SQLJoinTableSource

use of com.alibaba.druid.sql.ast.statement.SQLJoinTableSource in project canal by alibaba.

the class SqlParser method visitSelectTable.

/**
 * 解析表
 *
 * @param schemaItem 视图对象
 * @param sqlTableSource sqlTableSource
 * @param tableItems 表对象列表
 * @param tableItemTmp 表对象(临时)
 */
private static void visitSelectTable(SchemaItem schemaItem, SQLTableSource sqlTableSource, List<TableItem> tableItems, TableItem tableItemTmp) {
    if (sqlTableSource instanceof SQLExprTableSource) {
        SQLExprTableSource sqlExprTableSource = (SQLExprTableSource) sqlTableSource;
        TableItem tableItem;
        if (tableItemTmp != null) {
            tableItem = tableItemTmp;
        } else {
            tableItem = new TableItem(schemaItem);
        }
        tableItem.setSchema(sqlExprTableSource.getSchema());
        tableItem.setTableName(sqlExprTableSource.getTableName());
        if (tableItem.getAlias() == null) {
            tableItem.setAlias(sqlExprTableSource.getAlias());
        }
        if (tableItems.isEmpty()) {
            // 第一张表为主表
            tableItem.setMain(true);
        }
        tableItems.add(tableItem);
    } else if (sqlTableSource instanceof SQLJoinTableSource) {
        SQLJoinTableSource sqlJoinTableSource = (SQLJoinTableSource) sqlTableSource;
        SQLTableSource leftTableSource = sqlJoinTableSource.getLeft();
        visitSelectTable(schemaItem, leftTableSource, tableItems, null);
        SQLTableSource rightTableSource = sqlJoinTableSource.getRight();
        TableItem rightTableItem = new TableItem(schemaItem);
        // 解析on条件字段
        visitOnCondition(sqlJoinTableSource.getCondition(), rightTableItem);
        visitSelectTable(schemaItem, rightTableSource, tableItems, rightTableItem);
    } else if (sqlTableSource instanceof SQLSubqueryTableSource) {
        SQLSubqueryTableSource subQueryTableSource = (SQLSubqueryTableSource) sqlTableSource;
        MySqlSelectQueryBlock sqlSelectQuery = (MySqlSelectQueryBlock) subQueryTableSource.getSelect().getQuery();
        TableItem tableItem;
        if (tableItemTmp != null) {
            tableItem = tableItemTmp;
        } else {
            tableItem = new TableItem(schemaItem);
        }
        tableItem.setAlias(subQueryTableSource.getAlias());
        tableItem.setSubQuerySql(SQLUtils.toMySqlString(sqlSelectQuery));
        tableItem.setSubQuery(true);
        tableItem.setSubQueryFields(collectSelectQueryFields(sqlSelectQuery));
        visitSelectTable(schemaItem, sqlSelectQuery.getFrom(), tableItems, tableItem);
    }
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) TableItem(com.alibaba.otter.canal.client.adapter.es.core.config.SchemaItem.TableItem) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLJoinTableSource(com.alibaba.druid.sql.ast.statement.SQLJoinTableSource) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource)

Example 5 with SQLJoinTableSource

use of com.alibaba.druid.sql.ast.statement.SQLJoinTableSource in project druid by alibaba.

the class Issue2049 method test_0.

public void test_0() throws Exception {
    String sql = "select * from emp a,dmp b;";
    List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    SQLSelectStatement stmt = (SQLSelectStatement) stmtList.get(0);
    SQLSelectQueryBlock queryBlock = stmt.getSelect().getQueryBlock();
    SQLJoinTableSource joinTableSource = (SQLJoinTableSource) queryBlock.getFrom();
    assertEquals("a", joinTableSource.getLeft().getAlias());
    assertEquals("b", joinTableSource.getRight().getAlias());
}
Also used : SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLJoinTableSource(com.alibaba.druid.sql.ast.statement.SQLJoinTableSource) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Aggregations

SQLJoinTableSource (com.alibaba.druid.sql.ast.statement.SQLJoinTableSource)9 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)3 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)3 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)2 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)2 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)1 TableConfig (com.actiontech.dble.config.model.TableConfig)1 StringPtr (com.actiontech.dble.plan.common.ptr.StringPtr)1 SchemaInfo (com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)1 SQLWithSubqueryClause (com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause)1 MySqlDeleteStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement)1 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)1 OracleWithSubqueryEntry (com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleWithSubqueryEntry)1 OracleSelectJoin (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectJoin)1 OracleSelectSubqueryTableSource (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectSubqueryTableSource)1 OracleSelectTableReference (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectTableReference)1 TableItem (com.alibaba.otter.canal.client.adapter.es.core.config.SchemaItem.TableItem)1