IDEMPIERE-1132 Improve AD_Index; AD_View; foreign keys management - Validation using unique AD_TableIndex

This commit is contained in:
Elaine Tan 2013-07-16 19:08:06 +08:00
parent 0bdbe4a99c
commit a8731ef4a5
4 changed files with 33 additions and 9 deletions

View File

@ -338,6 +338,8 @@ public interface AdempiereDatabase
* @return true if lock is granted
*/
public boolean forUpdate(PO po, int timeout);
public String getNameOfUniqueConstraintError(Exception e);
} // AdempiereDatabase

View File

@ -4423,26 +4423,24 @@ public abstract class PO
if ("DBExecuteError".equals(msg))
info = "DBExecuteError:" + info;
// Unique Constraint
if (DBException.isUniqueContraintError(CLogger.retrieveException()))
Exception e = CLogger.retrieveException();
if (DBException.isUniqueContraintError(e))
{
boolean found = false;
String dbIndexName = DB.getDatabase().getNameOfUniqueConstraintError(e);
if (log.isLoggable(Level.FINE)) log.fine("dbIndexName=" + dbIndexName);
MTableIndex[] indexes = MTableIndex.get(MTable.get(getCtx(), get_Table_ID()));
for (MTableIndex index : indexes)
{
String indexName = index.getName().toLowerCase();
if (DB.isPostgreSQL())
indexName = "\"" + indexName + "\"";
else
indexName = "." + indexName + ")";
if (info.toLowerCase().contains(indexName))
if (dbIndexName.equalsIgnoreCase(index.getName()))
{
if (index.getAD_Message_ID() > 0)
{
MMessage message = MMessage.get(getCtx(), index.getAD_Message_ID());
log.saveError("SaveError", message.getMsgText());
log.saveError("SaveError", Msg.getMsg(getCtx(), message.getValue()));
found = true;
break;
}
break;
}
}

View File

@ -1380,4 +1380,16 @@ public class DB_Oracle implements AdempiereDatabase
}
return false;
}
@Override
public String getNameOfUniqueConstraintError(Exception e) {
String info = e.getMessage();
int fromIndex = info.indexOf(".");
if (fromIndex == -1)
return info;
int toIndex = info.indexOf(")", fromIndex + 1);
if (toIndex == -1)
return info;
return info.substring(fromIndex + 1, toIndex);
}
} // DB_Oracle

View File

@ -1138,4 +1138,16 @@ public class DB_PostgreSQL implements AdempiereDatabase
}
return false;
}
@Override
public String getNameOfUniqueConstraintError(Exception e) {
String info = e.getMessage();
int fromIndex = info.indexOf("\"");
if (fromIndex == -1)
return info;
int toIndex = info.indexOf("\"", fromIndex + 1);
if (toIndex == -1)
return info;
return info.substring(fromIndex + 1, toIndex);
}
} // DB_PostgreSQL