Search in sources :

Example 1 with TimestampData

use of org.hsqldb_voltpatches.types.TimestampData in project voltdb by VoltDB.

the class Expression method voltGetXML.

/**
     * VoltDB added method to get a non-catalog-dependent
     * representation of this HSQLDB object.
     * @param context The context encapsulates the current Session object and (optionally)
     * select statement display columns that may be needed to resolve some names.
     * @return A VoltXML tree structure.
     * @throws HSQLParseException
     */
VoltXMLElement voltGetXML(SimpleColumnContext context, String realAlias) throws HSQLParseException {
    // The voltXML representations of expressions tends to be driven much more by the expression's opType
    // than its Expression class.
    int exprOp = getType();
    // That case gets explicitly enabled here by fudging the opType from SIMPLE_COLUMN to COLUMN.
    if (exprOp == OpTypes.SIMPLE_COLUMN) {
        VoltXMLElement asResolved = context.resolveSimpleColumn(this);
        if (asResolved != null) {
            return asResolved;
        }
        // ENG-10429 moved the following two lines here. See fix note https://issues.voltdb.com/browse/ENG-10429.
        // convert the SIMPLE_COLUMN into a COLUMN
        opType = OpTypes.COLUMN;
        exprOp = OpTypes.COLUMN;
    }
    // Use the opType to find a pre-initialized prototype VoltXMLElement with the correct
    // name and any required hard-coded values pre-set.
    VoltXMLElement exp = prototypes.get(exprOp);
    if (exp == null) {
        // Must have found an unsupported opType.
        throwForUnsupportedExpression(exprOp);
    }
    // Duplicate the prototype and add any expression particulars needed for the specific opType value,
    // as well as a unique identifier, a possible alias, and child nodes.
    exp = exp.duplicate();
    exp.attributes.put("id", getUniqueId(context.m_session));
    if (realAlias != null) {
        exp.attributes.put("alias", realAlias);
    } else if ((alias != null) && (getAlias().length() > 0)) {
        exp.attributes.put("alias", getAlias());
    }
    // Add expression sub type
    if (exprSubType == OpTypes.ANY_QUANTIFIED) {
        exp.attributes.put("opsubtype", "any");
    } else if (exprSubType == OpTypes.ALL_QUANTIFIED) {
        exp.attributes.put("opsubtype", "all");
    }
    for (Expression expr : nodes) {
        if (expr != null) {
            VoltXMLElement vxmle = expr.voltGetXML(context, null);
            exp.children.add(vxmle);
            assert (vxmle != null);
        }
    }
    // VoltXMLElement.
    switch(exprOp) {
        case OpTypes.VALUE:
            // (I'm not sure why this MUST be the case --paul.)
            if (valueData == null) {
                String valueType = (dataType == null) ? "NULL" : Types.getTypeName(dataType.typeCode);
                exp.attributes.put("valuetype", valueType);
                return exp;
            }
            exp.attributes.put("valuetype", Types.getTypeName(dataType.typeCode));
            if (valueData instanceof TimestampData) {
                // When we get the default from the DDL,
                // it gets jammed into a TimestampData object.  If we
                // don't do this, we get a Java class/reference
                // string in the output schema for the DDL.
                // EL HACKO: I'm just adding in the timezone seconds
                // at the moment, hope this is right --izzy
                TimestampData time = (TimestampData) valueData;
                exp.attributes.put("value", Long.toString(Math.round((time.getSeconds() + time.getZone()) * 1e6) + time.getNanos() / 1000));
                return exp;
            }
            // convert binary values to hex
            if (valueData instanceof BinaryData) {
                BinaryData bd = (BinaryData) valueData;
                exp.attributes.put("value", hexEncode(bd.getBytes()));
                return exp;
            }
            // Otherwise just string format the value.
            if (dataType instanceof NumberType && !dataType.isIntegralType()) {
                // remove the scentific exponent notation
                exp.attributes.put("value", new BigDecimal(valueData.toString()).toPlainString());
                return exp;
            }
            exp.attributes.put("value", valueData.toString());
            return exp;
        case OpTypes.COLUMN:
            ExpressionColumn ec = (ExpressionColumn) this;
            return ec.voltAnnotateColumnXML(exp);
        case OpTypes.COALESCE:
            return convertUsingColumnrefToCoaleseExpression(context.m_session, exp, dataType);
        case OpTypes.SQL_FUNCTION:
            FunctionSQL fn = (FunctionSQL) this;
            return fn.voltAnnotateFunctionXML(exp);
        case OpTypes.COUNT:
        case OpTypes.SUM:
        case OpTypes.AVG:
            if (((ExpressionAggregate) this).isDistinctAggregate) {
                exp.attributes.put("distinct", "true");
            }
            return exp;
        case OpTypes.ORDER_BY:
            if (((ExpressionOrderBy) this).isDescending()) {
                exp.attributes.put("desc", "true");
            }
            return exp;
        case OpTypes.CAST:
            if (dataType == null) {
                throw new HSQLParseException("VoltDB could not determine the type in a CAST operation");
            }
            exp.attributes.put("valuetype", dataType.getNameString());
            return exp;
        case OpTypes.TABLE_SUBQUERY:
            if (subQuery == null || subQuery.queryExpression == null) {
                throw new HSQLParseException("VoltDB could not determine the subquery");
            }
            ExpressionColumn[] parameters = new ExpressionColumn[0];
            exp.children.add(StatementQuery.voltGetXMLExpression(subQuery.queryExpression, parameters, context.m_session));
            return exp;
        case OpTypes.ALTERNATIVE:
            assert (nodes.length == 2);
            // If with ELSE clause, pad NULL with it.
            if (nodes[RIGHT] instanceof ExpressionValue) {
                ExpressionValue val = (ExpressionValue) nodes[RIGHT];
                if (val.valueData == null && val.dataType == Type.SQL_ALL_TYPES) {
                    exp.children.get(RIGHT).attributes.put("valuetype", dataType.getNameString());
                }
            }
        case OpTypes.CASEWHEN:
            // Hsql has check dataType can not be null.
            assert (dataType != null);
            exp.attributes.put("valuetype", dataType.getNameString());
            return exp;
        case OpTypes.WINDOWED_RANK:
        case OpTypes.WINDOWED_DENSE_RANK:
        case OpTypes.WINDOWED_COUNT:
        case OpTypes.WINDOWED_MIN:
        case OpTypes.WINDOWED_MAX:
        case OpTypes.WINDOWED_SUM:
            assert (dataType != null);
            assert (this instanceof ExpressionWindowed);
            exp.attributes.put("valuetype", dataType.getNameString());
            ExpressionWindowed erank = (ExpressionWindowed) this;
            return erank.voltAnnotateWindowedAggregateXML(exp, context);
        default:
            return exp;
    }
}
Also used : TimestampData(org.hsqldb_voltpatches.types.TimestampData) BigDecimal(java.math.BigDecimal) HSQLParseException(org.hsqldb_voltpatches.HSQLInterface.HSQLParseException) NumberType(org.hsqldb_voltpatches.types.NumberType) BinaryData(org.hsqldb_voltpatches.types.BinaryData)

