Search in sources :

Example 1 with DatatypeCoder

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

the class FieldDescriptorTest method shouldUseEncodingSpecificDatatypeCoder_blobTextType_notDefaultCharset.

@Test
public void shouldUseEncodingSpecificDatatypeCoder_blobTextType_notDefaultCharset() {
    FieldDescriptor descriptor = createFieldDescriptor(ISCConstants.SQL_BLOB, 1, CHARSET_ID_WIN1252);
    EncodingDefinition win1252EncodingDefinition = encodingFactory.getEncodingDefinitionByCharacterSetId(CHARSET_ID_WIN1252);
    DatatypeCoder datatypeCoder = descriptor.getDatatypeCoder();
    assertThat(datatypeCoder, instanceOf(EncodingSpecificDatatypeCoder.class));
    assertEquals(win1252EncodingDefinition, datatypeCoder.getEncodingDefinition());
    assertEquals(-1, descriptor.getCharacterLength());
}
Also used : EncodingSpecificDatatypeCoder(org.firebirdsql.gds.ng.EncodingSpecificDatatypeCoder) EncodingDefinition(org.firebirdsql.encodings.EncodingDefinition) DefaultDatatypeCoder(org.firebirdsql.gds.ng.DefaultDatatypeCoder) EncodingSpecificDatatypeCoder(org.firebirdsql.gds.ng.EncodingSpecificDatatypeCoder) DatatypeCoder(org.firebirdsql.gds.ng.DatatypeCoder) Test(org.junit.Test)

Example 2 with DatatypeCoder

use of org.firebirdsql.gds.ng.DatatypeCoder 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 3 with DatatypeCoder

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

the class TestJnaStatement method testSelect_WithParameters_Execute_and_Fetch.

@Test
public void testSelect_WithParameters_Execute_and_Fetch() throws Exception {
    allocateStatement();
    statement.addStatementListener(listener);
    statement.prepare("SELECT a.RDB$CHARACTER_SET_NAME " + "FROM RDB$CHARACTER_SETS a " + "WHERE a.RDB$CHARACTER_SET_ID = ? OR a.RDB$BYTES_PER_CHARACTER = ?");
    final DatatypeCoder coder = db.getDatatypeCoder();
    // smallint = 3 (id of UNICODE_FSS)
    FieldValue param1 = new FieldValue(coder.encodeShort(3));
    // smallint = 1 (single byte character sets)
    FieldValue param2 = new FieldValue(coder.encodeShort(1));
    statement.execute(RowValue.of(param1, param2));
    assertEquals("Expected hasResultSet to be set to true", Boolean.TRUE, listener.hasResultSet());
    assertEquals("Expected hasSingletonResult to be set to false", Boolean.FALSE, listener.hasSingletonResult());
    assertNull("Expected allRowsFetched not set yet", listener.isAllRowsFetched());
    assertEquals("Expected no rows to be fetched yet", 0, listener.getRows().size());
    assertNull("Expected no SQL counts yet", listener.getSqlCounts());
    // JNAStatement only executes a single fetch to prevent problems with positioned updates,
    // so this doesn't get all rows fetched immediately
    statement.fetchRows(100);
    assertEquals("Expected allRowsFetched to haven't been called yet", null, listener.isAllRowsFetched());
    assertEquals("Expected a single row to have been fetched", 1, listener.getRows().size());
    // 100 should be sufficient to fetch all character sets; limit to prevent infinite loop with bugs in fetchRows
    int count = 0;
    while (listener.isAllRowsFetched() != Boolean.TRUE && count < 100) {
        statement.fetchRows(1);
        count++;
    }
    assertEquals("Expected allRowsFetched to be set to true", Boolean.TRUE, listener.isAllRowsFetched());
    // Number is database dependent (unicode_fss + all single byte character sets)
    assertTrue("Expected more than two rows", listener.getRows().size() > 2);
    assertNull("expected no SQL counts immediately after retrieving all rows", listener.getSqlCounts());
    statement.getSqlCounts();
    assertNotNull("Expected SQL counts", listener.getSqlCounts());
    assertEquals("Unexpected select count", listener.getRows().size(), listener.getSqlCounts().getLongSelectCount());
}
Also used : DatatypeCoder(org.firebirdsql.gds.ng.DatatypeCoder) FieldValue(org.firebirdsql.gds.ng.fields.FieldValue) AbstractStatementTest(org.firebirdsql.gds.ng.AbstractStatementTest) Test(org.junit.Test)

Example 4 with DatatypeCoder

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

the class EncodingFactory method getOrCreateDatatypeCoder.

@SuppressWarnings("unchecked")
@Override
public <T extends DatatypeCoder> T getOrCreateDatatypeCoder(Class<T> datatypeCoderClass) {
    DatatypeCoder coder = datatypeCoderCache.get(datatypeCoderClass);
    if (coder == null) {
        T newCoder = createNewDatatypeCoder(datatypeCoderClass, this);
        coder = datatypeCoderCache.putIfAbsent(datatypeCoderClass, newCoder);
        if (coder == null) {
            return newCoder;
        }
    }
    return (T) coder;
}
Also used : BigEndianDatatypeCoder(org.firebirdsql.gds.ng.jna.BigEndianDatatypeCoder) DefaultDatatypeCoder(org.firebirdsql.gds.ng.DefaultDatatypeCoder) LittleEndianDatatypeCoder(org.firebirdsql.gds.ng.jna.LittleEndianDatatypeCoder) DatatypeCoder(org.firebirdsql.gds.ng.DatatypeCoder)

Example 5 with DatatypeCoder

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

the class ConnectionEncodingFactory method getOrCreateDatatypeCoder.

@SuppressWarnings("unchecked")
@Override
public <T extends DatatypeCoder> T getOrCreateDatatypeCoder(Class<T> datatypeCoderClass) {
    DatatypeCoder coder = datatypeCoderCache.get(datatypeCoderClass);
    if (coder == null) {
        T newCoder = EncodingFactory.createNewDatatypeCoder(datatypeCoderClass, this);
        coder = datatypeCoderCache.putIfAbsent(datatypeCoderClass, newCoder);
        if (coder == null) {
            return newCoder;
        }
    }
    return (T) coder;
}
Also used : DatatypeCoder(org.firebirdsql.gds.ng.DatatypeCoder)

Aggregations

DatatypeCoder (org.firebirdsql.gds.ng.DatatypeCoder)6 Test (org.junit.Test)4 DefaultDatatypeCoder (org.firebirdsql.gds.ng.DefaultDatatypeCoder)3 EncodingDefinition (org.firebirdsql.encodings.EncodingDefinition)2 EncodingSpecificDatatypeCoder (org.firebirdsql.gds.ng.EncodingSpecificDatatypeCoder)2 FieldValue (org.firebirdsql.gds.ng.fields.FieldValue)2 AbstractStatementTest (org.firebirdsql.gds.ng.AbstractStatementTest)1 FbBlob (org.firebirdsql.gds.ng.FbBlob)1 FbStatement (org.firebirdsql.gds.ng.FbStatement)1 FbTransaction (org.firebirdsql.gds.ng.FbTransaction)1 BigEndianDatatypeCoder (org.firebirdsql.gds.ng.jna.BigEndianDatatypeCoder)1 LittleEndianDatatypeCoder (org.firebirdsql.gds.ng.jna.LittleEndianDatatypeCoder)1 FbWireDatabase (org.firebirdsql.gds.ng.wire.FbWireDatabase)1 SimpleStatementListener (org.firebirdsql.gds.ng.wire.SimpleStatementListener)1