⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlboolean.java

📁 derby database source code.good for you.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*   Derby - Class org.apache.derby.iapi.types.SQLBoolean   Copyright 1999, 2004 The Apache Software Foundation or its licensors, as applicable.   Licensed under the Apache License, Version 2.0 (the "License");   you may not use this file except in compliance with the License.   You may obtain a copy of the License at      http://www.apache.org/licenses/LICENSE-2.0   Unless required by applicable law or agreed to in writing, software   distributed under the License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the License for the specific language governing permissions and   limitations under the License. */package org.apache.derby.iapi.types;import org.apache.derby.iapi.services.io.ArrayInputStream;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.services.io.Storable;import org.apache.derby.iapi.services.io.StoredFormatIds;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.types.DataValueDescriptor;import org.apache.derby.iapi.types.TypeId;import org.apache.derby.iapi.types.BooleanDataValue;import org.apache.derby.iapi.services.cache.ClassSize;import org.apache.derby.iapi.util.StringUtil;import java.io.ObjectOutput;import java.io.ObjectInput;import java.io.IOException;import java.sql.ResultSet;import java.sql.PreparedStatement;import java.sql.SQLException;/** * SQLBoolean satisfies the DataValueDescriptor * interfaces (i.e., DataType). It implements a boolean column,  * e.g. for * storing a column value; it can be specified * when constructed to not allow nulls. Nullability cannot be changed * after construction, as it affects the storage size and mechanism. * <p> * Because DataType is a subtype of DataType, * SQLBoolean can play a role in either a DataType/Row * or a DataType/Row, interchangeably. * <p> * We assume the store has a flag for nullness of the value, * and simply return a 0-length array for the stored form * when the value is null. * <p> * PERFORMANCE: There are likely alot of performance improvements * possible for this implementation -- it new's Integer * more than it probably wants to. */public final class SQLBoolean	extends DataType implements BooleanDataValue{	/*	 * DataValueDescriptor interface	 * (mostly implemented in DataType)	 */	/*	 * see if the integer value is null.	 */	public boolean isNull()	{		return isnull;	}	public boolean	getBoolean()	{		return value;	}	private static int makeInt(boolean b)	{		return (b?1:0);	}	/** 	 * @see DataValueDescriptor#getByte 	 */	public byte	getByte() 	{		return (byte) makeInt(value);	}	/** 	 * @see DataValueDescriptor#getShort 	 */	public short	getShort()	{		return (short) makeInt(value);	}	/** 	 * @see DataValueDescriptor#getInt 	 */	public int	getInt()	{		return makeInt(value);	}	/** 	 * @see DataValueDescriptor#getLong 	 */	public long	getLong()	{		return (long) makeInt(value);	}	/** 	 * @see DataValueDescriptor#getFloat 	 */	public float	getFloat()	{		return (float) makeInt(value);	}	/** 	 * @see DataValueDescriptor#getDouble 	 */	public double	getDouble()	{		return (double) makeInt(value);	}	/**	 * Implementation for BOOLEAN type. Convert to a BigDecimal using long	 */	public int typeToBigDecimal()	{		return java.sql.Types.BIGINT;	}	public String	getString()	{		if (isNull())			return null;		else if (value == true)			return "true";		else			return "false";	}	public Object	getObject()	{		if (isNull())			return null;		else			return new Boolean(value);	}	public int	getLength()	{		return BOOLEAN_LENGTH;	}	// this is for DataType's error generator	public String getTypeName()	{		return TypeId.BOOLEAN_NAME;	}	/*	 * Storable interface, implies Externalizable, TypedFormat	 */	/**		Return my format identifier.		@see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId	*/	public int getTypeFormatId() {		return StoredFormatIds.SQL_BOOLEAN_ID;	}	public void writeExternal(ObjectOutput out) throws IOException {		// never called when value is null		if (SanityManager.DEBUG)			SanityManager.ASSERT(! isNull());		out.writeBoolean(value);	}	/** @see java.io.Externalizable#readExternal */	public void readExternal(ObjectInput in) throws IOException {		if (SanityManager.DEBUG)			SanityManager.ASSERT( ! immutable,						"Attempt to set the value of an immutable SQLBoolean");		value = in.readBoolean();		isnull = false;	}	public void readExternalFromArray(ArrayInputStream in) throws IOException {		if (SanityManager.DEBUG)			SanityManager.ASSERT( ! immutable,						"Attempt to set the value of an immutable SQLBoolean");		value = in.readBoolean();		isnull = false;	}	/**	 * @see Storable#restoreToNull	 *	 */	public void restoreToNull()	{		if (SanityManager.DEBUG)			SanityManager.ASSERT( ! immutable,						"Attempt to set the value of an immutable SQLBoolean");		value = false;		isnull = true;	}	/*	 * Orderable interface	 */	/**		@exception StandardException thrown on error	 */	public int compare(DataValueDescriptor other) throws StandardException	{		/* Use compare method from dominant type, negating result		 * to reflect flipping of sides.		 */		if (typePrecedence() < other.typePrecedence())		{			return - (other.compare(this));		}		boolean thisNull, otherNull;		thisNull = this.isNull();		otherNull = other.isNull();		/*		 * thisNull otherNull thisValue thatValue return		 *	T		T			X		X			0	(this == other)		 *	F		T			X		X			1 	(this > other)		 *	T		F			X		X			-1	(this < other)		 *		 *	F		F			T		T			0	(this == other)		 *	F		F			T		F			1	(this > other)		 *	F		F			F		T			-1	(this < other)		 *	F		F			F		F			0	(this == other)		 */		if (thisNull || otherNull)		{			if (!thisNull)		// otherNull must be true				return 1;			if (!otherNull)		// thisNull must be true				return -1;			return 0;		}		/* neither are null, get the value */		boolean thisValue;		boolean otherValue = false;		thisValue = this.getBoolean();		otherValue = other.getBoolean();		if (thisValue == otherValue)			return 0;		else if (thisValue && !otherValue)			return 1;		else			return -1;	}	/**		@exception StandardException thrown on error	 */	public boolean compare(int op,						   DataValueDescriptor other,						   boolean orderedNulls,						   boolean unknownRV)		throws StandardException	{		if (!orderedNulls)		// nulls are unordered		{			if (this.isNull() || other.isNull())				return unknownRV;		}		/* Do the comparison */		return super.compare(op, other, orderedNulls, unknownRV);	}	/*	 * DataValueDescriptor interface	 */	/** @see DataValueDescriptor#getClone */	public DataValueDescriptor getClone()	{		return new SQLBoolean(value, isnull);	}	/**	 * @see DataValueDescriptor#getNewNull	 */	public DataValueDescriptor getNewNull()	{		return new SQLBoolean();	}	/** 	 * @see DataValueDescriptor#setValueFromResultSet 	 *	 * @exception SQLException		Thrown on error	 */	public void setValueFromResultSet(ResultSet resultSet, int colNumber,									  boolean isNullable)		throws SQLException	{			value = resultSet.getBoolean(colNumber);			isnull = (isNullable && resultSet.wasNull());	}	/**		Set the value into a PreparedStatement.		@exception SQLException Error setting value in PreparedStatement	*/	public final void setInto(PreparedStatement ps, int position) throws SQLException {		if (isNull()) {			ps.setNull(position, java.sql.Types.BIT);			return;		}		ps.setBoolean(position, value);	}	/*	 * class interface	 */	/*	 * constructors	 */	/* NOTE - other data types have both (type value) and (boolean nulls), 	 * (value, nulls)	 * We can't do both (boolean value) and (boolean nulls) here,	 * so we'll skip over (boolean value) and have (Boolean value) so	 * that we can support (boolean nulls).	 */	public SQLBoolean()	{		isnull = true;	}	public SQLBoolean(boolean val)	{		value = val;	}	public SQLBoolean(Boolean obj) {		if (isnull = (obj == null))			;		else			value = obj.booleanValue();	}	/* This constructor gets used for the getClone() method */	private SQLBoolean(boolean val, boolean isnull)	{		value = val;		this.isnull = isnull;	}	/** @see BooleanDataValue#setValue */	public void setValue(boolean theValue)	{		if (SanityManager.DEBUG)			SanityManager.ASSERT( ! immutable,						"Attempt to set the value of an immutable SQLBoolean");		value = theValue;		isnull = false;	}	public void setValue(Boolean theValue)	{		if (SanityManager.DEBUG)			SanityManager.ASSERT( ! immutable,						"Attempt to set the value of an immutable SQLBoolean");		if (theValue == null)		{			value = false;			isnull = true;		}		else		{			value = theValue.booleanValue();			isnull = false;		}	}	// REMIND: do we need this, or is long enough?	public void setValue(byte theValue)	{		if (SanityManager.DEBUG)			SanityManager.ASSERT( ! immutable,						"Attempt to set the value of an immutable SQLBoolean");		value = theValue != 0;		isnull = false;	}	// REMIND: do we need this, or is long enough?	public void setValue(short theValue)	{		if (SanityManager.DEBUG)			SanityManager.ASSERT( ! immutable,						"Attempt to set the value of an immutable SQLBoolean");		value = theValue != 0;		isnull = false;	}	// REMIND: do we need this, or is long enough?	public void setValue(int theValue)	{		if (SanityManager.DEBUG)			SanityManager.ASSERT( ! immutable,						"Attempt to set the value of an immutable SQLBoolean");		value = theValue != 0;		isnull = false;	}	public void setValue(long theValue)	{		if (SanityManager.DEBUG)			SanityManager.ASSERT( ! immutable,						"Attempt to set the value of an immutable SQLBoolean");		value = theValue != 0;		isnull = false;	}	// REMIND: do we need this, or is double enough?	public void setValue(float theValue)	{		if (SanityManager.DEBUG)			SanityManager.ASSERT( ! immutable,						"Attempt to set the value of an immutable SQLBoolean");		value = theValue != 0;		isnull = false;	}	public void setValue(double theValue)	{		if (SanityManager.DEBUG)			SanityManager.ASSERT( ! immutable,						"Attempt to set the value of an immutable SQLBoolean");		value = theValue != 0;		isnull = false;	}	public void setBigDecimal(Number bigDecimal) throws StandardException	{		if (SanityManager.DEBUG)			SanityManager.ASSERT( ! immutable,						"Attempt to set the value of an immutable SQLBoolean");		if (bigDecimal == null)		{			value = false;			isnull = true;		}		else		{			DataValueDescriptor tempDecimal = NumberDataType.ZERO_DECIMAL.getNewNull();			tempDecimal.setBigDecimal(bigDecimal);			value = NumberDataType.ZERO_DECIMAL.compare(tempDecimal) != 0;			isnull = false;		}	}	/**	 * Set the value of this BooleanDataValue to the given byte array value	 *	 * @param theValue	The value to set this BooleanDataValue to	 *	 * @return	This BooleanDataValue	 */	public void setValue(byte[] theValue)	{		if (SanityManager.DEBUG)			SanityManager.ASSERT( ! immutable,						"Attempt to set the value of an immutable SQLBoolean");		if (theValue != null)		{			isnull = false;			int length = theValue.length;				/*			** Step through all bytes.  As soon			** as we get one with something other			** than 0, then we know we have a 'true'			*/			for (int i = 0; i < length; i++)			{				if (theValue[i] != 0)				{					value = true;					return;				}			}		}		else		{			isnull = true;		}		value = false;	}	/**	 * Set the value of this BooleanDataValue to the given String.	 * String is trimmed and upcased.  If resultant string is not	 * TRUE or FALSE, then an error is thrown.	 *	 * @param theValue	The value to set this BooleanDataValue to	 *	 * @return	This BooleanDataValue	 *	 * @exception StandardException Thrown on error	 */	public void setValue(String theValue)		throws StandardException	{		if (SanityManager.DEBUG)			SanityManager.ASSERT( ! immutable,						"Attempt to set the value of an immutable SQLBoolean");		if (theValue == null)		{			value = false;			isnull = true;		}		else		{			/*			** Note: cannot use getBoolean(String) here because			** it doesn't trim, and doesn't throw exceptions.			*/			String cleanedValue = StringUtil.SQLToUpperCase(theValue.trim());

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -