Convert Java field and method names to SQL friendly format
Java convention is camelCaseNames, as the names are case sensitive.
SQL convention is usually camel_case_names, as names are not
generally case sensitive in a SQL database, and are often displayed
in full uppercase.
Signed-off-by: Shawn O. Pearce <sop@google.com>
diff --git a/src/com/google/gwtorm/schema/ColumnModel.java b/src/com/google/gwtorm/schema/ColumnModel.java
index 8122d52..ce2dc7a 100644
--- a/src/com/google/gwtorm/schema/ColumnModel.java
+++ b/src/com/google/gwtorm/schema/ColumnModel.java
@@ -40,7 +40,7 @@
+ Column.class.getName() + " annotation");
}
column = col;
- origName = Util.any(column.name(), fieldName);
+ origName = Util.any(column.name(), Util.makeSqlFriendly(fieldName));
columnName = origName;
}
diff --git a/src/com/google/gwtorm/schema/RelationModel.java b/src/com/google/gwtorm/schema/RelationModel.java
index 6757dc6..e4c4b73 100644
--- a/src/com/google/gwtorm/schema/RelationModel.java
+++ b/src/com/google/gwtorm/schema/RelationModel.java
@@ -48,7 +48,7 @@
}
relation = rel;
methodName = method;
- relationName = Util.any(relation.name(), methodName);
+ relationName = Util.any(relation.name(), Util.makeSqlFriendly(methodName));
}
protected void initColumns(final Collection<? extends ColumnModel> allFields)
diff --git a/src/com/google/gwtorm/schema/SequenceModel.java b/src/com/google/gwtorm/schema/SequenceModel.java
index aa9e389..80279f4 100644
--- a/src/com/google/gwtorm/schema/SequenceModel.java
+++ b/src/com/google/gwtorm/schema/SequenceModel.java
@@ -40,11 +40,11 @@
final String n;
if (methodName.startsWith("next")) {
- n = methodName.substring(4).toLowerCase();
+ n = methodName.substring(4);
} else {
n = methodName;
}
- name = Util.any(sequence.name(), n);
+ name = Util.any(sequence.name(), Util.makeSqlFriendly(n));
returnType = type;
}
diff --git a/src/com/google/gwtorm/schema/Util.java b/src/com/google/gwtorm/schema/Util.java
index 2a90f28..0a028cb 100644
--- a/src/com/google/gwtorm/schema/Util.java
+++ b/src/com/google/gwtorm/schema/Util.java
@@ -22,6 +22,28 @@
return "GwtOrm$$" + nameCounter++;
}
+ public static String makeSqlFriendly(final String name) {
+ final StringBuilder r = new StringBuilder(name.length() + 8);
+ boolean lastWasCap = true;
+ for (int i = 0; i < name.length(); i++) {
+ final char c = name.charAt(i);
+ if (Character.isUpperCase(c)) {
+ if (!lastWasCap) {
+ r.append('_');
+ lastWasCap = true;
+ }
+ r.append(Character.toLowerCase(c));
+ } else if (c == '_') {
+ lastWasCap = true;
+ r.append(c);
+ } else {
+ lastWasCap = false;
+ r.append(c);
+ }
+ }
+ return r.toString();
+ }
+
public static String any(final String a, final String b) {
if (a != null && a.length() > 0) {
return a;
diff --git a/test/com/google/gwtorm/schema/UtilTestCase.java b/test/com/google/gwtorm/schema/UtilTestCase.java
new file mode 100644
index 0000000..8295e99
--- /dev/null
+++ b/test/com/google/gwtorm/schema/UtilTestCase.java
@@ -0,0 +1,28 @@
+// Copyright 2008 Google Inc.
+//
+// Licensed 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 com.google.gwtorm.schema;
+
+import junit.framework.TestCase;
+
+public class UtilTestCase extends TestCase {
+ public void testSqlFriendlyNames() {
+ assertEquals("a", Util.makeSqlFriendly("a"));
+ assertEquals("url", Util.makeSqlFriendly("url"));
+ assertEquals("next_url", Util.makeSqlFriendly("nextUrl"));
+ assertEquals("next_url", Util.makeSqlFriendly("nextURL"));
+ assertEquals("urlnext", Util.makeSqlFriendly("URLnext"));
+ assertEquals("camel_case_name", Util.makeSqlFriendly("camelCaseName"));
+ }
+}