Search in sources :

Example 1 with ValueResultSet

use of org.h2.value.ValueResultSet in project h2database by h2database.

the class TableFunction method getTable.

private ValueResultSet getTable(Session session, Expression[] argList, boolean onlyColumnList, boolean distinctRows) {
    int len = columnList.length;
    Expression[] header = new Expression[len];
    Database db = session.getDatabase();
    for (int i = 0; i < len; i++) {
        Column c = columnList[i];
        ExpressionColumn col = new ExpressionColumn(db, c);
        header[i] = col;
    }
    LocalResult result = new LocalResult(session, header, len);
    if (distinctRows) {
        result.setDistinct();
    }
    if (!onlyColumnList) {
        Value[][] list = new Value[len][];
        int rows = 0;
        for (int i = 0; i < len; i++) {
            Value v = argList[i].getValue(session);
            if (v == ValueNull.INSTANCE) {
                list[i] = new Value[0];
            } else {
                ValueArray array = (ValueArray) v.convertTo(Value.ARRAY);
                Value[] l = array.getList();
                list[i] = l;
                rows = Math.max(rows, l.length);
            }
        }
        for (int row = 0; row < rows; row++) {
            Value[] r = new Value[len];
            for (int j = 0; j < len; j++) {
                Value[] l = list[j];
                Value v;
                if (l.length <= row) {
                    v = ValueNull.INSTANCE;
                } else {
                    Column c = columnList[j];
                    v = l[row];
                    v = c.convert(v);
                    v = v.convertPrecision(c.getPrecision(), false);
                    v = v.convertScale(true, c.getScale());
                }
                r[j] = v;
            }
            result.addRow(r);
        }
    }
    result.done();
    return ValueResultSet.get(getSimpleResultSet(result, Integer.MAX_VALUE));
}
Also used : LocalResult(org.h2.result.LocalResult) Column(org.h2.table.Column) Database(org.h2.engine.Database) Value(org.h2.value.Value) ValueArray(org.h2.value.ValueArray)

Example 2 with ValueResultSet

use of org.h2.value.ValueResultSet in project h2database by h2database.

the class TestValue method testValueResultSet.

private void testValueResultSet() throws SQLException {
    SimpleResultSet rs = new SimpleResultSet();
    rs.setAutoClose(false);
    rs.addColumn("ID", Types.INTEGER, 0, 0);
    rs.addColumn("NAME", Types.VARCHAR, 255, 0);
    rs.addRow(1, "Hello");
    rs.addRow(2, "World");
    rs.addRow(3, "Peace");
    ValueResultSet v;
    v = ValueResultSet.get(rs);
    assertTrue(rs == v.getObject());
    v = ValueResultSet.getCopy(rs, 2);
    assertEquals(0, v.hashCode());
    assertEquals(Integer.MAX_VALUE, v.getDisplaySize());
    assertEquals(Integer.MAX_VALUE, v.getPrecision());
    assertEquals(0, v.getScale());
    assertEquals("", v.getSQL());
    assertEquals(Value.RESULT_SET, v.getType());
    assertEquals("((1, Hello), (2, World))", v.getString());
    rs.beforeFirst();
    ValueResultSet v2 = ValueResultSet.getCopy(rs, 2);
    assertTrue(v.equals(v));
    assertFalse(v.equals(v2));
    rs.beforeFirst();
    ResultSet rs2 = v.getResultSet();
    rs2.next();
    rs.next();
    assertEquals(rs.getInt(1), rs2.getInt(1));
    assertEquals(rs.getString(2), rs2.getString(2));
    rs2.next();
    rs.next();
    assertEquals(rs.getInt(1), rs2.getInt(1));
    assertEquals(rs.getString(2), rs2.getString(2));
    assertFalse(rs2.next());
    assertTrue(rs.next());
}
Also used : ValueResultSet(org.h2.value.ValueResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet) ValueResultSet(org.h2.value.ValueResultSet)

Example 3 with ValueResultSet

use of org.h2.value.ValueResultSet in project h2database by h2database.

the class FunctionTable method getValueResultSet.

