OpenJPA-1051: Fixed MappingDefaultsImpl to avoid column name duplications when long column names are supplied for a database that accepts only shorter names. Changes submitted for Ravi Palacherla.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@774580 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
David Ezzio 2009-05-13 22:54:32 +00:00
parent f81e627b27
commit 862edc8fe1
4 changed files with 113 additions and 6 deletions

View File

@ -539,7 +539,9 @@ public class MappingDefaultsImpl
else if (_dsIdName != null)
cols[i].setName(_dsIdName + i);
correctName(table, cols[i]);
table.addSubColumn(cols[i].getName());
}
table.resetSubColumns();
}
/**
@ -582,7 +584,9 @@ public class MappingDefaultsImpl
} else if (_versName != null)
cols[i].setName(_versName + i);
correctName(table, cols[i]);
table.addSubColumn(cols[i].getName());
}
table.resetSubColumns();
}
public void populateColumns(Discriminator disc, Table table,
@ -593,7 +597,9 @@ public class MappingDefaultsImpl
else if (_discName != null)
cols[i].setName(_discName + i);
correctName(table, cols[i]);
table.addSubColumn(cols[i].getName());
}
table.resetSubColumns();
}
public void populateJoinColumn(ClassMapping cm, Table local, Table foreign,
@ -618,8 +624,11 @@ public class MappingDefaultsImpl
public void populateColumns(ValueMapping vm, String name, Table table,
Column[] cols) {
for (int i = 0; i < cols.length; i++)
for (int i = 0; i < cols.length; i++) {
correctName(table, cols[i]);
table.addSubColumn(cols[i].getName());
}
table.resetSubColumns();
}
public boolean populateOrderColumns(FieldMapping fm, Table table,
@ -630,7 +639,9 @@ public class MappingDefaultsImpl
else if (_orderName != null)
cols[i].setName(_orderName + i);
correctName(table, cols[i]);
table.addSubColumn(cols[i].getName());
}
table.resetSubColumns();
return _orderLists && (JavaTypes.ARRAY == fm.getTypeCode()
|| List.class.isAssignableFrom(fm.getType()));
}
@ -643,7 +654,9 @@ public class MappingDefaultsImpl
else if (_nullIndName != null)
cols[i].setName(_nullIndName + i);
correctName(table, cols[i]);
table.addSubColumn(cols[i].getName());
}
table.resetSubColumns();
return _addNullInd;
}

View File

@ -39,13 +39,17 @@ public class NameSet
private Set _names = null;
// an additional names Set for checking name duplication
private Set _subNames = null;
/**
* Return true if the given name is in use already.
*/
public boolean isNameTaken(String name) {
if (name == null)
return true;
return _names != null && _names.contains(name.toUpperCase());
return (_names != null && _names.contains(name.toUpperCase())) ||
(_subNames != null && _subNames.contains(name.toUpperCase()));
}
/**
@ -77,4 +81,20 @@ public class NameSet
if (name != null && _names != null)
_names.remove(name.toUpperCase());
}
/**
* Attempt to add the given name to the set.
*
* @param name the name to add
*/
protected void addSubName(String name) {
if (_subNames == null) {
_subNames = new HashSet();
}
_subNames.add(name.toUpperCase());
}
protected void resetSubNames() {
_subNames = null;
}
}

View File

@ -756,4 +756,15 @@ public class Table
public void setColNumber(int colNum) {
_colNum = colNum;
}
/**
* Add a column to the subNames set to avoid naming conflict.
*/
public void addSubColumn(String name) {
addSubName(name);
}
public void resetSubColumns() {
resetSubNames();
}
}

View File

@ -0,0 +1,63 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.jdbc.meta;
import org.apache.openjpa.jdbc.schema.Table;
import org.apache.openjpa.jdbc.schema.Column;
import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
import org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl;
import junit.framework.TestCase;
public class TestMappingDefaultsImpl extends TestCase {
public void setUp() {
}
/**
* For databases that accept only short column names, test avoidance of
* duplicate column names when populating the table with long column names.
*
* @author Hiroki Tateno
*/
public void testPopulateWithLongColumnNames() {
MappingDefaultsImpl mapping = new MappingDefaultsImpl();
JDBCConfiguration conf = new JDBCConfigurationImpl(false, false);
conf.setDBDictionary("oracle");
mapping.setConfiguration(conf);
Table table = new Table("testtable", null);
Column[] cols = new Column[3];
cols[0] = new
Column("longnamelongnamelongnamelongnamelongnamelongname1", null);
cols[1] = new
Column("longnamelongnamelongnamelongnamelongnamelongname2", null);
cols[2] = new
Column("longnamelongnamelongnamelongnamelongnamelongname3", null);
MappingRepository mr = new MappingRepository();
mr.setConfiguration(conf);
Version version = new Version(new ClassMapping(String.class,mr));
mapping.populateColumns(version, table, cols);
assertFalse("column names are conflicted : " + cols[0].getName(),
cols[0].getName().equals(cols[1].getName()));
assertFalse("column names are conflicted : " + cols[0].getName(),
cols[0].getName().equals(cols[2].getName()));
assertFalse("column names are conflicted : " + cols[1].getName(),
cols[1].getName().equals(cols[2].getName()));
}
}