IDEMPIERE-1132 Improve AD_Index; AD_View; foreign keys management - Foreign keys management
This commit is contained in:
parent
ea8b457a51
commit
4c6c12ea58
|
@ -120,14 +120,73 @@ public class ColumnSync extends SvrProcess
|
|||
DB.close(rs);
|
||||
rs = null;
|
||||
|
||||
boolean isNoTable = noColumns == 0;
|
||||
// No Table
|
||||
if (noColumns == 0)
|
||||
if (isNoTable)
|
||||
sql = table.getSQLCreate ();
|
||||
// No existing column
|
||||
else if (sql == null)
|
||||
sql = column.getSQLAdd(table);
|
||||
|
||||
// foreign key
|
||||
if (isNoTable)
|
||||
{
|
||||
MColumn[] cols = table.getColumns(false);
|
||||
for (MColumn col : cols)
|
||||
{
|
||||
String fkConstraintSql = getForeignKeyConstraintSql(md, catalog, schema, tableName, table, col);
|
||||
if (fkConstraintSql != null && fkConstraintSql.length() > 0)
|
||||
sql += fkConstraintSql;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
String fkConstraintSql = getForeignKeyConstraintSql(md, catalog, schema, tableName, table, column);
|
||||
if (fkConstraintSql != null && fkConstraintSql.length() > 0)
|
||||
sql += fkConstraintSql;
|
||||
}
|
||||
|
||||
int no = 0;
|
||||
if (sql.indexOf(DB.SQLSTATEMENT_SEPARATOR) == -1)
|
||||
{
|
||||
no = DB.executeUpdate(sql, false, get_TrxName());
|
||||
addLog (0, null, new BigDecimal(no), sql);
|
||||
}
|
||||
else
|
||||
{
|
||||
String statements[] = sql.split(DB.SQLSTATEMENT_SEPARATOR);
|
||||
for (int i = 0; i < statements.length; i++)
|
||||
{
|
||||
int count = DB.executeUpdateEx(statements[i], get_TrxName());
|
||||
addLog (0, null, new BigDecimal(count), statements[i]);
|
||||
no += count;
|
||||
}
|
||||
}
|
||||
|
||||
if (no == -1)
|
||||
{
|
||||
StringBuilder msg = new StringBuilder("@Error@ ");
|
||||
ValueNamePair pp = CLogger.retrieveError();
|
||||
if (pp != null)
|
||||
msg = new StringBuilder(pp.getName()).append(" - ");
|
||||
msg.append(sql);
|
||||
throw new AdempiereUserError (msg.toString());
|
||||
}
|
||||
return sql;
|
||||
} finally {
|
||||
DB.close(rs);
|
||||
rs = null;
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (Exception e) {}
|
||||
}
|
||||
}
|
||||
} // doIt
|
||||
|
||||
private String getForeignKeyConstraintSql(DatabaseMetaData md, String catalog, String schema, String tableName, MTable table, MColumn column) throws Exception
|
||||
{
|
||||
StringBuilder fkConstraintSql = new StringBuilder();
|
||||
|
||||
if (!column.isKey() && !column.getColumnName().equals(PO.getUUIDColumnName(table.getTableName())))
|
||||
{
|
||||
int refid = column.getAD_Reference_ID();
|
||||
|
@ -143,7 +202,7 @@ public class ColumnSync extends SvrProcess
|
|||
else if (md.storesLowerCaseIdentifiers())
|
||||
referenceTableName = referenceTableName.toLowerCase();
|
||||
|
||||
rs = md.getCrossReference(catalog, schema, referenceTableName, catalog, schema, tableName);
|
||||
ResultSet rs = md.getCrossReference(catalog, schema, referenceTableName, catalog, schema, tableName);
|
||||
while (rs.next())
|
||||
{
|
||||
String dbFKName = rs.getString("FK_NAME");
|
||||
|
@ -175,7 +234,6 @@ public class ColumnSync extends SvrProcess
|
|||
htForeignKeys.remove(key);
|
||||
}
|
||||
|
||||
StringBuilder fkConstraintSql = new StringBuilder();
|
||||
boolean modified = false;
|
||||
en = htForeignKeys.keys();
|
||||
while (en.hasMoreElements())
|
||||
|
@ -220,49 +278,10 @@ public class ColumnSync extends SvrProcess
|
|||
fkConstraintSql.append(fkConstraint);
|
||||
}
|
||||
}
|
||||
|
||||
if (fkConstraintSql != null && fkConstraintSql.length() > 0)
|
||||
sql += fkConstraintSql.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int no = 0;
|
||||
if (sql.indexOf(DB.SQLSTATEMENT_SEPARATOR) == -1)
|
||||
{
|
||||
no = DB.executeUpdate(sql, false, get_TrxName());
|
||||
addLog (0, null, new BigDecimal(no), sql);
|
||||
return fkConstraintSql.toString();
|
||||
}
|
||||
else
|
||||
{
|
||||
String statements[] = sql.split(DB.SQLSTATEMENT_SEPARATOR);
|
||||
for (int i = 0; i < statements.length; i++)
|
||||
{
|
||||
int count = DB.executeUpdateEx(statements[i], get_TrxName());
|
||||
addLog (0, null, new BigDecimal(count), statements[i]);
|
||||
no += count;
|
||||
}
|
||||
}
|
||||
|
||||
if (no == -1)
|
||||
{
|
||||
StringBuilder msg = new StringBuilder("@Error@ ");
|
||||
ValueNamePair pp = CLogger.retrieveError();
|
||||
if (pp != null)
|
||||
msg = new StringBuilder(pp.getName()).append(" - ");
|
||||
msg.append(sql);
|
||||
throw new AdempiereUserError (msg.toString());
|
||||
}
|
||||
return sql;
|
||||
} finally {
|
||||
DB.close(rs);
|
||||
rs = null;
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (Exception e) {}
|
||||
}
|
||||
}
|
||||
} // doIt
|
||||
|
||||
} // ColumnSync
|
||||
|
|
Loading…
Reference in New Issue