This commit is contained in:
Clebert Suconic 2017-02-10 09:55:58 -05:00
commit 2cf594adf7
3 changed files with 16 additions and 18 deletions

View File

@ -26,16 +26,12 @@ public class DerbySQLProvider extends GenericSQLProvider {
private final String createFileTableSQL;
private final String appendToFileSQL;
private DerbySQLProvider(String tableName) {
super(tableName.toUpperCase());
createFileTableSQL = "CREATE TABLE " + tableName +
"(ID BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)," +
"FILENAME VARCHAR(255), EXTENSION VARCHAR(10), DATA BLOB, PRIMARY KEY(ID))";
appendToFileSQL = "UPDATE " + tableName + " SET DATA = DATA || ? WHERE ID=?";
}
@Override
@ -48,11 +44,6 @@ public class DerbySQLProvider extends GenericSQLProvider {
return createFileTableSQL;
}
@Override
public String getAppendToLargeObjectSQL() {
return appendToFileSQL;
}
@Override
public boolean closeConnectionOnShutdown() {
return false;

View File

@ -74,7 +74,7 @@ public class JDBCSequentialFileFactoryDriver extends AbstractJDBCDriver {
this.copyFileRecord = connection.prepareStatement(sqlProvider.getCopyFileRecordByIdSQL());
this.renameFile = connection.prepareStatement(sqlProvider.getUpdateFileNameByIdSQL());
this.readLargeObject = connection.prepareStatement(sqlProvider.getReadLargeObjectSQL());
this.appendToLargeObject = connection.prepareStatement(sqlProvider.getAppendToLargeObjectSQL());
this.appendToLargeObject = connection.prepareStatement(sqlProvider.getAppendToLargeObjectSQL(), ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
this.selectFileNamesByExtension = connection.prepareStatement(sqlProvider.getSelectFileNamesByExtensionSQL());
}
@ -154,7 +154,8 @@ public class JDBCSequentialFileFactoryDriver extends AbstractJDBCDriver {
try (ResultSet rs = readLargeObject.executeQuery()) {
if (rs.next()) {
file.setWritePosition((int) rs.getBlob(1).length());
Blob blob = rs.getBlob(1);
file.setWritePosition((int) blob.length());
}
connection.commit();
} catch (SQLException e) {
@ -242,13 +243,19 @@ public class JDBCSequentialFileFactoryDriver extends AbstractJDBCDriver {
*/
public int writeToFile(JDBCSequentialFile file, byte[] data) throws SQLException {
synchronized (connection) {
try {
connection.setAutoCommit(false);
appendToLargeObject.setBytes(1, data);
appendToLargeObject.setLong(2, file.getId());
appendToLargeObject.executeUpdate();
connection.setAutoCommit(false);
appendToLargeObject.setLong(1, file.getId());
int bytesWritten = 0;
try (ResultSet rs = appendToLargeObject.executeQuery()) {
if (rs.next()) {
Blob blob = rs.getBlob(1);
bytesWritten = blob.setBytes(blob.length() + 1, data);
rs.updateBlob(1, blob);
rs.updateRow();
}
connection.commit();
return data.length;
return bytesWritten;
} catch (SQLException e) {
connection.rollback();
throw e;

View File

@ -69,7 +69,7 @@ public class GenericSQLProvider implements SQLProvider {
selectIdByFileNameSQL = "SELECT ID, FILENAME, EXTENSION, DATA FROM " + tableName + " WHERE fileName=?";
appendToFileSQL = "UPDATE " + tableName + " SET DATA = CONCAT(DATA, ?) WHERE ID=?";
appendToFileSQL = "SELECT DATA FROM " + tableName + " WHERE ID=? FOR UPDATE";
readLargeObjectSQL = "SELECT DATA FROM " + tableName + " WHERE ID=?";