From 46cb0cef9303940ade51ffe16831f55ee9cd8e1f Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 10 Aug 2013 18:28:36 +0200 Subject: o Start of loading referenced (singular) entities. --- .../java/io/trygvis/persistence/sql/SqlDao.java | 40 +++++++++++++++------- .../trygvis/persistence/sql/SqlEntityManager.java | 24 +++++++------ .../io/trygvis/persistence/sql/SqlSession.java | 2 +- 3 files changed, 41 insertions(+), 25 deletions(-) (limited to 'sql-persistence/src/main/java/io/trygvis/persistence') 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 2c82474..c253229 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 @@ -7,12 +7,12 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -public abstract class SqlDao implements FromResultSet { +public abstract class SqlDao { - protected final Connection c; + protected final S session; - protected SqlDao(Connection c) { - this.c = c; + protected SqlDao(S session) { + this.session = session; } public abstract void insert(T o) throws SQLException; @@ -31,16 +31,30 @@ public abstract class SqlDao implements FromResultSet { // Protected // ----------------------------------------------------------------------- - protected static List runQuery(Connection c, String sql, FromResultSet f) throws SQLException { - try (PreparedStatement stmt = c.prepareStatement(sql)) { - ResultSet rs = stmt.executeQuery(); + protected abstract Row newRow(ResultSet rs) throws SQLException; - List list = new ArrayList<>(); - while (rs.next()) { - list.add(f.fromResultSet(rs)); - } + protected abstract T newEntity(Row row) throws SQLException; + + protected List runQuery(final String sql) throws SQLException { + return session.query(new SqlExecutor.QueryCommand() { + @Override + public List run(Connection c) throws SQLException { + try (PreparedStatement stmt = c.prepareStatement(sql)) { + ResultSet rs = stmt.executeQuery(); + + List rows = new ArrayList<>(); + while (rs.next()) { + rows.add(newRow(rs)); + } - return list; - } + List entities = new ArrayList(rows.size()); + for (Row row : rows) { + entities.add(newEntity(row)); + } + + return entities; + } + } + }); } } diff --git a/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlEntityManager.java b/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlEntityManager.java index acd4d35..2a44d82 100644 --- a/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlEntityManager.java +++ b/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlEntityManager.java @@ -24,7 +24,7 @@ public abstract class SqlEntityManager implements EntityManager { private Map properties = new HashMap<>(); - public abstract SqlDao getDao(Class klass); + public abstract SqlDao getDao(Class klass); // ----------------------------------------------------------------------- // @@ -83,7 +83,7 @@ public abstract class SqlEntityManager implements EntityManager { try { @SuppressWarnings("unchecked") Class klass = (Class) entity.getClass(); - SqlDao dao = getDao(klass); + SqlDao dao = getDao(klass); T t = dao.selectById(entity); if (t == null) { dao.insert(entity); @@ -115,7 +115,7 @@ public abstract class SqlEntityManager implements EntityManager { @Override public T find(Class entityClass, Object primaryKey) { try { - SqlDao dao = getDao(entityClass); + SqlDao dao = getDao(entityClass); return dao.selectById(primaryKey); } catch (SQLException e) { throw new PersistenceException(e); @@ -232,12 +232,13 @@ public abstract class SqlEntityManager implements EntityManager { @Override public TypedQuery createQuery(String sqlString, Class resultClass) { - // What happens if the transaction is aborted and this query is executes? Can a Query outlive it's connection? - // Or even EntityManager? Should probably store a reference to the current connection and check that the - // current one is the same when executing. - - SqlDao dao = this.getDao(resultClass); - return new SqlQuery<>(dao, new SqlExecutorDelegate(), sqlString, true); + throw new UnsupportedOperationException(); +// // What happens if the transaction is aborted and this query is executes? Can a Query outlive it's connection? +// // Or even EntityManager? Should probably store a reference to the current connection and check that the +// // current one is the same when executing. +// +// SqlDao dao = this.getDao(resultClass); +// return new SqlQuery<>(dao, new SqlExecutorDelegate(), sqlString, true); } @Override @@ -257,8 +258,9 @@ public abstract class SqlEntityManager implements EntityManager { @Override public Query createNativeQuery(String sql, Class resultClass) { - SqlDao dao = getDao(resultClass); - return new SqlQuery<>(dao, new SqlExecutorDelegate(), sql, true); + throw new UnsupportedOperationException(); +// SqlDao dao = getDao(resultClass); +// return new SqlQuery<>(dao, new SqlExecutorDelegate(), sql, true); } private class SqlExecutorDelegate implements SqlExecutor { 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 3caa621..b5e58eb 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,7 +6,7 @@ import java.sql.Statement; import java.util.List; public class SqlSession { - private final Connection c; + public final Connection c; public SqlSession(Connection c) { this.c = c; -- cgit v1.2.3