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/AbstractTypedQuery.java227
-rw-r--r--sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlEntityDesc.java11
2 files changed, 238 insertions, 0 deletions
diff --git a/sql-persistence/src/main/java/io/trygvis/persistence/sql/AbstractTypedQuery.java b/sql-persistence/src/main/java/io/trygvis/persistence/sql/AbstractTypedQuery.java
new file mode 100644
index 0000000..1cb8405
--- /dev/null
+++ b/sql-persistence/src/main/java/io/trygvis/persistence/sql/AbstractTypedQuery.java
@@ -0,0 +1,227 @@
+package io.trygvis.persistence.sql;
+
+import javax.persistence.FlushModeType;
+import javax.persistence.LockModeType;
+import javax.persistence.NoResultException;
+import javax.persistence.NonUniqueResultException;
+import javax.persistence.Parameter;
+import javax.persistence.PersistenceException;
+import javax.persistence.TemporalType;
+import javax.persistence.TypedQuery;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public abstract class AbstractTypedQuery<A> implements TypedQuery<A> {
+ private final Connection c;
+ private final SqlEntityDesc sqlEntity;
+
+ private int firstResult;
+ private int maxResults;
+
+ protected AbstractTypedQuery(Connection c, SqlEntityDesc sqlEntity) {
+ this.c = c;
+ this.sqlEntity = sqlEntity;
+ }
+
+ public abstract A fromResultSet(ResultSet rs) throws SQLException;
+
+ @Override
+ public TypedQuery<A> setMaxResults(int maxResult) {
+ this.maxResults = maxResult;
+ return this;
+ }
+
+ @Override
+ public TypedQuery<A> setFirstResult(int firstResult) {
+ this.firstResult = firstResult;
+ return this;
+ }
+
+ @Override
+ public TypedQuery<A> setHint(String hintName, Object value) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public <T> TypedQuery<A> setParameter(Parameter<T> param, T value) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public TypedQuery<A> setParameter(Parameter<Calendar> param, Calendar value, TemporalType temporalType) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public TypedQuery<A> setParameter(Parameter<Date> param, Date value, TemporalType temporalType) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public TypedQuery<A> setParameter(String name, Object value) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public TypedQuery<A> setParameter(String name, Calendar value, TemporalType temporalType) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public TypedQuery<A> setParameter(String name, Date value, TemporalType temporalType) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public TypedQuery<A> setParameter(int position, Object value) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public TypedQuery<A> setParameter(int position, Calendar value, TemporalType temporalType) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public TypedQuery<A> setParameter(int position, Date value, TemporalType temporalType) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public TypedQuery<A> setFlushMode(FlushModeType flushMode) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public TypedQuery<A> setLockMode(LockModeType lockMode) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public int executeUpdate() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public int getMaxResults() {
+ return maxResults;
+ }
+
+ @Override
+ public int getFirstResult() {
+ return firstResult;
+ }
+
+ @Override
+ public Map<String, Object> getHints() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Set<Parameter<?>> getParameters() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Parameter<?> getParameter(String name) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public <T> Parameter<T> getParameter(String name, Class<T> type) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Parameter<?> getParameter(int position) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public <T> Parameter<T> getParameter(int position, Class<T> type) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean isBound(Parameter<?> param) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public <T> T getParameterValue(Parameter<T> param) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Object getParameterValue(String name) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Object getParameterValue(int position) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public FlushModeType getFlushMode() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public LockModeType getLockMode() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public <T> T unwrap(Class<T> cls) {
+ throw new UnsupportedOperationException();
+ }
+
+ public List<A> getResultList() {
+ return getResultList(null, null);
+ }
+
+ public List<A> getResultList(Integer offset, Integer limit) {
+ String sql = "SELECT " + sqlEntity.defaultFields + " FROM " + sqlEntity.tableName;
+
+ if(offset != null) {
+ sql += " OFFSET " + offset;
+ }
+ if(limit != null) {
+ sql += " LIMIT " + limit;
+ }
+
+ sql += ";";
+
+ List<A> list = new ArrayList<>();
+ try (PreparedStatement stmt = c.prepareStatement(sql)) {
+ ResultSet rs = stmt.executeQuery();
+ while (rs.next()) {
+ list.add(fromResultSet(rs));
+ }
+ return list;
+ } catch (SQLException e) {
+ throw new PersistenceException(e);
+ }
+ }
+
+ public A getSingleResult() {
+ List<A> list = getResultList(null, 2);
+ switch (list.size()) {
+ case 1:
+ return list.get(0);
+ case 0:
+ throw new NoResultException();
+ default:
+ throw new NonUniqueResultException();
+ }
+ }
+}
diff --git a/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlEntityDesc.java b/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlEntityDesc.java
new file mode 100644
index 0000000..dbbeed7
--- /dev/null
+++ b/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlEntityDesc.java
@@ -0,0 +1,11 @@
+package io.trygvis.persistence.sql;
+
+public class SqlEntityDesc {
+ public final String tableName;
+ public final String defaultFields;
+
+ public SqlEntityDesc(String tableName, String defaultFields) {
+ this.tableName = tableName;
+ this.defaultFields = defaultFields;
+ }
+}