Search in sources :

Example 6 with SimpleStatementListener

use of org.firebirdsql.gds.ng.wire.SimpleStatementListener in project jaybird by FirebirdSQL.

the class BaseTestBlob method writeBlob.

/**
 * Writes a blob using the gds.ng API.
 *
 * @param testId Id of the record to insert
 * @param testBytes Bytes to write
 * @param db Database to use
 * @param blobParameterBuffer Blob parameter buffer (or null)
 * @throws SQLException
 */
protected void writeBlob(int testId, byte[] testBytes, FbDatabase db, BlobParameterBuffer blobParameterBuffer) throws SQLException {
    final SimpleStatementListener listener = new SimpleStatementListener();
    final FbTransaction transaction = getTransaction(db);
    try {
        final FbStatement statement = db.createStatement(transaction);
        statement.addStatementListener(listener);
        final FbBlob blob = db.createBlobForOutput(transaction, blobParameterBuffer);
        blob.open();
        int bytesWritten = 0;
        while (bytesWritten < testBytes.length) {
            // TODO the interface for writing blobs should be simpler
            byte[] buffer = new byte[Math.min(blob.getMaximumSegmentSize(), testBytes.length - bytesWritten)];
            System.arraycopy(testBytes, bytesWritten, buffer, 0, buffer.length);
            blob.putSegment(buffer);
            bytesWritten += buffer.length;
        }
        blob.close();
        statement.prepare(INSERT_BLOB_TABLE);
        final DatatypeCoder datatypeCoder = db.getDatatypeCoder();
        FieldValue param1 = new FieldValue(datatypeCoder.encodeInt(testId));
        FieldValue param2 = new FieldValue(datatypeCoder.encodeLong(blob.getBlobId()));
        statement.execute(RowValue.of(param1, param2));
        statement.close();
    } finally {
        transaction.commit();
    }
}
Also used : SimpleStatementListener(org.firebirdsql.gds.ng.wire.SimpleStatementListener) FieldValue(org.firebirdsql.gds.ng.fields.FieldValue)

Example 7 with SimpleStatementListener

use of org.firebirdsql.gds.ng.wire.SimpleStatementListener in project jaybird by FirebirdSQL.

the class TestV10OutputBlob method testUsingCancelledBlob.

/**
 * Test whether a cancelled blob cannot be used (indicating it was indeed cancelled).
 */
@Test
public void testUsingCancelledBlob() throws Exception {
    expectedException.expect(SQLException.class);
    expectedException.expect(allOf(errorCodeEquals(ISCConstants.isc_bad_segstr_id), message(startsWith(getFbMessage(ISCConstants.isc_bad_segstr_id)))));
    final int testId = 1;
    final byte[] baseContent = generateBaseContent();
    final int requiredSize = 256;
    final byte[] testBytes = generateBlobContent(baseContent, requiredSize);
    try (FbWireDatabase db = createDatabaseConnection()) {
        final SimpleStatementListener listener = new SimpleStatementListener();
        final FbTransaction transaction = getTransaction(db);
        try {
            final FbStatement statement = db.createStatement(transaction);
            statement.addStatementListener(listener);
            final FbBlob blob = db.createBlobForOutput(transaction, null);
            blob.open();
            int bytesWritten = 0;
            while (bytesWritten < testBytes.length) {
                // TODO the interface for writing blobs should be simpler
                byte[] buffer = new byte[Math.min(blob.getMaximumSegmentSize(), testBytes.length - bytesWritten)];
                System.arraycopy(testBytes, bytesWritten, buffer, 0, buffer.length);
                blob.putSegment(buffer);
                bytesWritten += buffer.length;
            }
            blob.cancel();
            statement.prepare(INSERT_BLOB_TABLE);
            final DatatypeCoder datatypeCoder = db.getDatatypeCoder();
            FieldValue param1 = new FieldValue(datatypeCoder.encodeInt(testId));
            FieldValue param2 = new FieldValue(datatypeCoder.encodeLong(blob.getBlobId()));
            statement.execute(RowValue.of(param1, param2));
            statement.close();
        } finally {
            transaction.commit();
        }
    }
}
Also used : SimpleStatementListener(org.firebirdsql.gds.ng.wire.SimpleStatementListener) DatatypeCoder(org.firebirdsql.gds.ng.DatatypeCoder) FieldValue(org.firebirdsql.gds.ng.fields.FieldValue) FbStatement(org.firebirdsql.gds.ng.FbStatement) FbWireDatabase(org.firebirdsql.gds.ng.wire.FbWireDatabase) FbTransaction(org.firebirdsql.gds.ng.FbTransaction) FbBlob(org.firebirdsql.gds.ng.FbBlob) Test(org.junit.Test)

