Java Code Examples for com.alibaba.druid.sql.ast.expr.SQLBinaryOperator

The following examples show how to use com.alibaba.druid.sql.ast.expr.SQLBinaryOperator. These examples are extracted from open source projects. 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 Project: Mycat2   Source File: JoinParser.java    License: GNU General Public License v3.0 6 votes vote down vote up
private void opSQLExpr(SQLBinaryOpExpr expr,String Operator) {
	   if (expr==null) {
		   return;
	   }
	   SQLExpr exprL=expr.getLeft();
	   if (!(exprL instanceof SQLBinaryOpExpr))
	   {
		   String field=exprL.toString(); //获取表达式 左边的值
		   String value=getExpValue(expr.getRight()).toString(); //获取表达式右边的值
		   if (expr.getOperator()==SQLBinaryOperator.Equality) {  
			 if (checkJoinField(value)) {//设置joinKey
				//joinLkey=field;
				//joinRkey=value; 
				tableFilter.setJoinKey(field,value);
			 }
			 else {
				 tableFilter.addWhere(field, value, expr.getOperator().getName(), Operator);
			 }
		   }
		   else {
			   tableFilter.addWhere(field, value, expr.getOperator().getName(), Operator);
		   }
	   }		
}
 
Example 2
/**
	 * 递归拆分OR
	 * 
	 * @param whereUnit
	 * TODO:考虑嵌套or语句,条件中有子查询、 exists等很多种复杂情况是否能兼容
	 */
	private void splitUntilNoOr(WhereUnit whereUnit) {
		if(whereUnit.isFinishedParse()) {
			if(whereUnit.getSubWhereUnit().size() > 0) {
				for(int i = 0; i < whereUnit.getSubWhereUnit().size(); i++) {
					splitUntilNoOr(whereUnit.getSubWhereUnit().get(i));
				}
			} 
		} else {
			SQLBinaryOpExpr expr = whereUnit.getCanSplitExpr();
			if(expr.getOperator() == SQLBinaryOperator.BooleanOr) {
//				whereUnit.addSplitedExpr(expr.getRight());
				addExprIfNotFalse(whereUnit, expr.getRight());
				if(expr.getLeft() instanceof SQLBinaryOpExpr) {
					whereUnit.setCanSplitExpr((SQLBinaryOpExpr)expr.getLeft());
					splitUntilNoOr(whereUnit);
				} else {
					addExprIfNotFalse(whereUnit, expr.getLeft());
				}
			} else {
				addExprIfNotFalse(whereUnit, expr);
				whereUnit.setFinishedParse(true);
			}
		}
    }
 
Example 3
Source Project: baymax   Source File: OrVisitor.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean visit(SQLBinaryOpExpr x) {
       x.getLeft().setParent(x);
       x.getRight().setParent(x);

       if (!SQLBinaryOperator.BooleanOr.equals(x.getOperator())) {
           return super.visit(x);
       }

       // true * (a and b) = (a and b) 没有意义
       if (Boolean.TRUE.equals(WallVisitorUtils.getValue(x))) {
           return false;
       }

       // 这是一个Or表达式 这个or代表的集合是要和当前已经获取的Condition列表 相乘的.
       orEntity.add(new OrEntity(this, x));
       return false;
   }
 
Example 4
Source Project: Mycat2   Source File: DruidSelectParser.java    License: GNU General Public License v3.0 5 votes vote down vote up
private HavingCols buildGroupByHaving(SQLExpr having,Map<String, String> aliaColumns ){
	if (having == null) {
		return null;
	}

	SQLBinaryOpExpr expr  = ((SQLBinaryOpExpr) having);
	SQLExpr left = expr.getLeft();
	SQLBinaryOperator operator = expr.getOperator();
	SQLExpr right = expr.getRight();

	String leftValue = null;;
	if (left instanceof SQLAggregateExpr) {
		leftValue = ((SQLAggregateExpr) left).getMethodName() + "("
				+ ((SQLAggregateExpr) left).getArguments().get(0) + ")";
		String aggrColumnAlias = getAliaColumn(aliaColumns,leftValue);
		if(aggrColumnAlias != null) { // having聚合函数存在别名
			expr.setLeft(new SQLIdentifierExpr(aggrColumnAlias));
			leftValue = aggrColumnAlias;
		}
	} else if (left instanceof SQLIdentifierExpr) {
		leftValue = ((SQLIdentifierExpr) left).getName();
	}

	String rightValue = null;
	if (right instanceof  SQLNumericLiteralExpr) {
		rightValue = right.toString();
	}else if(right instanceof SQLTextLiteralExpr){
		rightValue = StringUtil.removeBackquote(right.toString());
	}

	return new HavingCols(leftValue,rightValue,operator.getName());
}
 
