Search in sources :

Example 6 with Builder

use of org.h2.mvstore.MVMap.Builder in project h2database by h2database.

the class UpdatableRow method readRow.

/**
 * Re-reads a row from the database and updates the values in the array.
 *
 * @param row the values that contain the key
 * @return the row
 * @throws SQLException on failure
 */
public Value[] readRow(Value[] row) throws SQLException {
    StringBuilder builder = new StringBuilder("SELECT ");
    appendColumnList(builder, false);
    builder.append(" FROM ");
    appendTableName(builder);
    appendKeyCondition(builder);
    PreparedStatement prep = conn.prepareStatement(builder.toString());
    setKey(prep, 1, row);
    JdbcResultSet rs = (JdbcResultSet) prep.executeQuery();
    if (!rs.next()) {
        throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
    }
    Value[] newRow = new Value[columnCount];
    for (int i = 0; i < columnCount; i++) {
        newRow[i] = ValueToObjectConverter.readValue(conn.getSession(), rs, i + 1);
    }
    return newRow;
}
Also used : Value(org.h2.value.Value) PreparedStatement(java.sql.PreparedStatement) JdbcResultSet(org.h2.jdbc.JdbcResultSet)

Example 7 with Builder

use of org.h2.mvstore.MVMap.Builder in project h2database by h2database.

the class UpdatableRow method insertRow.

/**
 * Insert a new row into the database.
 *
 * @param row the new row
 * @throws SQLException if the row could not be inserted
 */
public void insertRow(Value[] row) throws SQLException {
    StringBuilder builder = new StringBuilder("INSERT INTO ");
    appendTableName(builder);
    builder.append('(');
    appendColumnList(builder, false);
    builder.append(")VALUES(");
    for (int i = 0; i < columnCount; i++) {
        if (i > 0) {
            builder.append(',');
        }
        Value v = row[i];
        if (v == null) {
            builder.append("DEFAULT");
        } else {
            builder.append('?');
        }
    }
    builder.append(')');
    PreparedStatement prep = conn.prepareStatement(builder.toString());
    for (int i = 0, j = 0; i < columnCount; i++) {
        Value v = row[i];
        if (v != null) {
            JdbcUtils.set(prep, j++ + 1, v, conn);
        }
    }
    int count = prep.executeUpdate();
    if (count != 1) {
        throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
    }
}
Also used : Value(org.h2.value.Value) PreparedStatement(java.sql.PreparedStatement)

Example 8 with Builder

use of org.h2.mvstore.MVMap.Builder in project h2database by h2database.

the class DefaultRow method toString.

@Override
public String toString() {
    StringBuilder builder = new StringBuilder("( /* key:").append(key).append(" */ ");
    for (int i = 0, length = data.length; i < length; i++) {
        if (i > 0) {
            builder.append(", ");
        }
        Value v = data[i];
        builder.append(v == null ? "null" : v.getTraceSQL());
    }
    return builder.append(')').toString();
}
Also used : Value(org.h2.value.Value) ValueBigint(org.h2.value.ValueBigint)

Example 9 with Builder

use of org.h2.mvstore.MVMap.Builder in project h2database by h2database.

the class Aggregate method getListaggTruncate.

private StringBuilder getListaggTruncate(Value[] array, String separator, String filter, boolean withoutCount) {
    int count = array.length;
    String[] strings = new String[count];
    String s = getListaggItem(array[0]);
    strings[0] = s;
    StringBuilder builder = new StringBuilder(s);
    loop: for (int i = 1; i < count; i++) {
        builder.append(separator).append(strings[i] = s = getListaggItem(array[i]));
        int length = builder.length();
        if (length > Constants.MAX_STRING_LENGTH) {
            for (; i > 0; i--) {
                length -= strings[i].length();
                builder.setLength(length);
                builder.append(filter);
                if (!withoutCount) {
                    builder.append('(').append(count - i).append(')');
                }
                if (builder.length() <= Constants.MAX_STRING_LENGTH) {
                    break loop;
                }
                length -= separator.length();
            }
            builder.setLength(0);
            builder.append(filter).append('(').append(count).append(')');
            break;
        }
    }
    return builder;
}
Also used : ValueBigint(org.h2.value.ValueBigint)

Example 10 with Builder

use of org.h2.mvstore.MVMap.Builder in project h2database by h2database.

the class ConcatenationOperation method getValue.

private Value getValue(SessionLocal session, int l) {
    Value[] values = new Value[l];
    for (int i = 0; i < l; i++) {
        Value v = args[i].getValue(session).convertTo(type, session);
        if (v == ValueNull.INSTANCE) {
            return ValueNull.INSTANCE;
        }
        values[i] = v;
    }
    int valueType = type.getValueType();
    if (valueType == Value.VARCHAR) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < l; i++) {
            builder.append(values[i].getString());
        }
        return ValueVarchar.get(builder.toString(), session);
    } else if (valueType == Value.VARBINARY) {
        int totalLength = 0;
        for (int i = 0; i < l; i++) {
            totalLength += values[i].getBytesNoCopy().length;
        }
        byte[] v = new byte[totalLength];
        int offset = 0;
        for (int i = 0; i < l; i++) {
            byte[] a = values[i].getBytesNoCopy();
            int length = a.length;
            System.arraycopy(a, 0, v, offset, length);
            offset += length;
        }
        return ValueVarbinary.getNoCopy(v);
    } else {
        int totalLength = 0;
        for (int i = 0; i < l; i++) {
            totalLength += ((ValueArray) values[i]).getList().length;
        }
        Value[] v = new Value[totalLength];
        int offset = 0;
        for (int i = 0; i < l; i++) {
            Value[] a = ((ValueArray) values[i]).getList();
            int length = a.length;
            System.arraycopy(a, 0, v, offset, length);
            offset += length;
        }
        return ValueArray.get((TypeInfo) type.getExtTypeInfo(), v, session);
    }
}
Also used : Value(org.h2.value.Value) TypeInfo(org.h2.value.TypeInfo)

Aggregations

Value (org.h2.value.Value)43 Column (org.h2.table.Column)22 MVStore (org.h2.mvstore.MVStore)20 PreparedStatement (java.sql.PreparedStatement)19 DbException (org.h2.message.DbException)19 Constraint (org.h2.constraint.Constraint)16 ValueBigint (org.h2.value.ValueBigint)16 ResultSet (java.sql.ResultSet)14 ArrayList (java.util.ArrayList)14 Index (org.h2.index.Index)14 Table (org.h2.table.Table)14 Row (org.h2.result.Row)10 IndexColumn (org.h2.table.IndexColumn)10 IOException (java.io.IOException)9 Connection (java.sql.Connection)8 Database (org.h2.engine.Database)8 DbObject (org.h2.engine.DbObject)8 ResultInterface (org.h2.result.ResultInterface)8 Schema (org.h2.schema.Schema)8 Sequence (org.h2.schema.Sequence)8