Exemplo n.º 1
0
  private boolean existingRecord(SqlConn con) throws SQLException {

    con.prepareStatement("select 1 from " + table + " where " + PK + "=?");

    if (fields.get(PK).getType() == DAOutils.STRING_TYPE) con.setString(1, getValueAsString(PK));
    else con.setInt(1, getValueAsInt(PK));

    return con.exPrep(true).first();
  }
Exemplo n.º 2
0
  public void delete() {
    SqlConn con = null;
    try {

      con = ConnFactory.getConnection(DB);
      con.prepareStatement("delete from " + table + " where " + PK + "=?");

      if (fields.get(PK).getType() == DAOutils.STRING_TYPE) con.setString(1, getValueAsString(PK));
      else con.setInt(1, getValueAsInt(PK));

      con.exPrep();

    } catch (SQLException e) {
      logger.error(
          "Failed to delete dao with id " + getField(PK).getValueAsInt() + " from table " + table,
          e);
    } finally {
      if (con != null) con.close();
    }
  }
Exemplo n.º 3
0
  private int saveNewRecord(SqlConn con) throws SQLException {

    // CREATE STMT TO INSERT NEW ENTRY INTO DB
    StringBuilder sb = new StringBuilder("insert into " + table + "(");
    for (Field field : fields.values()) {
      if (field.getValue() == null) continue;
      logger.info(
          "adding field to insert clause: " + field.name + " val: " + field.getValueAsString());
      sb.append(field.name + ",");
    }
    sb.deleteCharAt(sb.length() - 1);
    sb.append(") values (");

    for (Field field : fields.values()) {
      if (field.getValue() == null) continue;
      if (field.getType() == DAOutils.PASSWORD_TYPE) {
        sb.append("password(?),");
      } else {
        sb.append("?,");
      }
    }
    sb.deleteCharAt(sb.length() - 1);
    sb.append(")");

    // FILL STMT TO INSERT NEW ENTRY INTO DB
    int ind = 1;
    logger.info("save dao new " + sb.toString());
    con.prepareStatement(sb.toString());
    for (Field field : fields.values()) {

      if (field.getValue() == null) continue;
      switch (field.getType()) {
        case DAOutils.STRING_TYPE:
          con.setString(ind++, field.getValueAsString());
          break;
        case DAOutils.INT_TYPE:
          con.setInt(ind++, field.getValueAsInt());
          break;
        case DAOutils.TIMESTAMP_TYPE:
          con.setTimestamp(ind++, (Timestamp) field.getValue());
          break;
        case DAOutils.FLOAT_TYPE:
          con.setFloat(ind++, ((Float) field.getValue()).floatValue());
          break;
        case DAOutils.DATE_TYPE:
          con.setDate(ind++, (Date) field.getValue());
          break;
        case DAOutils.PASSWORD_TYPE:
          logger.info("password is " + field.getValueAsString());
          con.setString(ind++, field.getValueAsString());
          break;
        case DAOutils.BLOB_TYPE:
          con.setBlob(ind++, (Blob) field.getValue());
          break;
      }
    }

    // EXECUTE UPDATE
    con.exPrep();

    // IF NEW INSERTION OF AUTO-INCREMENT PRIMARY KEY, RETURN THE AUTO-ID
    if (fields.get(PK).getType() == DAOutils.INT_TYPE && fields.get(PK).getValue() == null) {
      int id = con.getLastSerial(table, PK);
      return id;
    }

    return -1;
  }
Exemplo n.º 4
0
  private int saveExistingRecord(SqlConn con) throws SQLException {

    // CREATE STMT TO UPDATE ENTRY IN DB
    StringBuilder sb = new StringBuilder("update " + table + " set ");

    for (Field field : fields.values()) {
      if (field.getValue() == null) continue;
      if (field.getType() == DAOutils.PASSWORD_TYPE) {
        sb.append(field.name + "=password(?),");
      } else {
        sb.append(field.name + "=?,");
      }
    }

    sb.deleteCharAt(sb.length() - 1);
    if (fields.get(PK).getType() == DAOutils.STRING_TYPE)
      sb.append(" where " + PK + "='" + fields.get(PK).getValueAsString() + "'");
    else sb.append(" where " + PK + "=" + fields.get(PK).getValueAsInt());

    // FILL STMT TO UPDATE ENTRY IN DB
    int ind = 1;
    con.prepareStatement(sb.toString());
    for (Field field : fields.values()) {

      logger.debug(
          " saving field "
              + field.getName()
              + " with val "
              + field.getValueAsString()
              + " in table "
              + this.table
              + " with doa hash "
              + this.hashCode());
      if (field.getValue() == null) continue;

      switch (field.getType()) {
        case DAOutils.STRING_TYPE:
          con.setString(ind++, field.getValueAsString());
          break;
        case DAOutils.INT_TYPE:
          con.setInt(ind++, field.getValueAsInt());
          break;
        case DAOutils.TIMESTAMP_TYPE:
          con.setTimestamp(ind++, (Timestamp) field.getValue());
          break;
        case DAOutils.FLOAT_TYPE:
          con.setFloat(ind++, ((Float) field.getValue()).floatValue());
          break;
        case DAOutils.DATE_TYPE:
          con.setDate(ind++, (Date) field.getValue());
          break;
        case DAOutils.PASSWORD_TYPE:
          con.setString(ind++, field.getValueAsString());
          break;
        case DAOutils.BLOB_TYPE:
          con.setBlob(ind++, (Blob) field.getValue());
      }
    }

    // EXECUTE UPDATE
    con.exPrep();

    // IF STRING TYPE PK, WE KNOW WE'RE NOT DOING AUTO-INCREMENT,
    // SO IGNORE RETURNING A REAL INT.
    if (fields.get(PK).getType() == DAOutils.STRING_TYPE) return 0;

    return getValueAsInt(PK);
  }