Example 2 with TimestampData

use of org.hsqldb_voltpatches.types.TimestampData in project voltdb by VoltDB.

the class FunctionCustom method getValue.

@Override
Object getValue(Session session, Object[] data) {
    switch(funcType) {
        case FUNC_EXTRACT:
        case FUNC_TRIM_CHAR:
        case FUNC_OVERLAY_CHAR:
            return super.getValue(session, data);
        case FUNC_DATABASE:
            return session.getDatabase().getPath();
        case FUNC_ISAUTOCOMMIT:
            return session.isAutoCommit() ? Boolean.TRUE : Boolean.FALSE;
        case FUNC_ISREADONLYSESSION:
            return session.isReadOnlyDefault() ? Boolean.TRUE : Boolean.FALSE;
        case FUNC_ISREADONLYDATABASE:
            return session.getDatabase().databaseReadOnly ? Boolean.TRUE : Boolean.FALSE;
        case FUNC_ISREADONLYDATABASEFILES:
            return session.getDatabase().isFilesReadOnly() ? Boolean.TRUE : Boolean.FALSE;
        case FUNC_IDENTITY:
            {
                Number id = session.getLastIdentity();
                if (id instanceof Long) {
                    return id;
                } else {
                    return ValuePool.getLong(id.longValue());
                }
            }
        case FUNC_TIMESTAMPADD:
            {
                if (data[1] == null || data[2] == null) {
                    return null;
                }
                int part = ((Number) nodes[0].valueData).intValue();
                long units = ((Number) data[1]).longValue();
                TimestampData source = (TimestampData) data[2];
                IntervalType t;
                Object o;
                switch(part) {
                    case Tokens.SQL_TSI_FRAC_SECOND:
                        {
                            long seconds = units / DTIType.limitNanoseconds;
                            int nanos = (int) (units % DTIType.limitNanoseconds);
                            t = Type.SQL_INTERVAL_SECOND_MAX_FRACTION;
                            o = new IntervalSecondData(seconds, nanos, t);
                            return dataType.add(source, o, t);
                        }
                    case Tokens.SQL_TSI_SECOND:
                        t = Type.SQL_INTERVAL_SECOND;
                        o = IntervalSecondData.newIntervalSeconds(units, t);
                        return dataType.add(source, o, t);
                    case Tokens.SQL_TSI_MINUTE:
                        t = Type.SQL_INTERVAL_MINUTE;
                        o = IntervalSecondData.newIntervalMinute(units, t);
                        return dataType.add(source, o, t);
                    case Tokens.SQL_TSI_HOUR:
                        t = Type.SQL_INTERVAL_HOUR;
                        o = IntervalSecondData.newIntervalHour(units, t);
                        return dataType.add(source, o, t);
                    case Tokens.SQL_TSI_DAY:
                        t = Type.SQL_INTERVAL_DAY;
                        o = IntervalSecondData.newIntervalDay(units, t);
                        return dataType.add(source, o, t);
                    case Tokens.SQL_TSI_WEEK:
                        t = Type.SQL_INTERVAL_DAY;
                        o = IntervalSecondData.newIntervalDay(units * 7, t);
                        return dataType.add(source, o, t);
                    case Tokens.SQL_TSI_MONTH:
                        t = Type.SQL_INTERVAL_MONTH;
                        o = IntervalMonthData.newIntervalMonth(units, t);
                        return dataType.add(source, o, t);
                    case Tokens.SQL_TSI_QUARTER:
                        t = Type.SQL_INTERVAL_MONTH;
                        o = IntervalMonthData.newIntervalMonth(units * 3, t);
                        return dataType.add(source, o, t);
                    case Tokens.SQL_TSI_YEAR:
                        t = Type.SQL_INTERVAL_YEAR;
                        o = IntervalMonthData.newIntervalMonth(units, t);
                        return dataType.add(source, o, t);
                    default:
                        throw Error.runtimeError(ErrorCode.U_S0500, "FunctionCustom");
                }
            }
        case FUNC_TIMESTAMPDIFF:
            {
                if (data[1] == null || data[2] == null) {
                    return null;
                }
                int part = ((Number) nodes[0].valueData).intValue();
                TimestampData a = (TimestampData) data[2];
                TimestampData b = (TimestampData) data[1];
                if (nodes[2].dataType.isDateTimeTypeWithZone()) {
                    a = (TimestampData) Type.SQL_TIMESTAMP.convertToType(session, a, Type.SQL_TIMESTAMP_WITH_TIME_ZONE);
                }
                if (nodes[1].dataType.isDateTimeTypeWithZone()) {
                    b = (TimestampData) Type.SQL_TIMESTAMP.convertToType(session, b, Type.SQL_TIMESTAMP_WITH_TIME_ZONE);
                }
                IntervalType t;
                switch(part) {
                    case Tokens.SQL_TSI_FRAC_SECOND:
                        t = Type.SQL_INTERVAL_SECOND_MAX_PRECISION;
                        IntervalSecondData interval = (IntervalSecondData) t.subtract(a, b, null);
                        return new Long(DTIType.limitNanoseconds * interval.getSeconds() + interval.getNanos());
                    case Tokens.SQL_TSI_SECOND:
                        t = Type.SQL_INTERVAL_SECOND_MAX_PRECISION;
                        return new Long(t.convertToLong(t.subtract(a, b, null)));
                    case Tokens.SQL_TSI_MINUTE:
                        t = Type.SQL_INTERVAL_MINUTE_MAX_PRECISION;
                        return new Long(t.convertToLong(t.subtract(a, b, null)));
                    case Tokens.SQL_TSI_HOUR:
                        t = Type.SQL_INTERVAL_HOUR_MAX_PRECISION;
                        return new Long(t.convertToLong(t.subtract(a, b, null)));
                    case Tokens.SQL_TSI_DAY:
                        t = Type.SQL_INTERVAL_DAY_MAX_PRECISION;
                        return new Long(t.convertToLong(t.subtract(a, b, null)));
                    case Tokens.SQL_TSI_WEEK:
                        t = Type.SQL_INTERVAL_DAY_MAX_PRECISION;
                        return new Long(t.convertToLong(t.subtract(a, b, null)) / 7);
                    case Tokens.SQL_TSI_MONTH:
                        t = Type.SQL_INTERVAL_MONTH_MAX_PRECISION;
                        return new Long(t.convertToLong(t.subtract(a, b, null)));
                    case Tokens.SQL_TSI_QUARTER:
                        t = Type.SQL_INTERVAL_MONTH_MAX_PRECISION;
                        return new Long(t.convertToLong(t.subtract(a, b, null)) / 3);
                    case Tokens.SQL_TSI_YEAR:
                        t = Type.SQL_INTERVAL_YEAR_MAX_PRECISION;
                        return new Long(t.convertToLong(t.subtract(a, b, null)));
                    default:
                        throw Error.runtimeError(ErrorCode.U_S0500, "FunctionCustom");
                }
            }
        case FUNC_SECONDS_MIDNIGHT:
            {
                if (data[0] == null) {
                    return null;
                }
            }
        // $FALL-THROUGH$
        case FUNC_TRUNCATE:
            {
                if (data[0] == null || data[1] == null) {
                    return null;
                }
                return ((NumberType) dataType).truncate(data[0], ((Number) data[1]).intValue());
            }
        case FUNC_TO_CHAR:
            {
                if (data[0] == null || data[1] == null) {
                    return null;
                }
                SimpleDateFormat format = session.getSimpleDateFormatGMT();
                String javaPattern = HsqlDateTime.toJavaDatePattern((String) data[1]);
                try {
                    format.applyPattern(javaPattern);
                } catch (Exception e) {
                    throw Error.error(ErrorCode.X_22511);
                }
                Date date = (Date) ((DateTimeType) nodes[0].dataType).convertSQLToJavaGMT(session, data[0]);
                return format.format(date);
            }
        case FUNC_TIMESTAMP:
            {
                boolean unary = nodes[1] == null;
                if (data[0] == null) {
                    return null;
                }
                if (unary) {
                    return Type.SQL_TIMESTAMP.convertToType(session, data[0], nodes[0].dataType);
                }
                if (data[1] == null) {
                    return null;
                }
                TimestampData date = (TimestampData) Type.SQL_DATE.convertToType(session, data[0], nodes[0].dataType);
                TimeData time = (TimeData) Type.SQL_TIME.convertToType(session, data[1], nodes[1].dataType);
                return new TimestampData(date.getSeconds() + time.getSeconds(), time.getNanos());
            }
        case FUNC_PI:
            return Double.valueOf(Math.PI);
        case FUNC_RAND:
            {
                if (nodes[0] == null) {
                    return Double.valueOf(session.random());
                } else {
                    long seed = ((Number) data[0]).longValue();
                    return Double.valueOf(seed);
                }
            }
        case FUNC_ACOS:
            {
                if (data[0] == null) {
                    return null;
                }
                double d = NumberType.toDouble(data[0]);
                return Double.valueOf(java.lang.Math.acos(d));
            }
        case FUNC_ASIN:
            {
                if (data[0] == null) {
                    return null;
                }
                double d = NumberType.toDouble(data[0]);
                return Double.valueOf(java.lang.Math.asin(d));
            }
        case FUNC_ATAN:
            {
                if (data[0] == null) {
                    return null;
                }
                double d = NumberType.toDouble(data[0]);
                return Double.valueOf(java.lang.Math.atan(d));
            }
        case FUNC_COS:
            {
                if (data[0] == null) {
                    return null;
                }
                double d = NumberType.toDouble(data[0]);
                return Double.valueOf(java.lang.Math.cos(d));
            }
        case FUNC_CSC:
            {
                if (data[0] == null) {
                    return null;
                }
                double c = NumberType.toDouble(data[0]);
                double sinValue = java.lang.Math.sin(c);
                if (sinValue == 0) {
                    return null;
                }
                double d = 1.0 / sinValue;
                return Double.valueOf(d);
            }
        case FUNC_COT:
            {
                if (data[0] == null) {
                    return null;
                }
                double d = NumberType.toDouble(data[0]);
                double c = 1.0 / java.lang.Math.tan(d);
                return Double.valueOf(c);
            }
        case FUNC_DEGREES:
            {
                if (data[0] == null) {
                    return null;
                }
                double d = NumberType.toDouble(data[0]);
                return Double.valueOf(java.lang.Math.toDegrees(d));
            }
        case FUNC_SIN:
            {
                if (data[0] == null) {
                    return null;
                }
                double d = NumberType.toDouble(data[0]);
                return Double.valueOf(java.lang.Math.sin(d));
            }
        case FUNC_SEC:
            {
                if (data[0] == null) {
                    return null;
                }
                double c = NumberType.toDouble(data[0]);
                double cosValue = java.lang.Math.cos(c);
                if (cosValue == 0) {
                    return null;
                }
                double d = 1.0 / cosValue;
                return Double.valueOf(d);
            }
        case FUNC_TAN:
            {
                if (data[0] == null) {
                    return null;
                }
                double d = NumberType.toDouble(data[0]);
                return Double.valueOf(java.lang.Math.tan(d));
            }
        case FUNC_LOG10:
            {
                if (data[0] == null) {
                    return null;
                }
                double d = NumberType.toDouble(data[0]);
                if (d <= 0) {
                    throw Error.error(ErrorCode.X_2201E);
                }
                return Double.valueOf(java.lang.Math.log10(d));
            }
        case FUNC_RADIANS:
            {
                if (data[0] == null) {
                    return null;
                }
                double d = NumberType.toDouble(data[0]);
                return Double.valueOf(java.lang.Math.toRadians(d));
            }
        //
        case FUNC_SIGN:
            {
                if (data[0] == null) {
                    return null;
                }
                return ((NumberType) nodes[0].dataType).compareToZero(data[0]);
            }
        case FUNC_ATAN2:
            {
                if (data[0] == null) {
                    return null;
                }
                double a = NumberType.toDouble(data[0]);
                double b = NumberType.toDouble(data[1]);
                return Double.valueOf(java.lang.Math.atan2(a, b));
            }
        case FUNC_ASCII:
            {
                String arg;
                if (data[0] == null) {
                    return null;
                }
                if (nodes[0].dataType.isLobType()) {
                    arg = ((ClobData) data[0]).getSubString(session, 0, 1);
                } else {
                    arg = (String) data[0];
                }
                if (arg.length() == 0) {
                    return null;
                }
                return ValuePool.getInt(arg.charAt(0));
            }
        case FUNC_CHAR:
            if (data[0] == null) {
                return null;
            }
            int arg = ((Number) data[0]).intValue();
            return String.valueOf(arg);
        case FUNC_ROUND:
            {
                if (data[0] == null || data[1] == null) {
                    return null;
                }
                double d = NumberType.toDouble(data[0]);
                int i = ((Number) data[1]).intValue();
                return Library.round(d, i);
            }
        case FUNC_ROUNDMAGIC:
            {
                if (data[0] == null) {
                    return null;
                }
                double d = NumberType.toDouble(data[0]);
                return Library.roundMagic(d);
            }
        case FUNC_SOUNDEX:
            {
                if (data[0] == null) {
                    return null;
                }
                String s = (String) data[0];
                return Library.soundex(s);
            }
        case FUNC_BITAND:
        case FUNC_BITOR:
        case FUNC_BITXOR:
            {
                for (int i = 0; i < data.length; i++) {
                    if (data[0] == null) {
                        return null;
                    }
                }
                /************************* Volt DB Extensions *************************/
                if (nodes[0].dataType.isIntegralType()) {
                    if (data[0] == null || data[1] == null)
                        return null;
                    long v = 0;
                    long a = ((Number) data[0]).longValue();
                    long b = ((Number) data[1]).longValue();
                    switch(funcType) {
                        case FUNC_BITAND:
                            v = a & b;
                            break;
                        case FUNC_BITOR:
                            v = a | b;
                            break;
                        case FUNC_BITXOR:
                            v = a ^ b;
                            break;
                    }
                    return ValuePool.getLong(v);
                /**********************************************************************/
                } else {
                    /** @todo - for binary */
                    return null;
                }
            }
        case FUNC_DIFFERENCE:
            {
                for (int i = 0; i < data.length; i++) {
                    if (data[0] == null) {
                        return null;
                    }
                }
                int v = Library.difference((String) data[0], (String) data[1]);
                return ValuePool.getInt(v);
            }
        case FUNC_HEXTORAW:
            {
                if (data[0] == null) {
                    return null;
                }
                return dataType.convertToType(session, data[0], nodes[0].dataType);
            }
        case FUNC_RAWTOHEX:
            {
                if (data[0] == null) {
                    return null;
                }
                return nodes[0].dataType.convertToString(data[0]);
            }
        case FUNC_LOCATE:
            {
                for (int i = 0; i < data.length; i++) {
                    if (data[0] == null) {
                        return null;
                    }
                }
                int v = Library.locate((String) data[0], (String) data[1], (Integer) data[2]);
                return ValuePool.getInt(v);
            }
        case FUNC_REPEAT:
            {
                for (int i = 0; i < data.length; i++) {
                    if (data[0] == null) {
                        return null;
                    }
                }
                return Library.repeat((String) data[0], ((Number) data[1]).intValue());
            }
        case FUNC_REPLACE:
            {
                for (int i = 0; i < data.length; i++) {
                    if (data[0] == null) {
                        return null;
                    }
                }
                return Library.replace((String) data[0], (String) data[1], (String) data[2]);
            }
        case FUNC_LEFT:
        case FUNC_RIGHT:
            {
                for (int i = 0; i < data.length; i++) {
                    if (data[0] == null) {
                        return null;
                    }
                }
                int count = ((Number) data[1]).intValue();
                return ((CharacterType) dataType).substring(session, data[0], 0, count, true, funcType == FUNC_RIGHT);
            }
        case FUNC_SPACE:
            {
                for (int i = 0; i < data.length; i++) {
                    if (data[0] == null) {
                        return null;
                    }
                }
                int count = ((Number) data[0]).intValue();
                return ValuePool.getSpaces(count);
            }
        default:
            throw Error.runtimeError(ErrorCode.U_S0500, "FunctionCustom");
    }
}
Also used : IntervalSecondData(org.hsqldb_voltpatches.types.IntervalSecondData) TimestampData(org.hsqldb_voltpatches.types.TimestampData) IntervalType(org.hsqldb_voltpatches.types.IntervalType) Date(java.util.Date) DateTimeType(org.hsqldb_voltpatches.types.DateTimeType) TimeData(org.hsqldb_voltpatches.types.TimeData) ClobData(org.hsqldb_voltpatches.types.ClobData) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with TimestampData

