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

The following examples show how to use com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr. 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: Zebra   Source File: SQLInExprWrapper.java    License: Apache License 2.0 6 votes vote down vote up
public Set<Integer> getValueRefSet() {
	if (!initValueRefSet) {
		if (sqlExpr instanceof SQLVariantRefExpr) {
			valueRefSet.add(((SQLVariantRefExpr) sqlExpr).getIndex());
		} else if (sqlExpr instanceof SQLListExpr) {
			List<SQLExpr> items = ((SQLListExpr) sqlExpr).getItems();
			if (items != null) {
				for (SQLExpr expr : items) {
					if (expr instanceof SQLVariantRefExpr) {
						valueRefSet.add(((SQLVariantRefExpr) expr).getIndex());
					}
				}
			}
		}
		initValueRefSet = true;
	}
	if (valueRefSet == null) {
		valueRefSet = new HashSet<Integer>();
	}
	return valueRefSet;
}
 
Example 2
Source Project: elasticsearch-sql   Source File: Util.java    License: Apache License 2.0 6 votes vote down vote up
public static Object expr2Object(SQLExpr expr, String charWithQuote) {
    Object value = null;
    if (expr instanceof SQLNumericLiteralExpr) {
        value = ((SQLNumericLiteralExpr) expr).getNumber();
    } else if (expr instanceof SQLCharExpr) {
        value = charWithQuote + ((SQLCharExpr) expr).getText() + charWithQuote;
    } else if (expr instanceof SQLIdentifierExpr) {
        value = expr.toString();
    } else if (expr instanceof SQLPropertyExpr) {
        value = expr.toString();
    } else if (expr instanceof SQLVariantRefExpr) {
        value = expr.toString();
    } else if (expr instanceof SQLAllColumnExpr) {
        value = "*";
    } else if (expr instanceof SQLValuableExpr) {
        value = ((SQLValuableExpr) expr).getValue();
    } else if (expr instanceof SQLBooleanExpr) {
        value = ((SQLBooleanExpr) expr).getValue();
    } else {
        //throw new SqlParseException("can not support this type " + expr.getClass());
    }
    return value;
}
 
Example 3
Source Project: elasticsearch-sql   Source File: Util.java    License: Apache License 2.0 6 votes vote down vote up
public static Object getScriptValueWithQuote(SQLExpr expr, String quote) throws SqlParseException {
    if (expr instanceof SQLIdentifierExpr || expr instanceof SQLPropertyExpr || expr instanceof SQLVariantRefExpr) {
        return "doc['" + expr.toString() + "'].value";
    }  else if (expr instanceof SQLCharExpr) {
        return quote + ((SQLCharExpr) expr).getValue() + quote;
    } else if (expr instanceof SQLIntegerExpr) {
        return ((SQLIntegerExpr) expr).getValue();
    } else if (expr instanceof SQLNumericLiteralExpr) {
        return ((SQLNumericLiteralExpr) expr).getNumber();
    } else if (expr instanceof SQLNullExpr) {
        return ((SQLNullExpr) expr).toString().toLowerCase();
    } else if (expr instanceof  SQLBinaryOpExpr) {
        //zhongshu-comment 该分支由忠树添加
        String left = "doc['" + ((SQLBinaryOpExpr) expr).getLeft().toString() + "'].value";
        String operator = ((SQLBinaryOpExpr) expr).getOperator().getName();
        String right = "doc['" + ((SQLBinaryOpExpr) expr).getRight().toString() + "'].value";
        return left + operator + right;
    }
    throw new SqlParseException("could not parse sqlBinaryOpExpr need to be identifier/valuable got " + expr.getClass().toString() + " with value:" + expr.toString());
}
 
Example 4
Source Project: dts   Source File: UpdateParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public TableDataInfo getPresentValue(List<Object> sqlParamsList, SQLUpdateStatement parseSqlStatement,
    StatementAdapter statementAdapter, TableMetaInfo tableMetaInfo) throws SQLException {
    TableDataInfo txcTable = new TableDataInfo();
    txcTable.setTableName(parseSqlStatement.getTableName().getSimpleName());
    TxcLine txcLine = new TxcLine();
    List<SQLUpdateSetItem> items = parseSqlStatement.getItems();
    int variantExpr = 0;
    for (int i = 0; i < items.size(); i++) {
        SQLUpdateSetItem sqlUpdateSetItem = items.get(i);
        TxcField txcField = new TxcField();
        String cloumnName =
            SQLUtils.toSQLString(sqlUpdateSetItem.getColumn()).replace("\'", "").replace("`", "").trim();
        txcField.setName(cloumnName);
        if (sqlUpdateSetItem.getValue() instanceof SQLVariantRefExpr) {
            txcField.setValue(sqlParamsList.get(variantExpr++));
        } else if (sqlUpdateSetItem.getValue() instanceof SQLValuableExpr) {
            txcField.setValue(SQLUtils.toSQLString(items.get(i).getValue()));
        } else {
            throw new UnsupportedOperationException(
                String.format("Do not support complex sql,%s", sqlUpdateSetItem.getClass().toString()));
        }
        txcField.setJdkValue(SerializeUtils.serialize(txcField.getValue()));
        txcLine.getFields().add(txcField);
    }
    txcTable.getLine().add(txcLine);
    return txcTable;
}
 