Example 5
private void parseThreeLevelPageSql(SQLStatement stmt, RouteResultset rrs, SchemaConfig schema, SQLSubqueryTableSource from, SQLBinaryOpExpr one, SQLBinaryOperator operator)
{
       SQLIntegerExpr right = (SQLIntegerExpr) one.getRight();
	int firstrownum = right.getNumber().intValue();
	if (operator == SQLBinaryOperator.GreaterThanOrEqual&&firstrownum!=0) {
		firstrownum = firstrownum - 1;
	}
	SQLSelectQuery subSelect = from.getSelect().getQuery();
	if (subSelect instanceof OracleSelectQueryBlock)
       {  //第二层子查询
           OracleSelectQueryBlock twoSubSelect = (OracleSelectQueryBlock) subSelect;
           if (twoSubSelect.getWhere() instanceof SQLBinaryOpExpr && twoSubSelect.getFrom() instanceof SQLSubqueryTableSource)
           {
               SQLBinaryOpExpr twoWhere = (SQLBinaryOpExpr) twoSubSelect.getWhere();
               boolean isRowNum = "rownum".equalsIgnoreCase(twoWhere.getLeft().toString());
               boolean isLess = twoWhere.getOperator() == SQLBinaryOperator.LessThanOrEqual || twoWhere.getOperator() == SQLBinaryOperator.LessThan;
               if (isRowNum && twoWhere.getRight() instanceof SQLIntegerExpr && isLess)
               {
                   int lastrownum = ((SQLIntegerExpr) twoWhere.getRight()).getNumber().intValue();
                   if (operator == SQLBinaryOperator.LessThan&&lastrownum!=0) {
					lastrownum = lastrownum - 1;
				}
                   SQLSelectQuery finalQuery = ((SQLSubqueryTableSource) twoSubSelect.getFrom()).getSelect().getQuery();
                   if (finalQuery instanceof OracleSelectQueryBlock)
                   {
					setLimitIFChange(stmt, rrs, schema, one, firstrownum, lastrownum);
                       parseOrderAggGroupOracle(stmt,rrs, (OracleSelectQueryBlock) finalQuery, schema);
                       isNeedParseOrderAgg=false;
                   }

               }

           }

       }
}
 
Example 6
Source Project: dble   Source File: ExpressionUtil.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * convert Expression to DNF Expression
 *
 * @param expr
 * @return
 */
public static SQLExpr toDNF(SQLExpr expr) {
    if (expr == null)
        return null;

    while (!isDNF(expr)) {
        SQLBinaryOpExpr binOpExpr = (SQLBinaryOpExpr) expr;
        if (binOpExpr.getOperator() == SQLBinaryOperator.BooleanOr) {
            expr = expandOrExpression(binOpExpr);
        } else if (binOpExpr.getOperator() == SQLBinaryOperator.BooleanAnd) {
            expr = expandAndExpression(binOpExpr);
        }
    }
    return expr;
}
 
Example 7
Source Project: dble   Source File: ExpressionUtil.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * (A OR B) AND C OR B OR (E OR F) AND G = ((A AND C ) OR (A AND B)) OR B OR
 * ((E AND G) OR (F AND G))
 *
 * @param expr
 * @return
 */
private static SQLBinaryOpExpr expandOrExpression(SQLBinaryOpExpr expr) {
    SQLExpr left = expr.getLeft();
    SQLExpr right = expr.getRight();
    SQLExpr leftOp = toDNF(left);
    SQLExpr rightOp = toDNF(right);
    return new SQLBinaryOpExpr(leftOp, SQLBinaryOperator.BooleanOr, rightOp);
}
 
Example 8
Source Project: dble   Source File: ExpressionUtil.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * allow (A and B) and C
 *
 * @param expr
 * @return
 */
