1 28 29 package com.caucho.amber.expr; 30 31 import com.caucho.amber.query.QueryParser; 32 import com.caucho.amber.type.*; 33 import com.caucho.util.CharBuffer; 34 35 import java.sql.PreparedStatement ; 36 import java.sql.SQLException ; 37 38 41 public class ArgExpr extends AbstractAmberExpr { 42 private QueryParser _parser; 43 44 private int _index; 46 47 private int _sqlIndex; 48 49 private String _name; 50 51 private Type _type; 52 53 58 public ArgExpr(QueryParser parser, int index) 59 { 60 _parser = parser; 61 62 _index = index; 63 64 _sqlIndex = -1; 65 } 66 67 72 public ArgExpr(QueryParser parser, String name, int index) 73 { 74 _parser = parser; 75 76 _name = name; 77 78 _index = index; 79 80 _sqlIndex = -1; 81 } 82 83 98 99 102 int getIndex() 103 { 104 return _index; 105 } 106 107 110 public Type getType() 111 { 112 return _type; 113 } 114 115 118 public void setType(Type type) 119 { 120 _type = type; 121 } 122 123 126 public AmberExpr bindSelect(QueryParser parser) 127 { 128 parser.addArg(this); 129 130 return this; 131 } 132 133 136 @Override 137 public boolean exists() 138 { 139 return true; 141 } 142 143 146 public void generateWhere(CharBuffer cb) 147 { 148 generateInternalWhere(cb, true); 149 } 150 151 154 public void generateUpdateWhere(CharBuffer cb) 155 { 156 generateInternalWhere(cb, false); 157 } 158 159 162 public void generateHaving(CharBuffer cb) 163 { 164 generateWhere(cb); 165 } 166 167 171 public String getName() 172 { 173 return _name; 174 } 175 176 179 public void setParameter(PreparedStatement pstmt, int i, 180 Type []argTypes, Object []argValues) 181 throws SQLException 182 { 183 if (_name == null) { 184 185 if (getType() != null) { 187 if (! ((getType() instanceof UtilDateType) || 188 (getType() instanceof CalendarType))) { 189 argTypes[_index - 1] = getType(); 190 } 191 } 192 193 if (argTypes[_index - 1] != null) { 194 195 argTypes[_index - 1].setParameter(pstmt, _sqlIndex + 1, 196 argValues[_index - 1]); 197 } 199 else 200 pstmt.setString(_sqlIndex + 1, null); 201 } 202 else { 203 if (getType() != null) { 205 if (! ((getType() instanceof UtilDateType) || 207 (getType() instanceof CalendarType))) { 208 argTypes[i - 1] = getType(); 209 } 210 } 211 212 if (argTypes[i - 1] != null) { 213 215 argTypes[i - 1].setParameter(pstmt, i, argValues[i - 1]); 217 } 218 else 219 pstmt.setString(_sqlIndex + 1, null); 220 } 221 222 } 223 224 public String toString() 225 { 226 if (_name == null) 227 return "?" + _index; 228 else 229 return ":" + _name; 230 } 231 232 235 private void generateInternalWhere(CharBuffer cb, 236 boolean select) 237 { 238 if (_sqlIndex < 0) 239 _sqlIndex = _parser.generateSQLArg(); 240 241 cb.append("?"); 242 } 243 } 244 | Popular Tags |