예제 #1
0
  public <T> Number[] deleteByFieldIsValue(Class<T>[] clazz, String[] fields, String[] values)
      throws DAOException {
    Number[] ids = null;
    Connection con = null;
    if (clazz == null || clazz.length == 0) return ids;

    ids = new Number[clazz.length];
    try {
      con = ds.getConnection();
      Object[] ts = new Object[clazz.length];
      for (int i = 0; i < clazz.length; ++i) {
        ts[i] = clazz[i].newInstance();
      }

      for (int i = 0; i < ts.length; i++) {
        String[] sqls = SqlFactory.getDeleteSql(ts[i]).delete(fields, values);
        if (sqls == null) ids[i] = -1;
        else {
          int rs = (Integer) JdbcUtil.update(con, sqls[0]);
          ids[i] = rs;
        }
      }
    } catch (Exception e) {
      throw new DAOException("", e);
    }

    return ids;
  }
예제 #2
0
파일: DAOImpl.java 프로젝트: neuqik/jlxt
  private <T> List<T> query(String sql) {
    List<T> result = null;
    try {

      if (Map.class.isAssignableFrom(this.clazz)) {
        Connection con = ds.getConnection();
        if (args != null && args.size() > 0) {
          result =
              (List<T>) JdbcUtil.getListWithArgs(con, clazz, sql, args.toArray(new Object[] {}));
        } else {
          result = (List<T>) JdbcUtil.getList(con, clazz, sql);
        }

      } else {
        if (args != null && args.size() > 0) {
          result =
              (List<T>)
                  DAOFactory.getSelectDAO(dsName)
                      .selectBySQL(clazz, sql, args.toArray(new Object[] {}));
        } else {
          result = (List<T>) DAOFactory.getSelectDAO(dsName).selectBySQL(clazz, sql);
        }
      }

      // this.clear();
      if (result != null && result.size() > 0) {
        for (T t : result) {
          // ToOne relation class cascade select
          final String[] fields = ORMConfigBeanUtil.getToOneField(t.getClass());
          if (fields == null || fields.length == 0) continue;

          DAOFactory.getCascadeDAO(dsName).select(t, fields);
        }
      }

      return result;
    } catch (Exception e) {
      log.error("sql-->" + sql + "exception:" + StringUtil.getExceptionString(e));
      throw new DAOException(sql + " execute exception", e);
    }
  }
예제 #3
0
  public <T> Number[] updateBySQLWithArgs(String[] sqls, Object[][] args) throws DAOException {
    Number[] result = null;
    Connection con = null;
    try {
      con = ds.getConnection();
      result = JdbcUtil.updateWithArgs(con, sqls, args);
    } catch (Exception e) {
      throw new DAOException("updateBySQLWithArgs exception ", e);
    }

    return result;
  }
예제 #4
0
  public <T> Number[] updateBySQL(String... sqls) throws DAOException {
    Number[] result = null;
    Connection con = null;
    try {
      con = ds.getConnection();
      result = JdbcUtil.update(con, sqls);
    } catch (Exception e) {
      throw new DAOException("updateBySQL exception ", e);
    }

    return result;
  }
예제 #5
0
파일: DAOImpl.java 프로젝트: neuqik/jlxt
  public int execute() {
    int id = -1;
    String sql =
        this.sql.toString().replace("${_where_}", this.condition.toString()).replace("'?'", "?");
    DataSource ds = DataSourceWrapCache.get(dsName);
    try {
      int rs = 0;
      if (args != null && args.size() > 0) {
        rs =
            (Integer)
                JdbcUtil.updateWithArgs(ds.getConnection(), sql, args.toArray(new Object[] {}));
      } else {
        rs = (Integer) JdbcUtil.update(ds.getConnection(), sql);
      }

      if (rs > 0 && sql.contains("INSERT INTO")) {
        if (Map.class.isAssignableFrom(clazz)) {
          if (map == null) {
            map = new HashMap<String, Object>();
            map.put("idColumn", "id");
            map.put("table", this.table);
          } else if (map.get("idColumn") == null) {
            map.put("idColumn", "id");
          }

          id = (Integer) DAOUtil.selectMaxId(map, ds.getConnection(), dbType);
        } else {
          id = (Integer) DAOUtil.selectMaxId(clazz, ds.getConnection(), dbType);
        }
      }

    } catch (SQLException e) {
      log.error("sql-->" + sql + "exception:" + StringUtil.getExceptionString(e));
      throw new DAOException(sql + " execute exception", e);
    }

    // this.clear();
    return id;
  }
