/**
   * Retrieves whether values for the designated parameter can be signed numbers.
   *
   * @param param the first parameter is 1, the second is 2, ...
   * @return <code>true</code> if so; <code>false</code> otherwise
   * @exception SQLException if a database access error occurs
   * @since JDK 1.4, HSQLDB 1.7.2
   */
  public boolean isSigned(int param) throws SQLException {

    checkRange(param);

    Type type = translateType(rmd.columnTypes[--param]);

    return type.isNumberType();
  }
  /**
   * Retrieves the designated parameter's SQL type.
   *
   * @param param the first parameter is 1, the second is 2, ...
   * @return SQL type from <code>java.sql.Types</code>
   * @exception SQLException if a database access error occurs
   * @since JDK 1.4, HSQLDB 1.7.2
   * @see java.sql.Types
   */
  public int getParameterType(int param) throws SQLException {

    checkRange(param);

    Type type = translateType(rmd.columnTypes[--param]);

    return type.getJDBCTypeCode();
  }
  /**
   * Retrieves the fully-qualified name of the Java class whose instances should be passed to the
   * method <code>PreparedStatement.setObject</code>.
   *
   * @param param the first parameter is 1, the second is 2, ...
   * @return the fully-qualified name of the class in the Java programming language that would be
   *     used by the method <code>PreparedStatement.setObject</code> to set the value in the
   *     specified parameter. This is the class name used for custom mapping.
   * @exception SQLException if a database access error occurs
   * @since JDK 1.4, HSQLDB 1.7.2
   */
  public String getParameterClassName(int param) throws SQLException {

    checkRange(param);

    Type type = translateType(rmd.columnTypes[--param]);

    return type.getJDBCClassName();
  }
  /** Translates an INTERVAL type to VARCHAR. Removes time zone from datetime types. */
  private Type translateType(Type type) {

    if (this.translateTTIType) {
      if (type.isIntervalType()) {
        type = ((IntervalType) type).getCharacterType();
      } else if (type.isDateTimeTypeWithZone()) {
        type = ((DateTimeType) type).getDateTimeTypeWithoutZone();
      }
    }

    return type;
  }
Esempio n. 5
0
 public Constraint(
     HsqlNameManager.HsqlName paramHsqlName, Table paramTable, Index paramIndex, int paramInt) {
   this.name = paramHsqlName;
   this.constType = paramInt;
   this.core = new ConstraintCore();
   this.core.mainTable = paramTable;
   this.core.mainIndex = paramIndex;
   this.core.mainCols = paramIndex.getColumns();
   for (int i = 0; i < this.core.mainCols.length; i++) {
     Type localType = paramTable.getColumn(this.core.mainCols[i]).getDataType();
     if (localType.isLobType()) throw Error.error(5534);
   }
 }
  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);
  }
Esempio n. 7
0
  @Override
  public String getBaseTypeName() throws SQLException {

    checkClosed();

    return elementType.getNameString();
  }
Esempio n. 8
0
  @Override
  public int getBaseType() throws SQLException {

    checkClosed();

    return elementType.getJDBCTypeCode();
  }
