1 29 30 package com.caucho.db.sql; 31 32 import com.caucho.log.Log; 33 import com.caucho.util.QDate; 34 35 import java.io.InputStream ; 36 import java.sql.SQLException ; 37 import java.util.ArrayList ; 38 import java.util.logging.Logger ; 39 40 class ParamExpr extends Expr { 41 private static final Logger log = Log.open(ParamExpr.class); 42 43 private static final int NULL = 0; 44 private static final int BOOLEAN = NULL + 1; 45 private static final int STRING = BOOLEAN + 1; 46 private static final int LONG = STRING + 1; 47 private static final int DOUBLE = LONG + 1; 48 private static final int DATE = DOUBLE + 1; 49 private static final int BINARY = DATE + 1; 50 51 private int _index; 52 53 private int _type = NULL; 54 55 private String _stringValue; 56 private long _longValue; 57 private double _doubleValue; 58 59 private InputStream _binaryStream; 60 private int _streamLength; 61 62 ParamExpr(int index) 63 { 64 _index = index; 65 } 66 67 70 public Class getType() 71 { 72 switch (_type) { 73 case NULL: 74 return Object .class; 75 76 case BOOLEAN: 77 return boolean.class; 78 79 case STRING: 80 return String .class; 81 82 case LONG: 83 return long.class; 84 85 case DOUBLE: 86 return double.class; 87 88 case DATE: 89 return java.util.Date .class; 90 91 case BINARY: 92 return java.io.InputStream .class; 93 94 default: 95 return Object .class; 96 } 97 } 98 99 102 public long subCost(ArrayList <FromItem> fromList) 103 { 104 return 0; 105 } 106 107 110 public void clear() 111 { 112 _type = NULL; 113 } 114 115 118 public void setString(String value) 119 { 120 if (value == null) 121 _type = NULL; 122 else { 123 _type = STRING; 124 _stringValue = value; 125 } 126 } 127 128 131 public void setBoolean(boolean value) 132 { 133 _type = BOOLEAN; 134 _longValue = value ? 1 : 0; 135 } 136 137 140 public void setLong(long value) 141 { 142 _type = LONG; 143 _longValue = value; 144 } 145 146 149 public void setDouble(double value) 150 { 151 _type = DOUBLE; 152 _doubleValue = value; 153 } 154 155 158 public void setDate(long value) 159 { 160 _type = DATE; 161 _longValue = value; 162 } 163 164 167 public void setBinaryStream(InputStream is, int length) 168 { 169 _type = BINARY; 170 _binaryStream = is; 171 _streamLength = length; 172 } 173 174 181 public boolean isNull(QueryContext context) 182 throws SQLException 183 { 184 return _type == NULL; 185 } 186 187 194 public String evalString(QueryContext context) 195 throws SQLException 196 { 197 switch (_type) { 198 case NULL: 199 return null; 200 201 case BOOLEAN: 202 return _longValue != 0 ? "1" : "0"; 203 204 case STRING: 205 return _stringValue; 206 207 case LONG: 208 return String.valueOf(_longValue); 209 210 case DATE: 211 return QDate.formatISO8601(_longValue); 212 213 case DOUBLE: 214 return String.valueOf(_doubleValue); 215 216 default: 217 throw new UnsupportedOperationException (String.valueOf(_type)); 218 } 219 } 220 221 228 public int evalBoolean(QueryContext context) 229 throws SQLException 230 { 231 switch (_type) { 232 case NULL: 233 return UNKNOWN; 234 235 case BOOLEAN: 236 case LONG: 237 return _longValue != 0 ? TRUE : FALSE; 238 239 case DOUBLE: 240 return _doubleValue != 0 ? TRUE : FALSE; 241 242 default: 243 throw new UnsupportedOperationException (); 244 } 245 } 246 247 254 public long evalLong(QueryContext context) 255 throws SQLException 256 { 257 switch (_type) { 258 case NULL: 259 return 0; 260 261 case BOOLEAN: 262 case LONG: 263 case DATE: 264 return _longValue; 265 266 case DOUBLE: 267 return (long) _doubleValue; 268 269 case STRING: 270 return Long.parseLong(_stringValue); 271 272 default: 273 throw new UnsupportedOperationException ("" + _type); 274 } 275 } 276 277 284 public double evalDouble(QueryContext context) 285 throws SQLException 286 { 287 switch (_type) { 288 case NULL: 289 return 0; 290 291 case LONG: 292 case DATE: 293 return _longValue; 294 295 case DOUBLE: 296 return _doubleValue; 297 298 case STRING: 299 return Double.parseDouble(_stringValue); 300 301 default: 302 throw new UnsupportedOperationException (); 303 } 304 } 305 306 313 public long evalDate(QueryContext context) 314 throws SQLException 315 { 316 switch (_type) { 317 case NULL: 318 return 0; 319 320 case LONG: 321 case DATE: 322 return _longValue; 323 324 case DOUBLE: 325 return (long) _doubleValue; 326 327 default: 328 throw new UnsupportedOperationException (); 329 } 330 } 331 332 339 public InputStream evalStream(QueryContext context) 340 throws SQLException 341 { 342 switch (_type) { 343 case NULL: 344 return null; 345 346 case BINARY: 347 return _binaryStream; 348 349 default: 350 throw new UnsupportedOperationException (); 351 } 352 } 353 354 public String toString() 355 { 356 return "?" + _index; 357 } 358 } 359 | Popular Tags |