Search in sources :

Example 56 with Row

use of org.h2.result.Row in project h2database by h2database.

the class Aggregate method getValue.

@Override
public Value getValue(Session session) {
    if (select.isQuickAggregateQuery()) {
        switch(type) {
            case COUNT:
            case COUNT_ALL:
                Table table = select.getTopTableFilter().getTable();
                return ValueLong.get(table.getRowCount(session));
            case MIN:
            case MAX:
                {
                    boolean first = type == AggregateType.MIN;
                    Index index = getMinMaxColumnIndex();
                    int sortType = index.getIndexColumns()[0].sortType;
                    if ((sortType & SortOrder.DESCENDING) != 0) {
                        first = !first;
                    }
                    Cursor cursor = index.findFirstOrLast(session, first);
                    SearchRow row = cursor.getSearchRow();
                    Value v;
                    if (row == null) {
                        v = ValueNull.INSTANCE;
                    } else {
                        v = row.getValue(index.getColumns()[0].getColumnId());
                    }
                    return v;
                }
            case MEDIAN:
                {
                    return AggregateDataMedian.getResultFromIndex(session, on, dataType);
                }
            default:
                DbException.throwInternalError("type=" + type);
        }
    }
    HashMap<Expression, Object> group = select.getCurrentGroup();
    if (group == null) {
        throw DbException.get(ErrorCode.INVALID_USE_OF_AGGREGATE_FUNCTION_1, getSQL());
    }
    AggregateData data = (AggregateData) group.get(this);
    if (data == null) {
        data = AggregateData.create(type);
    }
    if (type == AggregateType.GROUP_CONCAT) {
        Value[] array = ((AggregateDataCollecting) data).getArray();
        if (array == null) {
            return ValueNull.INSTANCE;
        }
        if (orderByList != null || distinct) {
            sortWithOrderBy(array);
        }
        StatementBuilder buff = new StatementBuilder();
        String sep = groupConcatSeparator == null ? "," : groupConcatSeparator.getValue(session).getString();
        for (Value val : array) {
            String s;
            if (val.getType() == Value.ARRAY) {
                s = ((ValueArray) val).getList()[0].getString();
            } else {
                s = val.getString();
            }
            if (s == null) {
                continue;
            }
            if (sep != null) {
                buff.appendExceptFirst(sep);
            }
            buff.append(s);
        }
        return ValueString.get(buff.toString());
    } else if (type == AggregateType.ARRAY_AGG) {
        Value[] array = ((AggregateDataCollecting) data).getArray();
        if (array == null) {
            return ValueNull.INSTANCE;
        }
        if (orderByList != null || distinct) {
            sortWithOrderBy(array);
        }
        if (orderByList != null) {
            for (int i = 0; i < array.length; i++) {
                array[i] = ((ValueArray) array[i]).getList()[0];
            }
        }
        return ValueArray.get(array);
    }
    return data.getValue(session.getDatabase(), dataType, distinct);
}
Also used : Table(org.h2.table.Table) Index(org.h2.index.Index) ValueString(org.h2.value.ValueString) Cursor(org.h2.index.Cursor) StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value) SearchRow(org.h2.result.SearchRow)

Example 57 with Row

use of org.h2.result.Row in project h2database by h2database.

the class LinkedIndex method add.

@Override
public void add(Session session, Row row) {
    ArrayList<Value> params = New.arrayList();
    StatementBuilder buff = new StatementBuilder("INSERT INTO ");
    buff.append(targetTableName).append(" VALUES(");
    for (int i = 0; i < row.getColumnCount(); i++) {
        Value v = row.getValue(i);
        buff.appendExceptFirst(", ");
        if (v == null) {
            buff.append("DEFAULT");
        } else if (isNull(v)) {
            buff.append("NULL");
        } else {
            buff.append('?');
            params.add(v);
        }
    }
    buff.append(')');
    String sql = buff.toString();
    try {
        link.execute(sql, params, true);
        rowCount++;
    } catch (Exception e) {
        throw TableLink.wrapException(sql, e);
    }
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value) DbException(org.h2.message.DbException)

Example 58 with Row

use of org.h2.result.Row in project h2database by h2database.

the class LinkedIndex method update.

/**
 * Update a row using a UPDATE statement. This method is to be called if the
 * emit updates option is enabled.
 *
 * @param oldRow the old data
 * @param newRow the new data
 */
public void update(Row oldRow, Row newRow) {
    ArrayList<Value> params = New.arrayList();
    StatementBuilder buff = new StatementBuilder("UPDATE ");
    buff.append(targetTableName).append(" SET ");
    for (int i = 0; i < newRow.getColumnCount(); i++) {
        buff.appendExceptFirst(", ");
        buff.append(table.getColumn(i).getSQL()).append('=');
        Value v = newRow.getValue(i);
        if (v == null) {
            buff.append("DEFAULT");
        } else {
            buff.append('?');
            params.add(v);
        }
    }
    buff.append(" WHERE ");
    buff.resetCount();
    for (int i = 0; i < oldRow.getColumnCount(); i++) {
        Column col = table.getColumn(i);
        buff.appendExceptFirst(" AND ");
        buff.append(col.getSQL());
        Value v = oldRow.getValue(i);
        if (isNull(v)) {
            buff.append(" IS NULL");
        } else {
            buff.append('=');
            params.add(v);
            addParameter(buff, col);
        }
    }
    String sql = buff.toString();
    try {
        link.execute(sql, params, true);
    } catch (Exception e) {
        throw TableLink.wrapException(sql, e);
    }
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value) DbException(org.h2.message.DbException)

Example 59 with Row

use of org.h2.result.Row in project h2database by h2database.

the class NonUniqueHashIndex method add.

@Override
public void add(Session session, Row row) {
    Value key = row.getValue(indexColumn);
    ArrayList<Long> positions = rows.get(key);
    if (positions == null) {
        positions = New.arrayList();
        rows.put(key, positions);
    }
    positions.add(row.getKey());
    rowCount++;
}
Also used : Value(org.h2.value.Value)

Example 60 with Row

use of org.h2.result.Row in project h2database by h2database.

the class PageLog method logAddOrRemoveRow.

/**
 * A record is added to a table, or removed from a table.
 *
 * @param session the session
 * @param tableId the table id
 * @param row the row to add
 * @param add true if the row is added, false if it is removed
 */
void logAddOrRemoveRow(Session session, int tableId, Row row, boolean add) {
    if (trace.isDebugEnabled()) {
        trace.debug("log " + (add ? "+" : "-") + " s: " + session.getId() + " table: " + tableId + " row: " + row);
    }
    session.addLogPos(logSectionId, logPos);
    logPos++;
    Data data = dataBuffer;
    data.reset();
    int columns = row.getColumnCount();
    data.writeVarInt(columns);
    data.checkCapacity(row.getByteCount(data));
    if (session.isRedoLogBinaryEnabled()) {
        for (int i = 0; i < columns; i++) {
            data.writeValue(row.getValue(i));
        }
    } else {
        for (int i = 0; i < columns; i++) {
            Value v = row.getValue(i);
            if (v.getType() == Value.BYTES) {
                data.writeValue(ValueNull.INSTANCE);
            } else {
                data.writeValue(v);
            }
        }
    }
    Data buffer = getBuffer();
    buffer.writeByte((byte) (add ? ADD : REMOVE));
    buffer.writeVarInt(session.getId());
    buffer.writeVarInt(tableId);
    buffer.writeVarLong(row.getKey());
    if (add) {
        buffer.writeVarInt(data.length());
        buffer.checkCapacity(data.length());
        buffer.write(data.getBytes(), 0, data.length());
    }
    write(buffer);
}
Also used : Value(org.h2.value.Value)

Aggregations

Value (org.h2.value.Value)118 Row (org.h2.result.Row)49 Column (org.h2.table.Column)48 DbException (org.h2.message.DbException)44 SearchRow (org.h2.result.SearchRow)37 SQLException (java.sql.SQLException)29 Index (org.h2.index.Index)28 IndexColumn (org.h2.table.IndexColumn)24 Cursor (org.h2.index.Cursor)21 PreparedStatement (java.sql.PreparedStatement)20 ResultSet (java.sql.ResultSet)20 StatementBuilder (org.h2.util.StatementBuilder)20 ArrayList (java.util.ArrayList)19 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)18 Expression (org.h2.expression.Expression)17 Statement (java.sql.Statement)16 Constraint (org.h2.constraint.Constraint)16 ValueString (org.h2.value.ValueString)16 Connection (java.sql.Connection)15 SimpleResultSet (org.h2.tools.SimpleResultSet)15