/** * Append several elements to the collection column of a row */ @Override public void append(CassandraSessionPool.Session session, CassandraBackendEntry.Row entry) { List<HugeKeys> idNames = this.idColumnName(); List<HugeKeys> colNames = this.modifiableColumnName(); Map<HugeKeys, Object> columns = entry.columns(); Update update = QueryBuilder.update(table()); for (HugeKeys key : colNames) { if (!columns.containsKey(key)) { continue; } String name = formatKey(key); Object value = columns.get(key); if (value instanceof Map) { update.with(QueryBuilder.putAll(name, (Map<?, ?>) value)); } else if (value instanceof List) { update.with(QueryBuilder.appendAll(name, (List<?>) value)); } else { update.with(QueryBuilder.append(name, value)); } } for (HugeKeys idName : idNames) { assert columns.containsKey(idName); update.where(formatEQ(idName, columns.get(idName))); } session.add(update); }
/** * Append data to label index table */ private static void appendLabelIndex(CassandraSessionPool.Session session, String table, CassandraBackendEntry.Row entry) { Update update = QueryBuilder.update(table); update.with(QueryBuilder.append(ELEMENT_IDS, IdUtil.writeString(entry.id()))); update.where(CassandraTable.formatEQ(HugeKeys.LABEL, entry.column(HugeKeys.LABEL))); session.add(update); }
.with() .and(prepend("b", 3)) .and(append("c", "a")) .and(appendAll("d", Arrays.asList(1, 2, 3))) .and(discard("e", 1));
@Test(groups = "unit", expectedExceptions = InvalidQueryException.class) public void should_not_allow_bind_marker_for_append() { update("foo").with(append("l", bindMarker())).where(eq("k", 1)); }
/** * Append several elements to the collection column of a row */ @Override public void append(CassandraSessionPool.Session session, CassandraBackendEntry.Row entry) { List<HugeKeys> idNames = this.idColumnName(); List<HugeKeys> colNames = this.modifiableColumnName(); Map<HugeKeys, Object> columns = entry.columns(); Update update = QueryBuilder.update(table()); for (HugeKeys key : colNames) { if (!columns.containsKey(key)) { continue; } String name = formatKey(key); Object value = columns.get(key); if (value instanceof Map) { update.with(QueryBuilder.putAll(name, (Map<?, ?>) value)); } else if (value instanceof List) { update.with(QueryBuilder.appendAll(name, (List<?>) value)); } else { update.with(QueryBuilder.append(name, value)); } } for (HugeKeys idName : idNames) { assert columns.containsKey(idName); update.where(formatEQ(idName, columns.get(idName))); } session.add(update); }
@Override public UpdateBuilder<T> addList(String name, Object value) { update.with(QueryBuilder.append(classBean.toColumn(name), value)); return this; } @Override
public static BoundStatement prepareAppendItemToCollection(Object id, Class<?> clazz, String propertyName, Object item, WriteOptions options, String keyspace, Session session) { EntityTypeMetadata emeta = EntityTypeParser.getEntityMetadata(clazz); EntityFieldMetaData fmeta = emeta.getFieldMetadata(propertyName); Update update = QueryBuilder.update(keyspace, emeta.getTableName()); if (item instanceof Set<?> && fmeta.getType() == Set.class) { Set<?> set = (Set<?>) item; if (set.size() == 0) return null; update.with(QueryBuilder.addAll(fmeta.getColumnName(), set)); } else if (item instanceof List<?> && fmeta.getType() == List.class) { List<?> list = (List<?>) item; if (list.size() == 0) return null; update.with(QueryBuilder.appendAll(fmeta.getColumnName(), list)); } else if (item instanceof Map<?, ?>) { Map<?, ?> map = (Map<?, ?>) item; if (map.size() == 0) return null; update.with(QueryBuilder.putAll(fmeta.getColumnName(), map)); } else if (fmeta.getType() == Set.class) { update.with(QueryBuilder.add(fmeta.getColumnName(), item)); } else if (fmeta.getType() == List.class) { update.with(QueryBuilder.append(fmeta.getColumnName(), item)); } applyOptions(options, update, null); return prepareUpdate(id, emeta, update, session); }
QueryBuilder qb = new QueryBuilder(); qb.select("img"); qb.from("Image", "img"); qb.join("img.pixels", "pix", true, false); // Can't join anymore after this qb.where(); // First qb.append("("); qb.and("pt.details.creationTime > :time"); qb.param("time", new Date()); qb.append(")"); qb.and("img.id in (:ids)"); qb.paramList("ids", new HashSet()); qb.order("img.id", true); qb.order("this.details.creationEvent.time", false);
.with() .and(prepend("b", 3)) .and(append("c", "a")) .and(appendAll("d", Arrays.asList(1, 2, 3))) .and(discard("e", 1));
private ImmutableList<BuiltStatement> nonIdempotentBuiltStatements() { return ImmutableList.of( update("foo").with(append("l", 1)).where(eq("k", 1)), // append to list update("foo").with(set("v", 1)).and(prepend("l", 1)).where(eq("k", 1)), // prepend to list update("foo").with(incr("c")).where(eq("k", 1)), // counter update
/** * Append data to label index table */ private static void appendLabelIndex(CassandraSessionPool.Session session, String table, CassandraBackendEntry.Row entry) { Update update = QueryBuilder.update(table); update.with(QueryBuilder.append(ELEMENT_IDS, IdUtil.writeString(entry.id()))); update.where(CassandraTable.formatEQ(HugeKeys.LABEL, entry.column(HugeKeys.LABEL))); session.add(update); }
@Test(groups = "unit", expectedExceptions = InvalidQueryException.class) public void should_not_allow_bind_marker_for_append() { update("foo").with(append("l", bindMarker())).where(eq("k", 1)); }
private ImmutableList<BuiltStatement> nonIdempotentBuiltStatements() { return ImmutableList.of( update("foo").with(append("l", 1)).where(eq("k", 1)), // append to list update("foo").with(set("v", 1)).and(prepend("l", 1)).where(eq("k", 1)), // prepend to list update("foo").with(incr("c")).where(eq("k", 1)), // counter update