1 28 29 package com.caucho.amber.expr; 30 31 import com.caucho.amber.query.FromItem; 32 import com.caucho.amber.query.QueryParser; 33 import com.caucho.amber.table.LinkColumns; 34 import com.caucho.amber.table.Table; 35 import com.caucho.util.CharBuffer; 36 37 40 public class EmptyExpr extends AbstractAmberExpr { 41 private AmberExpr _collectionExpr; 42 43 private String _tableName; 44 45 public EmptyExpr(AmberExpr collectionExpr) 46 { 47 _collectionExpr = collectionExpr; 48 } 49 50 53 public boolean isBoolean() 54 { 55 return true; 56 } 57 58 61 public AmberExpr bindSelect(QueryParser parser) 62 { 63 _tableName = parser.createTableName(); 64 65 return this; 66 } 67 68 71 public boolean usesFrom(FromItem from, int type, boolean isNot) 72 { 73 return (_collectionExpr.usesFrom(from, type)); 74 } 75 76 79 public AmberExpr replaceJoin(JoinExpr join) 80 { 81 _collectionExpr = _collectionExpr.replaceJoin(join); 82 83 return this; 84 } 85 86 89 public void generateWhere(CharBuffer cb) 90 { 91 generateInternalWhere(cb, true); 92 } 93 94 97 public void generateUpdateWhere(CharBuffer cb) 98 { 99 generateInternalWhere(cb, false); 100 } 101 102 105 public void generateHaving(CharBuffer cb) 106 { 107 generateWhere(cb); 108 } 109 110 113 private void generateInternalWhere(CharBuffer cb, 114 boolean select) 115 { 116 OneToManyExpr oneToMany = null; 117 118 if (_collectionExpr instanceof ManyToOneExpr) { 121 PathExpr expr = ((ManyToOneExpr) _collectionExpr).getParent(); 122 if (expr instanceof OneToManyExpr) 123 oneToMany = (OneToManyExpr) expr; 124 125 } else if (_collectionExpr instanceof OneToManyExpr) { 126 oneToMany = (OneToManyExpr) _collectionExpr; 127 } 128 else 129 throw new UnsupportedOperationException (); 130 131 LinkColumns join = oneToMany.getLinkColumns(); 132 133 Table table = join.getSourceTable(); 134 cb.append("EXISTS(SELECT "); 135 136 if (table.getIdColumns().size() > 0) 137 cb.append(table.getIdColumns().get(0).getName()); 138 else 139 cb.append('*'); 140 141 cb.append(" FROM " + table.getName() + " " + _tableName); 142 cb.append(" WHERE "); 143 144 String targetTable = oneToMany.getParent().getChildFromItem().getName(); 145 146 cb.append(join.generateJoin(_tableName, targetTable)); 147 148 cb.append(')'); 149 } 150 } 151 | Popular Tags |