Example #1
0
 public String getDDL() {
   final StringBuffer sql_columns = new StringBuffer();
   for (final MIndexColumn ic : getColumns()) {
     if (sql_columns.length() > 0) {
       sql_columns.append(",");
     }
     sql_columns.append(ic.getColumnName());
   }
   if (sql_columns.length() == 0) {
     throw new AdempiereException("Index has no columns defined");
   }
   //
   final StringBuffer sql = new StringBuffer("CREATE ");
   if (isUnique()) {
     sql.append("UNIQUE ");
   }
   sql.append("INDEX ")
       .append(getName())
       .append(" ON ")
       .append(getTableName())
       .append(" (")
       .append(sql_columns)
       .append(")");
   //
   final String whereClause = getWhereClause();
   if (!Check.isEmpty(whereClause, true)) {
     if (DB.isPostgreSQL()) {
       sql.append(" WHERE ").append(whereClause);
     } else {
       throw new AdempiereException("Partial Index not supported for this database");
     }
   }
   //
   return sql.toString();
 }
Example #2
0
  /**
   * Validate table data and throw exception if any error occur
   *
   * @param trxName
   * @throws DBUniqueConstraintException if not unique data found
   */
  public void validateData(String trxName) {
    if (!isActive()) {
      return;
    }
    if (!isUnique()) {
      return;
    }
    //
    final StringBuffer sqlGroupBy = new StringBuffer();
    for (final MIndexColumn c : getColumns()) {
      if (sqlGroupBy.length() > 0) {
        sqlGroupBy.append(",");
      }
      sqlGroupBy.append(c.getColumnName());
    }
    if (sqlGroupBy.length() == 0) {
      return;
    }

    //
    final StringBuffer sql = new StringBuffer("SELECT " + sqlGroupBy + " FROM " + getTableName());
    final String whereClause = getWhereClause();

    if (!Check.isEmpty(whereClause, true)) {
      sql.append(" WHERE ").append(whereClause);
    }
    //
    sql.append(" GROUP BY ").append(sqlGroupBy);
    sql.append(" HAVING COUNT(1) > 1");
    //
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt = DB.prepareStatement(sql.toString(), trxName);
      rs = pstmt.executeQuery();
      if (rs.next()) {
        throw new DBUniqueConstraintException(this);
      }
    } catch (final SQLException e) {
      throw new DBException(e, sql.toString());
    } finally {
      DB.close(rs, pstmt);
      rs = null;
      pstmt = null;
    }
  }
Example #3
0
  /**
   * Get SQL DDL
   *
   * @return DDL
   */
  private String createDDL() {
    StringBuilder sql = null;
    if (!isCreateConstraint()) {
      sql = new StringBuilder("CREATE ");
      if (isUnique()) sql.append("UNIQUE ");
      sql.append("INDEX ").append(getName()).append(" ON ").append(getTableName()).append(" (");
      //
      getColumns(false);
      for (int i = 0; i < m_columns.length; i++) {
        MIndexColumn ic = m_columns[i];
        if (i > 0) sql.append(",");
        sql.append(ic.getColumnName());
      }

      sql.append(")");
    } else if (isUnique()) {
      sql =
          new StringBuilder("ALTER TABLE ")
              .append(getTableName())
              .append(" ADD CONSTRAINT ")
              .append(getName());
      if (isKey()) sql.append(" PRIMARY KEY (");
      else sql.append(" UNIQUE (");
      getColumns(false);
      for (int i = 0; i < m_columns.length; i++) {
        MIndexColumn ic = m_columns[i];
        if (i > 0) sql.append(",");
        sql.append(ic.getColumnName());
      }

      sql.append(")");
    } else {
      String errMsg =
          Msg.getMsg(
              getCtx(), "NeitherTableIndexNorUniqueConstraint", new Object[] {getTableName()});
      log.severe(errMsg);
      throw new AdempiereException(errMsg);
    }

    return sql.toString();
  }