private Predicate<?> from(Part part, Iterator<Comparable<?>> iterator) { String property = part.getProperty().toDotPath(); Type type = part.getType(); boolean ignoreCase = (part.shouldIgnoreCase() != IgnoreCaseType.NEVER); Predicate<?> predicate = null; switch (type) { case TRUE: predicate = new EqualsPredicate(true); break; case FALSE: predicate = new EqualsPredicate(false); break; case SIMPLE_PROPERTY: if (ignoreCase) { predicate = new ILikePredicate(iterator.next().toString()); } else { predicate = new EqualsPredicate(iterator.next()); } break; case GREATER_THAN: case GREATER_THAN_EQUAL: case LESS_THAN: case LESS_THAN_EQUAL: predicate = fromInequalityVariant(type, ignoreCase, iterator); break; case LIKE: predicate = new LikePredicate(iterator.next().toString()); break; case IS_NULL: predicate = NullCheckPredicate.isNull(); break; case IS_NOT_NULL: predicate = NullCheckPredicate.notNull(); break; // TODO /* * case AFTER: case BEFORE: case BETWEEN: case CONTAINING: case * ENDING_WITH: case EXISTS: case IN: case NEAR: case * NEGATING_SIMPLE_PROPERTY: case NOT_CONTAINING: case NOT_IN: case * NOT_LIKE: case REGEX: case STARTING_WITH: case WITHIN: */ default: throw new InvalidDataAccessApiUsageException(String.format("Found invalid part '%s' in query", type)); } return PropertyPredicate.wrap(predicate, property); }
private String buildQueryCondition(boolean basic) { StringBuilder builder = new StringBuilder(); builder.append("<trim prefix=\" where \" prefixOverrides=\"and |or \">"); int c = 0; for (Iterator<PartTree.OrPart> iterator = tree.iterator(); iterator.hasNext(); ) { PartTree.OrPart orPart = iterator.next(); builder.append(" or ("); builder.append("<trim prefix=\"\" prefixOverrides=\"and |or \">"); for (Iterator<Part> it = orPart.iterator(); it.hasNext(); ) { String columnName = null; Part part = it.next(); MybatisPersistentProperty property = persistentEntity.getPersistentProperty(part.getProperty().getSegment()); if (null == property) { throw new MybatisQueryException("can not find property: " + part.getProperty().getSegment() + " from entity: " + persistentEntity.getName()); } if (!property.isEntity()) { columnName = quota(persistentEntity.getEntityName()) + "." + dialect.wrapColumnName(property.getColumnName()); } else if (!basic) { if (property.isAssociation()) { Association<MybatisPersistentProperty> ass = property.getAssociation(); if (ass instanceof MybatisManyToOneAssociation) { MybatisManyToOneAssociation association = (MybatisManyToOneAssociation) ass; MybatisPersistentEntity<?> obversePersistentEntity = association.getObversePersistentEntity(); if (null == obversePersistentEntity) { throw new MybatisQueryException("can not find obverse persistent entity."); } PropertyPath leaf = part.getProperty().getLeafProperty(); if (obversePersistentEntity.getType() == leaf.getType()) { //columnName = quota(persistentEntity.getEntityName() + "." + part.getProperty().getSegment()) + "." + dialect.wrapColumnName(obversePersistentEntity.getIdProperty().getColumnName()); throw new UnsupportedOperationException("findBy{Association Model} Style is not support now."); } else { MybatisPersistentProperty leafProperty = obversePersistentEntity.getPersistentProperty(leaf.getSegment()); if (null == leafProperty) { throw new MybatisQueryException("can not find property: " + leaf.getSegment() + " from entity: " + obversePersistentEntity.getName()); } columnName = quota(persistentEntity.getEntityName() + "." + part.getProperty().getSegment()) + "." + dialect.wrapColumnName(leafProperty.getColumnName()); } } else if (ass instanceof MybatisEmbeddedAssociation) { columnName = quota(persistentEntity.getEntityName()) + "." + dialect.wrapColumnName(ass.getObverse().getColumnName()); } } } if (null == columnName) { throw new MybatisQueryException("can not find property: " + part.getProperty().getSegment() + " in " + method.getName()); } builder.append(" and "); IgnoreCaseType ignoreCaseType = part.shouldIgnoreCase(); if (ignoreCaseType == ALWAYS || ignoreCaseType == WHEN_POSSIBLE) { builder.append("upper(").append(columnName).append(")"); } else { builder.append(columnName); } builder.append(generator.buildConditionOperate(part.getType())); String[] properties = new String[part.getType().getNumberOfArguments()]; for (int i = 0; i < properties.length; i++) { properties[i] = resolveParameterName(c++); } builder.append(generator.buildConditionCaluse(part.getType(), ignoreCaseType, properties)); } builder.append("</trim>"); builder.append(" )"); } builder.append("</trim>"); return builder.toString(); }
public String buildConditionCaluse(Type type, IgnoreCaseType ignoreCaseType, String[] properties) { StringBuilder builder = new StringBuilder(); switch (type) { case CONTAINING: case NOT_CONTAINING: if (ignoreCaseType == ALWAYS || ignoreCaseType == WHEN_POSSIBLE) { builder.append("concat('%',upper(#{" + properties[0] + "}),'%')"); } else { builder.append("concat('%',#{" + properties[0] + "},'%')"); } break; case STARTING_WITH: if (ignoreCaseType == ALWAYS || ignoreCaseType == WHEN_POSSIBLE) { builder.append("concat(upper(#{" + properties[0] + "}),'%')"); } else { builder.append("concat(#{" + properties[0] + "},'%')"); } break; case ENDING_WITH: if (ignoreCaseType == ALWAYS || ignoreCaseType == WHEN_POSSIBLE) { builder.append("concat('%',upper(#{" + properties[0] + "}))"); } else { builder.append("concat('%',#{" + properties[0] + "})"); } break; case IN: case NOT_IN: builder.append("<foreach item=\"item\" index=\"index\" collection=\"" + properties[0] + "\" open=\"(\" separator=\",\" close=\")\">#{item}</foreach>"); break; case IS_NOT_NULL: builder.append(" is not null"); break; case IS_NULL: builder.append(" is null"); break; case TRUE: builder.append("=true"); break; case FALSE: builder.append("=false"); break; default: if (ignoreCaseType == ALWAYS || ignoreCaseType == WHEN_POSSIBLE) { builder.append("upper(#{" + properties[0] + "})"); } else { builder.append("#{" + properties[0] + "}"); } break; } return builder.toString(); }
private static boolean isIgnoreCase(Part part) { return part.shouldIgnoreCase() != IgnoreCaseType.NEVER; }
private Predicate<?, ?> from(Part part, Iterator<Comparable<?>> iterator) { String property = part.getProperty().toDotPath(); Type type = part.getType(); boolean ignoreCase = (part.shouldIgnoreCase() != IgnoreCaseType.NEVER); switch (type) { case FALSE: case TRUE: return fromBooleanVariant(type, property); case SIMPLE_PROPERTY: return fromEqualityVariant(type, ignoreCase, property, iterator); case GREATER_THAN: case GREATER_THAN_EQUAL: case LESS_THAN: case LESS_THAN_EQUAL: return fromInequalityVariant(type, ignoreCase, property, iterator); case LIKE: return fromLikeVariant(type, property, iterator); case IS_NOT_NULL: case IS_NULL: return fromNullVariant(type, property); /* case AFTER: * case BEFORE: * case BETWEEN: * case CONTAINING: * case ENDING_WITH: * case EXISTS: * case IN: * case NEAR: * case NEGATING_SIMPLE_PROPERTY: * case NOT_CONTAINING: * case NOT_IN: * case NOT_LIKE: * case REGEX: * case STARTING_WITH: * case WITHIN: */ default: throw new InvalidDataAccessApiUsageException(String.format("Found invalid part '%s' in query", type)); } }
protected DynamoDBQueryCriteria<T, ID> addCriteria(DynamoDBQueryCriteria<T, ID> criteria, Part part, Iterator<Object> iterator) { if (part.shouldIgnoreCase().equals(IgnoreCaseType.ALWAYS)) throw new UnsupportedOperationException("Case insensitivity not supported"); Class<?> leafNodePropertyType = part.getProperty().getLeafProperty().getType(); PropertyPath leafNodePropertyPath = part.getProperty().getLeafProperty(); String leafNodePropertyName = leafNodePropertyPath.toDotPath(); if (leafNodePropertyName.indexOf(".") != -1) { int index = leafNodePropertyName.lastIndexOf("."); leafNodePropertyName = leafNodePropertyName.substring(index); } switch (part.getType()) { case IN: Object in = iterator.next(); Assert.notNull(in, "Creating conditions on null parameters not supported: please specify a value for '" + leafNodePropertyName + "'"); boolean isIterable = ClassUtils.isAssignable(in.getClass(), Iterable.class); boolean isArray = ObjectUtils.isArray(in); Assert.isTrue(isIterable || isArray, "In criteria can only operate with Iterable or Array parameters"); Iterable<?> iterable = isIterable ? ((Iterable<?>) in) : Arrays.asList(ObjectUtils.toObjectArray(in)); return criteria.withPropertyIn(leafNodePropertyName, iterable, leafNodePropertyType); case CONTAINING: return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.CONTAINS, iterator.next(), leafNodePropertyType); case STARTING_WITH: return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.BEGINS_WITH, iterator.next(), leafNodePropertyType); case BETWEEN: Object first = iterator.next(); Object second = iterator.next(); return criteria.withPropertyBetween(leafNodePropertyName, first, second, leafNodePropertyType); case AFTER: case GREATER_THAN: return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.GT, iterator.next(), leafNodePropertyType); case BEFORE: case LESS_THAN: return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.LT, iterator.next(), leafNodePropertyType); case GREATER_THAN_EQUAL: return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.GE, iterator.next(), leafNodePropertyType); case LESS_THAN_EQUAL: return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.LE, iterator.next(), leafNodePropertyType); case IS_NULL: return criteria.withNoValuedCriteria(leafNodePropertyName, ComparisonOperator.NULL); case IS_NOT_NULL: return criteria.withNoValuedCriteria(leafNodePropertyName, ComparisonOperator.NOT_NULL); case TRUE: return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.EQ, Boolean.TRUE, leafNodePropertyType); case FALSE: return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.EQ, Boolean.FALSE, leafNodePropertyType); case SIMPLE_PROPERTY: return criteria.withPropertyEquals(leafNodePropertyName, iterator.next(), leafNodePropertyType); case NEGATING_SIMPLE_PROPERTY: return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.NE, iterator.next(), leafNodePropertyType); default: throw new IllegalArgumentException("Unsupported keyword " + part.getType()); } }