Search in sources :

Example 1 with Converter

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);
    }
}
Also used : Converter(io.requery.Converter)

Example 2 with Converter

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);
}
Also used : Attribute(io.requery.meta.Attribute) ZonedDateTimeConverter(io.requery.converter.ZonedDateTimeConverter) LocalDateConverter(io.requery.converter.LocalDateConverter) LocalDateTimeConverter(io.requery.converter.LocalDateTimeConverter) EnumStringConverter(io.requery.converter.EnumStringConverter) URLConverter(io.requery.converter.URLConverter) URIConverter(io.requery.converter.URIConverter) UUIDConverter(io.requery.converter.UUIDConverter) OffsetDateTimeConverter(io.requery.converter.OffsetDateTimeConverter) LocalTimeConverter(io.requery.converter.LocalTimeConverter) Converter(io.requery.Converter)

Aggregations

Converter (io.requery.Converter)2 EnumStringConverter (io.requery.converter.EnumStringConverter)1 LocalDateConverter (io.requery.converter.LocalDateConverter)1 LocalDateTimeConverter (io.requery.converter.LocalDateTimeConverter)1 LocalTimeConverter (io.requery.converter.LocalTimeConverter)1 OffsetDateTimeConverter (io.requery.converter.OffsetDateTimeConverter)1 URIConverter (io.requery.converter.URIConverter)1 URLConverter (io.requery.converter.URLConverter)1 UUIDConverter (io.requery.converter.UUIDConverter)1 ZonedDateTimeConverter (io.requery.converter.ZonedDateTimeConverter)1 Attribute (io.requery.meta.Attribute)1