Search in sources :

Example 1 with IntegerType

use of io.requery.sql.type.IntegerType 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 {
        referencedAttribute = referenced.getKeyAttributes().iterator().next();
    }
    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());
    }
}
Also used : IntegerType(io.requery.sql.type.IntegerType) Attribute(io.requery.meta.Attribute)

Aggregations

Attribute (io.requery.meta.Attribute)1 IntegerType (io.requery.sql.type.IntegerType)1