summaryrefslogtreecommitdiff
path: root/sql-persistence/src/main/java/io
diff options
context:
space:
mode:
Diffstat (limited to 'sql-persistence/src/main/java/io')
-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);
+}