summaryrefslogtreecommitdiff
path: root/sql-persistence
diff options
context:
space:
mode:
Diffstat (limited to 'sql-persistence')
-rw-r--r--sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlDao.java40
-rw-r--r--sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlEntityManager.java24
-rw-r--r--sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlSession.java2
3 files changed, 41 insertions, 25 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 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<Id, T> implements FromResultSet<T> {
+public abstract class SqlDao<Id, T, S extends SqlSession, Row> {
- 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<Id, T> implements FromResultSet<T> {
// Protected
// -----------------------------------------------------------------------
- protected static <T> List<T> runQuery(Connection c, String sql, FromResultSet<T> f) throws SQLException {
- try (PreparedStatement stmt = c.prepareStatement(sql)) {
- ResultSet rs = stmt.executeQuery();
+ protected abstract Row newRow(ResultSet rs) throws SQLException;
- List<T> list = new ArrayList<>();
- while (rs.next()) {
- list.add(f.fromResultSet(rs));
- }
+ protected abstract T newEntity(Row row) throws SQLException;
+
+ protected List<T> runQuery(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)) {
+ ResultSet rs = stmt.executeQuery();
+
+ List<Row> rows = new ArrayList<>();
+ while (rs.next()) {
+ rows.add(newRow(rs));
+ }
- return list;
- }
+ List<T> entities = new ArrayList<T>(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<String, Object> properties = new HashMap<>();
- public abstract <Id, T> SqlDao<Id, T> getDao(Class<T> klass);
+ public abstract <Id, T> SqlDao<Id, T, ?, ?> getDao(Class<T> klass);
// -----------------------------------------------------------------------
//
@@ -83,7 +83,7 @@ public abstract class SqlEntityManager implements EntityManager {
try {
@SuppressWarnings("unchecked")
Class<T> klass = (Class<T>) entity.getClass();
- SqlDao<Object, T> dao = getDao(klass);
+ SqlDao<Object, T, ?, ?> 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> T find(Class<T> entityClass, Object primaryKey) {
try {
- SqlDao<Object, T> dao = getDao(entityClass);
+ SqlDao<Object, T, ?, ?> 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 <T> TypedQuery<T> createQuery(String sqlString, Class<T> 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<?, T> dao = this.<Object, T>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<?, T> dao = this.<Object, T>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;