Search in sources :

Example 6 with SQLJoinTableSource

use of com.alibaba.druid.sql.ast.statement.SQLJoinTableSource in project Mycat-Server by MyCATApache.

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 7 with SQLJoinTableSource

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

the class DruidDeleteParser method visitorParse.

@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
    String schemaName = schema == null ? null : schema.getName();
    MySqlDeleteStatement delete = (MySqlDeleteStatement) stmt;
    SQLTableSource tableSource = delete.getTableSource();
    SQLTableSource fromSource = delete.getFrom();
    if (fromSource != null) {
        tableSource = fromSource;
    }
    if (tableSource instanceof SQLJoinTableSource) {
        StringPtr sqlSchema = new StringPtr(null);
        if (!SchemaUtil.isNoSharding(sc, (SQLJoinTableSource) tableSource, stmt, schemaName, sqlSchema)) {
            String msg = "DELETE query with multiple tables is not supported, sql:" + stmt;
            throw new SQLNonTransientException(msg);
        } else {
            if (delete.getWhere() != null && !SchemaUtil.isNoSharding(sc, delete.getWhere(), schemaName, sqlSchema)) {
                String msg = "DELETE query with sub-query is not supported, sql:" + stmt;
                throw new SQLNonTransientException(msg);
            }
            String realSchema = sqlSchema.get() == null ? schemaName : sqlSchema.get();
            SchemaConfig schemaConfig = DbleServer.getInstance().getConfig().getSchemas().get(realSchema);
            rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), realSchema));
            RouterUtil.routeToSingleNode(rrs, schemaConfig.getDataNode());
            rrs.setFinishedRoute(true);
            return schema;
        }
    } else {
        SQLExprTableSource deleteTableSource = (SQLExprTableSource) tableSource;
        SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, deleteTableSource);
        if (!ServerPrivileges.checkPrivilege(sc, schemaInfo.getSchema(), schemaInfo.getTable(), CheckType.DELETE)) {
            String msg = "The statement DML privilege check is not passed, sql:" + stmt;
            throw new SQLNonTransientException(msg);
        }
        schema = schemaInfo.getSchemaConfig();
        rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema()));
        if (RouterUtil.isNoSharding(schema, schemaInfo.getTable())) {
            if (delete.getWhere() != null && !SchemaUtil.isNoSharding(sc, delete.getWhere(), schemaName, new StringPtr(schemaInfo.getSchema()))) {
                String msg = "DELETE query with sub-query is not supported, sql:" + stmt;
                throw new SQLNonTransientException(msg);
            }
            RouterUtil.routeToSingleNode(rrs, schema.getDataNode());
            return schema;
        }
        super.visitorParse(schema, rrs, stmt, visitor, sc);
        if (visitor.isHasSubQuery()) {
            String msg = "DELETE query with sub-query  is not supported, sql:" + stmt;
            throw new SQLNonTransientException(msg);
        }
        TableConfig tc = schema.getTables().get(schemaInfo.getTable());
        if (tc != null && tc.isGlobalTable()) {
            RouterUtil.routeToMultiNode(false, rrs, tc.getDataNodes(), tc.isGlobalTable());
            rrs.setFinishedRoute(true);
            return schema;
        }
    }
    return schema;
}
Also used : MySqlDeleteStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement) SQLNonTransientException(java.sql.SQLNonTransientException) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) StringPtr(com.actiontech.dble.plan.common.ptr.StringPtr) TableConfig(com.actiontech.dble.config.model.TableConfig) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLJoinTableSource(com.alibaba.druid.sql.ast.statement.SQLJoinTableSource) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Example 8 with SQLJoinTableSource

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

the class Issue2876 method test_0.

public void test_0() throws Exception {
    String sql = "SELECT CONCAT(\"'\",b.PRIMARY_ID,\"'\") \n" + "FROM s_user_session_attributes a \n" + "LEFT JOIN s_user_session b ON a.SESSION_PRIMARY_ID=b.PRIMARY_ID \n" + "WHERE a.ATTRIBUTE_NAME='KAPTCHA_SESSION_KEY' AND b.LAST_ACCESS_TIME <= 1540429945459";
    System.out.println(sql);
    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)

Example 9 with SQLJoinTableSource

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

the class MySqlSelectTest_178 method test_0.

public void test_0() throws Exception {
    String sql = "select rt_dws_csn_sta_lgt_ord_ri.metrics_id as yujiu from rt_dws_csn_sta_lgt_ord_ri CROSS JOIN rt_dws_csn_sta_lgt_ord_mi ;";
    // 
    List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL, SQLParserFeature.EnableSQLBinaryOpExprGroup, SQLParserFeature.OptimizedForParameterized);
    SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
    assertEquals(1, statementList.size());
    assertEquals("SELECT rt_dws_csn_sta_lgt_ord_ri.metrics_id AS yujiu\n" + "FROM rt_dws_csn_sta_lgt_ord_ri\n" + "\tCROSS JOIN rt_dws_csn_sta_lgt_ord_mi;", stmt.toString());
    assertEquals("select rt_dws_csn_sta_lgt_ord_ri.metrics_id as yujiu\n" + "from rt_dws_csn_sta_lgt_ord_ri\n" + "\tcross join rt_dws_csn_sta_lgt_ord_mi;", stmt.toLowerCaseString());
    assertEquals("SELECT rt_dws_csn_sta_lgt_ord_ri.metrics_id AS yujiu\n" + "FROM rt_dws_csn_sta_lgt_ord_ri\n" + "\tCROSS JOIN rt_dws_csn_sta_lgt_ord_mi;", stmt.toParameterizedString());
    SQLJoinTableSource join = (SQLJoinTableSource) stmt.getSelect().getQueryBlock().getFrom();
    assertEquals(SQLJoinTableSource.JoinType.CROSS_JOIN, join.getJoinType());
    assertNull(join.getLeft().getAlias());
}
Also used : SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) 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