📄 tscolumn.java
字号:
}
if ( columnTable != (tinySQLTable)null )
{
name = columnTable.table + "->" + columnTable.tableAlias
+ "." + upperName;
type = columnTable.ColType(upperName);
size = columnTable.ColSize(upperName);
decimalPlaces = columnTable.ColDec(upperName);
tableName = columnTable.table + "->" + columnTable.tableAlias;
}
}
}
}
}
/*
* This function sets the column to a null value if the column belongs
* to the input table, or the column is a function which has an
* argument which belongs to the input table and whose value is null
* if any argument is null.
*/
public boolean clear()
{
return clear( (String)null );
}
public boolean clear(String inputTableName)
{
int i;
tsColumn argColumn;
boolean argClear;
if ( functionName == (String)null )
{
if ( !isConstant )
{
if ( inputTableName == (String)null )
{
notNull = false;
valueSet = false;
} else if ( tableName == (String)null ) {
notNull = false;
valueSet = false;
} else if ( tableName.equals(inputTableName) ) {
notNull = false;
valueSet = false;
}
}
} else {
for ( i = 0; i < functionArgs.size(); i++ )
{
argColumn = (tsColumn)functionArgs.elementAt(i);
argClear = argColumn.clear(inputTableName);
if ( argClear & Utils.clearFunction(functionName) )
{
notNull = false;
valueSet = false;
}
}
}
return isNull();
}
/*
* This method updates the value of the column. In the case of a function
* only the argument values are updated, not the function as a whole. Functions
* must be done using updateFunctions because of the requirement
* to evaluate summary functions only once per row.
*/
public void update(String inputColumnName,String inputColumnValue)
throws tinySQLException
{
int i;
tsColumn argColumn;
if ( isConstant | inputColumnName == (String)null ) return;
if ( inputColumnName.trim().length() == 0 ) return;
if ( functionName == (String)null )
{
/*
* Only update the * column once per row.
*/
if ( name.equals("*") & valueSet ) return;
if ( inputColumnName.equals(name) | name.equals("*") )
{
if ( tinySQLGlobals.DEBUG )
System.out.println("Set " + contextToString()
+ " column " + name + " = " + inputColumnValue.trim());
/*
* If this is a simple column value, reset to null before
* trying to interpret the inputColumnValue.
*/
valueSet = true;
notNull = false;
stringValue = (String)null;
intValue = Integer.MIN_VALUE;
floatValue = Float.MIN_VALUE;
/*
* Empty string will be interpreted as nulls
*/
if ( inputColumnValue == (String)null ) return;
if ( inputColumnValue.trim().length() == 0 ) return;
notNull = true;
if ( type == Types.CHAR | type == Types.DATE | type == -1 )
{
stringValue = inputColumnValue;
} else if ( type == Types.INTEGER & notNull ) {
try
{
intValue = Integer.parseInt(inputColumnValue.trim());
} catch (Exception ex) {
throw new tinySQLException(inputColumnValue + " is not an integer.");
}
} else if ( type == Types.FLOAT & notNull ) {
try
{
floatValue = Float.valueOf(inputColumnValue.trim()).floatValue();
} catch (Exception ex) {
throw new tinySQLException(inputColumnValue + " is not a Float.");
}
}
}
} else {
/*
* Update the function arguments.
*/
for ( i = 0; i < functionArgs.size(); i++ )
{
argColumn = (tsColumn)functionArgs.elementAt(i);
argColumn.update(inputColumnName,inputColumnValue);
}
}
}
/*
* This method evaluates the value of functions. This step must be kept
* separate from the update of individual columns to prevent evaluation
* of summary functions such as COUNT and SUM more than once, or when
* the row being processed will ultimately fail a where clause condition.
*/
public void updateFunctions()
throws tinySQLException
{
int i,startAt,charCount,day,monthAt,month,year;
tsColumn argColumn;
StringBuffer concatBuffer;
FieldTokenizer ft;
String[] ftFields;
String months = "-JAN-FEB-MAR-APR-MAY-JUN-JUL-AUG-SEP-OCT-NOV-DEC-",
monthName,dayField,monthField,yearField;
if ( isConstant ) return;
if ( functionName == (String)null ) return;
if ( functionName.equals("CONCAT") )
{
concatBuffer = new StringBuffer();
for ( i = 0; i < functionArgs.size(); i++ )
{
argColumn = (tsColumn)functionArgs.elementAt(i);
argColumn.updateFunctions();
if ( argColumn.isValueSet() ) valueSet = true;
if ( argColumn.notNull )
{
concatBuffer.append(argColumn.getString());
notNull = true;
}
}
stringValue = concatBuffer.toString();
} else if ( functionName.equals("UPPER") ) {
argColumn = (tsColumn)functionArgs.elementAt(0);
argColumn.updateFunctions();
if ( argColumn.isValueSet() ) valueSet = true;
if ( argColumn.notNull )
{
stringValue = argColumn.getString().toUpperCase();
notNull = true;
}
} else if ( functionName.equals("TRIM") ) {
argColumn = (tsColumn)functionArgs.elementAt(0);
argColumn.updateFunctions();
if ( argColumn.isValueSet() ) valueSet = true;
if ( argColumn.notNull )
{
stringValue = argColumn.getString().trim();
notNull = true;
}
} else if ( functionName.equals("SUBSTR") ) {
if ( functionArgs.size() != 3 )
throw new tinySQLException("Wrong number of arguments for SUBSTR");
argColumn = (tsColumn)functionArgs.elementAt(1);
startAt = argColumn.intValue;
argColumn = (tsColumn)functionArgs.elementAt(2);
charCount = argColumn.intValue;
argColumn = (tsColumn)functionArgs.elementAt(0);
argColumn.updateFunctions();
if ( argColumn.isValueSet() ) valueSet = true;
if ( argColumn.notNull )
{
stringValue = argColumn.stringValue;
if ( startAt < stringValue.length() - 1 & charCount > 0 )
{
stringValue = stringValue.substring(startAt - 1 ,startAt + charCount - 1);
notNull = true;
} else {
stringValue = (String)null;
}
}
} else if ( functionName.equals("COUNT") ) {
argColumn = (tsColumn)functionArgs.elementAt(0);
argColumn.updateFunctions();
/*
* The COUNT function always returns a not null value
*/
notNull = true;
valueSet = true;
if ( intValue == Integer.MIN_VALUE )
{
intValue = 0;
} else {
intValue = intValue + 1;
}
} else if ( functionName.equals("TO_DATE") ) {
/*
* Validate the TO_DATE argument
*/
argColumn = (tsColumn)functionArgs.elementAt(0);
argColumn.updateFunctions();
if ( argColumn.isValueSet() ) valueSet = true;
type = Types.DATE;
size = 10;
if ( argColumn.notNull )
{
stringValue = argColumn.getString().trim();
ft = new FieldTokenizer(stringValue,'-',false);
ftFields = ft.getFields();
if ( ftFields.length < 3 )
{
throw new tinySQLException(stringValue + " is not a date with "
+ "format DD-MON-YY!");
} else {
try
{
day = Integer.parseInt(ftFields[0]);
if ( day < 1 | day > 31 )
throw new tinySQLException(stringValue + " day not "
+ "between 1 and 31.");
monthName = ftFields[1].toUpperCase();
monthAt = months.indexOf("-" + monthName + "-");
if ( monthAt == -1 )
throw new tinySQLException(stringValue + " month not "
+ "recognized.");
month = (monthAt + 4)/4;
year = Integer.parseInt(ftFields[2]);
if ( year < 0 | year > 2100 )
throw new tinySQLException(stringValue + " year not "
+ "recognized.");
/*
* Assume that years < 50 are in the 21st century, otherwise
* the 20th.
*/
if ( year < 50 )
{
year = 2000 + year;
} else if ( year < 100 ) {
year = 1900 + year;
}
dayField = Integer.toString(day);
if ( dayField.length() < 2 ) dayField = "0" + dayField;
monthField = Integer.toString(month);
if ( monthField.length() < 2 ) monthField = "0" + monthField;
yearField = Integer.toString(year);
stringValue = yearField + "-" + monthField + "-" + dayField;
} catch (Exception dayEx ) {
throw new tinySQLException(stringValue + " exception "
+ dayEx.getMessage());
}
}
notNull = true;
}
} else if ( functionName.equals("SUM") ) {
argColumn = (tsColumn)functionArgs.elementAt(0);
argColumn.updateFunctions();
if ( argColumn.isValueSet() ) valueSet = true;
if ( argColumn.type == Types.CHAR | argColumn.type == Types.DATE )
throw new tinySQLException(argColumn.name + " is not numeric!");
if ( argColumn.notNull )
{
notNull = true;
if ( floatValue == Float.MIN_VALUE )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -