Search in sources :

Example 11 with Row

use of org.h2.result.Row in project ignite by apache.

the class GridSqlInsert method getSQL.

/** {@inheritDoc} */
@Override
public String getSQL() {
    StatementBuilder buff = new StatementBuilder(explain() ? "EXPLAIN " : "");
    buff.append("INSERT").append("\nINTO ").append(into.getSQL()).append('(');
    for (GridSqlColumn col : cols) {
        buff.appendExceptFirst(", ");
        buff.append('\n').append(col.getSQL());
    }
    buff.append("\n)\n");
    if (direct)
        buff.append("DIRECT ");
    if (sorted)
        buff.append("SORTED ");
    if (!rows.isEmpty()) {
        buff.append("VALUES\n");
        StatementBuilder valuesBuff = new StatementBuilder();
        for (GridSqlElement[] row : rows()) {
            valuesBuff.appendExceptFirst(",\n");
            StatementBuilder rowBuff = new StatementBuilder("(");
            for (GridSqlElement e : row) {
                rowBuff.appendExceptFirst(", ");
                rowBuff.append(e != null ? e.getSQL() : "DEFAULT");
            }
            rowBuff.append(')');
            valuesBuff.append(rowBuff.toString());
        }
        buff.append(valuesBuff.toString());
    } else
        buff.append('\n').append(qry.getSQL());
    return buff.toString();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Example 12 with Row

use of org.h2.result.Row in project ignite by apache.

the class GridSqlMerge method getSQL.

/** {@inheritDoc} */
@Override
public String getSQL() {
    StatementBuilder buff = new StatementBuilder(explain() ? "EXPLAIN " : "");
    buff.append("MERGE INTO ").append(into.getSQL()).append("(");
    for (GridSqlColumn col : cols) {
        buff.appendExceptFirst(", ");
        buff.append('\n').append(col.getSQL());
    }
    buff.append("\n)\n");
    if (keys != null) {
        buff.append("KEY(\n");
        buff.resetCount();
        for (GridSqlColumn c : keys) {
            buff.appendExceptFirst(", ");
            buff.append(c.getSQL()).append('\n');
        }
        buff.append(")\n");
    }
    if (!rows.isEmpty()) {
        buff.append("VALUES\n");
        StatementBuilder valuesBuff = new StatementBuilder();
        for (GridSqlElement[] row : rows()) {
            valuesBuff.appendExceptFirst(",\n");
            StatementBuilder rowBuff = new StatementBuilder("(");
            for (GridSqlElement e : row) {
                rowBuff.appendExceptFirst(", ");
                rowBuff.append(e != null ? e.getSQL() : "DEFAULT");
            }
            rowBuff.append(')');
            valuesBuff.append(rowBuff.toString());
        }
        buff.append(valuesBuff.toString());
    } else
        buff.append('\n').append(qry.getSQL());
    return buff.toString();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Example 13 with Row

use of org.h2.result.Row in project ignite by apache.

the class DmlStatementsProcessor method streamUpdateQuery.

/**
     * Perform given statement against given data streamer. Only rows based INSERT and MERGE are supported
     * as well as key bound UPDATE and DELETE (ones with filter {@code WHERE _key = ?}).
     *
     * @param streamer Streamer to feed data to.
     * @param stmt Statement.
     * @param args Statement arguments.
     * @return Number of rows in given statement for INSERT and MERGE, {@code 1} otherwise.
     * @throws IgniteCheckedException if failed.
     */
@SuppressWarnings({ "unchecked", "ConstantConditions" })
long streamUpdateQuery(IgniteDataStreamer streamer, PreparedStatement stmt, Object[] args) throws IgniteCheckedException {
    args = U.firstNotNull(args, X.EMPTY_OBJECT_ARRAY);
    Prepared p = GridSqlQueryParser.prepared(stmt);
    assert p != null;
    UpdatePlan plan = UpdatePlanBuilder.planForStatement(p, null);
    if (!F.eq(streamer.cacheName(), plan.tbl.rowDescriptor().context().name()))
        throw new IgniteSQLException("Cross cache streaming is not supported, please specify cache explicitly" + " in connection options", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
    if (plan.mode == UpdateMode.INSERT && plan.rowsNum > 0) {
        assert plan.isLocSubqry;
        final GridCacheContext cctx = plan.tbl.rowDescriptor().context();
        QueryCursorImpl<List<?>> cur;
        final ArrayList<List<?>> data = new ArrayList<>(plan.rowsNum);
        final GridQueryFieldsResult res = idx.queryLocalSqlFields(idx.schema(cctx.name()), plan.selectQry, F.asList(args), null, false, 0, null);
        QueryCursorImpl<List<?>> stepCur = new QueryCursorImpl<>(new Iterable<List<?>>() {

            @Override
            public Iterator<List<?>> iterator() {
                try {
                    return new GridQueryCacheObjectsIterator(res.iterator(), idx.objectContext(), cctx.keepBinary());
                } catch (IgniteCheckedException e) {
                    throw new IgniteException(e);
                }
            }
        }, null);
        data.addAll(stepCur.getAll());
        cur = new QueryCursorImpl<>(new Iterable<List<?>>() {

            @Override
            public Iterator<List<?>> iterator() {
                return data.iterator();
            }
        }, null);
        if (plan.rowsNum == 1) {
            IgniteBiTuple t = rowToKeyValue(cctx, cur.iterator().next(), plan);
            streamer.addData(t.getKey(), t.getValue());
            return 1;
        }
        Map<Object, Object> rows = new LinkedHashMap<>(plan.rowsNum);
        for (List<?> row : cur) {
            final IgniteBiTuple t = rowToKeyValue(cctx, row, plan);
            rows.put(t.getKey(), t.getValue());
        }
        streamer.addData(rows);
        return rows.size();
    } else
        throw new IgniteSQLException("Only tuple based INSERT statements are supported in streaming mode", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Prepared(org.h2.command.Prepared) ArrayList(java.util.ArrayList) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) GridQueryFieldsResult(org.apache.ignite.internal.processors.query.GridQueryFieldsResult) LinkedHashMap(java.util.LinkedHashMap) GridBoundedConcurrentLinkedHashMap(org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashMap) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) IgniteSingletonIterator(org.apache.ignite.internal.util.lang.IgniteSingletonIterator) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) BinaryObject(org.apache.ignite.binary.BinaryObject) UpdatePlan(org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan)

Example 14 with Row

use of org.h2.result.Row in project ignite by apache.

the class IgniteH2Indexing method rebuildIndexesFromHash.

/** {@inheritDoc} */
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
@Override
public void rebuildIndexesFromHash(GridCacheContext cctx, String schemaName, String typeName) throws IgniteCheckedException {
    H2TableDescriptor tbl = tableDescriptor(schemaName, typeName);
    if (tbl == null)
        return;
    assert tbl.table() != null;
    assert tbl.table().rebuildFromHashInProgress();
    H2PkHashIndex hashIdx = tbl.primaryKeyHashIndex();
    Cursor cursor = hashIdx.find((Session) null, null, null);
    while (cursor.next()) {
        CacheDataRow dataRow = (CacheDataRow) cursor.get();
        boolean done = false;
        while (!done) {
            GridCacheEntryEx entry = cctx.cache().entryEx(dataRow.key());
            try {
                synchronized (entry) {
                    // TODO : How to correctly get current value and link here?
                    GridH2Row row = tbl.table().rowDescriptor().createRow(entry.key(), entry.partition(), dataRow.value(), entry.version(), entry.expireTime());
                    row.link(dataRow.link());
                    List<Index> indexes = tbl.table().getAllIndexes();
                    for (int i = 2; i < indexes.size(); i++) {
                        Index idx = indexes.get(i);
                        if (idx instanceof H2TreeIndex)
                            ((H2TreeIndex) idx).put(row);
                    }
                    done = true;
                }
            } catch (GridCacheEntryRemovedException e) {
            // No-op
            }
        }
    }
    tbl.table().markRebuildFromHashInProgress(false);
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.database.CacheDataRow) H2TreeIndex(org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) GridH2Row(org.apache.ignite.internal.processors.query.h2.opt.GridH2Row) Index(org.h2.index.Index) H2TreeIndex(org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex) H2PkHashIndex(org.apache.ignite.internal.processors.query.h2.database.H2PkHashIndex) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) H2PkHashIndex(org.apache.ignite.internal.processors.query.h2.database.H2PkHashIndex) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cursor(org.h2.index.Cursor) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor)

