com.alibaba.druid.sql.ast.expr.SQLListExpr Java Examples

The following examples show how to use com.alibaba.druid.sql.ast.expr.SQLListExpr. 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: SQLInExprWrapper.java    From Zebra with 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 File: SQLInExprWrapper.java    From Zebra with 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 #3
Source File: SQLInExprWrapper.java    From Zebra with 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 #4
Source File: SqlParser.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
private void findGroupBy(MySqlSelectQueryBlock query, Select select) throws SqlParseException {
    SQLSelectGroupByClause groupBy = query.getGroupBy();

    //modified by xzb group by 增加Having语法
    if (null != query.getGroupBy() && null != query.getGroupBy().getHaving()) {
        select.setHaving(query.getGroupBy().getHaving().toString());
    }

    SQLTableSource sqlTableSource = query.getFrom();
    if (groupBy == null) {
        return;
    }
    List<SQLExpr> items = groupBy.getItems();

    List<SQLExpr> standardGroupBys = new ArrayList<>();
    for (SQLExpr sqlExpr : items) {
        //todo: mysql expr patch
        if (sqlExpr instanceof MySqlOrderingExpr) {
            MySqlOrderingExpr sqlSelectGroupByExpr = (MySqlOrderingExpr) sqlExpr;
            sqlExpr = sqlSelectGroupByExpr.getExpr();
        }
        if ((sqlExpr instanceof SQLParensIdentifierExpr || !(sqlExpr instanceof SQLIdentifierExpr || sqlExpr instanceof SQLMethodInvokeExpr)) && !standardGroupBys.isEmpty()) {
            // flush the standard group bys
            // zhongshu-comment 先将standardGroupBys里面的字段传到select对象的groupBys字段中,然后给standardGroupBys分配一个没有元素的新的list
            select.addGroupBy(convertExprsToFields(standardGroupBys, sqlTableSource));
            standardGroupBys = new ArrayList<>();
        }

        if (sqlExpr instanceof SQLParensIdentifierExpr) {
            // single item with parens (should get its own aggregation)
            select.addGroupBy(FieldMaker.makeField(((SQLParensIdentifierExpr) sqlExpr).getExpr(), null, sqlTableSource.getAlias()));
        } else if (sqlExpr instanceof SQLListExpr) {
            // multiple items in their own list
            SQLListExpr listExpr = (SQLListExpr) sqlExpr;
            select.addGroupBy(convertExprsToFields(listExpr.getItems(), sqlTableSource));
        } else {
            // everything else gets added to the running list of standard group bys
            standardGroupBys.add(sqlExpr);
        }
    }
    if (!standardGroupBys.isEmpty()) {
        select.addGroupBy(convertExprsToFields(standardGroupBys, sqlTableSource));
    }
}