summaryrefslogtreecommitdiff
path: root/sql-persistence
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2013-08-03 23:13:32 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2013-08-03 23:13:32 +0200
commit2b1c32590db960be2b5f62897e65bb10d434cae0 (patch)
tree84623e4293f59f67331a7760a700121737313c48 /sql-persistence
parentdf92538ab3d83da9839f08b28fc8a67317565463 (diff)
downloadcontainer-playground-2b1c32590db960be2b5f62897e65bb10d434cae0.tar.gz
container-playground-2b1c32590db960be2b5f62897e65bb10d434cae0.tar.bz2
container-playground-2b1c32590db960be2b5f62897e65bb10d434cae0.tar.xz
container-playground-2b1c32590db960be2b5f62897e65bb10d434cae0.zip
wip
Diffstat (limited to 'sql-persistence')
-rw-r--r--sql-persistence/pom.xml19
-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
3 files changed, 257 insertions, 0 deletions
diff --git a/sql-persistence/pom.xml b/sql-persistence/pom.xml
new file mode 100644
index 0000000..6d5a56d
--- /dev/null
+++ b/sql-persistence/pom.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <artifactId>container-playground</artifactId>
+ <groupId>io.trygvis.container</groupId>
+ <version>1.0-SNAPSHOT</version>
+ </parent>
+ <artifactId>sql-persistence</artifactId>
+ <dependencies>
+ <dependency>
+ <groupId>org.hibernate.javax.persistence</groupId>
+ <artifactId>hibernate-jpa-2.0-api</artifactId>
+ <version>1.0.1.Final</version>
+ </dependency>
+ </dependencies>
+</project>
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;
+ }
+}