예제 #6
0
 public <T> Number deleteByFields(T t, String[] fields) throws DAOException {
   Number id = 0;
   if (t != null) {
     Connection con = null;
     try {
       con = ds.getConnection();
       String[] sqls = SqlFactory.getDeleteSql(new Object[] {t}).delete(fields);
       id = JdbcUtil.update(con, sqls[0]);
     } catch (Exception e) {
       throw new DAOException("", e);
     }
   }
   return id;
 }
예제 #7
0
 public <T> Number deleteByFieldIsValue(Class<T> clazz, String[] fields, String[] values)
     throws DAOException {
   Number id = 0;
   if (clazz != null && fields != null && values != null && fields.length == values.length) {
     Connection con = null;
     try {
       con = ds.getConnection();
       String[] sqls =
           SqlFactory.getDeleteSql(new Object[] {clazz.newInstance()}).delete(fields, values);
       id = JdbcUtil.update(con, sqls[0]);
     } catch (Exception e) {
       throw new DAOException("", e);
     }
   }
   return id;
 }
예제 #8
0
 public <T> Number[] updateByFields(T[] ts, String... fields) throws DAOException {
   Number[] ids = null;
   if (ts != null && ts.length > 0 && fields != null && fields.length > 0) {
     Connection con = null;
     ids = new Number[ts.length];
     try {
       con = ds.getConnection();
       for (int i = 0; i < ts.length; i++) {
         String[] sqls = SqlFactory.getUpdateSql(ts).update(fields);
         ids[i] = JdbcUtil.update(con, sqls[i]);
         // 更新缓存
       }
     } catch (Exception e) {
       throw new DAOException("updateByFields exception ", e);
     }
   }
   return ids;
 }
예제 #9
0
 public <T> Number[] batchUpdate(T... ts) throws DAOException {
   Number[] ids = null;
   if (ts != null && ts.length > 0) {
     Connection con = null;
     ids = new Number[ts.length];
     try {
       con = ds.getConnection();
       for (int i = 0; i < ts.length; i++) {
         String[] sqls = SqlFactory.getUpdateSql(new Object[] {ts[i]}).update();
         ids[i] = JdbcUtil.update(con, sqls[i]);
         // 更新缓存
       }
     } catch (Exception e) {
       throw new DAOException("batchUpdate exception ", e);
     }
   }
   return ids;
 }
예제 #10
0
 public <T> Number deleteWhere(Class<T> clazz, String condition, Object[] args)
     throws DAOException {
   Number id = 0;
   if (clazz != null) {
     Connection con = null;
     try {
       con = ds.getConnection();
       String sql =
           SqlFactory.getDeleteSql(new Object[] {clazz.newInstance()})
               .deleteWhere(ORMConfigBeanUtil.parseQuery(condition, clazz));
       id = JdbcUtil.updateWithArgs(con, sql, args);
       // 缓存
     } catch (Exception e) {
       throw new DAOException("", e);
     }
   }
   return id;
 }
예제 #11
0
  public <T> Number[] batchDelete(T... ts) throws DAOException {
    Number[] ids = null;
    Connection con = null;
    if (ts == null || ts.length == 0) return ids;

    ids = new Number[ts.length];
    try {
      con = ds.getConnection();
      for (int i = 0; i < ts.length; i++) {
        String[] sqls = SqlFactory.getDeleteSql(new Object[] {ts[i]}).delete();
        if (sqls == null) ids[i] = -1;
        else {
          int rs = (Integer) JdbcUtil.update(con, sqls[0]);
          ids[i] = rs;
        }
      }
    } catch (Exception e) {
      throw new DAOException("", e);
    }

    return ids;
  }