use of org.hsqldb_voltpatches.types.TimestampData in project voltdb by VoltDB.

the class DatabaseInformationFull method SYSTEM_SESSIONS.

/**
     * Retrieves a <code>Table</code> object describing all visible
     * sessions. ADMIN users see *all* sessions
     * while non-admin users see only their own session.<p>
     *
     * Each row is a session state description with the following columns: <p>
     *
     * <pre class="SqlCodeExample">
     * SESSION_ID         INTEGER   session identifier
     * CONNECTED          TIMESTAMP time at which session was created
     * USER_NAME          VARCHAR   db user name of current session user
     * IS_ADMIN           BOOLEAN   is session user an admin user?
     * AUTOCOMMIT         BOOLEAN   is session in autocommit mode?
     * READONLY           BOOLEAN   is session in read-only mode?
     * MAXROWS            INTEGER   session's MAXROWS setting
     * LAST_IDENTITY      INTEGER   last identity value used by this session
     * TRANSACTION_SIZE   INTEGER   # of undo items in current transaction
     * SCHEMA             VARCHAR   current schema for session
     * </pre> <p>
     *
     * @return a <code>Table</code> object describing all visible
     *      sessions
     */
Table SYSTEM_SESSIONS() {
    Table t = sysTables[SYSTEM_SESSIONS];
    if (t == null) {
        t = createBlankTable(sysTableHsqlNames[SYSTEM_SESSIONS]);
        addColumn(t, "SESSION_ID", CARDINAL_NUMBER);
        addColumn(t, "CONNECTED", TIME_STAMP);
        addColumn(t, "USER_NAME", SQL_IDENTIFIER);
        addColumn(t, "IS_ADMIN", Type.SQL_BOOLEAN);
        addColumn(t, "AUTOCOMMIT", Type.SQL_BOOLEAN);
        addColumn(t, "READONLY", Type.SQL_BOOLEAN);
        addColumn(t, "MAXROWS", CARDINAL_NUMBER);
        // Note: some sessions may have a NULL LAST_IDENTITY value
        addColumn(t, "LAST_IDENTITY", CARDINAL_NUMBER);
        addColumn(t, "TRANSACTION_SIZE", CARDINAL_NUMBER);
        addColumn(t, "SCHEMA", SQL_IDENTIFIER);
        // order:  SESSION_ID
        // true primary key
        HsqlName name = HsqlNameManager.newInfoSchemaObjectName(sysTableHsqlNames[SYSTEM_SESSIONS].name, false, SchemaObject.INDEX);
        t.createPrimaryKey(name, new int[] { 0 }, true);
        return t;
    }
    // column number mappings
    final int isid = 0;
    final int ict = 1;
    final int iuname = 2;
    final int iis_admin = 3;
    final int iautocmt = 4;
    final int ireadonly = 5;
    final int imaxrows = 6;
    final int ilast_id = 7;
    final int it_size = 8;
    final int it_schema = 9;
    //
    PersistentStore store = database.persistentStoreCollection.getStore(t);
    // intermediate holders
    Session[] sessions;
    Session s;
    Object[] row;
    // Initialisation
    sessions = ns.listVisibleSessions(session);
    // Do it.
    for (int i = 0; i < sessions.length; i++) {
        s = sessions[i];
        row = t.getEmptyRowData();
        row[isid] = ValuePool.getLong(s.getId());
        row[ict] = new TimestampData(s.getConnectTime() / 1000);
        row[iuname] = s.getUsername();
        row[iis_admin] = ValuePool.getBoolean(s.isAdmin());
        row[iautocmt] = ValuePool.getBoolean(s.isAutoCommit());
        row[ireadonly] = ValuePool.getBoolean(s.isReadOnlyDefault());
        row[imaxrows] = ValuePool.getInt(s.getSQLMaxRows());
        row[ilast_id] = ValuePool.getLong(((Number) s.getLastIdentity()).longValue());
        row[it_size] = ValuePool.getInt(s.getTransactionSize());
        row[it_schema] = s.getCurrentSchemaHsqlName().name;
        t.insertSys(store, row);
    }
    return t;
}
Also used : TimestampData(org.hsqldb_voltpatches.types.TimestampData) Table(org.hsqldb_voltpatches.Table) TextTable(org.hsqldb_voltpatches.TextTable) PersistentStore(org.hsqldb_voltpatches.persist.PersistentStore) HsqlName(org.hsqldb_voltpatches.HsqlNameManager.HsqlName) SchemaObject(org.hsqldb_voltpatches.SchemaObject) Constraint(org.hsqldb_voltpatches.Constraint) Session(org.hsqldb_voltpatches.Session)

