KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > HsqlNameManager


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb;
33
34 import org.hsqldb.lib.StringConverter;
35
36 /**
37  * Provides Name Management for SQL objects. <p>
38  *
39  * This class now includes the HsqlName class introduced in 1.7.1 and improves
40  * auto-naming with multiple databases in the engine.<p>
41  *
42  * Methods check user defined names and issue system generated names
43  * for SQL objects.<p>
44  *
45  * This class does not deal with the type of the SQL object for which it
46  * is used.<p>
47  *
48  * Some names beginning with SYS_ are reserved for system generated names.
49  * These are defined in isReserveName(String name) and created by the
50  * makeAutoName(String type) factory method<p>
51  *
52  * sysNumber is used to generate system-generated names. It is
53  * set to the largest integer encountered in names that use the
54  * SYS_xxxxxxx_INTEGER format. As the DDL is processed before any ALTER
55  * command, any new system generated name will have a larger integer suffix
56  * than all the existing names.
57  *
58  * @author fredt@users
59  * @version 1.8.0
60  * @since 1.7.2
61  */

62 public class HsqlNameManager {
63
64     private static HsqlNameManager staticManager = new HsqlNameManager();
65
66     static {
67         staticManager.serialNumber = Integer.MIN_VALUE;
68     }
69
70     private int serialNumber = 1; // 0 is reserved in lookups
71
private int sysNumber = 0;
72
73     static HsqlName newHsqlSystemObjectName(String JavaDoc name) {
74         return new HsqlName(staticManager, name);
75     }
76
77     public HsqlName newHsqlName(String JavaDoc name, boolean isquoted) {
78         return new HsqlName(this, name, isquoted);
79     }
80
81     HsqlName newHsqlName(String JavaDoc prefix, String JavaDoc name, boolean isquoted) {
82         return new HsqlName(this, prefix, name, isquoted);
83     }
84
85     HsqlName newHsqlName(String JavaDoc name) {
86         return new HsqlName(this, name);
87     }
88
89     /**
90      * Auto names are used for autogenerated indexes or anonymous constraints.
91      * Also the name of a pseudo-column is the autoname ""
92      */

93     public HsqlName newAutoName(String JavaDoc type) {
94         return newAutoName(type, null);
95     }
96
97     /**
98      * Auto names are used for autogenerated indexes or anonymous constraints.
99      */

100     HsqlName newAutoName(String JavaDoc type, String JavaDoc namepart) {
101
102         StringBuffer JavaDoc sbname = new StringBuffer JavaDoc();
103
104         if (type != null) {
105             if (type.length() != 0) {
106                 sbname.append("SYS_");
107                 sbname.append(type);
108                 sbname.append('_');
109
110                 if (namepart != null) {
111                     sbname.append(namepart);
112                     sbname.append('_');
113                 }
114
115                 sbname.append(++sysNumber);
116             }
117         } else {
118             sbname.append(namepart);
119         }
120
121         return new HsqlName(this, sbname.toString());
122     }
123
124     void resetNumbering() {
125         sysNumber = 0;
126         serialNumber = 0;
127     }
128
129     public static class HsqlName {
130
131         HsqlNameManager manager;
132         public String JavaDoc name;
133         boolean isNameQuoted;
134         public String JavaDoc statementName;
135         public HsqlName schema;
136         private final int hashCode;
137
138         private HsqlName(HsqlNameManager man) {
139             manager = man;
140             hashCode = manager.serialNumber++;
141         }
142
143         private HsqlName(HsqlNameManager man, String JavaDoc name, boolean isquoted) {
144
145             this(man);
146
147             rename(name, isquoted);
148         }
149
150         private HsqlName(HsqlNameManager man, String JavaDoc prefix, String JavaDoc name,
151                          boolean isquoted) {
152
153             this(man);
154
155             rename(prefix, name, isquoted);
156         }
157
158         private HsqlName(HsqlNameManager man, String JavaDoc name) {
159
160             this(man);
161
162             this.name = this.statementName = name;
163         }
164
165         void rename(String JavaDoc name, boolean isquoted) {
166
167             this.name = name;
168             this.statementName = name;
169             this.isNameQuoted = isquoted;
170
171             if (isNameQuoted) {
172                 statementName = StringConverter.toQuotedString(name, '"',
173                         true);
174             }
175
176             if (name.startsWith("SYS_")) {
177                 int index = name.lastIndexOf('_') + 1;
178
179                 try {
180                     int temp = Integer.parseInt(name.substring(index));
181
182                     if (temp > manager.sysNumber) {
183                         manager.sysNumber = temp;
184                     }
185                 } catch (NumberFormatException JavaDoc e) {}
186             }
187         }
188
189         void rename(String JavaDoc prefix, String JavaDoc name, boolean isquoted) {
190
191             StringBuffer JavaDoc sbname = new StringBuffer JavaDoc(prefix);
192
193             sbname.append('_');
194             sbname.append(name);
195             rename(sbname.toString(), isquoted);
196         }
197
198         public boolean equals(Object JavaDoc other) {
199
200             if (other instanceof HsqlName) {
201                 return hashCode == ((HsqlName) other).hashCode;
202             }
203
204             return false;
205         }
206
207         /**
208          * hash code for this object is its unique serial number.
209          */

210         public int hashCode() {
211             return hashCode;
212         }
213
214         /**
215          * "SYS_IDX_" is used for auto-indexes on referring FK columns or
216          * unique constraints.
217          * "SYS_PK_" is for the primary key indexes.
218          * "SYS_REF_" is for FK constraints in referenced tables
219          *
220          */

221         static boolean isReservedIndexName(String JavaDoc name) {
222             return (name.startsWith("SYS_IDX_") || name.startsWith("SYS_PK_")
223                     || name.startsWith("SYS_REF_"));
224         }
225
226         boolean isReservedIndexName() {
227             return isReservedIndexName(name);
228         }
229
230         public String JavaDoc toString() {
231
232             return getClass().getName() + super.hashCode()
233                    + "[this.hashCode()=" + this.hashCode + ", name=" + name
234                    + ", name.hashCode()=" + name.hashCode()
235                    + ", isNameQuoted=" + isNameQuoted + "]";
236         }
237
238         public int compareTo(Object JavaDoc o) {
239             return hashCode - o.hashCode();
240         }
241
242         /**
243          * Returns true if the identifier consists of all uppercase letters
244          * digits and underscore, beginning with a letter and is not in the
245          * keyword list.
246          */

247         static boolean isRegularIdentifier(String JavaDoc name) {
248
249             for (int i = 0, length = name.length(); i < length; i++) {
250                 int c = name.charAt(i);
251
252                 if (c >= 'A' && c <= 'Z') {
253                     continue;
254                 } else if (c == '_' && i > 0) {
255                     continue;
256                 } else if (c >= '0' && c <= '9') {
257                     continue;
258                 }
259
260                 return false;
261             }
262
263             return !Token.isKeyword(name);
264         }
265     }
266 }
267
Popular Tags