Example 15 with Row

use of org.h2.result.Row in project ignite by apache.

the class GridH2AbstractKeyValueRow method toString.

/** {@inheritDoc} */
@Override
public String toString() {
    SB sb = new SB("Row@");
    sb.a(Integer.toHexString(System.identityHashCode(this)));
    addOffheapRowId(sb);
    Value v = peekValue(KEY_COL);
    sb.a("[ key: ").a(v == null ? "nil" : v.getString());
    v = WeakValue.unwrap(peekValue(VAL_COL));
    sb.a(", val: ").a(v == null ? "nil" : v.getString());
    v = peekValue(VER_COL);
    sb.a(", ver: ").a(v == null ? "nil" : v.getString());
    sb.a(" ][ ");
    if (v != null) {
        for (int i = DEFAULT_COLUMNS_COUNT, cnt = getColumnCount(); i < cnt; i++) {
            v = getValue(i);
            if (i != DEFAULT_COLUMNS_COUNT)
                sb.a(", ");
            if (!desc.isKeyValueOrVersionColumn(i))
                sb.a(v == null ? "nil" : v.getString());
        }
    }
    sb.a(" ]");
    return sb.toString();
}
Also used : Value(org.h2.value.Value) SB(org.apache.ignite.internal.util.typedef.internal.SB)

Aggregations

Value (org.h2.value.Value)12 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)8 ArrayList (java.util.ArrayList)4 CacheException (javax.cache.CacheException)4 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)4 H2PkHashIndex (org.apache.ignite.internal.processors.query.h2.database.H2PkHashIndex)4 Index (org.h2.index.Index)4 ResultSet (java.sql.ResultSet)3 UUID (java.util.UUID)3 BinaryObject (org.apache.ignite.binary.BinaryObject)3 GridH2ValueMessage (org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2ValueMessage)3 SearchRow (org.h2.result.SearchRow)3 Column (org.h2.table.Column)3 IndexColumn (org.h2.table.IndexColumn)3 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Random (java.util.Random)2 Lock (java.util.concurrent.locks.Lock)2 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)2 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)2