Search in sources :

Example 1 with EntityField

use of org.nutz.dao.entity.EntityField in project nutz by nutzam.

the class AbstractJdbcExpert method createIndexSql.

public Sql createIndexSql(Entity<?> en, EntityIndex index) {
    StringBuilder sb = new StringBuilder();
    if (index.isUnique())
        sb.append("Create UNIQUE Index ");
    else
        sb.append("Create Index ");
    sb.append(index.getName(en));
    sb.append(" ON ").append(en.getTableName()).append("(");
    for (EntityField field : index.getFields()) {
        if (field instanceof MappingField) {
            MappingField mf = (MappingField) field;
            sb.append(mf.getColumnNameInSql()).append(',');
        } else {
            throw Lang.makeThrow(DaoException.class, "%s %s is NOT a mapping field, can't use as index field!!", en.getClass(), field.getName());
        }
    }
    sb.setCharAt(sb.length() - 1, ')');
    return Sqls.create(sb.toString());
}
Also used : MappingField(org.nutz.dao.entity.MappingField) EntityField(org.nutz.dao.entity.EntityField)

Example 2 with EntityField

use of org.nutz.dao.entity.EntityField in project nutz by nutzam.

the class AnnotationEntityMaker method _evalEntityIndexes.

private void _evalEntityIndexes(NutEntity<?> en, TableIndexes indexes) {
    for (Index idx : indexes.value()) {
        NutEntityIndex index = new NutEntityIndex();
        index.setUnique(idx.unique());
        index.setName(idx.name());
        for (String indexName : idx.fields()) {
            EntityField ef = en.getField(indexName);
            if (null == ef) {
                throw Lang.makeThrow("Fail to find field '%s' in '%s' by @Index(%s:%s)", indexName, en.getType().getName(), index.getName(en), Lang.concat(idx.fields()));
            }
            index.addField(ef);
        }
        en.addIndex(index);
    }
    for (Field field : en.getMirror().getFields()) {
        Index idx = field.getAnnotation(Index.class);
        if (idx == null)
            continue;
        NutEntityIndex index = new NutEntityIndex();
        index.setUnique(idx.unique());
        index.setName(idx.name());
        index.addField(en.getField(field.getName()));
        en.addIndex(index);
    }
}
Also used : OneLinkField(org.nutz.dao.impl.entity.field.OneLinkField) NutMappingField(org.nutz.dao.impl.entity.field.NutMappingField) MappingField(org.nutz.dao.entity.MappingField) ManyManyLinkField(org.nutz.dao.impl.entity.field.ManyManyLinkField) ManyLinkField(org.nutz.dao.impl.entity.field.ManyLinkField) EntityField(org.nutz.dao.entity.EntityField) Field(java.lang.reflect.Field) Index(org.nutz.dao.entity.annotation.Index) EntityField(org.nutz.dao.entity.EntityField)

Aggregations

EntityField (org.nutz.dao.entity.EntityField)2 MappingField (org.nutz.dao.entity.MappingField)2 Field (java.lang.reflect.Field)1 Index (org.nutz.dao.entity.annotation.Index)1 ManyLinkField (org.nutz.dao.impl.entity.field.ManyLinkField)1 ManyManyLinkField (org.nutz.dao.impl.entity.field.ManyManyLinkField)1 NutMappingField (org.nutz.dao.impl.entity.field.NutMappingField)1 OneLinkField (org.nutz.dao.impl.entity.field.OneLinkField)1