0001: package com.workingdogs.village;
0002:
0003: /*
0004: * Licensed to the Apache Software Foundation (ASF) under one
0005: * or more contributor license agreements. See the NOTICE file
0006: * distributed with this work for additional information
0007: * regarding copyright ownership. The ASF licenses this file
0008: * to you under the Apache License, Version 2.0 (the
0009: * "License"); you may not use this file except in compliance
0010: * with the License. You may obtain a copy of the License at
0011: *
0012: * http://www.apache.org/licenses/LICENSE-2.0
0013: *
0014: * Unless required by applicable law or agreed to in writing,
0015: * software distributed under the License is distributed on an
0016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0017: * KIND, either express or implied. See the License for the
0018: * specific language governing permissions and limitations
0019: * under the License.
0020: */
0021:
0022: import java.math.BigDecimal;
0023:
0024: import java.sql.Blob;
0025: import java.sql.PreparedStatement;
0026: import java.sql.ResultSet;
0027: import java.sql.SQLException;
0028: import java.sql.Time;
0029: import java.sql.Timestamp;
0030: import java.sql.Types;
0031:
0032: import java.util.Calendar;
0033:
0034: /**
0035: * A Value represents a single cell in a database table. In other words,
0036: * it is the cross between a row and column and contains the
0037: * information held there.
0038: *
0039: * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
0040: * @version $Revision: 568 $
0041: */
0042: public class Value {
0043: /** the object that is stored in this object */
0044: private Object valueObject;
0045:
0046: /** the column number that this object came from */
0047: private int columnNumber;
0048:
0049: /** what sql type of object is this? */
0050: private int type;
0051:
0052: /**
0053: * Creates a new Value object based on the ResultSet, columnNumber and
0054: * type
0055: *
0056: * @param rs
0057: * @param columnNumber
0058: * @param type
0059: *
0060: * @exception SQLException
0061: */
0062: public Value(ResultSet rs, int columnNumber, int type)
0063: throws SQLException {
0064: this .columnNumber = columnNumber;
0065: this .type = type;
0066: this .valueObject = null;
0067:
0068: if (rs == null) {
0069: return;
0070: }
0071:
0072: switch (type()) {
0073: case Types.BIT:
0074:
0075: String tmp = rs.getString(columnNumber);
0076:
0077: if (tmp == null) {
0078: valueObject = Boolean.FALSE;
0079: } else if (isTrue(tmp)) {
0080: valueObject = Boolean.TRUE;
0081: } else {
0082: valueObject = Boolean.FALSE;
0083: }
0084:
0085: break;
0086:
0087: case Types.TINYINT:
0088: valueObject = new Byte(rs.getByte(columnNumber));
0089:
0090: break;
0091:
0092: case Types.BIGINT:
0093: valueObject = new Long(rs.getLong(columnNumber));
0094:
0095: break;
0096:
0097: case Types.SMALLINT:
0098: valueObject = new Short(rs.getShort(columnNumber));
0099:
0100: break;
0101:
0102: case Types.INTEGER:
0103: valueObject = new Integer(rs.getInt(columnNumber));
0104:
0105: break;
0106:
0107: case Types.REAL:
0108: valueObject = new Float(rs.getFloat(columnNumber));
0109:
0110: break;
0111:
0112: case Types.FLOAT:
0113: case Types.DOUBLE:
0114: valueObject = new Double(rs.getDouble(columnNumber));
0115:
0116: break;
0117:
0118: case Types.NUMERIC:
0119: case Types.DECIMAL:
0120:
0121: String number = rs.getString(columnNumber);
0122:
0123: if (number == null) {
0124: valueObject = null;
0125: } else {
0126: valueObject = new BigDecimal(number);
0127: }
0128:
0129: break;
0130:
0131: case Types.LONGVARBINARY:
0132: case Types.VARBINARY:
0133: case Types.BINARY:
0134: valueObject = rs.getBytes(columnNumber);
0135:
0136: break;
0137:
0138: case Types.BLOB:
0139:
0140: Blob blob = rs.getBlob(columnNumber);
0141: valueObject = blob.getBytes(1, (int) blob.length());
0142:
0143: break;
0144:
0145: case Types.LONGVARCHAR:
0146: case Types.CHAR:
0147: case Types.VARCHAR:
0148: case Types.OTHER:
0149: valueObject = rs.getString(columnNumber);
0150:
0151: break;
0152:
0153: case Types.DATE:
0154: valueObject = rs.getDate(columnNumber);
0155:
0156: break;
0157:
0158: case Types.TIME:
0159: valueObject = rs.getTime(columnNumber);
0160:
0161: break;
0162:
0163: case Types.TIMESTAMP:
0164: valueObject = rs.getTimestamp(columnNumber);
0165:
0166: break;
0167:
0168: case Types.NULL:
0169: valueObject = null;
0170:
0171: break;
0172:
0173: default:
0174: valueObject = rs.getString(columnNumber);
0175:
0176: break;
0177: }
0178:
0179: if (rs.wasNull()) {
0180: valueObject = null;
0181: }
0182:
0183: return;
0184: }
0185:
0186: /**
0187: * Sets the value of this object
0188: *
0189: * @param value
0190: */
0191: void setValue(Object value) {
0192: this .valueObject = value;
0193: }
0194:
0195: /**
0196: * Gets the object from this Value
0197: *
0198: * @return the object from this Value
0199: */
0200: Object getValue() {
0201: return this .valueObject;
0202: }
0203:
0204: /**
0205: * This is used in Record in order to do a saveWithInsert/Update/Delete
0206: *
0207: * @param stmt
0208: * @param stmtNumber
0209: *
0210: * @exception DataSetException
0211: * @exception SQLException
0212: */
0213: void setPreparedStatementValue(PreparedStatement stmt,
0214: int stmtNumber) throws DataSetException, SQLException {
0215: if (isNull()) {
0216: stmt.setNull(stmtNumber, type());
0217:
0218: return;
0219: }
0220:
0221: switch (type()) {
0222: case Types.BIT:
0223: stmt.setBoolean(stmtNumber, this .asBoolean());
0224:
0225: break;
0226:
0227: case Types.TINYINT:
0228: stmt.setByte(stmtNumber, this .asByte());
0229:
0230: break;
0231:
0232: case Types.BIGINT:
0233: stmt.setLong(stmtNumber, this .asLong());
0234:
0235: break;
0236:
0237: case Types.SMALLINT:
0238: stmt.setShort(stmtNumber, this .asShort());
0239:
0240: break;
0241:
0242: case Types.INTEGER:
0243: stmt.setInt(stmtNumber, this .asInt());
0244:
0245: break;
0246:
0247: case Types.REAL:
0248: stmt.setFloat(stmtNumber, this .asFloat());
0249:
0250: break;
0251:
0252: case Types.FLOAT:
0253: case Types.DOUBLE:
0254: stmt.setDouble(stmtNumber, this .asDouble());
0255:
0256: break;
0257:
0258: case Types.NUMERIC:
0259: case Types.DECIMAL:
0260: stmt.setBigDecimal(stmtNumber, this .asBigDecimal());
0261:
0262: break;
0263:
0264: case Types.LONGVARBINARY:
0265: case Types.VARBINARY:
0266: case Types.BINARY:
0267: case Types.BLOB:
0268:
0269: // The following form is reported to work and be necessary for
0270: // Oracle when the blob exceeds 4k.
0271: byte[] value = this .asBytes();
0272: stmt.setBinaryStream(stmtNumber,
0273: new java.io.ByteArrayInputStream(value),
0274: value.length);
0275:
0276: break;
0277:
0278: case Types.LONGVARCHAR:
0279: case Types.CHAR:
0280: case Types.VARCHAR:
0281: case Types.OTHER:
0282: stmt.setString(stmtNumber, this .asString());
0283:
0284: break;
0285:
0286: case Types.DATE:
0287: stmt.setDate(stmtNumber, this .asDate());
0288:
0289: break;
0290:
0291: case Types.TIME:
0292: stmt.setTime(stmtNumber, this .asTime());
0293:
0294: break;
0295:
0296: case Types.TIMESTAMP:
0297: stmt.setTimestamp(stmtNumber, this .asTimestamp());
0298:
0299: break;
0300:
0301: case Types.NULL:
0302: stmt.setNull(stmtNumber, 0);
0303:
0304: break;
0305:
0306: default:
0307: stmt.setString(stmtNumber, this .asString());
0308:
0309: break;
0310: }
0311: }
0312:
0313: /**
0314: * Returns the string representation of this object
0315: *
0316: * @return a string
0317: */
0318: public String toString() {
0319: return this .asString();
0320: }
0321:
0322: /**
0323: * Returns the string representation of this object
0324: *
0325: * @return a string
0326: */
0327: public String asString() {
0328: if (isNull()) {
0329: return null;
0330: } else if (isString()) {
0331: return (String) valueObject;
0332: } else if (isBytes()) {
0333: return new String((byte[]) valueObject);
0334: } else {
0335: return valueObject.toString();
0336: }
0337: }
0338:
0339: /**
0340: * Get the value as a BigDecimal
0341: *
0342: * @return a BigDecimal
0343: *
0344: * @exception DataSetException
0345: */
0346: public BigDecimal asBigDecimal() throws DataSetException {
0347: try {
0348: if (isNull()) {
0349: return null;
0350: } else if (isBigDecimal()) {
0351: return (BigDecimal) valueObject;
0352: } else if (isDouble()) {
0353: return new BigDecimal(((Double) valueObject)
0354: .doubleValue());
0355: } else if (isFloat()) {
0356: return new BigDecimal(((Float) valueObject)
0357: .doubleValue());
0358: } else if (isString() || isInt() || isLong() || isShort()
0359: || isByte()) {
0360: return new BigDecimal(asString());
0361: } else {
0362: return null;
0363: }
0364: } catch (Exception e) {
0365: throw new DataSetException("Illegal conversion: "
0366: + e.toString());
0367: }
0368: }
0369:
0370: /**
0371: * Get the value as a BigDecimal
0372: *
0373: * @param scale TODO: DOCUMENT ME!
0374: *
0375: * @return a BigDecimal
0376: *
0377: * @exception DataSetException
0378: */
0379: public BigDecimal asBigDecimal(int scale) throws DataSetException {
0380: try {
0381: if (isNull()) {
0382: return null;
0383: } else if (isBigDecimal()) {
0384: return ((BigDecimal) valueObject).setScale(scale);
0385: } else if (isDouble()) {
0386: return new BigDecimal(((Double) valueObject)
0387: .doubleValue()).setScale(scale);
0388: } else if (isFloat()) {
0389: return new BigDecimal(((Float) valueObject)
0390: .doubleValue()).setScale(scale);
0391: } else if (isString() || isInt() || isLong() || isShort()
0392: || isByte()) {
0393: return new BigDecimal(asString()).setScale(scale);
0394: } else {
0395: return null;
0396: }
0397: } catch (Exception e) {
0398: throw new DataSetException("Bad conversion: "
0399: + e.toString());
0400: }
0401: }
0402:
0403: /**
0404: * Get the value as a asBoolean
0405: *
0406: * @return a boolean
0407: *
0408: * @exception DataSetException
0409: */
0410: public boolean asBoolean() throws DataSetException {
0411: try {
0412: if (isNull()) {
0413: return false;
0414: } else if (isBoolean()) {
0415: return ((Boolean) valueObject).booleanValue();
0416: }
0417:
0418: String check = asString();
0419:
0420: return (check == null) ? false : isTrue(check);
0421: } catch (Exception e) {
0422: throw new DataSetException("Bad conversion: "
0423: + e.toString());
0424: }
0425: }
0426:
0427: /**
0428: * Get the value as a Boolean object
0429: *
0430: * @return a Boolean
0431: *
0432: * @exception DataSetException
0433: */
0434: public Boolean asBooleanObj() throws DataSetException {
0435: try {
0436: if (isNull()) {
0437: return null;
0438: } else if (isBoolean()) {
0439: return (Boolean) valueObject;
0440: }
0441:
0442: String check = asString();
0443:
0444: if (check == null) {
0445: return null;
0446: } else if (isTrue(check)) {
0447: return Boolean.TRUE;
0448: } else {
0449: return Boolean.FALSE;
0450: }
0451: } catch (Exception e) {
0452: throw new DataSetException("Bad conversion: "
0453: + e.toString());
0454: }
0455: }
0456:
0457: /**
0458: * Get the value as a asInt
0459: *
0460: * @return an int
0461: *
0462: * @exception DataSetException
0463: */
0464: public int asInt() throws DataSetException {
0465: try {
0466: if (isNull()) {
0467: return 0;
0468: } else if (isInt()) {
0469: return ((Integer) valueObject).intValue();
0470: } else if (isString()) {
0471: return Integer.valueOf((String) valueObject).intValue();
0472: } else if (isLong()) {
0473: return ((Long) valueObject).intValue();
0474: } else if (isDouble()) {
0475: return ((Double) valueObject).intValue();
0476: } else if (isFloat()) {
0477: return ((Float) valueObject).intValue();
0478: } else if (isBigDecimal()) {
0479: return ((BigDecimal) valueObject).intValue();
0480: } else {
0481: return Integer.valueOf(asString()).intValue();
0482: }
0483: } catch (Exception e) {
0484: throw new DataSetException("Bad conversion: "
0485: + e.toString());
0486: }
0487: }
0488:
0489: /**
0490: * Get the value as a Integer Ojbect
0491: *
0492: * @return an Integer
0493: *
0494: * @exception DataSetException
0495: */
0496: public Integer asIntegerObj() throws DataSetException {
0497: try {
0498: if (isNull()) {
0499: return null;
0500: } else if (isInt()) {
0501: return ((Integer) valueObject);
0502: } else if (isString() || isDouble() || isFloat()
0503: || isBigDecimal() || isLong() || isShort()
0504: || isByte()) {
0505: return new Integer(asString());
0506: } else {
0507: throw new DataSetException("Invalid type for Integer");
0508: }
0509: } catch (Exception e) {
0510: throw new DataSetException("Illegal conversion: "
0511: + e.toString());
0512: }
0513: }
0514:
0515: /**
0516: * Get the value as a asByte
0517: *
0518: * @return a byte
0519: *
0520: * @exception DataSetException
0521: */
0522: public byte asByte() throws DataSetException {
0523: try {
0524: if (isNull()) {
0525: return 0;
0526: } else if (isByte()) {
0527: return ((Byte) valueObject).byteValue();
0528: } else if (isString()) {
0529: return Integer.valueOf((String) valueObject)
0530: .byteValue();
0531: } else if (isShort()) {
0532: return ((Short) valueObject).byteValue();
0533: } else if (isInt()) {
0534: return ((Integer) valueObject).byteValue();
0535: } else if (isLong()) {
0536: return ((Long) valueObject).byteValue();
0537: } else if (isDouble()) {
0538: return ((Double) valueObject).byteValue();
0539: } else if (isFloat()) {
0540: return ((Float) valueObject).byteValue();
0541: } else if (isBigDecimal()) {
0542: return ((BigDecimal) valueObject).byteValue();
0543: } else {
0544: return Integer.valueOf(asString()).byteValue();
0545: }
0546: } catch (Exception e) {
0547: throw new DataSetException("Bad conversion: "
0548: + e.toString());
0549: }
0550: }
0551:
0552: /**
0553: * Get the value as a Byte Object
0554: *
0555: * @return a Byte
0556: *
0557: * @exception DataSetException
0558: */
0559: public Byte asByteObj() throws DataSetException {
0560: try {
0561: if (isNull()) {
0562: return null;
0563: } else if (isByte()) {
0564: return ((Byte) valueObject);
0565: } else if (isString() || isDouble() || isFloat() || isInt()
0566: || isLong() || isShort() || isBigDecimal()) {
0567: return new Byte(asString());
0568: } else {
0569: throw new DataSetException("Invalid type for Byte");
0570: }
0571: } catch (Exception e) {
0572: throw new DataSetException("Illegal conversion: "
0573: + e.toString());
0574: }
0575: }
0576:
0577: /**
0578: * Get the value as a asBytes
0579: *
0580: * @return a byte array
0581: *
0582: * @exception DataSetException
0583: */
0584: public byte[] asBytes() throws DataSetException {
0585: try {
0586: if (isNull()) {
0587: return new byte[0];
0588: } else if (isBytes()) {
0589: return (byte[]) valueObject;
0590: } else if (isString()) {
0591: return ((String) valueObject).getBytes();
0592: }
0593: } catch (Exception e) {
0594: throw new DataSetException("Bad conversion: "
0595: + e.toString());
0596: }
0597:
0598: return new byte[0];
0599: }
0600:
0601: /**
0602: * Get the value as a asShort
0603: *
0604: * @return a short
0605: *
0606: * @exception DataSetException
0607: */
0608: public short asShort() throws DataSetException {
0609: try {
0610: if (isNull()) {
0611: return 0;
0612: } else if (isShort()) {
0613: return ((Short) valueObject).shortValue();
0614: } else if (isString()) {
0615: return Integer.valueOf((String) valueObject)
0616: .shortValue();
0617: } else if (isInt()) {
0618: return ((Integer) valueObject).shortValue();
0619: } else if (isLong()) {
0620: return ((Long) valueObject).shortValue();
0621: } else if (isDouble()) {
0622: return ((Double) valueObject).shortValue();
0623: } else if (isFloat()) {
0624: return ((Float) valueObject).shortValue();
0625: } else if (isBigDecimal()) {
0626: return ((BigDecimal) valueObject).shortValue();
0627: } else {
0628: return Integer.valueOf(asString()).shortValue();
0629: }
0630: } catch (Exception e) {
0631: throw new DataSetException("Bad conversion: "
0632: + e.toString());
0633: }
0634: }
0635:
0636: /**
0637: * Get the value as a Short Object
0638: *
0639: * @return a Short
0640: *
0641: * @exception DataSetException
0642: */
0643: public Short asShortObj() throws DataSetException {
0644: try {
0645: if (isNull()) {
0646: return null;
0647: } else if (isShort()) {
0648: return ((Short) valueObject);
0649: } else if (isString() || isDouble() || isFloat() || isInt()
0650: || isLong() || isBigDecimal() || isByte()) {
0651: return new Short(asString());
0652: } else {
0653: throw new DataSetException("Invalid type for Short");
0654: }
0655: } catch (Exception e) {
0656: throw new DataSetException("Illegal conversion: "
0657: + e.toString());
0658: }
0659: }
0660:
0661: /**
0662: * Get the value as a asLong
0663: *
0664: * @return a long
0665: *
0666: * @exception DataSetException
0667: */
0668: public long asLong() throws DataSetException {
0669: try {
0670: if (isNull()) {
0671: return 0;
0672: } else if (isLong()) {
0673: return ((Long) valueObject).longValue();
0674: } else if (isString()) {
0675: return Integer.valueOf((String) valueObject)
0676: .longValue();
0677: } else if (isShort()) {
0678: return ((Short) valueObject).longValue();
0679: } else if (isInt()) {
0680: return ((Integer) valueObject).longValue();
0681: } else if (isDouble()) {
0682: return ((Double) valueObject).longValue();
0683: } else if (isFloat()) {
0684: return ((Float) valueObject).longValue();
0685: } else if (isBigDecimal()) {
0686: return ((BigDecimal) valueObject).longValue();
0687: } else {
0688: return Integer.valueOf(asString()).longValue();
0689: }
0690: } catch (Exception e) {
0691: throw new DataSetException("Bad conversion: "
0692: + e.toString());
0693: }
0694: }
0695:
0696: /**
0697: * Get the value as a Long Object
0698: *
0699: * @return a Long
0700: *
0701: * @exception DataSetException
0702: */
0703: public Long asLongObj() throws DataSetException {
0704: try {
0705: if (isNull()) {
0706: return null;
0707: } else if (isLong()) {
0708: return ((Long) valueObject);
0709: } else if (isString() || isDouble() || isFloat() || isInt()
0710: || isBigDecimal() || isShort() || isByte()) {
0711: return new Long(asString());
0712: } else {
0713: throw new DataSetException("Invalid type for Long");
0714: }
0715: } catch (Exception e) {
0716: throw new DataSetException("Illegal conversion: "
0717: + e.toString());
0718: }
0719: }
0720:
0721: /**
0722: * Get the value as a asDouble
0723: *
0724: * @return a double
0725: *
0726: * @exception DataSetException
0727: */
0728: public double asDouble() throws DataSetException {
0729: try {
0730: if (isNull()) {
0731: return 0.0D;
0732: } else if (isDouble()) {
0733: return ((Double) valueObject).doubleValue();
0734: } else if (isString()) {
0735: return Integer.valueOf((String) valueObject)
0736: .doubleValue();
0737: } else if (isShort()) {
0738: return ((Short) valueObject).doubleValue();
0739: } else if (isInt()) {
0740: return ((Integer) valueObject).doubleValue();
0741: } else if (isLong()) {
0742: return ((Long) valueObject).doubleValue();
0743: } else if (isFloat()) {
0744: return ((Float) valueObject).doubleValue();
0745: } else if (isBigDecimal()) {
0746: return ((BigDecimal) valueObject).doubleValue();
0747: } else {
0748: return Integer.valueOf(asString()).doubleValue();
0749: }
0750: } catch (Exception e) {
0751: throw new DataSetException("Bad conversion: "
0752: + e.toString());
0753: }
0754: }
0755:
0756: /**
0757: * Get the value as a Double Object
0758: *
0759: * @return a Double
0760: *
0761: * @exception DataSetException
0762: */
0763: public Double asDoubleObj() throws DataSetException {
0764: try {
0765: if (isNull()) {
0766: return null;
0767: } else if (isDouble()) {
0768: return ((Double) valueObject);
0769: } else if (isString() || isBigDecimal() || isFloat()
0770: || isInt() || isLong() || isShort() || isByte()) {
0771: return new Double(asString());
0772: } else {
0773: throw new DataSetException("Invalid type for Double");
0774: }
0775: } catch (Exception e) {
0776: throw new DataSetException("Illegal conversion: "
0777: + e.toString());
0778: }
0779: }
0780:
0781: /**
0782: * Get the value as a asFloat
0783: *
0784: * @return a float
0785: *
0786: * @exception DataSetException
0787: */
0788: public float asFloat() throws DataSetException {
0789: try {
0790: if (isNull()) {
0791: return 0.0F;
0792: } else if (isFloat()) {
0793: return ((Float) valueObject).floatValue();
0794: } else if (isString()) {
0795: return Integer.valueOf((String) valueObject)
0796: .floatValue();
0797: } else if (isShort()) {
0798: return ((Short) valueObject).floatValue();
0799: } else if (isInt()) {
0800: return ((Integer) valueObject).floatValue();
0801: } else if (isLong()) {
0802: return ((Long) valueObject).floatValue();
0803: } else if (isDouble()) {
0804: return ((Double) valueObject).floatValue();
0805: } else if (isBigDecimal()) {
0806: return ((BigDecimal) valueObject).floatValue();
0807: } else {
0808: return Integer.valueOf(asString()).floatValue();
0809: }
0810: } catch (Exception e) {
0811: throw new DataSetException("Bad conversion: "
0812: + e.toString());
0813: }
0814: }
0815:
0816: /**
0817: * Get the value as a Float Obj
0818: *
0819: * @return a Float
0820: *
0821: * @exception DataSetException
0822: */
0823: public Float asFloatObj() throws DataSetException {
0824: try {
0825: if (isNull()) {
0826: return null;
0827: } else if (isFloat()) {
0828: return ((Float) valueObject);
0829: } else if (isString() || isDouble() || isBigDecimal()
0830: || isInt() || isLong() || isShort() || isByte()) {
0831: return new Float(asString());
0832: } else {
0833: throw new DataSetException("Invalid type for Float");
0834: }
0835: } catch (Exception e) {
0836: throw new DataSetException("Illegal conversion: "
0837: + e.toString());
0838: }
0839: }
0840:
0841: /**
0842: * Get the value as a asTime
0843: *
0844: * @return a Time
0845: *
0846: * @exception DataSetException
0847: */
0848: public Time asTime() throws DataSetException {
0849: try {
0850: if (isNull()) {
0851: return null;
0852: } else if (isTime()) {
0853: return (Time) valueObject;
0854: }
0855:
0856: Calendar cal = Calendar.getInstance();
0857:
0858: if (isTimestamp()) {
0859: cal.setTime((Timestamp) valueObject);
0860:
0861: return new Time(cal.getTime().getTime());
0862: } else if (isUtilDate()) {
0863: cal.setTime((java.util.Date) valueObject);
0864:
0865: return new Time(cal.getTime().getTime());
0866: } else if (isString()) {
0867: return Time.valueOf((String) valueObject);
0868: } else {
0869: return Time.valueOf(asString());
0870: }
0871: } catch (IllegalArgumentException a) {
0872: throw new DataSetException("Bad date value - "
0873: + "Java Time Objects cannot be earlier than 1/1/70");
0874: } catch (Exception b) {
0875: throw new DataSetException("Bad conversion: "
0876: + b.toString());
0877: }
0878: }
0879:
0880: /**
0881: * Get the value as a asTimestamp
0882: *
0883: * @return a Timestamp
0884: *
0885: * @exception DataSetException
0886: */
0887: public Timestamp asTimestamp() throws DataSetException {
0888: try {
0889: if (isNull()) {
0890: return null;
0891: } else if (isTimestamp()) {
0892: return (Timestamp) valueObject;
0893: }
0894:
0895: if (isTime()) {
0896: Calendar cal = Calendar.getInstance();
0897: cal.setTime((Time) valueObject);
0898:
0899: return new Timestamp(cal.getTime().getTime());
0900: } else if (isUtilDate()) {
0901: return new Timestamp(((java.util.Date) valueObject)
0902: .getTime());
0903: } else if (isString()) {
0904: return Timestamp.valueOf((String) valueObject);
0905: } else {
0906: return Timestamp.valueOf(asString());
0907: }
0908: } catch (IllegalArgumentException a) {
0909: throw new DataSetException(
0910: "Bad date value - "
0911: + "Java Timestamp Objects cannot be earlier than 1/1/70");
0912: } catch (Exception b) {
0913: throw new DataSetException("Bad conversion: "
0914: + b.toString());
0915: }
0916: }
0917:
0918: /**
0919: * Get the value as a asDate
0920: *
0921: * @return a java.sql.Date
0922: *
0923: * @exception DataSetException
0924: */
0925: public java.sql.Date asDate() throws DataSetException {
0926: try {
0927: if (isNull()) {
0928: return null;
0929: } else if (isDate()) {
0930: return (java.sql.Date) valueObject;
0931: }
0932:
0933: Calendar cal = Calendar.getInstance();
0934:
0935: if (isTimestamp()) {
0936: Timestamp ts = (Timestamp) valueObject;
0937: long date = ts.getTime();
0938: int nanos = ts.getNanos();
0939:
0940: return new java.sql.Date(date + (nanos / 1000000));
0941: } else if (isTime()) {
0942: cal.setTime((Time) valueObject);
0943:
0944: return java.sql.Date.valueOf(cal.get(Calendar.YEAR)
0945: + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
0946: + cal.get(Calendar.DAY_OF_MONTH));
0947: } else if (isUtilDate()) {
0948: cal.setTime((java.util.Date) valueObject);
0949:
0950: return java.sql.Date.valueOf(cal.get(Calendar.YEAR)
0951: + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
0952: + cal.get(Calendar.DAY_OF_MONTH));
0953: } else if (isString()) {
0954: return java.sql.Date.valueOf((String) valueObject);
0955: } else {
0956: return java.sql.Date.valueOf(asString());
0957: }
0958: } catch (IllegalArgumentException a) {
0959: throw new DataSetException(
0960: "Bad date value - "
0961: + "Java Timestamp Objects cannot be earlier than 1/1/70");
0962: } catch (Exception b) {
0963: throw new DataSetException("Bad conversion: "
0964: + b.toString());
0965: }
0966: }
0967:
0968: /**
0969: * Get the value as a asUtilDate
0970: *
0971: * @return a java.util.Date
0972: *
0973: * @exception DataSetException
0974: */
0975: public java.util.Date asUtilDate() throws DataSetException {
0976: try {
0977: if (isNull()) {
0978: return null;
0979: } else if (isUtilDate()) {
0980: return (java.util.Date) valueObject;
0981: }
0982:
0983: Calendar cal = Calendar.getInstance();
0984:
0985: if (isTimestamp()) {
0986: Timestamp ts = (Timestamp) valueObject;
0987: long date = ts.getTime();
0988: int nanos = ts.getNanos();
0989:
0990: return new java.util.Date(date + (nanos / 1000000));
0991: } else if (isTime()) {
0992: cal.setTime((Time) valueObject);
0993:
0994: return java.sql.Date.valueOf(cal.get(Calendar.YEAR)
0995: + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
0996: + cal.get(Calendar.DAY_OF_MONTH));
0997: } else if (isUtilDate()) {
0998: cal.setTime((java.util.Date) valueObject);
0999:
1000: return java.sql.Date.valueOf(cal.get(Calendar.YEAR)
1001: + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
1002: + cal.get(Calendar.DAY_OF_MONTH));
1003: } else {
1004: return null;
1005: }
1006: } catch (IllegalArgumentException a) {
1007: throw new DataSetException(
1008: "Bad date value - "
1009: + "Java java.util.Date Objects cannot be earlier than 1/1/70");
1010: } catch (Exception b) {
1011: throw new DataSetException("Bad conversion: "
1012: + b.toString());
1013: }
1014: }
1015:
1016: /**
1017: * Is the value a isBigDecimal
1018: *
1019: * @return true if BigDecimal
1020: */
1021: public boolean isBigDecimal() {
1022: return valueObject instanceof BigDecimal;
1023: }
1024:
1025: /**
1026: * Is the value a isByte
1027: *
1028: * @return true if is Byte
1029: */
1030: public boolean isByte() {
1031: return valueObject instanceof Byte;
1032: }
1033:
1034: /**
1035: * Is the value a isBytes
1036: *
1037: * @return true if is byte[]
1038: */
1039: public boolean isBytes() {
1040: return valueObject instanceof byte[];
1041: }
1042:
1043: /**
1044: * Is the value a isDate
1045: *
1046: * @return true if is java.sql.Date
1047: */
1048: public boolean isDate() {
1049: return valueObject instanceof java.sql.Date;
1050: }
1051:
1052: /**
1053: * Is the value a isShort
1054: *
1055: * @return true if is Short
1056: */
1057: public boolean isShort() {
1058: return valueObject instanceof Short;
1059: }
1060:
1061: /**
1062: * Is the value a isInt
1063: *
1064: * @return true if is Integer
1065: */
1066: public boolean isInt() {
1067: return valueObject instanceof Integer;
1068: }
1069:
1070: /**
1071: * Is the value a isLong
1072: *
1073: * @return true if is Long
1074: */
1075: public boolean isLong() {
1076: return valueObject instanceof Long;
1077: }
1078:
1079: /**
1080: * Is the value a isDouble
1081: *
1082: * @return true if is Double
1083: */
1084: public boolean isDouble() {
1085: return valueObject instanceof Double;
1086: }
1087:
1088: /**
1089: * Is the value a isFloat
1090: *
1091: * @return true if is Float
1092: */
1093: public boolean isFloat() {
1094: return valueObject instanceof Float;
1095: }
1096:
1097: /**
1098: * Is the value a isBoolean
1099: *
1100: * @return true if is Boolean
1101: */
1102: public boolean isBoolean() {
1103: return valueObject instanceof Boolean;
1104: }
1105:
1106: /**
1107: * Is the value a isNull
1108: *
1109: * @return true if is null
1110: */
1111: public boolean isNull() {
1112: return valueObject == null;
1113: }
1114:
1115: /**
1116: * Is the value a isString
1117: *
1118: * @return true if is String
1119: */
1120: public boolean isString() {
1121: return valueObject instanceof String;
1122: }
1123:
1124: /**
1125: * Is the value a isTime
1126: *
1127: * @return true if is java.sql.Time
1128: */
1129: public boolean isTime() {
1130: return valueObject instanceof java.sql.Time;
1131: }
1132:
1133: /**
1134: * Is the value a isTimestamp
1135: *
1136: * @return true if is java.sql.Timestamp
1137: */
1138: public boolean isTimestamp() {
1139: return valueObject instanceof java.sql.Timestamp;
1140: }
1141:
1142: /**
1143: * Is the value a isUtilDate
1144: *
1145: * @return true if is java.util.Date
1146: */
1147: public boolean isUtilDate() {
1148: return valueObject instanceof java.util.Date;
1149: }
1150:
1151: /**
1152: * Return the type of this value
1153: *
1154: * @return the type of this value
1155: */
1156: public int type() {
1157: return this .type;
1158: }
1159:
1160: /**
1161: * Gets the columnNumber which this value represents.
1162: *
1163: * @return an int
1164: */
1165: int columnNumber() {
1166: return this .columnNumber;
1167: }
1168:
1169: /**
1170: * DOCUMENT ME!
1171: *
1172: * @param value TODO: DOCUMENT ME!
1173: *
1174: * @return true if (true || t | yes | y | 1)
1175: */
1176: private boolean isTrue(String value) {
1177: return (value.equalsIgnoreCase("true")
1178: || value.equalsIgnoreCase("t")
1179: || value.equalsIgnoreCase("yes")
1180: || value.equalsIgnoreCase("y") || value.equals("1"));
1181: }
1182: }
|