static LookupStrategy chooseStrategy(PartTree tree, JdbcQueryMethod method) { if (tree.isCountProjection()) { return LookupStrategy.COUNT; } if (method.isCollectionQuery()) { return LookupStrategy.COLLECTION_QUERY; } if (method.isQueryForEntity()) { return LookupStrategy.SINGLE_QUERY; } if (method.isPageQuery()) { return LookupStrategy.PAGE_QUERY; } if (tree.isDelete()) { return LookupStrategy.UPDATE_QUERY; } if (tree.isExistsProjection()) { return LookupStrategy.EXISTS_QUERY; } throw new IllegalArgumentException("Don't know what lookupStrategy to follow!!"); }
protected PartTreeMybatisQuery( MybatisMappingContext context, SqlSessionTemplate sqlSessionTemplate, Dialect dialect, MybatisQueryMethod method) { super(sqlSessionTemplate, method); this.context = context; this.dialect = dialect; this.entityInformation = method.getEntityInformation(); this.domainClass = this.entityInformation.getJavaType(); this.tree = new PartTree(method.getName(), domainClass); this.parameters = method.getParameters(); this.persistentEntity = context.getPersistentEntity(domainClass); this.generator = new MybatisMapperGenerator(dialect, persistentEntity); this.statementName = super.getStatementName() + UUID.randomUUID().toString().replace("-", ""); doCreateQueryStatement(method); // prepare mybatis statement. }
private Predicate createPredicate(PathBuilder<?> builder, PartTree tree, BeanWrapper bean){ //BeanWrapper paramWrapper = PropertyAccessorFactory.forBeanPropertyAccess(param); BooleanExpression base = null; for (OrPart node : tree) { //logger.debug("OrPart : "+node.getClass()); BooleanExpression criteria = null; for (Part part : node) { //logger.debug("Part "+part.getClass()); if(part.getProperty() != null){ BooleanExpression newCriteria = create(builder, part, bean); if(newCriteria != null){ //logger.debug("ok...."); criteria = criteria == null ? newCriteria : and(criteria, newCriteria, bean); } } } base = base == null ? criteria : or(base, criteria, bean); } //logger.debug("base "+base); return base; }
private KeyValueQuery<SpelExpression> createQueryForMethodWithArgs(String methodName, Object... args) throws NoSuchMethodException, SecurityException { Class<?>[] argTypes = new Class<?>[args.length]; if (!ObjectUtils.isEmpty(args)) { for (int i = 0; i < args.length; i++) { argTypes[i] = args[i].getClass(); } } Method method = PersonRepository.class.getMethod(methodName, argTypes); doReturn(Person.class).when(metadataMock).getReturnedDomainClass(method); PartTree partTree = new PartTree(method.getName(), method.getReturnType()); SpelQueryCreator creator = new SpelQueryCreator(partTree, new ParametersParameterAccessor( new QueryMethod(method, metadataMock, new SpelAwareProxyProjectionFactory()).getParameters(), args)); KeyValueQuery<SpelExpression> q = creator.createQuery(); q.getCriteria().setEvaluationContext(new StandardEvaluationContext(args)); return q; }
private static SpelCriteria createQueryForMethodWithArgs(String methodName, Object... args) throws Exception { List<Class<?>> types = new ArrayList<>(args.length); for (Object arg : args) { types.add(arg.getClass()); } Method method = PersonRepository.class.getMethod(methodName, types.toArray(new Class<?>[types.size()])); RepositoryMetadata metadata = mock(RepositoryMetadata.class); doReturn(method.getReturnType()).when(metadata).getReturnedDomainClass(method); PartTree partTree = new PartTree(method.getName(), method.getReturnType()); SpelQueryCreator creator = new SpelQueryCreator(partTree, new ParametersParameterAccessor( new QueryMethod(method, metadata, new SpelAwareProxyProjectionFactory()).getParameters(), args)); return new SpelCriteria(creator.createQuery().getCriteria(), new StandardEvaluationContext(args)); }
@Override protected RepositoryQuery resolveQuery(JdbcQueryMethod method, NamedQueries namedQueries) { PartTree tree = new PartTree(method.getName(), method.getEntityInformation().getJavaType()); JdbcByNameRepositoryQueryCreator creator = new JdbcByNameRepositoryQueryCreator(method, tree, generator, template, tableDescription, rowMapper); return creator.createQuery(); }
public JdbcByNameRepositoryQueryCreator(JdbcQueryMethod method, PartTree tree, SqlGenerator builder, NamedParameterJdbcOperations template, TableDescription tableDescription, @SuppressWarnings("rawtypes") RowMapper rowMapper) { super(tree, ReflectionMethodsUtils.generateParameters(method)); this.tree = tree; this.method = method; this.builder = builder; this.template = template; this.rowMapper = rowMapper; this.tableDescription = tableDescription; this.lookupStrategy = LookupStrategyFactory.chooseStrategy(tree, method); }
public PartTreeDocumentDbQuery(DocumentDbQueryMethod method, DocumentDbOperations operations) { super(method, operations); this.processor = method.getResultProcessor(); this.tree = new PartTree(method.getName(), processor.getReturnedType().getDomainType()); this.mappingContext = operations.getConverter().getMappingContext(); }
public DocumentDbQueryCreator(PartTree tree, DocumentDbParameterAccessor accessor, MappingContext<?, DocumentDbPersistentProperty> mappingContext) { super(tree, accessor); this.accessor = accessor; this.mappingContext = mappingContext; }
private TarantoolQueryCreator createQueryCreatorForMethodWithArgs(Method method, Object[] args) { PartTree partTree = new PartTree(method.getName(), method.getReturnType()); TarantoolQueryCreator creator = new TarantoolQueryCreator(partTree, new ParametersParameterAccessor(new DefaultParameters( method), args)); return creator; }
private GcloudDatastoreQueryCreator createCreator(Class<?> repositoryClass, Method method, Object... values) { QueryMethod queryMethod = new QueryMethod(method, new DefaultRepositoryMetadata(repositoryClass), new SpelAwareProxyProjectionFactory()); return new GcloudDatastoreQueryCreator( new PartTree(method.getName(), queryMethod.getResultProcessor().getReturnedType() .getDomainType()), new ParametersParameterAccessor(queryMethod.getParameters(), values), DatastoreOptions.getDefaultInstance()); }
protected Object doExecute(Object[] parameters) { Class<?> domainClass = queryMethod.getEntityInformation().getJavaType(); int paramIndex = 0; PartTree tree = new PartTree(getQueryMethod().getName(), getQueryMethod().getEntityInformation().getJavaType()); System.out.println(tree); List<Filter> orFilters = new ArrayList<Filter>(); for (Iterator<OrPart> orPartIter = tree.iterator(); orPartIter.hasNext();) { OrPart orPart = orPartIter.next(); List<Filter> andFilters = new ArrayList<Filter>(); for (Iterator<Part> partIter = orPart.iterator(); partIter.hasNext();) { Part part = partIter.next(); PropertyPath propertyPath = part.getProperty(); String propName = propertyPath.getSegment(); Object propValue = parameters[paramIndex++]; FilterOperator operator = getFilterOperation(part); Filter filter = new Query.FilterPredicate(propName, operator, propValue); andFilters.add(filter); } if (andFilters.size() == 1) { orFilters.add(andFilters.get(0)); } else if (andFilters.size() >= 2){ orFilters.add(CompositeFilterOperator.and(andFilters)); } } com.googlecode.objectify.cmd.Query q = ofy().load().type(domainClass); if (orFilters.size() == 1) { q = q.filter(orFilters.get(0)); } else if (orFilters.size() >= 2){ q = q.filter(CompositeFilterOperator.or(orFilters)); } return q.list(); }
/** * Creates a new {@link PartTreeEbeanQuery}. * * @param method must not be {@literal null}. * @param ebeanServer must not be {@literal null}. */ public PartTreeEbeanQuery(EbeanQueryMethod method, EbeanServer ebeanServer) { super(method, ebeanServer); this.domainClass = method.getEntityInformation().getJavaType(); this.tree = new PartTree(method.getName(), domainClass); this.parameters = (DefaultParameters) method.getParameters(); this.queryPreparer = new QueryPreparer(ebeanServer); }
/** * Create a new {@link EbeanQueryCreator}. * * @param tree must not be {@literal null}. * @param type must not be {@literal null}. * @param expressionList must not be {@literal null}. * @param provider must not be {@literal null}. */ public EbeanQueryCreator(PartTree tree, ReturnedType type, ExpressionList expressionList, ParameterMetadataProvider provider) { super(tree); this.tree = tree; this.root = expressionList; this.provider = provider; this.returnedType = type; }
/** * Create a new {@link VaultQueryCreator} given {@link PartTree} and * {@link ParameterAccessor}. * * @param tree must not be {@literal null}. * @param parameters must not be {@literal null}. * @param mappingContext must not be {@literal null}. */ public VaultQueryCreator( PartTree tree, ParameterAccessor parameters, MappingContext<VaultPersistentEntity<?>, VaultPersistentProperty> mappingContext) { super(tree, parameters); this.mappingContext = mappingContext; }
VaultQuery createQuery(String methodName, String value) { DefaultParameters defaultParameters = new DefaultParameters( ReflectionUtils.findMethod(dummy.class, "someUnrelatedMethod", String.class)); PartTree partTree = new PartTree(methodName, Credentials.class); VaultQueryCreator queryCreator = new VaultQueryCreator( partTree, new ParametersParameterAccessor(defaultParameters, new Object[] { value }), mappingContext); return queryCreator.createQuery().getCriteria(); }
VaultQuery createQuery(String methodName, List<String> value) { DefaultParameters defaultParameters = new DefaultParameters( ReflectionUtils .findMethod(dummy.class, "someUnrelatedMethod", List.class)); PartTree partTree = new PartTree(methodName, Credentials.class); VaultQueryCreator queryCreator = new VaultQueryCreator( partTree, new ParametersParameterAccessor(defaultParameters, new Object[] { value }), mappingContext); return queryCreator.createQuery().getCriteria(); }
VaultQuery createQuery(String methodName, String value, String anotherValue) { DefaultParameters defaultParameters = new DefaultParameters( ReflectionUtils.findMethod(dummy.class, "someUnrelatedMethod", String.class, String.class)); PartTree partTree = new PartTree(methodName, Credentials.class); VaultQueryCreator queryCreator = new VaultQueryCreator(partTree, new ParametersParameterAccessor(defaultParameters, new Object[] { value, anotherValue }), mappingContext); return queryCreator.createQuery().getCriteria(); }
public QueryMethodResolver(PathBuilder<T> pathBuilder, Object queryMethod){ String queryMethodName = QueryMethodUtil.methodName(queryMethod); logger.warn(" QueryDsl: "+queryMethodName); PartTree tree = new PartTree(queryMethodName, pathBuilder.getType()); BeanWrapper bean = PropertyAccessorFactory.forBeanPropertyAccess(queryMethod); this.predicate = createPredicate(pathBuilder, tree, bean); this.sort = tree.getSort(); }
private Query createQueryForMethodWithArgs(Method method, Object[] args) { PartTree partTree = new PartTree(method.getName(), method.getReturnType()); SolrQueryMethod queryMethod = new SolrQueryMethod(method, metadataMock, entityInformationCreatorMock); SolrQueryCreator creator = new SolrQueryCreator(partTree, new SolrParametersParameterAccessor(queryMethod, args), mappingContext); return creator.createQuery(); }
/** * <P> * Determine if the arguments to the method need reordered. * </P> * <P> * For searches such as {@code findBySomethingNotNull} there may be more parts than parameters needed to be bound to * them. * </P> * * @param partTree Query parts * @param bindableParameters Parameters expected */ @SuppressWarnings("unchecked") private void prepareRearrange(final PartTree partTree, final Parameters<?, ?> bindableParameters) { this.isRearrangeRequired = false; if (partTree == null || bindableParameters == null) { return; } List<String> queryParams = new ArrayList<>(); List<String> methodParams = new ArrayList<>(); Iterator<Part> partTreeIterator = partTree.getParts().iterator(); while (partTreeIterator.hasNext()) { Part part = partTreeIterator.next(); queryParams.add(part.getProperty().getSegment()); } Iterator<Parameter> bindableParameterIterator = (Iterator<Parameter>) bindableParameters.iterator(); while (bindableParameterIterator.hasNext()) { Parameter parameter = bindableParameterIterator.next(); methodParams.add(parameter.getName()); } this.rearrangeIndex = new int[queryParams.size()]; String[] paramsExpected = queryParams.toArray(new String[queryParams.size()]); String[] paramsProvided = methodParams.toArray(new String[methodParams.size()]); for (int i = 0; i < this.rearrangeIndex.length; i++) { this.rearrangeIndex[i] = i; for (int j = 0; j < paramsProvided.length; j++) { if (paramsProvided[j] != null && paramsProvided[j].equals(paramsExpected[i])) { this.rearrangeIndex[i] = j; this.isRearrangeRequired = true; } } } }
/** * Creates a new {@link HazelcastQueryCreator} for the given {@link PartTree}. * * @param tree must not be {@literal null}. */ public HazelcastQueryCreator(PartTree tree) { super(tree); if (tree.isLimiting() && tree.getMaxResults() > 0) { this.limit = tree.getMaxResults(); } else { this.limit = 0; } }
/** * Creates a new {@link HazelcastQueryCreator} for the given {@link PartTree} and {@link ParameterAccessor}. The * latter is used to hand actual parameter values into the callback methods as well as to apply dynamic sorting via a * {@link Sort} parameter. * * @param tree must not be {@literal null}. * @param parameters can be {@literal null}. */ public HazelcastQueryCreator(PartTree tree, ParameterAccessor parameters) { super(tree, parameters); if (tree.isLimiting() && tree.getMaxResults() > 0) { this.limit = tree.getMaxResults(); } else { this.limit = 0; } }
/** * Create a {@link KeyValueQuery} given {@link ParameterAccessor}. * * @param accessor must not be {@literal null}. * @return the {@link KeyValueQuery}. */ public KeyValueQuery<?> createQuery(ParameterAccessor accessor) { PartTree tree = new PartTree(getQueryMethod().getName(), getQueryMethod().getEntityInformation().getJavaType()); AbstractQueryCreator<? extends KeyValueQuery<?>, ?> queryCreator = queryCreatorFactory.queryCreatorFor(tree, accessor); KeyValueQuery<?> query = queryCreator.createQuery(); if (tree.isLimiting()) { query.setRows(tree.getMaxResults()); } return query; }
@Override public AbstractQueryCreator<KeyValueQuery<?>, ?> queryCreatorFor(PartTree partTree, ParameterAccessor accessor) { Assert.state(constructor != null, () -> String.format("No constructor (PartTree, ParameterAccessor) could be found on type %s!", type)); return (AbstractQueryCreator<KeyValueQuery<?>, ?>) BeanUtils.instantiateClass(constructor, partTree, accessor); }
public AbstractDynamoDBQueryCreator(PartTree tree, ParameterAccessor parameterAccessor, DynamoDBEntityInformation<T, ID> entityMetadata, DynamoDBOperations dynamoDBOperations) { super(tree, parameterAccessor); this.entityMetadata = entityMetadata; this.dynamoDBOperations = dynamoDBOperations; }
public DynamoDBCountQueryCreator(PartTree tree, ParameterAccessor parameterAccessor, DynamoDBEntityInformation<T, ID> entityMetadata, DynamoDBOperations dynamoDBOperations,boolean pageQuery) { super(tree, parameterAccessor, entityMetadata, dynamoDBOperations); this.pageQuery = pageQuery; }
/** * Convert a {@link PartTree} into a where query alike to the one present in the * {@link Query}'s where property. */ public static String toIndexedQuery(final PartTree tree) { final StringBuilder result = new StringBuilder(); final Iterator<OrPart> orIt = tree.iterator(); while(orIt.hasNext()) { final OrPart orPart = orIt.next(); final Iterator<Part> partIt = orPart.iterator(); while(partIt.hasNext()) { final Part part = partIt.next(); result.append(" " + part.getProperty().getSegment() + " "); result.append(convertOperator(part.getType())); if(partIt.hasNext()) { result.append(" AND "); } } if(orIt.hasNext()) { result.append(" OR "); } } return StringUtil.removeExtraSpaces(result.toString()); }
public SimpleDbPartTreeQueryMethod(Method method, RepositoryMetadata metadata, SimpleDbDomain simpleDbDomain) { super(method, metadata, simpleDbDomain); final String domainName = simpleDbDomain.getDomain(metadata.getDomainType()); final SimpleDbEntityInformation<?, ?> entityInformation = SimpleDbEntityInformationSupport.getMetadata(metadata.getDomainType(), domainName); whereExpression = PartTreeConverter.toIndexedQuery(new PartTree(method.getName(), entityInformation.getJavaType())); }
@Test public void should_create_corect_query_for_simple_property() { final String methodName = "findByItemName"; final PartTree tree = new PartTree(methodName, SimpleDbSampleEntity.class); final String query = PartTreeConverter.toIndexedQuery(tree); final String expected = " itemName = ? "; assertEquals(expected, query); }
@Test public void should_create_corect_query_for_between() { final String methodName = "readByAgeBetween"; final PartTree tree = new PartTree(methodName, SimpleDbSampleEntity.class); final String query = PartTreeConverter.toIndexedQuery(tree); final String expected = " age BETWEEN ? and ? "; assertEquals(expected, query); }
@Test public void should_create_corect_query_for_complex_operators() { final String methodName = "getByItemNameLikeOrAgeGreaterThanAndAgeLessThan"; final PartTree tree = new PartTree(methodName, SimpleDbSampleEntity.class); final String query = PartTreeConverter.toIndexedQuery(tree); final String expected = " itemName LIKE ? OR age > ? AND age < ? "; assertEquals(expected, query); }
@Test(expected = MappingException.class) public void shoud_fail_for_unsupported_operator() { final String methodName = "readByAgeEndsWith"; final PartTree tree = new PartTree(methodName, SimpleDbSampleEntity.class); PartTreeConverter.toIndexedQuery(tree); }
public PartTree getTree() { return this.tree; }
public TarantoolQueryCreator(PartTree tree, ParameterAccessor parameters) { super(tree, parameters); }
public SnowdropQueryCreator(Class<?> entityClass, PartTree tree, ParameterAccessor parameters, MappingContext<?, SnowdropPersistentProperty> context) { super(tree, parameters); this.entityClass = entityClass; this.context = context; }
public SnowdropPartQuery(SnowdropQueryMethod queryMethod, SnowdropOperations snowdropOperations) { super(queryMethod, snowdropOperations); this.tree = new PartTree(queryMethod.getName(), queryMethod.getEntityInformation().getJavaType()); this.mappingContext = snowdropOperations.getMappingContext(); }
public GcloudDatastoreQueryCreator(PartTree tree, ParameterAccessor accessor, DatastoreOptions datastoreOptions) { super(tree, accessor); this.datastoreOptions = datastoreOptions; }
public QueryCreator(PartTree tree) { super(tree); }
public QueryCreator(PartTree tree, ParameterAccessor parameters) { super(tree, parameters); }