Move logic that filters out unset columns from INSERT statements into InsertOperations because we need to use the proxy implementation's ability to fetch default type-specific values in other places. This change also enables concrete implementations of mapped interfaces to implement MapExportable and use the same method to filter unset values.

This commit is contained in:
Greg Burd 2017-08-02 11:35:11 -04:00
parent 03567dc57e
commit 18cfc85f45
6 changed files with 31 additions and 16 deletions

View file

@ -11,6 +11,8 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: io.dropwizard.metrics:metrics-core:3.2.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.github.ben-manes.caffeine:caffeine:2.2.6" level="project" />
<orderEntry type="library" name="Maven: org.scala-lang:scala-library:2.13.0-M1" level="project" />
<orderEntry type="library" name="Maven: com.datastax.cassandra:cassandra-driver-core:3.3.0" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-handler:4.0.47.Final" level="project" />

View file

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.helenus</groupId>
<artifactId>helenus-core</artifactId>
<version>2.0.4-SNAPSHOT</version>
<version>2.0.5-SNAPSHOT</version>
<packaging>jar</packaging>
<name>helenus</name>

View file

@ -193,6 +193,7 @@ public abstract class AbstractStatementOperation<E, O extends AbstractStatementO
public String cql() {
Statement statement = buildStatement();
if (statement == null) return "";
if (statement instanceof BuiltStatement) {
BuiltStatement buildStatement = (BuiltStatement) statement;
return buildStatement.setForceNoValues(true).getQueryString();

View file

@ -15,19 +15,21 @@
*/
package net.helenus.core.operation;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.*;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.querybuilder.BuiltStatement;
import com.datastax.driver.core.querybuilder.Insert;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.google.common.base.Predicates;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.helenus.core.AbstractSessionOperations;
import net.helenus.core.Getter;
import net.helenus.core.reflect.HelenusPropertyNode;
import net.helenus.core.reflect.MapExportable;
import net.helenus.mapping.ColumnType;
import net.helenus.mapping.HelenusEntity;
import net.helenus.mapping.HelenusProperty;
import net.helenus.mapping.MappingUtil;
@ -55,17 +57,26 @@ public final class InsertOperation extends AbstractOperation<ResultSet, InsertOp
boolean ifNotExists) {
super(sessionOperations);
this.entity = entity;
this.ifNotExists = ifNotExists;
Set<String> keys = (pojo instanceof MapExportable) ? ((MapExportable)pojo).toMap().keySet() : null;
Collection<HelenusProperty> properties = entity.getOrderedProperties();
for (HelenusProperty prop : entity.getOrderedProperties()) {
for (HelenusProperty prop : properties) {
Object value = BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop);
value = sessionOps.getValuePreparer().prepareColumnValue(value, prop);
// Skip properties that are not in the map of the pojo we're storing. This creates a path
// for entity instances to be {in,up}serted without including all columns in the INSERT statement.
if (keys == null || keys.contains(prop.getPropertyName())) {
if (value != null) {
HelenusPropertyNode node = new HelenusPropertyNode(prop, Optional.empty());
values.add(Fun.Tuple2.of(node, value));
}
Object value = BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop);
value = sessionOps.getValuePreparer().prepareColumnValue(value, prop);
if (value != null) {
HelenusPropertyNode node = new HelenusPropertyNode(prop, Optional.empty());
values.add(Fun.Tuple2.of(node, value));
}
}
}
@ -102,6 +113,9 @@ public final class InsertOperation extends AbstractOperation<ResultSet, InsertOp
values.forEach(t -> addPropertyNode(t._1));
if (values.isEmpty())
return null;
if (entity == null) {
throw new HelenusMappingException("unknown entity");
}

View file

@ -88,7 +88,7 @@ public class MapperInvocationHandler<E> implements InvocationHandler {
Object value = src.get(methodName);
if (value == null && src.containsKey(methodName)) {
if (value == null) {
Class<?> returnType = method.getReturnType();

View file

@ -54,9 +54,7 @@ public class CompondKeyTest extends AbstractEmbeddedCassandraTest {
public String text() {
return text;
}
}
@Before