use of java.sql.Blob in project hibernate-orm by hibernate.
the class BlobDescriptorTest method testExternalization.
@Test
@Override
public void testExternalization() {
// blobs of the same internal value are not really comparable
String externalized = BlobTypeDescriptor.INSTANCE.toString(original);
Blob consumed = BlobTypeDescriptor.INSTANCE.fromString(externalized);
try {
PrimitiveByteArrayTypeDescriptor.INSTANCE.areEqual(DataHelper.extractBytes(original.getBinaryStream()), DataHelper.extractBytes(consumed.getBinaryStream()));
} catch (SQLException e) {
fail("SQLException accessing blob : " + e.getMessage());
}
}
use of java.sql.Blob in project jOOQ by jOOQ.
the class DefaultExecuteContext method clean.
/**
* Clean up blobs, clobs and the local configuration.
* <p>
* <h5>BLOBS and CLOBS</h5>
* <p>
* [#1326] This is necessary in those dialects that have long-lived
* temporary lob objects, which can cause memory leaks in certain contexts,
* where the lobs' underlying session / connection is long-lived as well.
* Specifically, Oracle and ojdbc have some trouble when streaming temporary
* lobs to UDTs:
* <ol>
* <li>The lob cannot have a call-scoped life time with UDTs</li>
* <li>Freeing the lob after binding will cause an ORA-22275</li>
* <li>Not freeing the lob after execution will cause an
* {@link OutOfMemoryError}</li>
* </ol>
* <p>
* <h5>Local configuration</h5>
* <p>
* [#1544] There exist some corner-cases regarding the {@link SQLOutput}
* API, used for UDT serialisation / deserialisation, which have no elegant
* solutions of obtaining a {@link Configuration} and thus a JDBC
* {@link Connection} object short of:
* <ul>
* <li>Making assumptions about the JDBC driver and using proprietary API,
* e.g. that of ojdbc</li>
* <li>Dealing with this problem globally by using such a local
* configuration</li>
* </ul>
*
* @see <a
* href="http://stackoverflow.com/q/11439543/521799">http://stackoverflow.com/q/11439543/521799</a>
*/
static final void clean() {
List<Blob> blobs = BLOBS.get();
List<Clob> clobs = CLOBS.get();
List<SQLXML> xmls = SQLXMLS.get();
List<Array> arrays = ARRAYS.get();
if (blobs != null) {
for (Blob blob : blobs) {
JDBCUtils.safeFree(blob);
}
BLOBS.remove();
}
if (clobs != null) {
for (Clob clob : clobs) {
JDBCUtils.safeFree(clob);
}
CLOBS.remove();
}
if (xmls != null) {
for (SQLXML xml : xmls) {
JDBCUtils.safeFree(xml);
}
SQLXMLS.remove();
}
if (arrays != null) {
for (Array array : arrays) {
JDBCUtils.safeFree(array);
}
SQLXMLS.remove();
}
LOCAL_CONFIGURATION.remove();
LOCAL_DATA.remove();
LOCAL_CONNECTION.remove();
}
use of java.sql.Blob in project jOOQ by jOOQ.
the class DefaultBinding method set.
@Override
public void set(BindingSetSQLOutputContext<U> ctx) throws SQLException {
Configuration configuration = ctx.configuration();
T value = converter.to(ctx.value());
if (value == null) {
ctx.output().writeObject(null);
} else if (type == Blob.class) {
ctx.output().writeBlob((Blob) value);
} else if (type == Boolean.class) {
ctx.output().writeBoolean((Boolean) value);
} else if (type == BigInteger.class) {
ctx.output().writeBigDecimal(new BigDecimal((BigInteger) value));
} else if (type == BigDecimal.class) {
ctx.output().writeBigDecimal((BigDecimal) value);
} else if (type == Byte.class) {
ctx.output().writeByte((Byte) value);
} else if (type == byte[].class) {
// Use reflection to avoid dependency on OJDBC
if (isLob) {
Blob blob = null;
try {
blob = on("oracle.sql.BLOB").call("createTemporary", on(ctx.output()).call("getSTRUCT").call("getJavaSqlConnection").get(), false, on("oracle.sql.BLOB").get("DURATION_SESSION")).get();
blob.setBytes(1, (byte[]) value);
ctx.output().writeBlob(blob);
} finally {
DefaultExecuteContext.register(blob);
}
} else {
ctx.output().writeBytes((byte[]) value);
}
} else if (type == Clob.class) {
ctx.output().writeClob((Clob) value);
} else if (type == Date.class) {
Date date = (Date) value;
ctx.output().writeDate(date);
} else if (type == Double.class) {
ctx.output().writeDouble((Double) value);
} else if (type == Float.class) {
ctx.output().writeFloat((Float) value);
} else if (type == Integer.class) {
ctx.output().writeInt((Integer) value);
} else if (type == Long.class) {
ctx.output().writeLong((Long) value);
} else if (type == Short.class) {
ctx.output().writeShort((Short) value);
} else if (type == String.class) {
// Use reflection to avoid dependency on OJDBC
if (isLob) {
Clob clob = null;
try {
clob = on("oracle.sql.CLOB").call("createTemporary", on(ctx.output()).call("getSTRUCT").call("getJavaSqlConnection").get(), false, on("oracle.sql.CLOB").get("DURATION_SESSION")).get();
clob.setString(1, (String) value);
ctx.output().writeClob(clob);
} finally {
DefaultExecuteContext.register(clob);
}
} else {
ctx.output().writeString((String) value);
}
} else if (type == Time.class) {
ctx.output().writeTime((Time) value);
} else if (type == Timestamp.class) {
ctx.output().writeTimestamp((Timestamp) value);
} else if (type == YearToMonth.class) {
ctx.output().writeString(value.toString());
} else if (type == DayToSecond.class) {
ctx.output().writeString(value.toString());
} else // }
if (UNumber.class.isAssignableFrom(type)) {
ctx.output().writeString(value.toString());
} else if (type == UUID.class) {
ctx.output().writeString(value.toString());
} else if (EnumType.class.isAssignableFrom(type)) {
ctx.output().writeString(((EnumType) value).getLiteral());
} else if (UDTRecord.class.isAssignableFrom(type)) {
ctx.output().writeObject((UDTRecord<?>) value);
} else {
throw new UnsupportedOperationException("Type " + type + " is not supported");
}
}
use of java.sql.Blob in project voltdb by VoltDB.
the class JDBCResultSet method getSQLXML.
//#endif JAVA6
/**
* Retrieves the value of the designated column in the current row of
* this <code>ResultSet</code> as a
* <code>java.sql.SQLXML</code> object in the Java programming language.
*
* @param columnIndex the first column is 1, the second is 2, ...
* @return a <code>SQLXML</code> object that maps an <code>SQL XML</code> value
* @throws SQLException if a database access error occurs
* or this method is called on a closed result set
* @exception SQLFeatureNotSupportedException if the JDBC driver does not support
* this method
* @since JDK 1.6, HSQLDB 1.9.0
*/
//#ifdef JAVA6
public SQLXML getSQLXML(int columnIndex) throws SQLException {
checkColumn(columnIndex);
SQLXML sqlxml;
int type = resultMetaData.columnTypes[columnIndex - 1].typeCode;
switch(type) {
case Types.SQL_XML:
{
Object object = getObject(columnIndex);
if (object == null) {
sqlxml = null;
} else if (object instanceof SQLXML) {
sqlxml = (SQLXML) object;
} else {
throw Util.notSupported();
}
break;
}
case Types.SQL_CLOB:
{
Clob clob = getClob(columnIndex);
if (clob == null) {
sqlxml = null;
} else {
sqlxml = new JDBCSQLXML(clob.getCharacterStream());
}
break;
}
case Types.SQL_CHAR:
case Types.SQL_VARCHAR:
case Types.VARCHAR_IGNORECASE:
{
java.io.Reader reader = getCharacterStream(columnIndex);
if (reader == null) {
sqlxml = null;
} else {
sqlxml = new JDBCSQLXML(reader);
}
break;
}
case Types.SQL_NCHAR:
case Types.SQL_NVARCHAR:
{
java.io.Reader nreader = getNCharacterStream(columnIndex);
if (nreader == null) {
sqlxml = null;
} else {
sqlxml = new JDBCSQLXML(nreader);
}
break;
}
case Types.SQL_BLOB:
{
Blob blob = getBlob(columnIndex);
if (blob == null) {
sqlxml = null;
} else {
sqlxml = new JDBCSQLXML(blob.getBinaryStream());
}
break;
}
case Types.SQL_BINARY:
case Types.SQL_VARBINARY:
{
java.io.InputStream inputStream = getBinaryStream(columnIndex);
if (inputStream == null) {
sqlxml = null;
} else {
sqlxml = new JDBCSQLXML(inputStream);
}
break;
}
case Types.OTHER:
case Types.JAVA_OBJECT:
{
Object data = getObject(columnIndex);
if (data == null) {
sqlxml = null;
} else if (data instanceof SQLXML) {
sqlxml = (SQLXML) data;
} else if (data instanceof String) {
sqlxml = new JDBCSQLXML((String) data);
} else if (data instanceof byte[]) {
sqlxml = new JDBCSQLXML((byte[]) data);
} else if (data instanceof Blob) {
Blob blob = (Blob) data;
sqlxml = new JDBCSQLXML(blob.getBinaryStream());
} else if (data instanceof Clob) {
Clob clob = (Clob) data;
sqlxml = new JDBCSQLXML(clob.getCharacterStream());
} else {
throw Util.notSupported();
}
break;
}
default:
{
throw Util.notSupported();
}
}
return sqlxml;
}
use of java.sql.Blob in project cubrid-manager by CUBRID.
the class TextRecordProcessor method getTextData.
/**
* Get text data
*
* @param dataIndex - column index
* @return
* @throws SQLException
*/
private String getTextData(CUBRIDResultSetProxy resultSet, int dataIndex, QueryRecord queryRecord) throws SQLException {
// FIXME move this logic to core module
ColumnInfo columnInfo = (ColumnInfo) queryRecord.getColumnInfoList().get(dataIndex - 1);
String columnType = columnInfo.getType();
Object rsObj = resultSet.getObject(dataIndex);
String dataToput = null;
if (rsObj != null) {
if (DataType.DATATYPE_SET.equals(columnType) || DataType.DATATYPE_MULTISET.equals(columnType) || DataType.DATATYPE_SEQUENCE.equals(columnType)) {
StringBuffer data = new StringBuffer();
Object[] set = (Object[]) resultSet.getCollection(dataIndex);
data.append("{");
for (int i = 0; i < set.length; i++) {
Object setI = set[i];
if (null == setI) {
data.append(DataType.VALUE_NULL);
} else if (setI.getClass() == CUBRIDOIDProxy.getCUBRIDOIDClass(resultSet.getJdbcVersion())) {
data.append((new CUBRIDOIDProxy(setI)).getOidString());
} else {
data.append(setI);
}
if (i < set.length - 1) {
data.append(",");
}
}
data.append("}");
dataToput = data.toString();
} else if (DataType.DATATYPE_DATETIME.equalsIgnoreCase(columnType)) {
dataToput = CommonUITool.formatDate(resultSet.getTimestamp(dataIndex), FieldHandlerUtils.FORMAT_DATETIME);
} else if (DataType.DATATYPE_BIT_VARYING.equalsIgnoreCase(columnType) || DataType.DATATYPE_BIT.equalsIgnoreCase(columnType)) {
byte[] dataTmp = resultSet.getBytes(dataIndex);
if (dataTmp.length > FieldHandlerUtils.BIT_TYPE_MUCH_VALUE_LENGTH) {
dataToput = DataType.BIT_EXPORT_FORMAT;
} else {
dataToput = "X'" + DBAttrTypeFormatter.getHexString(dataTmp) + "'";
}
} else if (DataType.DATATYPE_FLOAT.equalsIgnoreCase(columnType)) {
formater.applyPattern(FORMAT_FLOAT);
dataToput = formater.format(resultSet.getFloat(dataIndex));
} else if (DataType.DATATYPE_DOUBLE.equalsIgnoreCase(columnType)) {
formater.applyPattern(FORMAT_DOUBLE);
dataToput = formater.format(resultSet.getDouble(dataIndex));
} else if (DataType.DATATYPE_BLOB.equalsIgnoreCase(columnType) || rsObj instanceof Blob) {
columnInfo.setType(DataType.DATATYPE_BLOB);
dataToput = DataType.BLOB_EXPORT_FORMAT;
} else if (DataType.DATATYPE_CLOB.equalsIgnoreCase(columnType) || rsObj instanceof Clob) {
columnInfo.setType(DataType.DATATYPE_CLOB);
dataToput = DataType.CLOB_EXPORT_FORMAT;
} else if (DataType.DATATYPE_NCHAR.equalsIgnoreCase(columnType)) {
columnInfo.setType(DataType.DATATYPE_NCHAR);
dataToput = "N'" + resultSet.getString(dataIndex) + "'";
} else if (DataType.DATATYPE_NCHAR_VARYING.equalsIgnoreCase(columnType)) {
columnInfo.setType(DataType.DATATYPE_NCHAR_VARYING);
dataToput = "N'" + resultSet.getString(dataIndex) + "'";
} else {
dataToput = resultSet.getString(dataIndex);
}
}
return dataToput;
}
Aggregations