IDEMPIERE-4847 TableIndexValidate: include only active columns in table index (#741)
* TableIndexValidate: include only active columns in table index * simplified MTableIndex.getColumns() * MTableIndex.createDDL() - restored space positioning Co-authored-by: Andreas <sumerauer@kanzlei-wmv.de>
This commit is contained in:
parent
73b389652f
commit
0bfeb8c0b0
|
@ -110,6 +110,8 @@ public class MTableIndex extends X_AD_TableIndex {
|
|||
/** Index Create DDL */
|
||||
private String m_ddl = null;
|
||||
|
||||
private String m_whereClause = "";
|
||||
|
||||
/**
|
||||
* Get index columns
|
||||
* @param reload reload data
|
||||
|
@ -117,10 +119,27 @@ public class MTableIndex extends X_AD_TableIndex {
|
|||
*/
|
||||
public MIndexColumn[] getColumns(boolean reload)
|
||||
{
|
||||
if (m_columns != null && !reload)
|
||||
return getColumns(reload, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get index columns
|
||||
* @param reload reload data
|
||||
* @param activeOnly return active records only
|
||||
* @return array of index column
|
||||
*/
|
||||
public MIndexColumn[] getColumns(boolean reload, boolean activeOnly)
|
||||
{
|
||||
StringBuilder where = new StringBuilder(MIndexColumn.COLUMNNAME_AD_TableIndex_ID).append("=?");
|
||||
if(activeOnly)
|
||||
where.append(" AND IsActive='Y'");
|
||||
String whereClause = where.toString();
|
||||
|
||||
if (m_columns != null && !reload && m_whereClause.equalsIgnoreCase(whereClause) )
|
||||
return m_columns;
|
||||
|
||||
Query query = new Query(getCtx(), MIndexColumn.Table_Name, MIndexColumn.COLUMNNAME_AD_TableIndex_ID + "=?", get_TrxName());
|
||||
m_whereClause = whereClause;
|
||||
Query query = new Query(getCtx(), MIndexColumn.Table_Name, whereClause, get_TrxName());
|
||||
query.setParameters(getAD_TableIndex_ID());
|
||||
query.setOrderBy(MIndexColumn.COLUMNNAME_SeqNo);
|
||||
List<MIndexColumn> list = query.<MIndexColumn>list();
|
||||
|
@ -154,36 +173,16 @@ public class MTableIndex extends X_AD_TableIndex {
|
|||
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(")");
|
||||
.append(createColumnList());
|
||||
}
|
||||
else if (isUnique())
|
||||
{
|
||||
sql = new StringBuilder("ALTER TABLE ").append(getTableName()).append(" ADD CONSTRAINT ").append(getName());
|
||||
if (isKey())
|
||||
sql.append(" PRIMARY KEY (");
|
||||
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(")");
|
||||
sql.append(" UNIQUE");
|
||||
sql.append(createColumnList());
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -195,6 +194,22 @@ public class MTableIndex extends X_AD_TableIndex {
|
|||
return sql.toString();
|
||||
}
|
||||
|
||||
private String createColumnList() {
|
||||
getColumns(false, true);
|
||||
if (m_columns.length <= 0)
|
||||
throw new AdempiereException(Msg.getMsg(getCtx(), "NoIndexColumnsSpecified"));
|
||||
StringBuilder columnList = new StringBuilder(" (");
|
||||
for (int i = 0; i < m_columns.length; i++)
|
||||
{
|
||||
MIndexColumn ic = m_columns[i];
|
||||
if (i > 0)
|
||||
columnList.append(",");
|
||||
columnList.append(ic.getColumnName());
|
||||
}
|
||||
columnList.append(")");
|
||||
return columnList.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get SQL index create DDL
|
||||
* @return SQL DDL
|
||||
|
|
|
@ -87,7 +87,7 @@ public class TableIndexValidate extends SvrProcess {
|
|||
}
|
||||
rs.close();
|
||||
|
||||
MIndexColumn[] indexCols = index.getColumns(true);
|
||||
MIndexColumn[] indexCols = index.getColumns(true, true);
|
||||
boolean modified = false;
|
||||
|
||||
if (indexCols.length <= 0)
|
||||
|
|
Loading…
Reference in New Issue