Author: pcl
Date: Mon Nov 5 23:36:55 2007
New Revision: 592319
URL: http://svn.apache.org/viewvc?rev=592319&view=rev
Log:
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.
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/hugariannotation/
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/hugariannotation/HungarianNotationFieldDuplicates.java
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/hugariannotation/HungarianNotationFields.java
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/hugariannotation/OtherClass.java
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/hugariannotation/TestHungarianNotationRemoval.java
Modified:
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingDefaultsImpl.java
openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/PersistenceMappingDefaults.java
Modified:
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingDefaultsImpl.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingDefaultsImpl.java?rev=592319&r1=592318&r2=592319&view=diff
==============================================================================
---
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingDefaultsImpl.java
(original)
+++
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingDefaultsImpl.java
Mon Nov 5 23:36:55 2007
@@ -70,6 +70,15 @@
private String _discName = null;
private String _orderName = 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.
@@ -535,8 +544,28 @@
* Correct the given column's name.
*/
protected void correctName(Table table, Column col) {
- if (!_defMissing)
- col.setName(dict.getValidColumnName(col.getName(), table));
+ if (!_defMissing || _removeHungarianNotation)
+ {
+ 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) {
@@ -676,6 +705,10 @@
// based on defaults
if (name == null)
name = cols[0].getName();
+
+ if (_removeHungarianNotation)
+ name = removeHungarianNotation(name);
+
return dict.getValidIndexName(name, table);
}
Modified:
openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/PersistenceMappingDefaults.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/PersistenceMappingDefaults.java?rev=592319&r1=592318&r2=592319&view=diff
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/PersistenceMappingDefaults.java
(original)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/PersistenceMappingDefaults.java
Mon Nov 5 23:36:55 2007
@@ -181,6 +181,12 @@
if (target instanceof Column) {
if (elem)
name = vm.getFieldMapping().getName();
+
+ if (isRemoveHungarianNotation())
+ name = removeHungarianNotation(name);
+
+ name = dict.getValidColumnName(name, local);
+
col.setName(name + "_" + ((Column) target).getName());
}
}
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/hugariannotation/HungarianNotationFieldDuplicates.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/hugariannotation/HungarianNotationFieldDuplicates.java?rev=592319&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/hugariannotation/HungarianNotationFieldDuplicates.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/hugariannotation/HungarianNotationFieldDuplicates.java
Mon Nov 5 23:36:55 2007
@@ -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
+ */
[EMAIL PROTECTED]
+public class HungarianNotationFieldDuplicates {
+
+ private String strFooBar;
+ private Integer intFooBar;
+}
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/hugariannotation/HungarianNotationFields.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/hugariannotation/HungarianNotationFields.java?rev=592319&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/hugariannotation/HungarianNotationFields.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/hugariannotation/HungarianNotationFields.java
Mon Nov 5 23:36:55 2007
@@ -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
+ */
[EMAIL PROTECTED]
+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;
+}
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/hugariannotation/OtherClass.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/hugariannotation/OtherClass.java?rev=592319&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/hugariannotation/OtherClass.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/hugariannotation/OtherClass.java
Mon Nov 5 23:36:55 2007
@@ -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
+ */
[EMAIL PROTECTED]
+public class OtherClass {
+
+ @Id
+ private Integer m_intBarFoo1;
+}
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/hugariannotation/TestHungarianNotationRemoval.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/hugariannotation/TestHungarianNotationRemoval.java?rev=592319&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/hugariannotation/TestHungarianNotationRemoval.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/hugariannotation/TestHungarianNotationRemoval.java
Mon Nov 5 23:36:55 2007
@@ -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"));
+ }
+ }
+ */
+}
+