예제 #1
0
  public void delete(Connection conn, WhereElement emt, Object o) {
    String sql = WhereElementUtil.toSql(emt);
    List byColumns = new LinkedList();
    List hints = new LinkedList();
    WhereElementUtil.extractColumns(emt, byColumns, o, hints);

    PreparedStatement pstmt = null;
    try {
      pstmt = conn.prepareStatement("DELETE FROM " + table.getName() + " " + sql);

      if (byColumns.size() > 0) {
        int i = 1;
        Iterator hintIter = hints.iterator();
        for (Iterator columnIter = byColumns.iterator(); columnIter.hasNext(); ) {
          Column column = (Column) columnIter.next();
          ColumnAdapter adapter = column.getAdapter();
          adapter.setPreparedStatement(pstmt, i, hintIter.next());
          i++;
        }
      }

      pstmt.executeUpdate();
    } catch (SQLException e) {
      logger.error("", e);
      throw new DBLevelException(e);
    } finally {
      DBUtil.close(pstmt);
    }
  }
예제 #2
0
    public void doInPage(Collection coll) throws SQLException {
      for (Iterator iter = coll.iterator(); iter.hasNext(); ) {
        Object o = iter.next();
        for (int colIndex = 0; colIndex < columns.length; colIndex++) {
          Column column = columns[colIndex];
          ColumnAdapter columnAdapter = column.getAdapter();
          columnAdapter.setPreparedStatement(pstmt, colIndex + 1, o);
        }
        pstmt.addBatch();
      }

      int[] pageResult = pstmt.executeBatch();
      // COPY insert page result into batch result
      for (int i = 0; i < pageResult.length; i++) {
        result.add(new Integer(pageResult[i]));
      }
    }
예제 #3
0
    public void doInPage(Object[] objs) throws SQLException {
      for (int osIndex = 0; osIndex < objs.length; osIndex++) {
        for (int colIndex = 0; colIndex < columns.length; colIndex++) {
          Column column = columns[colIndex];
          ColumnAdapter columnAdapter = column.getAdapter();
          columnAdapter.setPreparedStatement(pstmt, colIndex + 1, objs[osIndex]);
        }
        pstmt.addBatch();
      }

      int[] pageResult = pstmt.executeBatch();
      // COPY insert page result into batch result
      for (int i = 0; i < pageResult.length; i++) {
        result[index] = pageResult[i];
        index++;
      }
    }
예제 #4
0
  public void deleteByIdColumns(Connection conn, Object o) {
    PreparedStatement pstmt = null;
    try {
      pstmt = conn.prepareStatement(deleteSql);
      // set DELETE WHERE PART VALUES
      Column[] idColumns = table.getIdColumns();
      for (int i = 0; i < idColumns.length; i++) {
        Column column = idColumns[i];
        ColumnAdapter columnAdapter = column.getAdapter();
        columnAdapter.setPreparedStatement(pstmt, i + 1, o);
      }

      pstmt.executeUpdate();
    } catch (SQLException e) {
      logger.error("", e);

      throw new DBLevelException(e);
    } finally {
      DBUtil.close(pstmt);
    }
  }
예제 #5
0
  public void save(Connection conn, Object o) throws EntityExistException, DBLevelException {
    PreparedStatement pstmt = null;
    try {
      pstmt = conn.prepareStatement(insertSql);
      Column[] columns = table.getColumns();
      for (int i = 0; i < columns.length; i++) {
        Column column = columns[i];
        ColumnAdapter columnAdapter = column.getAdapter();
        columnAdapter.setPreparedStatement(pstmt, i + 1, o);
      }

      pstmt.executeUpdate();
    } catch (SQLException e) {
      logger.error("", e);

      if (conn != null) {
        // violate pk or unique constraints?
        if (!Util.isEmpty(table.getIdColumns())) {
          boolean isExist = selector.isExistByIdColumns(conn, o);
          if (isExist) {
            throw new EntityExistException(o);
          }
        }

        if (!Util.isEmpty(table.getUniqueColumns())) {
          boolean isExist = selector.isExistByUniqueColumns(conn, o);
          if (isExist) {
            throw new EntityExistException(o);
          }
        }

        throw new DBLevelException(e);
      } else {
        throw new DBLevelException(e);
      }
    } finally {
      DBUtil.close(pstmt);
    }
  }