diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2013-08-10 19:12:49 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2013-08-10 19:20:24 +0200 |
commit | 01557b4626d184f58c492c3d4c1fc74cb9a29a1e (patch) | |
tree | db8b1ab51c7a861f239642ffe49378bb79b132f9 /sql-persistence/src/main/java/io/trygvis/persistence | |
parent | 46cb0cef9303940ade51ffe16831f55ee9cd8e1f (diff) | |
download | container-playground-01557b4626d184f58c492c3d4c1fc74cb9a29a1e.tar.gz container-playground-01557b4626d184f58c492c3d4c1fc74cb9a29a1e.tar.bz2 container-playground-01557b4626d184f58c492c3d4c1fc74cb9a29a1e.tar.xz container-playground-01557b4626d184f58c492c3d4c1fc74cb9a29a1e.zip |
o Getting selectById() to work with references.
Diffstat (limited to 'sql-persistence/src/main/java/io/trygvis/persistence')
-rw-r--r-- | sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlDao.java | 15 | ||||
-rw-r--r-- | sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlSession.java | 1 |
2 files changed, 15 insertions, 1 deletions
diff --git a/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlDao.java b/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlDao.java index c253229..361fd95 100644 --- a/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlDao.java +++ b/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlDao.java @@ -36,10 +36,19 @@ public abstract class SqlDao<Id, T, S extends SqlSession, Row> { protected abstract T newEntity(Row row) throws SQLException; protected List<T> runQuery(final String sql) throws SQLException { + return runQuery(new Preparator() { + @Override + public void prepare(PreparedStatement stmt) throws SQLException { + } + }, sql); + } + + protected List<T> runQuery(final Preparator preparator, final String sql) throws SQLException { return session.query(new SqlExecutor.QueryCommand<T>() { @Override public List<T> run(Connection c) throws SQLException { try (PreparedStatement stmt = c.prepareStatement(sql)) { + preparator.prepare(stmt); ResultSet rs = stmt.executeQuery(); List<Row> rows = new ArrayList<>(); @@ -47,7 +56,7 @@ public abstract class SqlDao<Id, T, S extends SqlSession, Row> { rows.add(newRow(rs)); } - List<T> entities = new ArrayList<T>(rows.size()); + List<T> entities = new ArrayList<>(rows.size()); for (Row row : rows) { entities.add(newEntity(row)); } @@ -57,4 +66,8 @@ public abstract class SqlDao<Id, T, S extends SqlSession, Row> { } }); } + + protected static interface Preparator { + void prepare(PreparedStatement stmt) throws SQLException; + } } diff --git a/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlSession.java b/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlSession.java index b5e58eb..43f1206 100644 --- a/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlSession.java +++ b/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlSession.java @@ -6,6 +6,7 @@ import java.sql.Statement; import java.util.List; public class SqlSession { + // TODO: Make private public final Connection c; public SqlSession(Connection c) { |