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

📄 column.java

📁 Java写的含有一个jdbc驱动的小型数据库数据库引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Column.java
 *
 * Copyright (c) 2001, 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 THE REGENTS 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 package is based on HypersonicSQL, originally developed by Thomas Mueller.
 *
 */
package org.hsqldb;

import java.sql.*;
import java.math.BigDecimal;
import java.io.*;
import java.util.*;

/**
 * Class declaration
 *
 *
 * @version 1.0.0.1
 */
class Column {
    private static Hashtable hTypes;
    final static int	     BIT = Types.BIT;			     // -7
    final static int	     TINYINT = Types.TINYINT;		     // -6
    final static int	     BIGINT = Types.BIGINT;		     // -5
    final static int	     LONGVARBINARY = Types.LONGVARBINARY;    // -4
    final static int	     VARBINARY = Types.VARBINARY;	     // -3
    final static int	     BINARY = Types.BINARY;		     // -2
    final static int	     LONGVARCHAR = Types.LONGVARCHAR;	     // -1
    final static int	     CHAR = Types.CHAR;			     // 1
    final static int	     NUMERIC = Types.NUMERIC;		     // 2
    final static int	     DECIMAL = Types.DECIMAL;		     // 3
    final static int	     INTEGER = Types.INTEGER;		     // 4
    final static int	     SMALLINT = Types.SMALLINT;		     // 5
    final static int	     FLOAT = Types.FLOAT;		     // 6
    final static int	     REAL = Types.REAL;			     // 7
    final static int	     DOUBLE = Types.DOUBLE;		     // 8
    final static int	     VARCHAR = Types.VARCHAR;		     // 12
    final static int	     DATE = Types.DATE;			     // 91
    final static int	     TIME = Types.TIME;			     // 92
    final static int	     TIMESTAMP = Types.TIMESTAMP;	     // 93
    final static int	     OTHER = Types.OTHER;		     // 1111
    final static int	     NULL = Types.NULL;			     // 0
    final static int	     VARCHAR_IGNORECASE =
	100;							     // this is the only non-standard type

    // NULL and VARCHAR_IGNORECASE is not part of TYPES
    final static int	     TYPES[] = {
	BIT, TINYINT, BIGINT, LONGVARBINARY, VARBINARY, BINARY, LONGVARCHAR,
	CHAR, NUMERIC, DECIMAL, INTEGER, SMALLINT, FLOAT, REAL, DOUBLE,
	VARCHAR, DATE, TIME, TIMESTAMP, OTHER
    };
    String		     sName;
    int			     iType;
    private boolean	     bNullable;
    private boolean	     bIdentity;
    static {
	hTypes = new Hashtable();

	addTypes(INTEGER, "INTEGER", "int", "java.lang.Integer");
	addType(INTEGER, "INT");
	addTypes(DOUBLE, "DOUBLE", "double", "java.lang.Double");
	addType(FLOAT, "FLOAT");		       // this is a Double
	addTypes(VARCHAR, "VARCHAR", "java.lang.String", null);
	addTypes(CHAR, "CHAR", "CHARACTER", null);
	addType(LONGVARCHAR, "LONGVARCHAR");

	// for ignorecase data types, the 'original' type name is lost
	addType(VARCHAR_IGNORECASE, "VARCHAR_IGNORECASE");
	addTypes(DATE, "DATE", "java.sql.Date", null);
	addTypes(TIME, "TIME", "java.sql.Time", null);

	// DATETIME is for compatibility with MS SQL 7
	addTypes(TIMESTAMP, "TIMESTAMP", "java.sql.Timestamp", "DATETIME");
	addTypes(DECIMAL, "DECIMAL", "java.math.BigDecimal", null);
	addType(NUMERIC, "NUMERIC");
	addTypes(BIT, "BIT", "java.lang.Boolean", "boolean");
	addTypes(TINYINT, "TINYINT", "java.lang.Short", "short");
	addType(SMALLINT, "SMALLINT");
	addTypes(BIGINT, "BIGINT", "java.lang.Long", "long");
	addTypes(REAL, "REAL", "java.lang.Float", "float");
	addTypes(BINARY, "BINARY", "byte[]", null);    // maybe better "[B"
	addType(VARBINARY, "VARBINARY");
	addType(LONGVARBINARY, "LONGVARBINARY");
	addTypes(OTHER, "OTHER", "java.lang.Object", "OBJECT");
    }