Example 8 with SimpleStatementListener

use of org.firebirdsql.gds.ng.wire.SimpleStatementListener in project jaybird by FirebirdSQL.

the class TestJnaStatement method testMultiplePrepare.

@Test
public void testMultiplePrepare() throws Exception {
    allocateStatement();
    statement.prepare("SELECT RDB$DESCRIPTION AS \"Description\", RDB$RELATION_ID, RDB$SECURITY_CLASS, RDB$CHARACTER_SET_NAME " + "FROM RDB$DATABASE");
    final SimpleStatementListener statementListener = new SimpleStatementListener();
    statement.addStatementListener(statementListener);
    statement.execute(RowValue.EMPTY_ROW_VALUE);
    assertEquals("Expected hasResultSet to be set to true", Boolean.TRUE, statementListener.hasResultSet());
    assertEquals("Expected hasSingletonResult to be set to false", Boolean.FALSE, statementListener.hasSingletonResult());
    assertNull("Expected allRowsFetched not set yet", statementListener.isAllRowsFetched());
    assertEquals("Expected no rows to be fetched yet", 0, statementListener.getRows().size());
    // JNAStatement only executes a single fetch to prevent problems with positioned updates,
    // so this doesn't get all rows fetched immediately
    statement.fetchRows(10);
    assertEquals("Expected allRowsFetched to haven't been called yet", null, statementListener.isAllRowsFetched());
    assertEquals("Expected a single row to have been fetched", 1, statementListener.getRows().size());
    statement.fetchRows(1);
    assertEquals("Expected allRowsFetched to be set to true", Boolean.TRUE, statementListener.isAllRowsFetched());
    assertEquals("Expected a single row to have been fetched", 1, statementListener.getRows().size());
    statement.closeCursor();
    statement.prepare("SELECT RDB$DESCRIPTION AS \"Description\", RDB$RELATION_ID, RDB$SECURITY_CLASS, RDB$CHARACTER_SET_NAME " + "FROM RDB$DATABASE");
    final SimpleStatementListener statementListener2 = new SimpleStatementListener();
    statement.addStatementListener(statementListener2);
    statement.execute(RowValue.EMPTY_ROW_VALUE);
    assertEquals("Expected hasResultSet to be set to true", Boolean.TRUE, statementListener2.hasResultSet());
    assertEquals("Expected hasSingletonResult to be set to false", Boolean.FALSE, statementListener2.hasSingletonResult());
    assertNull("Expected allRowsFetched not set yet", statementListener2.isAllRowsFetched());
    assertEquals("Expected no rows to be fetched yet", 0, statementListener2.getRows().size());
    // JNAStatement only executes a single fetch to prevent problems with positioned updates,
    // so this doesn't get all rows fetched immediately
    statement.fetchRows(10);
    assertEquals("Expected allRowsFetched to haven't been called yet", null, statementListener2.isAllRowsFetched());
    assertEquals("Expected a single row to have been fetched", 1, statementListener2.getRows().size());
    statement.fetchRows(1);
    assertEquals("Expected allRowsFetched to be set to true", Boolean.TRUE, statementListener2.isAllRowsFetched());
    assertEquals("Expected a single row to have been fetched", 1, statementListener2.getRows().size());
}
Also used : SimpleStatementListener(org.firebirdsql.gds.ng.wire.SimpleStatementListener) AbstractStatementTest(org.firebirdsql.gds.ng.AbstractStatementTest) Test(org.junit.Test)

Example 9 with SimpleStatementListener

use of org.firebirdsql.gds.ng.wire.SimpleStatementListener in project jaybird by FirebirdSQL.

the class TestJnaStatement method testSelect_NoParameters_Execute_and_Fetch.

