1 29 30 package com.caucho.amber.field; 31 32 import com.caucho.amber.manager.AmberPersistenceUnit; 33 import com.caucho.amber.table.Column; 34 import com.caucho.amber.type.RelatedType; 35 import com.caucho.config.ConfigException; 36 import com.caucho.java.JavaWriter; 37 import com.caucho.log.Log; 38 import com.caucho.util.CharBuffer; 39 import com.caucho.util.L10N; 40 41 import java.io.IOException ; 42 import java.sql.ResultSet ; 43 import java.sql.SQLException ; 44 import java.util.ArrayList ; 45 import java.util.HashMap ; 46 import java.util.HashSet ; 47 import java.util.Map ; 48 import java.util.logging.Logger ; 49 50 53 public class Id { 54 private static final L10N L = new L10N(Id.class); 55 protected static final Logger log = Log.open(Id.class); 56 57 private RelatedType _ownerType; 58 59 private ArrayList <IdField> _keys = new ArrayList <IdField>(); 60 private ArrayList <Column> _columns = new ArrayList <Column>(); 61 62 private EmbeddedIdField _embeddedIdField; 63 64 public Id(RelatedType ownerType, ArrayList <IdField> keys) 65 { 66 _ownerType = ownerType; 67 68 for (IdField key : keys) { 69 addKey(key); 70 } 71 } 72 73 public Id(RelatedType ownerType, IdField key) 74 { 75 _ownerType = ownerType; 76 77 if (key instanceof EmbeddedIdField) { 78 _embeddedIdField = (EmbeddedIdField) key; 79 } 80 else { 81 addKey(key); 83 } 84 } 85 86 89 private void addKey(IdField key) 90 { 91 _keys.add(key); 92 95 _columns.addAll(key.getColumns()); 96 98 for (Column column : key.getColumns()) { 99 _ownerType.getTable().addIdColumn(column); 100 } 101 } 102 103 private static ArrayList <IdField> createField(IdField field) 104 { 105 ArrayList <IdField> fields = new ArrayList <IdField>(); 106 fields.add(field); 107 return fields; 108 } 109 110 113 public RelatedType getOwnerType() 114 { 115 return _ownerType; 116 } 117 118 121 public ArrayList <Column> getColumns() 122 { 123 return _columns; 124 } 125 126 129 public ArrayList <IdField> getKeys() 130 { 131 return _keys; 132 } 133 134 137 public int getKeyCount() 138 { 139 return _columns.size(); 140 } 141 142 145 public IdField getKey() 146 { 147 return _keys.get(0); 148 } 149 150 153 public String getForeignTypeName() 154 { 155 return _keys.get(0).getForeignTypeName(); 156 } 157 158 161 public void init() 162 throws ConfigException 163 { 164 } 165 166 169 public void generatePrologue(JavaWriter out, 170 HashSet <Object > completedSet, 171 String name) 172 throws IOException 173 { 174 } 175 176 179 public void generatePrologue(JavaWriter out, HashSet <Object > completedSet) 180 throws IOException 181 { 182 for (int i = 0; i < _keys.size(); i++) 183 _keys.get(i).generatePrologue(out, completedSet); 184 } 185 186 189 public int generateLoadForeign(JavaWriter out, String rs, 190 String indexVar, int index) 191 throws IOException 192 { 193 return generateLoadForeign(out, rs, indexVar, index, 194 getForeignTypeName().replace('.', '_')); 195 } 196 197 200 public int generateLoadForeign(JavaWriter out, String rs, 201 String indexVar, int index, 202 String name) 203 throws IOException 204 { 205 if (_keys.size() > 1) 206 throw new UnsupportedOperationException (); 207 208 return _keys.get(0).generateLoadForeign(out, rs, indexVar, index, name); 209 } 210 211 214 public String generateSelect(String id) 215 { 216 return getKey().generateSelect(id); 217 } 218 219 222 public String generateJavaSelect(String id) 223 { 224 return getKey().generateJavaSelect(id); 225 } 226 227 230 public String generateLoadSelect(String id) 231 { 232 return null; 233 } 234 235 238 public String generateGetProperty(String value) 239 { 240 return getKey().generateGet(value); 241 } 242 243 246 public String generateGetProxyKey(String value) 247 { 248 return "((" + getForeignTypeName() + ") " + value + ".getPrimaryKey())"; 249 } 250 251 254 public void generateLoadFromObject(JavaWriter out, String obj) 255 throws IOException 256 { 257 ArrayList <IdField> keys = getKeys(); 258 259 for (int i = 0; i < keys.size(); i++) { 260 keys.get(i).generateLoadFromObject(out, obj); 261 } 262 } 263 264 267 public void generateSet(JavaWriter out, String obj) 268 throws IOException 269 { 270 IdField key = getKey(); 271 272 key.generateSet(out, key.toValue(obj)); 273 } 275 276 279 public void generateUpdateFromObject(JavaWriter out, String obj) 280 throws IOException 281 { 282 ArrayList <IdField> keys = getKeys(); 283 284 for (int i = 0; i < keys.size(); i++) { 285 keys.get(i).generateUpdateFromObject(out, obj); 286 } 287 } 288 289 292 public String generateMatchArgWhere(String id) 293 { 294 CharBuffer cb = new CharBuffer(); 295 296 boolean isFirst = true; 297 298 if (_embeddedIdField == null) { 299 for (IdField field : getKeys()) { 300 for (Column column : field.getColumns()) { 301 if (! isFirst) 302 cb.append(" and "); 303 isFirst = false; 304 305 cb.append(column.generateMatchArgWhere(id)); 306 } 307 } 308 } else { 309 HashMap <String , Column> columns 310 = _embeddedIdField.getEmbeddedColumns(); 311 312 for (Map.Entry <String , Column> entry : columns.entrySet()) { 313 Column column = entry.getValue(); 314 if (isFirst) 315 isFirst = false; 316 else 317 cb.append(" and "); 318 cb.append(column.generateMatchArgWhere(id)); 319 } 320 } 321 322 return cb.toString(); 323 } 324 325 328 public String generateRawWhere(String id) 329 { 330 ArrayList <IdField> keys = getKeys(); 331 332 CharBuffer cb = CharBuffer.allocate(); 333 334 for (int i = 0; i < keys.size(); i++) { 335 if (i != 0) 336 cb.append(" and "); 337 338 cb.append(keys.get(i).generateRawWhere(id)); 339 } 340 341 return cb.close(); 342 } 343 344 347 public String generateCreateTableSQL(AmberPersistenceUnit manager) 348 { 349 String sql = getKey().generateCreateTableSQL(manager); 350 351 if (getKey().getGenerator() != null) 352 return sql + " PRIMARY KEY auto_increment"; 353 else 354 return sql + " PRIMARY KEY"; 355 } 356 357 360 public void generateSetKey(JavaWriter out, String pstmt, 361 String index, String keyObject) 362 throws IOException 363 { 364 IdField key = getKey(); 365 366 key.getType().generateSet(out, pstmt, index, keyObject); 367 } 368 369 372 public void generateSet(JavaWriter out, String pstmt, 373 String index, String value) 374 throws IOException 375 { 376 ArrayList <IdField> keys = getKeys(); 377 378 for (int i = 0; i < keys.size(); i++) { 379 keys.get(i).generateSet(out, pstmt, index, value); 380 } 381 } 382 383 386 public void generateSet(JavaWriter out, String pstmt, String index) 387 throws IOException 388 { 389 ArrayList <IdField> keys = getKeys(); 390 391 for (int i = 0; i < keys.size(); i++) { 392 keys.get(i).generateSet(out, pstmt, index); 393 } 394 } 395 396 399 420 421 424 public void generateSetInsert(JavaWriter out, String pstmt, String index) 425 throws IOException 426 { 427 ArrayList <IdField> keys = getKeys(); 428 429 for (int i = 0; i < keys.size(); i++) { 430 keys.get(i).generateSetInsert(out, pstmt, index); 431 } 432 } 433 434 437 public String generateCastFromObject(String value) 438 { 439 return value; 440 } 441 442 445 public void generateMatch(JavaWriter out, String key) 446 throws IOException 447 { 448 IdField id = getKeys().get(0); 449 450 out.println("return (" + id.generateEquals(id.generateSuperGetter(), 451 id.toValue(key)) + ");"); 452 } 453 454 457 public String generateEquals(String leftBase, String value) 458 { 459 ArrayList <IdField> keys = getKeys(); 460 461 String eq = "("; 462 463 for (int i = 0; i < keys.size(); i++) { 464 IdField key = keys.get(i); 465 466 if (i != 0) 467 eq += " && "; 468 469 eq += key.generateEquals(leftBase, value); 470 } 471 472 return eq + ")"; 473 } 474 475 478 public void generateCheckCreateKey(JavaWriter out) 479 throws IOException 480 { 481 ArrayList <IdField> keys = getKeys(); 482 483 for (int i = 0; i < keys.size(); i++) { 484 keys.get(i).generateCheckCreateKey(out); 485 } 486 } 487 488 491 public void generateSetGeneratedKeys(JavaWriter out, String pstmt) 492 throws IOException 493 { 494 ArrayList <IdField> keys = getKeys(); 495 496 for (int i = 0; i < keys.size(); i++) { 497 keys.get(i).generateSetGeneratedKeys(out, pstmt); 498 } 499 } 500 501 504 public EmbeddedIdField getEmbeddedIdField() 505 { 506 return _embeddedIdField; 507 } 508 509 512 public boolean isEmbeddedId() 513 { 514 return _embeddedIdField != null; 515 } 516 517 520 public String toObject(String value) 521 { 522 return getKey().toObject(value); 523 } 524 525 528 public Object toObjectKey(long value) 529 { 530 return new Long (value); 532 } 533 534 537 public Object getObject(ResultSet rs, int index) 538 throws SQLException 539 { 540 return null; 541 } 542 } 543 | Popular Tags |