    /**
     * Method declaration
     *
     *
     * @param type
     * @param name
     * @param n2
     * @param n3
     */
    private static void addTypes(int type, String name, String n2,
				 String n3) {
	addType(type, name);
	addType(type, n2);
	addType(type, n3);
    }

    /**
     * Method declaration
     *
     *
     * @param type
     * @param name
     */
    private static void addType(int type, String name) {
	if (name != null) {
	    hTypes.put(name, new Integer(type));
	}
    }

    /**
     * Constructor declaration
     *
     *
     * @param name
     * @param nullable
     * @param type
     * @param identity
     */
    Column(String name, boolean nullable, int type, boolean identity) {
	sName = name;
	bNullable = nullable;
	iType = type;
	bIdentity = identity;
    }

    /**
     * Method declaration
     *
     *
     * @return
     */
    boolean isIdentity() {
	return bIdentity;
    }

    /**
     * Method declaration
     *
     *
     * @param type
     *
     * @return
     *
     * @throws SQLException
     */
    static int getTypeNr(String type) throws SQLException {
	Integer i = (Integer) hTypes.get(type);

	Trace.check(i != null, Trace.WRONG_DATA_TYPE, type);

	return i.intValue();
    }

    /**
     * Method declaration
     *
     *
     * @param type
     *
     * @return
     *
     * @throws SQLException
     */
    static String getType(int type) throws SQLException {
	switch (type) {

	case NULL:
	    return "NULL";

	case INTEGER:
	    return "INTEGER";

	case DOUBLE:
	    return "DOUBLE";

	case VARCHAR_IGNORECASE:
	    return "VARCHAR_IGNORECASE";

	case VARCHAR:
	    return "VARCHAR";

	case CHAR:
	    return "CHAR";

	case LONGVARCHAR:
	    return "LONGVARCHAR";

	case DATE:
	    return "DATE";

	case TIME:
	    return "TIME";

	case DECIMAL:
	    return "DECIMAL";

	case BIT:
	    return "BIT";

	case TINYINT:
	    return "TINYINT";

	case SMALLINT:
	    return "SMALLINT";

	case BIGINT:
	    return "BIGINT";

	case REAL:
	    return "REAL";

	case FLOAT:
	    return "FLOAT";

	case NUMERIC:
	    return "NUMERIC";

	case TIMESTAMP:
	    return "TIMESTAMP";

	case BINARY:
	    return "BINARY";

	case VARBINARY:
	    return "VARBINARY";

	case LONGVARBINARY:
	    return "LONGVARBINARY";

	case OTHER:
	    return "OBJECT";

	default:
	    throw Trace.error(Trace.WRONG_DATA_TYPE, type);
	}
    }

    /**
     * Method declaration
     *
     *
     * @return
     */
    boolean isNullable() {
	return bNullable;
    }

