start UDT test and some minor changes
This commit is contained in:
parent
a622d9475d
commit
034ca8feda
14 changed files with 292 additions and 25 deletions
|
@ -23,6 +23,7 @@ import java.util.Set;
|
|||
|
||||
import casser.mapping.CasserMappingEntity;
|
||||
import casser.mapping.CasserMappingProperty;
|
||||
import casser.mapping.CqlUtil;
|
||||
import casser.mapping.OrderingDirection;
|
||||
import casser.support.CasserMappingException;
|
||||
|
||||
|
@ -39,12 +40,17 @@ public final class SchemaUtil {
|
|||
private SchemaUtil() {
|
||||
}
|
||||
|
||||
public static String useCql(String keyspace) {
|
||||
return "USE " + keyspace;
|
||||
public static String useCql(String keyspace, boolean forceQuote) {
|
||||
if (forceQuote) {
|
||||
return "USE " + keyspace;
|
||||
}
|
||||
else {
|
||||
return "USE" + CqlUtil.forceQuote(keyspace);
|
||||
}
|
||||
}
|
||||
|
||||
public static String createTableCql(CasserMappingEntity<?> entity) {
|
||||
|
||||
|
||||
Create create = SchemaBuilder.createTable(entity.getTableName());
|
||||
|
||||
List<CasserMappingProperty<?>> partitionKeys = new ArrayList<CasserMappingProperty<?>>();
|
||||
|
@ -75,7 +81,7 @@ public final class SchemaUtil {
|
|||
for (CasserMappingProperty<?> prop : clusteringColumns) {
|
||||
create.addClusteringColumn(prop.getColumnName(), prop.getDataType());
|
||||
}
|
||||
|
||||
|
||||
for (CasserMappingProperty<?> prop : columns) {
|
||||
|
||||
if (prop.isStatic()) {
|
||||
|
|
|
@ -107,7 +107,12 @@ public class SessionInitializer extends AbstractSessionOperations {
|
|||
}
|
||||
|
||||
public SessionInitializer use(String keyspace) {
|
||||
session.execute(SchemaUtil.useCql(keyspace));
|
||||
session.execute(SchemaUtil.useCql(keyspace, false));
|
||||
return this;
|
||||
}
|
||||
|
||||
public SessionInitializer use(String keyspace, boolean forceQuote) {
|
||||
session.execute(SchemaUtil.useCql(keyspace, forceQuote));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
@ -99,12 +99,13 @@ public class CasserMappingEntity<E> implements CasserEntity<E> {
|
|||
|
||||
if (table != null) {
|
||||
tableName = table.value();
|
||||
if (table.forceQuote()) {
|
||||
tableName = CqlUtil.forceQuote(tableName);
|
||||
}
|
||||
}
|
||||
|
||||
if (tableName == null || tableName.isEmpty()) {
|
||||
|
||||
tableName = getDefaultTableName();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -170,6 +170,9 @@ public class CasserMappingProperty<E> implements CasserProperty<E> {
|
|||
Column column = getterMethod.getDeclaredAnnotation(Column.class);
|
||||
if (column != null) {
|
||||
columnName = column.value();
|
||||
if (column.forceQuote()) {
|
||||
columnName = CqlUtil.forceQuote(columnName);
|
||||
}
|
||||
}
|
||||
|
||||
PartitionKey partitionKey = getterMethod.getDeclaredAnnotation(PartitionKey.class);
|
||||
|
@ -180,6 +183,9 @@ public class CasserMappingProperty<E> implements CasserProperty<E> {
|
|||
}
|
||||
|
||||
columnName = partitionKey.value();
|
||||
if (partitionKey.forceQuote()) {
|
||||
columnName = CqlUtil.forceQuote(columnName);
|
||||
}
|
||||
}
|
||||
|
||||
ClusteringColumn clusteringColumn = getterMethod.getDeclaredAnnotation(ClusteringColumn.class);
|
||||
|
@ -190,6 +196,9 @@ public class CasserMappingProperty<E> implements CasserProperty<E> {
|
|||
}
|
||||
|
||||
columnName = clusteringColumn.value();
|
||||
if (clusteringColumn.forceQuote()) {
|
||||
columnName = CqlUtil.forceQuote(columnName);
|
||||
}
|
||||
}
|
||||
|
||||
if (columnName == null || columnName.isEmpty()) {
|
||||
|
@ -299,8 +308,8 @@ public class CasserMappingProperty<E> implements CasserProperty<E> {
|
|||
|
||||
Class<?> propertyType = getJavaType();
|
||||
|
||||
Qualify annotation = getterMethod.getDeclaredAnnotation(Qualify.class);
|
||||
if (annotation != null && annotation.type() != null) {
|
||||
DataTypeName annotation = getterMethod.getDeclaredAnnotation(DataTypeName.class);
|
||||
if (annotation != null && annotation.value() != null) {
|
||||
return qualifyAnnotatedType(annotation);
|
||||
}
|
||||
|
||||
|
@ -333,6 +342,12 @@ public class CasserMappingProperty<E> implements CasserProperty<E> {
|
|||
|
||||
DataType dataType = SimpleDataTypes.getDataTypeByJavaClass(propertyType);
|
||||
if (dataType == null) {
|
||||
|
||||
UserDefinedType userDefinedType = propertyType.getDeclaredAnnotation(UserDefinedType.class);
|
||||
if (userDefinedType != null) {
|
||||
|
||||
}
|
||||
|
||||
throw new CasserMappingException(
|
||||
"only primitive types and Set,List,Map collections are allowed, unknown type for property '" + this.getPropertyName()
|
||||
+ "' type is '" + this.getJavaType() + "' in the entity " + this.entity.getMappingInterface());
|
||||
|
@ -341,20 +356,20 @@ public class CasserMappingProperty<E> implements CasserProperty<E> {
|
|||
return dataType;
|
||||
}
|
||||
|
||||
private DataType qualifyAnnotatedType(Qualify annotation) {
|
||||
DataType.Name type = annotation.type();
|
||||
private DataType qualifyAnnotatedType(DataTypeName annotation) {
|
||||
DataType.Name type = annotation.value();
|
||||
if (type.isCollection()) {
|
||||
switch (type) {
|
||||
case MAP:
|
||||
ensureTypeArguments(annotation.typeArguments().length, 2);
|
||||
return DataType.map(resolvePrimitiveType(annotation.typeArguments()[0]),
|
||||
resolvePrimitiveType(annotation.typeArguments()[1]));
|
||||
ensureTypeArguments(annotation.typeParameters().length, 2);
|
||||
return DataType.map(resolvePrimitiveType(annotation.typeParameters()[0]),
|
||||
resolvePrimitiveType(annotation.typeParameters()[1]));
|
||||
case LIST:
|
||||
ensureTypeArguments(annotation.typeArguments().length, 1);
|
||||
return DataType.list(resolvePrimitiveType(annotation.typeArguments()[0]));
|
||||
ensureTypeArguments(annotation.typeParameters().length, 1);
|
||||
return DataType.list(resolvePrimitiveType(annotation.typeParameters()[0]));
|
||||
case SET:
|
||||
ensureTypeArguments(annotation.typeArguments().length, 1);
|
||||
return DataType.set(resolvePrimitiveType(annotation.typeArguments()[0]));
|
||||
ensureTypeArguments(annotation.typeParameters().length, 1);
|
||||
return DataType.set(resolvePrimitiveType(annotation.typeParameters()[0]));
|
||||
default:
|
||||
throw new CasserMappingException("unknown collection DataType for property '" + this.getPropertyName()
|
||||
+ "' type is '" + this.getJavaType() + "' in the entity " + this.entity.getMappingInterface());
|
||||
|
|
27
src/main/java/casser/mapping/CqlUtil.java
Normal file
27
src/main/java/casser/mapping/CqlUtil.java
Normal 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 casser.mapping;
|
||||
|
||||
public final class CqlUtil {
|
||||
|
||||
private CqlUtil() {
|
||||
}
|
||||
|
||||
public static String forceQuote(String identity) {
|
||||
return "\"" + identity + "\"";
|
||||
}
|
||||
|
||||
}
|
|
@ -23,10 +23,10 @@ import com.datastax.driver.core.DataType;
|
|||
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Qualify {
|
||||
public @interface DataTypeName {
|
||||
|
||||
DataType.Name type();
|
||||
DataType.Name value();
|
||||
|
||||
DataType.Name[] typeArguments() default {};
|
||||
DataType.Name[] typeParameters() default {};
|
||||
|
||||
}
|
33
src/main/java/casser/mapping/Field.java
Normal file
33
src/main/java/casser/mapping/Field.java
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* 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 casser.mapping;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(value = { ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
|
||||
public @interface Field {
|
||||
|
||||
String value() default "";
|
||||
|
||||
boolean forceQuote() default false;
|
||||
}
|
33
src/main/java/casser/mapping/UserDefinedType.java
Normal file
33
src/main/java/casser/mapping/UserDefinedType.java
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* 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 casser.mapping;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Inherited
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.TYPE })
|
||||
public @interface UserDefinedType {
|
||||
|
||||
String value() default "";
|
||||
|
||||
boolean forceQuote() default false;
|
||||
|
||||
}
|
33
src/main/java/casser/support/Mutable.java
Normal file
33
src/main/java/casser/support/Mutable.java
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* 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 casser.support;
|
||||
|
||||
public final class Mutable<T> {
|
||||
|
||||
private volatile T value;
|
||||
|
||||
public Mutable(T initialValue) {
|
||||
this.value = initialValue;
|
||||
}
|
||||
|
||||
public T get() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void set(T value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
|
@ -18,12 +18,13 @@ package casser.test.integration.core.compound;
|
|||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import casser.core.Casser;
|
||||
import casser.core.CasserSession;
|
||||
import casser.mapping.OrderingDirection;
|
||||
import casser.support.Mutable;
|
||||
import casser.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
|
||||
public class CompondKeyTest extends AbstractEmbeddedCassandraTest {
|
||||
|
@ -61,10 +62,25 @@ public class CompondKeyTest extends AbstractEmbeddedCassandraTest {
|
|||
|
||||
session.showCql(true);
|
||||
|
||||
final Mutable<Date> d = new Mutable<Date>(null);
|
||||
final Mutable<Integer> c = new Mutable<Integer>(0);
|
||||
|
||||
session.select(timeline::getUserId, timeline::getTimestamp, timeline::getText)
|
||||
.where(timeline::getUserId, "==", userId)
|
||||
.orderBy(timeline::getTimestamp, "desc").limit(5).sync()
|
||||
.forEach(t -> System.out.println(t));
|
||||
.forEach(t -> {
|
||||
//System.out.println(t);
|
||||
c.set(c.get() + 1);
|
||||
|
||||
Date cd = d.get();
|
||||
if (cd != null) {
|
||||
Assert.assertTrue(cd.after(t.v2));
|
||||
}
|
||||
d.set(t.v2);
|
||||
|
||||
});
|
||||
|
||||
Assert.assertEquals(Integer.valueOf(5), c.get());
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ import java.util.UUID;
|
|||
import casser.mapping.ClusteringColumn;
|
||||
import casser.mapping.Column;
|
||||
import casser.mapping.PartitionKey;
|
||||
import casser.mapping.Qualify;
|
||||
import casser.mapping.DataTypeName;
|
||||
|
||||
import com.datastax.driver.core.DataType.Name;
|
||||
|
||||
|
@ -34,7 +34,7 @@ public interface Timeline {
|
|||
void setUserId(UUID uid);
|
||||
|
||||
@ClusteringColumn
|
||||
@Qualify(type=Name.TIMEUUID)
|
||||
@DataTypeName(Name.TIMEUUID)
|
||||
Date getTimestamp();
|
||||
|
||||
void setTimestamp(Date ts);
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package casser.test.integration.core.usertype;
|
||||
|
||||
import casser.mapping.PartitionKey;
|
||||
import casser.mapping.Table;
|
||||
|
||||
@Table
|
||||
public interface Account {
|
||||
|
||||
@PartitionKey
|
||||
long getId();
|
||||
|
||||
void setId(long id);
|
||||
|
||||
Address getAddress();
|
||||
|
||||
void setAddress(Address address);
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package casser.test.integration.core.usertype;
|
||||
|
||||
import casser.mapping.Field;
|
||||
import casser.mapping.UserDefinedType;
|
||||
|
||||
@UserDefinedType("address")
|
||||
public class Address {
|
||||
|
||||
@Field("line_1")
|
||||
private String street;
|
||||
|
||||
private String city;
|
||||
|
||||
private int zip;
|
||||
|
||||
private String country;
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public int getZip() {
|
||||
return zip;
|
||||
}
|
||||
|
||||
public void setZip(int zip) {
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package casser.test.integration.core.usertype;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import casser.core.Casser;
|
||||
import casser.core.CasserSession;
|
||||
import casser.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
|
||||
public class UserDefinedTypeTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
Account account;
|
||||
|
||||
CasserSession session;
|
||||
|
||||
@Before
|
||||
public void beforeTest() {
|
||||
|
||||
account = Casser.dsl(Account.class);
|
||||
|
||||
//session = Casser.init(getSession()).showCql().createDrop(Account.class).get();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUDT() {
|
||||
|
||||
System.out.println("test it");
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue