Example #1
0
  public void deleteRelation(Connection conn, Object leftValue, Object rightValue)
      throws PersistenceException {

    PreparedStatement stmt = null;
    try {
      int count = 1;
      stmt = conn.prepareStatement(delete);
      if (leftType.length > 1) {
        Complex left = (Complex) leftValue;
        for (int i = 0; i < left.size(); i++) {
          stmt.setObject(count, idToSQL(i, left.get(i)), leftType[i]);
          count++;
        }
      } else {
        stmt.setObject(count, idToSQL(0, leftValue), leftType[0]);
        count++;
      }
      if (rightType.length > 1) {
        Complex right = (Complex) rightValue;
        for (int i = 0; i < right.size(); i++) {
          stmt.setObject(count, ridToSQL(i, right.get(i)), rightType[i]);
          count++;
        }
      } else {
        stmt.setObject(count, ridToSQL(0, rightValue), rightType[0]);
      }
      stmt.executeUpdate();
    } catch (SQLException e) {
      e.printStackTrace();
      throw new PersistenceException(e.toString());
    } finally {
      JDOUtils.closeStatement(stmt);
    }
  }
Example #2
0
  public void createRelation(Connection conn, Object leftValue, Object rightValue)
      throws PersistenceException {

    ResultSet rset = null;
    PreparedStatement selectStatement = null;
    PreparedStatement insertStatement = null;

    try {
      int count = 1;
      selectStatement = conn.prepareStatement(select);
      if (leftType.length > 1) {
        Complex left = (Complex) leftValue;
        for (int i = 0; i < left.size(); i++) {
          selectStatement.setObject(count, idToSQL(i, left.get(i)), leftType[i]);
          count++;
        }
      } else {
        selectStatement.setObject(count, idToSQL(0, leftValue), leftType[0]);
        count++;
      }
      if (rightType.length > 1) {
        Complex right = (Complex) rightValue;
        for (int i = 0; i < right.size(); i++) {
          selectStatement.setObject(count, ridToSQL(i, right.get(i)), rightType[i]);
          count++;
        }
      } else {
        selectStatement.setObject(count, ridToSQL(0, rightValue), rightType[0]);
      }
      count = 1;
      rset = selectStatement.executeQuery();

      insertStatement = conn.prepareStatement(insert);
      if (!rset.next()) {
        if (leftType.length > 1) {
          Complex left = (Complex) leftValue;
          for (int i = 0; i < left.size(); i++) {
            insertStatement.setObject(count, idToSQL(i, left.get(i)), leftType[i]);
            count++;
          }
        } else {
          insertStatement.setObject(count, idToSQL(0, leftValue), leftType[0]);
          count++;
        }
        if (rightType.length > 1) {
          Complex right = (Complex) rightValue;
          for (int i = 0; i < right.size(); i++) {
            insertStatement.setObject(count, ridToSQL(i, right.get(i)), rightType[i]);
            count++;
          }
        } else {
          insertStatement.setObject(count, ridToSQL(0, rightValue), rightType[0]);
        }
        insertStatement.executeUpdate();
      }
    } catch (SQLException e) {
      e.printStackTrace();
      throw new PersistenceException(e.toString());
    } finally {
      JDOUtils.closeResultSet(rset);
      JDOUtils.closeStatement(selectStatement);
      JDOUtils.closeStatement(insertStatement);
    }
  }