/** * Creates a new {@link N1qlQuery} for the given class, this method build a query like: * * <pre> * select b.* from bucket b where b._class = 'model.Beer' * </pre> * * @param bucket Bucket name. * @param entityClass Entity class. * @return A query. */ public static GroupByPath from(final String bucket, final Class<?> entityClass) { String alias = String.valueOf(entityClass.getSimpleName().charAt(0)); return Select.select(alias + ".*") .from(i(bucket) + " " + alias) .where(path(alias, CLASS).eq(s(entityClass.getName()))); }
/** * Escape the given bucketName and produce an {@link Expression}. */ public static Expression escapedBucket(String bucketName) { return i(bucketName); }
/** * Escape the given bucketName and produce an {@link Expression}. */ public static Expression escapedBucket(String bucketName) { return i(bucketName); }
private static Expression expressionOrIdentifier(Object o) { if (o instanceof Expression) { return (Expression) o; } else if (o instanceof String) { return i((String) o); } else { throw new IllegalArgumentException("Fields for index must be either an Expression or a String identifier"); } }
public static InitialInsertPath insertInto(String bucket) { return insertInto(i(bucket)); }
public static DeleteUsePath deleteFrom(String bucket) { return deleteFrom(i(bucket)); }
public static MergeSourcePath mergeInto(String bucket) { return mergeInto(i(bucket)); }
public static UpdateUsePath update(String bucket) { return update(i(bucket)); }
public static InitialInsertPath upsertInto(String bucket) { return upsertInto(i(bucket)); }
/** * Creates the returning clause for N1ql deletes with all attributes of the entity and meta information * * @param bucketName the bucket that stores the entity documents (will be escaped). * @return the needed returning clause of the statement. */ public static Expression createReturningExpressionForDelete(String bucketName) { Expression fullEntity = path(i(bucketName), "*"); Expression metaId = path(meta(i(bucketName)), "id").as(SELECT_ID); Expression metaCas = path(meta(i(bucketName)), "cas").as(SELECT_CAS); List<Expression> expList = new ArrayList<Expression>(); expList.add(fullEntity); expList.add(metaId); expList.add(metaCas); StringBuilder sb = new StringBuilder(); for (Expression exp: expList) { if (sb.length() != 0) { sb.append(", "); } sb.append(exp.toString()); } return x(sb.toString()); }
/** * Creates the returning clause for N1ql deletes with all attributes of the entity and meta information * * @param bucketName the bucket that stores the entity documents (will be escaped). * @return the needed returning clause of the statement. */ public static Expression createReturningExpressionForDelete(String bucketName) { Expression fullEntity = path(i(bucketName), "*"); Expression metaId = path(meta(i(bucketName)), "id").as(SELECT_ID); Expression metaCas = path(meta(i(bucketName)), "cas").as(SELECT_CAS); List<Expression> expList = new ArrayList<Expression>(); expList.add(fullEntity); expList.add(metaId); expList.add(metaCas); StringBuilder sb = new StringBuilder(); for (Expression exp: expList) { if (sb.length() != 0) { sb.append(", "); } sb.append(exp.toString()); } return x(sb.toString()); }
public static N1qlSpelValues createN1qlSpelValues(String bucketName, String typeField, Class<?> typeValue, boolean isCount) { String b = "`" + bucketName + "`"; String entity = "META(" + b + ").id AS " + SELECT_ID + ", META(" + b + ").cas AS " + SELECT_CAS; String count = "COUNT(*) AS " + CountFragment.COUNT_ALIAS; String selectEntity; if (isCount) { selectEntity = "SELECT " + count + " FROM " + b; } else { selectEntity = "SELECT " + entity + ", " + b + ".* FROM " + b; } String typeSelection = "`" + typeField + "` = \"" + typeValue.getName() + "\""; String delete = deleteFrom(i(bucketName)).toString(); String returning = " returning " + N1qlUtils.createReturningExpressionForDelete(bucketName).toString(); return new N1qlSpelValues(selectEntity, entity, b, typeSelection, delete, returning); }
public static N1qlSpelValues createN1qlSpelValues(String bucketName, String typeField, Class<?> typeValue, boolean isCount) { String b = "`" + bucketName + "`"; String entity = "META(" + b + ").id AS " + SELECT_ID + ", META(" + b + ").cas AS " + SELECT_CAS; String count = "COUNT(*) AS " + CountFragment.COUNT_ALIAS; String selectEntity; if (isCount) { selectEntity = "SELECT " + count + " FROM " + b; } else { selectEntity = "SELECT " + entity + ", " + b + ".* FROM " + b; } String typeSelection = "`" + typeField + "` = \"" + typeValue.getName() + "\""; String delete = deleteFrom(i(bucketName)).toString(); String returning = " returning " + N1qlUtils.createReturningExpressionForDelete(bucketName).toString(); return new N1qlSpelValues(selectEntity, entity, b, typeSelection, delete, returning); }
private static Statement get(String bucket, String[] documents, int firstResult, int maxResult) { boolean hasFistResult = firstResult > 0; boolean hasMaxResult = maxResult > 0; if (hasFistResult && hasMaxResult) { return select(documents) .from(i(bucket)) .limit(maxResult) .offset(firstResult); } else if (hasFistResult) { return select(documents).from(i(bucket)).offset(firstResult); } else if (hasMaxResult) { return select(documents).from(i(bucket)).limit(maxResult); } return select(documents).from(i(bucket)); }
private static Statement get(String bucket, String[] documents, int firstResult, int maxResult, Sort[] sorts) { boolean hasFistResult = firstResult > 0; boolean hasMaxResult = maxResult > 0; if (hasFistResult && hasMaxResult) { return select(documents) .from(i(bucket)) .orderBy(sorts) .limit(maxResult) .offset(firstResult); } else if (hasFistResult) { return select(documents).from(i(bucket)).orderBy(sorts).offset(firstResult); } else if (hasMaxResult) { return select(documents).from(i(bucket)).orderBy(sorts).limit(maxResult); } return select(documents).from(i(bucket)).orderBy(sorts); }
private static Statement get(String bucket, String[] documents, int firstResult, int maxResult, Expression condition) { boolean hasFistResult = firstResult > 0; boolean hasMaxResult = maxResult > 0; if (hasFistResult && hasMaxResult) { return select(documents).from(i(bucket)) .where(condition) .limit(maxResult) .offset(firstResult); } else if (hasFistResult) { return select(documents).from(i(bucket)) .where(condition) .offset(firstResult); } else if (hasMaxResult) { return select(documents).from(i(bucket)).where(condition) .limit(maxResult); } return select(documents).from(i(bucket)).where(condition); }
/** * Produces an {@link Expression} that can serve as a WHERE clause criteria to only select documents in a bucket * that matches a particular Spring Data entity (as given by the {@link EntityMetadata} parameter). * * @param baseWhereCriteria the other criteria of the WHERE clause, or null if none. * @param converter the {@link CouchbaseConverter} giving the attribute storing the type information can be extracted. * @param entityInformation the expected type information. * @return an {@link Expression} to be used as a WHERE clause, that additionally restricts on the given type. */ public static Expression createWhereFilterForEntity(Expression baseWhereCriteria, CouchbaseConverter converter, EntityMetadata<?> entityInformation) { //add part that filters on type key String typeKey = converter.getTypeKey(); String typeValue = entityInformation.getJavaType().getName(); Expression typeSelector = i(typeKey).eq(s(typeValue)); if (baseWhereCriteria == null) { baseWhereCriteria = typeSelector; } else { baseWhereCriteria = x("(" + baseWhereCriteria.toString() + ")").and(typeSelector); } return baseWhereCriteria; }
@Override protected Statement getCount(ParameterAccessor accessor, Object[] runtimeParameters) { Expression bucket = i(getCouchbaseOperations().getCouchbaseBucket().name()); WherePath countFrom = select(count("*").as(CountFragment.COUNT_ALIAS)).from(bucket); N1qlCountQueryCreator queryCountCreator = new N1qlCountQueryCreator(partTree, accessor, countFrom, getCouchbaseOperations().getConverter(), getQueryMethod()); Statement statement = queryCountCreator.createQuery(); this.placeHolderValues = queryCountCreator.getPlaceHolderValues(); return statement; }
@Override protected Statement getCount(ParameterAccessor accessor, Object[] runtimeParameters) { Expression bucket = i(getCouchbaseOperations().getCouchbaseBucket().name()); WherePath countFrom = select(count("*").as(CountFragment.COUNT_ALIAS)).from(bucket); N1qlCountQueryCreator queryCountCreator = new N1qlCountQueryCreator(partTree, accessor, countFrom, getCouchbaseOperations().getConverter(), getQueryMethod()); Statement statement = queryCountCreator.createQuery(); this.placeHolderValues = queryCountCreator.getPlaceHolderValues(); return statement; }