Пример #1
0
  public void setTable(Table table) {
    this.table = table;

    if (!Util.isEmpty(table.getIdColumns())) {
      deleteSql = DBUtil.deleteSql(table.getName(), table.getIdColumnNames());
    }
  }
Пример #2
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);
    }
  }
Пример #3
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);
    }
  }