001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.orm.hibernate.naming; 020 021import lucee.loader.util.Util; 022import lucee.runtime.orm.naming.NamingStrategy; 023 024public class SmartNamingStrategy implements NamingStrategy { 025 026 public static final NamingStrategy INSTANCE = new SmartNamingStrategy(); 027 028 @Override 029 public String convertTableName(String tableName) { 030 return translate(tableName); 031 } 032 033 @Override 034 public String convertColumnName(String columnName) { 035 return translate(columnName); 036 } 037 038 private static String translate(String name) { 039 if(Util.isEmpty(name)) return ""; 040 041 int len=name.length(); 042 StringBuilder sb = new StringBuilder(); 043 char c,p,n; 044 for(int i=0;i<len;i++) { 045 c=name.charAt(i); 046 if(i==0 || i+1==len) { 047 sb.append(Character.toUpperCase(c)); 048 continue; 049 } 050 p=name.charAt(i-1); 051 n=name.charAt(i+1); 052 053 // is Camel 054 if(Character.isLowerCase(p) && Character.isUpperCase(c) && Character.isLowerCase(n)) { 055 sb.append('_'); 056 sb.append(Character.toUpperCase(c)); 057 sb.append(Character.toUpperCase(n)); 058 i++; 059 } 060 else 061 sb.append(Character.toUpperCase(c)); 062 } 063 return sb.toString(); 064 } 065 066 @Override 067 public String getType() { 068 return "smart"; 069 } 070 071}