public static RuntimeException launderThrowable(Throwable cause) { return launderThrowable("An error has occurred.", cause); }
private boolean tableExists(Handle h, String tableName) { try { String tableToCheck = tableName; boolean caseSensitive = this.databaseKind == DatabaseKind.PostgreSQL; if (!caseSensitive) { tableToCheck = tableName.toUpperCase(Locale.ROOT); } DatabaseMetaData metaData = h.getConnection().getMetaData(); try (ResultSet rs = metaData.getTables(null, null, tableToCheck, null)) { while (rs.next()) { String foundTable = rs.getString("TABLE_NAME"); if (tableToCheck.equalsIgnoreCase(foundTable)) { return true; } } } return false; } catch (SQLException ex) { throw ExtensionDataAccessException.launderThrowable("Cannot check if the table " + tableName + " already exists", ex); } }
private void doWriteDerby(Handle h, String path, InputStream file) { doDelete(h, path); try { Blob blob = h.getConnection().createBlob(); try (OutputStream out = blob.setBinaryStream(1)) { IOUtils.copy(file, out); } h.insert("INSERT INTO filestore(path, data) values (?,?)", path, blob); } catch (IOException | SQLException ex) { throw ExtensionDataAccessException.launderThrowable(ex); } }
throw ExtensionDataAccessException.launderThrowable(e);
/** * Postgres does not allow to read from the large object after the connection has been closed. */ private InputStream doReadPostgres(String path) { Handle h = dbi.open(); try { h.getConnection().setAutoCommit(false); List<Map<String, Object>> res = h.select("SELECT data FROM filestore WHERE path=?", path); Optional<Long> oid = res.stream() .map(row -> row.get("data")) .map(Long.class::cast) .findFirst(); if (oid.isPresent()) { LargeObjectManager lobj = getPostgresConnection(h.getConnection()).getLargeObjectAPI(); LargeObject obj = lobj.open(oid.get(), LargeObjectManager.READ); return new HandleCloserInputStream(h, obj.getInputStream()); } else { h.close(); return null; } } catch (SQLException e) { IOUtils.closeQuietly(h); throw ExtensionDataAccessException.launderThrowable(e); } }
private void doWritePostgres(Handle h, String path, InputStream file) { doDelete(h, path); try { LargeObjectManager lobj = getPostgresConnection(h.getConnection()).getLargeObjectAPI(); long oid = lobj.createLO(); LargeObject obj = lobj.open(oid, LargeObjectManager.WRITE); try (OutputStream lob = obj.getOutputStream()) { IOUtils.copy(file, lob); } h.insert("INSERT INTO filestore(path, data) values (?,?)", path, oid); } catch (IOException | SQLException ex) { throw ExtensionDataAccessException.launderThrowable(ex); } }