Example 4 with TimestampData

use of org.hsqldb_voltpatches.types.TimestampData in project voltdb by VoltDB.

the class JDBCResultSet method getTimestamp.

/**
     * <!-- start generic documentation -->
     * Retrieves the value of the designated column in the current row
     * of this <code>ResultSet</code> object as a <code>java.sql.Timestamp</code> object
     * in the Java programming language.
     * This method uses the given calendar to construct an appropriate millisecond
     * value for the timestamp if the underlying database does not store
     * timezone information.
     * <!-- end generic documentation -->
     * <h3>HSQLDB-Specific Information:</h3> <p>
     *
     * The JDBC specification for this method is vague. HSQLDB interprets the
     * specification as follows:
     *
     * <ol>
     * <li>If the SQL type of the column is WITH TIME ZONE, then the UTC value
     * of the returned java.sql.Timestamp object is the UTC of the SQL value
     * without modification. In other words, the Calendar object is not used.
     * </li>
     * <li>If the SQL type of the column is WITHOUT TIME ZONE, then the UTC
     * value of the returned java.sql.Timestamp is correct for the given
     * Calendar object.</li>
     * <li>If the cal argument is null, it it ignored and the method returns
     * the same Object as the method without the Calendar parameter.</li>
     * </ol>
     * </div>
     *
     * @param columnIndex the first column is 1, the second is 2, ...
     * @param cal the <code>java.util.Calendar</code> object
     * to use in constructing the timestamp
     * @return the column value as a <code>java.sql.Timestamp</code> object;
     * if the value is SQL <code>NULL</code>,
     * the value returned is <code>null</code> in the Java programming language
     * @exception SQLException if a database access error occurs
     * or this method is called on a closed result set
     * @since JDK 1.2 (JDK 1.1.x developers: read the overview for
     *  JDBCResultSet)
     */
