📄 tscolumn.java
字号:
/*
* tsColumn.java - Column Object for tinySQL.
*
* Copyright 1996, Brian C. Jepson
* (bjepson@ids.net)
* $Author: davis $
* $Date: 2004/12/18 21:25:35 $
* $Revision: 1.1 $
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser 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
*
*/
package com.sqlmagic.tinysql;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.*;
import java.sql.Types;
/*
* Object to hold column metadata and value
* Example for a column_def entity:
* phone CHAR(30) DEFAULT '-' NOT NULL
*
* @author Thomas Morgner <mgs@sherito.org> type is now integer
* and contains one of the java.sql.Types Values
*/
class tsColumn implements Cloneable
{
public String name = null; // the column's name
public String alias = null; // the column's definition
public String longName = null; // the column's long name ( > 11 chars)
public Vector contextList; // the columns context (SELECT,ORDER,etc.)
public int type = -1; // the column's type
// dBase types:
// 'C' Char (max 254 bytes)
// 'N' '-.0123456789' (max 19 bytes)
// 'L' 'YyNnTtFf?' (1 byte)
// 'M' 10 digit .DBT block number
// 'D' 8 digit YMD
public int size = 0; // the column's size
public int decimalPlaces = 0; // decimal places in numeric column
public String defaultVal = null;// not yet supported
public int position = 0; // internal use
public String tableName = ""; // the table which "owns" the column
public tinySQLTable columnTable = null;
public String newLine = System.getProperty("line.separator");
String functionName = (String)null; // Function name
String functionArgString = (String)null; // Function arguments
Vector functionArgs = (Vector)null; // Function arguments as columns
boolean notNull = false;
boolean valueSet = false;
String stringValue = (String)null;
int intValue = Integer.MIN_VALUE;
float floatValue = Float.MIN_VALUE;
SimpleDateFormat fmtyyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
Calendar today = Calendar.getInstance();
boolean isConstant = false;
boolean groupedColumn = false;
/*
* The constructor creates a column object using recursion if this is a
* function.
*/
tsColumn (String s) throws tinySQLException
{
this(s,(Hashtable)null,"SELECT");
}
tsColumn (String s, Hashtable tableDefs,String inputContext)
throws tinySQLException
{
FieldTokenizer ft,ftArgs;
int i,j,numericType,nameLength,dotAt,argIndex;
String upperName,checkName,nextArg;
tinySQLTable jtbl;
tsColumn tcol;
Vector t;
Enumeration col_keys;
name = s;
longName = name;
nameLength = name.length();
contextList = new Vector();
contextList.addElement(inputContext);
ft = new FieldTokenizer(name,'(',false);
if ( ft.countFields() == 2 )
{
/*
* This is a function rather than a simple column or constant
*/
functionName = ft.getField(0).toUpperCase();
if ( functionName.equals("COUNT") )
{
type = Types.INTEGER;
size = 10;
intValue = Integer.MIN_VALUE;
groupedColumn = true;
} else if ( functionName.equals("SUM") ) {
type = Types.FLOAT;
size = 10;
groupedColumn = true;
} else if ( functionName.equals("TO_DATE") ) {
type = Types.DATE;
size = 10;
} else if ( functionName.equals("CONCAT") |
functionName.equals("UPPER") |
functionName.equals("SUBSTR") |
functionName.equals("TRIM") ) {
type = Types.CHAR;
}
functionArgString = ft.getField(1);
ftArgs = new FieldTokenizer(functionArgString,',',false);
functionArgs = new Vector();
argIndex = 0;
while ( ftArgs.hasMoreFields() )
{
nextArg = ftArgs.nextField();
tcol = new tsColumn(nextArg,tableDefs,inputContext);
if ( tcol.isGroupedColumn() ) groupedColumn = true;
/*
* MAX and MIN functions can be either FLOAT or CHAR types
* depending upon the type of the argument.
*/
if ( functionName.equals("MAX") | functionName.equals("MIN") )
{
if ( argIndex > 0 )
throw new tinySQLException("Function can only have 1 argument");
groupedColumn = true;
type = tcol.type;
size = tcol.size;
} else if ( functionName.equals("CONCAT") ) {
type = Types.CHAR;
size += tcol.size;
} else if ( functionName.equals("UPPER") ) {
type = Types.CHAR;
size = tcol.size;
} else if ( functionName.equals("TO_DATE") ) {
type = Types.DATE;
size = 10;
} else if ( functionName.equals("TRIM") ) {
type = Types.CHAR;
size = tcol.size;
} else if ( functionName.equals("SUBSTR") ) {
type = Types.CHAR;
if ( argIndex == 0 & tcol.type != Types.CHAR )
{
throw new tinySQLException("SUBSTR first argument must be character");
} else if ( argIndex == 1 ) {
if ( tcol.type != Types.INTEGER | tcol.intValue < 1 )
throw new tinySQLException("SUBSTR second argument "
+ tcol.getString() + " must be integer > 0");
} else if ( argIndex == 2 ) {
if ( tcol.type != Types.INTEGER | tcol.intValue < 1)
throw new tinySQLException("SUBSTR third argument "
+ tcol.getString() + " must be integer > 0");
size = tcol.intValue;
}
}
argIndex++;
functionArgs.addElement(tcol);
}
} else {
/*
* Check for SYSDATE
*/
if ( name.toUpperCase().equals("SYSDATE") )
{
isConstant = true;
type = Types.DATE;
size = 10;
notNull = true;
valueSet = true;
stringValue = fmtyyyyMMdd.format(today.getTime());
/*
* Check for a quoted string
*/
} else if ( UtilString.isQuotedString(name) ) {
isConstant = true;
type = Types.CHAR;
stringValue = UtilString.removeQuotes(name);
if ( stringValue != (String)null )
{
size = stringValue.length();
notNull = true;
valueSet = true;
}
} else {
/*
* Check for a numeric constant
*/
numericType = UtilString.getValueType(name);
if ( numericType == Types.INTEGER )
{
intValue = Integer.valueOf(name).intValue();
size = 10;
type = numericType;
isConstant = true;
notNull = true;
valueSet = true;
} else if ( numericType == Types.FLOAT ) {
floatValue = Float.valueOf(name).floatValue();
size = 10;
type = numericType;
isConstant = true;
notNull = true;
valueSet = true;
} else {
/*
* This should be a column name.
*/
columnTable = (tinySQLTable)null;
upperName = name.toUpperCase();
if ( tinySQLGlobals.DEBUG )
System.out.println("Trying to find table for " + upperName);
dotAt = upperName.indexOf(".");
if ( dotAt > -1 )
{
tableName = upperName.substring(0,dotAt);
if ( tableDefs != (Hashtable)null &
tableName.indexOf("->") < 0 )
{
t = (Vector)tableDefs.get("TABLE_SELECT_ORDER");
tableName = UtilString.findTableAlias(tableName,t);
}
upperName = upperName.substring(dotAt + 1);
/*
* Check to see if this column name has a short equivalent.
*/
if ( upperName.length() > 11 )
{
longName = name;
upperName = tinySQLGlobals.getShortName(upperName);
}
columnTable = (tinySQLTable)tableDefs.get(tableName);
} else if ( tableDefs != (Hashtable)null ) {
/*
* Check to see if this column name has a short equivalent.
*/
if ( upperName.length() > 11 )
{
longName = name;
upperName = tinySQLGlobals.getShortName(upperName);
}
/*
* Use an enumeration to go through all of the tables to find
* this column.
*/
t = (Vector)tableDefs.get("TABLE_SELECT_ORDER");
for ( j = 0; j < t.size(); j++ )
{
tableName = (String)t.elementAt(j);
jtbl = (tinySQLTable)tableDefs.get(tableName);
col_keys = jtbl.column_info.keys();
/*
* Check all columns.
*/
while (col_keys.hasMoreElements())
{
checkName = (String)col_keys.nextElement();
if ( checkName.equals(upperName) )
{
upperName = checkName;
columnTable = jtbl;
break;
}
}
if ( columnTable != (tinySQLTable)null ) break;
}
} else {
if ( tinySQLGlobals.DEBUG )
System.out.println("No table definitions.");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -