diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2013-08-10 10:21:52 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2013-08-10 10:21:52 +0200 |
commit | 30fe316402288ea19a86340d4c869351ce7aa1b1 (patch) | |
tree | a60699c7800d906c81c4ca8e13f9e9e057f9ea2b /sql-persistence | |
parent | 26b01b500065634eb3133dc354a0ba71b13bff56 (diff) | |
download | container-playground-30fe316402288ea19a86340d4c869351ce7aa1b1.tar.gz container-playground-30fe316402288ea19a86340d4c869351ce7aa1b1.tar.bz2 container-playground-30fe316402288ea19a86340d4c869351ce7aa1b1.tar.xz container-playground-30fe316402288ea19a86340d4c869351ce7aa1b1.zip |
o Making a bit more usable.
Diffstat (limited to 'sql-persistence')
-rw-r--r-- | sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlEntityManager.java | 33 | ||||
-rw-r--r-- | sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlQuery.java | 5 |
2 files changed, 25 insertions, 13 deletions
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 adcd2e2..acd4d35 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 @@ -36,6 +36,8 @@ public abstract class SqlEntityManager implements EntityManager { private boolean autoCommit; + private boolean open = true; + private final Tx tx = new Tx(); protected Connection currentConnection() { @@ -229,8 +231,13 @@ public abstract class SqlEntityManager implements EntityManager { } @Override - public <T> TypedQuery<T> createQuery(String qlString, Class<T> resultClass) { - throw new UnsupportedOperationException(); + 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); } @Override @@ -245,19 +252,13 @@ public abstract class SqlEntityManager implements EntityManager { @Override public Query createNativeQuery(String sql) { - throw new UnsupportedOperationException(); + return new SqlQuery<>(null, new SqlExecutorDelegate(), sql, true); } @Override - public Query createNativeQuery(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. - @SuppressWarnings({"UnnecessaryLocalVariable", "unchecked"}) - Class<Object> klass = resultClass; - - SqlDao<?, Object> dao = this.<Object, Object>getDao(klass); - return new SqlQuery<>(dao, new SqlExecutorDelegate(), sqlString, true); + public Query createNativeQuery(String sql, Class resultClass) { + SqlDao<?, ?> dao = getDao(resultClass); + return new SqlQuery<>(dao, new SqlExecutorDelegate(), sql, true); } private class SqlExecutorDelegate implements SqlExecutor { @@ -323,11 +324,17 @@ public abstract class SqlEntityManager implements EntityManager { @Override public void close() { + try { + c.close(); + } catch (SQLException ignore) { + } finally { + open = false; + } } @Override public boolean isOpen() { - throw new UnsupportedOperationException(); + return open; } @Override diff --git a/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlQuery.java b/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlQuery.java index b50b56e..1615113 100644 --- a/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlQuery.java +++ b/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlQuery.java @@ -57,6 +57,10 @@ public class SqlQuery<T> implements TypedQuery<T> { } public List<T> getResultList(int offset, int limit) { + if (fromResultSet == null) { + throw new PersistenceException("This query type doesn't support queries, only updates."); + } + final String sql = generateSql(this.sql, offset, limit); return executor.executeQuery(new SqlExecutor.QueryCommand<T>() { @Override @@ -91,6 +95,7 @@ public class SqlQuery<T> implements TypedQuery<T> { @Override public int run(Connection c) throws SQLException { try (Statement stmt = c.createStatement()) { + System.out.println("sql = " + sql); return stmt.executeUpdate(sql); } } |