1 28 29 package com.caucho.amber.expr.fun; 30 31 import com.caucho.amber.expr.AbstractAmberExpr; 32 import com.caucho.amber.expr.AmberExpr; 33 import com.caucho.amber.expr.IdExpr; 34 import com.caucho.amber.expr.KeyColumnExpr; 35 import com.caucho.amber.manager.AmberConnection; 36 import com.caucho.amber.query.FromItem; 37 import com.caucho.amber.query.QueryParser; 38 import com.caucho.util.CharBuffer; 39 import com.caucho.util.L10N; 40 41 import java.sql.ResultSet ; 42 import java.sql.SQLException ; 43 import java.util.ArrayList ; 44 45 46 49 public class FunExpr extends AbstractAmberExpr { 50 private static final L10N L = new L10N(FunExpr.class); 51 52 QueryParser _parser; 53 54 String _id; 55 ArrayList <AmberExpr> _args; 56 boolean _distinct; 57 58 61 protected FunExpr(QueryParser parser, 62 String id, 63 ArrayList <AmberExpr> args, 64 boolean distinct) 65 { 66 _parser = parser; 67 _id = id; 68 _args = args; 69 _distinct = distinct; 70 } 71 72 public static FunExpr create(QueryParser parser, 73 String id, 74 ArrayList <AmberExpr> args, 75 boolean distinct) 76 { 77 return new FunExpr(parser, id, args, distinct); 78 } 79 80 83 public AmberExpr bindSelect(QueryParser parser) 84 { 85 for (int i = 0; i < _args.size(); i++) { 86 AmberExpr arg = _args.get(i); 87 88 arg = arg.bindSelect(parser); 89 90 _args.set(i, arg); 91 } 92 93 return this; 94 } 95 96 99 public boolean usesFrom(FromItem from, int type, boolean isNot) 100 { 101 for (int i = 0; i < _args.size(); i++) { 102 AmberExpr arg = _args.get(i); 103 104 if (arg instanceof IdExpr) { 105 IdExpr id = (IdExpr) arg; 106 107 if (id.getFromItem() == from) 109 return true; 110 } 111 112 if (arg instanceof KeyColumnExpr) { 113 KeyColumnExpr key = (KeyColumnExpr) arg; 114 115 if (key.usesFrom(from, IS_INNER_JOIN, false)) 117 return true; 118 } 119 120 if (arg.usesFrom(from, type)) 121 return true; 122 } 123 124 return false; 125 } 126 127 130 public void generateWhere(CharBuffer cb) 131 { 132 generateInternalWhere(cb, true); 133 } 134 135 138 public void generateUpdateWhere(CharBuffer cb) 139 { 140 generateInternalWhere(cb, false); 141 } 142 143 146 public void generateHaving(CharBuffer cb) 147 { 148 generateWhere(cb); 149 } 150 151 154 public Object getObject(AmberConnection aConn, ResultSet rs, int index) 155 throws SQLException 156 { 157 if (_id.equalsIgnoreCase("count")) 158 return rs.getLong(index); 159 160 if (_id.equalsIgnoreCase("avg")) 161 return rs.getDouble(index); 162 163 return super.getObject(aConn, rs, index); 164 } 165 166 public String toString() 167 { 168 String str = _id + "("; 169 170 if (_distinct) 171 str += "distinct "; 172 173 for (int i = 0; i < _args.size(); i++) { 174 if (i != 0) 175 str += ','; 176 177 str += _args.get(i); 178 } 179 180 return str + ")"; 181 } 182 183 185 188 ArrayList <AmberExpr> getArgs() 189 { 190 return _args; 191 } 192 193 196 private void generateInternalWhere(CharBuffer cb, 197 boolean select) 198 { 199 cb.append(_id); 200 cb.append('('); 201 202 if (_distinct) 203 cb.append("distinct "); 204 205 for (int i = 0; i < _args.size(); i++) { 206 if (i != 0) 207 cb.append(','); 208 209 if (select) 210 _args.get(i).generateWhere(cb); 211 else 212 _args.get(i).generateUpdateWhere(cb); 213 } 214 215 cb.append(')'); 216 } 217 } 218 | Popular Tags |