-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor mysql, postgres and sqlserver blob resource implementations …
…to use the respective streaming APIs
- Loading branch information
Paul Warren
committed
Oct 30, 2018
1 parent
3bb874b
commit 9a9e6b2
Showing
15 changed files
with
308 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 0 additions & 34 deletions
34
.../src/main/java/internal/org/springframework/content/jpa/io/GenericBlobResourceLoader.java
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
...tent-jpa/src/main/java/internal/org/springframework/content/jpa/io/MySQLBlobResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package internal.org.springframework.content.jpa.io; | ||
|
||
import org.springframework.content.jpa.io.AbstractBlobResource; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
public class MySQLBlobResource extends AbstractBlobResource { | ||
|
||
public MySQLBlobResource(Object id, JdbcTemplate template, PlatformTransactionManager txnMgr) { | ||
super(id, template, txnMgr); | ||
} | ||
|
||
@Override | ||
protected String getSelectBlobSQL(Object id) { | ||
return "SELECT id, 'content' as content FROM BLOBS WHERE id='" + id + "'"; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ntent-jpa/src/main/java/internal/org/springframework/content/jpa/io/ResourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package internal.org.springframework.content.jpa.io; | ||
|
||
import org.springframework.core.io.Resource; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
public interface ResourceProvider { | ||
|
||
Resource getResource(Object id, JdbcTemplate template, PlatformTransactionManager txnMgr); | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
...-jpa/src/main/java/internal/org/springframework/content/jpa/io/SQLServerBlobResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package internal.org.springframework.content.jpa.io; | ||
|
||
import com.microsoft.sqlserver.jdbc.SQLServerStatement; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.springframework.content.jpa.io.AbstractBlobResource; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
import javax.sql.DataSource; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.sql.Connection; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
import static java.lang.String.format; | ||
|
||
public class SQLServerBlobResource extends AbstractBlobResource { | ||
|
||
private static Log logger = LogFactory.getLog(SQLServerBlobResource.class); | ||
|
||
public SQLServerBlobResource(Object id, JdbcTemplate template, PlatformTransactionManager txnMgr) { | ||
super(id, template, txnMgr); | ||
} | ||
|
||
@Override | ||
public InputStream getInputStream() throws IOException { | ||
final Object id = getId(); | ||
|
||
String sql = getSelectBlobSQL(getId()); | ||
|
||
DataSource ds = getTemplate().getDataSource(); | ||
Connection conn = DataSourceUtils.getConnection(ds); | ||
try { | ||
conn.setAutoCommit(false); | ||
} catch (SQLException e) { | ||
logger.error(format("setting autocommit to false whilst getting content %s", id), e); | ||
} | ||
InputStream is = null; | ||
Statement stmt = null; | ||
ResultSet rs = null; | ||
try { | ||
stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); | ||
|
||
if (stmt.isWrapperFor(com.microsoft.sqlserver.jdbc.SQLServerStatement.class)) { | ||
SQLServerStatement SQLstmt = stmt.unwrap(com.microsoft.sqlserver.jdbc.SQLServerStatement.class); | ||
SQLstmt.setResponseBuffering("adaptive"); | ||
} | ||
rs = stmt.executeQuery(sql); | ||
|
||
if (!rs.next()) | ||
return null; | ||
is = rs.getBinaryStream(2); | ||
} | ||
catch (SQLException e) { | ||
logger.error(format("getting content %s", id), e); | ||
} | ||
return new ClosingInputStream(id, is, rs, stmt, conn, ds); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...-jpa/src/main/java/org/springframework/content/jpa/io/CustomizableBlobResourceLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.springframework.content.jpa.io; | ||
|
||
import internal.org.springframework.content.jpa.io.GenericBlobResource; | ||
import internal.org.springframework.content.jpa.io.ResourceProvider; | ||
import org.springframework.content.jpa.io.BlobResourceLoader; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.util.ClassUtils; | ||
|
||
import java.util.function.Function; | ||
|
||
public class CustomizableBlobResourceLoader implements BlobResourceLoader { | ||
|
||
private JdbcTemplate template; | ||
private PlatformTransactionManager txnMgr; | ||
private String databaseName; | ||
private ResourceProvider resourceProvider; | ||
|
||
public CustomizableBlobResourceLoader(JdbcTemplate template, PlatformTransactionManager txnMgr) { | ||
this.template = template; | ||
this.txnMgr = txnMgr; | ||
this.databaseName = "GENERIC"; | ||
this.resourceProvider = (l, t, txn) -> { return new GenericBlobResource(l, t, txn);}; | ||
} | ||
|
||
public CustomizableBlobResourceLoader(JdbcTemplate template, PlatformTransactionManager txnMgr, String databaseName, ResourceProvider resourceProvider) { | ||
this.template = template; | ||
this.txnMgr = txnMgr; | ||
this.databaseName = databaseName; | ||
this.resourceProvider = resourceProvider; | ||
} | ||
|
||
@Override | ||
public String getDatabaseName() { | ||
return databaseName; | ||
} | ||
|
||
@Override | ||
public Resource getResource(String location) { | ||
return resourceProvider.getResource(location, template, txnMgr); | ||
} | ||
|
||
@Override | ||
public ClassLoader getClassLoader() { | ||
return ClassUtils.getDefaultClassLoader(); | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
spring-content-jpa/src/main/resources/org/springframework/content/jpa/schema-postgresql.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
CREATE TABLE IF NOT EXISTS BLOBS ( | ||
id SERIAL PRIMARY KEY, | ||
content bytea | ||
content oid | ||
); | ||
ALTER TABLE BLOBS ALTER COLUMN id TYPE VARCHAR(36) | ||
ALTER TABLE BLOBS ALTER COLUMN id TYPE VARCHAR(36) |
5 changes: 2 additions & 3 deletions
5
spring-content-jpa/src/main/resources/org/springframework/content/jpa/schema-sqlserver.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='BLOBS' AND xtype='U') CREATE TABLE BLOBS ( id VARCHAR(36) NOT NULL, content varBinary(MAX) ) | ||
ALTER TABLE BLOBS ADD CONSTRAINT pk_id PRIMARY KEY (id) | ||
|
||
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='BLOBS' AND xtype='U') CREATE TABLE BLOBS ( id VARCHAR(36) NOT NULL, content varBinary(MAX) ); | ||
ALTER TABLE BLOBS ADD CONSTRAINT pk_id PRIMARY KEY (id); |
Oops, something went wrong.