KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > sql > Alias


1 //$Id: Alias.java,v 1.3 2005/02/12 07:19:45 steveebersole Exp $
2
package org.hibernate.sql;
3
4 import org.hibernate.dialect.Dialect;
5
6 /**
7  * An alias generator for SQL identifiers
8  * @author Gavin King
9  */

10 public final class Alias {
11
12     private final int length;
13     private final String JavaDoc suffix;
14
15     /**
16      * Constructor for Alias.
17      */

18     public Alias(int length, String JavaDoc suffix) {
19         super();
20         this.length = (suffix==null) ? length : length - suffix.length();
21         this.suffix = suffix;
22     }
23
24     /**
25      * Constructor for Alias.
26      */

27     public Alias(String JavaDoc suffix) {
28         super();
29         this.length = Integer.MAX_VALUE;
30         this.suffix = suffix;
31     }
32
33     public String JavaDoc toAliasString(String JavaDoc sqlIdentifier) {
34         char begin = sqlIdentifier.charAt(0);
35         int quoteType = Dialect.QUOTE.indexOf(begin);
36         String JavaDoc 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 JavaDoc toUnquotedAliasString(String JavaDoc sqlIdentifier) {
47         return getUnquotedAliasString(sqlIdentifier);
48     }
49
50     private String JavaDoc getUnquotedAliasString(String JavaDoc sqlIdentifier) {
51         char begin = sqlIdentifier.charAt(0);
52         int quoteType = Dialect.QUOTE.indexOf(begin);
53         return getUnquotedAliasString(sqlIdentifier, quoteType);
54     }
55
56     private String JavaDoc getUnquotedAliasString(String JavaDoc sqlIdentifier, int quoteType) {
57         String JavaDoc unquoted = sqlIdentifier;
58         if ( quoteType >= 0 ) {
59             //if the identifier is quoted, remove the quotes
60
unquoted = unquoted.substring( 1, unquoted.length()-1 );
61         }
62         if ( unquoted.length() > length ) {
63             //truncate the identifier to the max alias length, less the suffix length
64
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 JavaDoc[] toUnquotedAliasStrings(String JavaDoc[] sqlIdentifiers) {
75         String JavaDoc[] aliases = new String JavaDoc[ 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 JavaDoc[] toAliasStrings(String JavaDoc[] sqlIdentifiers) {
83         String JavaDoc[] aliases = new String JavaDoc[ 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