Added full project
This commit is contained in:
parent
0c69e02063
commit
6daddc6d2a
137 changed files with 11274 additions and 0 deletions
39
tpquery-executor-jpa/pom.xml
Normal file
39
tpquery-executor-jpa/pom.xml
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
<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>
|
||||
<groupId>net.forwardfire.tpquery</groupId>
|
||||
<artifactId>tpquery</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>tpquery-executor-jpa</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.forwardfire.tpquery</groupId>
|
||||
<artifactId>tpquery-store</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-entitymanager</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat</groupId>
|
||||
<artifactId>tomcat-jasper-el</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- note has to be after api-impl -->
|
||||
<dependency>
|
||||
<groupId>javax</groupId>
|
||||
<artifactId>javaee-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Copyright (c) 2013-2015, Willem Cazander
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
|
||||
* that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
|
||||
* following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
|
||||
* the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
||||
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package net.forwardfire.tpquery.store.executor.jpa;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import net.forwardfire.tpquery.TPQFactory;
|
||||
import net.forwardfire.tpquery.store.executor.AbstractTPQExecutor;
|
||||
import net.forwardfire.tpquery.store.executor.TPQExecutorContext;
|
||||
|
||||
public class TPQExecutorJpa extends AbstractTPQExecutor<EntityManager,Query> {
|
||||
|
||||
private static final String HINT_TIMEOUT = "javax.persistence.query.timeout";
|
||||
|
||||
public TPQExecutorJpa(TPQExecutorContext context) {
|
||||
super(context);
|
||||
registrateStatementCreator(TPQFactory.StatementLanguage.HQL, (em,stmt) -> em.createQuery(stmt));
|
||||
registrateStatementCreator(TPQFactory.StatementLanguage.SQL, (em,stmt) -> em.createNativeQuery(stmt));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <E> List<E> selectList(EntityManager entityManager,String queryName,Map<String,Object> parameters) {
|
||||
return createPreparedStatement(entityManager, queryName, parameters).getResultList();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <E> E selectObject(EntityManager entityManager,String queryName,Map<String,Object> parameters) {
|
||||
return (E)createPreparedStatement(entityManager, queryName, parameters).getSingleResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int executeUpdate(Query persistenceQuery) {
|
||||
return persistenceQuery.executeUpdate();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void prepareParameterValue(Query persistenceQuery, int index,Object value) {
|
||||
persistenceQuery.setParameter(index,value);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void prepareTimeout(Query persistenceQuery, Integer timeout) {
|
||||
persistenceQuery.setHint(HINT_TIMEOUT, timeout);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
/**
|
||||
* Persistance executor.
|
||||
*
|
||||
* @author Willem Cazander
|
||||
*/
|
||||
package net.forwardfire.tpquery.store.executor.jpa;
|
||||
|
|
@ -0,0 +1,229 @@
|
|||
package net.forwardfire.tpquery.store.executor.jpa;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
import net.forwardfire.tpquery.TPQManager;
|
||||
import net.forwardfire.tpquery.TPQFactory;
|
||||
import net.forwardfire.tpquery.config.TPQConfig;
|
||||
import net.forwardfire.tpquery.statement.AbstractTPQStatementWriter;
|
||||
import net.forwardfire.tpquery.statement.TPQStatementWriter;
|
||||
import net.forwardfire.tpquery.statement.language.TPQStatementLanguage;
|
||||
import net.forwardfire.tpquery.store.executor.jpa.TPQExecutorJpa;
|
||||
import net.forwardfire.tpquery.store.proxy.TPQueryName;
|
||||
import net.forwardfire.tpquery.store.proxy.TPQueryProxyFactory;
|
||||
import net.forwardfire.tpquery.store.proxy.TPQueryParameterName;
|
||||
import net.forwardfire.tpquery.store.proxy.TPQueryProxy;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
|
||||
public class TPQExecutorJpaTest {
|
||||
|
||||
private EntityManagerFactory emFactory;
|
||||
private EntityManager em = null;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupLogging() {
|
||||
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
|
||||
lc.reset(); // disable logging
|
||||
}
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
emFactory = Persistence.createEntityManagerFactory("junit");
|
||||
em = emFactory.createEntityManager();
|
||||
}
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
em.close();
|
||||
emFactory.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simple() throws Exception {
|
||||
|
||||
TestEntity t = new TestEntity();
|
||||
t.active = true;
|
||||
t.name = "test";
|
||||
t.description = "junit test 012345678901234567890123456789";
|
||||
|
||||
em.getTransaction().begin();
|
||||
em.persist(t);
|
||||
em.getTransaction().commit();
|
||||
|
||||
TPQManager store = TPQFactory.createManagerBuilder()
|
||||
.createQuerySet("junit", "jar:junit:mem")
|
||||
.setLanguage(TPQFactory.StatementLanguage.HQL)
|
||||
.createQuery("test-select-sql")
|
||||
.setLanguage("SQL")
|
||||
.setTimeout(2000)
|
||||
.appendText("SELECT * from TestEntity")
|
||||
.build()
|
||||
.createQuery("test-select").appendText("from TestEntity").build()
|
||||
.createQuery("test-select-single")
|
||||
.appendText("from TestEntity te where te.name=")
|
||||
.appendParameter("name")
|
||||
.createQueryParameter("name","text").build()
|
||||
.build()
|
||||
.createQuery("test-update")
|
||||
.appendText("update TestEntity te set te.name=")
|
||||
.appendParameter("name")
|
||||
.createQueryParameter("name","text").build()
|
||||
.build()
|
||||
.build()
|
||||
.build();
|
||||
|
||||
TPQExecutorJpa exe = new TPQExecutorJpa(store.getQueryExecutorContext());
|
||||
|
||||
//TQueryPrepared p = store.getQueryExecutorContext().prepareQuery("junit.test", null);
|
||||
//Query q = em.createQuery(p.getStatement());
|
||||
//q.getSingleResult();
|
||||
//q.getResultList();
|
||||
|
||||
List<TestEntity> resultSql = exe.selectList(em, "junit.test-select-sql", null);
|
||||
assertNotNull(resultSql);
|
||||
assertFalse(resultSql.isEmpty());
|
||||
|
||||
List<TestEntity> result = exe.selectList(em, "junit.test-select", null);
|
||||
assertNotNull(result);
|
||||
assertFalse(result.isEmpty());
|
||||
|
||||
Map<String,Object> para = new HashMap<>();
|
||||
para.put("name", "foobar");
|
||||
em.getTransaction().begin();
|
||||
int updateResult = exe.executeUpdate(em, "junit.test-update", para);
|
||||
em.getTransaction().commit();
|
||||
assertEquals(1,updateResult);
|
||||
|
||||
TestEntity resultSingle = exe.selectObject(em, "junit.test-select-single", para);
|
||||
assertNotNull(resultSingle);
|
||||
//assertEquals("foobar",resultSingle.name);
|
||||
}
|
||||
|
||||
@Test(expected=NullPointerException.class)
|
||||
public void testUnsupportedLanguage() throws Exception {
|
||||
TPQConfig config = new TPQConfig();
|
||||
config.addStatementLanguage(new TPQStatementLanguage() {
|
||||
@Override
|
||||
public String getLanguageType() {
|
||||
return "XQL";
|
||||
}
|
||||
|
||||
@Override
|
||||
public TPQStatementWriter createQueryWriter(Map<String, Object> parameterData) {
|
||||
return new AbstractTPQStatementWriter(parameterData) {
|
||||
};
|
||||
}
|
||||
});
|
||||
TPQManager store = TPQFactory
|
||||
.createManagerBuilder(config)
|
||||
.createQuerySet("junit", "jar:mem:test")
|
||||
.createQuery("test")
|
||||
.setLanguage("XQL")
|
||||
.parseStatement("select * from table")
|
||||
.build()
|
||||
.build()
|
||||
.build();
|
||||
TPQExecutorJpa exe = new TPQExecutorJpa(store.getQueryExecutorContext());
|
||||
exe.selectList(em, "junit.test", null);
|
||||
}
|
||||
|
||||
@Test()
|
||||
public void testQueryProxy() throws Exception {
|
||||
TPQManager store = TPQFactory.createManagerBuilder()
|
||||
.createQuerySet("junit", "jar:junit:mem")
|
||||
.setLanguage(TPQFactory.StatementLanguage.HQL)
|
||||
.createQuery("testSelect").appendText("from TestEntity").build()
|
||||
.createQuery("testSelectSingle")
|
||||
.appendText("from TestEntity te where te.name=")
|
||||
.appendParameter("name")
|
||||
.createQueryParameter("name","text").build()
|
||||
.build()
|
||||
.createQuery("testSelectMultiple")
|
||||
.appendText("from TestEntity te where te.name=")
|
||||
.appendParameter("name")
|
||||
.appendText(" and length(te.name) > ")
|
||||
.appendParameter("minLength")
|
||||
.createQueryParameter("name","text").build()
|
||||
.createQueryParameter("minLength","int").build()
|
||||
.build()
|
||||
.createQuery("testUpdate")
|
||||
.appendText("update TestEntity te set te.name=")
|
||||
.appendParameter("name")
|
||||
.createQueryParameter("name","text").build()
|
||||
.build()
|
||||
.createQuerySetTree("TestEntityFooBarDataService")
|
||||
.createQuery("testFoo").appendText("from TestEntity").build()
|
||||
.buildTree()
|
||||
.build()
|
||||
.build();
|
||||
TPQExecutorJpa exe = new TPQExecutorJpa(store.getQueryExecutorContext());
|
||||
|
||||
|
||||
TPQueryProxyFactory proxy = new TPQueryProxyFactory();
|
||||
proxy.registrateResultHandler(Object.class, (queryName,parameters) -> exe.selectObject (em, queryName, parameters));
|
||||
proxy.registrateResultHandler(List.class, (queryName,parameters) -> exe.selectList (em, queryName, parameters));
|
||||
proxy.registrateResultHandler(void.class, (queryName,parameters) -> exe.executeUpdate(em, queryName, parameters));
|
||||
proxy.registrateResultHandler(int.class, (queryName,parameters) -> exe.executeUpdate(em, queryName, parameters));
|
||||
proxy.registrateResultHandler(Integer.class, (queryName,parameters) -> exe.executeUpdate(em, queryName, parameters));
|
||||
|
||||
TestEntityDataService dataService = proxy.newProxyInstance(TestEntityDataService.class);
|
||||
TestEntityFooBarDataService foobarService = proxy.newProxyInstance(TestEntityFooBarDataService.class);
|
||||
|
||||
List<TestEntity> d = dataService.testSelect();
|
||||
assertNotNull(d);
|
||||
|
||||
d = dataService.testSelectMultiple(1,"abc1");
|
||||
d = dataService.testSelectMultiple(2,"abc2");
|
||||
d = dataService.testSelectMultiple(3,"abc3");
|
||||
assertNotNull(d);
|
||||
|
||||
d = foobarService.testFoo();
|
||||
assertNotNull(d);
|
||||
}
|
||||
|
||||
@TPQueryProxy(prefix="junit.")
|
||||
public interface TestEntityDataService {
|
||||
|
||||
List<TestEntity> testSelect();
|
||||
|
||||
@TPQueryName("testSelectSingle")
|
||||
TestEntity testSelectSingle(
|
||||
@TPQueryParameterName("name")
|
||||
String name
|
||||
);
|
||||
|
||||
List<TestEntity> testSelectMultiple(
|
||||
@TPQueryParameterName("minLength")
|
||||
int length,
|
||||
@TPQueryParameterName("name")
|
||||
String name
|
||||
);
|
||||
|
||||
void testUpdate(@TPQueryParameterName("name")String name);
|
||||
}
|
||||
|
||||
@TPQueryProxy(prefix="junit.%s.")
|
||||
interface TestEntityFooBarDataService {
|
||||
|
||||
@TPQueryName()
|
||||
List<TestEntity> testFoo();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
package net.forwardfire.tpquery.store.executor.jpa;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
import org.hibernate.validator.constraints.NotEmpty;
|
||||
|
||||
|
||||
@Entity
|
||||
public class TestEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
Long id;
|
||||
|
||||
@Column(nullable=false)
|
||||
@NotNull
|
||||
Boolean active;
|
||||
|
||||
@NotEmpty
|
||||
@Column(unique = true)
|
||||
String name;
|
||||
|
||||
@NotNull
|
||||
@Length(min = 30, max=200)
|
||||
String description;
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
|
||||
version="1.0">
|
||||
<persistence-unit name="junit" transaction-type="RESOURCE_LOCAL">
|
||||
<class>net.forwardfire.tpquery.store.executor.jpa.TestEntity</class>
|
||||
<properties>
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
|
||||
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
|
||||
|
||||
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" />
|
||||
<property name="javax.persistence.jdbc.user" value="" />
|
||||
<property name="javax.persistence.jdbc.password" value="" />
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
||||
Loading…
Add table
Add a link
Reference in a new issue