Esempio n. 9
0
  /**
   * Convert type for JDBC reads. Same as convertToType, but supports non-standard SQL conversions
   * supported by JDBC
   */
  public Object convertToTypeJDBC(SessionInterface session, Object a, Type otherType) {

    if (otherType.isLobType()) {
      throw Error.error(ErrorCode.X_42561);
    }

    return convertToType(session, a, otherType);
  }
  Type readDataType(RowInputBinary in) throws IOException {

    int typeCode = in.readType();
    long size = in.readLong();
    int scale = in.readInt();

    return Type.getType(typeCode, 0, size, scale);
  }
  Object getValue(Session session, Type type) {

    if (dataType == type || valueData == null) {
      return valueData;
    }

    return type.convertToType(session, valueData, dataType);
  }
  /**
   * Retrieves the designated parameter's specified column size.
   *
   * <p>The returned value represents the maximum column size for the given parameter. For numeric
   * data, this is the maximum precision. For character data, this is the length in characters. For
   * datetime datatypes, this is the length in characters of the String representation (assuming the
   * maximum allowed precision of the fractional seconds component). For binary data, this is the
   * length in bytes. For the ROWID datatype, this is the length in bytes. 0 is returned for data
   * types where the column size is not applicable.
   *
   * @param param the first parameter is 1, the second is 2, ...
   * @return precision
   * @exception SQLException if a database access error occurs
   * @since JDK 1.4, HSQLDB 1.7.2
   */
  public int getPrecision(int param) throws SQLException {

    checkRange(param);

    Type type = translateType(rmd.columnTypes[--param]);

    if (type.isDateTimeType()) {
      return type.displaySize();
    } else {
      long size = type.precision;

      if (size > Integer.MAX_VALUE) {
        size = 0;
      }

      return (int) size;
    }
  }
  /**
   * Moves the data from an old store to new after changes to table The colindex argument is the
   * index of the column that was added or removed. The adjust argument is {-1 | 0 | +1}
   */
  public final void moveData(Session session, PersistentStore other, int colindex, int adjust) {

    Object colvalue = null;
    Type oldtype = null;
    Type newtype = null;

    if (adjust >= 0 && colindex != -1) {
      ColumnSchema column = ((Table) table).getColumn(colindex);

      colvalue = column.getDefaultValue(session);

      if (adjust == 0) {
        oldtype = ((Table) other.getTable()).getColumnTypes()[colindex];
        newtype = ((Table) table).getColumnTypes()[colindex];
      }
    }

    RowIterator it = other.rowIterator();
    Table table = (Table) this.table;

    try {
      while (it.hasNext()) {
        Row row = it.getNextRow();
        Object[] o = row.getData();
        Object[] data = table.getEmptyRowData();

        if (adjust == 0 && colindex != -1) {
          colvalue = newtype.convertToType(session, o[colindex], oldtype);
        }

        ArrayUtil.copyAdjustArray(o, data, colvalue, colindex, adjust);
        table.systemSetIdentityColumn(session, data);
        table.enforceTypeLimits(session, data);
        table.enforceRowConstraints(session, data);

        // get object without RowAction
        Row newrow = (Row) getNewCachedObject(null, data);

        indexRow(null, newrow);
      }
    } catch (java.lang.OutOfMemoryError e) {
      throw Error.error(ErrorCode.OUT_OF_MEMORY);
    }
  }
  public int precedenceDegree(Type other) {

    if (other.isNumberType()) {
      int otherWidth = ((NumberType) other).typeWidth;

      return otherWidth - typeWidth;
    }

    return Integer.MIN_VALUE;
  }
  public boolean canConvertFrom(Type otherType) {

    if (otherType.typeCode == Types.SQL_ALL_TYPES) {
      return true;
    }

    if (otherType.isNumberType()) {
      return true;
    }

    if (otherType.isIntervalType()) {
      return true;
    }

    if (otherType.isCharacterType()) {
      return true;
    }

    return false;
  }
Esempio n. 16
0
  public static Type getAggregateType(Type add, Type existing) {

    if (existing == null || existing.typeCode == Types.SQL_ALL_TYPES) {
      return add;
    }

    if (add == null || add.typeCode == Types.SQL_ALL_TYPES) {
      return existing;
    }

    return existing.getAggregateType(add);
  }
Esempio n. 17
0
  public static Type getAggregatedType(Type add, Type existing) throws HsqlException {

    if (existing == null || existing.type == Types.SQL_ALL_TYPES) {
      return add;
    }

    if (add == null || add.type == Types.SQL_ALL_TYPES) {
      return existing;
    }

    return existing.getAggregateType(add);
  }
