use of org.springframework.data.repository.query.parser.Part in project ignite by apache.
the class IgniteQueryGenerator method generateSql.
/**
* @param mtd Method.
* @param metadata Metadata.
*/
@NotNull
public static IgniteQuery generateSql(Method mtd, RepositoryMetadata metadata) {
PartTree parts = new PartTree(mtd.getName(), metadata.getDomainType());
StringBuilder sql = new StringBuilder();
if (parts.isDelete())
throw new UnsupportedOperationException("DELETE clause is not supported now.");
else {
sql.append("SELECT ");
if (parts.isDistinct())
throw new UnsupportedOperationException("DISTINCT clause in not supported.");
if (parts.isCountProjection())
sql.append("COUNT(1) ");
else
sql.append(" * ");
}
sql.append("FROM ").append(metadata.getDomainType().getSimpleName());
if (parts.iterator().hasNext()) {
sql.append(" WHERE ");
for (PartTree.OrPart orPart : parts) {
sql.append("(");
for (Part part : orPart) {
handleQueryPart(sql, part);
sql.append(" AND ");
}
sql.delete(sql.length() - 5, sql.length());
sql.append(") OR ");
}
sql.delete(sql.length() - 4, sql.length());
}
addSorting(sql, parts.getSort());
if (parts.isLimiting()) {
sql.append(" LIMIT ");
sql.append(parts.getMaxResults().intValue());
}
return new IgniteQuery(sql.toString(), parts.isCountProjection(), getOptions(mtd));
}
use of org.springframework.data.repository.query.parser.Part in project spring-data-mongodb by spring-projects.
the class MongoQueryCreator method from.
/**
* Populates the given {@link CriteriaDefinition} depending on the {@link Part} given.
*
* @param part
* @param property
* @param criteria
* @param parameters
* @return
*/
private Criteria from(Part part, MongoPersistentProperty property, Criteria criteria, Iterator<Object> parameters) {
Type type = part.getType();
switch(type) {
case AFTER:
case GREATER_THAN:
return criteria.gt(parameters.next());
case GREATER_THAN_EQUAL:
return criteria.gte(parameters.next());
case BEFORE:
case LESS_THAN:
return criteria.lt(parameters.next());
case LESS_THAN_EQUAL:
return criteria.lte(parameters.next());
case BETWEEN:
return criteria.gt(parameters.next()).lt(parameters.next());
case IS_NOT_NULL:
return criteria.ne(null);
case IS_NULL:
return criteria.is(null);
case NOT_IN:
return criteria.nin(nextAsArray(parameters));
case IN:
return criteria.in(nextAsArray(parameters));
case LIKE:
case STARTING_WITH:
case ENDING_WITH:
case CONTAINING:
return createContainingCriteria(part, property, criteria, parameters);
case NOT_LIKE:
return createContainingCriteria(part, property, criteria.not(), parameters);
case NOT_CONTAINING:
return createContainingCriteria(part, property, criteria.not(), parameters);
case REGEX:
return criteria.regex(parameters.next().toString());
case EXISTS:
return criteria.exists((Boolean) parameters.next());
case TRUE:
return criteria.is(true);
case FALSE:
return criteria.is(false);
case NEAR:
Range<Distance> range = accessor.getDistanceRange();
Optional<Distance> distance = range.getUpperBound().getValue();
Optional<Distance> minDistance = range.getLowerBound().getValue();
Point point = accessor.getGeoNearLocation();
Point pointToUse = point == null ? nextAs(parameters, Point.class) : point;
boolean isSpherical = isSpherical(property);
return distance.map(it -> {
if (isSpherical || !Metrics.NEUTRAL.equals(it.getMetric())) {
criteria.nearSphere(pointToUse);
} else {
criteria.near(pointToUse);
}
criteria.maxDistance(it.getNormalizedValue());
minDistance.ifPresent(min -> criteria.minDistance(min.getNormalizedValue()));
return criteria;
}).orElseGet(() -> isSpherical ? criteria.nearSphere(pointToUse) : criteria.near(pointToUse));
case WITHIN:
Object parameter = parameters.next();
return criteria.within((Shape) parameter);
case SIMPLE_PROPERTY:
return isSimpleComparisionPossible(part) ? criteria.is(parameters.next()) : createLikeRegexCriteriaOrThrow(part, property, criteria, parameters, false);
case NEGATING_SIMPLE_PROPERTY:
return isSimpleComparisionPossible(part) ? criteria.ne(parameters.next()) : createLikeRegexCriteriaOrThrow(part, property, criteria, parameters, true);
default:
throw new IllegalArgumentException("Unsupported keyword!");
}
}
use of org.springframework.data.repository.query.parser.Part in project spring-data-mongodb by spring-projects.
the class IndexEnsuringQueryCreationListener method onCreation.
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.support.QueryCreationListener#onCreation(org.springframework.data.repository.query.RepositoryQuery)
*/
public void onCreation(PartTreeMongoQuery query) {
PartTree tree = query.getTree();
if (!tree.hasPredicate()) {
return;
}
Index index = new Index();
index.named(query.getQueryMethod().getName());
Sort sort = tree.getSort();
for (Part part : tree.getParts()) {
if (GEOSPATIAL_TYPES.contains(part.getType())) {
return;
}
String property = part.getProperty().toDotPath();
Direction order = toDirection(sort, property);
index.on(property, order);
}
// Add fixed sorting criteria to index
if (sort.isSorted()) {
for (Order order : sort) {
index.on(order.getProperty(), order.getDirection());
}
}
MongoEntityMetadata<?> metadata = query.getQueryMethod().getEntityInformation();
indexOperationsProvider.indexOps(metadata.getCollectionName()).ensureIndex(index);
LOG.debug(String.format("Created %s!", index));
}
Aggregations