use of com.datastax.oss.driver.api.mapper.annotations.Insert in project java-driver by datastax.
the class NestedUdtIT method setup.
@BeforeClass
public static void setup() {
CqlSession session = SESSION_RULE.session();
for (String query : ImmutableList.of("CREATE TYPE type1(s1 text, s2 text)", "CREATE TYPE type2(i1 int, i2 int)", "CREATE TYPE type1_partial(s1 text)", "CREATE TYPE type2_partial(i1 int)", "CREATE TABLE container(id uuid PRIMARY KEY, " + "list frozen<list<type1>>, " + "map1 frozen<map<text, list<type1>>>, " + "map2 frozen<map<type1, set<list<type2>>>>," + "map3 frozen<map<type1, map<text, set<type2>>>>" + ")", "CREATE TABLE container_partial(id uuid PRIMARY KEY, " + "list frozen<list<type1_partial>>, " + "map1 frozen<map<text, list<type1_partial>>>, " + "map2 frozen<map<type1_partial, set<list<type2_partial>>>>," + "map3 frozen<map<type1_partial, map<text, set<type2_partial>>>>" + ")")) {
session.execute(SimpleStatement.builder(query).setExecutionProfile(SESSION_RULE.slowProfile()).build());
}
UserDefinedType type1Partial = session.getKeyspace().flatMap(ks -> session.getMetadata().getKeyspace(ks)).flatMap(ks -> ks.getUserDefinedType("type1_partial")).orElseThrow(AssertionError::new);
session.execute(SimpleStatement.newInstance("INSERT INTO container_partial (id, list) VALUES (?, ?)", SAMPLE_CONTAINER.getId(), Lists.newArrayList(type1Partial.newValue("a"), type1Partial.newValue("b"))));
UdtsMapper udtsMapper = new NestedUdtIT_UdtsMapperBuilder(session).build();
containerDao = udtsMapper.containerDao(SESSION_RULE.keyspace());
}
use of com.datastax.oss.driver.api.mapper.annotations.Insert in project java-driver by datastax.
the class ImmutableEntityIT method setup.
@BeforeClass
public static void setup() {
CqlSession session = SESSION_RULE.session();
for (String query : createStatements(CCM_RULE)) {
session.execute(SimpleStatement.builder(query).setExecutionProfile(SESSION_RULE.slowProfile()).build());
}
UserDefinedType dimensions2d = session.getKeyspace().flatMap(ks -> session.getMetadata().getKeyspace(ks)).flatMap(ks -> ks.getUserDefinedType("dimensions2d")).orElseThrow(AssertionError::new);
session.execute("INSERT INTO product2d (id, description, dimensions) VALUES (?, ?, ?)", PRODUCT_2D_ID, "2D product", dimensions2d.newValue(12, 34));
InventoryMapper mapper = InventoryMapper.builder(session).build();
dao = mapper.immutableProductDao(SESSION_RULE.keyspace());
}
use of com.datastax.oss.driver.api.mapper.annotations.Insert in project java-driver by datastax.
the class DaoInsertMethodGenerator method generate.
@Override
public Optional<MethodSpec> generate() {
// Validate the parameters:
// - the first one must be the entity.
// - the others are completely free-form (they'll be used as additional bind variables)
// A Function<BoundStatementBuilder, BoundStatementBuilder> can be added in last position.
List<? extends VariableElement> parameters = methodElement.getParameters();
VariableElement boundStatementFunction = findBoundStatementFunction(methodElement);
if (boundStatementFunction != null) {
parameters = parameters.subList(0, parameters.size() - 1);
}
TypeElement entityElement = parameters.isEmpty() ? null : EntityUtils.asEntityElement(parameters.get(0), typeParameters);
if (entityElement == null) {
context.getMessager().error(methodElement, "%s methods must take the entity to insert as the first parameter", Insert.class.getSimpleName());
return Optional.empty();
}
// Validate the return type:
DaoReturnType returnType = parseAndValidateReturnType(getSupportedReturnTypes(), Insert.class.getSimpleName());
if (returnType == null) {
return Optional.empty();
}
if (returnType.getEntityElement() != null && !returnType.getEntityElement().equals(entityElement)) {
context.getMessager().error(methodElement, "Invalid return type: %s methods must return the same entity as their argument ", Insert.class.getSimpleName());
return Optional.empty();
}
// Generate the method:
String helperFieldName = enclosingClass.addEntityHelperField(ClassName.get(entityElement));
String statementName = enclosingClass.addPreparedStatement(methodElement, (methodBuilder, requestName) -> generatePrepareRequest(methodBuilder, requestName, helperFieldName));
CodeBlock.Builder createStatementBlock = CodeBlock.builder();
createStatementBlock.addStatement("$T boundStatementBuilder = $L.boundStatementBuilder()", BoundStatementBuilder.class, statementName);
populateBuilderWithStatementAttributes(createStatementBlock, methodElement);
populateBuilderWithFunction(createStatementBlock, boundStatementFunction);
warnIfCqlNamePresent(parameters.subList(0, 1));
String entityParameterName = parameters.get(0).getSimpleName().toString();
NullSavingStrategy nullSavingStrategy = nullSavingStrategyValidation.getNullSavingStrategy(Insert.class, Insert::nullSavingStrategy, methodElement, enclosingClass);
createStatementBlock.addStatement("$1L.set($2L, boundStatementBuilder, $3T.$4L, false)", helperFieldName, entityParameterName, NullSavingStrategy.class, nullSavingStrategy);
// Handle all remaining parameters as additional bound values
if (parameters.size() > 1) {
List<? extends VariableElement> bindMarkers = parameters.subList(1, parameters.size());
if (validateCqlNamesPresent(bindMarkers)) {
GeneratedCodePatterns.bindParameters(bindMarkers, createStatementBlock, enclosingClass, context, false);
} else {
return Optional.empty();
}
}
createStatementBlock.addStatement("$T boundStatement = boundStatementBuilder.build()", BoundStatement.class);
return crudMethod(createStatementBlock, returnType, helperFieldName);
}
use of com.datastax.oss.driver.api.mapper.annotations.Insert in project java-driver by datastax.
the class DaoInsertMethodGenerator method generatePrepareRequest.
private void generatePrepareRequest(MethodSpec.Builder methodBuilder, String requestName, String helperFieldName) {
methodBuilder.addCode("$[$T $L = $L.insert()", SimpleStatement.class, requestName, helperFieldName);
Insert annotation = methodElement.getAnnotation(Insert.class);
if (annotation.ifNotExists()) {
methodBuilder.addCode(".ifNotExists()");
}
maybeAddTtl(annotation.ttl(), methodBuilder);
maybeAddTimestamp(annotation.timestamp(), methodBuilder);
methodBuilder.addCode(".build()$];\n");
}
use of com.datastax.oss.driver.api.mapper.annotations.Insert in project java-driver by datastax.
the class GetEntityIT method setup.
@BeforeClass
public static void setup() {
CqlSession session = SESSION_RULE.session();
for (String query : createStatements(CCM_RULE)) {
session.execute(SimpleStatement.builder(query).setExecutionProfile(SESSION_RULE.slowProfile()).build());
}
UserDefinedType dimensions2d = session.getKeyspace().flatMap(ks -> session.getMetadata().getKeyspace(ks)).flatMap(ks -> ks.getUserDefinedType("dimensions2d")).orElseThrow(AssertionError::new);
session.execute("INSERT INTO product2d (id, description, dimensions) VALUES (?, ?, ?)", PRODUCT_2D_ID, "2D product", dimensions2d.newValue(12, 34));
InventoryMapper inventoryMapper = new GetEntityIT_InventoryMapperBuilder(session).build();
dao = inventoryMapper.productDao(SESSION_RULE.keyspace());
dao.save(FLAMETHROWER);
dao.save(MP3_DOWNLOAD);
}
Aggregations