com.alibaba.druid.sql.ast.statement.SQLDeleteStatement Java Examples

The following examples show how to use com.alibaba.druid.sql.ast.statement.SQLDeleteStatement. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ESActionFactory.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
/**
 * Create the compatible Query object
 * based on the SQL query.
 * @param sql The SQL query.
 * @return Query object.
 */
   public static QueryAction create(Client client, String sql) throws SqlParseException, SQLFeatureNotSupportedException {
       sql = sql.replaceAll("\n", " ").trim();
       String firstWord = sql.substring(0, sql.indexOf(' '));
       switch (firstWord.toUpperCase()) {
		case "SELECT":
		    //zhongshu-comment 将sql字符串解析成AST,即SQLQueryExpr sqlExpr就是AST了,下面的代码就开始访问AST、从中获取token
			SQLQueryExpr sqlExpr = (SQLQueryExpr) toSqlExpr(sql);
               if(isMulti(sqlExpr)){//zhongshu-comment 判断是不是union查询,union查询两个select语句,btw:子查询也有多个select语句,至少2个
                   MultiQuerySelect multiSelect = new SqlParser().parseMultiSelect((SQLUnionQuery) sqlExpr.getSubQuery().getQuery());
                   handleSubQueries(client,multiSelect.getFirstSelect());
                   handleSubQueries(client,multiSelect.getSecondSelect());
                   return new MultiQueryAction(client, multiSelect);
               }
               else if(isJoin(sqlExpr,sql)){//zhongshu-comment join连接查询
                   JoinSelect joinSelect = new SqlParser().parseJoinSelect(sqlExpr);
                   handleSubQueries(client, joinSelect.getFirstTable());
                   handleSubQueries(client, joinSelect.getSecondTable());
                   return ESJoinQueryActionFactory.createJoinAction(client, joinSelect);
               }
               else {
                   //zhongshu-comment 大部分查询都是走这个分支,先看懂这个分支
                   Select select = new SqlParser().parseSelect(sqlExpr);
                   //todo 看不懂,测试了好几个常见的sql,都没有进去handleSubQueries该方法,那就先不理了,看别的
                   handleSubQueries(client, select);
                   return handleSelect(client, select);
               }
		case "DELETE":
               SQLStatementParser parser = createSqlStatementParser(sql);
			SQLDeleteStatement deleteStatement = parser.parseDeleteStatement();
			Delete delete = new SqlParser().parseDelete(deleteStatement);
			return new DeleteQueryAction(client, delete);
           case "SHOW":
               return new ShowQueryAction(client,sql);
		default:
			throw new SQLFeatureNotSupportedException(String.format("Unsupported query: %s", sql));
	}
}
 
Example #2
Source File: MycatSchemaStatVisitor.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
/**
     * 从between语句中获取字段所属的表名。
     * 对于容易出现ambiguous的(字段不知道到底属于哪个表),实际应用中必须使用别名来避免歧义
     * @param betweenExpr
     * @param column
     * @return
     */
    private String getOwnerTableName(SQLBetweenExpr betweenExpr,String column) {
        if(tableStats.size() == 1) {//只有一个表,直接返回这一个表名
            return tableStats.keySet().iterator().next().getName();
        } else if(tableStats.size() == 0) {//一个表都没有,返回空串
            return "";
        } else {//多个表名
            for (Column col : columns.keySet())
            {
                if(col.getName().equals(column)) {
                    return col.getTable();
                }
            }
//            for(Column col : columns) {//从columns中找表名
//                if(col.getName().equals(column)) {
//                    return col.getTable();
//                }
//            }

            //前面没找到表名的,自己从parent中解析

            SQLObject parent = betweenExpr.getParent();
            if(parent instanceof SQLBinaryOpExpr)
            {
                parent=parent.getParent();
            }

            if(parent instanceof MySqlSelectQueryBlock) {
                MySqlSelectQueryBlock select = (MySqlSelectQueryBlock) parent;
                if(select.getFrom() instanceof SQLJoinTableSource) {//多表连接
                    SQLJoinTableSource joinTableSource = (SQLJoinTableSource)select.getFrom();
                    return joinTableSource.getLeft().toString();//将left作为主表,此处有不严谨处,但也是实在没有办法,如果要准确,字段前带表名或者表的别名即可
                } else if(select.getFrom() instanceof SQLExprTableSource) {//单表
                    return select.getFrom().toString();
                }
            }
            else if(parent instanceof SQLUpdateStatement) {
                SQLUpdateStatement update = (SQLUpdateStatement) parent;
                return update.getTableName().getSimpleName();
            } else if(parent instanceof SQLDeleteStatement) {
                SQLDeleteStatement delete = (SQLDeleteStatement) parent;
                return delete.getTableName().getSimpleName();
            } else {
                
            }
        }
        return "";
    }
 
Example #3
Source File: WhereParser.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
public WhereParser(SqlParser sqlParser, SQLDeleteStatement delete) {
    this.sqlParser = sqlParser;
    this.delete = delete;
    this.where = delete.getWhere();
}
 
Example #4
Source File: SqlParser.java    From elasticsearch-sql with Apache License 2.0 3 votes vote down vote up
public Delete parseDelete(SQLDeleteStatement deleteStatement) throws SqlParseException {
    Delete delete = new Delete();
    WhereParser whereParser = new WhereParser(this, deleteStatement);

    delete.getFrom().addAll(findFrom(deleteStatement.getTableSource()));

    delete.setWhere(whereParser.findWhere());

    delete.getHints().addAll(parseHints(((MySqlDeleteStatement) deleteStatement).getHints()));

    findLimit(((MySqlDeleteStatement) deleteStatement).getLimit(), delete);

    return delete;
}