Example 5
Source Project: dts   Source File: UpdateParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected List<Object> getWhereParams(List<Object> sqlParamsList, SQLUpdateStatement parseSqlStatement) {
    if (sqlParamsList != null && !sqlParamsList.isEmpty()) {
        int size = 0;
        for (SQLUpdateSetItem sqlUpdateSetItem : parseSqlStatement.getItems()) {
            if (sqlUpdateSetItem.getValue() instanceof SQLVariantRefExpr) {
                size++;
            }
        }
        return Lists.newArrayList(sqlParamsList.subList(size, sqlParamsList.size()));
    }
    return Lists.newArrayList();
}
 
Example 6
Source Project: dble   Source File: SelectHandler.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static boolean isVariantRef(SQLExpr expr) {
    if (expr instanceof SQLVariantRefExpr) {
        return true;
    } else if (expr instanceof SQLPropertyExpr) {
        return isVariantRef(((SQLPropertyExpr) expr).getOwner());
    } else {
        return false;
    }
}
 
Example 7
Source Project: Zebra   Source File: SQLInExprWrapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int hashCode() {
	if (!initHash) {
		if (sqlExpr instanceof SQLValuableExpr) {
			hash = sqlExpr.hashCode();
		} else if (sqlExpr instanceof SQLVariantRefExpr) {
			SQLVariantRefExpr ref = (SQLVariantRefExpr) sqlExpr;
			hash = ref.hashCode() * 31 + ref.getIndex();
			valueRefSet.add(ref.getIndex());
		} else if (sqlExpr instanceof SQLListExpr) {
			SQLListExpr listExpr = (SQLListExpr) sqlExpr;
			List<SQLExpr> items = listExpr.getItems();
			hash = 1;
			if (items != null) {
				for (SQLExpr expr : items) {
					if (expr instanceof SQLVariantRefExpr) {
						hash = (hash * 31 + expr.hashCode()) * 31 + ((SQLVariantRefExpr) expr).getIndex();
						valueRefSet.add(((SQLVariantRefExpr) expr).getIndex());
					} else {
						hash = hash * 31 + expr.hashCode();
					}
				}
			}
		}
		initValueRefSet = true;
		initHash = true;
	}
	return hash;
}
 
Example 8
Source Project: Zebra   Source File: ShardPreparedStatement.java    License: Apache License 2.0 5 votes vote down vote up
private void replaceLimitParams(SQLParsedResult parseResult) {
	if (parseResult != null) {
		SQLStatement sqlStatement = parseResult.getStmt();
		if (parseResult.getStmt() != null && sqlStatement instanceof SQLSelectStatement) {
			SQLSelect sqlSelect = ((SQLSelectStatement) sqlStatement).getSelect();
			if (sqlSelect != null) {
				SQLSelectQuery sqlSelectQuery = sqlSelect.getQuery();
				if (sqlSelectQuery != null && sqlSelectQuery instanceof MySqlSelectQueryBlock) {
					MySqlSelectQueryBlock sqlSelectQueryBlock = (MySqlSelectQueryBlock) sqlSelectQuery;
					MySqlSelectQueryBlock.Limit limitExpr = sqlSelectQueryBlock.getLimit();
					if (limitExpr != null) {
						int offsetRefIndex = -1;
						int countRefIndex = -1;
						if (limitExpr.getOffset() instanceof SQLVariantRefExpr
						      && limitExpr.getRowCount() instanceof SQLVariantRefExpr) {
							SQLVariantRefExpr offsetExpr = (SQLVariantRefExpr) limitExpr.getOffset();
							SQLVariantRefExpr countExpr = (SQLVariantRefExpr) limitExpr.getRowCount();

							offsetRefIndex = offsetExpr.getIndex();
							countRefIndex = countExpr.getIndex();

							if (offsetRefIndex > countRefIndex && offsetRefIndex != -1 && countRefIndex != -1) {
								offsetExpr.setIndex(countRefIndex);
								countExpr.setIndex(offsetRefIndex);
							}
						}
					}
				}
			}
		}
	}
}
 
