1 10 11 package com.triactive.jdo.store; 12 13 14 class SQLIdentifier 15 { 16 protected final DatabaseAdapter dba; 17 18 protected String javaName; 19 protected String sqlIdentifier; 20 protected String 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 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 66 67 private static final int HASH_RANGE = Character.MAX_RADIX * Character.MAX_RADIX / 2; 68 69 protected void setJavaName(String javaName) 70 { 71 this.javaName = javaName; 72 73 StringBuffer s = new StringBuffer (); 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 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 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 ("Illegal Java identifier: " + javaName); 111 112 setSQLIdentifier(truncate(s.toString(), getMaxLength())); 113 } 114 115 116 protected void setSQLIdentifier(String 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 truncate(String sqlIdentifier, int length) 126 { 127 if (sqlIdentifier.length() > length) 128 { 129 int tailIndex = length - 2; 130 int tailHash = sqlIdentifier.substring(tailIndex).hashCode(); 131 132 133 if (tailHash < 0) 134 tailHash = tailHash % HASH_RANGE + (HASH_RANGE - 1); 135 else 136 tailHash = tailHash % HASH_RANGE + HASH_RANGE; 137 138 String 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 getJavaName() 148 { 149 return javaName; 150 } 151 152 153 public String 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 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 toString() 180 { 181 if (dba.isSQLKeyword(sqlIdentifier)) 182 return identifierQuoteString + sqlIdentifier + identifierQuoteString; 183 else 184 return sqlIdentifier; 185 } 186 } 187 | Popular Tags |