public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException {
    TimestampData t = (TimestampData) getColumnInType(columnIndex, Type.SQL_TIMESTAMP);
    if (t == null) {
        return null;
    }
    long millis = t.getSeconds() * 1000;
    if (resultMetaData.columnTypes[--columnIndex].isDateTimeTypeWithZone()) {
    } else {
        // UTC - calZO == (UTC - sessZO) + (sessionZO - calZO)
        if (cal != null) {
            int zoneOffset = HsqlDateTime.getZoneMillis(cal, millis);
            millis += session.getZoneSeconds() * 1000 - zoneOffset;
        }
    }
    Timestamp ts = new Timestamp(millis);
    ts.setNanos(t.getNanos());
    return ts;
}
Also used : TimestampData(org.hsqldb_voltpatches.types.TimestampData) Timestamp(java.sql.Timestamp)

Example 5 with TimestampData

use of org.hsqldb_voltpatches.types.TimestampData in project voltdb by VoltDB.

the class JDBCPreparedStatement method setDate.

/**
     * <!-- start generic documentation -->
     * Sets the designated parameter to the given <code>java.sql.Date</code> value,
     * using the given <code>Calendar</code> object.  The driver uses
     * the <code>Calendar</code> object to construct an SQL <code>DATE</code> value,
     * which the driver then sends to the database.  With
     * a <code>Calendar</code> object, the driver can calculate the date
     * taking into account a custom timezone.  If no
     * <code>Calendar</code> object is specified, the driver uses the default
     * timezone, which is that of the virtual machine running the application.
     * <!-- end generic documentation -->
     *
     * @param parameterIndex the first parameter is 1, the second is 2, ...
     * @param x the parameter value
     * @param cal the <code>Calendar</code> object the driver will use
     *            to construct the date
     * @exception SQLException if a database access error occurs or
     * this method is called on a closed <code>PreparedStatement</code>
     * @since JDK 1.2 (JDK 1.1.x developers: read the overview for
     *   JDBCParameterMetaData)
     */
