OPENJPA-430 -- strip hungarian notation prefixes. Checking in patch for Ben Short, along with minor whitespace reformatting and a small tweak to reduce computation if branch logic in correctName() will not use the calculated name.

The initial patch included a test for fields that would end up having duplicate names after truncation. The code, however, does not do anything to resolve / avoid duplicates. To make it work, we would need to change MappingDefaultsImpl.correctName() to take a ValueMapping as an argument, and do a two-pass algorithm to check for other fields that would turn into duplicates. Even doing this will not be foolproof, as a duplicate column might come from a subclass or an embedded class. Since this option is not enabled by default anyways, and the duplicate error would become evident during testing / schema creation, I do not believe that it would be worthwhile to spend much time addressing this.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@592319 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Patrick Linskey 2007-11-06 07:36:55 +00:00
parent 3ca7d9d4d6
commit af4ea13a2c
6 changed files with 250 additions and 2 deletions

View File

@ -70,6 +70,15 @@ public class MappingDefaultsImpl
private String _discName = null; private String _discName = null;
private String _orderName = null; private String _orderName = null;
private String _nullIndName = null; private String _nullIndName = null;
private boolean _removeHungarianNotation = false;
public boolean isRemoveHungarianNotation() {
return _removeHungarianNotation;
}
public void setRemoveHungarianNotation(boolean removeHungarianNotation) {
this._removeHungarianNotation = removeHungarianNotation;
}
/** /**
* Default base class strategy alias. * Default base class strategy alias.
@ -535,8 +544,28 @@ public class MappingDefaultsImpl
* Correct the given column's name. * Correct the given column's name.
*/ */
protected void correctName(Table table, Column col) { protected void correctName(Table table, Column col) {
if (!_defMissing) if (!_defMissing || _removeHungarianNotation)
col.setName(dict.getValidColumnName(col.getName(), table)); {
String name = col.getName();
if (_removeHungarianNotation)
name = removeHungarianNotation(name);
col.setName(dict.getValidColumnName(name, table));
}
}
protected String removeHungarianNotation(String columnName) {
char[] name = columnName.toCharArray();
int newStart = 0;
for (int i = 0; i < name.length; i++) {
if (Character.isUpperCase(name[i]))
{
newStart = i;
break;
}
}
return columnName.substring(newStart);
} }
public void populateColumns(Version vers, Table table, Column[] cols) { public void populateColumns(Version vers, Table table, Column[] cols) {
@ -676,6 +705,10 @@ public class MappingDefaultsImpl
// based on defaults // based on defaults
if (name == null) if (name == null)
name = cols[0].getName(); name = cols[0].getName();
if (_removeHungarianNotation)
name = removeHungarianNotation(name);
return dict.getValidIndexName(name, table); return dict.getValidIndexName(name, table);
} }

View File

@ -181,6 +181,12 @@ public class PersistenceMappingDefaults
if (target instanceof Column) { if (target instanceof Column) {
if (elem) if (elem)
name = vm.getFieldMapping().getName(); name = vm.getFieldMapping().getName();
if (isRemoveHungarianNotation())
name = removeHungarianNotation(name);
name = dict.getValidColumnName(name, local);
col.setName(name + "_" + ((Column) target).getName()); col.setName(name + "_" + ((Column) target).getName());
} }
} }

View File

@ -0,0 +1,34 @@
/*
* 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.persistence.hugariannotation;
import javax.persistence.Entity;
/**
* Created by IntelliJ IDEA.
* User: Ben
* Date: 02-Nov-2007
* Time: 22:44:47
*/
@Entity
public class HungarianNotationFieldDuplicates {
private String strFooBar;
private Integer intFooBar;
}

View File

@ -0,0 +1,53 @@
/*
* 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.persistence.hugariannotation;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
/**
* Created by IntelliJ IDEA.
* User: Ben
* Date: 30-Oct-2007
* Time: 22:11:00
*/
@Entity
public class HungarianNotationFields {
@Id
private Long mFooBar7;
private String mFooBar1;
private String strFooBar2;
private Integer intFooBar3;
private Long lgFooBar4;
private int m_intFooBar5;
@ManyToOne(targetEntity = OtherClass.class)
private OtherClass m_clzFooBar6;
@Column(name="M_INTFOOBAR7_CUSTOM_NAME")
private int m_intFooBar7;
}

View File

@ -0,0 +1,35 @@
/*
* 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.persistence.hugariannotation;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* Created by IntelliJ IDEA.
* User: Ben
* Date: 31-Oct-2007
* Time: 21:04:54
*/
@Entity
public class OtherClass {
@Id
private Integer m_intBarFoo1;
}

View File

@ -0,0 +1,87 @@
/*
* 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.persistence.hugariannotation;
import org.apache.openjpa.jdbc.meta.ClassMapping;
import org.apache.openjpa.jdbc.meta.FieldMapping;
import org.apache.openjpa.persistence.JPAFacadeHelper;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Created by IntelliJ IDEA.
* User: Ben
* Date: 02-Nov-2007
* Time: 21:36:36
*/
public class TestHungarianNotationRemoval extends SingleEMFTestCase {
public void setUp() {
setUp(HungarianNotationFieldDuplicates.class,
HungarianNotationFields.class, OtherClass.class, CLEAR_TABLES,
"openjpa.jdbc.MappingDefaults", "removeHungarianNotation=true");
}
public void testSimpleColumnNameTruncation() {
ClassMapping cm = (ClassMapping) JPAFacadeHelper
.getMetaData(emf, HungarianNotationFields.class);
FieldMapping[] fieldMappings = cm.getFieldMappings();
for (int i = 0; i < fieldMappings.length; i++) {
final String name = fieldMappings[i].getColumns()[0].getName();
// this one doesn't follow the rules
if (fieldMappings[i].getName().equals("m_intFooBar7"))
continue;
assertTrue(
"Failed to removed Hungarian Notation, resulting column name : "
+ name, name.toUpperCase().startsWith("FOOBAR"));
}
}
public void testCustomNameNotAltered() {
ClassMapping cm = (ClassMapping) JPAFacadeHelper
.getMetaData(emf, HungarianNotationFields.class);
assertEquals("M_INTFOOBAR7_CUSTOM_NAME",
cm.getFieldMapping("m_intFooBar7").getColumns()[0].getName());
}
/*
pcl: This test currently fails. To make it work, we would need to
change MappingDefaultsImpl.correctName() to take a ValueMapping as
an argument, and do a two-pass algorithm to check for other fields
that would turn into duplicates. Even doing this will not be
foolproof, as a duplicate column might come from a subclass or an
embedded class.
public void testDuplicateColumnNameTruncation() {
ClassMapping cm = (ClassMapping) JPAFacadeHelper
.getMetaData(emf, HungarianNotationFieldDuplicates.class);
for (FieldMapping fm : cm.getFieldMappings()) {
String name = fm.getColumns()[0].getName();
assertTrue(name.toUpperCase().endsWith("FOOBAR"));
assertFalse(name.toUpperCase().startsWith("FOOBAR"));
}
}
*/
}