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 | |
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.
4 files changed, 30 insertions, 16 deletions
diff --git a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/EntityHandler.java b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/EntityHandler.java index 9bce51e..1dbc534 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/EntityHandler.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/EntityHandler.java @@ -261,8 +261,8 @@ public class EntityHandler extends AbstractHandler { String desc = "new " + sqlEntityDescType + "(" + toJavaString(entityMirror.tableName) + ", " + toJavaString(entityMirror.defaultFields()) + ", " + - toJavaString(createTableSql.name) + ", " + - toJavaString(dropTableSql.name) + + createTableSql.name + ", " + + dropTableSql.name + ")"; g.addPublicStaticFinalField(sqlEntityDescType, "desc").value(desc); ClassG.InnerClassG typedQuery = g.addInnerClass(entityMirror.queryType(g.imports)); diff --git a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/ClassG.java b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/ClassG.java index 402498c..3aa9c62 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/ClassG.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/ClassG.java @@ -6,6 +6,7 @@ import javax.annotation.Generated; import javax.lang.model.type.TypeMirror; import java.lang.reflect.Modifier; import java.util.ArrayList; +import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import java.util.TreeSet; @@ -22,7 +23,8 @@ public class ClassG { private TypeRef extendsType; private List<TypeRef> implementsTypes = new ArrayList<>(); public final Imports imports = new Imports(); - private final Set<FieldRef> fields = new TreeSet<>(); + // Field order is important + private final Set<FieldRef> fields = new LinkedHashSet<>(); private final List<MethodRef> methods = new ArrayList<>(); private final List<Constructor> constructors = new ArrayList<>(); private final List<InnerClassG> innerClasses = new ArrayList<>(); 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); } } |