Search in sources :

Example 46 with DAOException

use of org.eweb4j.orm.dao.DAOException in project eweb4j-framework by laiweiwei.

the class DivPageDAOImpl method divPageByFieldIsValue.

public <T> List<T> divPageByFieldIsValue(T t, String[] fields, String orderField, int orderType, int currPage, int numPerPage, boolean isOR) throws DAOException {
    List<T> list = null;
    if (t != null) {
        Connection con = null;
        @SuppressWarnings("unchecked") Class<T> clazz = (Class<T>) t.getClass();
        try {
            con = ds.getConnection();
            String sql = SqlFactory.getSelectSql(t, dbType).selectWhere(fields, null, -2, false, false, isOR, orderField, orderType, currPage, numPerPage);
            list = JdbcUtil.getList(con, clazz, sql);
        } catch (Exception e) {
            throw new DAOException("divPageByFieldIsValue exception ", e);
        }
    }
    return list;
}
Also used : DAOException(org.eweb4j.orm.dao.DAOException) Connection(java.sql.Connection) DAOException(org.eweb4j.orm.dao.DAOException)

Example 47 with DAOException

use of org.eweb4j.orm.dao.DAOException in project eweb4j-framework by laiweiwei.

the class DivPageDAOImpl method divPageByFieldNotLikeValue.

public <T> List<T> divPageByFieldNotLikeValue(T t, String[] fields, int likeType, String orderField, int orderType, int currPage, int numPerPage, boolean isOR) throws DAOException {
    List<T> list = null;
    if (t != null) {
        Connection con = null;
        @SuppressWarnings("unchecked") Class<T> clazz = (Class<T>) t.getClass();
        try {
            con = ds.getConnection();
            String sql = SqlFactory.getSelectSql(t, dbType).selectWhere(fields, null, likeType, true, true, isOR, orderField, orderType, currPage, numPerPage);
            list = JdbcUtil.getList(con, clazz, sql);
        } catch (Exception e) {
            throw new DAOException("", e);
        }
    }
    return list;
}
Also used : DAOException(org.eweb4j.orm.dao.DAOException) Connection(java.sql.Connection) DAOException(org.eweb4j.orm.dao.DAOException)

Example 48 with DAOException

use of org.eweb4j.orm.dao.DAOException in project eweb4j-framework by laiweiwei.

the class DivPageDAOImpl method divPageByFieldLikeValue.

public <T> List<T> divPageByFieldLikeValue(Class<T> clazz, String[] fields, String[] values, int likeType, String orderField, int orderType, int currPage, int numPerPage, boolean isOR) throws DAOException {
    List<T> list = null;
    if (clazz != null) {
        Connection con = null;
        try {
            con = ds.getConnection();
            String sql = SqlFactory.getSelectSql(clazz.newInstance(), dbType).selectWhere(fields, values, likeType, true, false, isOR, orderField, orderType, currPage, numPerPage);
            list = JdbcUtil.getList(con, clazz, sql);
        } catch (Exception e) {
            throw new DAOException("divPageByFieldLikeValue exception ", e);
        }
    }
    return list;
}
Also used : DAOException(org.eweb4j.orm.dao.DAOException) Connection(java.sql.Connection) DAOException(org.eweb4j.orm.dao.DAOException)

Example 49 with DAOException

use of org.eweb4j.orm.dao.DAOException in project eweb4j-framework by laiweiwei.

the class ManyToManyDAO method delete.

/**
	 * 多对多级联删除 
	 * 1.如果主对象不存在与数据库,不处理 
	 * 2.否则,检查当前主对象中的关联对象,如果关联对象为空,则删除所有与主对象有关的关联关系。
	 * 3.如果当前主对象中含有关联对象,则删除这些关联对象与主对象的关系
	 * 4.不会删除主对象
	 * 
	 */
