Also support ordering in clustering columns for materialized views (an oversight).

This commit is contained in:
Greg Burd 2017-10-10 15:29:48 -04:00
parent 953b47036c
commit 7156d733fa
4 changed files with 40 additions and 9 deletions

View file

@ -1,3 +1,18 @@
/*
* Copyright (C) 2015 The Helenus Authors
*
* 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.datastax.driver.core.querybuilder;
import com.datastax.driver.core.CodecRegistry;

View file

@ -8,13 +8,15 @@ public class CreateMaterializedView extends Create {
private String viewName;
private Select.Where selection;
private String primaryKey;
private String clustering;
public CreateMaterializedView(
String keyspaceName, String viewName, Select.Where selection, String primaryKey) {
String keyspaceName, String viewName, Select.Where selection, String primaryKey, String clustering) {
super(keyspaceName, viewName);
this.viewName = viewName;
this.selection = selection;
this.primaryKey = primaryKey;
this.clustering = clustering;
}
public String getQueryString(CodecRegistry codecRegistry) {
@ -37,6 +39,9 @@ public class CreateMaterializedView extends Create {
createStatement.setLength(createStatement.length() - 1);
createStatement.append(" ");
createStatement.append(primaryKey);
if (clustering != null) {
createStatement.append(" ").append(clustering);
}
createStatement.append(";");
return createStatement.toString();

View file

@ -27,6 +27,7 @@ import java.util.stream.Collectors;
import net.helenus.core.reflect.HelenusPropertyNode;
import net.helenus.mapping.*;
import net.helenus.mapping.ColumnType;
import net.helenus.mapping.annotation.ClusteringColumn;
import net.helenus.mapping.type.OptionalColumnMetadata;
import net.helenus.support.CqlUtil;
import net.helenus.support.HelenusMappingException;
@ -171,11 +172,12 @@ public final class SchemaUtil {
String columnName = prop.getColumnName();
selection = selection.column(columnName);
}
String tableName =
Helenus.entity(entity.getMappingInterface().getInterfaces()[0]).getName().toCql();
Class<?> iface = entity.getMappingInterface();
String tableName = Helenus.entity(iface.getInterfaces()[0]).getName().toCql();
Select.Where where = selection.from(tableName).where();
List<String> p = new ArrayList<String>(props.size());
List<String> c = new ArrayList<String>(props.size());
List<String> o = new ArrayList<String>(props.size());
for (HelenusPropertyNode prop : props) {
String columnName = prop.getColumnName();
@ -184,9 +186,15 @@ public final class SchemaUtil {
p.add(columnName);
where = where.and(new IsNotNullClause(columnName));
break;
case CLUSTERING_COLUMN:
c.add(columnName);
where = where.and(new IsNotNullClause(columnName));
ClusteringColumn clusteringColumn = prop.getProperty().getGetterMethod().getAnnotation(ClusteringColumn.class);
if (clusteringColumn != null && clusteringColumn.ordering() != null) {
o.add(columnName + " " + clusteringColumn.ordering().cql());
}
break;
default:
break;
@ -201,7 +209,11 @@ public final class SchemaUtil {
: "")
+ ")";
return new CreateMaterializedView(keyspace, viewName, where, primaryKey);
String clustering = "";
if (o.size() > 0) {
clustering = "WITH CLUSTERING ORDER BY (" + String.join(", ", o) + ")";
}
return new CreateMaterializedView(keyspace, viewName, where, primaryKey, clustering);
}
public static SchemaStatement dropMaterializedView(

View file

@ -2,17 +2,16 @@ package net.helenus.test.integration.core.views;
import java.util.Date;
import java.util.UUID;
import net.helenus.mapping.annotation.ClusteringColumn;
import net.helenus.mapping.annotation.Index;
import net.helenus.mapping.annotation.MaterializedView;
import net.helenus.mapping.annotation.PartitionKey;
import net.helenus.mapping.OrderingDirection;
import net.helenus.mapping.annotation.*;
@MaterializedView
public interface CyclistsByAge extends Cyclist {
@PartitionKey
UUID cid();
@ClusteringColumn
@ClusteringColumn(ordering = OrderingDirection.ASC)
int age();
Date birthday();