Search in sources :

Example 46 with Table

use of org.h2.table.Table in project aries by apache.

the class AbstractTransactionTest method initTestTable.

protected void initTestTable(String jdbcUrl) throws SQLException {
    Driver d = new Driver();
    try (Connection c = d.connect(jdbcUrl, null)) {
        Statement s = c.createStatement();
        try {
            s.execute("DROP TABLE TEST_TABLE");
        } catch (SQLException sqle) {
        }
        s.execute("CREATE TABLE TEST_TABLE ( message varchar(255) )");
        c.commit();
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) Driver(org.h2.Driver)

Example 47 with Table

use of org.h2.table.Table in project ignite by apache.

the class DmlAstUtils method selectForUpdate.

/**
 * Generate SQL SELECT based on UPDATE's WHERE, LIMIT, etc.
 *
 * @param update Update statement.
 * @param keysParamIdx Index of new param for the array of keys.
 * @return SELECT statement.
 */
public static GridSqlSelect selectForUpdate(GridSqlUpdate update, @Nullable Integer keysParamIdx) {
    GridSqlSelect mapQry = new GridSqlSelect();
    mapQry.from(update.target());
    Set<GridSqlTable> tbls = new HashSet<>();
    collectAllGridTablesInTarget(update.target(), tbls);
    assert tbls.size() == 1 : "Failed to determine target table for UPDATE";
    GridSqlTable tbl = tbls.iterator().next();
    GridH2Table gridTbl = tbl.dataTable();
    assert gridTbl != null : "Failed to determine target grid table for UPDATE";
    Column h2KeyCol = gridTbl.getColumn(GridH2KeyValueRowOnheap.KEY_COL);
    Column h2ValCol = gridTbl.getColumn(GridH2KeyValueRowOnheap.VAL_COL);
    GridSqlColumn keyCol = new GridSqlColumn(h2KeyCol, tbl, h2KeyCol.getName());
    keyCol.resultType(GridSqlType.fromColumn(h2KeyCol));
    GridSqlColumn valCol = new GridSqlColumn(h2ValCol, tbl, h2ValCol.getName());
    valCol.resultType(GridSqlType.fromColumn(h2ValCol));
    mapQry.addColumn(keyCol, true);
    mapQry.addColumn(valCol, true);
    for (GridSqlColumn c : update.cols()) {
        String newColName = Parser.quoteIdentifier("_upd_" + c.columnName());
        // We have to use aliases to cover cases when the user
        // wants to update _val field directly (if it's a literal)
        GridSqlAlias alias = new GridSqlAlias(newColName, elementOrDefault(update.set().get(c.columnName()), c), true);
        alias.resultType(c.resultType());
        mapQry.addColumn(alias, true);
    }
    GridSqlElement where = update.where();
    if (keysParamIdx != null)
        where = injectKeysFilterParam(where, keyCol, keysParamIdx);
    mapQry.where(where);
    mapQry.limit(update.limit());
    return mapQry;
}
Also used : GridSqlAlias(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlias) Column(org.h2.table.Column) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) GridSqlTable(org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) ValueString(org.h2.value.ValueString) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect) HashSet(java.util.HashSet)

Example 48 with Table

use of org.h2.table.Table in project ignite by apache.

the class H2Utils method createSpatialIndex.

/**
 * Create spatial index.
 *
 * @param tbl Table.
 * @param idxName Index name.
 * @param cols Columns.
 */
public static GridH2IndexBase createSpatialIndex(GridH2Table tbl, String idxName, IndexColumn[] cols) {
    try {
        Class<?> cls = Class.forName(SPATIAL_IDX_CLS);
        Constructor<?> ctor = cls.getConstructor(GridH2Table.class, String.class, Integer.TYPE, IndexColumn[].class);
        if (!ctor.isAccessible())
            ctor.setAccessible(true);
        final int segments = tbl.rowDescriptor().context().config().getQueryParallelism();
        return (GridH2IndexBase) ctor.newInstance(tbl, idxName, segments, cols);
    } catch (Exception e) {
        throw new IgniteException("Failed to instantiate: " + SPATIAL_IDX_CLS, e);
    }
}
Also used : GridH2IndexBase(org.apache.ignite.internal.processors.query.h2.opt.GridH2IndexBase) IgniteException(org.apache.ignite.IgniteException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) SQLException(java.sql.SQLException) IndexColumn(org.h2.table.IndexColumn)

