use of io.requery.meta.Attribute in project requery by requery.
the class GenericMapping method write.
@SuppressWarnings("unchecked")
@Override
public <A> void write(Expression<A> expression, PreparedStatement statement, int index, A value) throws SQLException {
Class<?> type;
Converter converter = null;
FieldType fieldType;
if (expression.getExpressionType() == ExpressionType.ATTRIBUTE) {
Attribute<?, A> attribute = (Attribute) expression;
converter = attribute.getConverter();
fieldType = mapAttribute(attribute);
type = attribute.isAssociation() ? attribute.getReferencedAttribute().get().getClassType() : attribute.getClassType();
} else {
type = expression.getClassType();
fieldType = getSubstitutedType(type);
}
if (converter == null && !type.isPrimitive()) {
converter = converterForType(type);
}
Object converted = value;
if (converter != null) {
converted = converter.convertToPersisted(value);
}
fieldType.write(statement, index, converted);
}
use of io.requery.meta.Attribute in project requery by requery.
the class EntityReader method fromBuilder.
final <B> E fromBuilder(ResultSet results, Attribute[] selection) throws SQLException {
EntityBuilderProxy<B, E> proxy = new EntityBuilderProxy<>(type);
int index = 1;
for (Attribute expression : selection) {
@SuppressWarnings("unchecked") Attribute<E, ?> attribute = (Attribute<E, ?>) expression;
if (attribute.getPrimitiveKind() != null) {
readPrimitiveField(proxy, attribute, results, index);
} else {
Object value = mapping.read((Expression) attribute, results, index);
proxy.setObject(attribute, value, PropertyState.LOADED);
}
index++;
}
return proxy.build();
}
use of io.requery.meta.Attribute in project requery by requery.
the class SchemaModifier method tableCreateStatement.
/**
* Generates the create table for a specific type.
*
* @param type to generate the table statement
* @param mode creation mode
* @param <T> Type
* @return create table sql string
*/
public <T> String tableCreateStatement(Type<T> type, TableCreationMode mode) {
String tableName = type.getName();
QueryBuilder qb = createQueryBuilder();
qb.keyword(CREATE);
if (type.getTableCreateAttributes() != null) {
for (String attribute : type.getTableCreateAttributes()) {
qb.append(attribute, true);
}
}
qb.keyword(TABLE);
if (mode == TableCreationMode.CREATE_NOT_EXISTS) {
qb.keyword(IF, NOT, EXISTS);
}
qb.tableName(tableName);
qb.openParenthesis();
int index = 0;
// columns to define first
Predicate<Attribute> filter = new Predicate<Attribute>() {
@Override
public boolean test(Attribute value) {
if (value.isVersion() && !platform.versionColumnDefinition().createColumn()) {
return false;
}
if (platform.supportsInlineForeignKeyReference()) {
return !value.isForeignKey() && !value.isAssociation();
} else {
return value.isForeignKey() || !value.isAssociation();
}
}
};
Set<Attribute<T, ?>> attributes = type.getAttributes();
for (Attribute attribute : attributes) {
if (filter.test(attribute)) {
if (index > 0) {
qb.comma();
}
createColumn(qb, attribute);
index++;
}
}
// foreign keys
for (Attribute attribute : attributes) {
if (attribute.isForeignKey()) {
if (index > 0) {
qb.comma();
}
createForeignKeyColumn(qb, attribute, true, false);
index++;
}
}
// composite primary key
if (type.getKeyAttributes().size() > 1) {
if (index > 0) {
qb.comma();
}
qb.keyword(PRIMARY, KEY);
qb.openParenthesis();
qb.commaSeparated(type.getKeyAttributes(), new QueryBuilder.Appender<Attribute<T, ?>>() {
@Override
public void append(QueryBuilder qb, Attribute<T, ?> value) {
qb.attribute(value);
}
});
qb.closeParenthesis();
}
qb.closeParenthesis();
return qb.toString();
}
use of io.requery.meta.Attribute in project requery by requery.
the class SchemaModifier method createForeignKeyColumn.
private void createForeignKeyColumn(QueryBuilder qb, Attribute<?, ?> attribute, boolean forCreateStatement, boolean forceInline) {
Type<?> referenced = model.typeOf(attribute.getReferencedClass() != null ? attribute.getReferencedClass() : attribute.getClassType());
final Attribute referencedAttribute;
if (attribute.getReferencedAttribute() != null) {
referencedAttribute = attribute.getReferencedAttribute().get();
} else if (!referenced.getKeyAttributes().isEmpty()) {
referencedAttribute = referenced.getKeyAttributes().iterator().next();
} else {
referencedAttribute = null;
}
if (!forceInline && (!platform.supportsInlineForeignKeyReference() || !forCreateStatement)) {
qb.keyword(FOREIGN, KEY).openParenthesis().attribute(attribute).closeParenthesis().space();
} else {
qb.attribute(attribute);
FieldType fieldType = null;
if (referencedAttribute != null) {
fieldType = mapping.mapAttribute(referencedAttribute);
}
if (fieldType == null) {
fieldType = new IntegerType(int.class);
}
qb.value(fieldType.getIdentifier());
}
qb.keyword(REFERENCES);
qb.tableName(referenced.getName());
if (referencedAttribute != null) {
qb.openParenthesis().attribute(referencedAttribute).closeParenthesis().space();
}
if (attribute.getDeleteAction() != null) {
qb.keyword(ON, DELETE);
appendReferentialAction(qb, attribute.getDeleteAction());
}
if (platform.supportsOnUpdateCascade() && referencedAttribute != null && !referencedAttribute.isGenerated() && attribute.getUpdateAction() != null) {
qb.keyword(ON, UPDATE);
appendReferentialAction(qb, attribute.getUpdateAction());
}
if (platform.supportsInlineForeignKeyReference()) {
if (!attribute.isNullable()) {
qb.keyword(NOT, NULL);
}
if (attribute.isUnique()) {
qb.keyword(UNIQUE);
}
}
}
use of io.requery.meta.Attribute in project requery by requery.
the class SelectResult method createIterator.
@Override
public CloseableIterator<E> createIterator(int skip, int take) {
Statement statement = null;
try {
// connection held by the iterator if statement not reused
BoundParameters parameters = createQuery(skip, take);
statement = createStatement(!parameters.isEmpty());
statement.setFetchSize(limit == null ? 0 : limit);
StatementListener listener = configuration.getStatementListener();
listener.beforeExecuteQuery(statement, sql, parameters);
ResultSet results;
if (parameters.isEmpty()) {
results = statement.executeQuery(sql);
} else {
PreparedStatement preparedStatement = (PreparedStatement) statement;
Mapping mapping = configuration.getMapping();
for (int i = 0; i < parameters.count(); i++) {
Expression expression = parameters.expressionAt(i);
Object value = parameters.valueAt(i);
if (expression instanceof Attribute) {
// extract foreign key reference
Attribute attribute = (Attribute) expression;
if (attribute.isAssociation() && (attribute.isForeignKey() || attribute.isKey())) {
// get the referenced value
if (value != null && ((Expression<?>) expression).getClassType().isAssignableFrom(value.getClass())) {
value = Attributes.replaceKeyReference(value, attribute);
}
}
}
mapping.write(expression, preparedStatement, i + 1, value);
}
results = preparedStatement.executeQuery();
}
listener.afterExecuteQuery(statement);
return new ResultSetIterator<>(reader, results, selection, true, closeConnection);
} catch (Exception e) {
throw StatementExecutionException.closing(statement, e, sql);
}
}
Aggregations