/** * Converter from a numeric object to Long. Input is checked to be within range represented by * Long. */ static Long convertToLong(Object a) throws HsqlException { if (a instanceof Integer) { return ValuePool.getLong(((Integer) a).intValue()); } else if (a instanceof Long) { return (Long) a; } else if (a instanceof BigDecimal) { BigDecimal bd = (BigDecimal) a; if (bd.compareTo(MAX_LONG) > 0 || bd.compareTo(MIN_LONG) < 0) { throw Error.error(ErrorCode.X_22003); } return ValuePool.getLong(bd.longValue()); } else if (a instanceof Double || a instanceof Float) { double d = ((Number) a).doubleValue(); if (Double.isInfinite(d) || Double.isNaN(d) || d >= (double) Long.MAX_VALUE + 1 || d <= (double) Long.MIN_VALUE - 1) { throw Error.error(ErrorCode.X_22003); } return ValuePool.getLong((long) d); } else { throw Error.error(ErrorCode.X_42561); } }
public static double toDouble(Object a) throws HsqlException { double value; if (a instanceof java.lang.Double) { return ((Double) a).doubleValue(); } else if (a instanceof BigDecimal) { BigDecimal bd = (BigDecimal) a; value = bd.doubleValue(); int signum = bd.signum(); BigDecimal bdd = new BigDecimal(value + signum); if (bdd.compareTo(bd) != signum) { throw Error.error(ErrorCode.X_22003); } } else if (a instanceof Number) { value = ((Number) a).doubleValue(); } else { throw Error.error(ErrorCode.X_22501); } return value; }
public Object floor(Object a) throws HsqlException { if (a == null) { return null; } switch (typeCode) { case Types.SQL_REAL: case Types.SQL_FLOAT: case Types.SQL_DOUBLE: { double value = Math.floor(((Double) a).doubleValue()); if (Double.isInfinite(value)) { throw Error.error(ErrorCode.X_22003); } return ValuePool.getDouble(Double.doubleToLongBits(value)); } case Types.SQL_NUMERIC: case Types.SQL_DECIMAL: { BigDecimal value = ((BigDecimal) a).setScale(0, BigDecimal.ROUND_FLOOR); if (JavaSystem.precision(value) > precision) { throw Error.error(ErrorCode.X_22003); } } // fall through default: return a; } }
long readBigint() { boolean minus = false; if (token.tokenType == Tokens.MINUS) { minus = true; read(); } checkIsValue(); if (minus && token.dataType.typeCode == Types.SQL_NUMERIC && LONG_MAX_VALUE_INCREMENT.equals(token.tokenValue)) { read(); return Long.MIN_VALUE; } if (token.dataType.typeCode != Types.SQL_INTEGER && token.dataType.typeCode != Types.SQL_BIGINT) { throw Error.error(ErrorCode.X_42565); } long val = ((Number) token.tokenValue).longValue(); if (minus) { val = -val; } read(); return val; }
public String getNameString() { switch (typeCode) { case Types.TINYINT: return Tokens.T_TINYINT; case Types.SQL_SMALLINT: return Tokens.T_SMALLINT; case Types.SQL_INTEGER: return Tokens.T_INTEGER; case Types.SQL_BIGINT: return Tokens.T_BIGINT; case Types.SQL_REAL: return Tokens.T_REAL; case Types.SQL_FLOAT: return Tokens.T_FLOAT; case Types.SQL_DOUBLE: return Tokens.T_DOUBLE; case Types.SQL_NUMERIC: return Tokens.T_NUMERIC; case Types.SQL_DECIMAL: return Tokens.T_DECIMAL; default: throw Error.runtimeError(ErrorCode.U_S0500, "NumberType"); } }
void prepareCheckConstraint(Session session, Table table, boolean checkValues) { // to ensure no subselects etc. are in condition check.checkValidCheckConstraint(); if (table == null) { check.resolveTypes(session, null); } else { QuerySpecification s = Expression.getCheckSelect(session, table, check); Result r = s.getResult(session, 1); if (r.getNavigator().getSize() != 0) { String[] info = new String[] {table.getName().name, ""}; throw Error.error(ErrorCode.X_23504, ErrorCode.CONSTRAINT, info); } rangeVariable = s.rangeVariables[0]; // removes reference to the Index object in range variable rangeVariable.setForCheckConstraint(); } if (check.getType() == OpTypes.NOT && check.getLeftNode().getType() == OpTypes.IS_NULL && check.getLeftNode().getLeftNode().getType() == OpTypes.COLUMN) { notNullColumnIndex = check.getLeftNode().getLeftNode().getColumnIndex(); isNotNull = true; } }
int readInteger() { boolean minus = false; if (token.tokenType == Tokens.MINUS) { minus = true; read(); } checkIsValue(); if (minus && token.dataType.typeCode == Types.SQL_BIGINT && ((Number) token.tokenValue).longValue() == -(long) Integer.MIN_VALUE) { read(); return Integer.MIN_VALUE; } if (token.dataType.typeCode != Types.SQL_INTEGER) { throw Error.error(ErrorCode.X_42565); } int val = ((Number) token.tokenValue).intValue(); if (minus) { val = -val; } read(); return val; }
boolean hasColumnOnly(int colIndex) { switch (constType) { case CHECK: return rangeVariable.usedColumns[colIndex] && ArrayUtil.countTrueElements(rangeVariable.usedColumns) == 1; case PRIMARY_KEY: case UNIQUE: return core.mainCols.length == 1 && core.mainCols[0] == colIndex; case MAIN: return core.mainCols.length == 1 && core.mainCols[0] == colIndex && core.mainTable == core.refTable; case FOREIGN_KEY: return core.refCols.length == 1 && core.refCols[0] == colIndex && core.mainTable == core.refTable; default: throw Error.runtimeError(ErrorCode.U_S0500, "Constraint"); } }
public static NumberType getNumberType(int type, long precision, int scale) { switch (type) { case Types.SQL_INTEGER: return SQL_INTEGER; case Types.SQL_SMALLINT: return SQL_SMALLINT; case Types.SQL_BIGINT: return SQL_BIGINT; case Types.TINYINT: return TINYINT; case Types.SQL_REAL: case Types.SQL_DOUBLE: return SQL_DOUBLE; case Types.SQL_NUMERIC: case Types.SQL_DECIMAL: return new NumberType(type, precision, scale); default: throw Error.runtimeError(ErrorCode.U_S0500, "NumberType"); } }
/* * Tests a row against this CHECK constraint. */ void checkCheckConstraint(Session session, Table table, Object[] data) { /* if (session.compiledStatementExecutor.rangeIterators[1] == null) { session.compiledStatementExecutor.rangeIterators[1] = rangeVariable.getIterator(session); } */ RangeIteratorBase it = (RangeIteratorBase) session.sessionContext.getCheckIterator(); if (it == null) { it = rangeVariable.getIterator(session); session.sessionContext.setCheckIterator(it); } it.currentData = data; boolean nomatch = Boolean.FALSE.equals(check.getValue(session)); it.currentData = null; if (nomatch) { String[] info = new String[] {name.name, table.tableName.name}; throw Error.error(ErrorCode.X_23504, ErrorCode.CONSTRAINT, info); } }
public boolean isNegative(Object a) throws HsqlException { if (a == null) { return false; } switch (typeCode) { case Types.SQL_REAL: case Types.SQL_FLOAT: case Types.SQL_DOUBLE: { double ad = ((Number) a).doubleValue(); return ad < 0; } case Types.SQL_NUMERIC: case Types.SQL_DECIMAL: return ((BigDecimal) a).signum() < 0; case Types.TINYINT: case Types.SQL_SMALLINT: case Types.SQL_INTEGER: return ((Number) a).intValue() < 0; case Types.SQL_BIGINT: return ((Number) a).longValue() < 0; default: throw Error.runtimeError(ErrorCode.U_S0500, "NumberType"); } }
Result getResult(Session session) { switch (type) { case StatementTypes.SIGNAL: case StatementTypes.RESIGNAL: // todo case StatementTypes.ITERATE: case StatementTypes.LEAVE: case StatementTypes.RETURN: case StatementTypes.CONDITION: return this.getResultValue(session); case StatementTypes.ASSIGNMENT: { Object[] variables = variable.getType() == SchemaObject.PARAMETER ? session.sessionContext.routineArguments : session.sessionContext.routineVariables; try { Object o = expression.getValue(session, variable.getDataType()); variables[variableIndex] = o; return Result.updateZeroResult; } catch (HsqlException e) { return Result.newErrorResult(e); } } default: throw Error.runtimeError(ErrorCode.U_S0500, ""); } }
public CachedObject get(RowInputInterface in) { try { return new RowAVLDisk(table, in); } catch (IOException e) { throw Error.error(ErrorCode.DATA_FILE_ERROR, e); } }
void checkCheckConstraint(Session session, Table table, Object data) { session.sessionData.currentValue = data; boolean nomatch = Boolean.FALSE.equals(check.getValue(session)); session.sessionData.currentValue = null; if (nomatch) { if (table == null) { throw Error.error(ErrorCode.X_23504, name.name); } else { String[] info = new String[] {name.name, table.tableName.name}; throw Error.error(ErrorCode.X_23504, ErrorCode.CONSTRAINT, info); } } }
/** Converts a value to this type */ public Object convertToDefaultType(SessionInterface session, Object a) throws HsqlException { if (a == null) { return a; } Type otherType; if (a instanceof Number) { if (a instanceof BigInteger) { a = new BigDecimal((BigInteger) a); } else if (a instanceof Float) { a = new Double(((Float) a).doubleValue()); } else if (a instanceof Byte) { a = ValuePool.getInt(((Byte) a).intValue()); } else if (a instanceof Short) { a = ValuePool.getInt(((Short) a).intValue()); } if (a instanceof Integer) { otherType = Type.SQL_INTEGER; } else if (a instanceof Long) { otherType = Type.SQL_BIGINT; } else if (a instanceof Double) { otherType = Type.SQL_DOUBLE; } else if (a instanceof BigDecimal) { if (typeCode == Types.SQL_DECIMAL || typeCode == Types.SQL_NUMERIC) { return convertToTypeLimits(a); } BigDecimal val = (BigDecimal) a; otherType = getNumberType(Types.SQL_DECIMAL, JavaSystem.precision(val), scale); } else { throw Error.error(ErrorCode.X_42561); } } else if (a instanceof String) { otherType = Type.SQL_VARCHAR; } else { throw Error.error(ErrorCode.X_42561); } return convertToType(session, a, otherType); }
static int getExpressionType(int tokenT) { int type = expressionTypeMap.get(tokenT, -1); if (type == -1) { throw Error.runtimeError(ErrorCode.U_S0500, "Parser"); } return type; }
public void resetAccessorKeys(Index[] keys) { if (indexList.length == 0 || indexList[0] == null || accessorList[0] == null) { indexList = keys; accessorList = new CachedObject[indexList.length]; return; } throw Error.runtimeError(ErrorCode.U_S0500, "RowStoreAVLDisk"); }
void read() { scanner.scanNext(); if (token.isMalformed) { int errorCode = -1; switch (token.tokenType) { case Tokens.X_MALFORMED_BINARY_STRING: errorCode = ErrorCode.X_42587; break; case Tokens.X_MALFORMED_BIT_STRING: errorCode = ErrorCode.X_42588; break; case Tokens.X_MALFORMED_UNICODE_STRING: errorCode = ErrorCode.X_42586; break; case Tokens.X_MALFORMED_STRING: errorCode = ErrorCode.X_42584; break; case Tokens.X_UNKNOWN_TOKEN: errorCode = ErrorCode.X_42582; break; case Tokens.X_MALFORMED_NUMERIC: errorCode = ErrorCode.X_42585; break; case Tokens.X_MALFORMED_COMMENT: errorCode = ErrorCode.X_42589; break; case Tokens.X_MALFORMED_IDENTIFIER: errorCode = ErrorCode.X_42583; break; } throw Error.error(errorCode); } if (isRecording) { Token dup = token.duplicate(); dup.position = scanner.getTokenPosition(); recordedStatement.add(dup); } }
HsqlException unexpectedToken() { if (token.tokenType == Tokens.X_ENDPARSE) { return Error.error(ErrorCode.X_42590); } String tokenS; if (token.charsetSchema != null) { tokenS = token.charsetSchema; } else if (token.charsetName != null) { tokenS = token.charsetName; } else if (token.namePrePrefix != null) { tokenS = token.namePrePrefix; } else if (token.namePrefix != null) { tokenS = token.namePrefix; } else { tokenS = token.tokenString; } return Error.error(ErrorCode.X_42581, tokenS); }
/** @todo - review usage to see if range enforcement / java type conversion is necessary */ public Object convertToTypeLimits(Object a) throws HsqlException { if (a == null) { return null; } switch (typeCode) { case Types.TINYINT: case Types.SQL_SMALLINT: case Types.SQL_INTEGER: case Types.SQL_BIGINT: return a; case Types.SQL_REAL: case Types.SQL_FLOAT: case Types.SQL_DOUBLE: return a; case Types.SQL_NUMERIC: case Types.SQL_DECIMAL: BigDecimal dec = (BigDecimal) a; if (scale != dec.scale()) { dec = dec.setScale(scale, BigDecimal.ROUND_HALF_DOWN); } int valuePrecision = JavaSystem.precision(dec); if (valuePrecision > precision) { throw Error.error(ErrorCode.X_22003); } return dec; default: throw Error.runtimeError(ErrorCode.U_S0500, "NumberType"); } }
HsqlException tooManyIdentifiers() { String tokenS; if (token.namePrePrefix != null) { tokenS = token.namePrePrefix; } else if (token.namePrefix != null) { tokenS = token.namePrefix; } else { tokenS = token.tokenString; } return Error.error(ErrorCode.X_42551, tokenS); }
HsqlException unexpectedTokenRequire(String required) { if (token.tokenType == Tokens.X_ENDPARSE) { return Error.error(ErrorCode.X_42590, ErrorCode.TOKEN_REQUIRED, new Object[] {"", required}); } String tokenS; if (token.charsetSchema != null) { tokenS = token.charsetSchema; } else if (token.charsetName != null) { tokenS = token.charsetName; } else if (token.namePrePrefix != null) { tokenS = token.namePrePrefix; } else if (token.namePrefix != null) { tokenS = token.namePrefix; } else { tokenS = token.tokenString; } return Error.error( ErrorCode.X_42581, ErrorCode.TOKEN_REQUIRED, new Object[] {tokenS, required}); }
public Object add(Object a, Object b, Type otherType) throws HsqlException { if (a == null || b == null) { return null; } switch (typeCode) { case Types.SQL_REAL: case Types.SQL_FLOAT: case Types.SQL_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.SQL_NUMERIC: case Types.SQL_DECIMAL: { a = convertToDefaultType(null, a); b = convertToDefaultType(null, b); BigDecimal abd = (BigDecimal) a; BigDecimal bbd = (BigDecimal) b; return abd.add(bbd); } case Types.TINYINT: case Types.SQL_SMALLINT: case Types.SQL_INTEGER: { int ai = ((Number) a).intValue(); int bi = ((Number) b).intValue(); return ValuePool.getInt(ai + bi); } case Types.SQL_BIGINT: { long longa = ((Number) a).longValue(); long longb = ((Number) b).longValue(); return ValuePool.getLong(longa + longb); } default: throw Error.runtimeError(ErrorCode.U_S0500, "NumberType"); } }
public Type getAggregateType(Type other) throws HsqlException { if (this == other) { return this; } if (other.isCharacterType()) { return other.getAggregateType(this); } switch (other.typeCode) { case Types.SQL_ALL_TYPES: return this; case Types.SQL_REAL: case Types.SQL_FLOAT: case Types.SQL_DOUBLE: case Types.SQL_NUMERIC: case Types.SQL_DECIMAL: case Types.TINYINT: case Types.SQL_SMALLINT: case Types.SQL_INTEGER: case Types.SQL_BIGINT: break; default: throw Error.error(ErrorCode.X_42562); } if (typeWidth == DOUBLE_WIDTH) { return this; } if (((NumberType) other).typeWidth == DOUBLE_WIDTH) { return other; } if (typeWidth <= BIGINT_WIDTH && ((NumberType) other).typeWidth <= BIGINT_WIDTH) { return (typeWidth > ((NumberType) other).typeWidth) ? this : other; } int newScale = scale > other.scale ? scale : other.scale; long newDigits = precision - scale > other.precision - other.scale ? precision - scale : other.precision - other.scale; return getNumberType(Types.SQL_DECIMAL, newDigits + newScale, newScale); }
public String convertToString(Object a) { if (a == null) { return null; } switch (this.typeCode) { case Types.TINYINT: case Types.SQL_SMALLINT: case Types.SQL_INTEGER: case Types.SQL_BIGINT: return a.toString(); case Types.SQL_REAL: case Types.SQL_DOUBLE: double value = ((Double) a).doubleValue(); // todo - java 5 format change if (value == Double.NEGATIVE_INFINITY) { return "-1E0/0"; } if (value == Double.POSITIVE_INFINITY) { return "1E0/0"; } if (Double.isNaN(value)) { return "0E0/0E0"; } String s = Double.toString(value); // ensure the engine treats the value as a DOUBLE, not DECIMAL if (s.indexOf('E') < 0) { s = s.concat("E0"); } return s; case Types.SQL_NUMERIC: case Types.SQL_DECIMAL: return JavaSystem.toString((BigDecimal) a); default: throw Error.runtimeError(ErrorCode.U_S0500, "NumberType"); } }
/** * For the candidate table row, finds any referring node in the main table. This is used to check * referential integrity when updating a node. We have to make sure that the main table still * holds a valid main record. returns true If a valid row is found, false if there are null in the * data Otherwise a 'INTEGRITY VIOLATION' Exception gets thrown. */ boolean checkHasMainRef(Session session, Object[] row) { if (ArrayUtil.hasNull(row, core.refCols)) { return false; } PersistentStore store = session.sessionData.getRowStore(core.mainTable); boolean exists = core.mainIndex.exists(session, store, row, core.refCols); if (!exists) { String[] info = new String[] {core.refName.name, core.mainTable.getName().name}; throw Error.error(ErrorCode.X_23502, ErrorCode.CONSTRAINT, info); } return exists; }
StatementSession(int type, HsqlName[] readNames, HsqlName[] writeNames) { super(type); this.isTransactionStatement = true; this.readTableNames = readNames; writeTableNames = writeNames; switch (type) { case StatementTypes.TRANSACTION_LOCK_TABLE: group = StatementTypes.X_HSQLDB_TRANSACTION; break; default: throw Error.runtimeError(ErrorCode.U_S0500, "StatementCommand"); } }
/** * Converter from a numeric object to Integer. Input is checked to be within range represented by * the given number type. */ static Integer convertToInt(Object a, int type) throws HsqlException { int value; if (a instanceof Integer) { if (type == Types.SQL_INTEGER) { return (Integer) a; } value = ((Integer) a).intValue(); } else if (a instanceof Long) { long temp = ((Long) a).longValue(); if (Integer.MAX_VALUE < temp || temp < Integer.MIN_VALUE) { throw Error.error(ErrorCode.X_22003); } value = (int) temp; } else if (a instanceof BigDecimal) { BigDecimal bd = ((BigDecimal) a); if (bd.compareTo(MAX_INT) > 0 || bd.compareTo(MIN_INT) < 0) { throw Error.error(ErrorCode.X_22003); } value = bd.intValue(); } else if (a instanceof Double || a instanceof Float) { double d = ((Number) a).doubleValue(); if (Double.isInfinite(d) || Double.isNaN(d) || d >= (double) Integer.MAX_VALUE + 1 || d <= (double) Integer.MIN_VALUE - 1) { throw Error.error(ErrorCode.X_22003); } value = (int) d; } else { throw Error.error(ErrorCode.X_42561); } if (type == Types.TINYINT) { if (Byte.MAX_VALUE < value || value < Byte.MIN_VALUE) { throw Error.error(ErrorCode.X_22003); } } else if (type == Types.SQL_SMALLINT) { if (Short.MAX_VALUE < value || value < Short.MIN_VALUE) { throw Error.error(ErrorCode.X_22003); } } return ValuePool.getInt(value); }
boolean hasColumn(int colIndex) { switch (constType) { case CHECK: return rangeVariable.usedColumns[colIndex]; case PRIMARY_KEY: case UNIQUE: case MAIN: return ArrayUtil.find(core.mainCols, colIndex) != -1; case FOREIGN_KEY: return ArrayUtil.find(core.refCols, colIndex) != -1; default: throw Error.runtimeError(ErrorCode.U_S0500, "Constraint"); } }
private static BigDecimal convertToDecimal(Object a) { if (a instanceof BigDecimal) { return (BigDecimal) a; } else if (a instanceof Integer || a instanceof Long) { return BigDecimal.valueOf(((Number) a).longValue()); } else if (a instanceof Double) { double value = ((Number) a).doubleValue(); if (Double.isInfinite(value) || Double.isNaN(value)) { return null; } return new BigDecimal(value); } else { throw Error.runtimeError(ErrorCode.U_S0500, "NumberType"); } }