diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/TableJDBCSeq.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/TableJDBCSeq.java index ef9b81dd2..f472b8cfb 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/TableJDBCSeq.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/TableJDBCSeq.java @@ -404,11 +404,11 @@ public class TableJDBCSeq } Unique u = table.addUnique(uniqueName); for (String columnName : _uniqueColumnNames) { - if (!table.containsColumn(columnName)) + if (!table.containsColumn(columnName, _conf.getDBDictionaryInstance())) throw new UserException(_loc.get("unique-missing-column", columnName, table.getName(), table.getColumnNames())); - Column col = table.getColumn(columnName); + Column col = table.getColumn(columnName, _conf.getDBDictionaryInstance()); u.addColumn(col); } } diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/DynamicSchemaFactory.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/DynamicSchemaFactory.java index 2b6e286b8..464c7c6f1 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/DynamicSchemaFactory.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/DynamicSchemaFactory.java @@ -119,10 +119,14 @@ public class DynamicSchemaFactory } public Column getColumn(String name) { + return getColumn(name, null); + } + + public Column getColumn(String name, DBDictionary dict) { if (name == null) return null; - Column col = super.getColumn(name); + Column col = super.getColumn(name, dict); if (col != null) return col; diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/ForeignKey.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/ForeignKey.java index 7539123ef..7b002c8c1 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/ForeignKey.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/ForeignKey.java @@ -769,9 +769,9 @@ public class ForeignKey fkTemp.setDeleteAction(fks[i].getDeleteAction()); } if( ! fkTemp.containsColumn( - localtable.getColumn(fks[i].getColumnName()))) - fkTemp.join(localtable.getColumn(fks[i].getColumnName()), - pkTable.getColumn(fks[i].getPrimaryKeyColumnName())); + localtable.getColumn(fks[i].getColumnName(), dbdict))) + fkTemp.join(localtable.getColumn(fks[i].getColumnName(), dbdict), + pkTable.getColumn(fks[i].getPrimaryKeyColumnName(), dbdict)); if( equalsForeignKey(fkTemp)) { if(addFK) diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaGenerator.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaGenerator.java index 62665816b..1ea48c32b 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaGenerator.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaGenerator.java @@ -527,13 +527,8 @@ public class SchemaGenerator { if (_log.isTraceEnabled()) _log.trace(_loc.get("gen-column", cols[i].getName(), table)); - if (table.getColumn(cols[i].getName()) == null) { - // It's possible that the original column name was delimited, - // so delimit it and try again - String delimCol = _dict.addDelimiters(cols[i].getName()); - if (table.getColumn(delimCol) == null) { - table.importColumn(cols[i]); - } + if (table.getColumn(cols[i].getName(), _dict) == null) { + table.importColumn(cols[i]); } } } @@ -630,7 +625,7 @@ public class SchemaGenerator { pk = table.getPrimaryKey(); if (pk == null) pk = table.addPrimaryKey(name); - pk.addColumn(table.getColumn(colName)); + pk.addColumn(table.getColumn(colName, _dict)); } } @@ -692,7 +687,7 @@ public class SchemaGenerator { continue; colName = idxs[i].getColumnName(); - if (table.getColumn(colName) == null) + if (table.getColumn(colName, _dict) == null) continue; if (_log.isTraceEnabled()) @@ -704,7 +699,7 @@ public class SchemaGenerator { idx = table.addIndex(name); idx.setUnique(idxs[i].isUnique()); } - idx.addColumn(table.getColumn(colName)); + idx.addColumn(table.getColumn(colName, _dict)); } } @@ -804,13 +799,13 @@ public class SchemaGenerator { if (invalids == null || !invalids.contains(fk)) { try { - Column fkCol = table.getColumn(fkColName); + Column fkCol = table.getColumn(fkColName, _dict); if (fkCol == null) { throw new IllegalArgumentException(_loc.get( "no-column", fkColName, table.getName()) .getMessage()); } - fk.join(fkCol, pkTable.getColumn(pkColName)); + fk.join(fkCol, pkTable.getColumn(pkColName, _dict)); } catch (IllegalArgumentException iae) { if (_log.isWarnEnabled()) _log.warn(_loc.get("bad-join", iae.toString())); diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaTool.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaTool.java index 2800b1c61..c3c364a80 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaTool.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaTool.java @@ -486,7 +486,7 @@ public class SchemaTool { colName = colName.substring(1, colName.length()-1); delimCol = true; } - col = dbTable.getColumn(colName); + col = dbTable.getColumn(colName, _dict); if (col == null) { if (addColumn(cols[k])) dbTable.importColumn(cols[k]); @@ -724,7 +724,7 @@ public class SchemaTool { reposTable = repos.findTable(tabs[j]); if (reposTable != null) { for (int k = 0; k < cols.length; k++) { - col = reposTable.getColumn(cols[k].getName()); + col = reposTable.getColumn(cols[k].getName(), _dict); if (col == null || !cols[k].equalsColumn(col)) { if (tabs[j].getColumns().length == 1) drops.add(tabs[j]); @@ -797,7 +797,7 @@ public class SchemaTool { dbCols = dbTable.getColumns(); for (int k = 0; k < dbCols.length; k++) - if (tabs[j].getColumn(dbCols[k].getName()) == null) + if (tabs[j].getColumn(dbCols[k].getName(), _dict) == null) continue tables; drops.add(tabs[j]); @@ -869,7 +869,7 @@ public class SchemaTool { for (int k = 0; k < cols.length; k++) { col = null; if (dbTable != null) - col = dbTable.getColumn(cols[k].getName()); + col = dbTable.getColumn(cols[k].getName(), _dict); if (dbTable == null || col == null) continue; diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Table.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Table.java index 7dd895aef..eb2012ef8 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Table.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Table.java @@ -27,6 +27,7 @@ import java.util.TreeMap; import java.util.LinkedHashMap; import org.apache.commons.lang.StringUtils; +import org.apache.openjpa.jdbc.sql.DBDictionary; import org.apache.openjpa.lib.meta.SourceTracker; /** @@ -272,9 +273,30 @@ public class Table * Return the column with the given name, or null if none. */ public Column getColumn(String name) { + return getColumn(name, null); + } + + /** + * Return the column with the given name, or null if none. + * @param dict the current database dictionary or null. + */ + public Column getColumn(String name, DBDictionary dict) { if (name == null || _colMap == null) return null; - return (Column) _colMap.get(name.toUpperCase()); + Column col = (Column)_colMap.get(name.toUpperCase()); + if (col == null) { + String delim = null; + if (dict != null) { + delim = dict.getDelimiter(); + } + else { + delim = "\""; + } + String delimName = delim + name + delim; + col = (Column) _colMap.get(delimName.toUpperCase()); + } + + return col; } /** @@ -284,8 +306,37 @@ public class Table * for dynamic table implementation. */ public boolean containsColumn(String name) { - return name != null && _colMap != null - && _colMap.containsKey(name.toUpperCase()); + return containsColumn(name, null); + } + + /** + * Affirms if this table contains the column of the given name without any + * side-effect. + * @param dict the current database dictionary or null. + * @see Table#getColumn(String) can have side-effect of creating a column + * for dynamic table implementation. + */ + public boolean containsColumn(String name, DBDictionary dict) { + if (name == null || _colMap == null) { + return false; + } + if (_colMap.containsKey(name.toUpperCase())) { + return true; + } + + String delim = null; + if (dict != null) { + delim = dict.getDelimiter(); + } + else { + delim = "\""; + } + String delimName = delim + name + delim; + if (_colMap.containsKey(delimName.toUpperCase())) { + return true; + } + + return false; } /** diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/XMLSchemaParser.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/XMLSchemaParser.java index c2d293215..6d5e62806 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/XMLSchemaParser.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/XMLSchemaParser.java @@ -189,7 +189,7 @@ public class XMLSchemaParser pkInfo = (PrimaryKeyInfo) itr.next(); for (Iterator cols = pkInfo.cols.iterator(); cols.hasNext();) { colName = (String) cols.next(); - col = pkInfo.pk.getTable().getColumn(colName); + col = pkInfo.pk.getTable().getColumn(colName, _dict); if (col == null) throwUserException(_loc.get("pk-resolve", new Object[] { colName, pkInfo.pk.getTable() })); @@ -210,7 +210,7 @@ public class XMLSchemaParser indexInfo = (IndexInfo) itr.next(); for (Iterator cols = indexInfo.cols.iterator(); cols.hasNext();) { colName = (String) cols.next(); - col = indexInfo.index.getTable().getColumn(colName); + col = indexInfo.index.getTable().getColumn(colName, _dict); if (col == null) throwUserException(_loc.get("index-resolve", new Object[] { indexInfo.index, colName, @@ -250,13 +250,13 @@ public class XMLSchemaParser pks = fkInfo.pks.iterator(); for (cols = fkInfo.cols.iterator(); cols.hasNext();) { colName = (String) cols.next(); - col = fkInfo.fk.getTable().getColumn(colName); + col = fkInfo.fk.getTable().getColumn(colName, _dict); if (col == null) throwUserException(_loc.get("fk-nocol", fkInfo.fk, colName, fkInfo.fk.getTable())); pkColName = (String) pks.next(); - pkCol = toTable.getColumn(pkColName); + pkCol = toTable.getColumn(pkColName, _dict); if (pkCol == null) throwUserException(_loc.get("fk-nopkcol", new Object[] { fkInfo.fk, pkColName, toTable, @@ -269,7 +269,7 @@ public class XMLSchemaParser cols = fkInfo.constCols.iterator(); for (Iterator vals = fkInfo.consts.iterator(); vals.hasNext();) { colName = (String) cols.next(); - col = fkInfo.fk.getTable().getColumn(colName); + col = fkInfo.fk.getTable().getColumn(colName, _dict); if (col == null) throwUserException(_loc.get("fk-nocol", fkInfo.fk, colName, fkInfo.fk.getTable())); @@ -280,7 +280,7 @@ public class XMLSchemaParser pks = fkInfo.constColsPK.iterator(); for (Iterator vals = fkInfo.constsPK.iterator(); vals.hasNext();) { pkColName = (String) pks.next(); - pkCol = toTable.getColumn(pkColName); + pkCol = toTable.getColumn(pkColName, _dict); if (pkCol == null) throwUserException(_loc.get("fk-nopkcol", new Object[] { fkInfo.fk, pkColName, toTable, @@ -303,7 +303,7 @@ public class XMLSchemaParser unqInfo = (UniqueInfo) itr.next(); for (Iterator cols = unqInfo.cols.iterator(); cols.hasNext();) { colName = (String) cols.next(); - col = unqInfo.unq.getTable().getColumn(colName); + col = unqInfo.unq.getTable().getColumn(colName, _dict); if (col == null) throwUserException(_loc.get("unq-resolve", new Object[] { unqInfo.unq, colName, unqInfo.unq.getTable() })); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/delimited/identifiers/TestManualDelimId.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/delimited/identifiers/TestManualDelimId.java index 6465983e2..8d160cbdf 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/delimited/identifiers/TestManualDelimId.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/delimited/identifiers/TestManualDelimId.java @@ -18,26 +18,16 @@ */ package org.apache.openjpa.persistence.delimited.identifiers; -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.SQLException; import java.util.List; -import java.util.Set; import javax.persistence.Query; -import org.apache.openjpa.jdbc.conf.JDBCConfiguration; -import org.apache.openjpa.jdbc.schema.Column; -import org.apache.openjpa.jdbc.sql.DBDictionary; import org.apache.openjpa.persistence.OpenJPAEntityManager; import org.apache.openjpa.persistence.test.SQLListenerTestCase; public class TestManualDelimId extends SQLListenerTestCase { OpenJPAEntityManager em; - int id = 0; EntityF entityF; - JDBCConfiguration conf; - DBDictionary dict; @Override public void setUp() throws Exception { @@ -48,14 +38,9 @@ public class TestManualDelimId extends SQLListenerTestCase { em = emf.createEntityManager(); assertNotNull(em); - - conf = (JDBCConfiguration) emf.getConfiguration(); - dict = conf.getDBDictionaryInstance(); } - // TODO: remove parameter - public void createEntityF(int id) { -// entityF = new EntityF(id, "fName"); + public void createEntityF() { entityF = new EntityF("fName"); entityF.setNonDelimName("fNonDelimName"); entityF.setSecName("sec name"); @@ -69,40 +54,8 @@ public class TestManualDelimId extends SQLListenerTestCase { entityF.addDelimCollectionMap("yyy", "zzz"); } -// TODO: temp - test on multiple DBs -// public void testDBCapability() { -// Connection conn = (Connection)em.getConnection(); -// try { -// DatabaseMetaData meta = conn.getMetaData(); -// System.out.println("LC - " + -// meta.storesLowerCaseIdentifiers()); -// System.out.println("LCQ - " + -// meta.storesLowerCaseQuotedIdentifiers()); -// System.out.println("MC - " + -// meta.storesMixedCaseIdentifiers()); -// System.out.println("MCQ - " + -// meta.storesMixedCaseQuotedIdentifiers()); -// System.out.println("UC - " + -// meta.storesUpperCaseIdentifiers()); -// System.out.println("UCQ - " + -// meta.storesUpperCaseQuotedIdentifiers()); -// System.out.println(""); -// System.out.println("db product name - " + -// meta.getDatabaseProductName()); -// System.out.println("db product version - " + -// meta.getDatabaseProductVersion()); -// System.out.println("driver name - " + -// meta.getDriverName()); -// System.out.println("driver version - " + -// meta.getDriverVersion()); -// } catch (SQLException e) { -// e.printStackTrace(); -// } -// } - public void testCreateF() { - id++; - createEntityF(id); + createEntityF(); em.getTransaction().begin(); em.persist(entityF); @@ -111,7 +64,16 @@ public class TestManualDelimId extends SQLListenerTestCase { runQueries(); } - + + // Run a second time to re-create a situation that initially caused a problem when running this + // test consecutive times. + public void testCreateF2() { + createEntityF(); + + em.getTransaction().begin(); + em.persist(entityF); + em.getTransaction().commit(); + } private void runQueries() { em.clear();