From 1e5e0d4e5f4864333e268e7d3c6e36869c8f9ca5 Mon Sep 17 00:00:00 2001 From: brinn <brinn> Date: Mon, 18 Jun 2007 15:49:07 +0000 Subject: [PATCH] add new method check() for checking the NOT NULL constraints of arbitrary objects SVN: 558 --- .../cisd/common/db/DBRestrictions.java | 40 ++++++++++++++----- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/common/source/java/ch/systemsx/cisd/common/db/DBRestrictions.java b/common/source/java/ch/systemsx/cisd/common/db/DBRestrictions.java index 62ad0f9db01..5d88f0d7f55 100644 --- a/common/source/java/ch/systemsx/cisd/common/db/DBRestrictions.java +++ b/common/source/java/ch/systemsx/cisd/common/db/DBRestrictions.java @@ -243,16 +243,7 @@ public class DBRestrictions final DBTableRestrictions restrictions = tableMap.get(tableName); assert restrictions != null : "Illegal table " + tableName; final int maxLength = restrictions.getLength(columnName); - if (value == null) - { - if (restrictions.hasNotNullConstraint(columnName)) - { - final String msg = String.format("Value 'NULL' not allowed for column %s.%s.", tableName, columnName); - operationLog.warn("Violation of database constraints detected: " + msg); - throw new UserFailureException(msg); - } - return; - } + checkNotNullConstraint(tableName, columnName, value, restrictions); final Set<String> checkedConstraint = restrictions.getCheckedConstaint(columnName); if (checkedConstraint != null && checkedConstraint.contains(value) == false) { @@ -272,6 +263,35 @@ public class DBRestrictions } } + /** + * Check <var>value</var> against the restrictions (maximal length and possible check constraints) of column + * <var>columnName</var> of table <var>tableName</var>. + * + * @throws UserFailureException If the <var>value</var> violates the constraints. + */ + public void check(String tableName, String columnName, Object value) throws UserFailureException + { + final DBTableRestrictions restrictions = tableMap.get(tableName); + assert restrictions != null : "Illegal table " + tableName; + checkNotNullConstraint(tableName, columnName, value, restrictions); + } + + private void checkNotNullConstraint(String tableName, String columnName, Object value, DBTableRestrictions restrictions) + throws UserFailureException + { + assert restrictions != null; + if (value == null) + { + if (restrictions.hasNotNullConstraint(columnName)) + { + final String msg = String.format("Value 'NULL' not allowed for column %s.%s.", tableName, columnName); + operationLog.warn("Violation of database constraints detected: " + msg); + throw new UserFailureException(msg); + } + return; + } + } + private String toString(Set<String> alternatives) { final StringBuilder builder = new StringBuilder(); -- GitLab