mirror of https://github.com/apache/openjpa.git
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
This commit is contained in:
parent
46734cf443
commit
15b5ec20dd
|
@ -18,18 +18,25 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.openjpa.persistence.jdbc.unique;
|
package org.apache.openjpa.persistence.jdbc.unique;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.StringTokenizer;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
|
|
||||||
import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
|
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.OpenJPAEntityManagerFactorySPI;
|
||||||
import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
|
import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
|
||||||
import org.apache.openjpa.persistence.jdbc.SQLSniffer;
|
import org.apache.openjpa.persistence.jdbc.SQLSniffer;
|
||||||
import org.apache.openjpa.persistence.test.SQLListenerTestCase;
|
import org.apache.openjpa.persistence.test.SQLListenerTestCase;
|
||||||
|
|
||||||
public class TestNamedUniqueConstraint extends SQLListenerTestCase {
|
public class TestNamedUniqueConstraint extends SQLListenerTestCase {
|
||||||
|
|
||||||
|
DBDictionary dict;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setUp(Object... props) {
|
public void setUp(Object... props) {
|
||||||
super.setUp(DROP_TABLES, NamedUniqueA.class, NamedUniqueB.class);
|
super.setUp(DROP_TABLES, NamedUniqueA.class, NamedUniqueB.class);
|
||||||
|
@ -50,26 +57,58 @@ public class TestNamedUniqueConstraint extends SQLListenerTestCase {
|
||||||
List<String> sqls = super.sql;
|
List<String> sqls = super.sql;
|
||||||
|
|
||||||
assertSQLFragnments(sqls, "CREATE TABLE N_UNIQUE_A",
|
assertSQLFragnments(sqls, "CREATE TABLE N_UNIQUE_A",
|
||||||
"uca_f1_f2 UNIQUE .*\\(f1, f2\\)",
|
getUniqueConstraint("uca_f1_f2 UNIQUE .*\\(f1, f2\\)"),
|
||||||
"uca_f3_f4 UNIQUE .*\\(f3, f4\\).*");
|
getUniqueConstraint("uca_f3_f4 UNIQUE .*\\(f3, f4\\).*"));
|
||||||
assertSQLFragnments(sqls, "CREATE TABLE N_UNIQUE_B",
|
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",
|
assertSQLFragnments(sqls, "CREATE TABLE N_UNIQUE_SECONDARY",
|
||||||
"uca_sf1 UNIQUE .*\\(sf1\\)");
|
getUniqueConstraint("uca_sf1 UNIQUE .*\\(sf1\\)"));
|
||||||
assertSQLFragnments(sqls, "CREATE TABLE N_UNIQUE_GENERATOR",
|
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",
|
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",
|
assertSQLFragnments(sqls, "CREATE TABLE N_U_COLL_TBL",
|
||||||
"ucb_f3 UNIQUE .*\\(f3\\).*");
|
getUniqueConstraint("ucb_f3 UNIQUE .*\\(f3\\).*"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean supportsUniqueConstraints() {
|
private boolean supportsUniqueConstraints() {
|
||||||
OpenJPAEntityManagerFactorySPI emfs = (OpenJPAEntityManagerFactorySPI)emf;
|
OpenJPAEntityManagerFactorySPI emfs = (OpenJPAEntityManagerFactorySPI)emf;
|
||||||
JDBCConfiguration jdbccfg = (JDBCConfiguration)emfs.getConfiguration();
|
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<String> tokens = new ArrayList<String>();
|
||||||
|
List<String> tokens1 = new ArrayList<String>();
|
||||||
|
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<String> list, String... keys) {
|
void assertSQLFragnments(List<String> list, String... keys) {
|
||||||
if (SQLSniffer.matches(list, keys))
|
if (SQLSniffer.matches(list, keys))
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -24,12 +24,16 @@ import java.util.List;
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
|
|
||||||
import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
|
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.OpenJPAEntityManagerFactorySPI;
|
||||||
import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
|
import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
|
||||||
import org.apache.openjpa.persistence.jdbc.SQLSniffer;
|
import org.apache.openjpa.persistence.jdbc.SQLSniffer;
|
||||||
import org.apache.openjpa.persistence.test.SQLListenerTestCase;
|
import org.apache.openjpa.persistence.test.SQLListenerTestCase;
|
||||||
|
|
||||||
public class TestNamedUniqueConstraintWithXMLDescriptor extends SQLListenerTestCase {
|
public class TestNamedUniqueConstraintWithXMLDescriptor extends SQLListenerTestCase {
|
||||||
|
DBDictionary dict;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setUp(Object... props) {
|
public void setUp(Object... props) {
|
||||||
super.setUp(DROP_TABLES, NamedUniqueA.class, NamedUniqueB.class);
|
super.setUp(DROP_TABLES, NamedUniqueA.class, NamedUniqueB.class);
|
||||||
|
@ -54,27 +58,39 @@ public class TestNamedUniqueConstraintWithXMLDescriptor extends SQLListenerTestC
|
||||||
List<String> sqls = super.sql;
|
List<String> sqls = super.sql;
|
||||||
|
|
||||||
assertSQLFragnments(sqls, "CREATE TABLE NX_UNIQUE_A",
|
assertSQLFragnments(sqls, "CREATE TABLE NX_UNIQUE_A",
|
||||||
"ucxa_f1_f2 UNIQUE .*\\(f1x, f2x\\)",
|
getUniqueConstraint("ucxa_f1_f2 UNIQUE .*\\(f1x, f2x\\)"),
|
||||||
"ucxa_f3_f4 UNIQUE .*\\(f3x, f4x\\).*");
|
getUniqueConstraint("ucxa_f3_f4 UNIQUE .*\\(f3x, f4x\\).*"));
|
||||||
assertSQLFragnments(sqls, "CREATE TABLE NX_UNIQUE_B",
|
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",
|
assertSQLFragnments(sqls, "CREATE TABLE NX_UNIQUE_SECONDARY",
|
||||||
"ucxa_sf1 UNIQUE .*\\(sf1x\\)");
|
getUniqueConstraint("ucxa_sf1 UNIQUE .*\\(sf1x\\)"));
|
||||||
assertSQLFragnments(sqls, "CREATE TABLE NX_UNIQUE_GENERATOR",
|
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",
|
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",
|
assertSQLFragnments(sqls, "CREATE TABLE NX_U_COLL_TBL",
|
||||||
"ucxb_f3 UNIQUE .*\\(f3x\\).*");
|
getUniqueConstraint("ucxb_f3 UNIQUE .*\\(f3x\\).*"));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean supportsUniqueConstraints() {
|
private boolean supportsUniqueConstraints() {
|
||||||
OpenJPAEntityManagerFactorySPI emfs = (OpenJPAEntityManagerFactorySPI)emf;
|
OpenJPAEntityManagerFactorySPI emfs = (OpenJPAEntityManagerFactorySPI)emf;
|
||||||
JDBCConfiguration jdbccfg = (JDBCConfiguration)emfs.getConfiguration();
|
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<String> list, String... keys) {
|
void assertSQLFragnments(List<String> list, String... keys) {
|
||||||
if (SQLSniffer.matches(list, keys))
|
if (SQLSniffer.matches(list, keys))
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in New Issue