📄 column.cs
字号:
/*
* Column.cs
*
* 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.
*
* C# port by Mark Tutt
*
*/
namespace SharpHSQL
{
using System;
using System.Collections;
using System.IO;
using System.Text;
/**
* Class declaration
*
*
* @version 1.0.0.1
*/
class Column
{
private static Hashtable hTypes;
public const int BIT = -7;
public const int TINYINT = -6;
public const int BIGINT = -5;
public const int LONGVARBINARY = -4;
public const int VARBINARY = -3;
public const int BINARY = -2;
public const int LONGVARCHAR = -1;
public const int CHAR = 1;
public const int NUMERIC = 2;
public const int DECIMAL = 3;
public const int INTEGER = 4;
public const int SMALLINT = 5;
public const int FLOAT = 6;
public const int REAL = 7;
public const int DOUBLE = 8;
public const int VARCHAR = 12;
public const int DATE = 91;
public const int TIME = 92;
public const int TIMESTAMP = 93;
public const int OTHER = 1111;
public const int NULL = 0;
public const int VARCHAR_IGNORECASE = 100; // this is the only non-standard type
// NULL and VARCHAR_IGNORECASE is not part of TYPES
public static int[] TYPES =
{
BIT, TINYINT, BIGINT, LONGVARBINARY, VARBINARY, BINARY, LONGVARCHAR,
CHAR, NUMERIC, DECIMAL, INTEGER, SMALLINT, FLOAT, REAL, DOUBLE,
VARCHAR, DATE, TIME, TIMESTAMP, OTHER
};
public string sName;
public int iType;
private bool bNullable;
private bool bIdentity;
/**
* 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.Add(name, type);
}
}
/**
* Constructor declaration
*
*
* @param name
* @param nullable
* @param type
* @param identity
*/
public Column(string name, bool nullable, int type, bool identity)
{
sName = name;
bNullable = nullable;
iType = type;
bIdentity = identity;
}
/**
* Method declaration
*
*
* @return
*/
public bool isIdentity()
{
return bIdentity;
}
/**
* Method declaration
*
*
* @param type
*
* @return
*
* @throws Exception
*/
public static int getTypeNr(string type)
{
if (hTypes == null)
{
hTypes = new Hashtable();
addTypes(INTEGER, "INTEGER", "int", "java.lang.int");
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", "bool");
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");
}
if (hTypes.ContainsKey(type))
{
int i = (int)hTypes[type];
return i;
}
else
throw Trace.error(Trace.WRONG_DATA_TYPE, type);
}
/**
* Method declaration
*
*
* @param type
*
* @return
*
* @throws Exception
*/
public static string getType(int type)
{
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
*/
public bool isNullable()
{
return bNullable;
}
/**
* Method declaration
*
*
* @param a
* @param b
* @param type
*
* @return
*
* @throws Exception
*/
public static object add(object a, object b, int type)
{
if (a == null || b == null)
{
return null;
}
switch (type)
{
case NULL:
return null;
case INTEGER:
int ai = (int)a;
int bi = (int)b;
return (ai + bi);
case FLOAT:
case REAL:
float ar = (float) a;
float br = (float) b;
return (ar + br);
case DOUBLE:
double ad = (double) a;
double bd = (double) b;
return (ad + bd);
case VARCHAR:
case CHAR:
case LONGVARCHAR:
case VARCHAR_IGNORECASE:
return (string) a + (string) b;
case NUMERIC:
case DECIMAL:
decimal abd = (decimal) a;
decimal bbd = (decimal) b;
return (abd + bbd);
case TINYINT:
byte at = (byte) a;
byte bt = (byte) b;
return (at + bt);
case SMALLINT:
short shorta = (short) a;
short shortb = (short) b;
return (shorta + shortb);
case BIGINT:
long longa = (long) a;
long longb = (long) b;
return (longa + longb);
default:
throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, type);
}
}
/**
* Method declaration
*
*
* @param a
* @param b
*
* @return
*
* @throws Exception
*/
public static object concat(object a, object b)
{
if (a == null)
{
return b;
}
else if (b == null)
{
return a;
}
return convertobject(a) + convertobject(b);
}
/**
* Method declaration
*
*
* @param a
* @param type
*
* @return
*
* @throws Exception
*/
public static object negate(object a, int type)
{
if (a == null)
{
return null;
}
switch (type)
{
case NULL:
return null;
case INTEGER:
return (-(int) a);
case FLOAT:
case REAL:
return (-(float) a);
case DOUBLE:
return (-(double) a);
case NUMERIC:
case DECIMAL:
return (-(decimal)a);
case TINYINT:
return (-(byte)a);
case SMALLINT:
return (-(short)a);
case BIGINT:
return (-(long) a);
default:
throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, type);
}
}
/**
* Method declaration
*
*
* @param a
* @param b
* @param type
*
* @return
*
* @throws Exception
*/
public static object multiply(object a, object b, int type)
{
if (a == null || b == null)
{
return null;
}
switch (type)
{
case NULL:
return null;
case INTEGER:
int ai = (int) a;
int bi = (int) b;
return (ai * bi);
case FLOAT:
case REAL:
float floata = (float) a;
float floatb = (float) b;
return (floata * floatb);
case DOUBLE:
double ad = (double) a;
double bd = (double) b;
return (ad * bd);
case NUMERIC:
case DECIMAL:
decimal abd = (decimal) a;
decimal bbd = (decimal) b;
return (abd * bbd);
case TINYINT:
byte ba = (byte) a;
byte bb = (byte) b;
return (ba * bb);
case SMALLINT:
short shorta = (short) a;
short shortb = (short) b;
return (shorta * shortb);
case BIGINT:
long longa = (long) a;
long longb = (long) b;
return (longa * longb);
default:
throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, type);
}
}
/**
* Method declaration
*
*
* @param a
* @param b
* @param type
*
* @return
*
* @throws Exception
*/
public static object divide(object a, object b, int type)
{
if (a == null || b == null)
{
return null;
}
switch (type)
{
case NULL:
return null;
case INTEGER:
if ((int)b == 0)
return null;
return ((int)a / (int)b);
case FLOAT:
case REAL:
if ((float) b == 0)
return null;
return ((float)a / (float)b);
case DOUBLE:
if ((double) b == 0)
return null;
return ((double)a / (double)b);
case NUMERIC:
case DECIMAL:
if ((decimal) b == 0)
return null;
return ((decimal)a / (decimal)b);
case TINYINT:
if ((byte) b == 0)
return null;
return ((byte)a / (byte)b);
case SMALLINT:
if ((short) b == 0)
return null;
return ((short)a / (short)b);
case BIGINT:
if ((long) b == 0)
return null;
return ((long)a / (long)b);
default:
throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, type);
}
}
/**
* Method declaration
*
*
* @param a
* @param b
* @param type
*
* @return
*
* @throws Exception
*/
public static object subtract(object a, object b, int type)
{
if (a == null || b == null)
{
return null;
}
switch (type)
{
case NULL:
return null;
case INTEGER:
return ((int)a - (int)b);
case FLOAT:
case REAL:
return ((float)a - (float)b);
case DOUBLE:
return ((double)a - (double)b);
case NUMERIC:
case DECIMAL:
return ((decimal)a - (decimal)b);
case TINYINT:
return ((byte)a - (byte)b);
case SMALLINT:
return ((short)a - (short)b);
case BIGINT:
return ((long)a - (long)b);
default:
throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, type);
}
}
/**
* Method declaration
*
*
* @param a
* @param b
* @param type
*
* @return
*
* @throws Exception
*/
public static object sum(object a, object b, int type)
{
if (a == null)
{
return b;
}
if (b == null)
{
return a;
}
switch (type)
{
case NULL:
return null;
case INTEGER:
return (((int) a) + ((int) b));
case FLOAT:
case REAL:
return (((float) a) + ((float) b));
case DOUBLE:
return (((double) a) + ((double) b));
case NUMERIC:
case DECIMAL:
return (((decimal) a) + ((decimal) b));
case TINYINT:
return (((byte) a) + ((byte) b));
case SMALLINT:
return (((short) a) + ((short) b));
case BIGINT:
return (((long) a) + ((long) b));
default:
Trace.error(Trace.SUM_OF_NON_NUMERIC);
}
return null;
}
/**
* Method declaration
*
*
* @param a
* @param type
* @param count
*
* @return
*
* @throws Exception
*/
public static object avg(object a, int type, int count)
{
if (a == null || count == 0)
{
return null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -