Search in sources :

Example 71 with Value

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

the class Query method getParameterValues.

public final Value[] getParameterValues() {
    ArrayList<Parameter> list = getParameters();
    if (list == null) {
        list = New.arrayList();
    }
    int size = list.size();
    Value[] params = new Value[size];
    for (int i = 0; i < size; i++) {
        Value v = list.get(i).getParamValue();
        params[i] = v;
    }
    return params;
}
Also used : Value(org.h2.value.Value) Parameter(org.h2.expression.Parameter)

Example 72 with Value

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

the class Query method sameResultAsLast.

private boolean sameResultAsLast(Session s, Value[] params, Value[] lastParams, long lastEval) {
    if (!cacheableChecked) {
        long max = getMaxDataModificationId();
        noCache = max == Long.MAX_VALUE;
        cacheableChecked = true;
    }
    if (noCache) {
        return false;
    }
    Database db = s.getDatabase();
    for (int i = 0; i < params.length; i++) {
        Value a = lastParams[i], b = params[i];
        if (a.getType() != b.getType() || !db.areEqual(a, b)) {
            return false;
        }
    }
    if (!isEverything(ExpressionVisitor.DETERMINISTIC_VISITOR) || !isEverything(ExpressionVisitor.INDEPENDENT_VISITOR)) {
        return false;
    }
    if (db.getModificationDataId() > lastEval && getMaxDataModificationId() > lastEval) {
        return false;
    }
    return true;
}
Also used : Database(org.h2.engine.Database) Value(org.h2.value.Value)

Example 73 with Value

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

the class ScriptCommand method add.

private void add(String s, boolean insert) throws IOException {
    if (s == null) {
        return;
    }
    if (lineSeparator.length > 1 || lineSeparator[0] != '\n') {
        s = StringUtils.replaceAll(s, "\n", lineSeparatorString);
    }
    s += ";";
    if (out != null) {
        byte[] buff = s.getBytes(charset);
        int len = MathUtils.roundUpInt(buff.length + lineSeparator.length, Constants.FILE_BLOCK_SIZE);
        buffer = Utils.copy(buff, buffer);
        if (len > buffer.length) {
            buffer = new byte[len];
        }
        System.arraycopy(buff, 0, buffer, 0, buff.length);
        for (int i = buff.length; i < len - lineSeparator.length; i++) {
            buffer[i] = ' ';
        }
        for (int j = 0, i = len - lineSeparator.length; i < len; i++, j++) {
            buffer[i] = lineSeparator[j];
        }
        out.write(buffer, 0, len);
        if (!insert) {
            Value[] row = { ValueString.get(s) };
            result.addRow(row);
        }
    } else {
        Value[] row = { ValueString.get(s) };
        result.addRow(row);
    }
}
Also used : Value(org.h2.value.Value) Constraint(org.h2.constraint.Constraint)

Example 74 with Value

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

the class ScriptCommand method writeLobStream.

private int writeLobStream(Value v) throws IOException {
    if (!tempLobTableCreated) {
        add("CREATE TABLE IF NOT EXISTS SYSTEM_LOB_STREAM" + "(ID INT NOT NULL, PART INT NOT NULL, " + "CDATA VARCHAR, BDATA BINARY)", true);
        add("CREATE PRIMARY KEY SYSTEM_LOB_STREAM_PRIMARY_KEY " + "ON SYSTEM_LOB_STREAM(ID, PART)", true);
        add("CREATE ALIAS IF NOT EXISTS " + "SYSTEM_COMBINE_CLOB FOR \"" + this.getClass().getName() + ".combineClob\"", true);
        add("CREATE ALIAS IF NOT EXISTS " + "SYSTEM_COMBINE_BLOB FOR \"" + this.getClass().getName() + ".combineBlob\"", true);
        tempLobTableCreated = true;
    }
    int id = nextLobId++;
    switch(v.getType()) {
        case Value.BLOB:
            {
                byte[] bytes = new byte[lobBlockSize];
                try (InputStream input = v.getInputStream()) {
                    for (int i = 0; ; i++) {
                        StringBuilder buff = new StringBuilder(lobBlockSize * 2);
                        buff.append("INSERT INTO SYSTEM_LOB_STREAM VALUES(").append(id).append(", ").append(i).append(", NULL, '");
                        int len = IOUtils.readFully(input, bytes, lobBlockSize);
                        if (len <= 0) {
                            break;
                        }
                        buff.append(StringUtils.convertBytesToHex(bytes, len)).append("')");
                        String sql = buff.toString();
                        add(sql, true);
                    }
                }
                break;
            }
        case Value.CLOB:
            {
                char[] chars = new char[lobBlockSize];
                try (Reader reader = v.getReader()) {
                    for (int i = 0; ; i++) {
                        StringBuilder buff = new StringBuilder(lobBlockSize * 2);
                        buff.append("INSERT INTO SYSTEM_LOB_STREAM VALUES(").append(id).append(", ").append(i).append(", ");
                        int len = IOUtils.readFully(reader, chars, lobBlockSize);
                        if (len == 0) {
                            break;
                        }
                        buff.append(StringUtils.quoteStringSQL(new String(chars, 0, len))).append(", NULL)");
                        String sql = buff.toString();
                        add(sql, true);
                    }
                }
                break;
            }
        default:
            DbException.throwInternalError("type:" + v.getType());
    }
    return id;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) ValueString(org.h2.value.ValueString) Constraint(org.h2.constraint.Constraint)

Example 75 with Value

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

the class Select method queryDistinct.

private void queryDistinct(ResultTarget result, long limitRows) {
    // limitRows is never 0 here
    if (limitRows > 0 && offsetExpr != null) {
        int offset = offsetExpr.getValue(session).getInt();
        if (offset > 0) {
            limitRows += offset;
        }
    }
    int rowNumber = 0;
    setCurrentRowNumber(0);
    Index index = topTableFilter.getIndex();
    SearchRow first = null;
    int columnIndex = index.getColumns()[0].getColumnId();
    int sampleSize = getSampleSizeValue(session);
    while (true) {
        setCurrentRowNumber(rowNumber + 1);
        Cursor cursor = index.findNext(session, first, null);
        if (!cursor.next()) {
            break;
        }
        SearchRow found = cursor.getSearchRow();
        Value value = found.getValue(columnIndex);
        if (first == null) {
            first = topTableFilter.getTable().getTemplateSimpleRow(true);
        }
        first.setValue(columnIndex, value);
        Value[] row = { value };
        result.addRow(row);
        rowNumber++;
        if ((sort == null || sortUsingIndex) && limitRows > 0 && rowNumber >= limitRows) {
            break;
        }
        if (sampleSize > 0 && rowNumber >= sampleSize) {
            break;
        }
    }
}
Also used : Value(org.h2.value.Value) Index(org.h2.index.Index) Cursor(org.h2.index.Cursor)

Aggregations

Value (org.h2.value.Value)291 SQLException (java.sql.SQLException)94 DbException (org.h2.message.DbException)91 ResultSet (java.sql.ResultSet)69 PreparedStatement (java.sql.PreparedStatement)61 Statement (java.sql.Statement)53 Column (org.h2.table.Column)44 ValueString (org.h2.value.ValueString)44 JdbcStatement (org.h2.jdbc.JdbcStatement)42 JdbcPreparedStatement (org.h2.jdbc.JdbcPreparedStatement)36 SimpleResultSet (org.h2.tools.SimpleResultSet)31 StatementBuilder (org.h2.util.StatementBuilder)28 IndexColumn (org.h2.table.IndexColumn)23 Expression (org.h2.expression.Expression)22 IOException (java.io.IOException)21 Row (org.h2.result.Row)19 SearchRow (org.h2.result.SearchRow)18 Connection (java.sql.Connection)17 ValueArray (org.h2.value.ValueArray)17 ValueTimestamp (org.h2.value.ValueTimestamp)17