OPENJPA-1189:

Try delimited table names if we can't find a match for non-delimited names
Submitted by : Dianne Richards

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@803720 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Dick 2009-08-12 21:45:51 +00:00
parent a445c1c4bd
commit 6c2e3e2d03
8 changed files with 94 additions and 82 deletions

View File

@ -404,11 +404,11 @@ public class TableJDBCSeq
} }
Unique u = table.addUnique(uniqueName); Unique u = table.addUnique(uniqueName);
for (String columnName : _uniqueColumnNames) { for (String columnName : _uniqueColumnNames) {
if (!table.containsColumn(columnName)) if (!table.containsColumn(columnName, _conf.getDBDictionaryInstance()))
throw new UserException(_loc.get("unique-missing-column", throw new UserException(_loc.get("unique-missing-column",
columnName, table.getName(), columnName, table.getName(),
table.getColumnNames())); table.getColumnNames()));
Column col = table.getColumn(columnName); Column col = table.getColumn(columnName, _conf.getDBDictionaryInstance());
u.addColumn(col); u.addColumn(col);
} }
} }

View File

@ -119,10 +119,14 @@ public class DynamicSchemaFactory
} }
public Column getColumn(String name) { public Column getColumn(String name) {
return getColumn(name, null);
}
public Column getColumn(String name, DBDictionary dict) {
if (name == null) if (name == null)
return null; return null;
Column col = super.getColumn(name); Column col = super.getColumn(name, dict);
if (col != null) if (col != null)
return col; return col;

View File

@ -769,9 +769,9 @@ public class ForeignKey
fkTemp.setDeleteAction(fks[i].getDeleteAction()); fkTemp.setDeleteAction(fks[i].getDeleteAction());
} }
if( ! fkTemp.containsColumn( if( ! fkTemp.containsColumn(
localtable.getColumn(fks[i].getColumnName()))) localtable.getColumn(fks[i].getColumnName(), dbdict)))
fkTemp.join(localtable.getColumn(fks[i].getColumnName()), fkTemp.join(localtable.getColumn(fks[i].getColumnName(), dbdict),
pkTable.getColumn(fks[i].getPrimaryKeyColumnName())); pkTable.getColumn(fks[i].getPrimaryKeyColumnName(), dbdict));
if( equalsForeignKey(fkTemp)) if( equalsForeignKey(fkTemp))
{ {
if(addFK) if(addFK)

View File

@ -527,16 +527,11 @@ public class SchemaGenerator {
if (_log.isTraceEnabled()) if (_log.isTraceEnabled())
_log.trace(_loc.get("gen-column", cols[i].getName(), table)); _log.trace(_loc.get("gen-column", cols[i].getName(), table));
if (table.getColumn(cols[i].getName()) == null) { if (table.getColumn(cols[i].getName(), _dict) == 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]); table.importColumn(cols[i]);
} }
} }
} }
}
/** /**
* Return whether the given table is allowed by the user's schema list. * Return whether the given table is allowed by the user's schema list.
@ -630,7 +625,7 @@ public class SchemaGenerator {
pk = table.getPrimaryKey(); pk = table.getPrimaryKey();
if (pk == null) if (pk == null)
pk = table.addPrimaryKey(name); pk = table.addPrimaryKey(name);
pk.addColumn(table.getColumn(colName)); pk.addColumn(table.getColumn(colName, _dict));
} }
} }
@ -692,7 +687,7 @@ public class SchemaGenerator {
continue; continue;
colName = idxs[i].getColumnName(); colName = idxs[i].getColumnName();
if (table.getColumn(colName) == null) if (table.getColumn(colName, _dict) == null)
continue; continue;
if (_log.isTraceEnabled()) if (_log.isTraceEnabled())
@ -704,7 +699,7 @@ public class SchemaGenerator {
idx = table.addIndex(name); idx = table.addIndex(name);
idx.setUnique(idxs[i].isUnique()); 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)) { if (invalids == null || !invalids.contains(fk)) {
try { try {
Column fkCol = table.getColumn(fkColName); Column fkCol = table.getColumn(fkColName, _dict);
if (fkCol == null) { if (fkCol == null) {
throw new IllegalArgumentException(_loc.get( throw new IllegalArgumentException(_loc.get(
"no-column", fkColName, table.getName()) "no-column", fkColName, table.getName())
.getMessage()); .getMessage());
} }
fk.join(fkCol, pkTable.getColumn(pkColName)); fk.join(fkCol, pkTable.getColumn(pkColName, _dict));
} catch (IllegalArgumentException iae) { } catch (IllegalArgumentException iae) {
if (_log.isWarnEnabled()) if (_log.isWarnEnabled())
_log.warn(_loc.get("bad-join", iae.toString())); _log.warn(_loc.get("bad-join", iae.toString()));

View File

@ -486,7 +486,7 @@ public class SchemaTool {
colName = colName.substring(1, colName.length()-1); colName = colName.substring(1, colName.length()-1);
delimCol = true; delimCol = true;
} }
col = dbTable.getColumn(colName); col = dbTable.getColumn(colName, _dict);
if (col == null) { if (col == null) {
if (addColumn(cols[k])) if (addColumn(cols[k]))
dbTable.importColumn(cols[k]); dbTable.importColumn(cols[k]);
@ -724,7 +724,7 @@ public class SchemaTool {
reposTable = repos.findTable(tabs[j]); reposTable = repos.findTable(tabs[j]);
if (reposTable != null) { if (reposTable != null) {
for (int k = 0; k < cols.length; k++) { 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 (col == null || !cols[k].equalsColumn(col)) {
if (tabs[j].getColumns().length == 1) if (tabs[j].getColumns().length == 1)
drops.add(tabs[j]); drops.add(tabs[j]);
@ -797,7 +797,7 @@ public class SchemaTool {
dbCols = dbTable.getColumns(); dbCols = dbTable.getColumns();
for (int k = 0; k < dbCols.length; k++) 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; continue tables;
drops.add(tabs[j]); drops.add(tabs[j]);
@ -869,7 +869,7 @@ public class SchemaTool {
for (int k = 0; k < cols.length; k++) { for (int k = 0; k < cols.length; k++) {
col = null; col = null;
if (dbTable != null) if (dbTable != null)
col = dbTable.getColumn(cols[k].getName()); col = dbTable.getColumn(cols[k].getName(), _dict);
if (dbTable == null || col == null) if (dbTable == null || col == null)
continue; continue;

View File

@ -27,6 +27,7 @@ import java.util.TreeMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.openjpa.jdbc.sql.DBDictionary;
import org.apache.openjpa.lib.meta.SourceTracker; 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. * Return the column with the given name, or null if none.
*/ */
public Column getColumn(String name) { 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) if (name == null || _colMap == null)
return 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. * for dynamic table implementation.
*/ */
public boolean containsColumn(String name) { public boolean containsColumn(String name) {
return name != null && _colMap != null return containsColumn(name, null);
&& _colMap.containsKey(name.toUpperCase()); }
/**
* 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;
} }
/** /**

View File

@ -189,7 +189,7 @@ public class XMLSchemaParser
pkInfo = (PrimaryKeyInfo) itr.next(); pkInfo = (PrimaryKeyInfo) itr.next();
for (Iterator cols = pkInfo.cols.iterator(); cols.hasNext();) { for (Iterator cols = pkInfo.cols.iterator(); cols.hasNext();) {
colName = (String) cols.next(); colName = (String) cols.next();
col = pkInfo.pk.getTable().getColumn(colName); col = pkInfo.pk.getTable().getColumn(colName, _dict);
if (col == null) if (col == null)
throwUserException(_loc.get("pk-resolve", new Object[] throwUserException(_loc.get("pk-resolve", new Object[]
{ colName, pkInfo.pk.getTable() })); { colName, pkInfo.pk.getTable() }));
@ -210,7 +210,7 @@ public class XMLSchemaParser
indexInfo = (IndexInfo) itr.next(); indexInfo = (IndexInfo) itr.next();
for (Iterator cols = indexInfo.cols.iterator(); cols.hasNext();) { for (Iterator cols = indexInfo.cols.iterator(); cols.hasNext();) {
colName = (String) cols.next(); colName = (String) cols.next();
col = indexInfo.index.getTable().getColumn(colName); col = indexInfo.index.getTable().getColumn(colName, _dict);
if (col == null) if (col == null)
throwUserException(_loc.get("index-resolve", new Object[] throwUserException(_loc.get("index-resolve", new Object[]
{ indexInfo.index, colName, { indexInfo.index, colName,
@ -250,13 +250,13 @@ public class XMLSchemaParser
pks = fkInfo.pks.iterator(); pks = fkInfo.pks.iterator();
for (cols = fkInfo.cols.iterator(); cols.hasNext();) { for (cols = fkInfo.cols.iterator(); cols.hasNext();) {
colName = (String) cols.next(); colName = (String) cols.next();
col = fkInfo.fk.getTable().getColumn(colName); col = fkInfo.fk.getTable().getColumn(colName, _dict);
if (col == null) if (col == null)
throwUserException(_loc.get("fk-nocol", throwUserException(_loc.get("fk-nocol",
fkInfo.fk, colName, fkInfo.fk.getTable())); fkInfo.fk, colName, fkInfo.fk.getTable()));
pkColName = (String) pks.next(); pkColName = (String) pks.next();
pkCol = toTable.getColumn(pkColName); pkCol = toTable.getColumn(pkColName, _dict);
if (pkCol == null) if (pkCol == null)
throwUserException(_loc.get("fk-nopkcol", new Object[] throwUserException(_loc.get("fk-nopkcol", new Object[]
{ fkInfo.fk, pkColName, toTable, { fkInfo.fk, pkColName, toTable,
@ -269,7 +269,7 @@ public class XMLSchemaParser
cols = fkInfo.constCols.iterator(); cols = fkInfo.constCols.iterator();
for (Iterator vals = fkInfo.consts.iterator(); vals.hasNext();) { for (Iterator vals = fkInfo.consts.iterator(); vals.hasNext();) {
colName = (String) cols.next(); colName = (String) cols.next();
col = fkInfo.fk.getTable().getColumn(colName); col = fkInfo.fk.getTable().getColumn(colName, _dict);
if (col == null) if (col == null)
throwUserException(_loc.get("fk-nocol", throwUserException(_loc.get("fk-nocol",
fkInfo.fk, colName, fkInfo.fk.getTable())); fkInfo.fk, colName, fkInfo.fk.getTable()));
@ -280,7 +280,7 @@ public class XMLSchemaParser
pks = fkInfo.constColsPK.iterator(); pks = fkInfo.constColsPK.iterator();
for (Iterator vals = fkInfo.constsPK.iterator(); vals.hasNext();) { for (Iterator vals = fkInfo.constsPK.iterator(); vals.hasNext();) {
pkColName = (String) pks.next(); pkColName = (String) pks.next();
pkCol = toTable.getColumn(pkColName); pkCol = toTable.getColumn(pkColName, _dict);
if (pkCol == null) if (pkCol == null)
throwUserException(_loc.get("fk-nopkcol", new Object[] throwUserException(_loc.get("fk-nopkcol", new Object[]
{ fkInfo.fk, pkColName, toTable, { fkInfo.fk, pkColName, toTable,
@ -303,7 +303,7 @@ public class XMLSchemaParser
unqInfo = (UniqueInfo) itr.next(); unqInfo = (UniqueInfo) itr.next();
for (Iterator cols = unqInfo.cols.iterator(); cols.hasNext();) { for (Iterator cols = unqInfo.cols.iterator(); cols.hasNext();) {
colName = (String) cols.next(); colName = (String) cols.next();
col = unqInfo.unq.getTable().getColumn(colName); col = unqInfo.unq.getTable().getColumn(colName, _dict);
if (col == null) if (col == null)
throwUserException(_loc.get("unq-resolve", new Object[] throwUserException(_loc.get("unq-resolve", new Object[]
{ unqInfo.unq, colName, unqInfo.unq.getTable() })); { unqInfo.unq, colName, unqInfo.unq.getTable() }));

View File

@ -18,26 +18,16 @@
*/ */
package org.apache.openjpa.persistence.delimited.identifiers; 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.List;
import java.util.Set;
import javax.persistence.Query; 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.OpenJPAEntityManager;
import org.apache.openjpa.persistence.test.SQLListenerTestCase; import org.apache.openjpa.persistence.test.SQLListenerTestCase;
public class TestManualDelimId extends SQLListenerTestCase { public class TestManualDelimId extends SQLListenerTestCase {
OpenJPAEntityManager em; OpenJPAEntityManager em;
int id = 0;
EntityF entityF; EntityF entityF;
JDBCConfiguration conf;
DBDictionary dict;
@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
@ -48,14 +38,9 @@ public class TestManualDelimId extends SQLListenerTestCase {
em = emf.createEntityManager(); em = emf.createEntityManager();
assertNotNull(em); assertNotNull(em);
conf = (JDBCConfiguration) emf.getConfiguration();
dict = conf.getDBDictionaryInstance();
} }
// TODO: remove parameter public void createEntityF() {
public void createEntityF(int id) {
// entityF = new EntityF(id, "fName");
entityF = new EntityF("fName"); entityF = new EntityF("fName");
entityF.setNonDelimName("fNonDelimName"); entityF.setNonDelimName("fNonDelimName");
entityF.setSecName("sec name"); entityF.setSecName("sec name");
@ -69,40 +54,8 @@ public class TestManualDelimId extends SQLListenerTestCase {
entityF.addDelimCollectionMap("yyy", "zzz"); 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() { public void testCreateF() {
id++; createEntityF();
createEntityF(id);
em.getTransaction().begin(); em.getTransaction().begin();
em.persist(entityF); em.persist(entityF);
@ -112,6 +65,15 @@ public class TestManualDelimId extends SQLListenerTestCase {
} }
// 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() { private void runQueries() {
em.clear(); em.clear();