Search in sources :

Example 1 with SimpleStatementListener

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

the class AbstractStatementTest method testInsertSelectUTF8Value.

@Test
public void testInsertSelectUTF8Value() throws Exception {
    allocateStatement();
    // Insert UTF8 columns
    statement.prepare(INSERT_THEUTFVALUE);
    final RowDescriptor parametersInsert = statement.getParameterDescriptor();
    final RowValue parameterValuesInsert = parametersInsert.createDefaultFieldValues();
    parameterValuesInsert.getFieldValue(0).setFieldData(db.getDatatypeCoder().encodeInt(1));
    final Encoding utf8Encoding = db.getEncodingFactory().getEncodingForFirebirdName("UTF8");
    final String aEuro = "a\u20AC";
    final byte[] insertFieldData = utf8Encoding.encodeToCharset(aEuro);
    parameterValuesInsert.getFieldValue(1).setFieldData(insertFieldData);
    parameterValuesInsert.getFieldValue(2).setFieldData(insertFieldData);
    statement.execute(parameterValuesInsert);
    // Retrieve the just inserted UTF8 values from the database for comparison
    statement.prepare(SELECT_THEUTFVALUE);
    final RowDescriptor parametersSelect = statement.getParameterDescriptor();
    final RowValue parameterValuesSelect = parametersSelect.createDefaultFieldValues();
    parameterValuesSelect.getFieldValue(0).setFieldData(db.getDatatypeCoder().encodeInt(1));
    final SimpleStatementListener statementListener = new SimpleStatementListener();
    statement.addStatementListener(statementListener);
    statement.execute(parameterValuesSelect);
    statement.fetchRows(1);
    final List<RowValue> rows = statementListener.getRows();
    assertEquals("Expected a row", 1, rows.size());
    final RowValue selectResult = rows.get(0);
    final byte[] selectVarcharFieldData = selectResult.getFieldValue(0).getFieldData();
    final byte[] selectCharFieldData = selectResult.getFieldValue(1).getFieldData();
    assertEquals("Length of selected varchar field data", 4, selectVarcharFieldData.length);
    assertEquals("Length of selected char field data", 20, selectCharFieldData.length);
    String decodedVarchar = utf8Encoding.decodeFromCharset(selectVarcharFieldData);
    String decodedChar = utf8Encoding.decodeFromCharset(selectCharFieldData);
    assertEquals("Unexpected value for varchar", aEuro, decodedVarchar);
    assertEquals("Unexpected value for trimmed char", aEuro, decodedChar.trim());
    // Note artificial result from the way UTF8 is handled
    assertEquals("Unexpected length for char", 18, decodedChar.length());
    char[] spaceChars16 = new char[16];
    Arrays.fill(spaceChars16, ' ');
    assertEquals("Unexpected trailing characters for char", new String(spaceChars16), decodedChar.substring(2));
}
Also used : SimpleStatementListener(org.firebirdsql.gds.ng.wire.SimpleStatementListener) RowValue(org.firebirdsql.gds.ng.fields.RowValue) Encoding(org.firebirdsql.encodings.Encoding) RowDescriptor(org.firebirdsql.gds.ng.fields.RowDescriptor) Test(org.junit.Test)

Example 2 with SimpleStatementListener

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

the class AbstractStatementTest method testMultipleExecute.

@Test
public void testMultipleExecute() 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());
    statement.fetchRows(10);
    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();
    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());
    statement.fetchRows(10);
    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) Test(org.junit.Test)

Example 3 with SimpleStatementListener

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

the class AbstractStatementTest 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());
    statement.fetchRows(10);
    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) Test(org.junit.Test)

Example 4 with SimpleStatementListener

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

the class AbstractStatementTest 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());
    statement.fetchRows(10);
    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());
    statement.fetchRows(10);
    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) Test(org.junit.Test)

Example 5 with SimpleStatementListener

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

the class BaseTestBlob method populateStreamBlob.

/**
 * Populates a stream blob for testing.
 *
 * @param testId Id of the record to be inserted.
 * @throws SQLException
 */
protected void populateStreamBlob(int testId, byte[] baseContent, int requiredSize) throws SQLException {
    final byte[] testBytes = generateBlobContent(baseContent, requiredSize);
    try (FbDatabase db = createDatabaseConnection()) {
        listener = new SimpleStatementListener();
        transaction = getTransaction(db);
        try {
            statement = db.createStatement(transaction);
            statement.addStatementListener(listener);
            final BlobParameterBuffer blobParameterBuffer = db.createBlobParameterBuffer();
            blobParameterBuffer.addArgument(BlobParameterBuffer.TYPE, BlobParameterBuffer.TYPE_STREAM);
            final FbBlob blob = db.createBlobForOutput(transaction, blobParameterBuffer);
            blob.open();
            int bytesWritten = 0;
            while (bytesWritten < testBytes.length) {
                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) BlobParameterBuffer(org.firebirdsql.gds.BlobParameterBuffer)

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