Esempio n. 18
0
  Object getValue(Session session, Type type, boolean isRasRoot) {

    if (!rasType.isEmpty()) {
      if (!isRasRoot) {
        return valueData + rasType;
      }
    }

    if (dataType == type || valueData == null) {
      return valueData;
    }

    return type.convertToType(session, valueData, dataType);
  }
Esempio n. 19
0
 void setColumnsIndexes(Table paramTable) {
   int i;
   Type localType;
   if (this.constType == 0) {
     if (this.mainColSet == null) {
       this.core.mainCols = this.core.mainTable.getPrimaryKey();
       if (this.core.mainCols == null) throw Error.error(5581);
     } else if (this.core.mainCols == null) {
       this.core.mainCols = this.core.mainTable.getColumnIndexes(this.mainColSet);
     }
     if (this.core.refCols == null)
       this.core.refCols = paramTable.getColumnIndexes(this.refColSet);
     for (i = 0; i < this.core.refCols.length; i++) {
       localType = paramTable.getColumn(this.core.refCols[i]).getDataType();
       if (localType.isLobType()) throw Error.error(5534);
     }
   } else if (this.mainColSet != null) {
     this.core.mainCols = paramTable.getColumnIndexes(this.mainColSet);
     for (i = 0; i < this.core.mainCols.length; i++) {
       localType = paramTable.getColumn(this.core.mainCols[i]).getDataType();
       if (localType.isLobType()) throw Error.error(5534);
     }
   }
 }
  public void resolveTypes(Session session, Expression parent) {

    switch (opType) {
      case OpTypes.DEFAULT:
        if (parent != null && parent.opType != OpTypes.ROW) {
          throw Error.error(ErrorCode.X_42544);
        }
        break;

      case OpTypes.COALESCE:
        {
          Type type = null;

          for (int i = 0; i < nodes.length; i++) {
            type = Type.getAggregateType(nodes[i].dataType, type);
          }

          dataType = type;

          break;
        }
    }
  }
  public Object convertToType(SessionInterface session, Object a, Type otherType)
      throws HsqlException {

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

    if (otherType.typeCode == typeCode) {
      switch (typeCode) {
        case Types.SQL_NUMERIC:
        case Types.SQL_DECIMAL:
          if (otherType.scale == scale && otherType.precision <= precision) {
            return a;
          }
          break;

        default:
          return a;
      }
    }

    if (otherType.isIntervalType()) {
      int endType = ((IntervalType) otherType).endIntervalType;

      switch (endType) {
        case Types.SQL_INTERVAL_YEAR:
        case Types.SQL_INTERVAL_MONTH:
        case Types.SQL_INTERVAL_DAY:
        case Types.SQL_INTERVAL_HOUR:
        case Types.SQL_INTERVAL_MINUTE:
          {
            Long value = ValuePool.getLong(((IntervalType) otherType).convertToLong(a));

            return convertToType(session, value, Type.SQL_BIGINT);
          }
        case Types.SQL_INTERVAL_SECOND:
          {
            long seconds = ((IntervalSecondData) a).units;
            long nanos = ((IntervalSecondData) a).nanos;
            BigDecimal value = ((DTIType) otherType).getSecondPart(seconds, nanos);

            return value;
          }
      }
    }

    switch (otherType.typeCode) {
      case Types.SQL_CLOB:
        a = ((ClobData) a).getSubString(0L, (int) ((ClobData) a).length());

        // fall through
      case Types.SQL_CHAR:
      case Types.SQL_VARCHAR:
      case Types.VARCHAR_IGNORECASE:
        {
          a = session.getScanner().convertToNumber((String) a, this);

          return convertToDefaultType(session, a);
        }
      case Types.TINYINT:
      case Types.SQL_SMALLINT:
      case Types.SQL_INTEGER:
      case Types.SQL_BIGINT:
      case Types.SQL_REAL:
      case Types.SQL_FLOAT:
      case Types.SQL_DOUBLE:
      case Types.SQL_NUMERIC:
      case Types.SQL_DECIMAL:
        break;

      default:
        throw Error.error(ErrorCode.X_42561);
    }

    switch (this.typeCode) {
      case Types.TINYINT:
      case Types.SQL_SMALLINT:
      case Types.SQL_INTEGER:
        return convertToInt(a, this.typeCode);

      case Types.SQL_BIGINT:
        return convertToLong(a);

      case Types.SQL_REAL:
      case Types.SQL_FLOAT:
      case Types.SQL_DOUBLE:
        return convertToDouble(a);

      case Types.SQL_NUMERIC:
      case Types.SQL_DECIMAL:
        BigDecimal value = convertToDecimal(a);

        return convertToTypeLimits(value);

      default:
        throw Error.error(ErrorCode.X_42561);
    }
  }
  /**
   * Returns a SQL type "wide" enough to represent the result of the expression.<br>
   * A type is "wider" than the other if it can represent all its numeric values.<br>
   * Arithmetic operation terms are promoted to a type that can represent the resulting values and
   * avoid incorrect results.
   *
   * <p>FLOAT/REAL/DOUBLE used in an operation results in the same type, regardless of the type of
   * the other operand. When the result or the expression is converted to the type of the target
   * column for storage, an exception is thrown if the resulting value cannot be stored in the
   * column
   *
   * <p>Types narrower than INTEGER (int) are promoted to INTEGER. The order of promotion is as
   * follows
   *
   * <p>INTEGER, BIGINT, NUMERIC/DECIMAL
   *
   * <p>TINYINT and SMALLINT in any combination return INTEGER<br>
   * TINYINT/SMALLINT/INTEGER and INTEGER return BIGINT<br>
   * TINYINT/SMALLINT/INTEGER and BIGINT return NUMERIC/DECIMAL<br>
   * BIGINT and BIGINT return NUMERIC/DECIMAL<br>
   * REAL/FLOAT/DOUBLE and any type return REAL/FLOAT/DOUBLE<br>
   * NUMERIC/DECIMAL any type other than REAL/FLOAT/DOUBLE returns NUMERIC/DECIMAL<br>
   * In the case of NUMERIC/DECIMAL returned, the result precision is always large enough to express
   * any value result, while the scale depends on the operation:<br>
   * For ADD/SUBTRACT/DIVIDE, the scale is the larger of the two<br>
   * For MULTIPLY, the scale is the sum of the two scales<br>
   */
  public Type getCombinedType(Type other, int operation) throws HsqlException {

    if (other.typeCode == Types.SQL_ALL_TYPES) {
      other = this;
    }

    switch (operation) {
      case OpTypes.ADD:
        break;

      case OpTypes.MULTIPLY:
        if (other.isIntervalType()) {
          return other.getCombinedType(this, OpTypes.MULTIPLY);
        }
        break;

      case OpTypes.DIVIDE:
      case OpTypes.SUBTRACT:
      default:

        // all derivatives of equality ops or comparison ops
        return getAggregateType(other);
    }

    // resolution for ADD and MULTIPLY only
    if (!other.isNumberType()) {
      throw Error.error(ErrorCode.X_42562);
    }

    if (typeWidth == DOUBLE_WIDTH || ((NumberType) other).typeWidth == DOUBLE_WIDTH) {
      return Type.SQL_DOUBLE;
    }

    int sum = typeWidth + ((NumberType) other).typeWidth;

    if (sum <= INTEGER_WIDTH) {
      return Type.SQL_INTEGER;
    }

    if (sum <= BIGINT_WIDTH) {
      return Type.SQL_BIGINT;
    }

    int newScale;
    long newDigits;

    switch (operation) {

        //            case OpCodes.DIVIDE :
        //            case OpCodes.SUBTRACT :
      case OpTypes.ADD:
        newScale = scale > other.scale ? scale : other.scale;
        newDigits =
            precision - scale > other.precision - other.scale
                ? precision - scale
                : other.precision - other.scale;

        newDigits++;
        break;

      case OpTypes.MULTIPLY:
        newDigits = precision - scale + other.precision - other.scale;
        newScale = scale + other.scale;
        break;

      default:
        throw Error.runtimeError(ErrorCode.U_S0500, "NumberType");
    }

    return getNumberType(Types.SQL_DECIMAL, newScale + newDigits, newScale);
  }
    private void getFirstRow() {

      if (currentJoinData == null
          || currentJoinData.length < conditions[condIndex].indexedColumnCount) {
        currentJoinData = new Object[conditions[condIndex].indexedColumnCount];
      }

      for (int i = 0; i < conditions[condIndex].indexedColumnCount; i++) {
        int range = 0;
        int opType =
            i == conditions[condIndex].indexedColumnCount - 1
                ? conditions[condIndex].opType
                : conditions[condIndex].indexCond[i].getType();

        if (opType == OpTypes.IS_NULL || opType == OpTypes.NOT || opType == OpTypes.MAX) {
          currentJoinData[i] = null;

          continue;
        }

        Type valueType = conditions[condIndex].indexCond[i].getRightNode().getDataType();
        Object value = conditions[condIndex].indexCond[i].getRightNode().getValue(session);
        Type targetType = conditions[condIndex].indexCond[i].getLeftNode().getDataType();

        if (targetType != valueType) {
          range = targetType.compareToTypeRange(value);

          if (range == 0) {
            if (targetType.typeComparisonGroup != valueType.typeComparisonGroup) {
              value = targetType.convertToType(session, value, valueType);
            }
          }
        }

        if (i == 0) {
          int exprType = conditions[condIndex].indexCond[0].getType();

          if (range < 0) {
            switch (exprType) {
              case OpTypes.GREATER:
              case OpTypes.GREATER_EQUAL:
              case OpTypes.GREATER_EQUAL_PRE:
                value = null;
                break;

              default:
                it = conditions[condIndex].rangeIndex.emptyIterator();

                return;
            }
          } else if (range > 0) {
            switch (exprType) {
              case OpTypes.NOT:
                value = null;
                break;

              default:
                it = conditions[condIndex].rangeIndex.emptyIterator();

                return;
            }
          }
        }

        currentJoinData[i] = value;
      }

      it =
          conditions[condIndex].rangeIndex.findFirstRow(
              session,
              store,
              currentJoinData,
              conditions[condIndex].indexedColumnCount,
              rangeVar.indexDistinctCount,
              conditions[condIndex].opType,
              conditions[condIndex].reversed,
              null);
    }
