summaryrefslogtreecommitdiff
path: root/sql-persistence/src
diff options
context:
space:
mode:
Diffstat (limited to 'sql-persistence/src')
-rw-r--r--sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlEntityManager.java33
-rw-r--r--sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlQuery.java5
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);
}
}