/** * Entry point into the {@link CreateUserTypeSpecification}'s fluent API given {@code name} to create a type. * Convenient if imported statically. * * @param name must not {@literal null}. * @return a new {@link CreateUserTypeSpecification}. */ public static CreateUserTypeSpecification createType(CqlIdentifier name) { return new CreateUserTypeSpecification(name); }
/** * Enables the inclusion of an{@code IF NOT EXISTS} clause. * * @return this {@link CreateUserTypeSpecification}. */ public CreateUserTypeSpecification ifNotExists() { return ifNotExists(true); }
/** * Returns a {@link CreateUserTypeSpecification} for the given entity, including all mapping information. * * @param entity must not be {@literal null}. */ public CreateUserTypeSpecification getCreateUserTypeSpecificationFor(CassandraPersistentEntity<?> entity) { Assert.notNull(entity, "CassandraPersistentEntity must not be null"); CreateUserTypeSpecification specification = CreateUserTypeSpecification.createType(entity.getTableName()); for (CassandraPersistentProperty property : entity) { // Use frozen literal to not resolve types from Cassandra; At this stage, they might be not created yet. specification.field(property.getRequiredColumnName(), getDataTypeWithUserTypeFactory(property, DataTypeProvider.FrozenLiteral)); } if (specification.getFields().isEmpty()) { throw new MappingException(String.format("No fields in user type [%s]", entity.getType())); } return specification; }
private StringBuilder preambleCql(StringBuilder cql) { return cql.append("CREATE TYPE ").append(spec().getIfNotExists() ? "IF NOT EXISTS " : "") .append(spec().getName()); }
@Override public StringBuilder toCql(StringBuilder cql) { Assert.notNull(getSpecification().getName(), "User type name must not be null"); Assert.isTrue(!getSpecification().getFields().isEmpty(), String.format("User type [%s] does not contain fields", getSpecification().getName())); return columns(preambleCql(cql)).append(";"); }
private StringBuilder columns(StringBuilder cql) { // begin columns cql.append(" ("); boolean first = true; for (FieldSpecification column : spec().getFields()) { if (!first) { cql.append(", "); } column.toCql(cql); first = false; } cql.append(")"); // end columns return cql; } }
/** * Entry point into the {@link CreateUserTypeSpecification}'s fluent API given {@code name} to create a type. * Convenient if imported statically. * * @param name must not {@literal null} or empty. * @return a new {@link CreateUserTypeSpecification}. */ public static CreateUserTypeSpecification createType(String name) { return new CreateUserTypeSpecification(CqlIdentifier.of(name)); }
/** * Create {@link List} of {@link CreateUserTypeSpecification}. * * @param ifNotExists {@literal true} to create types using {@code IF NOT EXISTS}. * @return {@link List} of {@link CreateUserTypeSpecification}. */ protected List<CreateUserTypeSpecification> createUserTypeSpecifications(boolean ifNotExists) { Collection<? extends CassandraPersistentEntity<?>> entities = new ArrayList<>(this.mappingContext.getUserDefinedTypeEntities()); Map<CqlIdentifier, CassandraPersistentEntity<?>> byTableName = entities.stream() .collect(Collectors.toMap(CassandraPersistentEntity::getTableName, entity -> entity)); List<CreateUserTypeSpecification> specifications = new ArrayList<>(); Set<CqlIdentifier> created = new HashSet<>(); entities.forEach(entity -> { Set<CqlIdentifier> seen = new LinkedHashSet<>(); seen.add(entity.getTableName()); visitUserTypes(entity, seen); List<CqlIdentifier> ordered = new ArrayList<>(seen); Collections.reverse(ordered); specifications.addAll(ordered.stream().filter(created::add).map(identifier -> this.mappingContext.getCreateUserTypeSpecificationFor(byTableName.get(identifier)).ifNotExists(ifNotExists)) .collect(Collectors.toList())); }); return specifications; }