Search in sources :

Example 51 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class JdbcBlob method setBinaryStream.

/**
 * Get a writer to update the Blob. This is only supported for new, empty
 * Blob objects that were created with Connection.createBlob(). The Blob is
 * created in a separate thread, and the object is only updated when
 * OutputStream.close() is called. The position must be 1, meaning the whole
 * Blob data is set.
 *
 * @param pos where to start writing (the first byte is at position 1)
 * @return an output stream
 */
@Override
public OutputStream setBinaryStream(long pos) throws SQLException {
    try {
        if (isDebugEnabled()) {
            debugCode("setBinaryStream(" + pos + ");");
        }
        checkClosed();
        if (pos != 1) {
            throw DbException.getInvalidValueException("pos", pos);
        }
        if (value.getPrecision() != 0) {
            throw DbException.getInvalidValueException("length", value.getPrecision());
        }
        // local variable avoids generating synthetic accessor method
        final JdbcConnection c = conn;
        final PipedInputStream in = new PipedInputStream();
        final Task task = new Task() {

            @Override
            public void call() {
                value = c.createBlob(in, -1);
            }
        };
        PipedOutputStream out = new PipedOutputStream(in) {

            @Override
            public void close() throws IOException {
                super.close();
                try {
                    task.get();
                } catch (Exception e) {
                    throw DbException.convertToIOException(e);
                }
            }
        };
        task.execute();
        return new BufferedOutputStream(out);
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : Task(org.h2.util.Task) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 52 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class JdbcClob method setCharacterStream.

/**
 * Get a writer to update the Clob. This is only supported for new, empty
 * Clob objects that were created with Connection.createClob() or
 * createNClob(). The Clob is created in a separate thread, and the object
 * is only updated when Writer.close() is called. The position must be 1,
 * meaning the whole Clob data is set.
 *
 * @param pos where to start writing (the first character is at position 1)
 * @return a writer
 */
@Override
public Writer setCharacterStream(long pos) throws SQLException {
    try {
        if (isDebugEnabled()) {
            debugCodeCall("setCharacterStream(" + pos + ");");
        }
        checkClosed();
        if (pos != 1) {
            throw DbException.getInvalidValueException("pos", pos);
        }
        if (value.getPrecision() != 0) {
            throw DbException.getInvalidValueException("length", value.getPrecision());
        }
        // required to avoid synthetic method creation
        final JdbcConnection c = conn;
        // PipedReader / PipedWriter are a lot slower
        // than PipedInputStream / PipedOutputStream
        // (Sun/Oracle Java 1.6.0_20)
        final PipedInputStream in = new PipedInputStream();
        final Task task = new Task() {

            @Override
            public void call() {
                value = c.createClob(IOUtils.getReader(in), -1);
            }
        };
        PipedOutputStream out = new PipedOutputStream(in) {

            @Override
            public void close() throws IOException {
                super.close();
                try {
                    task.get();
                } catch (Exception e) {
                    throw DbException.convertToIOException(e);
                }
            }
        };
        task.execute();
        return IOUtils.getBufferedWriter(out);
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : Task(org.h2.util.Task) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 53 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class JdbcConnection method getCatalog.

/**
 * Gets the current catalog name.
 *
 * @return the catalog name
 * @throws SQLException if the connection is closed
 */
@Override
public String getCatalog() throws SQLException {
    try {
        debugCodeCall("getCatalog");
        checkClosed();
        if (catalog == null) {
            CommandInterface cat = prepareCommand("CALL DATABASE()", Integer.MAX_VALUE);
            ResultInterface result = cat.executeQuery(0, false);
            result.next();
            catalog = result.currentRow()[0].getString();
            cat.close();
        }
        return catalog;
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : ResultInterface(org.h2.result.ResultInterface) CommandInterface(org.h2.command.CommandInterface) DbException(org.h2.message.DbException) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException)

Example 54 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class JdbcConnection method isReadOnly.

/**
 * Returns true if the database is read-only.
 *
 * @return if the database is read-only
 * @throws SQLException if the connection is closed
 */
@Override
public boolean isReadOnly() throws SQLException {
    try {
        debugCodeCall("isReadOnly");
        checkClosed();
        getReadOnly = prepareCommand("CALL READONLY()", getReadOnly);
        ResultInterface result = getReadOnly.executeQuery(0, false);
        result.next();
        return result.currentRow()[0].getBoolean();
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : ResultInterface(org.h2.result.ResultInterface) DbException(org.h2.message.DbException) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException)

Example 55 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class TestPreparedStatement method testCancelReuse.

private void testCancelReuse(Connection conn) throws Exception {
    conn.createStatement().execute("CREATE ALIAS SLEEP FOR \"java.lang.Thread.sleep\"");
    // sleep for 10 seconds
    final PreparedStatement prep = conn.prepareStatement("SELECT SLEEP(?) FROM SYSTEM_RANGE(1, 10000) LIMIT ?");
    prep.setInt(1, 1);
    prep.setInt(2, 10000);
    Task t = new Task() {

        @Override
        public void call() throws SQLException {
            TestPreparedStatement.this.execute(prep);
        }
    };
    t.execute();
    Thread.sleep(100);
    prep.cancel();
    SQLException e = (SQLException) t.getException();
    assertNotNull(e);
    assertEquals(ErrorCode.STATEMENT_WAS_CANCELED, e.getErrorCode());
    prep.setInt(1, 1);
    prep.setInt(2, 1);
    ResultSet rs = prep.executeQuery();
    assertTrue(rs.next());
    assertEquals(0, rs.getInt(1));
    assertFalse(rs.next());
}
Also used : Task(org.h2.util.Task) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

Task (org.h2.util.Task)69 Connection (java.sql.Connection)68 Statement (java.sql.Statement)64 PreparedStatement (java.sql.PreparedStatement)60 ResultSet (java.sql.ResultSet)48 SQLException (java.sql.SQLException)42 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 SimpleResultSet (org.h2.tools.SimpleResultSet)24 MVStore (org.h2.mvstore.MVStore)20 Random (java.util.Random)19 JdbcConnection (org.h2.jdbc.JdbcConnection)19 CallableStatement (java.sql.CallableStatement)14 DbException (org.h2.message.DbException)13 IOException (java.io.IOException)10 JdbcSQLException (org.h2.jdbc.JdbcSQLException)7 ArrayList (java.util.ArrayList)6 Expression (org.h2.expression.Expression)6 ValueString (org.h2.value.ValueString)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 OutputStream (java.io.OutputStream)4