public static <T, E> BooleanExpression doCreateCollectionExpression( PathBuilder<T> entityPath, String fieldName, Collection<E> values) { BooleanExpression expression = entityPath.get(fieldName).in(values); return expression; }
/** * {@inheritDoc} */ @Override public <T> BooleanExpression createStringExpression( PathBuilder<T> entityPath, String fieldName, Object searchObj, String operator) { if (StringUtils.equalsIgnoreCase(operator, OPERATOR_GOE)) { return entityPath.getString(fieldName).goe((String) searchObj); } else if (StringUtils.equalsIgnoreCase(operator, "gt")) { return entityPath.getString(fieldName).gt((String) searchObj); } else if (StringUtils.equalsIgnoreCase(operator, OPERATOR_LOE)) { return entityPath.getString(fieldName).loe((String) searchObj); } else if (StringUtils.equalsIgnoreCase(operator, "lt")) { return entityPath.getString(fieldName).lt((String) searchObj); } else if (StringUtils.equalsIgnoreCase(operator, "like")) { return entityPath.getString(fieldName).like((String) searchObj); } return entityPath.get(fieldName).eq(searchObj); }
/** * {@inheritDoc} */ @Override public <T> BooleanExpression createBooleanExpression( PathBuilder<T> entityPath, String fieldName, Object searchObj, String operator) { Boolean value = BooleanUtils.toBooleanObject((String) searchObj); if (value != null) { if (StringUtils.equalsIgnoreCase(operator, OPERATOR_GOE)) { return entityPath.getBoolean(fieldName).goe(value); } else if (StringUtils.equalsIgnoreCase(operator, "gt")) { return entityPath.getBoolean(fieldName).gt(value); } else if (StringUtils.equalsIgnoreCase(operator, OPERATOR_LOE)) { return entityPath.getBoolean(fieldName).loe(value); } else if (StringUtils.equalsIgnoreCase(operator, "lt")) { return entityPath.getBoolean(fieldName).lt(value); } } return entityPath.get(fieldName).eq(searchObj); }
PathBuilder<?> associationPath = entity.get(associationName); query = query.join(associationPath);
@Override public <T, E> BooleanExpression doCreateCollectionExpression( PathBuilder<T> entityPath, String fieldName, Collection<E> values) { BooleanExpression expression = entityPath.get(fieldName).in(values); return expression; }
return entityPath.getString(fieldName).like((String) searchObj); return entityPath.get(fieldName).eq(searchObj);
return entityPath.get(fieldName).eq(searchObj);
PathBuilder<?> associationPath = entity.get(associationName); query = query.join(associationPath);
default List<Component> findByCollectorTypeAndItemIdIn(CollectorType collectorType, List<ObjectId> collectorItemIds) { BooleanBuilder builder = new BooleanBuilder(); PathBuilder<Component> path = new PathBuilder<>(Component.class, "components"); builder.and(path.get("collectorItems", Map.class).get(collectorType.toString(),List.class).get("id", ObjectId.class).in(collectorItemIds)); return (List<Component>) findAll(builder.getValue()); } }
default Iterable<CollectorItem> findAllByOptionNameValueAndCollectorIdsIn(String optionName, String optionValue, List<ObjectId> collectorIds) { PathBuilder<CollectorItem> path = new PathBuilder<>(CollectorItem.class, "collectorItem"); BooleanBuilder builder = new BooleanBuilder(); builder.and(path.get("collectorId", ObjectId.class).in(collectorIds)); builder.and(path.get("options", Map.class).get(optionName, String.class).eq(optionValue)); return findAll(builder.getValue()); } }
default Iterable<CollectorItem> findAllByOptionNameValue(String optionName, String optionValue) { PathBuilder<CollectorItem> path = new PathBuilder<>(CollectorItem.class, "collectorItem"); BooleanBuilder builder = new BooleanBuilder(); builder.and(path.get("options", Map.class).get(optionName, String.class).eq(optionValue)); return findAll(builder.getValue()); }
public static PathBuilder<?> join(JPACommonQuery<?> query, PathBuilder<?> builder, Map<String, PathBuilder<?>> joins, String path) { PathBuilder<?> rv = joins.get(path); if (rv == null) { if (path.contains(".")) { String[] tokens = DOT.split(path); String[] parent = new String[tokens.length - 1]; System.arraycopy(tokens, 0, parent, 0, tokens.length - 1); String parentKey = StringUtils.join(parent, "."); builder = join(query, builder, joins, parentKey); rv = new PathBuilder(Object.class, StringUtils.join(tokens, "_")); query.leftJoin((EntityPath)builder.get(tokens[tokens.length - 1]), rv); } else { rv = new PathBuilder(Object.class, path); query.leftJoin((EntityPath)builder.get(path), rv); } joins.put(path, rv); } return rv; }
/** * Create a new Query-by-example query * * @param session * @param entity */ @SuppressWarnings("unchecked") public ExampleQuery(Configuration configuration, Session session, T entity) { this.session = Assert.notNull(session, "session"); this.entityPath = (PathBuilder) pathBuilderFactory.create(entity.getClass()); this.conditions = new BooleanBuilder(); BeanMap beanMap = new BeanMap(entity); MappedClass mappedClass = configuration.getMappedClass(entity.getClass()); for (MappedPath mappedPath : mappedClass.getProperties()) { MappedProperty<?> property = mappedPath.getMappedProperty(); Object value = property.getValue(beanMap); if (value != null // date/time values are skipped && !DATE_TIME_TYPES.contains(value.getClass()) // collection values are skipped && !property.isCollection() // map values are skipped && !property.isMap() // blank nodes are skipped && !(value instanceof BID)) { Expression<Object> propertyPath = (Expression) entityPath.get(property.getName(), property.getType()); conditions.and(ExpressionUtils.eqConst(propertyPath, value)); } } }
@Test public void SubQuery_Exists() throws Exception { query.from(user); query.where(new BeanSubQuery().from(user2).where(user2.get("firstName").eq(user.get("firstName"))).exists()); assertEquals("SELECT * WHERE { ?user ?_c2 ?_c3 . FILTER(exists {{ ?user2 ?_c2 ?_c3 . ?user ?_c5 ?user_firstName . ?user2 ?_c5 ?user_firstName } }) }"); }
@Test public void Id_Eq_Constant() throws Exception { query.from(user); query.where(user.get("id").eq(new UID(TEST.NS))); assertEquals("SELECT * WHERE { ?user ?_c2 ?_c3 }"); }
@Test public void Id_Ne_Constant() throws Exception { query.from(user); query.where(user.get("id").ne(new UID(TEST.NS))); assertEquals("SELECT * WHERE { ?user ?_c2 ?_c3 . FILTER(?user != ?_c4) }"); }
@Test public void Path_Column2() { PathBuilder<Entity> entity = new PathBuilder<Entity>(Entity.class,"entity"); Configuration conf = new Configuration(new MySQLTemplates()); NativeSQLSerializer serializer = new NativeSQLSerializer(conf, true); serializer.handle(entity.get("firstName")); assertEquals("entity.first_name", serializer.toString()); }
@Test public void Path_Column() { PathBuilder<Entity> entity = new PathBuilder<Entity>(Entity.class,"entity"); Configuration conf = new Configuration(new MySQLTemplates()); NativeSQLSerializer serializer = new NativeSQLSerializer(conf, true); serializer.handle(entity.get("name")); assertEquals("entity.name", serializer.toString()); }