public static boolean isDNF(SQLExpr expr) {
    if (!isLogicalExpression(expr)) {
        return true;
    }
    SQLBinaryOpExpr binOpExpr = (SQLBinaryOpExpr) expr;
    SQLExpr left = binOpExpr.getLeft();
    SQLExpr right = binOpExpr.getRight();
    if (binOpExpr.getOperator() == SQLBinaryOperator.BooleanAnd) {
        boolean isAllNonLogicExpr = true;
        if (left instanceof SQLBinaryOpExpr) {
            SQLBinaryOpExpr leftBinaryOp = (SQLBinaryOpExpr) left;
            if (leftBinaryOp.getOperator() == SQLBinaryOperator.BooleanOr) {
                return false;
            }
            if (isLogicalExpression(leftBinaryOp)) {
                isAllNonLogicExpr = false;
            }
        }
        if (right instanceof SQLBinaryOpExpr) {
            SQLBinaryOpExpr rightBinaryOp = (SQLBinaryOpExpr) right;
            if (rightBinaryOp.getOperator() == SQLBinaryOperator.BooleanOr) {
                return false;
            }
            if (isLogicalExpression(rightBinaryOp)) {
                isAllNonLogicExpr = false;
            }
        }
        if (isAllNonLogicExpr) {
            return true;
        }
    }

    if (!isDNF(left)) {
        return false;
    }
    return isDNF(right);
}
 
Example 9
Source Project: dble   Source File: ExpressionUtil.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static boolean isLogicalExpression(SQLExpr expr) { //XOR?
    if (!(expr instanceof SQLBinaryOpExpr)) {
        return false;
    }
    SQLBinaryOpExpr binOpExpr = (SQLBinaryOpExpr) expr;
    return binOpExpr.getOperator() == SQLBinaryOperator.BooleanAnd || binOpExpr.getOperator() == SQLBinaryOperator.BooleanOr;
}
 
Example 10
Source Project: dble   Source File: ItemAllAnySubQuery.java    License: GNU General Public License v2.0 5 votes vote down vote up
public ItemAllAnySubQuery(String currentDb, SQLSelectQuery query, SQLBinaryOperator operator, boolean isAll, ProxyMetaManager metaManager, Map<String, String> usrVariables) {
    super(currentDb, query, metaManager, usrVariables);
    this.isAll = isAll;
    this.operator = operator;
    if (this.planNode.getColumnsSelected().size() > 1) {
        throw new MySQLOutPutException(ErrorCode.ER_OPERAND_COLUMNS, "", "Operand should contain 1 column(s)");
    }
    this.select = this.planNode.getColumnsSelected().get(0);
}
 
Example 11
Source Project: Zebra   Source File: ShardRange.java    License: Apache License 2.0 5 votes vote down vote up
private static int convertOperator(SQLBinaryOperator operator) {
	if (operator == SQLBinaryOperator.GreaterThan) {
		return ShardRange.OP_Greater;
	} else if (operator == SQLBinaryOperator.GreaterThanOrEqual) {
		return ShardRange.OP_GreaterOrEqual;
	} else if (operator == SQLBinaryOperator.LessThan) {
		return ShardRange.OP_Less;
	} else if (operator == SQLBinaryOperator.LessThanOrEqual) {
		return ShardRange.OP_LessOrEqual;
	} else if (operator == SQLBinaryOperator.Equality) {
		return ShardRange.OP_Equal;
	} else {
		return ShardRange.OP_NONE;
	}
}
 
Example 12
Source Project: elasticsearch-sql   Source File: WhereParser.java    License: Apache License 2.0 5 votes vote down vote up
private boolean isAllowedMethodOnConditionLeft(SQLMethodInvokeExpr method, SQLBinaryOperator operator) {
    return (method.getMethodName().toLowerCase().equals("nested") ||
            method.getMethodName().toLowerCase().equals("children") ||
            SQLFunctions.buildInFunctions.contains(method.getMethodName().toLowerCase())
    ) &&
            !operator.isLogical();
}
 
Example 13
Source Project: dble   Source File: ItemFuncIstrue.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLExpr left = args.get(0).toExpression();
    return new SQLBinaryOpExpr(left, SQLBinaryOperator.Is, new SQLBooleanExpr(true));
}
 
Example 14
Source Project: dble   Source File: ItemFuncIsnottrue.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLExpr left = args.get(0).toExpression();
    return new SQLBinaryOpExpr(left, SQLBinaryOperator.IsNot, new SQLBooleanExpr(true));
}
 
