示例#1
0
  protected int saveProcess(ArrayList array, Object object) {
    int index = -1;
    String table = "";
    int i = 2;
    try {
      if (this.forceTable) {
        table = tableForcedName;
        array.set(0, table);
      } else {
        table = String.valueOf(array.get(0));
      }

      conex.openBlock(table);

      int size = array.size() - 1;
      for (; i < size; i++) {
        Object[] obj = (Object[]) array.get(i);

        String column = (String) obj[0];
        Object value = obj[1];

        conex.insertField(column, value);
      }

      index = conex.closeBlock();
      if (this.collection) {
        this.saveMany2Many(((String) array.get(0)), true, index);
      }
      this.manager.updateCache(object.getClass(), this);
    } catch (SQLException ex) {
      int value = -1;
      try {
        this.conex.getConnection().setAutoCommit(true);
        if (!this.checkTableExist(table)) {
          if (this.createTable(object, array.toArray())) {
            value = this.saveProcess(array, object);
          }
        } else {
          if (this.addColumn(table, object, array, i)) {
            value = this.saveProcess(array, object);
          }
        }
      } catch (Exception e) {
      }
      return value;
    } catch (Exception e) {
      conex.connectMySQL();
      try {
        conex.cancelTransaction();
      } catch (Exception ex) {
        return -1;
      }
    }

    return index;
  }
示例#2
0
  /**
   * Delete the object received as parameter from the Database
   *
   * @param Object
   * @return True if the operation complete successfully, False in the other case.
   */
  public boolean delete(Object object) {

    try {
      boolean commitValue = this.commit;
      if (this.commit || this.autoCommit) {
        conex.initTransaction();
        this.commit = false;
      }

      ArrayList array = this.manager.entity2Array(this, object, EntityManager.OPERATION.DELETE);

      conex.deleteRows(((String) array.get(0)), ((String) array.get(array.size() - 1)));

      this.manager.deleteParent(this, object);

      // Obtain the attributes from this object that are collections
      // and delete their relations in the Relational Table.
      ArrayList<String[]> strings = this.manager.getCollectionsTableForDelete(object, this);
      int index = (Integer) ((Object[]) array.get(1))[1];
      for (String[] s : strings) {
        if (this.checkTableExist(s[0])) {
          this.executeQuery("DELETE FROM " + s[0] + " WHERE base=" + index);
        } else if (this.checkTableExist(s[1])) {
          this.executeQuery("DELETE FROM " + s[1] + " WHERE related=" + index);
        }
      }

      this.commit = commitValue;
      if (this.commit || this.autoCommit) conex.confirmTransaction();
    } catch (Exception e) {
      this.commit = true;
      try {
        conex.cancelTransaction();
      } catch (Exception ex) {
        return false;
      }
      return false;
    }

    return true;
  }
示例#3
0
  protected boolean modifyProcess(Object object) {
    ArrayList array = this.manager.entity2Array(this, object, EntityManager.OPERATION.MODIFY);

    try {
      this.rs =
          conex.updateField(((String) array.get(0)), String.valueOf(array.get(array.size() - 1)));
      rs.next();

      do {
        int size = array.size() - 1;
        for (int i = 1; i < size; i++) {
          Object[] obj = (Object[]) array.get(i);

          String column = (String) obj[0];
          Object value = obj[1];

          rs.updateObject(column, value);
          rs.updateRow();
        }
      } while (rs.next());

      int index =
          Integer.parseInt(
              String.valueOf(array.get(array.size() - 1))
                  .substring(String.valueOf(array.get(array.size() - 1)).indexOf("=") + 1));

      if (this.collection) {
        this.saveMany2Many(((String) array.get(0)), false, index);
      }
      this.manager.updateCache(object.getClass(), this);
    } catch (Exception e) {
      try {
        conex.cancelTransaction();
      } catch (Exception ex) {
        return false;
      }
      return false;
    }

    return true;
  }