private ValueResultSet getValueResultSet(Session session) {
    functionExpr = functionExpr.optimize(session);
    Value v = functionExpr.getValue(session);
    if (v == ValueNull.INSTANCE) {
        return null;
    }
    return (ValueResultSet) v;
}
Also used : ValueResultSet(org.h2.value.ValueResultSet) Value(org.h2.value.Value)

Example 4 with ValueResultSet

use of org.h2.value.ValueResultSet in project h2database by h2database.

the class Data method writeValue.

/**
 * Append a value.
 *
 * @param v the value
 */
public void writeValue(Value v) {
    int start = pos;
    if (v == ValueNull.INSTANCE) {
        data[pos++] = 0;
        return;
    }
    int type = v.getType();
    switch(type) {
        case Value.BOOLEAN:
            writeByte((byte) (v.getBoolean() ? BOOLEAN_TRUE : BOOLEAN_FALSE));
            break;
        case Value.BYTE:
            writeByte((byte) type);
            writeByte(v.getByte());
            break;
        case Value.SHORT:
            writeByte((byte) type);
            writeShortInt(v.getShort());
            break;
        case Value.ENUM:
        case Value.INT:
            {
                int x = v.getInt();
                if (x < 0) {
                    writeByte((byte) INT_NEG);
                    writeVarInt(-x);
                } else if (x < 16) {
                    writeByte((byte) (INT_0_15 + x));
                } else {
                    writeByte((byte) type);
                    writeVarInt(x);
                }
                break;
            }
        case Value.LONG:
            {
                long x = v.getLong();
                if (x < 0) {
                    writeByte((byte) LONG_NEG);
                    writeVarLong(-x);
                } else if (x < 8) {
                    writeByte((byte) (LONG_0_7 + x));
                } else {
                    writeByte((byte) type);
                    writeVarLong(x);
                }
                break;
            }
        case Value.DECIMAL:
            {
                BigDecimal x = v.getBigDecimal();
                if (BigDecimal.ZERO.equals(x)) {
                    writeByte((byte) DECIMAL_0_1);
                } else if (BigDecimal.ONE.equals(x)) {
                    writeByte((byte) (DECIMAL_0_1 + 1));
                } else {
                    int scale = x.scale();
                    BigInteger b = x.unscaledValue();
                    int bits = b.bitLength();
                    if (bits <= 63) {
                        if (scale == 0) {
                            writeByte((byte) DECIMAL_SMALL_0);
                            writeVarLong(b.longValue());
                        } else {
                            writeByte((byte) DECIMAL_SMALL);
                            writeVarInt(scale);
                            writeVarLong(b.longValue());
                        }
                    } else {
                        writeByte((byte) type);
                        writeVarInt(scale);
                        byte[] bytes = b.toByteArray();
                        writeVarInt(bytes.length);
                        write(bytes, 0, bytes.length);
                    }
                }
                break;
            }
        case Value.TIME:
            if (STORE_LOCAL_TIME) {
                writeByte((byte) LOCAL_TIME);
                ValueTime t = (ValueTime) v;
                long nanos = t.getNanos();
                long millis = nanos / 1_000_000;
                nanos -= millis * 1_000_000;
                writeVarLong(millis);
                writeVarLong(nanos);
            } else {
                writeByte((byte) type);
                writeVarLong(DateTimeUtils.getTimeLocalWithoutDst(v.getTime()));
            }
            break;
        case Value.DATE:
            {
                if (STORE_LOCAL_TIME) {
                    writeByte((byte) LOCAL_DATE);
                    long x = ((ValueDate) v).getDateValue();
                    writeVarLong(x);
                } else {
                    writeByte((byte) type);
                    long x = DateTimeUtils.getTimeLocalWithoutDst(v.getDate());
                    writeVarLong(x / MILLIS_PER_MINUTE);
                }
                break;
            }
        case Value.TIMESTAMP:
            {
                if (STORE_LOCAL_TIME) {
                    writeByte((byte) LOCAL_TIMESTAMP);
                    ValueTimestamp ts = (ValueTimestamp) v;
                    long dateValue = ts.getDateValue();
                    writeVarLong(dateValue);
                    long nanos = ts.getTimeNanos();
                    long millis = nanos / 1_000_000;
                    nanos -= millis * 1_000_000;
                    writeVarLong(millis);
                    writeVarLong(nanos);
                } else {
                    Timestamp ts = v.getTimestamp();
                    writeByte((byte) type);
                    writeVarLong(DateTimeUtils.getTimeLocalWithoutDst(ts));
                    writeVarInt(ts.getNanos() % 1_000_000);
                }
                break;
            }
        case Value.TIMESTAMP_TZ:
            {
                ValueTimestampTimeZone ts = (ValueTimestampTimeZone) v;
                writeByte((byte) type);
                writeVarLong(ts.getDateValue());
                writeVarLong(ts.getTimeNanos());
                writeVarInt(ts.getTimeZoneOffsetMins());
                break;
            }
        case Value.GEOMETRY:
        // fall though
        case Value.JAVA_OBJECT:
            {
                writeByte((byte) type);
                byte[] b = v.getBytesNoCopy();
                int len = b.length;
                writeVarInt(len);
                write(b, 0, len);
                break;
            }
        case Value.BYTES:
            {
                byte[] b = v.getBytesNoCopy();
                int len = b.length;
                if (len < 32) {
                    writeByte((byte) (BYTES_0_31 + len));
                    write(b, 0, len);
                } else {
                    writeByte((byte) type);
                    writeVarInt(len);
                    write(b, 0, len);
                }
                break;
            }
        case Value.UUID:
            {
                writeByte((byte) type);
                ValueUuid uuid = (ValueUuid) v;
                writeLong(uuid.getHigh());
                writeLong(uuid.getLow());
                break;
            }
        case Value.STRING:
            {
                String s = v.getString();
                int len = s.length();
                if (len < 32) {
                    writeByte((byte) (STRING_0_31 + len));
                    writeStringWithoutLength(s, len);
                } else {
                    writeByte((byte) type);
                    writeString(s);
                }
                break;
            }
        case Value.STRING_IGNORECASE:
        case Value.STRING_FIXED:
            writeByte((byte) type);
            writeString(v.getString());
            break;
        case Value.DOUBLE:
            {
                double x = v.getDouble();
                if (x == 1.0d) {
                    writeByte((byte) (DOUBLE_0_1 + 1));
                } else {
                    long d = Double.doubleToLongBits(x);
                    if (d == ValueDouble.ZERO_BITS) {
                        writeByte((byte) DOUBLE_0_1);
                    } else {
                        writeByte((byte) type);
                        writeVarLong(Long.reverse(d));
                    }
                }
                break;
            }
        case Value.FLOAT:
            {
                float x = v.getFloat();
                if (x == 1.0f) {
                    writeByte((byte) (FLOAT_0_1 + 1));
                } else {
                    int f = Float.floatToIntBits(x);
                    if (f == ValueFloat.ZERO_BITS) {
                        writeByte((byte) FLOAT_0_1);
                    } else {
                        writeByte((byte) type);
                        writeVarInt(Integer.reverse(f));
                    }
                }
                break;
            }
        case Value.BLOB:
        case Value.CLOB:
            {
                writeByte((byte) type);
                if (v instanceof ValueLob) {
                    ValueLob lob = (ValueLob) v;
                    lob.convertToFileIfRequired(handler);
                    byte[] small = lob.getSmall();
                    if (small == null) {
                        int t = -1;
                        if (!lob.isLinkedToTable()) {
                            t = -2;
                        }
                        writeVarInt(t);
                        writeVarInt(lob.getTableId());
                        writeVarInt(lob.getObjectId());
                        writeVarLong(lob.getPrecision());
                        writeByte((byte) (lob.isCompressed() ? 1 : 0));
                        if (t == -2) {
                            writeString(lob.getFileName());
                        }
                    } else {
                        writeVarInt(small.length);
                        write(small, 0, small.length);
                    }
                } else {
                    ValueLobDb lob = (ValueLobDb) v;
                    byte[] small = lob.getSmall();
                    if (small == null) {
                        writeVarInt(-3);
                        writeVarInt(lob.getTableId());
                        writeVarLong(lob.getLobId());
                        writeVarLong(lob.getPrecision());
                    } else {
                        writeVarInt(small.length);
                        write(small, 0, small.length);
                    }
                }
                break;
            }
        case Value.ARRAY:
            {
                writeByte((byte) type);
                Value[] list = ((ValueArray) v).getList();
                writeVarInt(list.length);
                for (Value x : list) {
                    writeValue(x);
                }
                break;
            }
        case Value.RESULT_SET:
            {
                writeByte((byte) type);
                try {
                    ResultSet rs = ((ValueResultSet) v).getResultSet();
                    rs.beforeFirst();
                    ResultSetMetaData meta = rs.getMetaData();
                    int columnCount = meta.getColumnCount();
                    writeVarInt(columnCount);
                    for (int i = 0; i < columnCount; i++) {
                        writeString(meta.getColumnName(i + 1));
                        writeVarInt(meta.getColumnType(i + 1));
                        writeVarInt(meta.getPrecision(i + 1));
                        writeVarInt(meta.getScale(i + 1));
                    }
                    while (rs.next()) {
                        writeByte((byte) 1);
                        for (int i = 0; i < columnCount; i++) {
                            int t = DataType.getValueTypeFromResultSet(meta, i + 1);
                            Value val = DataType.readValue(null, rs, i + 1, t);
                            writeValue(val);
                        }
                    }
                    writeByte((byte) 0);
                    rs.beforeFirst();
                } catch (SQLException e) {
                    throw DbException.convert(e);
                }
                break;
            }
        default:
            DbException.throwInternalError("type=" + v.getType());
    }
    if (SysProperties.CHECK2) {
        if (pos - start != getValueLen(v, handler)) {
            throw DbException.throwInternalError("value size error: got " + (pos - start) + " expected " + getValueLen(v, handler));
        }
    }
}
Also used : ValueUuid(org.h2.value.ValueUuid) ValueLob(org.h2.value.ValueLob) ValueLobDb(org.h2.value.ValueLobDb) SQLException(java.sql.SQLException) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) ValueString(org.h2.value.ValueString) Timestamp(java.sql.Timestamp) ValueTimestamp(org.h2.value.ValueTimestamp) BigDecimal(java.math.BigDecimal) ValueTime(org.h2.value.ValueTime) ResultSetMetaData(java.sql.ResultSetMetaData) ValueTimestamp(org.h2.value.ValueTimestamp) Value(org.h2.value.Value) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet) ValueResultSet(org.h2.value.ValueResultSet) BigInteger(java.math.BigInteger)

