Search in sources :

Example 6 with DaoException

use of org.nutz.dao.DaoException in project nutz by nutzam.

the class NutDaoRunner method _runWithTransaction.

protected void _runWithTransaction(Transaction t, DataSource dataSource, ConnCallback callback) {
    Connection conn = null;
    Savepoint sp = null;
    try {
        conn = t.getConnection(selectDataSource(t, dataSource, callback));
        if (meta != null && meta.isPostgresql()) {
            sp = conn.setSavepoint();
        }
        runCallback(conn, callback);
    } catch (Exception e) {
        if (sp != null && conn != null)
            try {
                conn.rollback(sp);
            } catch (SQLException e1) {
            }
        if (e instanceof DaoException)
            throw (DaoException) e;
        throw new DaoException(e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) Savepoint(java.sql.Savepoint) DaoException(org.nutz.dao.DaoException) DaoException(org.nutz.dao.DaoException) SQLException(java.sql.SQLException)

Example 7 with DaoException

use of org.nutz.dao.DaoException in project nutz by nutzam.

the class BoneCP_Test method clear_links.

@Test
public void clear_links() {
    dao.create(Pet.class, true);
    Sql sql1 = Sqls.create("INSERT INTO t_pet (name) VALUES ('A')");
    Sql sql2 = Sqls.create("INSERT INTO t_pet (nocol) VALUES ('B')");
    try {
        dao.execute(sql1, sql2);
        fail();
    } catch (DaoException e) {
    }
    assertEquals(0, dao.count(Pet.class));
}
Also used : DaoException(org.nutz.dao.DaoException) Pet(org.nutz.dao.test.meta.Pet) Sql(org.nutz.dao.sql.Sql) Test(org.junit.Test)

Example 8 with DaoException

use of org.nutz.dao.DaoException in project nutz by nutzam.

the class FileSqlManager method addSql.

public synchronized void addSql(String key, String value) {
    log.debugf("key=[%s], sql=[%s]", key, value);
    if (!isAllowDuplicate() && sqls.containsKey(key))
        throw new DaoException("Duplicate sql key=[" + key + "]");
    sqls.put(key, value);
}
Also used : DaoException(org.nutz.dao.DaoException)

Example 9 with DaoException

use of org.nutz.dao.DaoException in project nutz by nutzam.

the class NutDao method setExpert.

// ---------------------------------------------------------------
// 专属于NutDao的一些帮助方法
public void setExpert(Object obj) throws Exception {
    if (obj == null)
        throw new NullPointerException("expert MUST NOT NULL!!");
    if (obj instanceof JdbcExpert) {
        this.expert = (JdbcExpert) obj;
    } else {
        String name = obj.toString();
        this.expert = Jdbcs.getExpert(name, "");
        if (this.expert == null) {
            if (name.contains(".")) {
                this.expert = (JdbcExpert) Lang.loadClass(name).newInstance();
            } else {
                throw new DaoException("not such expert=" + obj);
            }
        }
    }
    DataSource ds = this.dataSource;
    // 如果数据源比expert先设置,那么需要重新设置一次
    if (ds != null) {
        this.dataSource = null;
        setDataSource(ds);
    }
}
Also used : JdbcExpert(org.nutz.dao.jdbc.JdbcExpert) DaoException(org.nutz.dao.DaoException) DataSource(javax.sql.DataSource)

Example 10 with DaoException

use of org.nutz.dao.DaoException in project nutz by nutzam.

the class AnnotationEntityMaker method make.

public <T> Entity<T> make(Class<T> type) {
    NutEntity<T> en = _createNutEntity(type);
    TableInfo ti = _createTableInfo(type);
    // 全局
    if (null != expert.getConf()) {
        for (String key : expert.getConf().keySet()) en.getMetas().put(key, expert.getConf().get(key));
    }
    // 当前表
    if (null != ti.annMeta) {
        Map<String, Object> map = Lang.map(ti.annMeta.value());
        for (Entry<String, Object> entry : map.entrySet()) {
            en.getMetas().put(entry.getKey(), entry.getValue().toString());
        }
    }
    /*
         * 获得表名以及视图名称及注释
         */
    String tableName = null;
    if (null == ti.annTable) {
        tableName = Strings.lowerWord(type.getSimpleName(), '_');
        if (null == ti.annView)
            log.warnf("No @Table found, fallback to use table name='%s' for type '%s'", tableName, type.getName());
    } else {
        tableName = ti.annTable.value();
    }
    String viewName = null == ti.annView ? tableName : ti.annView.value();
    en.setTableName(tableName);
    en.setViewName(viewName);
    boolean hasTableComment = null != ti.tableComment;
    String tableComment = hasTableComment ? Strings.isBlank(ti.tableComment.value()) ? type.getName() : ti.tableComment.value() : null;
    en.setHasTableComment(hasTableComment);
    en.setTableComment(tableComment);
    /*
         * 获取所有的数据库字段
         */
    // 字段里面是不是有声明过 '@Column' @Comment
    boolean shouldUseColumn = false;
    boolean hasColumnComment = false;
    for (Field field : en.getMirror().getFields()) {
        if (shouldUseColumn && hasColumnComment) {
            break;
        }
        if (!shouldUseColumn && null != field.getAnnotation(Column.class)) {
            shouldUseColumn = true;
        }
        if (!hasColumnComment && null != field.getAnnotation(Comment.class)) {
            hasColumnComment = true;
        }
    }
    en.setHasColumnComment(hasColumnComment);
    /*
         * 循环获取实体字段
         */
    List<MappingInfo> infos = new ArrayList<MappingInfo>();
    List<LinkInfo> ones = new ArrayList<LinkInfo>();
    List<LinkInfo> manys = new ArrayList<LinkInfo>();
    List<LinkInfo> manymanys = new ArrayList<LinkInfo>();
    String[] _tmp = ti.annPK == null ? null : ti.annPK.value();
    List<String> pks = _tmp == null ? new ArrayList<String>() : Arrays.asList(_tmp);
    // 循环所有的字段,查找有没有数据库映射字段
    for (Field field : en.getMirror().getFields()) {
        // '@One'
        if (null != field.getAnnotation(One.class)) {
            ones.add(_Infos.createLinkInfo(field));
        } else // '@Many'
        if (null != field.getAnnotation(Many.class)) {
            manys.add(_Infos.createLinkInfo(field));
        } else // '@ManyMany'
        if (null != field.getAnnotation(ManyMany.class)) {
            manymanys.add(_Infos.createLinkInfo(field));
        } else // 应该忽略
        if ((Modifier.isTransient(field.getModifiers()) && null == field.getAnnotation(Column.class)) || (shouldUseColumn && (null == field.getAnnotation(Column.class) && null == field.getAnnotation(Id.class) && null == field.getAnnotation(Name.class))) && !pks.contains(field.getName())) {
            continue;
        } else // '@Column'
        {
            infos.add(_Infos.createMappingInfo(ti.annPK, field));
        }
    }
    // 循环所有方法,查找有没有虚拟数据库映射字段
    for (Method method : en.getType().getMethods()) {
        // '@One'
        if (null != method.getAnnotation(One.class)) {
            ones.add(_Infos.createLinkInfo(method));
        } else // '@Many'
        if (null != method.getAnnotation(Many.class)) {
            manys.add(_Infos.createLinkInfo(method));
        } else // '@ManyMany'
        if (null != method.getAnnotation(ManyMany.class)) {
            manymanys.add(_Infos.createLinkInfo(method));
        } else // 应该忽略
        if (null == method.getAnnotation(Column.class) && null == method.getAnnotation(Id.class) && null == method.getAnnotation(Name.class)) {
            continue;
        } else // '@Column'
        {
            infos.add(_Infos.createMapingInfo(ti.annPK, method));
        }
    }
    // 给字段排序一下, fix issue #29
    List<MappingInfo> tmp = new ArrayList<MappingInfo>(infos.size());
    MappingInfo miId = null;
    MappingInfo miName = null;
    for (MappingInfo mi : infos) {
        if (mi.annId != null) {
            if (miId != null) {
                throw new DaoException("Allows only a single @Id ! " + type);
            }
            miId = mi;
        } else if (mi.annName != null) {
            if (miName != null) {
                throw new DaoException("Allows only a single @Name ! " + type);
            }
            miName = mi;
        } else
            tmp.add(mi);
    }
    if (miName != null)
        tmp.add(0, miName);
    if (miId != null)
        tmp.add(0, miId);
    infos = tmp;
    // 映射字段搞完了? 我看看你到底有没有字段!!
    if (infos.isEmpty())
        throw Lang.makeThrow(IllegalArgumentException.class, "Pojo(%s) without any Mapping Field!!", type);
    /*
         * 解析所有映射字段
         */
    for (MappingInfo info : infos) {
        NutMappingField ef = new NutMappingField(en);
        _evalMappingField(ef, info);
        en.addMappingField(ef);
    }
    // 保存一下,这样别的实体映射到这里时会用的到
    holder.set(en);
    try {
        // 一对一 '@One'
        for (LinkInfo li : ones) {
            en.addLinkField(new OneLinkField(en, holder, li));
        }
        // 一对多 '@Many'
        for (LinkInfo li : manys) {
            en.addLinkField(new ManyLinkField(en, holder, li));
        }
        // 多对多 '@ManyMany'
        for (LinkInfo li : manymanys) {
            en.addLinkField(new ManyManyLinkField(en, holder, li));
        }
        // 检查复合主键
        en.checkCompositeFields(null == ti.annPK ? null : ti.annPK.value());
        /*
			 * 交付给 expert 来检查一下数据库一致性
			 */
        if (null != datasource && null != expert) {
            _checkupEntityFieldsWithDatabase(en);
        }
        /*
			 * 检查字段宏
			 */
        _evalFieldMacro(en, infos);
        /*
			 * 解析实体索引
			 */
        if (null != ti.annIndexes)
            _evalEntityIndexes(en, ti.annIndexes);
    } catch (RuntimeException e) {
        holder.remove(en);
        throw e;
    } catch (Throwable e) {
        holder.remove(en);
        throw Lang.wrapThrow(e);
    }
    // 搞定收工,哦耶 ^_^
    en.setComplete(true);
    return en;
}
Also used : One(org.nutz.dao.entity.annotation.One) ManyMany(org.nutz.dao.entity.annotation.ManyMany) ArrayList(java.util.ArrayList) DaoException(org.nutz.dao.DaoException) ManyManyLinkField(org.nutz.dao.impl.entity.field.ManyManyLinkField) ManyLinkField(org.nutz.dao.impl.entity.field.ManyLinkField) Name(org.nutz.dao.entity.annotation.Name) NutMappingField(org.nutz.dao.impl.entity.field.NutMappingField) 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) Column(org.nutz.dao.entity.annotation.Column) TableInfo(org.nutz.dao.impl.entity.info.TableInfo) ManyManyLinkField(org.nutz.dao.impl.entity.field.ManyManyLinkField) LinkInfo(org.nutz.dao.impl.entity.info.LinkInfo) Method(java.lang.reflect.Method) OneLinkField(org.nutz.dao.impl.entity.field.OneLinkField) Id(org.nutz.dao.entity.annotation.Id) MappingInfo(org.nutz.dao.impl.entity.info.MappingInfo)

Aggregations

DaoException (org.nutz.dao.DaoException)12 SQLException (java.sql.SQLException)4 Test (org.junit.Test)3 Connection (java.sql.Connection)2 Pojo (org.nutz.dao.sql.Pojo)2 Sql (org.nutz.dao.sql.Sql)2 Pet (org.nutz.dao.test.meta.Pet)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Savepoint (java.sql.Savepoint)1 ArrayList (java.util.ArrayList)1 DataSource (javax.sql.DataSource)1 EntityField (org.nutz.dao.entity.EntityField)1 MappingField (org.nutz.dao.entity.MappingField)1 Column (org.nutz.dao.entity.annotation.Column)1 Id (org.nutz.dao.entity.annotation.Id)1 ManyMany (org.nutz.dao.entity.annotation.ManyMany)1 Name (org.nutz.dao.entity.annotation.Name)1 One (org.nutz.dao.entity.annotation.One)1 ManyLinkField (org.nutz.dao.impl.entity.field.ManyLinkField)1