Example 49 with Table

use of org.h2.table.Table in project ignite by apache.

the class GridH2Table method update.

/**
 * Updates table for given key. If value is null then row with given key will be removed from table,
 * otherwise value and expiration time will be updated or new row will be added.
 *
 * @param row Row to be updated.
 * @param prevRow Previous row.
 * @param prevRowAvailable Whether previous row is available.
 * @throws IgniteCheckedException If failed.
 */
public void update(CacheDataRow row, @Nullable CacheDataRow prevRow, boolean prevRowAvailable) throws IgniteCheckedException {
    assert desc != null;
    GridH2KeyValueRowOnheap row0 = (GridH2KeyValueRowOnheap) desc.createRow(row);
    GridH2KeyValueRowOnheap prevRow0 = prevRow != null ? (GridH2KeyValueRowOnheap) desc.createRow(prevRow) : null;
    row0.prepareValuesCache();
    if (prevRow0 != null)
        prevRow0.prepareValuesCache();
    try {
        lock(false);
        try {
            ensureNotDestroyed();
            boolean replaced;
            if (prevRowAvailable)
                replaced = pk().putx(row0);
            else {
                prevRow0 = (GridH2KeyValueRowOnheap) pk().put(row0);
                replaced = prevRow0 != null;
            }
            if (!replaced)
                size.increment();
            for (int i = pkIndexPos + 1, len = idxs.size(); i < len; i++) {
                Index idx = idxs.get(i);
                if (idx instanceof GridH2IndexBase)
                    addToIndex((GridH2IndexBase) idx, row0, prevRow0);
            }
            if (!tmpIdxs.isEmpty()) {
                for (GridH2IndexBase idx : tmpIdxs.values()) addToIndex(idx, row0, prevRow0);
            }
        } finally {
            unlock(false);
        }
    } finally {
        row0.clearValuesCache();
        if (prevRow0 != null)
            prevRow0.clearValuesCache();
    }
}
Also used : Index(org.h2.index.Index) SpatialIndex(org.h2.index.SpatialIndex) H2TreeIndex(org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex)

Example 50 with Table

use of org.h2.table.Table in project ignite by apache.

the class H2DynamicTableSelfTest method testSqlFlagCompatibilityCheck.

/**
 * Test that attempting to start a node that has a cache with the name already present in the grid and whose
 * SQL flag does not match that of cache with the same name that is already started, yields an error.
 * @throws Exception if failed.
 */
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
public void testSqlFlagCompatibilityCheck() throws Exception {
    execute("CREATE TABLE \"Person\" (\"id\" int, \"city\" varchar, \"name\" varchar, \"surname\" varchar, " + "\"age\" int, PRIMARY KEY (\"id\", \"city\")) WITH \"template=cache\"");
    GridTestUtils.assertThrows(null, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            String cacheName = cacheName("Person");
            Ignition.start(clientConfiguration(5).setCacheConfiguration(new CacheConfiguration(cacheName)));
            return null;
        }
    }, IgniteException.class, "Cache configuration mismatch (local cache was created via Ignite API, while " + "remote cache was created via CREATE TABLE): SQL_PUBLIC_Person");
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteException(org.apache.ignite.IgniteException) SQLException(java.sql.SQLException) CacheException(javax.cache.CacheException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) JdbcSQLException(org.h2.jdbc.JdbcSQLException) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

Column (org.h2.table.Column)20 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)18 Statement (java.sql.Statement)13 SQLException (java.sql.SQLException)12 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)12 IndexColumn (org.h2.table.IndexColumn)11 ResultSet (java.sql.ResultSet)9 IgniteException (org.apache.ignite.IgniteException)9 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)8 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)8 SimpleResultSet (org.h2.tools.SimpleResultSet)8 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)7 Table (org.h2.table.Table)7 Connection (java.sql.Connection)6 HashSet (java.util.HashSet)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)6 Prepared (org.h2.command.Prepared)6 ValueString (org.h2.value.ValueString)6 PreparedStatement (java.sql.PreparedStatement)5