From 2b1c32590db960be2b5f62897e65bb10d434cae0 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 3 Aug 2013 23:13:32 +0200 Subject: wip --- .../persistence/sql/AbstractTypedQuery.java | 227 +++++++++++++++++++++ .../io/trygvis/persistence/sql/SqlEntityDesc.java | 11 + 2 files changed, 238 insertions(+) create mode 100644 sql-persistence/src/main/java/io/trygvis/persistence/sql/AbstractTypedQuery.java create mode 100644 sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlEntityDesc.java (limited to 'sql-persistence/src/main') 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 implements TypedQuery { + 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 setMaxResults(int maxResult) { + this.maxResults = maxResult; + return this; + } + + @Override + public TypedQuery setFirstResult(int firstResult) { + this.firstResult = firstResult; + return this; + } + + @Override + public TypedQuery setHint(String hintName, Object value) { + throw new UnsupportedOperationException(); + } + + @Override + public TypedQuery setParameter(Parameter param, T value) { + throw new UnsupportedOperationException(); + } + + @Override + public TypedQuery setParameter(Parameter param, Calendar value, TemporalType temporalType) { + throw new UnsupportedOperationException(); + } + + @Override + public TypedQuery setParameter(Parameter param, Date value, TemporalType temporalType) { + throw new UnsupportedOperationException(); + } + + @Override + public TypedQuery setParameter(String name, Object value) { + throw new UnsupportedOperationException(); + } + + @Override + public TypedQuery setParameter(String name, Calendar value, TemporalType temporalType) { + throw new UnsupportedOperationException(); + } + + @Override + public TypedQuery setParameter(String name, Date value, TemporalType temporalType) { + throw new UnsupportedOperationException(); + } + + @Override + public TypedQuery setParameter(int position, Object value) { + throw new UnsupportedOperationException(); + } + + @Override + public TypedQuery setParameter(int position, Calendar value, TemporalType temporalType) { + throw new UnsupportedOperationException(); + } + + @Override + public TypedQuery setParameter(int position, Date value, TemporalType temporalType) { + throw new UnsupportedOperationException(); + } + + @Override + public TypedQuery setFlushMode(FlushModeType flushMode) { + throw new UnsupportedOperationException(); + } + + @Override + public TypedQuery 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 getHints() { + throw new UnsupportedOperationException(); + } + + @Override + public Set> getParameters() { + throw new UnsupportedOperationException(); + } + + @Override + public Parameter getParameter(String name) { + throw new UnsupportedOperationException(); + } + + @Override + public Parameter getParameter(String name, Class type) { + throw new UnsupportedOperationException(); + } + + @Override + public Parameter getParameter(int position) { + throw new UnsupportedOperationException(); + } + + @Override + public Parameter getParameter(int position, Class type) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isBound(Parameter param) { + throw new UnsupportedOperationException(); + } + + @Override + public T getParameterValue(Parameter 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 unwrap(Class cls) { + throw new UnsupportedOperationException(); + } + + public List getResultList() { + return getResultList(null, null); + } + + public List 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 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 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; + } +} -- cgit v1.2.3