Esempio n. 24
0
  public Object readData(Type type) throws IOException {

    Object o = null;

    if (readNull()) {
      return null;
    }

    switch (type.typeCode) {
      case Types.SQL_ALL_TYPES:
        break;

      case Types.SQL_CHAR:
      case Types.SQL_VARCHAR:
      case Types.VARCHAR_IGNORECASE:
        o = readChar(type);
        break;

      case Types.TINYINT:
      case Types.SQL_SMALLINT:
        o = readSmallint();
        break;

      case Types.SQL_INTEGER:
        o = readInteger();
        break;

      case Types.SQL_BIGINT:
        o = readBigint();
        break;

      case Types.SQL_REAL:
      case Types.SQL_FLOAT:
      case Types.SQL_DOUBLE:
        o = readReal();
        break;

      case Types.SQL_NUMERIC:
      case Types.SQL_DECIMAL:
        o = readDecimal(type);
        break;

      case Types.SQL_DATE:
        o = readDate(type);
        break;

      case Types.SQL_TIME:
      case Types.SQL_TIME_WITH_TIME_ZONE:
        o = readTime(type);
        break;

      case Types.SQL_TIMESTAMP:
      case Types.SQL_TIMESTAMP_WITH_TIME_ZONE:
        o = readTimestamp(type);
        break;

      case Types.SQL_INTERVAL_YEAR:
      case Types.SQL_INTERVAL_YEAR_TO_MONTH:
      case Types.SQL_INTERVAL_MONTH:
        o = readYearMonthInterval(type);
        break;

      case Types.SQL_INTERVAL_DAY:
      case Types.SQL_INTERVAL_DAY_TO_HOUR:
      case Types.SQL_INTERVAL_DAY_TO_MINUTE:
      case Types.SQL_INTERVAL_DAY_TO_SECOND:
      case Types.SQL_INTERVAL_HOUR:
      case Types.SQL_INTERVAL_HOUR_TO_MINUTE:
      case Types.SQL_INTERVAL_HOUR_TO_SECOND:
      case Types.SQL_INTERVAL_MINUTE:
      case Types.SQL_INTERVAL_MINUTE_TO_SECOND:
      case Types.SQL_INTERVAL_SECOND:
        o = readDaySecondInterval(type);
        break;

      case Types.SQL_BOOLEAN:
        o = readBoole();
        break;

      case Types.OTHER:
        o = readOther();
        break;

      case Types.SQL_CLOB:
        o = readClob();
        break;

      case Types.SQL_BLOB:
        o = readBlob();
        break;

      case Types.SQL_ARRAY:
        o = readArray(type);
        break;

      case Types.SQL_BINARY:
      case Types.SQL_VARBINARY:
        o = readBinary();
        break;

      case Types.SQL_BIT:
      case Types.SQL_BIT_VARYING:
        o = readBit();
        break;

      default:
        throw Error.runtimeError(ErrorCode.U_S0500, "RowInputBase - " + type.getNameString());
    }

    return o;
  }
  ResultMetaData(RowInputBinary in) throws IOException {

    type = in.readInt();
    columnCount = in.readInt();

    switch (type) {
      case UPDATE_RESULT_METADATA:
      case SIMPLE_RESULT_METADATA:
        {
          columnTypes = new Type[columnCount];

          for (int i = 0; i < columnCount; i++) {
            int type = in.readType();

            columnTypes[i] = Type.getDefaultType(type);
          }

          return;
        }
      case GENERATED_INDEX_METADATA:
        {
          colIndexes = new int[columnCount];

          for (int i = 0; i < columnCount; i++) {
            colIndexes[i] = in.readInt();
          }

          return;
        }
      case GENERATED_NAME_METADATA:
        {
          columnLabels = new String[columnCount];

          for (int i = 0; i < columnCount; i++) {
            columnLabels[i] = in.readString();
          }

          return;
        }
      case PARAM_METADATA:
        {
          columnTypes = new Type[columnCount];
          columnLabels = new String[columnCount];
          paramModes = new byte[columnCount];
          paramNullable = new byte[columnCount];

          for (int i = 0; i < columnCount; i++) {
            columnTypes[i] = readDataType(in);
            columnLabels[i] = in.readString();

            decodeParamColumnAttrs(in.readByte(), i);
          }

          return;
        }
      case RESULT_METADATA:
        {
          extendedColumnCount = in.readInt();
          columnTypes = new Type[extendedColumnCount];
          columnLabels = new String[columnCount];
          columns = new ColumnBase[columnCount];

          if (columnCount != extendedColumnCount) {
            colIndexes = new int[columnCount];
          }

          for (int i = 0; i < extendedColumnCount; i++) {
            Type type = readDataType(in);

            columnTypes[i] = type;
          }

          for (int i = 0; i < columnCount; i++) {
            columnLabels[i] = in.readString();

            String catalog = in.readString();
            String schema = in.readString();
            String table = in.readString();
            String name = in.readString();
            ColumnBase column = new ColumnBase(catalog, schema, table, name);

            column.setType(columnTypes[i]);
            decodeTableColumnAttrs(in.readByte(), column);

            columns[i] = column;
          }

          if (columnCount != extendedColumnCount) {
            for (int i = 0; i < columnCount; i++) {
              colIndexes[i] = in.readInt();
            }
          }

          return;
        }
      default:
        {
          throw Error.runtimeError(ErrorCode.U_S0500, "ResultMetaData");
        }
    }
  }