1 30 31 32 package org.hsqldb; 33 34 import org.hsqldb.lib.StringConverter; 35 36 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; private int sysNumber = 0; 72 73 static HsqlName newHsqlSystemObjectName(String name) { 74 return new HsqlName(staticManager, name); 75 } 76 77 public HsqlName newHsqlName(String name, boolean isquoted) { 78 return new HsqlName(this, name, isquoted); 79 } 80 81 HsqlName newHsqlName(String prefix, String name, boolean isquoted) { 82 return new HsqlName(this, prefix, name, isquoted); 83 } 84 85 HsqlName newHsqlName(String name) { 86 return new HsqlName(this, name); 87 } 88 89 93 public HsqlName newAutoName(String type) { 94 return newAutoName(type, null); 95 } 96 97 100 HsqlName newAutoName(String type, String namepart) { 101 102 StringBuffer sbname = new StringBuffer (); 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 name; 133 boolean isNameQuoted; 134 public String 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 name, boolean isquoted) { 144 145 this(man); 146 147 rename(name, isquoted); 148 } 149 150 private HsqlName(HsqlNameManager man, String prefix, String name, 151 boolean isquoted) { 152 153 this(man); 154 155 rename(prefix, name, isquoted); 156 } 157 158 private HsqlName(HsqlNameManager man, String name) { 159 160 this(man); 161 162 this.name = this.statementName = name; 163 } 164 165 void rename(String 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 e) {} 186 } 187 } 188 189 void rename(String prefix, String name, boolean isquoted) { 190 191 StringBuffer sbname = new StringBuffer (prefix); 192 193 sbname.append('_'); 194 sbname.append(name); 195 rename(sbname.toString(), isquoted); 196 } 197 198 public boolean equals(Object other) { 199 200 if (other instanceof HsqlName) { 201 return hashCode == ((HsqlName) other).hashCode; 202 } 203 204 return false; 205 } 206 207 210 public int hashCode() { 211 return hashCode; 212 } 213 214 221 static boolean isReservedIndexName(String 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 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 o) { 239 return hashCode - o.hashCode(); 240 } 241 242 247 static boolean isRegularIdentifier(String 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 |