use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.
the class JDBCReferenceValueHandler method getValueFromObject.
@Override
public JDBCReference getValueFromObject(@NotNull DBCSession session, @NotNull DBSTypedObject type, Object object, boolean copy) throws DBCException {
String typeName;
try {
if (object instanceof Ref) {
typeName = ((Ref) object).getBaseTypeName();
} else {
typeName = type.getTypeName();
}
} catch (SQLException e) {
throw new DBCException(e, session.getDataSource());
}
DBSDataType dataType = null;
try {
dataType = DBUtils.resolveDataType(session.getProgressMonitor(), session.getDataSource(), typeName);
} catch (DBException e) {
log.error("Error resolving data type '" + typeName + "'", e);
}
if (dataType == null) {
dataType = new JDBCDataType(session.getDataSource().getContainer(), Types.REF, typeName, "Synthetic struct type for reference '" + typeName + "'", false, false, 0, 0, 0);
}
if (object == null) {
return new JDBCReference(dataType, null);
} else if (object instanceof JDBCReference) {
return (JDBCReference) object;
} else if (object instanceof Ref) {
return new JDBCReference(dataType, (Ref) object);
} else {
throw new DBCException("Unsupported struct type: " + object.getClass().getName());
}
}
use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.
the class JDBCStructValueHandler method getValueFromObject.
@Override
public Object getValueFromObject(@NotNull DBCSession session, @NotNull DBSTypedObject type, Object object, boolean copy) throws DBCException {
String typeName;
try {
if (object instanceof Struct) {
typeName = ((Struct) object).getSQLTypeName();
} else {
typeName = type.getTypeName();
}
} catch (SQLException e) {
throw new DBCException(e, session.getDataSource());
}
DBSDataType dataType = null;
try {
dataType = DBUtils.resolveDataType(session.getProgressMonitor(), session.getDataSource(), typeName);
} catch (DBException e) {
log.debug("Error resolving data type '" + typeName + "'", e);
}
if (dataType == null) {
if (object instanceof Struct) {
return new JDBCCompositeDynamic(session, (Struct) object, null);
} else {
return new JDBCCompositeUnknown(session, object);
}
}
if (object == null) {
return new JDBCCompositeStatic(session, dataType, new JDBCStructImpl(dataType.getTypeName(), null));
} else if (object instanceof JDBCCompositeStatic) {
return copy ? ((JDBCCompositeStatic) object).cloneValue(session.getProgressMonitor()) : object;
} else if (object instanceof Struct) {
return new JDBCCompositeStatic(session, dataType, (Struct) object);
} else {
return new JDBCCompositeUnknown(session, object);
}
}
use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.
the class JDBCComposite method getStructValue.
public Struct getStructValue() throws DBCException {
Object[] attrs = new Object[values.length];
for (int i = 0; i < values.length; i++) {
Object attr = values[i];
if (attr instanceof DBDValue) {
attr = ((DBDValue) attr).getRawValue();
}
attrs[i] = attr;
}
final DBSDataType dataType = getDataType();
try (DBCSession session = DBUtils.openUtilSession(VoidProgressMonitor.INSTANCE, dataType.getDataSource(), "Create JDBC struct")) {
if (session instanceof Connection) {
return ((Connection) session).createStruct(dataType.getTypeName(), attrs);
} else {
return new JDBCStructImpl(dataType.getTypeName(), attrs);
}
} catch (Throwable e) {
throw new DBCException("Error creating struct", e);
}
}
use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.
the class JDBCContentBLOB method getContents.
@Override
public DBDContentStorage getContents(DBRProgressMonitor monitor) throws DBCException {
if (storage == null && blob != null) {
long contentLength = getContentLength();
DBPPlatform platform = dataSource.getContainer().getPlatform();
if (contentLength < platform.getPreferenceStore().getInt(ModelPreferences.MEMORY_CONTENT_MAX_SIZE)) {
try {
try (InputStream bs = blob.getBinaryStream()) {
storage = BytesContentStorage.createFromStream(bs, contentLength, getDefaultEncoding());
}
} catch (IOException e) {
throw new DBCException("IO error while reading content", e);
} catch (Throwable e) {
throw new DBCException(e, dataSource);
}
} else {
// Create new local storage
File tempFile;
try {
tempFile = ContentUtils.createTempContentFile(monitor, platform, "blob" + blob.hashCode());
} catch (IOException e) {
throw new DBCException("Can't create temporary file", e);
}
try (OutputStream os = new FileOutputStream(tempFile)) {
try (InputStream bs = blob.getBinaryStream()) {
ContentUtils.copyStreams(bs, contentLength, os, monitor);
}
} catch (IOException e) {
ContentUtils.deleteTempFile(tempFile);
throw new DBCException("IO error while copying stream", e);
} catch (Throwable e) {
ContentUtils.deleteTempFile(tempFile);
throw new DBCException(e, dataSource);
}
this.storage = new TemporaryContentStorage(platform, tempFile, getDefaultEncoding());
}
// Free blob - we don't need it anymore
releaseBlob();
}
return storage;
}
use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.
the class JDBCContentBLOB method bindParameter.
@Override
public void bindParameter(JDBCSession session, JDBCPreparedStatement preparedStatement, DBSTypedObject columnType, int paramIndex) throws DBCException {
try {
if (storage != null) {
// Write new blob value
releaseTempStream();
tmpStream = storage.getContentStream();
try {
preparedStatement.setBinaryStream(paramIndex, tmpStream);
} catch (Throwable e) {
try {
if (e instanceof SQLException) {
throw (SQLException) e;
} else {
try {
preparedStatement.setBinaryStream(paramIndex, tmpStream, storage.getContentLength());
} catch (Throwable e1) {
if (e1 instanceof SQLException) {
throw (SQLException) e1;
} else {
preparedStatement.setBinaryStream(paramIndex, tmpStream, (int) storage.getContentLength());
}
}
}
} catch (SQLFeatureNotSupportedException e1) {
// Stream values seems to be unsupported
// Let's try bytes
int contentLength = (int) storage.getContentLength();
ByteArrayOutputStream buffer = new ByteArrayOutputStream(contentLength);
ContentUtils.copyStreams(tmpStream, contentLength, buffer, session.getProgressMonitor());
preparedStatement.setBytes(paramIndex, buffer.toByteArray());
}
}
} else if (blob != null) {
try {
preparedStatement.setBlob(paramIndex, blob);
} catch (Throwable e0) {
// Write new blob value
releaseTempStream();
tmpStream = blob.getBinaryStream();
try {
preparedStatement.setBinaryStream(paramIndex, tmpStream);
} catch (Throwable e) {
if (e instanceof SQLException) {
throw (SQLException) e;
} else {
try {
preparedStatement.setBinaryStream(paramIndex, tmpStream, blob.length());
} catch (Throwable e1) {
if (e1 instanceof SQLException) {
throw (SQLException) e1;
} else {
preparedStatement.setBinaryStream(paramIndex, tmpStream, (int) blob.length());
}
}
}
}
}
} else {
preparedStatement.setNull(paramIndex, java.sql.Types.BLOB);
}
} catch (SQLException e) {
throw new DBCException(e, session.getDataSource());
} catch (Throwable e) {
throw new DBCException("Error while reading content", e);
}
}
Aggregations