summaryrefslogtreecommitdiff
path: root/sql-persistence/src/main/java/io/trygvis
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2013-08-10 15:44:58 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2013-08-10 15:44:58 +0200
commit5a1256a8ed931f7a5ba05c4328353411bae31f2b (patch)
tree2d25ea2076d2dd229bfe7bac61b27977f11889eb /sql-persistence/src/main/java/io/trygvis
parent86c174ddf24b3e5ac8c8d45fa4d7dc6c5d5d3a3a (diff)
downloadcontainer-playground-5a1256a8ed931f7a5ba05c4328353411bae31f2b.tar.gz
container-playground-5a1256a8ed931f7a5ba05c4328353411bae31f2b.tar.bz2
container-playground-5a1256a8ed931f7a5ba05c4328353411bae31f2b.tar.xz
container-playground-5a1256a8ed931f7a5ba05c4328353411bae31f2b.zip
o Moving code out to separate generators.
o Creating SqlSession and SqlSessionFactory.
Diffstat (limited to 'sql-persistence/src/main/java/io/trygvis')
-rw-r--r--sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlDao.java25
-rw-r--r--sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlSession.java31
-rw-r--r--sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlSessionFactory.java23
3 files changed, 77 insertions, 2 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 7df3658..2c82474 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
@@ -1,7 +1,11 @@
package io.trygvis.persistence.sql;
import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
public abstract class SqlDao<Id, T> implements FromResultSet<T> {
@@ -17,9 +21,26 @@ public abstract class SqlDao<Id, T> implements FromResultSet<T> {
public abstract void deleteById(Id id) throws SQLException;
-// public abstract TypedQuery<T> query();
-
public abstract T selectById(Id id) throws SQLException;
public abstract void update(T entity) throws SQLException;
+
+ public abstract List<T> selectWhere(String where) throws SQLException;
+
+ // -----------------------------------------------------------------------
+ // 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();
+
+ List<T> list = new ArrayList<>();
+ while (rs.next()) {
+ list.add(f.fromResultSet(rs));
+ }
+
+ return list;
+ }
+ }
}
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
new file mode 100644
index 0000000..3caa621
--- /dev/null
+++ b/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlSession.java
@@ -0,0 +1,31 @@
+package io.trygvis.persistence.sql;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.List;
+
+public class SqlSession {
+ private final Connection c;
+
+ public SqlSession(Connection c) {
+ this.c = c;
+ }
+
+ public void commit() throws SQLException {
+ c.commit();
+ }
+
+ public void close() throws SQLException {
+ c.close();
+ }
+
+ public void executeUpdate(String sql) throws SQLException {
+ Statement stmt = c.createStatement();
+ stmt.executeUpdate(sql);
+ }
+
+ public <T> List<T> query(SqlExecutor.QueryCommand<T> query) throws SQLException {
+ return query.run(c);
+ }
+}
diff --git a/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlSessionFactory.java b/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlSessionFactory.java
new file mode 100644
index 0000000..adf36d3
--- /dev/null
+++ b/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlSessionFactory.java
@@ -0,0 +1,23 @@
+package io.trygvis.persistence.sql;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.SQLException;
+
+public abstract class SqlSessionFactory<T extends SqlSession> {
+ private final SqlUnit unit;
+ private final DataSource ds;
+
+ public SqlSessionFactory(SqlUnit unit, DataSource ds) {
+ this.unit = unit;
+ this.ds = ds;
+ }
+
+ public T newSession() throws SQLException {
+ Connection c = ds.getConnection();
+
+ return newSession(c);
+ }
+
+ protected abstract T newSession(Connection c);
+}