Skip to content

Commit 9c299ae

Browse files
author
Robin Komiwes
committed
added a new naming convention on plural names when determining primary keys name
1 parent 547b617 commit 9c299ae

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

src/main/java/com/spreadthesource/tapestry/dbmigration/migrations/impl/ConstraintImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
public class ConstraintImpl extends AbstractMigrationContext implements Constraint
1414
{
15-
private String name;
15+
private String tableName;
1616

1717
private List<String> foreignKeys = new ArrayList<String>();
1818

@@ -48,7 +48,7 @@ public void setForeignKey(String name, String foreignTable, String[] fromToColum
4848

4949
public void setTableName(String tableName)
5050
{
51-
this.name = tableName;
51+
this.tableName = tableName;
5252
}
5353

5454
public void setUnique(String name, String... columns)
@@ -59,12 +59,12 @@ public void setUnique(String name, String... columns)
5959

6060
public List<String> getQueries()
6161
{
62-
if (name == null) { throw new IllegalArgumentException(
62+
if (tableName == null) { throw new IllegalArgumentException(
6363
"Table name cannot be null to generate constraint query string."); }
6464

6565
List<String> result = new ArrayList<String>();
6666

67-
org.hibernate.mapping.Table hTable = new org.hibernate.mapping.Table(name);
67+
org.hibernate.mapping.Table hTable = new org.hibernate.mapping.Table(tableName);
6868

6969
// Add foreign keys alter queries
7070
for (String query : foreignKeys)

src/main/java/com/spreadthesource/tapestry/dbmigration/utils/MigrationUtils.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.spreadthesource.tapestry.dbmigration.utils;
22

3+
import java.util.ArrayList;
34
import java.util.Arrays;
45
import java.util.List;
56

@@ -24,10 +25,42 @@ public final static boolean checkIfImplements(Class<?> clazz, Class<?> inter)
2425

2526
public final static String buildPkColumnId(String tableName)
2627
{
27-
return (tableName.charAt(0) + tableName.substring(1).replaceAll("([A-Z])", "_$1"))
28-
.toLowerCase()
28+
return singularizeWords((tableName.charAt(0) + tableName.substring(1).replaceAll("([A-Z])", "_$1"))
29+
.toLowerCase(), "_")
2930
+ "_id";
3031
}
32+
33+
public final static String singularizeWords(String words, String separator) {
34+
StringBuffer singularWords = new StringBuffer();
35+
36+
String[] wordsArray = words.split(separator);
37+
38+
int length = wordsArray.length;
39+
for (int i = 0; i < length;i++)
40+
{
41+
String word = wordsArray[i];
42+
43+
singularWords.append(singularize(word));
44+
45+
if (i + 1 < length)
46+
singularWords.append("_");
47+
}
48+
49+
return singularWords.toString();
50+
}
51+
52+
/**
53+
* Convert a plural word to a singular word.
54+
* If word ends with "ies", like activities, then "ies" is replaced by "y".
55+
* Else final "s" is removed.
56+
*/
57+
public final static String singularize(String pluralWord) {
58+
if (!pluralWord.endsWith("s"))
59+
return pluralWord;
60+
61+
int length = pluralWord.length();
62+
return pluralWord.endsWith("ies") ? pluralWord.substring(0, length - 3) + "y" : pluralWord.substring(0, length - 1);
63+
}
3164

3265
/**
3366
* Build a hibernate column given our wrapper representation.

0 commit comments

Comments
 (0)