1 28 29 package com.caucho.db.sql; 30 31 import com.caucho.db.table.Column; 32 33 public class Data { 34 private static final int NULL = 0; 35 private static final int BOOLEAN = NULL + 1; 36 private static final int STRING = BOOLEAN + 1; 37 private static final int INTEGER = STRING + 1; 38 private static final int LONG = INTEGER + 1; 39 private static final int DOUBLE = LONG + 1; 40 private static final int EXPR = DOUBLE + 1; 41 42 private Column _column; 43 44 private int _type; 45 private boolean _booleanData; 46 private String _stringData; 47 private int _intData; 48 private long _longData; 49 private double _doubleData; 50 private Expr _expr; 51 52 public void clear() 53 { 54 _type = NULL; 55 } 56 57 public void setColumn(Column column) 58 { 59 _column = column; 60 } 61 62 public Column getColumn() 63 { 64 return _column; 65 } 66 67 70 public boolean isNull() 71 { 72 return _type == NULL; 73 } 74 75 78 public void setString(String value) 79 { 80 if (value == null) 81 _type = NULL; 82 else { 83 _type = STRING; 84 _stringData = value; 85 } 86 } 87 88 91 public String getString() 92 { 93 switch (_type) { 94 case NULL: 95 return null; 96 97 case BOOLEAN: 98 return _booleanData ? "true" : "false"; 99 100 case INTEGER: 101 return String.valueOf(_intData); 102 103 case LONG: 104 return String.valueOf(_longData); 105 106 case DOUBLE: 107 return String.valueOf(_doubleData); 108 109 case STRING: 110 return _stringData; 111 112 default: 113 throw new UnsupportedOperationException (); 114 } 115 } 116 117 120 public void setBoolean(boolean value) 121 { 122 _type = BOOLEAN; 123 _booleanData = value; 124 } 125 126 129 public int getBoolean() 130 { 131 switch (_type) { 132 case NULL: 133 return Expr.UNKNOWN; 134 135 case BOOLEAN: 136 return _booleanData ? Expr.TRUE : Expr.FALSE; 137 138 case INTEGER: 139 return _intData != 0 ? Expr.TRUE : Expr.FALSE; 140 141 case LONG: 142 return _longData != 0 ? Expr.TRUE : Expr.FALSE; 143 144 case DOUBLE: 145 return _doubleData != 0 ? Expr.TRUE : Expr.FALSE; 146 147 case STRING: 148 return _stringData.equalsIgnoreCase("y") ? Expr.TRUE : Expr.FALSE; 149 150 default: 151 throw new UnsupportedOperationException (); 152 } 153 } 154 155 158 public void setInt(int value) 159 { 160 _type = INTEGER; 161 _intData = value; 162 } 163 164 167 public int getInt() 168 { 169 switch (_type) { 170 case NULL: 171 return 0; 172 173 case BOOLEAN: 174 return _booleanData ? 1 : 0; 175 176 case INTEGER: 177 return _intData; 178 179 case LONG: 180 return (int) _longData; 181 182 case DOUBLE: 183 return (int) _doubleData; 184 185 case STRING: 186 return Integer.parseInt(_stringData); 187 188 default: 189 throw new UnsupportedOperationException (); 190 } 191 } 192 193 196 public void setLong(long value) 197 { 198 _type = LONG; 199 _longData = value; 200 } 201 202 205 public long getLong() 206 { 207 switch (_type) { 208 case NULL: 209 return 0; 210 211 case BOOLEAN: 212 return _booleanData ? 1 : 0; 213 214 case INTEGER: 215 return _intData; 216 217 case LONG: 218 return _longData; 219 220 case DOUBLE: 221 return (long) _doubleData; 222 223 case STRING: 224 return Long.parseLong(_stringData); 225 226 default: 227 throw new UnsupportedOperationException (); 228 } 229 } 230 231 234 public long getDate() 235 { 236 switch (_type) { 237 case NULL: 238 return 0; 239 240 case BOOLEAN: 241 return _booleanData ? 1 : 0; 242 243 case INTEGER: 244 return _intData; 245 246 case LONG: 247 return _longData; 248 249 case DOUBLE: 250 return (long) _doubleData; 251 252 case STRING: 253 return Long.parseLong(_stringData); 254 255 default: 256 throw new UnsupportedOperationException (); 257 } 258 } 259 260 263 public void setDouble(double value) 264 { 265 _type = DOUBLE; 266 _doubleData = value; 267 } 268 269 272 public double getDouble() 273 { 274 switch (_type) { 275 case NULL: 276 return 0; 277 278 case BOOLEAN: 279 return _booleanData ? 1 : 0; 280 281 case INTEGER: 282 return _intData; 283 284 case LONG: 285 return _longData; 286 287 case DOUBLE: 288 return _doubleData; 289 290 case STRING: 291 return Double.parseDouble(_stringData); 292 293 default: 294 throw new UnsupportedOperationException (); 295 } 296 } 297 298 301 public void copyTo(Data dst) 302 { 303 switch (_type) { 304 case NULL: 305 dst.setString(null); 306 break; 307 308 case BOOLEAN: 309 dst.setBoolean(_booleanData); 310 break; 311 312 case INTEGER: 313 dst.setInt(_intData); 314 break; 315 316 case LONG: 317 dst.setLong(_longData); 318 break; 319 320 case DOUBLE: 321 dst.setDouble(_doubleData); 322 break; 323 324 case STRING: 325 dst.setString(_stringData); 326 break; 327 328 default: 329 throw new UnsupportedOperationException (); 330 } 331 } 332 333 336 public int hashCode() 337 { 338 switch (_type) { 339 case NULL: 340 return 17; 341 342 case BOOLEAN: 343 return _booleanData ? 1 : 0; 344 345 case INTEGER: 346 return _intData; 347 348 case LONG: 349 return (int) _longData; 350 351 case DOUBLE: 352 return (int) _doubleData; 353 354 case STRING: 355 return _stringData.hashCode(); 356 357 default: 358 return 97; 359 } 360 } 361 362 365 public boolean equals(Object o) 366 { 367 if (this == o) 368 return true; 369 else if (! getClass().equals(o.getClass())) 370 return false; 371 372 Data data = (Data) o; 373 374 if (_type != data._type) 375 return false; 376 377 switch (_type) { 378 case NULL: 379 return false; 380 381 case BOOLEAN: 382 return _booleanData == data._booleanData; 383 384 case INTEGER: 385 return _intData == data._intData; 386 387 case LONG: 388 return _longData == data._longData; 389 390 case DOUBLE: 391 return _doubleData == data._doubleData; 392 393 case STRING: 394 return _stringData.equals(data._stringData); 395 396 default: 397 return false; 398 } 399 } 400 } 401 | Popular Tags |