Skip to content
Snippets Groups Projects
Commit 8eef92b2 authored by brinn's avatar brinn
Browse files

add: "NOT NULL" constraint checking

SVN: 315
parent c3cda93f
No related merge requests found
......@@ -63,6 +63,8 @@ public class DBRestrictions
final Map<String, Set<String>> checkedConstraintsMap = new HashMap<String, Set<String>>();
final Set<String> notNullSet = new HashSet<String>();
public int getLength(String columnName)
{
final Integer columnLength = columnLengthMap.get(columnName);
......@@ -74,6 +76,11 @@ public class DBRestrictions
{
return checkedConstraintsMap.get(columnName);
}
public boolean hasNotNullConstraint(String columnName)
{
return notNullSet.contains(columnName);
}
}
public DBRestrictions(String ddlScript)
......@@ -168,6 +175,26 @@ public class DBRestrictions
.group(1)));
}
}
final int nullIdx = findInArray(words, "null", 3);
if (nullIdx > 0 && "not".equals(words[nullIdx - 1]))
{
getTableRestrictions(tableName).notNullSet.add(columnName);
}
}
private int findInArray(String[] array, String term, int firstIndex)
{
assert array != null;
assert term != null;
for (int i = firstIndex; i < array.length; ++i)
{
if (term.equals(array[i]))
{
return i;
}
}
return -1;
}
private void parserCheckedConstraints(List<String> ddlScript)
......@@ -225,7 +252,12 @@ public class DBRestrictions
final int maxLength = restrictions.getLength(columnName);
if (value == null)
{
// TODO 2007-06-04, Bernd Rinn: add correct 'NON NULL' check
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;
}
final Set<String> checkedConstraint = restrictions.getCheckedConstaint(columnName);
......
......@@ -86,26 +86,26 @@ public class DBRestrictionsTest
assertEquals(50, parser.getTableRestrictions("materials").getLength("name"));
assertEquals(4, parser.getTableRestrictions("materials").getLength("mate_sub_type"));
}
@Test(expectedExceptions = AssertionError.class)
public void testInvalidTable()
{
final DBRestrictions parser = new DBRestrictions("");
assertEquals(Integer.MAX_VALUE, parser.getTableRestrictions("doesnotexit").getLength("doesnotexist"));
}
@Test(expectedExceptions = AssertionError.class)
public void testInvalidColumn()
{
final DBRestrictions parser = new DBRestrictions("create table tab (a integer, b varchar(1))");
assertEquals(Integer.MAX_VALUE, parser.getTableRestrictions("tab").getLength("doesnotexist"));
}
@Test
public void testCheckedConstraints()
{
final String sqlScript =
FileUtilities.loadToString(getClass(), "/ch/systemsx/cisd/common/parser/DBRestrictionsTest.sql");
FileUtilities.loadToString(getClass(), "/ch/systemsx/cisd/common/parser/DBRestrictionsTest.sql");
assert sqlScript != null;
final DBRestrictions parser = new DBRestrictions(sqlScript);
......@@ -114,12 +114,12 @@ public class DBRestrictionsTest
assertEquals(new HashSet<String>(Arrays.asList("STOB", "MATE")), parser.getTableRestrictions("materials")
.getCheckedConstaint("mate_sub_type"));
}
@Test
public void testCheckOK()
{
final String sqlScript =
FileUtilities.loadToString(getClass(), "/ch/systemsx/cisd/common/parser/DBRestrictionsTest.sql");
FileUtilities.loadToString(getClass(), "/ch/systemsx/cisd/common/parser/DBRestrictionsTest.sql");
assert sqlScript != null;
final DBRestrictions parser = new DBRestrictions(sqlScript);
......@@ -131,7 +131,7 @@ public class DBRestrictionsTest
public void testCheckViolateLength()
{
final String sqlScript =
FileUtilities.loadToString(getClass(), "/ch/systemsx/cisd/common/parser/DBRestrictionsTest.sql");
FileUtilities.loadToString(getClass(), "/ch/systemsx/cisd/common/parser/DBRestrictionsTest.sql");
assert sqlScript != null;
final DBRestrictions parser = new DBRestrictions(sqlScript);
......@@ -149,7 +149,7 @@ public class DBRestrictionsTest
public void testCheckAlternatives()
{
final String sqlScript =
FileUtilities.loadToString(getClass(), "/ch/systemsx/cisd/common/parser/DBRestrictionsTest.sql");
FileUtilities.loadToString(getClass(), "/ch/systemsx/cisd/common/parser/DBRestrictionsTest.sql");
assert sqlScript != null;
final DBRestrictions parser = new DBRestrictions(sqlScript);
......@@ -162,4 +162,22 @@ public class DBRestrictionsTest
throw ex;
}
}
@Test(expectedExceptions = UserFailureException.class)
public void testCheckNotNullConstraint()
{
final String sqlScript =
FileUtilities.loadToString(getClass(), "/ch/systemsx/cisd/common/parser/DBRestrictionsTest.sql");
assert sqlScript != null;
final DBRestrictions parser = new DBRestrictions(sqlScript);
try
{
parser.check("material_types", "description", null);
} catch (UserFailureException ex)
{
System.err.println(ex.getMessage());
throw ex;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment