DATACASS-121 - fixed Date querying
This commit is contained in:
@@ -15,10 +15,6 @@
|
||||
*/
|
||||
package org.springframework.cassandra.core.cql;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import com.datastax.driver.core.DataType;
|
||||
|
||||
public class CqlStringUtils {
|
||||
@@ -30,8 +26,6 @@ public class CqlStringUtils {
|
||||
protected static final String EMPTY_STRING = "";
|
||||
protected static final String TYPE_PARAMETER_PREFIX = "<";
|
||||
protected static final String TYPE_PARAMETER_SUFFIX = ">";
|
||||
public static final String ISO8601_DATE_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
|
||||
public static final DateFormat ISO8601_DATE_FORMAT = new SimpleDateFormat(ISO8601_DATE_FORMAT_PATTERN);
|
||||
|
||||
public static StringBuilder noNull(StringBuilder sb) {
|
||||
return sb == null ? new StringBuilder() : sb;
|
||||
@@ -134,12 +128,4 @@ public class CqlStringUtils {
|
||||
}
|
||||
return s.substring(1, s.length() - 1);
|
||||
}
|
||||
|
||||
public static String date(Date date) {
|
||||
return ISO8601_DATE_FORMAT.format(date);
|
||||
}
|
||||
|
||||
public static String date(long time) {
|
||||
return ISO8601_DATE_FORMAT.format(new Date(time));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ public class StringBasedCassandraQuery extends AbstractCassandraQuery {
|
||||
if (queryMethod.isStringLikeParameter(index)) {
|
||||
stringValue = "'" + CqlStringUtils.escapeSingle(value) + "'";
|
||||
} else if (queryMethod.isDateParameter(index)) {
|
||||
stringValue = "'" + CqlStringUtils.date((Date) value) + "'";
|
||||
stringValue = "" + ((Date) value).getTime();
|
||||
} else {
|
||||
stringValue = value.toString();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.springframework.data.cassandra.test.integration.querymethods.datekey;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.cassandra.config.SchemaAction;
|
||||
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
|
||||
import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest;
|
||||
import org.springframework.data.cassandra.test.integration.support.IntegrationTestConfig;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
public class DateKeyIntegrationTests extends AbstractSpringDataEmbeddedCassandraIntegrationTest {
|
||||
|
||||
@Configuration
|
||||
@EnableCassandraRepositories(basePackageClasses = ThingRepo.class)
|
||||
public static class Config extends IntegrationTestConfig {
|
||||
@Override
|
||||
public String[] getEntityBasePackages() {
|
||||
return new String[] { Thing.class.getPackage().getName() };
|
||||
}
|
||||
|
||||
@Override
|
||||
public SchemaAction getSchemaAction() {
|
||||
return SchemaAction.RECREATE_DROP_UNUSED;
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
ThingRepo repo;
|
||||
|
||||
@Test
|
||||
public void testQueryWithDate() {
|
||||
Date date = new Date();
|
||||
Thing saved = new Thing(date);
|
||||
repo.save(saved);
|
||||
|
||||
Thing found = repo.findThingByDate(date);
|
||||
assertNotNull(found);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.springframework.data.cassandra.test.integration.querymethods.datekey;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.cassandra.core.PrimaryKeyType;
|
||||
import org.springframework.data.cassandra.mapping.PrimaryKeyColumn;
|
||||
import org.springframework.data.cassandra.mapping.Table;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@Table
|
||||
public class Thing {
|
||||
|
||||
@PrimaryKeyColumn(ordinal = 0, type = PrimaryKeyType.PARTITIONED)
|
||||
private Date date;
|
||||
|
||||
protected Thing() {
|
||||
}
|
||||
|
||||
public Thing(Date date) {
|
||||
setDate(date);
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return new Date(date.getTime());
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
Assert.notNull(date);
|
||||
this.date = new Date(date.getTime());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.springframework.data.cassandra.test.integration.querymethods.datekey;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.data.cassandra.repository.CassandraRepository;
|
||||
import org.springframework.data.cassandra.repository.Query;
|
||||
|
||||
public interface ThingRepo extends CassandraRepository<Thing> {
|
||||
|
||||
@Query("SELECT * from thing where date = ?0")
|
||||
Thing findThingByDate(Date date);
|
||||
}
|
||||
Reference in New Issue
Block a user