1 28 29 package com.caucho.amber.table; 30 31 import com.caucho.amber.manager.AmberPersistenceUnit; 32 import com.caucho.amber.type.Type; 33 import com.caucho.config.ConfigException; 34 import com.caucho.config.LineConfigException; 35 import com.caucho.java.JavaWriter; 36 import com.caucho.util.CharBuffer; 37 import com.caucho.util.L10N; 38 39 import javax.sql.DataSource ; 40 import java.io.IOException ; 41 import java.sql.Connection ; 42 import java.sql.ResultSet ; 43 import java.sql.SQLException ; 44 import java.sql.Statement ; 45 46 49 public class Column { 50 private static final L10N L = new L10N(Column.class); 51 52 private Table _table; 53 54 private String _name; 55 56 private String _configLocation; 57 58 private Type _type; 59 60 private boolean _isPrimaryKey; 61 62 private String _sqlType; 63 64 private boolean _isNotNull; 65 private boolean _isUnique; 66 private int _length; 67 private int _precision; 68 private int _scale; 69 70 private String _generatorType; 71 private String _generator; 72 73 private String _fieldName; 75 76 77 Column(Table table, String name) 78 { 79 _table = table; 80 _name = name; 81 } 82 83 90 Column(Table table, String name, Type type) 91 { 92 _table = table; 93 _name = name; 94 _type = type; 95 } 96 97 100 public Table getTable() 101 { 102 return _table; 103 } 104 105 108 public String getName() 109 { 110 return _name; 111 } 112 113 116 public void setName(String name) 117 { 118 _name = name; 119 } 120 121 124 public void setConfigLocation(String location) 125 { 126 _configLocation = location; 127 } 128 129 132 public Type getType() 133 { 134 return _type; 135 } 136 137 140 public void setPrimaryKey(boolean isPrimaryKey) 141 { 142 _isPrimaryKey = isPrimaryKey; 143 } 144 145 148 public boolean isPrimaryKey() 149 { 150 return _isPrimaryKey; 151 } 152 153 156 public void setGeneratorType(String type) 157 { 158 _generatorType = type; 159 } 160 161 164 public String generateInsertName() 165 { 166 return _name; 167 } 168 169 172 public void setSQLType(String sqlType) 173 { 174 _sqlType = sqlType; 175 } 176 177 180 public String getSQLType() 181 { 182 return _sqlType; 183 } 184 185 188 public void setLength(int length) 189 { 190 _length = length; 191 } 192 193 196 public int getLength() 197 { 198 return _length; 199 } 200 201 204 public void setNotNull(boolean isNotNull) 205 { 206 _isNotNull = isNotNull; 207 } 208 209 212 public boolean isNotNull() 213 { 214 return _isNotNull; 215 } 216 217 220 public void setPrecision(int precision) { 221 _precision = precision; 222 } 223 224 227 public int getPrecision() { 228 return _precision; 229 } 230 231 234 public void setScale(int scale) { 235 _scale = scale; 236 } 237 238 241 public int getScale() { 242 return _scale; 243 } 244 245 248 public void setUnique(boolean isUnique) 249 { 250 _isUnique = isUnique; 251 } 252 253 256 public boolean isUnique() 257 { 258 return _isUnique; 259 } 260 261 264 String generateCreateTableSQL(AmberPersistenceUnit manager) 265 { 266 CharBuffer cb = new CharBuffer(); 267 cb.append(_name + " "); 268 String sqlType = _sqlType; 269 270 if (_sqlType != null) 271 sqlType = _sqlType; 272 else { 273 sqlType = _type.generateCreateColumnSQL(manager, _length, _precision, _scale); 274 } 275 276 if ("identity".equals(_generatorType)) { 277 cb.append(manager.getMetaData().createIdentitySQL(sqlType)); 278 } else { 279 cb.append(sqlType); 280 } 281 282 if (isPrimaryKey()) { 283 cb.append(" primary key"); 284 } else if (! "identity".equals(_generatorType)) { 285 if (isNotNull()) 286 cb.append(" not null"); 287 if (isUnique()) 288 cb.append(" unique"); 289 } 290 291 return cb.toString(); 292 } 293 294 297 void validateDatabase(AmberPersistenceUnit amberPersistenceUnit) 298 throws ConfigException 299 { 300 try { 301 DataSource ds = amberPersistenceUnit.getDataSource(); 302 Connection conn = ds.getConnection(); 303 try { 304 Statement stmt = conn.createStatement(); 305 306 String sql = "select " + getName() + " from " + _table.getName() + " where 1=0"; 307 308 try { 309 311 ResultSet rs = stmt.executeQuery(sql); 312 rs.close(); 313 return; 314 } catch (SQLException e) { 315 throw error(L.l("'{0}' is not a valid database column in table '{1}'. Either the table needs to be created or the create-database-tables attribute must be set.\n\n {2}\n\n{3}", 316 getName(), 317 getTable().getName(), 318 sql, 319 e.toString()), 320 e); 321 } 322 } finally { 323 conn.close(); 324 } 325 } catch (ConfigException e) { 326 throw e; 327 } catch (Exception e) { 328 throw new ConfigException(e); 329 } 330 } 331 332 335 public String generateSelect(String id) 336 { 337 if (id != null) 338 return id + "." + _name; 339 else 340 return _name; 341 } 342 343 346 public String generateMatchArgWhere(String id) 347 { 348 if (id != null) 349 return id + "." + _name + "=?"; 350 else 351 return _name + "=?"; 352 } 353 354 357 public String generateUpdateSet() 358 { 359 return _name + "=?"; 360 } 361 362 365 public String generateUpdateSetNull() 366 { 367 return _name + "=null"; 368 } 369 370 373 public void generatePrologue(JavaWriter out) 374 throws IOException 375 { 376 } 377 378 381 public String getFieldName() 382 { 383 return "__amber_" + getName(); 384 } 385 386 389 public void generateSet(JavaWriter out, String pstmt, 390 String index, String value) 391 throws IOException 392 { 393 if (value != null) 394 _type.generateSet(out, pstmt, index, value); 395 else 396 _type.generateSetNull(out, pstmt, index); 397 } 398 399 402 public void generateSetVersion(JavaWriter out, 403 String pstmt, 404 String index, 405 String value) 406 throws IOException 407 { 408 _type.generateSetVersion(out, pstmt, index, value); 409 } 410 411 414 public int generateLoad(JavaWriter out, String rs, 415 String indexVar, int index) 416 throws IOException 417 { 418 return _type.generateLoad(out, rs, indexVar, index); 419 } 420 421 424 public Object toObjectKey(long value) 425 { 426 return getType().toObject(value); 427 } 428 429 protected ConfigException error(String msg, Throwable e) 430 { 431 if (_configLocation != null) 432 return new LineConfigException(_configLocation + msg, e); 433 else if (_table.getLocation() != null) 434 return new LineConfigException(_table.getLocation() + msg, e); 435 else 436 return new ConfigException(msg, e); 437 } 438 439 442 public String toString() 443 { 444 return "Column[" + getName() + "]"; 445 } 446 } 447 | Popular Tags |