public void delete() throws DAOException {
    if (this.fields == null || this.fields.size() == 0)
        return;
    // "delete from {relTable} WHERE {from} = {fromRefVal} ;"
    String format = "delete from %s WHERE %s = ? ";
    for (Field f : fields) {
        Method tarGetter = ru.getGetter(f.getName());
        if (tarGetter == null)
            continue;
        ManyToMany ann = tarGetter.getAnnotation(ManyToMany.class);
        if (ann == null) {
            ann = f.getAnnotation(ManyToMany.class);
            if (ann == null)
                continue;
        }
        JoinTable join = tarGetter.getAnnotation(JoinTable.class);
        if (join == null) {
            join = f.getAnnotation(JoinTable.class);
            if (join == null)
                continue;
        }
        JoinColumn[] froms = join.joinColumns();
        if (froms == null || froms.length == 0)
            continue;
        JoinColumn[] tos = join.inverseJoinColumns();
        if (tos == null || tos.length == 0)
            continue;
        String relTable = join.name();
        String from = froms[0].name();
        String fromRefCol = froms[0].referencedColumnName();
        if (fromRefCol == null || fromRefCol.trim().length() == 0)
            fromRefCol = ORMConfigBeanUtil.getIdColumn(t);
        String fromRefField = ORMConfigBeanUtil.getField(t.getClass(), fromRefCol);
        Method fromRefFieldGetter = ru.getGetter(fromRefField);
        if (fromRefFieldGetter == null)
            throw new DAOException("can not find the 'from ref field -> " + fromRefField + "' of " + t.getClass() + " 's getter method", null);
        String fromRefVal = null;
        try {
            Object _obj = fromRefFieldGetter.invoke(t);
            if (_obj == null)
                continue;
            fromRefVal = String.valueOf(_obj);
        } catch (Exception e) {
            throw new DAOException("can not get the from ref val of " + t.getClass(), e);
        }
        List<?> tarList = null;
        try {
            tarList = (List<?>) tarGetter.invoke(t);
        } catch (Exception e) {
            throw new DAOException(tarGetter + " invoke exception, can not get the " + f.getName() + " objs ", e);
        }
        if (tarList == null || tarList.size() == 0) {
            String sql = String.format(format, relTable, from);
            // 删除所有关联记录
            DAOFactory.getUpdateDAO(dsName).updateBySQLWithArgs(sql, fromRefVal);
        } else {
            // 删除指定关联的记录
            Class<?> tarClass = ann.targetEntity();
            if (void.class.isAssignableFrom(tarClass)) {
                tarClass = ClassUtil.getGenericType(f);
            }
            String to = tos[0].name();
            String toRefCol = tos[0].referencedColumnName();
            if (toRefCol == null || toRefCol.trim().length() == 0)
                toRefCol = ORMConfigBeanUtil.getIdColumn(tarClass);
            String toRefField = ORMConfigBeanUtil.getField(tarClass, toRefCol);
            // "delete from {relTable} where {from} = {fromRefVal} and to = {toRefVal}"
            String _format = "delete from %s where %s = ? and %s = ?";
            for (int i = 0; i < tarList.size(); i++) {
                Object tarObj = tarList.get(i);
                if (tarObj == null)
                    continue;
                ReflectUtil tarRu = new ReflectUtil(tarObj);
                Method toRefFieldGetter = tarRu.getGetter(toRefField);
                if (toRefFieldGetter == null)
                    throw new DAOException("can not find the 'to ref field -> " + toRefField + "' of " + tarClass + " 's getter method", null);
                String toRefVal = null;
                try {
                    Object _obj = toRefFieldGetter.invoke(tarObj);
                    if (_obj == null)
                        continue;
                    toRefVal = String.valueOf(_obj);
                } catch (Exception e) {
                    throw new DAOException("can not get the to ref val of " + tarClass, e);
                }
                if (DAOFactory.getSelectDAO(dsName).selectOne(tarClass, new String[] { toRefField }, new String[] { toRefVal }) == null)
                    continue;
                String _sql = String.format(_format, relTable, from, to);
                DAOFactory.getUpdateDAO(dsName).updateBySQLWithArgs(_sql, fromRefVal, toRefVal);
            }
        }
    }
}
Also used : ManyToMany(javax.persistence.ManyToMany) Method(java.lang.reflect.Method) DAOException(org.eweb4j.orm.dao.DAOException) DAOException(org.eweb4j.orm.dao.DAOException) Field(java.lang.reflect.Field) ReflectUtil(org.eweb4j.util.ReflectUtil) JoinColumn(javax.persistence.JoinColumn) JoinTable(javax.persistence.JoinTable)

Example 50 with DAOException

use of org.eweb4j.orm.dao.DAOException in project eweb4j-framework by laiweiwei.

the class DivPageDAOImpl method preOne.

public <T> T preOne(Class<T> clazz, String column, int value) throws DAOException {
    if (clazz != null) {
        Connection con = null;
        try {
            con = ds.getConnection();
            List<T> list = JdbcUtil.getList(con, clazz, SqlFactory.getSelectSql(clazz.newInstance(), dbType).preOne(column, value));
            return list != null && !list.isEmpty() ? list.get(0) : null;
        } catch (Exception e) {
            throw new DAOException("preOne exception ", e);
        }
    }
    return null;
}
Also used : DAOException(org.eweb4j.orm.dao.DAOException) Connection(java.sql.Connection) DAOException(org.eweb4j.orm.dao.DAOException)

Aggregations

DAOException (org.eweb4j.orm.dao.DAOException)56 Connection (java.sql.Connection)42 Sql (org.eweb4j.orm.sql.Sql)13 Method (java.lang.reflect.Method)9 ReflectUtil (org.eweb4j.util.ReflectUtil)9 Field (java.lang.reflect.Field)7 ArrayList (java.util.ArrayList)6 JoinColumn (javax.persistence.JoinColumn)6 JoinTable (javax.persistence.JoinTable)6 ManyToMany (javax.persistence.ManyToMany)3 ManyToOne (javax.persistence.ManyToOne)3 OneToMany (javax.persistence.OneToMany)3 Trans (org.eweb4j.orm.jdbc.transaction.Trans)3 Master (test.po.Master)3 List (java.util.List)2 OrderBy (javax.persistence.OrderBy)2 Pet (test.po.Pet)2 OneToOne (javax.persistence.OneToOne)1