use of io.requery.util.function.Supplier in project requery by requery.
the class EntityMetaGenerator method generateType.
private void generateType(TypeSpec.Builder builder, TypeName targetName) {
CodeBlock.Builder block = CodeBlock.builder().add("new $T<$T>($T.class, $S)\n", TypeBuilder.class, targetName, targetName, entity.tableName());
block.add(".setBaseType($T.class)\n", ClassName.get(typeElement)).add(".setCacheable($L)\n", entity.isCacheable()).add(".setImmutable($L)\n", entity.isImmutable()).add(".setReadOnly($L)\n", entity.isReadOnly()).add(".setStateless($L)\n", entity.isStateless()).add(".setView($L)\n", entity.isView());
String factoryName = entity.classFactoryName();
if (!Names.isEmpty(factoryName)) {
block.add(".setFactory(new $L())\n", ClassName.bestGuess(factoryName));
} else if (entity.isImmutable()) {
// returns this class as the builder
TypeSpec.Builder supplier = TypeSpec.anonymousClassBuilder("").addSuperinterface(parameterizedTypeName(Supplier.class, typeName));
supplier.addMethod(CodeGeneration.overridePublicMethod("get").returns(typeName).addStatement("return new $T()", typeName).build());
block.add(".setBuilderFactory($L)\n", supplier.build());
MethodSpec.Builder applyMethod = CodeGeneration.overridePublicMethod("apply").addParameter(typeName, "value").returns(targetName);
// add embedded builder calls
entity.attributes().stream().filter(AttributeDescriptor::isEmbedded).forEach(attribute -> graph.embeddedDescriptorOf(attribute).ifPresent(embedded -> embedded.builderType().ifPresent(type -> {
String fieldName = attribute.fieldName() + "Builder";
String methodName = attribute.setterName();
applyMethod.addStatement("value.builder.$L(value.$L.build())", methodName, fieldName);
})));
applyMethod.addStatement(entity.builderType().isPresent() ? "return value.builder.build()" : "return value.build()");
TypeSpec.Builder buildFunction = TypeSpec.anonymousClassBuilder("").addSuperinterface(parameterizedTypeName(Function.class, typeName, targetName)).addMethod(applyMethod.build());
block.add(".setBuilderFunction($L)\n", buildFunction.build());
} else {
TypeSpec.Builder typeFactory = TypeSpec.anonymousClassBuilder("").addSuperinterface(parameterizedTypeName(Supplier.class, targetName)).addMethod(CodeGeneration.overridePublicMethod("get").addStatement("return new $T()", targetName).returns(targetName).build());
block.add(".setFactory($L)\n", typeFactory.build());
}
ParameterizedTypeName proxyType = parameterizedTypeName(EntityProxy.class, targetName);
TypeSpec.Builder proxyProvider = TypeSpec.anonymousClassBuilder("").addSuperinterface(parameterizedTypeName(Function.class, targetName, proxyType));
MethodSpec.Builder proxyFunction = CodeGeneration.overridePublicMethod("apply").addParameter(targetName, "entity").returns(proxyType);
if (entity.isImmutable() || entity.isUnimplementable()) {
proxyFunction.addStatement("return new $T(entity, $L)", proxyType, TYPE_NAME);
} else {
proxyFunction.addStatement("return entity.$L", PROXY_NAME);
}
proxyProvider.addMethod(proxyFunction.build());
block.add(".setProxyProvider($L)\n", proxyProvider.build());
if (entity.tableAttributes().length > 0) {
StringJoiner joiner = new StringJoiner(",", "new String[] {", "}");
for (String attribute : entity.tableAttributes()) {
joiner.add("\"" + attribute + "\"");
}
block.add(".setTableCreateAttributes($L)\n", joiner.toString());
}
if (entity.tableUniqueIndexes().length > 0) {
StringJoiner joiner = new StringJoiner(",", "new String[] {", "}");
for (String attribute : entity.tableUniqueIndexes()) {
joiner.add("\"" + attribute + "\"");
}
block.add(".setTableUniqueIndexes($L)\n", joiner.toString());
}
attributeNames.forEach(name -> block.add(".addAttribute($L)\n", name));
expressionNames.forEach(name -> block.add(".addExpression($L)\n", name));
block.add(".build()");
ParameterizedTypeName type = parameterizedTypeName(Type.class, targetName);
builder.addField(FieldSpec.builder(type, TYPE_NAME, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer("$L", block.build()).build());
}
use of io.requery.util.function.Supplier in project requery by requery.
the class DefaultOutput method appendConditionValue.
private void appendConditionValue(Expression expression, Object value, boolean parameterize) {
if (value instanceof QueryAttribute) {
appendColumn((Expression<?>) value);
} else if (value instanceof Supplier && ((Supplier) value).get() instanceof QueryAttribute) {
appendColumn((Expression<?>) ((Supplier) value).get());
} else if (value instanceof NamedExpression) {
NamedExpression namedExpression = (NamedExpression) value;
qb.append(namedExpression.getName());
} else if (value instanceof Function) {
appendFunction((Function) value);
} else if (value instanceof Collection && expression.getExpressionType() == ExpressionType.ROW) {
qb.openParenthesis();
qb.commaSeparated((Collection) value);
qb.closeParenthesis();
} else {
if (parameterize) {
if (parameters != null) {
parameters.add(expression, value);
}
qb.append("?").space();
} else {
if (value instanceof CharSequence) {
qb.appendQuoted(value.toString()).space();
} else {
qb.append(value).space();
}
}
}
}
use of io.requery.util.function.Supplier in project requery by requery.
the class CompletableEntityStoreTest method setup.
@Before
public void setup() throws SQLException {
Platform platform = new HSQL();
CommonDataSource dataSource = DatabaseType.getDataSource(platform);
EntityModel model = io.requery.test.model.Models.DEFAULT;
final TransactionListener transactionListener = new TransactionListener() {
@Override
public void beforeBegin(TransactionIsolation isolation) {
}
@Override
public void afterBegin(TransactionIsolation isolation) {
transactionState = TransactionState.BEGIN;
}
@Override
public void beforeCommit(Set<Type<?>> types) {
}
@Override
public void afterCommit(Set<Type<?>> types) {
transactionState = TransactionState.COMMIT;
}
@Override
public void beforeRollback(Set<Type<?>> types) {
}
@Override
public void afterRollback(Set<Type<?>> types) {
transactionState = TransactionState.ROLLBACK;
}
};
Configuration configuration = new ConfigurationBuilder(dataSource, model).useDefaultLogging().setStatementCacheSize(10).setBatchUpdateSize(50).setWriteExecutor(Executors.newSingleThreadExecutor()).addTransactionListenerFactory(new Supplier<TransactionListener>() {
@Override
public TransactionListener get() {
return transactionListener;
}
}).build();
data = new CompletableEntityStore<>(new EntityDataStore<Persistable>(configuration));
SchemaModifier tables = new SchemaModifier(configuration);
tables.createTables(TableCreationMode.DROP_CREATE);
}
Aggregations