1 package org.hibernate.sql; 3 4 import org.hibernate.dialect.Dialect; 5 6 10 public final class Alias { 11 12 private final int length; 13 private final String suffix; 14 15 18 public Alias(int length, String suffix) { 19 super(); 20 this.length = (suffix==null) ? length : length - suffix.length(); 21 this.suffix = suffix; 22 } 23 24 27 public Alias(String suffix) { 28 super(); 29 this.length = Integer.MAX_VALUE; 30 this.suffix = suffix; 31 } 32 33 public String toAliasString(String sqlIdentifier) { 34 char begin = sqlIdentifier.charAt(0); 35 int quoteType = Dialect.QUOTE.indexOf(begin); 36 String unquoted = getUnquotedAliasString(sqlIdentifier, quoteType); 37 if ( quoteType >= 0 ) { 38 char endQuote = Dialect.CLOSED_QUOTE.charAt(quoteType); 39 return begin + unquoted + endQuote; 40 } 41 else { 42 return unquoted; 43 } 44 } 45 46 public String toUnquotedAliasString(String sqlIdentifier) { 47 return getUnquotedAliasString(sqlIdentifier); 48 } 49 50 private String getUnquotedAliasString(String sqlIdentifier) { 51 char begin = sqlIdentifier.charAt(0); 52 int quoteType = Dialect.QUOTE.indexOf(begin); 53 return getUnquotedAliasString(sqlIdentifier, quoteType); 54 } 55 56 private String getUnquotedAliasString(String sqlIdentifier, int quoteType) { 57 String unquoted = sqlIdentifier; 58 if ( quoteType >= 0 ) { 59 unquoted = unquoted.substring( 1, unquoted.length()-1 ); 61 } 62 if ( unquoted.length() > length ) { 63 unquoted = unquoted.substring(0, length); 65 } 66 if ( suffix == null ) { 67 return unquoted; 68 } 69 else { 70 return unquoted + suffix; 71 } 72 } 73 74 public String [] toUnquotedAliasStrings(String [] sqlIdentifiers) { 75 String [] aliases = new String [ sqlIdentifiers.length ]; 76 for ( int i=0; i<sqlIdentifiers.length; i++ ) { 77 aliases[i] = toUnquotedAliasString(sqlIdentifiers[i]); 78 } 79 return aliases; 80 } 81 82 public String [] toAliasStrings(String [] sqlIdentifiers) { 83 String [] aliases = new String [ sqlIdentifiers.length ]; 84 for ( int i=0; i<sqlIdentifiers.length; i++ ) { 85 aliases[i] = toAliasString(sqlIdentifiers[i]); 86 } 87 return aliases; 88 } 89 90 } 91 | Popular Tags |