001: // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
002:
003: package jodd.db.orm;
004:
005: /**
006: * Various db name utilities.
007: */
008: public class DbNameUtil {
009:
010: // ---------------------------------------------------------------- tables
011:
012: public static String convertClassNameToTableName(Class clazz) {
013: return convertClassNameToTableName(clazz, null);
014: }
015:
016: public static String convertClassNameToTableName(Class clazz,
017: String tablePrefix) {
018: return convertClassNameToTableName(clazz.getSimpleName(),
019: tablePrefix);
020: }
021:
022: public static String convertClassNameToTableName(String className) {
023: return convertClassNameToTableName(className, null);
024: }
025:
026: /**
027: * Converts class name to table name. All lower characters are converted to
028: * upper ones. All upper characters are prefixed with '_'. Therefore, class name
029: * <code>FooBooZoo</code> will be converted to <code>FOO_BOO_ZOO</code>.
030: */
031: public static String convertClassNameToTableName(String className,
032: String tablePrefix) {
033: StringBuilder tableName = new StringBuilder(
034: className.length() + 10);
035: if (tablePrefix != null) {
036: tableName.append(tablePrefix);
037: }
038: for (int i = 0; i < className.length(); i++) {
039: char c = className.charAt(i);
040: if ((i != 0) && (Character.isUpperCase(c) == true)) {
041: tableName.append('_').append(c);
042: } else {
043: tableName.append(Character.toUpperCase(c));
044: }
045: }
046: return tableName.toString();
047: }
048:
049: public static String convertTableNameToClassName(String tableName) {
050: return convertTableNameToClassName(tableName, null);
051: }
052:
053: /**
054: * Converts table name to class name. All characters after '_' are converted to upper ones.
055: * Other characters are converted to lower. Therefore, table name <code>FOO_BOO_ZOO</code>
056: * will be converted to <code>FooBooZoo</code>.
057: */
058: public static String convertTableNameToClassName(String tableName,
059: String tablePrefix) {
060: StringBuilder className = new StringBuilder(tableName.length());
061: int i = 0;
062: if (tablePrefix != null) {
063: if (tableName.startsWith(tablePrefix) == true) {
064: i = tablePrefix.length();
065: }
066: }
067: boolean toUpper = true;
068: for (; i < tableName.length(); i++) {
069: char c = tableName.charAt(i);
070: if (c == '_') {
071: toUpper = true;
072: continue;
073: }
074: if (toUpper == true) {
075: className.append(Character.toUpperCase(c));
076: toUpper = false;
077: } else {
078: className.append(Character.toLowerCase(c));
079: }
080: }
081: return className.toString();
082: }
083:
084: // ---------------------------------------------------------------- columns
085:
086: /**
087: * Converts property name to column name.
088: */
089: public static String convertPropertyNameToColumnName(
090: String propertyName) {
091: StringBuilder tableName = new StringBuilder(propertyName
092: .length() + 10);
093: for (int i = 0; i < propertyName.length(); i++) {
094: char c = propertyName.charAt(i);
095: if ((i != 0) && (Character.isUpperCase(c) == true)) {
096: tableName.append('_').append(c);
097: } else {
098: tableName.append(Character.toUpperCase(c));
099: }
100: }
101: return tableName.toString();
102: }
103:
104: /**
105: * Converts column name to property name.
106: */
107: public static String convertColumnNameToPropertyName(
108: String columnName) {
109: StringBuilder propertyName = new StringBuilder(columnName
110: .length());
111: int i = 0;
112: boolean toUpper = false;
113: for (; i < columnName.length(); i++) {
114: char c = columnName.charAt(i);
115: if (c == '_') {
116: toUpper = true;
117: continue;
118: }
119: if (toUpper == true) {
120: propertyName.append(Character.toUpperCase(c));
121: toUpper = false;
122: } else {
123: propertyName.append(Character.toLowerCase(c));
124: }
125: }
126: return propertyName.toString();
127: }
128: }
|