prepare for UDT collections implementation task

This commit is contained in:
Albert Shift 2015-04-09 23:30:56 -07:00
parent 00d309121f
commit 3c7a06024a
21 changed files with 921 additions and 189 deletions

View file

@ -24,26 +24,21 @@ import java.util.stream.Collectors;
import com.datastax.driver.core.ColumnMetadata;
import com.datastax.driver.core.ColumnMetadata.IndexMetadata;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.RegularStatement;
import com.datastax.driver.core.SimpleStatement;
import com.datastax.driver.core.TableMetadata;
import com.datastax.driver.core.UserType;
import com.datastax.driver.core.schemabuilder.Alter;
import com.datastax.driver.core.schemabuilder.Create;
import com.datastax.driver.core.schemabuilder.Create.Options;
import com.datastax.driver.core.schemabuilder.CreateType;
import com.datastax.driver.core.schemabuilder.SchemaBuilder;
import com.datastax.driver.core.schemabuilder.SchemaStatement;
import com.datastax.driver.core.schemabuilder.UDTType;
import com.noorq.casser.mapping.CasserEntityType;
import com.noorq.casser.mapping.CasserEntity;
import com.noorq.casser.mapping.CasserEntityType;
import com.noorq.casser.mapping.CasserProperty;
import com.noorq.casser.mapping.IdentityName;
import com.noorq.casser.mapping.OrderingDirection;
import com.noorq.casser.support.CasserMappingException;
import com.noorq.casser.support.CqlUtil;
import com.noorq.casser.support.Either;
public final class SchemaUtil {
@ -73,19 +68,7 @@ public final class SchemaUtil {
throw new CasserMappingException("primary key columns are not supported in UserDefinedType for " + prop.getPropertyName() + " in entity " + entity);
}
Either<DataType,IdentityName> type = prop.getDataType();
if (type.isLeft()) {
create.addColumn(prop.getColumnName().toCql(), type.getLeft());
}
else if (type.isRight()) {
UDTType udtType = SchemaBuilder.frozen(type.getRight().toCql());
create.addUDTColumn(prop.getColumnName().toCql(), udtType);
}
else {
throwNoMapping(prop);
}
prop.getDataType().addColumn(create, prop.getColumnName());
}
return create;
@ -126,75 +109,13 @@ public final class SchemaUtil {
Collections.sort(clusteringColumns,
OrdinalBasedPropertyComparator.INSTANCE);
for (CasserProperty prop : partitionKeys) {
Either<DataType,IdentityName> type = prop.getDataType();
if (type.isRight()) {
throw new CasserMappingException("user defined type can not be a partition key for " + prop.getPropertyName() + " in " + prop.getEntity());
}
create.addPartitionKey(prop.getColumnName().toCql(), type.getLeft());
}
for (CasserProperty prop : clusteringColumns) {
Either<DataType,IdentityName> type = prop.getDataType();
if (type.isLeft()) {
create.addClusteringColumn(prop.getColumnName().toCql(), type.getLeft());
}
else if (type.isRight()) {
UDTType udtType = SchemaBuilder.frozen(type.getRight().toCql());
create.addUDTClusteringColumn(prop.getColumnName().toCql(), udtType);
}
else {
throwNoMapping(prop);
}
}
for (CasserProperty prop : columns) {
Either<DataType,IdentityName> type = prop.getDataType();
if (prop.isStatic()) {
if (type.isLeft()) {
create.addStaticColumn(prop.getColumnName().toCql(), type.getLeft());
}
else if (type.isRight()) {
UDTType udtType = SchemaBuilder.frozen(type.getRight().toCql());
create.addUDTStaticColumn(prop.getColumnName().toCql(), udtType);
}
else {
throwNoMapping(prop);
}
}
else {
if (type.isLeft()) {
create.addColumn(prop.getColumnName().toCql(), type.getLeft());
}
else if (type.isRight()) {
UDTType udtType = SchemaBuilder.frozen(type.getRight().toCql());
create.addUDTColumn(prop.getColumnName().toCql(), udtType);
}
else {
throwNoMapping(prop);
}
}
}
partitionKeys.forEach(p -> p.getDataType().addColumn(create, p.getColumnName()));
clusteringColumns.forEach(p -> p.getDataType().addColumn(create, p.getColumnName()));
columns.forEach(p -> p.getDataType().addColumn(create, p.getColumnName()));
if (!clusteringColumns.isEmpty()) {
Options options = create.withOptions();
for (CasserProperty prop : clusteringColumns) {
options.clusteringOrder(prop.getColumnName().toCql(), mapDirection(prop.getOrdering()));
}
clusteringColumns.forEach(p -> options.clusteringOrder(p.getColumnName().toCql(), mapDirection(p.getOrdering())));
}
return create;
@ -230,48 +151,13 @@ public final class SchemaUtil {
+ entity);
}
Either<DataType,IdentityName> type = prop.getDataType();
ColumnMetadata columnMetadata = tmd.getColumn(columnName);
if (columnMetadata != null) {
if (type.isLeft()) {
if (!type.getLeft().equals(columnMetadata.getType())) {
result.add(alter.alterColumn(prop.getColumnName().toCql()).type(type.getLeft()));
}
}
else if (type.isRight()) {
DataType metadataType = columnMetadata.getType();
if (metadataType.getName() == DataType.Name.UDT &&
metadataType instanceof UserType) {
UserType metadataUserType = (UserType) metadataType;
if (!type.getRight().equals(metadataUserType.getTypeName())) {
UDTType udtType = SchemaBuilder.frozen(type.getRight().toCql());
result.add(alter.alterColumn(prop.getColumnName().toCql()).udtType(udtType));
}
}
else {
throw new CasserMappingException("expected UserType in metadata " + metadataType + " for " + prop.getPropertyName() + " in " + prop.getEntity());
}
}
}
else if (type.isLeft()) {
result.add(alter.addColumn(prop.getColumnName().toCql()).type(type.getLeft()));
}
else if (type.isRight()) {
UDTType udtType = SchemaBuilder.frozen(type.getRight().toCql());
result.add(alter.addColumn(prop.getColumnName().toCql()).udtType(udtType));
}
SchemaStatement stmt = prop.getDataType().alterColumn(alter, prop.getColumnName(), columnMetadata);
if (stmt != null) {
result.add(stmt);
}
}
if (dropUnusedColumns) {

View file

@ -20,17 +20,16 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.UDTValue;
import com.datastax.driver.core.UserType;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.noorq.casser.mapping.CasserEntityType;
import com.noorq.casser.mapping.CasserEntity;
import com.noorq.casser.mapping.CasserEntityType;
import com.noorq.casser.mapping.CasserProperty;
import com.noorq.casser.mapping.IdentityName;
import com.noorq.casser.mapping.type.AbstractDataType;
import com.noorq.casser.mapping.type.UDTDataType;
import com.noorq.casser.support.CasserMappingException;
import com.noorq.casser.support.Either;
public final class SessionRepositoryBuilder {
@ -116,9 +115,9 @@ public final class SessionRepositoryBuilder {
for (CasserProperty prop : props) {
Either<DataType, IdentityName> type = prop.getDataType();
AbstractDataType type = prop.getDataType();
if (type.isRight() && !UDTValue.class.isAssignableFrom(prop.getJavaType())) {
if (type instanceof UDTDataType && !UDTValue.class.isAssignableFrom(prop.getJavaType())) {
CasserEntity addedUserType = add(prop.getJavaType(), OPTIONAL_UDT);

View file

@ -21,16 +21,15 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.UDTValue;
import com.noorq.casser.core.Casser;
import com.noorq.casser.mapping.CasserEntity;
import com.noorq.casser.mapping.CasserMappingEntity;
import com.noorq.casser.mapping.CasserProperty;
import com.noorq.casser.mapping.IdentityName;
import com.noorq.casser.mapping.type.AbstractDataType;
import com.noorq.casser.mapping.type.UDTDataType;
import com.noorq.casser.support.CasserException;
import com.noorq.casser.support.DslPropertyException;
import com.noorq.casser.support.Either;
public class DslInvocationHandler<E> implements InvocationHandler {
@ -50,9 +49,9 @@ public class DslInvocationHandler<E> implements InvocationHandler {
map.put(prop.getGetterMethod(), prop);
Either<DataType, IdentityName> type = prop.getDataType();
AbstractDataType type = prop.getDataType();
if (type.isRight() && !UDTValue.class.isAssignableFrom(prop.getJavaType())) {
if (type instanceof UDTDataType && !UDTValue.class.isAssignableFrom(prop.getJavaType())) {
Object childDsl = Casser.dsl(prop.getJavaType(), classLoader,
Optional.of(new CasserPropertyNode(prop, parent)));
@ -83,9 +82,9 @@ public class DslInvocationHandler<E> implements InvocationHandler {
if (prop != null) {
Either<DataType, IdentityName> type = prop.getDataType();
AbstractDataType type = prop.getDataType();
if (type.isRight()) {
if (type instanceof UDTDataType) {
Object childDsl = udtMap.get(method);

View file

@ -28,6 +28,7 @@ import java.util.UUID;
import java.util.function.Function;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.TupleValue;
import com.datastax.driver.core.UDTValue;
import com.datastax.driver.core.UserType;
import com.noorq.casser.core.SessionRepository;
@ -38,8 +39,10 @@ import com.noorq.casser.mapping.convert.StringToEnumConverter;
import com.noorq.casser.mapping.convert.TimeUUIDToDateConverter;
import com.noorq.casser.mapping.convert.TypedConverter;
import com.noorq.casser.mapping.convert.UDTValueToEntityConverter;
import com.noorq.casser.mapping.type.AbstractDataType;
import com.noorq.casser.mapping.type.DTDataType;
import com.noorq.casser.mapping.type.UDTDataType;
import com.noorq.casser.support.CasserMappingException;
import com.noorq.casser.support.Either;
public final class CasserMappingProperty implements CasserProperty {
@ -52,9 +55,12 @@ public final class CasserMappingProperty implements CasserProperty {
private final boolean isStatic;
private final KeyInformation keyInfo;
private final ColumnType columnType;
private final Type genericJavaType;
private final Class<?> javaType;
private final Either<DataType, IdentityName> columnDataType;
private final AbstractDataType dataType;
private volatile Optional<Function<Object, Object>> readConverter = null;
private volatile Optional<Function<Object, Object>> writeConverter = null;
@ -69,8 +75,11 @@ public final class CasserMappingProperty implements CasserProperty {
this.isStatic = MappingUtil.isStaticColumn(getter);
this.keyInfo = new KeyInformation(getter);
this.columnType = resolveColumnType(keyInfo, isStatic);
this.genericJavaType = getter.getGenericReturnType();
this.javaType = getter.getReturnType();
this.columnDataType = resolveColumnDataType(getter, this.javaType);
this.dataType = resolveAbstractDataType(getter, this.genericJavaType, this.javaType, this.columnType);
}
@ -81,12 +90,12 @@ public final class CasserMappingProperty implements CasserProperty {
@Override
public Class<?> getJavaType() {
return javaType;
return (Class<?>) javaType;
}
@Override
public Either<DataType, IdentityName> getDataType() {
return columnDataType;
public AbstractDataType getDataType() {
return dataType;
}
@Override
@ -146,9 +155,9 @@ public final class CasserMappingProperty implements CasserProperty {
private Function<Object, Object> resolveReadConverter(SessionRepository repository) {
Either<DataType, IdentityName> columnType = getDataType();
AbstractDataType abstractDataType = getDataType();
if (columnType.isRight()) {
if (abstractDataType instanceof UDTDataType) {
Class<Object> javaType = (Class<Object>) getJavaType();
@ -162,7 +171,7 @@ public final class CasserMappingProperty implements CasserProperty {
new UDTValueToEntityConverter(javaType, repository));
}
else {
else if (abstractDataType instanceof DTDataType) {
Class<?> propertyType = getJavaType();
@ -173,7 +182,7 @@ public final class CasserMappingProperty implements CasserProperty {
new StringToEnumConverter(propertyType));
}
DataType dataType = columnType.getLeft();
DataType dataType = ((DTDataType) abstractDataType).getDataType();
if (dataType.getName() == DataType.Name.TIMEUUID && propertyType == Date.class) {
return TypedConverter.create(
@ -184,6 +193,8 @@ public final class CasserMappingProperty implements CasserProperty {
return null;
}
return null;
}
@Override
@ -198,19 +209,21 @@ public final class CasserMappingProperty implements CasserProperty {
private Function<Object, Object> resolveWriteConverter(SessionRepository repository) {
Either<DataType, IdentityName> columnType = getDataType();
AbstractDataType abstractDataType = getDataType();
if (columnType.isRight()) {
if (abstractDataType instanceof UDTDataType) {
if (UDTValue.class.isAssignableFrom(javaType)) {
UDTDataType udtDataType = (UDTDataType) abstractDataType;
if (isUDTValue(javaType)) {
return null;
}
Class<Object> javaType = (Class<Object>) getJavaType();
UserType userType = repository.findUserType(columnType.getRight().getName());
UserType userType = repository.findUserType(udtDataType.getUdtName().getName());
if (userType == null) {
throw new CasserMappingException("UserType not found for " + columnType.getRight() + " with type " + javaType);
throw new CasserMappingException("UserType not found for " + udtDataType.getUdtName() + " with type " + javaType);
}
return TypedConverter.create(
javaType,
@ -218,11 +231,11 @@ public final class CasserMappingProperty implements CasserProperty {
new EntityToUDTValueConverter(javaType, userType, repository));
}
else {
else if (abstractDataType instanceof DTDataType) {
Class<?> propertyType = getJavaType();
Class<?> javaType = getJavaType();
if (Enum.class.isAssignableFrom(propertyType)) {
if (Enum.class.isAssignableFrom(javaType)) {
return TypedConverter.create(
Enum.class,
@ -231,9 +244,9 @@ public final class CasserMappingProperty implements CasserProperty {
}
DataType dataType = columnType.getLeft();
DataType dataType = ((DTDataType) abstractDataType).getDataType();
if (dataType.getName() == DataType.Name.TIMEUUID && propertyType == Date.class) {
if (dataType.getName() == DataType.Name.TIMEUUID && javaType == Date.class) {
return TypedConverter.create(
Date.class,
@ -243,19 +256,36 @@ public final class CasserMappingProperty implements CasserProperty {
return null;
}
return null;
}
private static Either<DataType, IdentityName> resolveColumnDataType(Method getter, Class<?> javaType) {
private static ColumnType resolveColumnType(KeyInformation keyInfo, boolean isStatic) {
if (isStatic) {
return ColumnType.STATIC_COLUMN;
}
else if (keyInfo.isPartitionKey()) {
return ColumnType.PARTITION_KEY;
}
else if (keyInfo.isClusteringColumn()) {
return ColumnType.CLUSTERING_COLUMN;
}
else {
return ColumnType.COLUMN;
}
}
private static AbstractDataType resolveAbstractDataType(Method getter, Type genericJavaType, Class<?> javaType, ColumnType columnType) {
DataType dataType = resolveDataType(getter, javaType);
DataType dataType = resolveDataType(getter, genericJavaType, javaType);
if (dataType != null) {
return Either.left(dataType);
return new DTDataType(columnType, dataType);
}
else {
IdentityName udtName = null;
if (UDTValue.class.isAssignableFrom(javaType)) {
if (isUDTValue(javaType)) {
UserTypeName userTypeName = getter.getDeclaredAnnotation(UserTypeName.class);
if (userTypeName == null) {
throw new CasserMappingException("absent UserTypeName annotation for " + getter);
@ -264,26 +294,31 @@ public final class CasserMappingProperty implements CasserProperty {
}
else {
udtName = MappingUtil.getUserDefinedTypeName(javaType, false);
if (udtName == null) {
throw new CasserMappingException("unknown property type for " + getter);
}
}
return Either.right(udtName);
return new UDTDataType(columnType, udtName);
}
}
private static DataType resolveDataType(Method getter, Class<?> javaType) {
private static DataType resolveDataType(Method getter, Type genericJavaType, Class<?> javaType) {
DataTypeName annotation = getter.getDeclaredAnnotation(DataTypeName.class);
if (annotation != null && annotation.value() != null) {
return qualifyAnnotatedType(getter, annotation);
}
if (Enum.class.isAssignableFrom(javaType)) {
if (isEnum(javaType)) {
return DataType.text();
}
if (isMap(javaType)) {
Type[] args = getTypeParameters(javaType);
Type[] args = getTypeParameters(genericJavaType);
ensureTypeArguments(getter, args.length, 2);
return DataType.map(autodetectPrimitiveType(getter, args[0]),
@ -291,14 +326,14 @@ public final class CasserMappingProperty implements CasserProperty {
}
if (isCollectionLike(javaType)) {
Type[] args = getTypeParameters(javaType);
Type[] args = getTypeParameters(genericJavaType);
ensureTypeArguments(getter, args.length, 1);
if (Set.class.isAssignableFrom(javaType)) {
if (isSet(javaType)) {
return DataType.set(autodetectPrimitiveType(getter, args[0]));
} else if (List.class.isAssignableFrom(javaType)) {
} else if (isList(javaType)) {
return DataType.list(autodetectPrimitiveType(getter, args[0]));
@ -345,12 +380,29 @@ public final class CasserMappingProperty implements CasserProperty {
return dataType;
}
static DataType autodetectPrimitiveType(Method getter, Type javaType) {
DataType dataType = SimpleDataTypes.getDataTypeByJavaClass(javaType);
static DataType autodetectPrimitiveType(Method getter, Type type) {
DataType dataType = null;
if (type instanceof Class<?>) {
Class<?> javaType = (Class<?>) type;
dataType = SimpleDataTypes.getDataTypeByJavaClass(javaType);
if (dataType == null) {
IdentityName udtName = MappingUtil.getUserDefinedTypeName(javaType, false);
if (udtName != null) {
//return SchemaBuilder.frozen(udtName.getName());
}
}
}
if (dataType == null) {
throw new CasserMappingException(
"only primitive types are allowed inside collections for the property " + getter);
"unknown type inside collections for the property " + getter);
}
return dataType;
}
@ -364,25 +416,41 @@ public final class CasserMappingProperty implements CasserProperty {
static boolean isMap(Class<?> javaType) {
return Map.class.isAssignableFrom(javaType);
}
static boolean isSet(Class<?> javaType) {
return Set.class.isAssignableFrom(javaType);
}
static boolean isList(Class<?> javaType) {
return List.class.isAssignableFrom(javaType);
}
static boolean isEnum(Class<?> javaType) {
return Enum.class.isAssignableFrom(javaType);
}
static boolean isCollectionLike(Class<?> javaType) {
Class<?> rawType = javaType;
if (rawType.isArray() || Iterable.class.equals(rawType)) {
if (javaType.isArray() || Iterable.class.equals(javaType)) {
return true;
}
return Collection.class.isAssignableFrom(rawType);
return Collection.class.isAssignableFrom(javaType);
}
static Type[] getTypeParameters(Class<?> javaTypeAsClass) {
static boolean isUDTValue(Class<?> javaType) {
return UDTValue.class.isAssignableFrom(javaType);
}
static boolean isTupleValue(Class<?> javaType) {
return TupleValue.class.isAssignableFrom(javaType);
}
static Type[] getTypeParameters(Type genericJavaType) {
Type javaType = (Type) javaTypeAsClass;
if (genericJavaType instanceof ParameterizedType) {
if (javaType instanceof ParameterizedType) {
ParameterizedType type = (ParameterizedType) javaType;
ParameterizedType type = (ParameterizedType) genericJavaType;
return type.getActualTypeArguments();
}

View file

@ -19,9 +19,8 @@ import java.lang.reflect.Method;
import java.util.Optional;
import java.util.function.Function;
import com.datastax.driver.core.DataType;
import com.noorq.casser.core.SessionRepository;
import com.noorq.casser.support.Either;
import com.noorq.casser.mapping.type.AbstractDataType;
public interface CasserProperty {
@ -37,7 +36,7 @@ public interface CasserProperty {
Class<?> getJavaType();
Either<DataType, IdentityName> getDataType();
AbstractDataType getDataType();
boolean isPartitionKey();

View file

@ -0,0 +1,20 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.mapping;
public enum ColumnType {
PARTITION_KEY, CLUSTERING_COLUMN, STATIC_COLUMN, COLUMN;
}

View file

@ -81,11 +81,8 @@ public class SimpleDataTypes {
return nameToDataTypeMap.get(name);
}
public static DataType getDataTypeByJavaClass(Type type) {
Class<?> javaClass = (Class<?>) type;
return javaClassToDataTypeMap.get(javaClass);
public static DataType getDataTypeByJavaClass(Class<?> javaType) {
return javaClassToDataTypeMap.get(javaType);
}
public static DataType.Name[] getDataTypeNamesForArguments(Type[] arguments) {

View file

@ -0,0 +1,55 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.mapping.type;
import com.datastax.driver.core.ColumnMetadata;
import com.datastax.driver.core.schemabuilder.Alter;
import com.datastax.driver.core.schemabuilder.Create;
import com.datastax.driver.core.schemabuilder.CreateType;
import com.datastax.driver.core.schemabuilder.SchemaStatement;
import com.noorq.casser.mapping.ColumnType;
import com.noorq.casser.mapping.IdentityName;
import com.noorq.casser.support.CasserMappingException;
public abstract class AbstractDataType {
public abstract void addColumn(Create create, IdentityName columnName);
public abstract void addColumn(CreateType create, IdentityName columnName);
public abstract SchemaStatement alterColumn(Alter alter, IdentityName columnName, ColumnMetadata columnMetadata);
final ColumnType columnType;
public AbstractDataType(ColumnType columnType) {
this.columnType = columnType;
}
public ColumnType getColumnType() {
return columnType;
}
void ensureSimpleColumn(IdentityName columnName) {
if (columnType != ColumnType.COLUMN) {
throwWrongColumnType(columnName);
}
}
void throwWrongColumnType(IdentityName columnName) {
throw new CasserMappingException("wrong column type " + columnType + " for UserDefinedType in columnName " + columnName);
}
}

View file

@ -0,0 +1,109 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.mapping.type;
import com.datastax.driver.core.ColumnMetadata;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.schemabuilder.Alter;
import com.datastax.driver.core.schemabuilder.Create;
import com.datastax.driver.core.schemabuilder.CreateType;
import com.datastax.driver.core.schemabuilder.SchemaStatement;
import com.noorq.casser.mapping.ColumnType;
import com.noorq.casser.mapping.IdentityName;
import com.noorq.casser.support.CasserMappingException;
public final class DTDataType extends AbstractDataType {
private final DataType dataType;
public DTDataType(ColumnType columnType, DataType dataType) {
super(columnType);
this.dataType = dataType;
}
public DataType getDataType() {
return dataType;
}
@Override
public void addColumn(Create create, IdentityName columnName) {
switch(columnType) {
case PARTITION_KEY:
create.addPartitionKey(columnName.toCql(), dataType);
break;
case CLUSTERING_COLUMN:
create.addClusteringColumn(columnName.toCql(), dataType);
break;
case STATIC_COLUMN:
create.addStaticColumn(columnName.toCql(), dataType);
break;
case COLUMN:
create.addColumn(columnName.toCql(), dataType);
break;
default:
throwWrongColumnType(columnName);
}
}
@Override
public void addColumn(CreateType create, IdentityName columnName) {
if (columnType != ColumnType.COLUMN) {
throwWrongColumnType(columnName);
}
create.addColumn(columnName.toCql(), dataType);
}
@Override
public SchemaStatement alterColumn(Alter alter, IdentityName columnName, ColumnMetadata columnMetadata) {
if (columnMetadata != null) {
if (!dataType.equals(columnMetadata.getType())) {
ensureSimpleColumn(columnName);
return alter.alterColumn(columnName.toCql()).type(dataType);
}
}
else {
switch(columnType) {
case STATIC_COLUMN:
return alter.addStaticColumn(columnName.toCql()).type(dataType);
case COLUMN:
return alter.addColumn(columnName.toCql()).type(dataType);
default:
throw new CasserMappingException("unable to alter " + columnType + " column " + columnName);
}
}
return null;
}
}

View file

@ -0,0 +1,63 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.mapping.type;
import com.datastax.driver.core.ColumnMetadata;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.schemabuilder.Alter;
import com.datastax.driver.core.schemabuilder.Create;
import com.datastax.driver.core.schemabuilder.CreateType;
import com.datastax.driver.core.schemabuilder.SchemaBuilder;
import com.datastax.driver.core.schemabuilder.SchemaStatement;
import com.datastax.driver.core.schemabuilder.UDTType;
import com.noorq.casser.mapping.ColumnType;
import com.noorq.casser.mapping.IdentityName;
import com.noorq.casser.support.CasserMappingException;
public final class DTKeyUTDValueMapDataType extends AbstractDataType {
private final DataType keyType;
private final IdentityName valueType;
public DTKeyUTDValueMapDataType(ColumnType columnType, DataType keyType, IdentityName valueType) {
super(columnType);
this.keyType = keyType;
this.valueType = valueType;
}
@Override
public void addColumn(Create create, IdentityName columnName) {
ensureSimpleColumn(columnName);
UDTType valueUdtType = SchemaBuilder.frozen(valueType.toCql());
create.addUDTMapColumn(columnName.toCql(), keyType, valueUdtType);
}
@Override
public void addColumn(CreateType create, IdentityName columnName) {
ensureSimpleColumn(columnName);
UDTType valueUdtType = SchemaBuilder.frozen(valueType.toCql());
create.addUDTMapColumn(columnName.toCql(), keyType, valueUdtType);
}
@Override
public SchemaStatement alterColumn(Alter alter, IdentityName columnName,
ColumnMetadata columnMetadata) {
throw new CasserMappingException("alter of UDTMap column is not possible now for " + columnName);
}
}

View file

@ -0,0 +1,123 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.mapping.type;
import com.datastax.driver.core.ColumnMetadata;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.UserType;
import com.datastax.driver.core.schemabuilder.Alter;
import com.datastax.driver.core.schemabuilder.Create;
import com.datastax.driver.core.schemabuilder.CreateType;
import com.datastax.driver.core.schemabuilder.SchemaBuilder;
import com.datastax.driver.core.schemabuilder.SchemaStatement;
import com.datastax.driver.core.schemabuilder.UDTType;
import com.noorq.casser.mapping.ColumnType;
import com.noorq.casser.mapping.IdentityName;
public final class UDTDataType extends AbstractDataType {
private final IdentityName udtName;
public UDTDataType(ColumnType columnType, IdentityName udtName) {
super(columnType);
this.udtName = udtName;
}
public IdentityName getUdtName() {
return udtName;
}
@Override
public void addColumn(Create create, IdentityName columnName) {
UDTType udtType = SchemaBuilder.frozen(udtName.toCql());
switch(columnType) {
case PARTITION_KEY:
create.addUDTPartitionKey(columnName.toCql(), udtType);
break;
case CLUSTERING_COLUMN:
create.addUDTClusteringColumn(columnName.toCql(), udtType);
break;
case STATIC_COLUMN:
create.addUDTStaticColumn(columnName.toCql(), udtType);
break;
case COLUMN:
create.addUDTColumn(columnName.toCql(), udtType);
break;
default:
throwWrongColumnType(columnName);
}
}
@Override
public void addColumn(CreateType create, IdentityName columnName) {
ensureSimpleColumn(columnName);
UDTType udtType = SchemaBuilder.frozen(udtName.toCql());
create.addUDTColumn(columnName.toCql(), udtType);
}
@Override
public SchemaStatement alterColumn(Alter alter, IdentityName columnName,
ColumnMetadata columnMetadata) {
ensureSimpleColumn(columnName);
if (columnMetadata != null) {
DataType metadataType = columnMetadata.getType();
if (metadataType.getName() == DataType.Name.UDT &&
metadataType instanceof UserType) {
UserType metadataUserType = (UserType) metadataType;
if (!udtName.getName().equals(metadataUserType.getTypeName())) {
UDTType udtType = SchemaBuilder.frozen(udtName.toCql());
return alter.alterColumn(columnName.toCql()).udtType(udtType);
}
}
else {
UDTType udtType = SchemaBuilder.frozen(udtName.toCql());
return alter.alterColumn(columnName.toCql()).udtType(udtType);
}
}
else {
UDTType udtType = SchemaBuilder.frozen(udtName.toCql());
return alter.addColumn(columnName.toCql()).udtType(udtType);
}
return null;
}
}

View file

@ -0,0 +1,63 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.mapping.type;
import com.datastax.driver.core.ColumnMetadata;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.schemabuilder.Alter;
import com.datastax.driver.core.schemabuilder.Create;
import com.datastax.driver.core.schemabuilder.CreateType;
import com.datastax.driver.core.schemabuilder.SchemaBuilder;
import com.datastax.driver.core.schemabuilder.SchemaStatement;
import com.datastax.driver.core.schemabuilder.UDTType;
import com.noorq.casser.mapping.ColumnType;
import com.noorq.casser.mapping.IdentityName;
import com.noorq.casser.support.CasserMappingException;
public final class UDTKeyDTValueMapDataType extends AbstractDataType {
private final IdentityName keyType;
private final DataType valueType;
public UDTKeyDTValueMapDataType(ColumnType columnType, IdentityName keyType, DataType valueType) {
super(columnType);
this.keyType = keyType;
this.valueType = valueType;
}
@Override
public void addColumn(Create create, IdentityName columnName) {
ensureSimpleColumn(columnName);
UDTType keyUdtType = SchemaBuilder.frozen(keyType.toCql());
create.addUDTMapColumn(columnName.toCql(), keyUdtType, valueType);
}
@Override
public void addColumn(CreateType create, IdentityName columnName) {
ensureSimpleColumn(columnName);
UDTType keyUdtType = SchemaBuilder.frozen(keyType.toCql());
create.addUDTMapColumn(columnName.toCql(), keyUdtType, valueType);
}
@Override
public SchemaStatement alterColumn(Alter alter, IdentityName columnName,
ColumnMetadata columnMetadata) {
throw new CasserMappingException("alter of UDTMap column is not possible now for " + columnName);
}
}

View file

@ -0,0 +1,64 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.mapping.type;
import com.datastax.driver.core.ColumnMetadata;
import com.datastax.driver.core.schemabuilder.Alter;
import com.datastax.driver.core.schemabuilder.Create;
import com.datastax.driver.core.schemabuilder.CreateType;
import com.datastax.driver.core.schemabuilder.SchemaBuilder;
import com.datastax.driver.core.schemabuilder.SchemaStatement;
import com.datastax.driver.core.schemabuilder.UDTType;
import com.noorq.casser.mapping.ColumnType;
import com.noorq.casser.mapping.IdentityName;
import com.noorq.casser.support.CasserMappingException;
public final class UDTKeyUTDValueMapDataType extends AbstractDataType {
private final IdentityName keyType;
private final IdentityName valueType;
public UDTKeyUTDValueMapDataType(ColumnType columnType, IdentityName keyType, IdentityName valueType) {
super(columnType);
this.keyType = keyType;
this.valueType = valueType;
}
@Override
public void addColumn(Create create, IdentityName columnName) {
ensureSimpleColumn(columnName);
UDTType keyUdtType = SchemaBuilder.frozen(keyType.toCql());
UDTType valueUdtType = SchemaBuilder.frozen(valueType.toCql());
create.addUDTMapColumn(columnName.toCql(), keyUdtType, valueUdtType);
}
@Override
public void addColumn(CreateType create, IdentityName columnName) {
ensureSimpleColumn(columnName);
UDTType keyUdtType = SchemaBuilder.frozen(keyType.toCql());
UDTType valueUdtType = SchemaBuilder.frozen(valueType.toCql());
create.addUDTMapColumn(columnName.toCql(), keyUdtType, valueUdtType);
}
@Override
public SchemaStatement alterColumn(Alter alter, IdentityName columnName,
ColumnMetadata columnMetadata) {
throw new CasserMappingException("alter of UDTMap column is not possible now for " + columnName);
}
}

View file

@ -0,0 +1,60 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.mapping.type;
import com.datastax.driver.core.ColumnMetadata;
import com.datastax.driver.core.schemabuilder.Alter;
import com.datastax.driver.core.schemabuilder.Create;
import com.datastax.driver.core.schemabuilder.CreateType;
import com.datastax.driver.core.schemabuilder.SchemaBuilder;
import com.datastax.driver.core.schemabuilder.SchemaStatement;
import com.datastax.driver.core.schemabuilder.UDTType;
import com.noorq.casser.mapping.ColumnType;
import com.noorq.casser.mapping.IdentityName;
import com.noorq.casser.support.CasserMappingException;
public final class UDTListDataType extends AbstractDataType {
private final IdentityName udtName;
public UDTListDataType(ColumnType columnType, IdentityName udtName) {
super(columnType);
this.udtName = udtName;
}
@Override
public void addColumn(Create create, IdentityName columnName) {
ensureSimpleColumn(columnName);
UDTType udtType = SchemaBuilder.frozen(udtName.toCql());
create.addUDTListColumn(columnName.toCql(), udtType);
}
@Override
public void addColumn(CreateType create, IdentityName columnName) {
ensureSimpleColumn(columnName);
UDTType udtType = SchemaBuilder.frozen(udtName.toCql());
create.addUDTListColumn(columnName.toCql(), udtType);
}
@Override
public SchemaStatement alterColumn(Alter alter, IdentityName columnName,
ColumnMetadata columnMetadata) {
throw new CasserMappingException("alter of UDTList column is not possible now for " + columnName);
}
}

View file

@ -0,0 +1,60 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.mapping.type;
import com.datastax.driver.core.ColumnMetadata;
import com.datastax.driver.core.schemabuilder.Alter;
import com.datastax.driver.core.schemabuilder.Create;
import com.datastax.driver.core.schemabuilder.CreateType;
import com.datastax.driver.core.schemabuilder.SchemaBuilder;
import com.datastax.driver.core.schemabuilder.SchemaStatement;
import com.datastax.driver.core.schemabuilder.UDTType;
import com.noorq.casser.mapping.ColumnType;
import com.noorq.casser.mapping.IdentityName;
import com.noorq.casser.support.CasserMappingException;
public final class UDTSetDataType extends AbstractDataType {
private final IdentityName udtName;
public UDTSetDataType(ColumnType columnType, IdentityName udtName) {
super(columnType);
this.udtName = udtName;
}
@Override
public void addColumn(Create create, IdentityName columnName) {
ensureSimpleColumn(columnName);
UDTType udtType = SchemaBuilder.frozen(udtName.toCql());
create.addUDTSetColumn(columnName.toCql(), udtType);
}
@Override
public void addColumn(CreateType create, IdentityName columnName) {
ensureSimpleColumn(columnName);
UDTType udtType = SchemaBuilder.frozen(udtName.toCql());
create.addUDTSetColumn(columnName.toCql(), udtType);
}
@Override
public SchemaStatement alterColumn(Alter alter, IdentityName columnName,
ColumnMetadata columnMetadata) {
throw new CasserMappingException("alter of UDTSet column is not possible now for " + columnName);
}
}

View file

@ -49,6 +49,10 @@ public abstract class AbstractEmbeddedCassandraTest {
return keyspace;
}
public static void setKeep(boolean enable) {
keep = enable;
}
@BeforeClass
public static void before() throws Exception {
EmbeddedCassandraServerHelper.startEmbeddedCassandra(BuildProperties.getCassandraConfig());

View file

@ -1,3 +1,18 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.test.integration.core.collection;
import java.util.HashMap;

View file

@ -1,3 +1,18 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.test.integration.core.collection;
import java.util.List;

View file

@ -0,0 +1,27 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.test.integration.core.udtcollection;
import com.noorq.casser.mapping.UserDefinedType;
@UserDefinedType
public interface Author {
String name();
String city();
}

View file

@ -0,0 +1,31 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.test.integration.core.udtcollection;
import java.util.List;
import com.noorq.casser.mapping.PartitionKey;
import com.noorq.casser.mapping.Table;
@Table
public interface Book {
@PartitionKey
int id();
List<Author> authors();
}

View file

@ -0,0 +1,75 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.test.integration.core.udtcollection;
import org.junit.BeforeClass;
import org.junit.Test;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.schemabuilder.Create;
import com.datastax.driver.core.schemabuilder.CreateType;
import com.datastax.driver.core.schemabuilder.SchemaBuilder;
import com.noorq.casser.core.Casser;
import com.noorq.casser.core.CasserSession;
import com.noorq.casser.test.integration.build.AbstractEmbeddedCassandraTest;
public class UDTCollectionTest extends AbstractEmbeddedCassandraTest {
//static Book book = Casser.dsl(Book.class);
static CasserSession csession;
@BeforeClass
public static void beforeTest() {
//csession = Casser.init(getSession()).showCql().add(book).autoCreateDrop().get();
/*
Session session = getSession();
CreateType ct = SchemaBuilder.createType("address");
ct.addColumn("street", DataType.text());
ct.addColumn("city", DataType.text());
ct.addColumn("zip_code", DataType.cint());
ct.addColumn("phones", DataType.set(DataType.text()));
String cql = ct.build();
System.out.println(cql);
session.execute(cql);
Create create = SchemaBuilder.createTable("users");
create.addPartitionKey("id", DataType.uuid());
create.addUDTMapColumn("addresses", DataType.text(), SchemaBuilder.frozen("address"));
cql = create.buildInternal();
System.out.println(cql);
*/
}
@Test
public void test() {
//System.out.println(book);
}
}