@Test
public void testSelect_NoParameters_Execute_and_Fetch() throws Exception {
    allocateStatement();
    statement.prepare("SELECT RDB$DESCRIPTION AS \"Description\", RDB$RELATION_ID, RDB$SECURITY_CLASS, RDB$CHARACTER_SET_NAME " + "FROM RDB$DATABASE");
    final SimpleStatementListener statementListener = new SimpleStatementListener();
    statement.addStatementListener(statementListener);
    statement.execute(RowValue.EMPTY_ROW_VALUE);
    assertEquals("Expected hasResultSet to be set to true", Boolean.TRUE, statementListener.hasResultSet());
    assertEquals("Expected hasSingletonResult to be set to false", Boolean.FALSE, statementListener.hasSingletonResult());
    assertNull("Expected allRowsFetched not set yet", statementListener.isAllRowsFetched());
    assertEquals("Expected no rows to be fetched yet", 0, statementListener.getRows().size());
    // JNAStatement only executes a single fetch to prevent problems with positioned updates,
    // so this doesn't get all rows fetched immediately
    statement.fetchRows(10);
    assertEquals("Expected allRowsFetched to haven't been called yet", null, statementListener.isAllRowsFetched());
    assertEquals("Expected a single row to have been fetched", 1, statementListener.getRows().size());
    statement.fetchRows(1);
    assertEquals("Expected allRowsFetched to be set to true", Boolean.TRUE, statementListener.isAllRowsFetched());
    assertEquals("Expected a single row to have been fetched", 1, statementListener.getRows().size());
}
Also used : SimpleStatementListener(org.firebirdsql.gds.ng.wire.SimpleStatementListener) AbstractStatementTest(org.firebirdsql.gds.ng.AbstractStatementTest) Test(org.junit.Test)

Example 10 with SimpleStatementListener

use of org.firebirdsql.gds.ng.wire.SimpleStatementListener in project jaybird by FirebirdSQL.

the class TestJnaBlob method testOutputBlobUsingCancelledBlob.

/**
 * Test whether a cancelled blob cannot be used (indicating it was indeed cancelled).
 */
@Test
public void testOutputBlobUsingCancelledBlob() throws Exception {
    expectedException.expect(SQLException.class);
    expectedException.expect(allOf(errorCodeEquals(ISCConstants.isc_bad_segstr_id), message(startsWith(getFbMessage(ISCConstants.isc_bad_segstr_id)))));
    final int testId = 1;
    final byte[] baseContent = generateBaseContent();
    final int requiredSize = 256;
    final byte[] testBytes = generateBlobContent(baseContent, requiredSize);
    try (JnaDatabase db = createDatabaseConnection()) {
        final SimpleStatementListener listener = new SimpleStatementListener();
        final FbTransaction transaction = getTransaction(db);
        try {
            final FbStatement statement = db.createStatement(transaction);
            statement.addStatementListener(listener);
            final FbBlob blob = db.createBlobForOutput(transaction, null);
            blob.open();
            int bytesWritten = 0;
            while (bytesWritten < testBytes.length) {
                // TODO the interface for writing blobs should be simpler
                byte[] buffer = new byte[Math.min(blob.getMaximumSegmentSize(), testBytes.length - bytesWritten)];
                System.arraycopy(testBytes, bytesWritten, buffer, 0, buffer.length);
                blob.putSegment(buffer);
                bytesWritten += buffer.length;
            }
            blob.cancel();
            statement.prepare(INSERT_BLOB_TABLE);
            final DatatypeCoder datatypeCoder = db.getDatatypeCoder();
            FieldValue param1 = new FieldValue(datatypeCoder.encodeInt(testId));
            FieldValue param2 = new FieldValue(datatypeCoder.encodeLong(blob.getBlobId()));
            statement.execute(RowValue.of(param1, param2));
            statement.close();
        } finally {
            transaction.commit();
        }
    }
}
Also used : SimpleStatementListener(org.firebirdsql.gds.ng.wire.SimpleStatementListener) FieldValue(org.firebirdsql.gds.ng.fields.FieldValue) Test(org.junit.Test)

Aggregations

SimpleStatementListener (org.firebirdsql.gds.ng.wire.SimpleStatementListener)12 Test (org.junit.Test)9 FieldValue (org.firebirdsql.gds.ng.fields.FieldValue)5 AbstractStatementTest (org.firebirdsql.gds.ng.AbstractStatementTest)3 RowValue (org.firebirdsql.gds.ng.fields.RowValue)2 Encoding (org.firebirdsql.encodings.Encoding)1 BlobParameterBuffer (org.firebirdsql.gds.BlobParameterBuffer)1 DatatypeCoder (org.firebirdsql.gds.ng.DatatypeCoder)1 FbBlob (org.firebirdsql.gds.ng.FbBlob)1 FbStatement (org.firebirdsql.gds.ng.FbStatement)1 FbTransaction (org.firebirdsql.gds.ng.FbTransaction)1 RowDescriptor (org.firebirdsql.gds.ng.fields.RowDescriptor)1 FbWireDatabase (org.firebirdsql.gds.ng.wire.FbWireDatabase)1