Refine search
/** * Factory method for a force-quoted {@link CqlIdentifier}. * * @since 2.0. */ public static CqlIdentifier quoted(CharSequence identifier) { return new CqlIdentifier(identifier, true); }
@Override public void setForceQuote(boolean forceQuote) { boolean changed = !Boolean.valueOf(forceQuote).equals(this.forceQuote); this.forceQuote = forceQuote; if (changed) { setTableName(of(getTableName().getUnquoted(), forceQuote)); } }
/** * Create a new CQL identifier, optionally force-quoting it. Force-quoting can be used to preserve identifier case. * <ul> * <li>If the given identifier is a legal quoted identifier or {@code forceQuote} is <code>true</code>, * {@link #isQuoted()} will return {@code true} and the identifier will be quoted when rendered.</li> * <li>If the given identifier is a legal unquoted identifier, {@link #isQuoted()} will return {@code false}, plus the * name will be converted to lower case and rendered as such.</li> * <li>If the given identifier is illegal, an {@link IllegalArgumentException} is thrown.</li> * </ul> * * @see #of(CharSequence, boolean) * @see #quoted(CharSequence) */ private CqlIdentifier(CharSequence identifier, boolean forceQuote) { Assert.notNull(identifier, "Identifier must not be null"); String string = identifier.toString(); Assert.hasText(string, "Identifier must not be empty"); if (forceQuote || isQuotedIdentifier(string)) { this.unquoted = string; this.identifier = "\"" + string + "\""; this.quoted = true; } else if (isUnquotedIdentifier(string)) { this.identifier = this.unquoted = string.toLowerCase(); this.quoted = false; } else { throw new IllegalArgumentException( String.format("given string [%s] is not a valid quoted or unquoted identifier", identifier)); } }
@Override public boolean deleteById(Object id, Class<?> entityClass) { Assert.notNull(id, "Id must not be null"); Assert.notNull(entityClass, "Entity type must not be null"); CassandraPersistentEntity<?> entity = getRequiredPersistentEntity(entityClass); CqlIdentifier tableName = entity.getTableName(); Delete delete = QueryBuilder.delete().from(tableName.toCql()); getConverter().write(id, delete.where(), entity); maybeEmitEvent(new BeforeDeleteEvent<>(delete, entityClass, tableName)); boolean result = getCqlOperations().execute(delete); maybeEmitEvent(new AfterDeleteEvent<>(delete, entityClass, tableName)); return result; }
@Override public void truncate(Class<?> entityClass) { Assert.notNull(entityClass, "Entity type must not be null"); CqlIdentifier tableName = getTableName(entityClass); Truncate truncate = QueryBuilder.truncate(tableName.toCql()); maybeEmitEvent(new BeforeDeleteEvent<>(truncate, entityClass, tableName)); getCqlOperations().execute(truncate); maybeEmitEvent(new AfterDeleteEvent<>(truncate, entityClass, tableName)); }
@Override public Optional<TableMetadata> getTableMetadata(String keyspace, CqlIdentifier tableName) { Assert.hasText(keyspace, "Keyspace name must not be empty"); Assert.notNull(tableName, "Table name must not be null"); return Optional.ofNullable(getCqlOperations().execute((SessionCallback<TableMetadata>) session -> session .getCluster().getMetadata().getKeyspace(keyspace).getTable(tableName.toCql()))); }
private Collection<Clause> getWhereClauses(MapId id, CassandraPersistentEntity<?> entity) { Assert.notNull(id, "MapId must not be null"); Collection<Clause> clauses = new ArrayList<>(); for (Entry<String, Object> entry : id.entrySet()) { CassandraPersistentProperty persistentProperty = entity.getPersistentProperty(entry.getKey()); if (persistentProperty == null) { throw new IllegalArgumentException(String.format( "MapId contains references [%s] that is an unknown property of [%s]", entry.getKey(), entity.getName())); } Object writeValue = getWriteValue(entry.getValue(), persistentProperty.getTypeInformation()); clauses.add(QueryBuilder.eq(persistentProperty.getRequiredColumnName().toCql(), writeValue)); } return clauses; }
/** * Explicitly set the {@link String name} of the table on which to perform the query. * <p> * Skip this step to use the default table derived from the {@link Class domain type}. * * @param table {@link String name} of the table; must not be {@literal null} or empty. * @return new instance of {@link SelectWithProjection}. * @throws IllegalArgumentException if {@link String table} is {@literal null} or empty. * @see #inTable(CqlIdentifier) * @see SelectWithProjection */ default SelectWithProjection<T> inTable(String table) { Assert.hasText(table, "Table name must not be null or empty"); return inTable(CqlIdentifier.of(table)); }
private Collection<Clause> getWhereClauses(ConvertingPropertyAccessor accessor, CassandraPersistentEntity<?> entity) { Assert.isTrue(entity.isCompositePrimaryKey(), String.format("Entity [%s] is not a composite primary key", entity.getName())); Collection<Clause> clauses = new ArrayList<>(); for (CassandraPersistentProperty property : entity) { TypeCodec<Object> codec = getCodec(property); Object value = accessor.getProperty(property, codec.getJavaType().getRawType()); clauses.add(QueryBuilder.eq(property.getRequiredColumnName().toCql(), value)); } return clauses; }
CqlIdentifier determineName(String value, boolean forceQuote) { if (!StringUtils.hasText(value)) { return of(getType().getSimpleName(), forceQuote); } String name = Optional.ofNullable(this.spelContext) .map(it -> SpelUtils.evaluate(value, it)) .orElse(value); Assert.state(name != null, () -> String.format("Cannot determine default name for %s", this)); return CqlIdentifier.of(name, forceQuote); }
private static Select createSelectAndOrder(List<Selector> selectors, CqlIdentifier from, Filter filter, Sort sort) { Select select; if (selectors.isEmpty()) { select = QueryBuilder.select().all().from(from.toCql()); } else { Selection selection = QueryBuilder.select(); selectors.forEach(selector -> selector.getAlias().map(CqlIdentifier::toCql).ifPresent(getSelection(selection, selector)::as)); select = selection.from(from.toCql()); } for (CriteriaDefinition criteriaDefinition : filter) { select.where(toClause(criteriaDefinition)); } if (sort.isSorted()) { List<Ordering> orderings = new ArrayList<>(); for (Order order : sort) { if (order.isAscending()) { orderings.add(QueryBuilder.asc(order.getProperty())); } else { orderings.add(QueryBuilder.desc(order.getProperty())); } } if (!orderings.isEmpty()) { select.orderBy(orderings.toArray(new Ordering[orderings.size()])); } } return select; }
private void writeUpdateFromWrapper(ConvertingPropertyAccessor propertyAccessor, Update update, CassandraPersistentEntity<?> entity) { for (CassandraPersistentProperty property : entity) { Object value = getWriteValue(property, propertyAccessor); if (property.isCompositePrimaryKey()) { CassandraPersistentEntity<?> compositePrimaryKey = getMappingContext().getRequiredPersistentEntity(property); if (value == null) { continue; } writeUpdateFromWrapper(newConvertingPropertyAccessor(value, compositePrimaryKey), update, compositePrimaryKey); continue; } if (isPrimaryKeyPart(property)) { update.where(QueryBuilder.eq(property.getRequiredColumnName().toCql(), value)); } else { update.with(QueryBuilder.set(property.getRequiredColumnName().toCql(), value)); } } }
/** * Create a {@link Insert} statement containing all properties including these with {@literal null} values. * * @param entity the entity, must not be {@literal null}. * @return the constructed {@link Insert} statement. */ static Insert createInsert(CassandraConverter converter, Object entity) { CassandraPersistentEntity<?> persistentEntity = converter.getMappingContext() .getRequiredPersistentEntity(entity.getClass()); Map<String, Object> toInsert = new LinkedHashMap<>(); converter.write(entity, toInsert, persistentEntity); Insert insert = QueryBuilder.insertInto(persistentEntity.getTableName().toCql()); for (Entry<String, Object> entry : toInsert.entrySet()) { insert.value(entry.getKey(), entry.getValue()); } return insert; } }
private static Delete delete(List<String> columnNames, CqlIdentifier from, Filter filter) { Delete select; if (columnNames.isEmpty()) { select = QueryBuilder.delete().all().from(from.toCql()); } else { Delete.Selection selection = QueryBuilder.delete(); columnNames.forEach(selection::column); select = selection.from(from.toCql()); } for (CriteriaDefinition criteriaDefinition : filter) { select.where(toClause(criteriaDefinition)); } return select; }
private static com.datastax.driver.core.querybuilder.Update update(CqlIdentifier table, Update mappedUpdate, Filter filter) { com.datastax.driver.core.querybuilder.Update update = QueryBuilder.update(table.toCql()); for (AssignmentOp assignmentOp : mappedUpdate.getUpdateOperations()) { update.with(getAssignment(assignmentOp)); } for (CriteriaDefinition criteriaDefinition : filter) { update.where(toClause(criteriaDefinition)); } return update; }
/** * Adds an {@literal RENAME} to the list of field changes. * * @param from must not be {@literal null} or empty. * @param to must not be {@literal null} or empty. * @return {@code this} {@link AlterUserTypeSpecification}. */ public AlterUserTypeSpecification rename(String from, String to) { return rename(CqlIdentifier.of(from), CqlIdentifier.of(to)); }