use of io.requery.Converter in project requery by requery.
the class SchemaModifier method createColumn.
private void createColumn(QueryBuilder qb, Attribute<?, ?> attribute) {
qb.attribute(attribute);
FieldType fieldType = mapping.mapAttribute(attribute);
GeneratedColumnDefinition generatedColumnDefinition = platform.generatedColumnDefinition();
if (!(attribute.isGenerated() && generatedColumnDefinition.skipTypeIdentifier())) {
// type id
Object identifier = fieldType.getIdentifier();
// type length
Converter converter = attribute.getConverter();
if (converter == null && mapping instanceof GenericMapping) {
GenericMapping genericMapping = (GenericMapping) mapping;
converter = genericMapping.converterForType(attribute.getClassType());
}
boolean hasLength = fieldType.hasLength() || (converter != null && converter.getPersistedSize() != null);
if (attribute.getDefinition() != null && attribute.getDefinition().length() > 0) {
qb.append(attribute.getDefinition());
} else if (hasLength) {
Integer length = attribute.getLength();
if (length == null && converter != null) {
length = converter.getPersistedSize();
}
if (length == null) {
length = fieldType.getDefaultLength();
}
if (length == null) {
length = 255;
}
qb.append(identifier).openParenthesis().append(length).closeParenthesis();
} else {
qb.append(identifier);
}
qb.space();
}
String suffix = fieldType.getIdentifierSuffix();
if (suffix != null) {
qb.append(suffix).space();
}
// generate the primary key
if (attribute.isKey() && !attribute.isForeignKey()) {
if (attribute.isGenerated() && !generatedColumnDefinition.postFixPrimaryKey()) {
generatedColumnDefinition.appendGeneratedSequence(qb, attribute);
qb.space();
}
// if more than one Primary key declaration appears at the end not inline
if (attribute.getDeclaringType().getKeyAttributes().size() == 1) {
qb.keyword(PRIMARY, KEY);
}
if (attribute.isGenerated() && generatedColumnDefinition.postFixPrimaryKey()) {
generatedColumnDefinition.appendGeneratedSequence(qb, attribute);
qb.space();
}
} else if (attribute.isGenerated()) {
generatedColumnDefinition.appendGeneratedSequence(qb, attribute);
qb.space();
}
if (attribute.getCollate() != null && attribute.getCollate().length() > 0) {
qb.keyword(COLLATE);
qb.append(attribute.getCollate());
qb.space();
}
if (attribute.getDefaultValue() != null && attribute.getDefaultValue().length() > 0) {
qb.keyword(DEFAULT);
qb.append(attribute.getDefaultValue());
qb.space();
}
if (!attribute.isNullable()) {
qb.keyword(NOT, NULL);
}
if (attribute.isUnique()) {
qb.keyword(UNIQUE);
}
}
use of io.requery.Converter 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);
}
Aggregations