www.pudn.com > town-1[1].0.4.rar > Value.java
package com.workingdogs.town;
import java.sql.*;
import java.math.*;
import java.util.Calendar;
/*
Town, a Java JDBC abstraction layer
Copyright (C) 1999 Serge Knystautas, Jon S. Stevens
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
/**
A Value represents a single cell in a database table. In other words, it is the
cross between a row and column and contains the information held there.
@author Jon S. Stevens jon@working-dogs.com
@author Serge Knystautas sergek@lokitech.com
@version 1.0
*/
public class Value
{
/** the object that is stored in this object */
private Object valueObject;
/** the column number that this object came from */
private int columnNumber;
/** what sql type of object is this? */
private int type;
private Value ()
{
}
/**
* Creates a new Value object based on the ResultSet, columnNumber and type
*
* @param rs
* @param columnNumber
* @param type
* @exception ConnectionException
*/
public Value (ResultSet rs, int columnNumber,
int type) throws ConnectionException
{
this.columnNumber = columnNumber;
this.type = type;
this.valueObject = null;
try
{
if (rs == null)
return;
switch (type())
{
case Types.BIT:
String tmp = rs.getString (columnNumber);
if (tmp == null)
valueObject = new Boolean (false);
else if (tmp.equalsIgnoreCase ("true") ||
tmp.equalsIgnoreCase ("yes") ||
tmp.equals ("1"))
valueObject = new Boolean (true);
else
valueObject = new Boolean (false);
break;
case Types.TINYINT:
valueObject = new Byte (rs.getByte (columnNumber));
break;
case Types.BIGINT:
valueObject = new Long (rs.getLong (columnNumber));
break;
case Types.SMALLINT:
valueObject = new Short (rs.getShort (columnNumber));
break;
case Types.INTEGER:
valueObject = new Integer (rs.getInt (columnNumber));
break;
case Types.REAL:
valueObject = new Float (rs.getFloat (columnNumber));
break;
case Types.FLOAT:
case Types.DOUBLE:
valueObject = new Double (rs.getDouble (columnNumber));
break;
case Types.NUMERIC:
case Types.DECIMAL:
valueObject =
new BigDecimal (rs.getDouble (columnNumber));
break;
case Types.LONGVARBINARY:
case Types.VARBINARY:
case Types.BINARY:
valueObject = rs.getBytes (columnNumber);
break;
case Types.LONGVARCHAR:
case Types.CHAR:
case Types.VARCHAR:
case Types.OTHER:
valueObject = rs.getString (columnNumber);
break;
case Types.DATE:
valueObject = rs.getDate (columnNumber);
break;
case Types.TIME:
valueObject = rs.getTime (columnNumber);
break;
case Types.TIMESTAMP:
valueObject = rs.getTimestamp (columnNumber);
break;
case Types.NULL:
valueObject = null;
break;
default:
valueObject = rs.getString (columnNumber);
break;
}
}
catch (SQLException sqle)
{
throw new ConnectionException (sqle);
}
return;
}
/**
* Get the value as a BigDecimal
*
* @return a BigDecimal
* @exception DataSetException
*/
public BigDecimal asBigDecimal() throws DataSetException
{
try
{
if (isNull())
return null;
else if (isBigDecimal())
return (BigDecimal) valueObject;
else if (isString() || isDouble() || isFloat() || isInt() ||
isLong())
return new BigDecimal (asString());
else
return null;
}
catch (Exception e)
{
throw new DataSetException("Illegal conversion: " +
e.toString());
}
}
/**
* Get the value as a BigDecimal
*
* @return a BigDecimal
* @exception DataSetException
*/
public BigDecimal asBigDecimal(int scale) throws DataSetException
{
try
{
if (isNull())
return null;
else if (isBigDecimal())
return ((BigDecimal) valueObject).setScale (scale);
else if (isString() || isDouble() || isFloat() || isInt() ||
isLong())
return new BigDecimal (asString()).setScale (scale);
else
return null;
}
catch (Exception e)
{
throw new DataSetException("Bad conversion: " + e.toString());
}
}
/**
* Get the value as a asBoolean
*
* @return a boolean
* @exception DataSetException
*/
public boolean asBoolean() throws DataSetException
{
try
{
if (isNull())
return false;
else if (isBoolean())
return ((Boolean) valueObject).booleanValue();
String check = asString();
if (check == null)
return false;
else if (check.equalsIgnoreCase ("yes") ||
check.equalsIgnoreCase ("true") || check.equals ("1"))
return true;
else
return false;
}
catch (Exception e)
{
throw new DataSetException("Bad conversion: " + e.toString());
}
}
/**
* Get the value as a asByte
*
* @return a byte
* @exception DataSetException
*/
public byte asByte() throws DataSetException
{
try
{
if (isNull())
return 0;
else if (isByte())
return ((Byte) valueObject).byteValue();
else if (isString())
return Integer.valueOf((String) valueObject).byteValue();
else if (isShort())
return ((Short) valueObject).byteValue();
else if (isInt())
return ((Integer) valueObject).byteValue();
else if (isLong())
return ((Long) valueObject).byteValue();
else if (isDouble())
return ((Double) valueObject).byteValue();
else if (isFloat())
return ((Float) valueObject).byteValue();
else if (isBigDecimal())
return ((BigDecimal) valueObject).byteValue();
else
return Integer.valueOf(asString()).byteValue();
}
catch (Exception e)
{
throw new DataSetException("Bad conversion: " + e.toString());
}
}
/**
* Get the value as a asBytes
*
* @return a byte array
* @exception DataSetException
*/
public byte[] asBytes() throws DataSetException
{
try
{
if (isNull())
return null;
else if (isBytes())
return (byte[]) valueObject;
else if (isString())
return ((String) valueObject).getBytes();
}
catch (Exception e)
{
throw new DataSetException("Bad conversion: " + e.toString());
}
return null;
}
/**
* Get the value as a asDate
*
* @return a java.sql.Date
* @exception DataSetException
*/
public java.sql.Date asDate() throws DataSetException
{
try
{
if (isNull())
return null;
else if (isDate())
return (java.sql.Date) valueObject;
Calendar cal = Calendar.getInstance();
if (isTimestamp())
{
cal.setTime ((Timestamp) valueObject);
return java.sql.Date.valueOf (cal.get(Calendar.YEAR) +
"-" + (cal.get(Calendar.MONTH) + 1) + "-" +
cal.get(Calendar.DAY_OF_MONTH));
}
else if (isTime())
{
cal.setTime ((Time) valueObject);
return java.sql.Date.valueOf (cal.get(Calendar.YEAR) +
"-" + (cal.get(Calendar.MONTH) + 1) + "-" +
cal.get(Calendar.DAY_OF_MONTH));
}
else if (isUtilDate())
{
cal.setTime ((java.util.Date) valueObject);
return java.sql.Date.valueOf (cal.get(Calendar.YEAR) +
"-" + (cal.get(Calendar.MONTH) + 1) + "-" +
cal.get(Calendar.DAY_OF_MONTH));
}
else if (isString())
return java.sql.Date.valueOf((String) valueObject);
else
return java.sql.Date.valueOf(asString());
}
catch (IllegalArgumentException a)
{
throw new DataSetException("Bad date value - Java Timestamp Objects cannot be earlier than 1/1/70");
}
catch (Exception b)
{
throw new DataSetException("Bad conversion: " + b.toString());
}
}
/**
* Get the value as a asDouble
*
* @return a double
* @exception DataSetException
*/
public double asDouble() throws DataSetException
{
try
{
if (isNull())
return 0.0D;
else if (isDouble())
return ((Double) valueObject).doubleValue();
else if (isString())
return Integer.valueOf((String) valueObject).doubleValue();
else if (isShort())
return ((Short) valueObject).doubleValue();
else if (isInt())
return ((Integer) valueObject).doubleValue();
else if (isLong())
return ((Long) valueObject).doubleValue();
else if (isFloat())
return ((Float) valueObject).doubleValue();
else if (isBigDecimal())
return ((BigDecimal) valueObject).doubleValue();
else
return Integer.valueOf(asString()).doubleValue();
}
catch (Exception e)
{
throw new DataSetException("Bad conversion: " + e.toString());
}
}
/**
* Get the value as a asFloat
*
* @return a float
* @exception DataSetException
*/
public float asFloat() throws DataSetException
{
try
{
if (isNull())
return 0.0F;
else if (isFloat())
return ((Float) valueObject).floatValue();
else if (isString())
return Integer.valueOf((String) valueObject).floatValue();
else if (isShort())
return ((Short) valueObject).floatValue();
else if (isInt())
return ((Integer) valueObject).floatValue();
else if (isLong())
return ((Long) valueObject).floatValue();
else if (isDouble())
return ((Double) valueObject).floatValue();
else if (isBigDecimal())
return ((BigDecimal) valueObject).floatValue();
else
return Integer.valueOf(asString()).floatValue();
}
catch (Exception e)
{
throw new DataSetException("Bad conversion: " + e.toString());
}
}
/**
* Get the value as a asInt
*
* @return an int
* @exception DataSetException
*/
public int asInt() throws DataSetException
{
try
{
if (isNull())
return 0;
else if (isInt())
return ((Integer) valueObject).intValue();
else if (isString())
return Integer.valueOf((String) valueObject).intValue();
else if (isLong())
return ((Long) valueObject).intValue();
else if (isDouble())
return ((Double) valueObject).intValue();
else if (isFloat())
return ((Float) valueObject).intValue();
else if (isBigDecimal())
return ((BigDecimal) valueObject).intValue();
else
return Integer.valueOf(asString()).intValue();
}
catch (Exception e)
{
throw new DataSetException("Bad conversion: " + e.toString());
}
}
/**
* Get the value as a asLong
*
* @return a long
* @exception DataSetException
*/
public long asLong() throws DataSetException
{
try
{
if (isNull())
return 0;
else if (isLong())
return ((Long) valueObject).longValue();
else if (isString())
return Integer.valueOf((String) valueObject).longValue();
else if (isShort())
return ((Short) valueObject).longValue();
else if (isInt())
return ((Integer) valueObject).longValue();
else if (isDouble())
return ((Double) valueObject).longValue();
else if (isFloat())
return ((Float) valueObject).longValue();
else if (isBigDecimal())
return ((BigDecimal) valueObject).longValue();
else
return Integer.valueOf(asString()).longValue();
}
catch (Exception e)
{
throw new DataSetException("Bad conversion: " + e.toString());
}
}
/**
* Get the value as a asShort
*
* @return a short
* @exception DataSetException
*/
public short asShort() throws DataSetException
{
try
{
if (isNull())
return 0;
else if (isShort())
return ((Short) valueObject).shortValue();
else if (isString())
return Integer.valueOf((String) valueObject).shortValue();
else if (isInt())
return ((Integer) valueObject).shortValue();
else if (isLong())
return ((Long) valueObject).shortValue();
else if (isDouble())
return ((Double) valueObject).shortValue();
else if (isFloat())
return ((Float) valueObject).shortValue();
else if (isBigDecimal())
return ((BigDecimal) valueObject).shortValue();
else
return Integer.valueOf(asString()).shortValue();
}
catch (Exception e)
{
throw new DataSetException("Bad conversion: " + e.toString());
}
}
/**
* Returns the string representation of this object
*
* @return a string
*/
public String asString()
{
if (isNull())
return null;
else if (isString())
return (String) valueObject;
else if (isBytes ())
return new String ((byte[])valueObject);
else
return valueObject.toString();
}
/**
* Get the value as a asTime
*
* @return a Time
* @exception DataSetException
*/
public Time asTime() throws DataSetException
{
try
{
if (isNull())
return null;
else if (isTime())
return (Time) valueObject;
Calendar cal = Calendar.getInstance();
if (isTimestamp())
{
cal.setTime ((Timestamp) valueObject);
return new Time(cal.get(Calendar.HOUR),
cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
}
else if (isUtilDate())
{
cal.setTime ((java.util.Date) valueObject);
return new Time(cal.get(Calendar.HOUR),
cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
}
else if (isString())
return Time.valueOf((String) valueObject);
else
return Time.valueOf(asString());
}
catch (IllegalArgumentException a)
{
throw new DataSetException("Bad date value - Java Time Objects cannot be earlier than 1/1/70");
}
catch (Exception b)
{
throw new DataSetException("Bad conversion: " + b.toString());
}
}
/**
* Get the value as a asTimestamp
*
* @return a Timestamp
* @exception DataSetException
*/
public Timestamp asTimestamp() throws DataSetException
{
try
{
if (isNull())
return null;
else if (isTimestamp())
return (Timestamp) valueObject;
Calendar cal = Calendar.getInstance();
if (isTime())
{
cal.setTime ((Time) valueObject);
return new Timestamp(cal.getTime().getTime());
}
else if (isUtilDate())
{
cal.setTime ((java.util.Date) valueObject);
return new Timestamp(cal.getTime().getTime());
}
else if (isString())
return Timestamp.valueOf((String) valueObject);
else
return Timestamp.valueOf(asString());
}
catch (IllegalArgumentException a)
{
throw new DataSetException("Bad date value - Java Timestamp Objects cannot be earlier than 1/1/70");
}
catch (Exception b)
{
throw new DataSetException("Bad conversion: " + b.toString());
}
}
/**
* Get the value as a asUtilDate
*
* @return a java.util.Date
* @exception DataSetException
*/
public java.util.Date asUtilDate() throws DataSetException
{
try
{
if (isNull())
return null;
else if (isUtilDate ())
return (java.util.Date) valueObject;
else
return null;
}
catch (IllegalArgumentException a)
{
throw new DataSetException("Bad date value - Java java.util.Date Objects cannot be earlier than 1/1/70");
}
catch (Exception b)
{
throw new DataSetException("Bad conversion: " + b.toString());
}
}
/**
* Gets the columnNumber which this value represents.
*
* @return an int
*/
int columnNumber()
{
return this.columnNumber;
}
/**
* Gets the object from this Value
*
* @return the object from this Value
*/
Object getValue ()
{
return this.valueObject;
}
/**
* Is the value a isBigDecimal
*
* @return true if BigDecimal
*/
public boolean isBigDecimal()
{
return valueObject instanceof BigDecimal;
}
/**
* Is the value a isBoolean
*
* @return true if is Boolean
*/
public boolean isBoolean()
{
return valueObject instanceof Boolean;
}
/**
* Is the value a isByte
*
* @return true if is Byte
*/
public boolean isByte()
{
return valueObject instanceof Byte;
}
/**
* Is the value a isBytes
*
* @return true if is byte[]
*/
public boolean isBytes()
{
return valueObject instanceof byte[];
}
/**
* Is the value a isDate
*
* @return true if is java.sql.Date
*/
public boolean isDate()
{
return valueObject instanceof java.sql.Date;
}
/**
* Is the value a isDouble
*
* @return true if is Double
*/
public boolean isDouble()
{
return valueObject instanceof Double;
}
/**
* Is the value a isFloat
*
* @return true if is Float
*/
public boolean isFloat()
{
return valueObject instanceof Float;
}
/**
* Is the value a isInt
*
* @return true if is Integer
*/
public boolean isInt()
{
return valueObject instanceof Integer;
}
/**
* Is the value a isLong
*
* @return true if is Long
*/
public boolean isLong()
{
return valueObject instanceof Long;
}
/**
* Is the value a isNull
*
* @return true if is null
*/
public boolean isNull()
{
return valueObject == null;
}
/**
* Is the value a isShort
*
* @return true if is Short
*/
public boolean isShort()
{
return valueObject instanceof Short;
}
/**
* Is the value a isString
*
* @return true if is String
*/
public boolean isString()
{
return valueObject instanceof String;
}
/**
* Is the value a isTime
*
* @return true if is java.sql.Time
*/
public boolean isTime()
{
return valueObject instanceof java.sql.Time;
}
/**
* Is the value a isTimestamp
*
* @return true if is java.sql.Timestamp
*/
public boolean isTimestamp()
{
return valueObject instanceof java.sql.Timestamp;
}
/**
* Is the value a isUtilDate
*
* @return true if is java.util.Date
*/
public boolean isUtilDate()
{
return valueObject instanceof java.util.Date;
}
/**
* This is used in Record in order to do a saveWithInsert/Update/Delete
*
* @param stmt
* @param stmtNumber
* @exception DataSetException
* @exception ConnectionException
*/
void setPreparedStatementValue (PreparedStatement stmt,
int stmtNumber) throws DataSetException, ConnectionException
{
try
{
if (isNull ())
{
stmt.setNull (stmtNumber, type ());
return;
}
switch (type())
{
case Types.BIT:
stmt.setBoolean (stmtNumber, this.asBoolean());
break;
case Types.TINYINT:
stmt.setByte (stmtNumber, this.asByte());
break;
case Types.BIGINT:
stmt.setLong (stmtNumber, this.asLong());
break;
case Types.SMALLINT:
stmt.setShort (stmtNumber, this.asShort());
break;
case Types.INTEGER:
stmt.setInt (stmtNumber, this.asInt());
break;
case Types.REAL:
stmt.setFloat (stmtNumber, this.asFloat());
break;
case Types.FLOAT:
case Types.DOUBLE:
stmt.setDouble (stmtNumber, this.asDouble());
break;
case Types.NUMERIC:
case Types.DECIMAL:
stmt.setBigDecimal (stmtNumber, this.asBigDecimal());
break;
case Types.LONGVARBINARY:
case Types.VARBINARY:
case Types.BINARY:
stmt.setBytes (stmtNumber, this.asBytes());
break;
case Types.LONGVARCHAR:
case Types.CHAR:
case Types.VARCHAR:
case Types.OTHER:
stmt.setString (stmtNumber, this.asString());
break;
case Types.DATE:
stmt.setDate (stmtNumber, this.asDate());
break;
case Types.TIME:
stmt.setTime (stmtNumber, this.asTime());
break;
case Types.TIMESTAMP:
stmt.setTimestamp (stmtNumber, this.asTimestamp());
break;
case Types.NULL:
stmt.setNull (stmtNumber, 0);
break;
default:
stmt.setString (stmtNumber, this.asString());
break;
}
}
catch (SQLException sqle)
{
throw new ConnectionException (sqle);
}
}
/**
* Sets the value of this object
*
* @param value
*/
void setValue (Object value)
{
this.valueObject = value;
}
/**
* Returns the string representation of this object
*
* @return a string
*/
public String toString()
{
return this.asString();
}
/**
* Return the type of this value
*
* @return the type of this value
*/
public int type()
{
return this.type;
}
/**
* Clones the object
*
* @return a clone of this object
*/
public Object clone ()
{
Value newVal = new Value ();
newVal.valueObject = this.valueObject;
newVal.columnNumber = this.columnNumber;
newVal.type = this.type;
return newVal;
}
/**
* Compares whether the two values are identical
*
* @return boolean
*/
public boolean equals (Object obj)
{
if (obj == null)
return false;
if (obj instanceof Value)
{
Value val = (Value)obj;
return this.valueObject.equals (val.valueObject);
}
return this.valueObject.equals (obj);
}
}