Example 9
Source Project: elasticsearch-sql   Source File: Util.java    License: Apache License 2.0 5 votes vote down vote up
public static Object removeTableAilasFromField(Object expr, String tableAlias) {

        if (expr instanceof SQLIdentifierExpr || expr instanceof SQLPropertyExpr || expr instanceof SQLVariantRefExpr) {
            String name = expr.toString().replace("`", "");
            if (tableAlias != null) {
                String aliasPrefix = tableAlias + ".";
                if (name.startsWith(aliasPrefix)) {
                    String newFieldName = name.replaceFirst(aliasPrefix, "");
                    return new SQLIdentifierExpr(newFieldName);
                }
            }
        }
        return expr;
    }
 
Example 10
Source Project: elasticsearch-sql   Source File: Util.java    License: Apache License 2.0 5 votes vote down vote up
public static Object getScriptValue(SQLExpr expr) throws SqlParseException {
    if (expr instanceof SQLIdentifierExpr || expr instanceof SQLPropertyExpr || expr instanceof SQLVariantRefExpr) {
        return "doc['" + expr.toString() + "'].value";
    } else if (expr instanceof SQLValuableExpr) {
        return ((SQLValuableExpr) expr).getValue();
    }
    throw new SqlParseException("could not parse sqlBinaryOpExpr need to be identifier/valuable got" + expr.getClass().toString() + " with value:" + expr.toString());
}
 
Example 11
Source Project: elasticsearch-sql   Source File: WhereParser.java    License: Apache License 2.0 5 votes vote down vote up
private boolean isCond(SQLBinaryOpExpr expr) {
    SQLExpr leftSide = expr.getLeft();
    if (leftSide instanceof SQLMethodInvokeExpr) {
        return isAllowedMethodOnConditionLeft((SQLMethodInvokeExpr) leftSide, expr.getOperator());
    }
    return leftSide instanceof SQLIdentifierExpr ||
            leftSide instanceof SQLPropertyExpr ||
            leftSide instanceof SQLVariantRefExpr ||
            leftSide instanceof SQLCastExpr;
}
 
Example 12
Source Project: dble   Source File: ItemVariables.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    return new SQLVariantRefExpr(name);
}
 
Example 13
Source Project: Zebra   Source File: SQLInExprWrapper.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public boolean equals(Object obj) {
	if (this == obj) {
		return true;
	}
	if (obj == null || getClass() != obj.getClass()) {
		return false;
	}

	SQLInExprWrapper wrapper = (SQLInExprWrapper) obj;
	if (sqlExpr == wrapper.sqlExpr) {
		return true;
	}

	if (sqlExpr != null && wrapper.sqlExpr != null) {
		if (sqlExpr.getClass() != wrapper.sqlExpr.getClass()) {
			return false;
		}
		if (sqlExpr instanceof SQLVariantRefExpr && wrapper.sqlExpr instanceof SQLVariantRefExpr) {
			SQLVariantRefExpr ref1 = (SQLVariantRefExpr) sqlExpr;
			SQLVariantRefExpr ref2 = (SQLVariantRefExpr) wrapper.sqlExpr;
			return (ref1.equals(ref2) && ref1.getIndex() == ref2.getIndex());
		} else if (sqlExpr instanceof SQLListExpr && wrapper.sqlExpr instanceof SQLListExpr) {
			List<SQLExpr> list1 = ((SQLListExpr) sqlExpr).getItems();
			List<SQLExpr> list2 = ((SQLListExpr) wrapper.sqlExpr).getItems();
			if (list1 == list2) {
				return true;
			} else if (list1 != null && list2 != null) {
				Iterator<SQLExpr> it1 = list1.iterator();
				Iterator<SQLExpr> it2 = list2.iterator();
				while (it1.hasNext() && it2.hasNext()) {
					SQLExpr expr1 = it1.next();
					SQLExpr expr2 = it2.next();
					if (expr1 == expr2) {
						continue;
					} else if (expr1 != null && expr2 != null) {
						if (expr1 instanceof SQLVariantRefExpr && expr2 instanceof SQLVariantRefExpr) {
							return expr1.equals(expr2)
									&& (((SQLVariantRefExpr) expr1).getIndex() == ((SQLVariantRefExpr) expr2).getIndex());
						}
						return expr1.equals(expr2);
					} else {
						return false;
					}
				}
				return !(it1.hasNext() || it2.hasNext());
			} else {
				return false;
			}
		} else {
			return sqlExpr.equals(wrapper.sqlExpr);
		}
	} else {
		return false;
	}
}
 
Example 14
Source Project: elasticsearch-sql   Source File: SQLFunctions.java    License: Apache License 2.0 4 votes vote down vote up
private static boolean isProperty(SQLExpr expr) {
    return (expr instanceof SQLIdentifierExpr || expr instanceof SQLPropertyExpr || expr instanceof SQLVariantRefExpr);
}