    /**
     * Method declaration
     *
     *
     * @param a
     * @param b
     * @param type
     *
     * @return
     *
     * @throws SQLException
     */
    static Object add(Object a, Object b, int type) throws SQLException {
	if (a == null || b == null) {
	    return null;
	}

	switch (type) {

	case NULL:
	    return null;

	case INTEGER:
	    int ai = ((Integer) a).intValue();
	    int bi = ((Integer) b).intValue();

	    return new Integer(ai + bi);

	case FLOAT:

	case DOUBLE:
	    double ad = ((Double) a).doubleValue();
	    double bd = ((Double) b).doubleValue();

	    return new Double(ad + bd);

	case VARCHAR:

	case CHAR:

	case LONGVARCHAR:

	case VARCHAR_IGNORECASE:
	    return (String) a + (String) b;

	case NUMERIC:

	case DECIMAL:
	    BigDecimal abd = (BigDecimal) a;
	    BigDecimal bbd = (BigDecimal) b;

	    return abd.add(bbd);

	case TINYINT:

	case SMALLINT:
	    short shorta = ((Short) a).shortValue();
	    short shortb = ((Short) b).shortValue();

	    return new Short((short) (shorta + shortb));

	case BIGINT:
	    long longa = ((Long) a).longValue();
	    long longb = ((Long) b).longValue();

	    return new Long(longa + longb);

	case REAL:
	    float floata = ((Float) a).floatValue();
	    float floatb = ((Float) b).floatValue();

	    return new Float(floata + floatb);

	default:
	    throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, type);
	}
    }

    /**
     * Method declaration
     *
     *
     * @param a
     * @param b
     *
     * @return
     *
     * @throws SQLException
     */
    static Object concat(Object a, Object b) throws SQLException {
	if (a == null) {
	    return b;
	} else if (b == null) {
	    return a;
	}

	return convertObject(a) + convertObject(b);
    }

    /**
     * Method declaration
     *
     *
     * @param a
     * @param type
     *
     * @return
     *
     * @throws SQLException
     */
    static Object negate(Object a, int type) throws SQLException {
	if (a == null) {
	    return null;
	}

	switch (type) {

	case NULL:
	    return null;

	case INTEGER:
	    return new Integer(-((Integer) a).intValue());

	case FLOAT:

	case DOUBLE:
	    return new Double(-((Double) a).doubleValue());

	case NUMERIC:

	case DECIMAL:
	    return ((BigDecimal) a).negate();

	case TINYINT:

	case SMALLINT:
	    return new Short((short) -((Short) a).shortValue());

	case BIGINT:
	    return new Long(-((Long) a).longValue());

	case REAL:
	    return new Float(-((Float) a).floatValue());

	default:
	    throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, type);
	}
    }

    /**
     * Method declaration
     *
     *
     * @param a
     * @param b
     * @param type
     *
     * @return
     *
     * @throws SQLException
     */
    static Object multiply(Object a, Object b, int type) throws SQLException {
	if (a == null || b == null) {
	    return null;
	}

	switch (type) {

	case NULL:
	    return null;

	case INTEGER:
	    int ai = ((Integer) a).intValue();
	    int bi = ((Integer) b).intValue();

	    return new Integer(ai * bi);

	case FLOAT:

	case DOUBLE:
	    double ad = ((Double) a).doubleValue();
	    double bd = ((Double) b).doubleValue();

	    return new Double(ad * bd);

	case NUMERIC:

	case DECIMAL:
	    BigDecimal abd = (BigDecimal) a;
	    BigDecimal bbd = (BigDecimal) b;

	    return abd.multiply(bbd);

	case TINYINT:

	case SMALLINT:
	    short shorta = ((Short) a).shortValue();
	    short shortb = ((Short) b).shortValue();

	    return new Short((short) (shorta * shortb));

	case BIGINT:
	    long longa = ((Long) a).longValue();
	    long longb = ((Long) b).longValue();

	    return new Long(longa * longb);

	case REAL:
	    float floata = ((Float) a).floatValue();
	    float floatb = ((Float) b).floatValue();

	    return new Float(floata * floatb);

	default:
	    throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, type);
	}
    }

    /**
     * Method declaration
     *
     *
     * @param a
     * @param b
     * @param type
     *
     * @return
     *
     * @throws SQLException
     */
    static Object divide(Object a, Object b, int type) throws SQLException {
	if (a == null || b == null) {
	    return null;
	}

	switch (type) {

	case NULL:
	    return null;

	case INTEGER:
	    int ai = ((Integer) a).intValue();
	    int bi = ((Integer) b).intValue();

	    Trace.check(bi != 0, Trace.DIVISION_BY_ZERO);

	    return new Integer(ai / bi);

	case FLOAT:

	case DOUBLE:
	    double ad = ((Double) a).doubleValue();
	    double bd = ((Double) b).doubleValue();

	    return bd == 0 ? null : new Double(ad / bd);

	case NUMERIC:

	case DECIMAL:
	    BigDecimal abd = (BigDecimal) a;
	    BigDecimal bbd = (BigDecimal) b;

	    return bbd.signum() == 0 ? null
		   : abd.divide(bbd, BigDecimal.ROUND_HALF_DOWN);

	case TINYINT:

	case SMALLINT:
	    short shorta = ((Short) a).shortValue();
	    short shortb = ((Short) b).shortValue();

	    return shortb == 0 ? null : new Short((short) (shorta / shortb));

	case BIGINT:
	    long longa = ((Long) a).longValue();
	    long longb = ((Long) b).longValue();

	    return longb == 0 ? null : new Long(longa / longb);

	case REAL:
	    float floata = ((Float) a).floatValue();
	    float floatb = ((Float) b).floatValue();

	    return floatb == 0 ? null : new Float(floata / floatb);

	default:
	    throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, type);
	}
    }

    /**
     * Method declaration
     *
     *
     * @param a
     * @param b
     * @param type
     *
     * @return
     *
     * @throws SQLException
     */
    static Object subtract(Object a, Object b, int type) throws SQLException {
	if (a == null || b == null) {
	    return null;
	}

	switch (type) {

	case NULL:
	    return null;

	case INTEGER:
	    int ai = ((Integer) a).intValue();
	    int bi = ((Integer) b).intValue();

	    return new Integer(ai - bi);

	case FLOAT:

	case DOUBLE:
	    double ad = ((Double) a).doubleValue();
	    double bd = ((Double) b).doubleValue();

	    return new Double(ad - bd);

	case NUMERIC:

	case DECIMAL:
	    BigDecimal abd = (BigDecimal) a;
	    BigDecimal bbd = (BigDecimal) b;

	    return abd.subtract(bbd);

	case TINYINT:

	case SMALLINT:
	    short shorta = ((Short) a).shortValue();
	    short shortb = ((Short) b).shortValue();

	    return new Short((short) (shorta - shortb));

	case BIGINT:
	    long longa = ((Long) a).longValue();
	    long longb = ((Long) b).longValue();

	    return new Long(longa - longb);

	case REAL:
	    float floata = ((Float) a).floatValue();
	    float floatb = ((Float) b).floatValue();

	    return new Float(floata - floatb);

	default:
	    throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, type);
	}
    }

    /**
     * Method declaration
     *
     *
     * @param a
     * @param b
     * @param type
     *
     * @return
     *
     * @throws SQLException
     */
    static Object sum(Object a, Object b, int type) throws SQLException {
	if (a == null) {
	    return b;
	}

	if (b == null) {
	    return a;
	}

	switch (type) {

	case NULL:
	    return null;

	case INTEGER:
	    return new Integer(((Integer) a).intValue()
			       + ((Integer) b).intValue());

	case FLOAT:

	case DOUBLE:
	    return new Double(((Double) a).doubleValue()
			      + ((Double) b).doubleValue());

	case NUMERIC:

	case DECIMAL:
	    return ((BigDecimal) a).add((BigDecimal) b);

	case TINYINT:

	case SMALLINT:
	    return new Short((short) (((Short) a).shortValue()
				      + ((Short) b).shortValue()));

	case BIGINT:
	    return new Long(((Long) a).longValue() + ((Long) b).longValue());

	case REAL:
	    return new Float(((Float) a).floatValue()
			     + ((Float) b).floatValue());

	default:
	    Trace.error(Trace.SUM_OF_NON_NUMERIC);
	}

	return null;
    }

    /**
     * Method declaration
     *
     *
     * @param a
     * @param type
     * @param count
     *
     * @return
     *
     * @throws SQLException
     */
    static Object avg(Object a, int type, int count) throws SQLException {
	if (a == null || count == 0) {
	    return null;
	}

	switch (type) {

	case NULL:
	    return null;

	case INTEGER:
	    return new Integer(((Integer) a).intValue() / count);

	case FLOAT:

	case DOUBLE:
	    return new Double(((Double) a).doubleValue() / count);

	case NUMERIC:

	case DECIMAL:
	    return ((BigDecimal) a).divide(new BigDecimal(count),
					   BigDecimal.ROUND_HALF_DOWN);

	case TINYINT:

⌨️ 快捷键说明

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