From 5a1256a8ed931f7a5ba05c4328353411bae31f2b Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 10 Aug 2013 15:44:58 +0200 Subject: o Moving code out to separate generators. o Creating SqlSession and SqlSessionFactory. --- .../java/io/trygvis/persistence/sql/SqlDao.java | 25 +++++++++++++++-- .../io/trygvis/persistence/sql/SqlSession.java | 31 ++++++++++++++++++++++ .../trygvis/persistence/sql/SqlSessionFactory.java | 23 ++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlSession.java create mode 100644 sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlSessionFactory.java (limited to 'sql-persistence/src/main/java/io') 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 implements FromResultSet { @@ -17,9 +21,26 @@ public abstract class SqlDao implements FromResultSet { public abstract void deleteById(Id id) throws SQLException; -// public abstract TypedQuery query(); - public abstract T selectById(Id id) throws SQLException; public abstract void update(T entity) throws SQLException; + + public abstract List selectWhere(String where) throws SQLException; + + // ----------------------------------------------------------------------- + // Protected + // ----------------------------------------------------------------------- + + protected static List runQuery(Connection c, String sql, FromResultSet f) throws SQLException { + try (PreparedStatement stmt = c.prepareStatement(sql)) { + ResultSet rs = stmt.executeQuery(); + + List 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 List query(SqlExecutor.QueryCommand 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 { + 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); +} -- cgit v1.2.3