Example 15
Source Project: dble   Source File: ItemFuncLt.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLExpr left = args.get(0).toExpression();
    SQLExpr right = args.get(1).toExpression();
    return new SQLBinaryOpExpr(left, SQLBinaryOperator.LessThan, right);
}
 
Example 16
Source Project: dble   Source File: ItemFuncGt.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLExpr left = args.get(0).toExpression();
    SQLExpr right = args.get(1).toExpression();
    return new SQLBinaryOpExpr(left, SQLBinaryOperator.GreaterThan, right);
}
 
Example 17
Source Project: dble   Source File: ItemFuncIsnotfalse.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLExpr left = args.get(0).toExpression();
    return new SQLBinaryOpExpr(left, SQLBinaryOperator.IsNot, new SQLBooleanExpr(false));
}
 
Example 18
Source Project: dble   Source File: ItemFuncIsnotnull.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLExpr left = args.get(0).toExpression();
    return new SQLBinaryOpExpr(left, SQLBinaryOperator.IsNot, new SQLIdentifierExpr("UNKNOWN"));
}
 
Example 19
Source Project: dble   Source File: ItemFuncNe.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLExpr left = args.get(0).toExpression();
    SQLExpr right = args.get(1).toExpression();
    return new SQLBinaryOpExpr(left, SQLBinaryOperator.LessThanOrGreater, right);
}
 
Example 20
Source Project: dble   Source File: ItemFuncLe.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLExpr left = args.get(0).toExpression();
    SQLExpr right = args.get(1).toExpression();
    return new SQLBinaryOpExpr(left, SQLBinaryOperator.LessThanOrEqual, right);
}
 
Example 21
Source Project: dble   Source File: ItemFuncIsnull.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLExpr left = args.get(0).toExpression();
    return new SQLBinaryOpExpr(left, SQLBinaryOperator.Is, new SQLIdentifierExpr("UNKNOWN"));
}
 
Example 22
Source Project: dble   Source File: ItemFuncStrictEqual.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLExpr left = args.get(0).toExpression();
    SQLExpr right = args.get(1).toExpression();
    return new SQLBinaryOpExpr(left, SQLBinaryOperator.LessThanOrEqualOrGreaterThan, right);
}
 
Example 23
Source Project: dble   Source File: ItemFuncEqual.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLExpr left = args.get(0).toExpression();
    SQLExpr right = args.get(1).toExpression();
    return new SQLBinaryOpExpr(left, SQLBinaryOperator.Equality, right);
}
 
Example 24
Source Project: dble   Source File: ItemFuncIsfalse.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLExpr left = args.get(0).toExpression();
    return new SQLBinaryOpExpr(left, SQLBinaryOperator.Is, new SQLBooleanExpr(false));
}
 
Example 25
Source Project: dble   Source File: ItemFuncGe.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLExpr left = args.get(0).toExpression();
    SQLExpr right = args.get(1).toExpression();
    return new SQLBinaryOpExpr(left, SQLBinaryOperator.GreaterThanOrEqual, right);
}
 
Example 26
Source Project: dble   Source File: ItemFuncRegex.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLExpr left = args.get(0).toExpression();
    SQLExpr right = args.get(1).toExpression();
    return new SQLBinaryOpExpr(left, SQLBinaryOperator.RegExp, right);
}
 
Example 27
Source Project: dble   Source File: ItemFuncXor.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLExpr left = args.get(0).toExpression();
    SQLExpr right = args.get(1).toExpression();
    return new SQLBinaryOpExpr(left, SQLBinaryOperator.BooleanXor, right);
}
 
Example 28
Source Project: dble   Source File: ItemFuncBitAnd.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    return new SQLBinaryOpExpr(args.get(0).toExpression(), SQLBinaryOperator.BitwiseAnd, args.get(1).toExpression());
}
 
Example 29
Source Project: dble   Source File: ItemFuncLeftShift.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    return new SQLBinaryOpExpr(args.get(0).toExpression(), SQLBinaryOperator.LeftShift, args.get(1).toExpression());
}
 
Example 30
Source Project: dble   Source File: ItemFuncBitOr.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    return new SQLBinaryOpExpr(args.get(0).toExpression(), SQLBinaryOperator.BitwiseOr, args.get(1).toExpression());
}