From 678c99027000886345e94a7a18890f34d04f5925 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Mon, 6 May 2013 10:34:26 -0700 Subject: [PATCH] Fix issue with primary keys, uniqueness and HSQLDB 2.2.4. Said version of HSQLDB freaks out if primary keys are included in the UNIQUE clause when creating a table. They're unique by definition, and the HSQLDB authors are clearly strong believers in DRY. So much so that RY is considered an error in this case. --- .../com/samskivert/jdbc/HsqldbLiaison.java | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/samskivert/jdbc/HsqldbLiaison.java b/src/main/java/com/samskivert/jdbc/HsqldbLiaison.java index 4b8660c2..bb73856a 100644 --- a/src/main/java/com/samskivert/jdbc/HsqldbLiaison.java +++ b/src/main/java/com/samskivert/jdbc/HsqldbLiaison.java @@ -5,6 +5,7 @@ package com.samskivert.jdbc; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; @@ -127,11 +128,24 @@ public class HsqldbLiaison extends BaseLiaison throw new IllegalArgumentException("Column name and definition number mismatch"); } - // make a set of unique constraints already provided - Set> uColSet = new HashSet>(); + // turn this into something less fiddly to work with + List uniqueCsts = new ArrayList(); if (uniqueConstraintColumns != null) { - for (String[] uCols : uniqueConstraintColumns) { - uColSet.add(Arrays.asList(uCols)); + uniqueCsts.addAll(Arrays.asList(uniqueConstraintColumns)); + } + + // note the set of single column unique constraints already provided (see method comment) + Set seenUniques = new HashSet(); + for (String[] uCols : uniqueCsts) { + if (uCols.length == 1) { + seenUniques.add(uCols[0]); + } + } + // primary key columns are also considered implicitly unique as of HSQL 2.2.4, and it will + // freak out if we also try to include them in the UNIQUE clause, so add those too + if (primaryKeyColumns != null) { + for (String pkCol : primaryKeyColumns) { + seenUniques.add(pkCol); } } @@ -144,13 +158,10 @@ public class HsqldbLiaison extends BaseLiaison // let's be nice and not mutate the caller's object newDefinitions[ii] = new ColumnDefinition( def.type, def.nullable, false, def.defaultValue); - // if a uniqueness constraint for this column was not in the - // uniqueConstraintColumns parameter, add such an entry - if (!uColSet.contains(Collections.singletonList(columns[ii]))) { - String[] newConstraint = new String[] { columns[ii] }; - uniqueConstraintColumns = (uniqueConstraintColumns == null) ? - new String[][] { newConstraint } : - ArrayUtil.append(uniqueConstraintColumns, newConstraint); + // if a uniqueness constraint for this column was not in the primaryKeys or + // uniqueConstraintColumns parameters, add the column to uniqueCsts + if (!seenUniques.contains(columns[ii])) { + uniqueCsts.add(new String[] { columns[ii] }); } } else { newDefinitions[ii] = def; @@ -158,8 +169,9 @@ public class HsqldbLiaison extends BaseLiaison } // now call the real implementation with our modified data - return super.createTableIfMissing( - conn, table, columns, newDefinitions, uniqueConstraintColumns, primaryKeyColumns); + return super.createTableIfMissing(conn, table, columns, newDefinitions, + uniqueCsts.toArray(new String[uniqueCsts.size()][]), + primaryKeyColumns); } @Override // from DatabaseLiaison