Example 5 with ValueResultSet

use of org.h2.value.ValueResultSet in project h2database by h2database.

the class FunctionTable method getResult.

/**
 * Read the result from the function. This method buffers the result in a
 * temporary file.
 *
 * @param session the session
 * @return the result
 */
public ResultInterface getResult(Session session) {
    ValueResultSet v = getValueResultSet(session);
    if (v == null) {
        return null;
    }
    if (cachedResult != null && cachedValue == v) {
        cachedResult.reset();
        return cachedResult;
    }
    LocalResult result = LocalResult.read(session, v.getResultSet(), 0);
    if (function.isDeterministic()) {
        cachedResult = result;
        cachedValue = v;
    }
    return result;
}
Also used : LocalResult(org.h2.result.LocalResult) ValueResultSet(org.h2.value.ValueResultSet)

Aggregations

ValueResultSet (org.h2.value.ValueResultSet)5 SimpleResultSet (org.h2.tools.SimpleResultSet)4 Value (org.h2.value.Value)4 ResultSet (java.sql.ResultSet)3 ResultSetMetaData (java.sql.ResultSetMetaData)3 SQLException (java.sql.SQLException)3 BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2 LocalResult (org.h2.result.LocalResult)2 ValueLobDb (org.h2.value.ValueLobDb)2 ValueString (org.h2.value.ValueString)2 ValueTime (org.h2.value.ValueTime)2 ValueTimestamp (org.h2.value.ValueTimestamp)2 ValueTimestampTimeZone (org.h2.value.ValueTimestampTimeZone)2 ValueUuid (org.h2.value.ValueUuid)2 Timestamp (java.sql.Timestamp)1 Database (org.h2.engine.Database)1 Column (org.h2.table.Column)1 ValueArray (org.h2.value.ValueArray)1 ValueLob (org.h2.value.ValueLob)1