From 15b5ec20ddf010f4a35bc5e05c3ddd942542b9b0 Mon Sep 17 00:00:00 2001 From: Fay Wang Date: Fri, 11 Sep 2009 21:05:30 +0000 Subject: [PATCH] fix unique constraint syntax in the test case for MySQL git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@814029 13f79535-47bb-0310-9956-ffa450edef68 --- .../unique/TestNamedUniqueConstraint.java | 55 ++++++++++++++++--- ...amedUniqueConstraintWithXMLDescriptor.java | 34 +++++++++--- 2 files changed, 72 insertions(+), 17 deletions(-) diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestNamedUniqueConstraint.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestNamedUniqueConstraint.java index e0d992bad..b6919e6a6 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestNamedUniqueConstraint.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestNamedUniqueConstraint.java @@ -18,18 +18,25 @@ */ package org.apache.openjpa.persistence.jdbc.unique; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.StringTokenizer; import javax.persistence.EntityManager; import org.apache.openjpa.jdbc.conf.JDBCConfiguration; +import org.apache.openjpa.jdbc.sql.DBDictionary; +import org.apache.openjpa.jdbc.sql.MySQLDictionary; import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI; import org.apache.openjpa.persistence.jdbc.SQLSniffer; import org.apache.openjpa.persistence.test.SQLListenerTestCase; public class TestNamedUniqueConstraint extends SQLListenerTestCase { + + DBDictionary dict; + @Override public void setUp(Object... props) { super.setUp(DROP_TABLES, NamedUniqueA.class, NamedUniqueB.class); @@ -50,26 +57,58 @@ public class TestNamedUniqueConstraint extends SQLListenerTestCase { List sqls = super.sql; assertSQLFragnments(sqls, "CREATE TABLE N_UNIQUE_A", - "uca_f1_f2 UNIQUE .*\\(f1, f2\\)", - "uca_f3_f4 UNIQUE .*\\(f3, f4\\).*"); + getUniqueConstraint("uca_f1_f2 UNIQUE .*\\(f1, f2\\)"), + getUniqueConstraint("uca_f3_f4 UNIQUE .*\\(f3, f4\\).*")); assertSQLFragnments(sqls, "CREATE TABLE N_UNIQUE_B", - "ucb_f1_f2 UNIQUE .*\\(f1, f2\\).*"); + getUniqueConstraint("ucb_f1_f2 UNIQUE .*\\(f1, f2\\).*")); assertSQLFragnments(sqls, "CREATE TABLE N_UNIQUE_SECONDARY", - "uca_sf1 UNIQUE .*\\(sf1\\)"); + getUniqueConstraint("uca_sf1 UNIQUE .*\\(sf1\\)")); assertSQLFragnments(sqls, "CREATE TABLE N_UNIQUE_GENERATOR", - "ucb_gen1_gen2 UNIQUE .*\\(GEN1, GEN2\\)"); + getUniqueConstraint("ucb_gen1_gen2 UNIQUE .*\\(GEN1, GEN2\\)")); assertSQLFragnments(sqls, "CREATE TABLE N_UNIQUE_JOINTABLE", - "uca_fka_fkb UNIQUE .*\\(FK_A, FK_B\\)"); + getUniqueConstraint("uca_fka_fkb UNIQUE .*\\(FK_A, FK_B\\)")); assertSQLFragnments(sqls, "CREATE TABLE N_U_COLL_TBL", - "ucb_f3 UNIQUE .*\\(f3\\).*"); + getUniqueConstraint("ucb_f3 UNIQUE .*\\(f3\\).*")); } private boolean supportsUniqueConstraints() { OpenJPAEntityManagerFactorySPI emfs = (OpenJPAEntityManagerFactorySPI)emf; JDBCConfiguration jdbccfg = (JDBCConfiguration)emfs.getConfiguration(); - return jdbccfg.getDBDictionaryInstance().supportsUniqueConstraints; + dict = jdbccfg.getDBDictionaryInstance(); + return dict.supportsUniqueConstraints; } + private String getUniqueConstraint(String unique) { + if (dict instanceof MySQLDictionary) { + //CREATE TABLE N_UNIQUE_A (aid INTEGER NOT NULL, f1 INTEGER NOT NULL, f2 INTEGER NOT NULL, + //f3 INTEGER NOT NULL, f4 INTEGER NOT NULL, f5 INTEGER, f6 INTEGER, PRIMARY KEY (aid), + //UNIQUE U_N_UNQU__F1 (f1), + //UNIQUE uca_f1_f2 (f1, f2), + //UNIQUE uca_f3_f4 (f3, f4)) TYPE = innodb + return getUniqueConstraint(dict, unique); + } + return unique; + } + + public static String getUniqueConstraint(DBDictionary dict, String unique) { + List tokens = new ArrayList(); + List tokens1 = new ArrayList(); + StringTokenizer st = new StringTokenizer(unique); + while (st.hasMoreTokens()) { + tokens.add(st.nextToken()); + } + tokens1.add(tokens.get(1)); + tokens1.add(tokens.get(0)); + for (int i = 2; i < tokens.size(); i++) { + tokens1.add(tokens.get(i)); + } + StringBuffer buf = new StringBuffer(); + for (String token : tokens1) { + buf.append(token).append(" "); + } + return buf.toString().trim(); + } + void assertSQLFragnments(List list, String... keys) { if (SQLSniffer.matches(list, keys)) return; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestNamedUniqueConstraintWithXMLDescriptor.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestNamedUniqueConstraintWithXMLDescriptor.java index 005d9fd19..cb3a00695 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestNamedUniqueConstraintWithXMLDescriptor.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestNamedUniqueConstraintWithXMLDescriptor.java @@ -24,12 +24,16 @@ import java.util.List; import javax.persistence.EntityManager; import org.apache.openjpa.jdbc.conf.JDBCConfiguration; +import org.apache.openjpa.jdbc.sql.DBDictionary; +import org.apache.openjpa.jdbc.sql.MySQLDictionary; import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI; import org.apache.openjpa.persistence.jdbc.SQLSniffer; import org.apache.openjpa.persistence.test.SQLListenerTestCase; public class TestNamedUniqueConstraintWithXMLDescriptor extends SQLListenerTestCase { + DBDictionary dict; + @Override public void setUp(Object... props) { super.setUp(DROP_TABLES, NamedUniqueA.class, NamedUniqueB.class); @@ -54,27 +58,39 @@ public class TestNamedUniqueConstraintWithXMLDescriptor extends SQLListenerTestC List sqls = super.sql; assertSQLFragnments(sqls, "CREATE TABLE NX_UNIQUE_A", - "ucxa_f1_f2 UNIQUE .*\\(f1x, f2x\\)", - "ucxa_f3_f4 UNIQUE .*\\(f3x, f4x\\).*"); + getUniqueConstraint("ucxa_f1_f2 UNIQUE .*\\(f1x, f2x\\)"), + getUniqueConstraint("ucxa_f3_f4 UNIQUE .*\\(f3x, f4x\\).*")); assertSQLFragnments(sqls, "CREATE TABLE NX_UNIQUE_B", - "ucxb_f1_f2 UNIQUE .*\\(f1x, f2x\\).*"); + getUniqueConstraint("ucxb_f1_f2 UNIQUE .*\\(f1x, f2x\\).*")); assertSQLFragnments(sqls, "CREATE TABLE NX_UNIQUE_SECONDARY", - "ucxa_sf1 UNIQUE .*\\(sf1x\\)"); + getUniqueConstraint("ucxa_sf1 UNIQUE .*\\(sf1x\\)")); assertSQLFragnments(sqls, "CREATE TABLE NX_UNIQUE_GENERATOR", - "ucxb_gen1_gen2 UNIQUE .*\\(GEN1_XML, GEN2_XML\\)"); + getUniqueConstraint("ucxb_gen1_gen2 UNIQUE .*\\(GEN1_XML, GEN2_XML\\)")); assertSQLFragnments(sqls, "CREATE TABLE NX_UNIQUE_JOINTABLE", - "ucxa_fka_fkb UNIQUE .*\\(FK_A_XML, FK_B_XML\\)"); + getUniqueConstraint("ucxa_fka_fkb UNIQUE .*\\(FK_A_XML, FK_B_XML\\)")); assertSQLFragnments(sqls, "CREATE TABLE NX_U_COLL_TBL", - "ucxb_f3 UNIQUE .*\\(f3x\\).*"); - + getUniqueConstraint("ucxb_f3 UNIQUE .*\\(f3x\\).*")); } private boolean supportsUniqueConstraints() { OpenJPAEntityManagerFactorySPI emfs = (OpenJPAEntityManagerFactorySPI)emf; JDBCConfiguration jdbccfg = (JDBCConfiguration)emfs.getConfiguration(); - return jdbccfg.getDBDictionaryInstance().supportsUniqueConstraints; + dict = jdbccfg.getDBDictionaryInstance(); + return dict.supportsUniqueConstraints; } + private String getUniqueConstraint(String unique) { + if (dict instanceof MySQLDictionary) { + //CREATE TABLE N_UNIQUE_A (aid INTEGER NOT NULL, f1 INTEGER NOT NULL, f2 INTEGER NOT NULL, + //f3 INTEGER NOT NULL, f4 INTEGER NOT NULL, f5 INTEGER, f6 INTEGER, PRIMARY KEY (aid), + //UNIQUE U_N_UNQU__F1 (f1), + //UNIQUE uca_f1_f2 (f1, f2), + //UNIQUE uca_f3_f4 (f3, f4)) TYPE = innodb + return TestNamedUniqueConstraint.getUniqueConstraint(dict, unique); + } + return unique; + } + void assertSQLFragnments(List list, String... keys) { if (SQLSniffer.matches(list, keys)) return;