net.sf.jsqlparser.expression.Expression Java Examples
The following examples show how to use
net.sf.jsqlparser.expression.Expression.
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: SQLCommandInfoHolder.java From sql-to-mongo-db-query-converter with Apache License 2.0 | 6 votes |
private AliasHolder generateHashAliasFromSelectItems(List<SelectItem> selectItems) { HashMap<String,String> aliasFromFieldHash = new HashMap<String,String>(); HashMap<String,String> fieldFromAliasHash = new HashMap<String,String>(); for(SelectItem sitem: selectItems) { if(!(sitem instanceof AllColumns)) { if(sitem instanceof SelectExpressionItem) { SelectExpressionItem seitem = (SelectExpressionItem) sitem; if(seitem.getAlias() != null) { Expression selectExp = seitem.getExpression(); selectExp.accept(new ExpVisitorEraseAliasTableBaseBuilder(this.from.getBaseAliasTable())); String expStr = selectExp.toString(); String aliasStr = seitem.getAlias().getName(); aliasFromFieldHash.put( expStr, aliasStr); fieldFromAliasHash.put( aliasStr, expStr); } } } } return new AliasHolder(aliasFromFieldHash, fieldFromAliasHash); }
Example #2
Source File: CTEToNestedQueryConverter.java From quetzal with Eclipse Public License 2.0 | 6 votes |
@Override public void visit(Function function) { clear(); boolean tmpLeftTableFound = false; boolean tmpRightTableFound = false; boolean tmpWhereOnlyExpFound = false; boolean prevIsTopVal = isTopLevel; isTopLevel = false; if (function.getParameters()!=null) { for (Expression e: function.getParameters().getExpressions()) { e.accept(this); tmpLeftTableFound |= leftTableFound; tmpRightTableFound |= rightTableFound; tmpWhereOnlyExpFound |= whereOnlyExpFound; } } leftTableFound = tmpLeftTableFound; rightTableFound = tmpRightTableFound; whereOnlyExpFound = tmpWhereOnlyExpFound; isTopLevel = prevIsTopVal; defaultTopLevelProcessing(function); }
Example #3
Source File: ObjPD.java From openprodoc with GNU Affero General Public License v3.0 | 6 votes |
private int EvalExprType(Expression where) { if (where instanceof AndExpression) return (EXPR_AND); else if (where instanceof OrExpression) return (EXPR_OR); else if (where instanceof BinaryExpression) return(EXPR_BASIC); else if (where instanceof Function) return(EXPR_FUNCT); else if (where instanceof Parenthesis) return (EXPR_PAR); else if (where instanceof InExpression) return (EXPR_IN); else if (where instanceof NotExpression) return (EXPR_NOT); return(-1); }
Example #4
Source File: FunctionCountTest.java From Mybatis-PageHelper with MIT License | 6 votes |
@Test public void test() { Select select = select("select max(name),code,min(aa),nvl(ab,0),heh from user where a > 100"); List<SelectItem> selectItems = ((PlainSelect) select.getSelectBody()).getSelectItems(); for (SelectItem item : selectItems) { if (item instanceof SelectExpressionItem) { Expression exp = ((SelectExpressionItem) item).getExpression(); if (exp instanceof Function) { System.out.println("Function:" + item.toString()); } else { System.out.println("Not a function:" + exp.toString()); } } else { System.out.println("Not a function:" + item.toString()); } } }
Example #5
Source File: BinaryExpressionConverter.java From sqlhelper with GNU Lesser General Public License v3.0 | 6 votes |
@Override public JE toJSqlParserExpression(SE expression) { SQLExpression left = (SQLExpression) expression.getLeft(); SQLExpression right = (SQLExpression) expression.getRight(); Expression leftExp = ExpressionConverters.toJSqlParserExpression(left); Expression rightExp = ExpressionConverters.toJSqlParserExpression(right); if (jsqlparserExpressionSupplier != null) { JE jsqlparserExpression = jsqlparserExpressionSupplier.get(expression); jsqlparserExpression.setLeftExpression(leftExp); jsqlparserExpression.setRightExpression(rightExp); return jsqlparserExpression; } else { return buildJSqlParserExpression(expression, leftExp, rightExp); } }
Example #6
Source File: AbstractSpannerExpressionVisitorAdapter.java From spanner-jdbc with MIT License | 6 votes |
@Override public void visit(SignedExpression value) { Expression underlyingValue = value.getExpression(); if (underlyingValue instanceof DoubleValue) { DoubleValue doubleValue = (DoubleValue) underlyingValue; doubleValue .setValue(value.getSign() == '-' ? -doubleValue.getValue() : doubleValue.getValue()); visit(doubleValue); } else if (underlyingValue instanceof LongValue) { LongValue longValue = (LongValue) underlyingValue; longValue.setValue(value.getSign() == '-' ? -longValue.getValue() : longValue.getValue()); visit(longValue); } else { super.visit(value); } }
Example #7
Source File: CloudSpannerPreparedStatement.java From spanner-jdbc with MIT License | 6 votes |
private Mutation createUpdateMutation(Update update, boolean generateParameterMetaData) throws SQLException { if (update.getTables().isEmpty()) throw new CloudSpannerSQLException("No table found in update statement", Code.INVALID_ARGUMENT); if (update.getTables().size() > 1) throw new CloudSpannerSQLException( "Update statements for multiple tables at once are not supported", Code.INVALID_ARGUMENT); String table = unquoteIdentifier(update.getTables().get(0).getFullyQualifiedName()); getParameterStore().setTable(table); List<Expression> expressions = update.getExpressions(); WriteBuilder builder = Mutation.newUpdateBuilder(table); int index = 0; for (Column col : update.getColumns()) { String columnName = unquoteIdentifier(col.getFullyQualifiedName()); expressions.get(index).accept(new ValueBinderExpressionVisitorAdapter<>(getParameterStore(), builder.set(columnName), columnName)); index++; } visitUpdateWhereClause(update.getWhere(), builder, generateParameterMetaData); return builder.build(); }
Example #8
Source File: CloudSpannerPreparedStatement.java From spanner-jdbc with MIT License | 6 votes |
private Mutation createDeleteMutation(Delete delete, boolean generateParameterMetaData) throws SQLException { String table = unquoteIdentifier(delete.getTable().getFullyQualifiedName()); getParameterStore().setTable(table); Expression where = delete.getWhere(); if (where == null) { // Delete all return Mutation.delete(table, KeySet.all()); } else { // Delete one DeleteKeyBuilder keyBuilder = new DeleteKeyBuilder(getConnection().getTable(table), generateParameterMetaData); visitDeleteWhereClause(where, keyBuilder, generateParameterMetaData); return Mutation.delete(table, keyBuilder.getKeyBuilder().build()); } }
Example #9
Source File: CloudSpannerPreparedStatement.java From spanner-jdbc with MIT License | 6 votes |
private void visitDeleteWhereClause(Expression where, DeleteKeyBuilder keyBuilder, boolean generateParameterMetaData) throws SQLException { if (where != null) { DMLWhereClauseVisitor whereClauseVisitor = new DMLWhereClauseVisitor(getParameterStore()) { @Override protected void visitExpression(Column col, Expression expression) { String columnName = unquoteIdentifier(col.getFullyQualifiedName()); keyBuilder.set(columnName); expression.accept( new KeyBuilderExpressionVisitorAdapter(getParameterStore(), columnName, keyBuilder)); } }; where.accept(whereClauseVisitor); if (!generateParameterMetaData && !whereClauseVisitor.isValid()) { throw new CloudSpannerSQLException(INVALID_WHERE_CLAUSE_DELETE_MESSAGE, Code.INVALID_ARGUMENT); } } }
Example #10
Source File: CloudSpannerPreparedStatement.java From spanner-jdbc with MIT License | 6 votes |
private void visitUpdateWhereClause(Expression where, WriteBuilder builder, boolean generateParameterMetaData) throws SQLException { if (where != null) { DMLWhereClauseVisitor whereClauseVisitor = new DMLWhereClauseVisitor(getParameterStore()) { @Override protected void visitExpression(Column col, Expression expression) { String columnName = unquoteIdentifier(col.getFullyQualifiedName()); expression.accept(new ValueBinderExpressionVisitorAdapter<>(getParameterStore(), builder.set(columnName), columnName)); } }; where.accept(whereClauseVisitor); if (!generateParameterMetaData && !whereClauseVisitor.isValid()) { throw new CloudSpannerSQLException(INVALID_WHERE_CLAUSE_UPDATE_MESSAGE, Code.INVALID_ARGUMENT); } } else { throw new SQLException(INVALID_WHERE_CLAUSE_UPDATE_MESSAGE); } }
Example #11
Source File: SqlUtils.java From sql-to-mongo-db-query-converter with Apache License 2.0 | 6 votes |
public static DateFunction isDateFunction(Expression incomingExpression) throws ParseException { if (ComparisonOperator.class.isInstance(incomingExpression)) { ComparisonOperator comparisonOperator = (ComparisonOperator)incomingExpression; String rightExpression = getStringValue(comparisonOperator.getRightExpression()); if (Function.class.isInstance(comparisonOperator.getLeftExpression())) { Function function = ((Function)comparisonOperator.getLeftExpression()); if ("date".equals(function.getName().toLowerCase()) && (function.getParameters().getExpressions().size()==2) && StringValue.class.isInstance(function.getParameters().getExpressions().get(1))) { String column = getStringValue(function.getParameters().getExpressions().get(0)); DateFunction dateFunction = null; try { dateFunction = new DateFunction(((StringValue)(function.getParameters().getExpressions().get(1))).getValue(),rightExpression,column); dateFunction.setComparisonFunction(comparisonOperator); } catch (IllegalArgumentException e) { throw new ParseException(e.getMessage()); } return dateFunction; } } } return null; }
Example #12
Source File: SqlUtils.java From sql-to-mongo-db-query-converter with Apache License 2.0 | 6 votes |
public static Object parseFunctionArguments(final ExpressionList parameters, final FieldType defaultFieldType, final Map<String, FieldType> fieldNameToFieldTypeMapping) { if (parameters == null) { return null; } else if (parameters.getExpressions().size()==1) { return getStringValue(parameters.getExpressions().get(0)); } else { return Lists.newArrayList(Lists.transform(parameters.getExpressions(), new com.google.common.base.Function<Expression, Object>() { @Override public Object apply(Expression expression) { try { return getValue(expression, null, defaultFieldType, fieldNameToFieldTypeMapping); } catch (ParseException e) { return getStringValue(expression); } } })); } }
Example #13
Source File: SqlUtils.java From sql-to-mongo-db-query-converter with Apache License 2.0 | 6 votes |
public static Object getValue(Expression incomingExpression, Expression otherSide, FieldType defaultFieldType, Map<String, FieldType> fieldNameToFieldTypeMapping) throws ParseException { FieldType fieldType = otherSide !=null ? firstNonNull(fieldNameToFieldTypeMapping.get(getStringValue(otherSide)), defaultFieldType) : FieldType.UNKNOWN; if (LongValue.class.isInstance(incomingExpression)) { return normalizeValue((((LongValue)incomingExpression).getValue()),fieldType); } else if (SignedExpression.class.isInstance(incomingExpression)) { return normalizeValue((((SignedExpression)incomingExpression).toString()),fieldType); } else if (StringValue.class.isInstance(incomingExpression)) { return normalizeValue((((StringValue)incomingExpression).getValue()),fieldType); } else if (Column.class.isInstance(incomingExpression)) { return normalizeValue(getStringValue(incomingExpression),fieldType); } else { throw new ParseException("can not parseNaturalLanguageDate: " + incomingExpression.toString()); } }
Example #14
Source File: SQLCommandInfoHolder.java From sql-to-mongo-db-query-converter with Apache License 2.0 | 6 votes |
public SQLCommandInfoHolder(SQLCommandType sqlCommandType, Expression whereClause, boolean isDistinct, boolean isCountAll, boolean isTotalGroup, FromHolder from, long limit, long offset, List<SelectItem> selectItems, List<Join> joins, List<String> groupBys, List<OrderByElement> orderByElements, AliasHolder aliasHolder, Expression havingClause) { this.sqlCommandType = sqlCommandType; this.whereClause = whereClause; this.isDistinct = isDistinct; this.isCountAll = isCountAll; this.isTotalGroup = isTotalGroup; this.from = from; this.limit = limit; this.offset = offset; this.selectItems = selectItems; this.joins = joins; this.groupBys = groupBys; this.havingClause = havingClause; this.orderByElements = orderByElements; this.aliasHolder = aliasHolder; }
Example #15
Source File: QueryConverter.java From sql-to-mongo-db-query-converter with Apache License 2.0 | 6 votes |
/** * Erase table base alias and get where part of main table when joins * @param exp * @param tholder * @return */ private Expression preprocessWhere(Expression exp, FromHolder tholder){ if(sqlCommandInfoHolder.getJoins()!=null && !sqlCommandInfoHolder.getJoins().isEmpty()) { ExpressionHolder partialWhereExpHolder = new ExpressionHolder(null); MutableBoolean haveOrExpression = new MutableBoolean(false); exp.accept(new WhereVisitorMatchAndLookupPipelineMatchBuilder(tholder.getBaseAliasTable(), partialWhereExpHolder, haveOrExpression)); if(haveOrExpression.booleanValue()) { return null;//with or exp we can't use match first step } exp = partialWhereExpHolder.getExpression(); } if(exp != null) { exp.accept(new ExpVisitorEraseAliasTableBaseBuilder(tholder.getBaseAliasTable())); } return exp; }
Example #16
Source File: CloudSpannerPreparedStatement.java From spanner-jdbc with MIT License | 6 votes |
private void setWhereParameters(Expression where, com.google.cloud.spanner.Statement.Builder builder) { if (where != null) { where.accept(new ExpressionVisitorAdapter() { private String currentCol = null; @Override public void visit(Column col) { currentCol = unquoteIdentifier(col.getFullyQualifiedName()); } @Override public void visit(JdbcParameter parameter) { parameter.accept(new ValueBinderExpressionVisitorAdapter<>(getParameterStore(), builder.bind("p" + parameter.getIndex()), currentCol)); currentCol = null; } @Override public void visit(SubSelect subSelect) { setSelectParameters(subSelect.getSelectBody(), builder); } }); } }
Example #17
Source File: CloudSpannerPreparedStatement.java From spanner-jdbc with MIT License | 6 votes |
private boolean isSingleRowWhereClause(TableKeyMetaData table, Expression where) { if (where != null) { SingleRowWhereClauseValidator validator = new SingleRowWhereClauseValidator(table); DMLWhereClauseVisitor whereClauseVisitor = new DMLWhereClauseVisitor(getParameterStore()) { @Override protected void visitExpression(Column col, Expression expression) { String columnName = unquoteIdentifier(col.getFullyQualifiedName()); validator.set(columnName); expression.accept(new SingleRowWhereClauseValidatorExpressionVisitorAdapter( getParameterStore(), validator)); } }; where.accept(whereClauseVisitor); return whereClauseVisitor.isValid() && validator.isValid(); } return false; }
Example #18
Source File: SqlUtils.java From sql-to-mongo-db-query-converter with Apache License 2.0 | 5 votes |
public static String getFieldFromFunction(Function function) throws ParseException{ List<String> parameters = function.getParameters()== null ? Collections.<String>emptyList() : Lists.transform(function.getParameters().getExpressions(), new com.google.common.base.Function<Expression, String>() { @Override public String apply(@Nonnull Expression expression) { return SqlUtils.getStringValue(expression); } }); if (parameters.size() > 1) { throw new ParseException(function.getName()+" function can only have one parameter"); } return parameters.size() > 0 ? Iterables.get(parameters, 0) : null; }
Example #19
Source File: CTEToNestedQueryConverter.java From quetzal with Eclipse Public License 2.0 | 5 votes |
private void defaultTopLevelProcessing(Expression exp) { if (isTopLevel) { isTopLevel = false; if (whereOnlyExpFound) { whereExp = exp; onExp = null; } else { whereExp = null; onExp = exp; } } }
Example #20
Source File: QueryUtils.java From foxtrot with Apache License 2.0 | 5 votes |
public static Number expressionToNumber(Expression expression) { if(expression instanceof StringValue) { return Long.valueOf(((StringValue)expression).getValue()); } if(expression instanceof LongValue) { return ((LongValue)expression).getValue(); } if(expression instanceof DoubleValue) { return ((DoubleValue)expression).getValue(); } return null; }
Example #21
Source File: QueryUtils.java From foxtrot with Apache License 2.0 | 5 votes |
public static String expressionToString(Expression expression) { if(expression instanceof Column) { return ((Column)expression).getFullyQualifiedName(); } if(expression instanceof StringValue) { return ((StringValue)expression).getValue(); } return null; }
Example #22
Source File: JSqlParsers.java From sqlhelper with GNU Lesser General Public License v3.0 | 5 votes |
public static boolean expressionEquals(Expression expr1, Expression expr2) { if (expr1 == null && expr2 == null) { return true; } if (expr1 == null || expr2 == null) { return false; } if (expr1 instanceof Column && expr2 instanceof Column) { return columnEquals((Column) expr1, (Column) expr2); } return expr1.toString().equalsIgnoreCase(expr2.toString()); }
Example #23
Source File: CTEToNestedQueryConverter.java From quetzal with Eclipse Public License 2.0 | 5 votes |
@Override public void visit(AndExpression andExpression) { clear(); boolean prevIsTopVal = isTopLevel; boolean tmpLeftTableFound = false; boolean tmpRightTableFound = false; boolean tmpWhereOnlyExpFound = false; isTopLevel = false; for (Expression exp: flatten(andExpression)) { exp.accept(this); if (prevIsTopVal) { if (whereOnlyExpFound) { whereExp = whereExp == null? exp: new AndExpression(whereExp, exp); } else { onExp = onExp==null? exp: new AndExpression(onExp, exp); } } // update tmp tmpLeftTableFound |= leftTableFound; tmpRightTableFound |= rightTableFound; tmpWhereOnlyExpFound |= whereOnlyExpFound; // } // update leftTableFound = tmpLeftTableFound; rightTableFound = tmpRightTableFound; whereOnlyExpFound = tmpWhereOnlyExpFound; // }
Example #24
Source File: SqlUtils.java From sql-to-mongo-db-query-converter with Apache License 2.0 | 5 votes |
public static boolean isSpecialtyFunction(Expression incomingExpression) { if (incomingExpression == null) { return false; } if (Function.class.isInstance(incomingExpression) && containsIgnoreCase(SPECIALTY_FUNCTIONS, ((Function)incomingExpression).getName())) { return true; } return false; }
Example #25
Source File: CTEToNestedQueryConverter.java From quetzal with Eclipse Public License 2.0 | 5 votes |
public void defaultVisit(Expression exp) { clear(); whereOnlyExpFound = true; if (isTopLevel) { whereExp = exp; onExp = null; } }
Example #26
Source File: SqlUtils.java From sql-to-mongo-db-query-converter with Apache License 2.0 | 5 votes |
public static String getStringValue(Expression expression) { if (StringValue.class.isInstance(expression)) { return ((StringValue)expression).getValue(); } else if (Column.class.isInstance(expression)) { String columnName = expression.toString(); Matcher matcher = SURROUNDED_IN_QUOTES.matcher(columnName); if (matcher.matches()) { return matcher.group(1); } return columnName; } return expression.toString(); }
Example #27
Source File: CTEToNestedQueryConverter.java From quetzal with Eclipse Public License 2.0 | 5 votes |
private Expression getExpression(SelectItem item, Table table) { SelectExpressionItem rightSelectItem = (SelectExpressionItem) item; Expression rightExp = rightSelectItem.getExpression(); if (rightExp instanceof Column) { Column c = (Column) rightExp; if (c.getTable() == null) { c.setTable(table); } } return rightExp; }
Example #28
Source File: SqlServerParser.java From Mybatis-PageHelper with MIT License | 5 votes |
/** * 复制 OrderByElement * * @param orig 原 OrderByElement * @param expression 新 OrderByElement 的排序要素 * @return 复制的新 OrderByElement */ protected OrderByElement cloneOrderByElement(OrderByElement orig, Expression expression) { OrderByElement element = new OrderByElement(); element.setAsc(orig.isAsc()); element.setAscDescPresent(orig.isAscDescPresent()); element.setNullOrdering(orig.getNullOrdering()); element.setExpression(expression); return element; }
Example #29
Source File: SQLUtils.java From nimble-orm with GNU General Public License v2.0 | 5 votes |
/** * 往where sql里面插入AND关系的表达式。 * * 例如:whereSql为 where a!=3 or a!=2 limit 1 * condExpress为 deleted=0 * 那么返回:where (deleted=0 and (a!=3 or a!=2)) limit 1 * * @param whereSql 从where起的sql子句,如果有where必须带上where关键字。 * @param condExpression 例如a=? 不带where或and关键字。 * @return 注意返回字符串前面没有空格 * @throws JSQLParserException */ public static String insertWhereAndExpression(String whereSql, String condExpression) throws JSQLParserException { if(condExpression == null || condExpression.trim().isEmpty()) { return whereSql == null ? "" : whereSql; } if(whereSql == null || whereSql.trim().isEmpty()) { return "WHERE " + condExpression; } whereSql = whereSql.trim(); if(!whereSql.toUpperCase().startsWith("WHERE ")) { return "WHERE " + condExpression + " " + whereSql; } // 为解决JSqlParse对复杂的condExpression不支持的问题,这里用替换的形式来达到目的 String magic = "A" + UUID.randomUUID().toString().replace("-", ""); String selectSql = "select * from dual "; // 辅助where sql解析用 Statement statement = CCJSqlParserUtil.parse(selectSql + whereSql); Select selectStatement = (Select) statement; PlainSelect plainSelect = (PlainSelect)selectStatement.getSelectBody(); Expression ce = CCJSqlParserUtil.parseCondExpression(magic); Expression oldWhere = plainSelect.getWhere(); Expression newWhere = new FixedAndExpression(oldWhere, ce); plainSelect.setWhere(newWhere); String result = plainSelect.toString().substring(selectSql.length()); return result.replace(magic, condExpression); }
Example #30
Source File: SqlUtils.java From sql-to-mongo-db-query-converter with Apache License 2.0 | 5 votes |
public static List<String> getGroupByColumnReferences(PlainSelect plainSelect) { if (plainSelect.getGroupBy()==null) { return Collections.emptyList(); } return Lists.transform(plainSelect.getGroupBy().getGroupByExpressions(), new com.google.common.base.Function<Expression, String>() { @Override public String apply(@Nonnull Expression expression) { return SqlUtils.getStringValue(expression); } }); }