📄 column.java
字号:
/* Copyright (c) 1995-2000, The Hypersonic SQL Group. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of the Hypersonic SQL Group nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE HYPERSONIC SQL GROUP, * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * This software consists of voluntary contributions made by many individuals * on behalf of the Hypersonic SQL Group. * * * For work added by the HSQL Development Group: * * Copyright (c) 2001-2005, The HSQL Development Group * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of the HSQL Development Group nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG, * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package org.hsqldb;import java.io.IOException;import java.io.Serializable;import java.math.BigDecimal;import java.math.BigInteger;import java.sql.Date;import java.sql.Time;import java.sql.Timestamp;import org.hsqldb.HsqlNameManager.HsqlName;import org.hsqldb.lib.StringConverter;import org.hsqldb.store.ValuePool;import org.hsqldb.types.Binary;import org.hsqldb.types.JavaObject;import org.hsqldb.lib.java.JavaSystem;// fredt@users 20020130 - patch 491987 by jimbag@users// fredt@users 20020320 - doc 1.7.0 - update// fredt@users 20020401 - patch 442993 by fredt - arithmetic expressions// to allow mixed type arithmetic expressions beginning with a narrower type// changes applied to several lines of code and not marked separately// consists of changes to arithmatic functions to allow promotion of// java.lang.Number values and new functions to choose type for promotion// fredt@users 20020401 - patch 455757 by galena@users (Michiel de Roo)// interpretation of TINYINT as Byte instead of Short// fredt@users 20020130 - patch 491987 by jimbag@users// support for sql standard char and varchar. size is maintained as// defined in the DDL and trimming and padding takes place accordingly// modified by fredt - trimming and padding are turned off by default but// can be applied accross the database by defining sql.enforce_size=true in// database.properties file// fredt@users 20020215 - patch 1.7.0 by fredt - quoted identifiers// applied to different parts to support the sql standard for// naming of columns and tables (use of quoted identifiers as names)// fredt@users 20020328 - patch 1.7.0 by fredt - change REAL to Double// fredt@users 20020402 - patch 1.7.0 by fredt - type conversions// frequently used type conversions are done without creating temporary// Strings to reduce execution time and garbage collection// fredt@users 20021013 - patch 1.7.1 by fredt - type conversions// scripting of Double.Nan and infinity values// fredt@users 20030715 - patch 1.7.2 - type narrowing for numeric values// fredt@users - patch 1.8.0 - enforcement of precision and scale/** * Implementation of SQL table columns as defined in DDL statements with * static methods to process their values.<p> * * Enhanced type checking and conversion by fredt@users * * @author Thomas Mueller (Hypersonic SQL Group) * @author fredt@users * @version 1.8.0 * @since Hypersonic SQL */public class Column {// -------------------------------------------------- // DDL name, size, scale, null, identity and default values // most variables are final but not declared so because of a bug in // JDK 1.1.8 compiler public HsqlName columnName; private int colType; private int colSize; private int colScale; private boolean isNullable; private boolean isIdentity; private boolean isPrimaryKey; private Expression defaultExpression; long identityStart; long identityIncrement; static final BigInteger MAX_LONG = BigInteger.valueOf(Long.MAX_VALUE); static final BigInteger MIN_LONG = BigInteger.valueOf(Long.MIN_VALUE); static final BigInteger MAX_INT = BigInteger.valueOf(Integer.MAX_VALUE); static final BigInteger MIN_INT = BigInteger.valueOf(Integer.MIN_VALUE); static final BigDecimal BIG_DECIMAL_0 = new BigDecimal(0.0); static final BigDecimal BIG_DECIMAL_1 = new BigDecimal(1.0); /** * Creates a column defined in DDL statement. * * @param name * @param nullable * @param type * @param size * @param scale * @param identity * @param startvalue * @param increment * @param primarykey * @param defstring */ Column(HsqlName name, boolean nullable, int type, int size, int scale, boolean primarykey, Expression defexpression) throws HsqlException { columnName = name; isNullable = nullable; colType = type; colSize = size; colScale = scale; isPrimaryKey = primarykey; defaultExpression = defexpression; } void setIdentity(boolean identity, long startvalue, long increment) throws HsqlException { isIdentity = identity; identityStart = startvalue; identityIncrement = increment; if (isIdentity) { if (colType == Types.INTEGER) { if (identityStart > Integer.MAX_VALUE || identityIncrement > Integer.MAX_VALUE) { throw Trace.error(Trace.NUMERIC_VALUE_OUT_OF_RANGE, columnName.statementName); } } } } private Column() {} /** * Used for primary key changes. */ Column duplicate(boolean withIdentity) throws HsqlException { Column newCol = new Column(); newCol.columnName = columnName; newCol.isNullable = isNullable; newCol.colType = colType; newCol.colSize = colSize; newCol.colScale = colScale; newCol.defaultExpression = defaultExpression; if (withIdentity) { newCol.setIdentity(isIdentity, identityStart, identityIncrement); } return newCol; } void setType(Column other) { isNullable = other.isNullable; colType = other.colType; colSize = other.colSize; colScale = other.colScale; } /** * Is this the identity column in the table. * * @return boolean */ boolean isIdentity() { return isIdentity; } /** * Is column nullable. * * @return boolean */ boolean isNullable() { return isNullable; } /** * Set nullable. * */ void setNullable(boolean value) { isNullable = value; } /** * Is this single column primary key of the table. * * @return boolean */ public boolean isPrimaryKey() { return isPrimaryKey; } /** * Set primary key. * */ void setPrimaryKey(boolean value) { isPrimaryKey = value; } /** * Returns default value in the session context. */ Object getDefaultValue(Session session) throws HsqlException { return defaultExpression == null ? null : defaultExpression.getValue(session, colType); } /** * Returns DDL for default value. */ String getDefaultDDL() { String ddl = null; try { ddl = defaultExpression == null ? null : defaultExpression.getDDL(); } catch (HsqlException e) {} return ddl; } /** * Returns default expression for the column. */ Expression getDefaultExpression() { return defaultExpression; } void setDefaultExpression(Expression expr) { defaultExpression = expr; } /** * Type of the column. * * @return java.sql.Types int value for the column */ int getType() { return colType; } int getDIType() { return colType == Types.VARCHAR_IGNORECASE ? Types.VARCHAR : colType; } int getDITypeSub() { if (colType == Types.VARCHAR_IGNORECASE) { return Types.TYPE_SUB_IGNORECASE; } return Types.TYPE_SUB_DEFAULT; } /** * Size of the column in DDL (0 if not defined). * * @return DDL size of column */ int getSize() { return colSize; } /** * Scale of the column in DDL (0 if not defined). * * @return DDL scale of column */ int getScale() { return colScale; } /** * Add two object of a given type * * @param a * @param b * @param type * @return result * @throws HsqlException */ static Object add(Object a, Object b, int type) throws HsqlException { if (a == null || b == null) { return null; } switch (type) { case Types.NULL : return null; case Types.REAL : case Types.FLOAT : case Types.DOUBLE : { double ad = ((Number) a).doubleValue(); double bd = ((Number) b).doubleValue(); return ValuePool.getDouble(Double.doubleToLongBits(ad + bd));// return new Double(ad + bd); } case Types.VARCHAR : case Types.CHAR : case Types.LONGVARCHAR : case Types.VARCHAR_IGNORECASE : return (String) a + (String) b; case Types.NUMERIC : case Types.DECIMAL : { BigDecimal abd = (BigDecimal) a; BigDecimal bbd = (BigDecimal) b; return abd.add(bbd); } case Types.TINYINT : case Types.SMALLINT : case Types.INTEGER : { int ai = ((Number) a).intValue(); int bi = ((Number) b).intValue(); return ValuePool.getInt(ai + bi); } case Types.BIGINT : { long longa = ((Number) a).longValue(); long longb = ((Number) b).longValue(); return ValuePool.getLong(longa + longb); } default : throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, Types.getTypeString(type)); } } /** * Concat two objects by turning them into strings first. * * @param a * @param b * @return result * @throws HsqlException */ static Object concat(Object a, Object b) throws HsqlException { if (a == null || b == null) { return null; } return a.toString() + b.toString(); } /** * Negate a numeric object. * * @param a * @param type * @return result * @throws HsqlException */ static Object negate(Object a, int type) throws HsqlException { if (a == null) { return null; } switch (type) { case Types.NULL : return null; case Types.REAL : case Types.FLOAT : case Types.DOUBLE : { double ad = -((Number) a).doubleValue(); return ValuePool.getDouble(Double.doubleToLongBits(ad)); } case Types.NUMERIC : case Types.DECIMAL : return ((BigDecimal) a).negate(); case Types.TINYINT : case Types.SMALLINT : case Types.INTEGER : return ValuePool.getInt(-((Number) a).intValue()); case Types.BIGINT : return ValuePool.getLong(-((Number) a).longValue()); default : throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, Types.getTypeString(type)); } } /** * Multiply two numeric objects. * * @param a * @param b * @param type * @return result * @throws HsqlException */ static Object multiply(Object a, Object b,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -