From 3c7a06024a304ed5fb47678f0b4e8489d406e01b Mon Sep 17 00:00:00 2001 From: Albert Shift Date: Thu, 9 Apr 2015 23:30:56 -0700 Subject: [PATCH] prepare for UDT collections implementation task --- .../com/noorq/casser/core/SchemaUtil.java | 136 ++-------------- .../casser/core/SessionRepositoryBuilder.java | 11 +- .../core/reflect/DslInvocationHandler.java | 13 +- .../casser/mapping/CasserMappingProperty.java | 154 +++++++++++++----- .../noorq/casser/mapping/CasserProperty.java | 5 +- .../com/noorq/casser/mapping/ColumnType.java | 20 +++ .../noorq/casser/mapping/SimpleDataTypes.java | 7 +- .../casser/mapping/type/AbstractDataType.java | 55 +++++++ .../noorq/casser/mapping/type/DTDataType.java | 109 +++++++++++++ .../type/DTKeyUTDValueMapDataType.java | 63 +++++++ .../casser/mapping/type/UDTDataType.java | 123 ++++++++++++++ .../type/UDTKeyDTValueMapDataType.java | 63 +++++++ .../type/UDTKeyUTDValueMapDataType.java | 64 ++++++++ .../casser/mapping/type/UDTListDataType.java | 60 +++++++ .../casser/mapping/type/UDTSetDataType.java | 60 +++++++ .../build/AbstractEmbeddedCassandraTest.java | 4 + .../core/collection/CollectionTest.java | 15 ++ .../integration/core/collection/Customer.java | 15 ++ .../core/udtcollection/Author.java | 27 +++ .../integration/core/udtcollection/Book.java | 31 ++++ .../core/udtcollection/UDTCollectionTest.java | 75 +++++++++ 21 files changed, 921 insertions(+), 189 deletions(-) create mode 100644 src/main/java/com/noorq/casser/mapping/ColumnType.java create mode 100644 src/main/java/com/noorq/casser/mapping/type/AbstractDataType.java create mode 100644 src/main/java/com/noorq/casser/mapping/type/DTDataType.java create mode 100644 src/main/java/com/noorq/casser/mapping/type/DTKeyUTDValueMapDataType.java create mode 100644 src/main/java/com/noorq/casser/mapping/type/UDTDataType.java create mode 100644 src/main/java/com/noorq/casser/mapping/type/UDTKeyDTValueMapDataType.java create mode 100644 src/main/java/com/noorq/casser/mapping/type/UDTKeyUTDValueMapDataType.java create mode 100644 src/main/java/com/noorq/casser/mapping/type/UDTListDataType.java create mode 100644 src/main/java/com/noorq/casser/mapping/type/UDTSetDataType.java create mode 100644 src/test/java/com/noorq/casser/test/integration/core/udtcollection/Author.java create mode 100644 src/test/java/com/noorq/casser/test/integration/core/udtcollection/Book.java create mode 100644 src/test/java/com/noorq/casser/test/integration/core/udtcollection/UDTCollectionTest.java diff --git a/src/main/java/com/noorq/casser/core/SchemaUtil.java b/src/main/java/com/noorq/casser/core/SchemaUtil.java index ff200b6..4ca7b58 100644 --- a/src/main/java/com/noorq/casser/core/SchemaUtil.java +++ b/src/main/java/com/noorq/casser/core/SchemaUtil.java @@ -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 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 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 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 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 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) { diff --git a/src/main/java/com/noorq/casser/core/SessionRepositoryBuilder.java b/src/main/java/com/noorq/casser/core/SessionRepositoryBuilder.java index 7770153..0bdafa7 100644 --- a/src/main/java/com/noorq/casser/core/SessionRepositoryBuilder.java +++ b/src/main/java/com/noorq/casser/core/SessionRepositoryBuilder.java @@ -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 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); diff --git a/src/main/java/com/noorq/casser/core/reflect/DslInvocationHandler.java b/src/main/java/com/noorq/casser/core/reflect/DslInvocationHandler.java index 4ce8001..9e69098 100644 --- a/src/main/java/com/noorq/casser/core/reflect/DslInvocationHandler.java +++ b/src/main/java/com/noorq/casser/core/reflect/DslInvocationHandler.java @@ -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 implements InvocationHandler { @@ -50,9 +49,9 @@ public class DslInvocationHandler implements InvocationHandler { map.put(prop.getGetterMethod(), prop); - Either 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 implements InvocationHandler { if (prop != null) { - Either type = prop.getDataType(); + AbstractDataType type = prop.getDataType(); - if (type.isRight()) { + if (type instanceof UDTDataType) { Object childDsl = udtMap.get(method); diff --git a/src/main/java/com/noorq/casser/mapping/CasserMappingProperty.java b/src/main/java/com/noorq/casser/mapping/CasserMappingProperty.java index fdaa691..dfade6a 100644 --- a/src/main/java/com/noorq/casser/mapping/CasserMappingProperty.java +++ b/src/main/java/com/noorq/casser/mapping/CasserMappingProperty.java @@ -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 columnDataType; + private final AbstractDataType dataType; private volatile Optional> readConverter = null; private volatile Optional> 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 getDataType() { - return columnDataType; + public AbstractDataType getDataType() { + return dataType; } @Override @@ -146,9 +155,9 @@ public final class CasserMappingProperty implements CasserProperty { private Function resolveReadConverter(SessionRepository repository) { - Either columnType = getDataType(); + AbstractDataType abstractDataType = getDataType(); - if (columnType.isRight()) { + if (abstractDataType instanceof UDTDataType) { Class javaType = (Class) 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 resolveWriteConverter(SessionRepository repository) { - Either 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 javaType = (Class) 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 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(); } diff --git a/src/main/java/com/noorq/casser/mapping/CasserProperty.java b/src/main/java/com/noorq/casser/mapping/CasserProperty.java index ec8e8e8..75cd275 100644 --- a/src/main/java/com/noorq/casser/mapping/CasserProperty.java +++ b/src/main/java/com/noorq/casser/mapping/CasserProperty.java @@ -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 getDataType(); + AbstractDataType getDataType(); boolean isPartitionKey(); diff --git a/src/main/java/com/noorq/casser/mapping/ColumnType.java b/src/main/java/com/noorq/casser/mapping/ColumnType.java new file mode 100644 index 0000000..1374bf5 --- /dev/null +++ b/src/main/java/com/noorq/casser/mapping/ColumnType.java @@ -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; +} diff --git a/src/main/java/com/noorq/casser/mapping/SimpleDataTypes.java b/src/main/java/com/noorq/casser/mapping/SimpleDataTypes.java index 1a48e71..fdc71dc 100644 --- a/src/main/java/com/noorq/casser/mapping/SimpleDataTypes.java +++ b/src/main/java/com/noorq/casser/mapping/SimpleDataTypes.java @@ -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) { diff --git a/src/main/java/com/noorq/casser/mapping/type/AbstractDataType.java b/src/main/java/com/noorq/casser/mapping/type/AbstractDataType.java new file mode 100644 index 0000000..c82a5ee --- /dev/null +++ b/src/main/java/com/noorq/casser/mapping/type/AbstractDataType.java @@ -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); + } + +} diff --git a/src/main/java/com/noorq/casser/mapping/type/DTDataType.java b/src/main/java/com/noorq/casser/mapping/type/DTDataType.java new file mode 100644 index 0000000..46f0480 --- /dev/null +++ b/src/main/java/com/noorq/casser/mapping/type/DTDataType.java @@ -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; + } + +} diff --git a/src/main/java/com/noorq/casser/mapping/type/DTKeyUTDValueMapDataType.java b/src/main/java/com/noorq/casser/mapping/type/DTKeyUTDValueMapDataType.java new file mode 100644 index 0000000..9dda816 --- /dev/null +++ b/src/main/java/com/noorq/casser/mapping/type/DTKeyUTDValueMapDataType.java @@ -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); + } + +} diff --git a/src/main/java/com/noorq/casser/mapping/type/UDTDataType.java b/src/main/java/com/noorq/casser/mapping/type/UDTDataType.java new file mode 100644 index 0000000..262aa8e --- /dev/null +++ b/src/main/java/com/noorq/casser/mapping/type/UDTDataType.java @@ -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; + } + + + +} diff --git a/src/main/java/com/noorq/casser/mapping/type/UDTKeyDTValueMapDataType.java b/src/main/java/com/noorq/casser/mapping/type/UDTKeyDTValueMapDataType.java new file mode 100644 index 0000000..b8a9964 --- /dev/null +++ b/src/main/java/com/noorq/casser/mapping/type/UDTKeyDTValueMapDataType.java @@ -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); + } + +} diff --git a/src/main/java/com/noorq/casser/mapping/type/UDTKeyUTDValueMapDataType.java b/src/main/java/com/noorq/casser/mapping/type/UDTKeyUTDValueMapDataType.java new file mode 100644 index 0000000..9b13a88 --- /dev/null +++ b/src/main/java/com/noorq/casser/mapping/type/UDTKeyUTDValueMapDataType.java @@ -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); + } + +} diff --git a/src/main/java/com/noorq/casser/mapping/type/UDTListDataType.java b/src/main/java/com/noorq/casser/mapping/type/UDTListDataType.java new file mode 100644 index 0000000..ea81bbc --- /dev/null +++ b/src/main/java/com/noorq/casser/mapping/type/UDTListDataType.java @@ -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); + } + +} diff --git a/src/main/java/com/noorq/casser/mapping/type/UDTSetDataType.java b/src/main/java/com/noorq/casser/mapping/type/UDTSetDataType.java new file mode 100644 index 0000000..4ad74da --- /dev/null +++ b/src/main/java/com/noorq/casser/mapping/type/UDTSetDataType.java @@ -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); + } + +} diff --git a/src/test/java/com/noorq/casser/test/integration/build/AbstractEmbeddedCassandraTest.java b/src/test/java/com/noorq/casser/test/integration/build/AbstractEmbeddedCassandraTest.java index 81b84ad..085389f 100644 --- a/src/test/java/com/noorq/casser/test/integration/build/AbstractEmbeddedCassandraTest.java +++ b/src/test/java/com/noorq/casser/test/integration/build/AbstractEmbeddedCassandraTest.java @@ -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()); diff --git a/src/test/java/com/noorq/casser/test/integration/core/collection/CollectionTest.java b/src/test/java/com/noorq/casser/test/integration/core/collection/CollectionTest.java index ca14414..557be7e 100644 --- a/src/test/java/com/noorq/casser/test/integration/core/collection/CollectionTest.java +++ b/src/test/java/com/noorq/casser/test/integration/core/collection/CollectionTest.java @@ -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; diff --git a/src/test/java/com/noorq/casser/test/integration/core/collection/Customer.java b/src/test/java/com/noorq/casser/test/integration/core/collection/Customer.java index 1180c71..007f75f 100644 --- a/src/test/java/com/noorq/casser/test/integration/core/collection/Customer.java +++ b/src/test/java/com/noorq/casser/test/integration/core/collection/Customer.java @@ -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; diff --git a/src/test/java/com/noorq/casser/test/integration/core/udtcollection/Author.java b/src/test/java/com/noorq/casser/test/integration/core/udtcollection/Author.java new file mode 100644 index 0000000..80437b9 --- /dev/null +++ b/src/test/java/com/noorq/casser/test/integration/core/udtcollection/Author.java @@ -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(); + +} diff --git a/src/test/java/com/noorq/casser/test/integration/core/udtcollection/Book.java b/src/test/java/com/noorq/casser/test/integration/core/udtcollection/Book.java new file mode 100644 index 0000000..fd90a61 --- /dev/null +++ b/src/test/java/com/noorq/casser/test/integration/core/udtcollection/Book.java @@ -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 authors(); + +} diff --git a/src/test/java/com/noorq/casser/test/integration/core/udtcollection/UDTCollectionTest.java b/src/test/java/com/noorq/casser/test/integration/core/udtcollection/UDTCollectionTest.java new file mode 100644 index 0000000..1ed4f8d --- /dev/null +++ b/src/test/java/com/noorq/casser/test/integration/core/udtcollection/UDTCollectionTest.java @@ -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); + + } + + +}