KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > SQLIdentifier


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: SQLIdentifier.java,v 1.9 2003/03/14 19:29:54 pierreg0 Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13
14 class SQLIdentifier
15 {
16     protected final DatabaseAdapter dba;
17
18     protected String JavaDoc javaName;
19     protected String JavaDoc sqlIdentifier;
20     protected String JavaDoc identifierQuoteString;
21
22
23     public SQLIdentifier(DatabaseAdapter dba)
24     {
25         this.dba = dba;
26         this.javaName = null;
27         this.sqlIdentifier = null;
28         this.identifierQuoteString = dba.getIdentifierQuoteString();
29     }
30
31
32     public SQLIdentifier(DatabaseAdapter dba, String JavaDoc sqlIdentifier)
33     {
34         this.dba = dba;
35         this.javaName = null;
36         this.identifierQuoteString = dba.getIdentifierQuoteString();
37         setSQLIdentifier(sqlIdentifier);
38     }
39
40
41     protected int getMaxLength()
42     {
43         return SQL92Constants.MAX_IDENTIFIER_LENGTH;
44     }
45
46
47     /**
48      * Sets the Java identifier to the given value and computes a corresponding
49      * SQL identifier.
50      *
51      * <p>Conversion consists of breaking the identifier into words, converting
52      * each word to upper-case, and separating each one with an underscore "_".
53      * Words are identified by a leading upper-case character.
54      * Any leading or trailing underscores are removed.
55      *
56      * <p>The resulting SQL identifier is "generic" and not suitable for direct
57      * use in a SQL statement; the toString() method should be used for that.
58      *
59      * <p>The resulting SQL identifier also truncated to getMaxLength().
60      * The excess characters plus the previous two characters are removed and
61      * replaced with the base 36 representation of their hash value mod 1296 (36
62      * * 36).
63      *
64      * @param javaName the Java identifier.
65      */

66
67     private static final int HASH_RANGE = Character.MAX_RADIX * Character.MAX_RADIX / 2;
68
69     protected void setJavaName(String JavaDoc javaName)
70     {
71         this.javaName = javaName;
72
73         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
74         char prev = '\0';
75
76         for (int i = 0; i < javaName.length(); ++i)
77         {
78             char c = javaName.charAt(i);
79
80             if (c >= 'A' && c <= 'Z')
81             {
82                 if (prev >= 'a' && prev <= 'z')
83                     s.append('_');
84
85                 s.append(c);
86             }
87             else if (c >= 'a' && c <= 'z')
88                 s.append((char)(c - ('a' - 'A')));
89             else if (c >= '0' && c <= '9' || c == '_')
90                 s.append(c);
91             else if (c == '.')
92                 s.append('_');
93             else
94             {
95                 String JavaDoc cval = "000" + Integer.toHexString((int)c);
96
97                 s.append(cval.substring(cval.length() - (c > 0xff ? 4 : 2)));
98             }
99
100             prev = c;
101         }
102
103         /* Remove leading and trailing underscores. */
104         while (s.length() > 0 && s.charAt(0) == '_')
105             s.deleteCharAt(0);
106         while (s.length() > 0 && s.charAt(s.length() - 1) == '_')
107             s.deleteCharAt(s.length() - 1);
108
109         if (s.length() == 0)
110             throw new IllegalArgumentException JavaDoc("Illegal Java identifier: " + javaName);
111
112         setSQLIdentifier(truncate(s.toString(), getMaxLength()));
113     }
114
115
116     protected void setSQLIdentifier(String JavaDoc sqlIdentifier)
117     {
118         if (dba.storesLowerCaseIdentifiers())
119             this.sqlIdentifier = sqlIdentifier.toLowerCase();
120         else
121             this.sqlIdentifier = sqlIdentifier.toUpperCase();
122     }
123
124
125     protected static String JavaDoc truncate(String JavaDoc sqlIdentifier, int length)
126     {
127         if (sqlIdentifier.length() > length)
128         {
129             int tailIndex = length - 2;
130             int tailHash = sqlIdentifier.substring(tailIndex).hashCode();
131
132             /* Scale the hash code down to the range 0 - 1295. */
133             if (tailHash < 0)
134                 tailHash = tailHash % HASH_RANGE + (HASH_RANGE - 1);
135             else
136                 tailHash = tailHash % HASH_RANGE + HASH_RANGE;
137
138             String JavaDoc suffix = "0" + Integer.toString(tailHash, Character.MAX_RADIX);
139
140             return sqlIdentifier.substring(0, tailIndex) + suffix.substring(suffix.length() - 2);
141         }
142         else
143             return sqlIdentifier;
144     }
145
146
147     public String JavaDoc getJavaName()
148     {
149         return javaName;
150     }
151
152
153     public String JavaDoc getSQLIdentifier()
154     {
155         return sqlIdentifier;
156     }
157
158
159     public int hashCode()
160     {
161         return sqlIdentifier.hashCode();
162     }
163
164
165     public boolean equals(Object JavaDoc obj)
166     {
167         if (obj == this)
168             return true;
169
170         if (!(obj instanceof SQLIdentifier))
171             return false;
172
173         SQLIdentifier id = (SQLIdentifier)obj;
174
175         return sqlIdentifier.equals(id.sqlIdentifier);
176     }
177
178
179     public String JavaDoc toString()
180     {
181         if (dba.isSQLKeyword(sqlIdentifier))
182             return identifierQuoteString + sqlIdentifier + identifierQuoteString;
183         else
184             return sqlIdentifier;
185     }
186 }
187
Popular Tags