public synchronized void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException {
    checkSetParameterIndex(parameterIndex, false);
    int i = parameterIndex - 1;
    if (x == null) {
        parameterValues[i] = null;
        return;
    }
    Type outType = parameterTypes[i];
    long millis = HsqlDateTime.convertToNormalisedDate(x.getTime(), cal);
    int zoneOffset = HsqlDateTime.getZoneMillis(cal, millis);
    switch(outType.typeCode) {
        case Types.SQL_DATE:
        case Types.SQL_TIMESTAMP:
        case Types.SQL_TIMESTAMP_WITH_TIME_ZONE:
            break;
        default:
            throw Util.sqlException(ErrorCode.X_42561);
    }
    parameterValues[i] = new TimestampData((millis + zoneOffset) / 1000);
}
Also used : Type(org.hsqldb_voltpatches.types.Type) TimestampData(org.hsqldb_voltpatches.types.TimestampData)

Aggregations

TimestampData (org.hsqldb_voltpatches.types.TimestampData)11 Date (java.sql.Date)2 Timestamp (java.sql.Timestamp)2 TimeData (org.hsqldb_voltpatches.types.TimeData)2 Type (org.hsqldb_voltpatches.types.Type)2 BigDecimal (java.math.BigDecimal)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Constraint (org.hsqldb_voltpatches.Constraint)1 HSQLParseException (org.hsqldb_voltpatches.HSQLInterface.HSQLParseException)1 HsqlName (org.hsqldb_voltpatches.HsqlNameManager.HsqlName)1 SchemaObject (org.hsqldb_voltpatches.SchemaObject)1 Session (org.hsqldb_voltpatches.Session)1 Table (org.hsqldb_voltpatches.Table)1 TextTable (org.hsqldb_voltpatches.TextTable)1 PersistentStore (org.hsqldb_voltpatches.persist.PersistentStore)1 BinaryData (org.hsqldb_voltpatches.types.BinaryData)1 ClobData (org.hsqldb_voltpatches.types.ClobData)1 DateTimeType (org.hsqldb_voltpatches.types.DateTimeType)1 IntervalSecondData (org.hsqldb_voltpatches.types.IntervalSecondData)1