use of io.requery.meta.Attribute in project requery by requery.
the class PreparedQueryOperation method mapParameters.
void mapParameters(PreparedStatement statement, BoundParameters parameters) throws SQLException {
for (int i = 0; i < parameters.count(); i++) {
Expression expression = parameters.expressionAt(i);
Object value = parameters.valueAt(i);
if (expression instanceof Attribute) {
Attribute attribute = (Attribute) expression;
if (attribute.isAssociation()) {
// get the referenced value
value = Attributes.replaceKeyReference(value, attribute);
}
}
Class<?> type = value == null ? null : value.getClass();
if (type != null) {
// allows entity arguments with single keys to be remapped to their keys
if (model.containsTypeOf(type)) {
Type<Object> entityType = model.typeOf(type);
Attribute<Object, ?> keyAttribute = entityType.getSingleKeyAttribute();
if (keyAttribute != null) {
value = keyAttribute.getProperty().get(value);
expression = (Expression) keyAttribute;
}
}
}
configuration.getMapping().write(expression, statement, i + 1, value);
}
}
use of io.requery.meta.Attribute in project requery by requery.
the class QueryElement method fromExpressions.
public Set<Expression<?>> fromExpressions() {
if (from == null) {
types = new LinkedHashSet<>();
Set<? extends Expression<?>> expressions;
switch(queryType) {
case SELECT:
expressions = getSelection();
break;
case INSERT:
case UPDATE:
case UPSERT:
expressions = updates.keySet();
break;
default:
expressions = Collections.emptySet();
}
for (Expression<?> expression : expressions) {
if (expression instanceof AliasedExpression) {
expression = ((AliasedExpression) expression).getInnerExpression();
}
if (expression instanceof Attribute) {
Type type = ((Attribute) expression).getDeclaringType();
types.add(type);
} else if (expression instanceof Function) {
Function function = (Function) expression;
for (Object arg : function.arguments()) {
Type type = null;
if (arg instanceof Attribute) {
type = ((Attribute) arg).getDeclaringType();
types.add(type);
} else if (arg instanceof Class) {
type = model.typeOf((Class) arg);
}
if (type != null) {
types.add(type);
}
}
}
}
if (from == null) {
from = new LinkedHashSet<>();
}
if (!types.isEmpty()) {
from.addAll(types);
}
}
return from;
}
use of io.requery.meta.Attribute in project requery by requery.
the class DefaultOutput method appendColumnExpression.
private void appendColumnExpression(Expression expression) {
switch(expression.getExpressionType()) {
case ATTRIBUTE:
Attribute attribute = (Attribute) expression;
qb.attribute(attribute);
break;
default:
if (expression instanceof RowExpression) {
RowExpression collection = (RowExpression) expression;
qb.openParenthesis();
qb.commaSeparated(collection.getExpressions(), new QueryBuilder.Appender<Expression<?>>() {
@Override
public void append(QueryBuilder qb, Expression<?> value) {
appendColumnForSelect(value);
}
});
qb.closeParenthesis().space();
} else {
qb.append(expression.getName()).space();
}
break;
}
}
use of io.requery.meta.Attribute in project requery by requery.
the class InsertGenerator method write.
@Override
public void write(final Output output, QueryElement<?> query) {
Map<Expression<?>, Object> values = query.updateValues();
InsertType insertType = query.insertType();
QueryBuilder qb = output.builder();
qb.keyword(INSERT, INTO);
output.appendTables();
if (values.isEmpty()) {
if (insertType == InsertType.VALUES) {
qb.keyword(DEFAULT, VALUES);
}
} else {
qb.openParenthesis().commaSeparated(values.entrySet(), new QueryBuilder.Appender<Map.Entry<Expression<?>, Object>>() {
@Override
public void append(QueryBuilder qb, Map.Entry<Expression<?>, Object> value) {
Expression<?> key = value.getKey();
switch(key.getExpressionType()) {
case ATTRIBUTE:
Attribute attribute = (Attribute) key;
if (attribute.isGenerated()) {
throw new IllegalStateException();
}
qb.attribute(attribute);
break;
default:
qb.append(key.getName()).space();
break;
}
}
}).closeParenthesis().space();
if (insertType == InsertType.VALUES) {
qb.keyword(VALUES).openParenthesis().commaSeparated(values.entrySet(), new QueryBuilder.Appender<Map.Entry<Expression<?>, Object>>() {
@Override
public void append(QueryBuilder qb, Map.Entry<Expression<?>, Object> value) {
output.appendConditionValue(value.getKey(), value.getValue());
}
}).closeParenthesis();
} else {
output.appendQuery(query.subQuery());
}
}
}
use of io.requery.meta.Attribute in project requery by requery.
the class EntityWriter method insert.
void insert(final E entity, EntityProxy<E> proxy, Cascade mode, GeneratedKeys<E> keys) {
// if the type is immutable return the key(s) to the caller instead of modifying the object
GeneratedResultReader keyReader = null;
if (hasGeneratedKey) {
final Settable<E> settable = keys == null ? proxy : keys;
keyReader = new GeneratedResultReader() {
@Override
public void read(int index, ResultSet results) throws SQLException {
if (results.next()) {
readGeneratedKeys(settable, results);
}
}
@Override
public String[] generatedColumns() {
return generatedColumnNames;
}
};
}
EntityUpdateOperation insert = new EntityUpdateOperation(context, keyReader) {
@Override
public int bindParameters(PreparedStatement statement) throws SQLException {
return EntityWriter.this.bindParameters(statement, entity, null);
}
};
QueryElement<Scalar<Integer>> query = new QueryElement<>(QueryType.INSERT, model, insert);
query.from(entityClass);
for (Attribute<E, ?> attribute : associativeAttributes) {
// persist the foreign key object if needed
cascadeKeyReference(Cascade.INSERT, proxy, attribute);
}
incrementVersion(proxy);
for (Attribute attribute : bindableAttributes) {
query.value((Expression) attribute, null);
}
context.getStateListener().preInsert(entity, proxy);
checkRowsAffected(query.get().value(), entity, null);
proxy.link(context.read(entityClass));
updateAssociations(mode, entity, proxy, null);
context.getStateListener().postInsert(entity, proxy);
// cache entity
if (cacheable) {
cache.put(entityClass, proxy.key(), entity);
}
}
Aggregations