Search in sources :

Example 51 with User

use of org.h2.engine.User in project sonarqube by SonarSource.

the class EmbeddedDatabaseTest method checkDbIsUp.

private void checkDbIsUp(int port, String user, String password) {
    try {
        String driverUrl = String.format("jdbc:h2:tcp://%s:%d/sonar;USER=%s;PASSWORD=%s;NON_KEYWORDS=VALUE", LOOPBACK_ADDRESS, port, user, password);
        DriverManager.registerDriver(new Driver());
        DriverManager.getConnection(driverUrl).close();
    } catch (Exception ex) {
        fail("Unable to connect after start");
    }
}
Also used : Driver(org.h2.Driver) IOException(java.io.IOException)

Example 52 with User

use of org.h2.engine.User in project sql2o by aaberg.

the class H2Tests method testIssue172NPEWhenCreatingBasicDataSourceInline.

@Test
public void testIssue172NPEWhenCreatingBasicDataSourceInline() {
    DataSource ds = new JdbcDataSource() {

        {
            setURL(url);
            setUser(user);
            setPassword(pass);
        }
    };
    Sql2o sql2o = new Sql2o(ds);
    assertThat(sql2o.getQuirks(), is(instanceOf(H2Quirks.class)));
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Sql2o(org.sql2o.Sql2o) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 53 with User

use of org.h2.engine.User in project ignite by apache.

the class DmlStatementsProcessor method convert.

/**
     * Convert value to column's expected type by means of H2.
     *
     * @param val Source value.
     * @param desc Row descriptor.
     * @param expCls Expected value class.
     * @param type Expected column type to convert to.
     * @return Converted object.
     * @throws IgniteCheckedException if failed.
     */
@SuppressWarnings({ "ConstantConditions", "SuspiciousSystemArraycopy" })
private static Object convert(Object val, GridH2RowDescriptor desc, Class<?> expCls, int type) throws IgniteCheckedException {
    if (val == null)
        return null;
    Class<?> currCls = val.getClass();
    if (val instanceof Date && currCls != Date.class && expCls == Date.class) {
        // precise Date instance. Let's satisfy it.
        return new Date(((Date) val).getTime());
    }
    // User-given UUID is always serialized by H2 to byte array, so we have to deserialize manually
    if (type == Value.UUID && currCls == byte[].class)
        return U.unmarshal(desc.context().marshaller(), (byte[]) val, U.resolveClassLoader(desc.context().gridConfig()));
    // Still, we only can convert from Object[] to something more precise.
    if (type == Value.ARRAY && currCls != expCls) {
        if (currCls != Object[].class)
            throw new IgniteCheckedException("Unexpected array type - only conversion from Object[] is assumed");
        // Why would otherwise type be Value.ARRAY?
        assert expCls.isArray();
        Object[] curr = (Object[]) val;
        Object newArr = Array.newInstance(expCls.getComponentType(), curr.length);
        System.arraycopy(curr, 0, newArr, 0, curr.length);
        return newArr;
    }
    int objType = DataType.getTypeFromClass(val.getClass());
    if (objType == type)
        return val;
    Value h2Val = desc.wrap(val, objType);
    return h2Val.convertTo(type).getObject();
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Value(org.h2.value.Value) BinaryObject(org.apache.ignite.binary.BinaryObject) Date(java.util.Date)

Example 54 with User

use of org.h2.engine.User in project ignite by apache.

the class UpdatePlanBuilder method planForUpdate.

/**
     * Prepare update plan for UPDATE or DELETE.
     *
     * @param stmt UPDATE or DELETE statement.
     * @param errKeysPos index to inject param for re-run keys at. Null if it's not a re-run plan.
     * @return Update plan.
     * @throws IgniteCheckedException if failed.
     */
private static UpdatePlan planForUpdate(GridSqlStatement stmt, @Nullable Integer errKeysPos) throws IgniteCheckedException {
    GridSqlElement target;
    FastUpdateArguments fastUpdate;
    UpdateMode mode;
    if (stmt instanceof GridSqlUpdate) {
        // Let's verify that user is not trying to mess with key's columns directly
        verifyUpdateColumns(stmt);
        GridSqlUpdate update = (GridSqlUpdate) stmt;
        target = update.target();
        fastUpdate = DmlAstUtils.getFastUpdateArgs(update);
        mode = UpdateMode.UPDATE;
    } else if (stmt instanceof GridSqlDelete) {
        GridSqlDelete del = (GridSqlDelete) stmt;
        target = del.from();
        fastUpdate = DmlAstUtils.getFastDeleteArgs(del);
        mode = UpdateMode.DELETE;
    } else
        throw new IgniteSQLException("Unexpected DML operation [cls=" + stmt.getClass().getName() + ']', IgniteQueryErrorCode.UNEXPECTED_OPERATION);
    GridSqlTable tbl = gridTableForElement(target);
    GridH2Table gridTbl = tbl.dataTable();
    GridH2RowDescriptor desc = gridTbl.rowDescriptor();
    if (desc == null)
        throw new IgniteSQLException("Row descriptor undefined for table '" + gridTbl.getName() + "'", IgniteQueryErrorCode.NULL_TABLE_DESCRIPTOR);
    if (fastUpdate != null)
        return UpdatePlan.forFastUpdate(mode, gridTbl, fastUpdate);
    else {
        GridSqlSelect sel;
        if (stmt instanceof GridSqlUpdate) {
            List<GridSqlColumn> updatedCols = ((GridSqlUpdate) stmt).cols();
            int valColIdx = -1;
            String[] colNames = new String[updatedCols.size()];
            int[] colTypes = new int[updatedCols.size()];
            for (int i = 0; i < updatedCols.size(); i++) {
                colNames[i] = updatedCols.get(i).columnName();
                colTypes[i] = updatedCols.get(i).resultType().type();
                Column column = updatedCols.get(i).column();
                if (desc.isValueColumn(column.getColumnId()))
                    valColIdx = i;
            }
            boolean hasNewVal = (valColIdx != -1);
            // Statement updates distinct properties if it does not have _val in updated columns list
            // or if its list of updated columns includes only _val, i.e. is single element.
            boolean hasProps = !hasNewVal || updatedCols.size() > 1;
            // Index of new _val in results of SELECT
            if (hasNewVal)
                valColIdx += 2;
            int newValColIdx = (hasNewVal ? valColIdx : 1);
            KeyValueSupplier newValSupplier = createSupplier(desc.context(), desc.type(), newValColIdx, hasProps, false, true);
            sel = DmlAstUtils.selectForUpdate((GridSqlUpdate) stmt, errKeysPos);
            return UpdatePlan.forUpdate(gridTbl, colNames, colTypes, newValSupplier, valColIdx, sel.getSQL());
        } else {
            sel = DmlAstUtils.selectForDelete((GridSqlDelete) stmt, errKeysPos);
            return UpdatePlan.forDelete(gridTbl, sel.getSQL());
        }
    }
}
Also used : GridSqlDelete(org.apache.ignite.internal.processors.query.h2.sql.GridSqlDelete) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) 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) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlUpdate(org.apache.ignite.internal.processors.query.h2.sql.GridSqlUpdate)

Example 55 with User

use of org.h2.engine.User 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)

Aggregations

Connection (java.sql.Connection)36 SQLException (java.sql.SQLException)21 PreparedStatement (java.sql.PreparedStatement)17 Statement (java.sql.Statement)17 ResultSet (java.sql.ResultSet)16 Server (org.h2.tools.Server)15 DbException (org.h2.message.DbException)14 Column (org.h2.table.Column)12 ValueString (org.h2.value.ValueString)12 Properties (java.util.Properties)10 Database (org.h2.engine.Database)10 Schema (org.h2.schema.Schema)8 IOException (java.io.IOException)7 User (org.h2.engine.User)7 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)7 SimpleResultSet (org.h2.tools.SimpleResultSet)7 Value (org.h2.value.Value)7 PrintStream (java.io.PrintStream)6 Timestamp (java.sql.Timestamp)6 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)6