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());
}
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();
}
}
}
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());
}
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;
}
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;
}
Aggregations