info.unterrainer.commons.httpserver.daos.JpqlDao Maven / Gradle / Ivy
package info.unterrainer.commons.httpserver.daos;
import java.util.function.Function;
import jakarta.persistence.EntityManagerFactory;
import info.unterrainer.commons.httpserver.jpas.BasicPermissionJpa;
import info.unterrainer.commons.rdbutils.entities.BasicJpa;
import io.javalin.http.Context;
public class JpqlDao extends BasicJpqlDao
{
/**
* Generates a DAO that lets you build and execute queries.
*
* This dao has a tenant-permission table attached and may retrieve and write
* data per tenant.
*
* {@code tenantReferenceFieldName} defaults to {@code referenceId}
* {@code tenantReferenceFieldName} defaults to {@code tenantId}
*
* @param emf the {@link EntityManagerFactory} to use
* @param type the return-type of the query (the underlying JPA)
* @param tenantJpaType the JPA of the tenant-permission table associated
*/
public JpqlDao(final EntityManagerFactory emf, final Class
type,
final Class extends BasicPermissionJpa> tenantJpaType) {
super(emf, type);
this.coreDao.tenantData = new TenantData(tenantJpaType);
}
/**
* Generates a DAO that lets you build and execute queries.
*
* This dao has a tenant-permission table attached and may retrieve and write
* data per tenant.
*
* {@code tenantReferenceFieldName} defaults to {@code referenceId}
* {@code tenantReferenceFieldName} defaults to {@code tenantId}
*
* @param entityManagerFactorySupplier a supplier providing the correct
* {@link EntityManagerFactory} based on a
* given {@link Context}
* @param type the return-type of the query (the
* underlying JPA)
* @param tenantJpaType the JPA of the tenant-permission table
* associated
*/
public JpqlDao(final Function entityManagerFactorySupplier, final Class type,
final Class extends BasicPermissionJpa> tenantJpaType) {
super(entityManagerFactorySupplier, type);
this.coreDao.tenantData = new TenantData(tenantJpaType);
}
/**
* Generates a DAO that lets you build and execute queries.
*
* This dao has a tenant-permission table attached and may retrieve and write
* data per tenant.
*
* @param emf the {@link EntityManagerFactory} to use
* @param type the return-type of the query (the underlying
* JPA)
* @param tenantJpaType the JPA of the tenant-permission table
* associated
* @param tenantReferenceFieldName the name of the field holding the reference
* to the main-table-id
* @param tenantIdFieldName the name of the field holding the tenant-ID
*/
public JpqlDao(final EntityManagerFactory emf, final Class
type,
final Class extends BasicPermissionJpa> tenantJpaType, final String tenantReferenceFieldName,
final String tenantIdFieldName) {
super(emf, type);
this.coreDao.tenantData = new TenantData(tenantJpaType, tenantReferenceFieldName, tenantIdFieldName);
}
/**
* Generates a DAO that lets you build and execute queries.
*
* This dao has a tenant-permission table attached and may retrieve and write
* data per tenant.
*
* @param entityManagerFactorySupplier a supplier providing the correct
* {@link EntityManagerFactory} based on a
* given {@link Context}
* @param type the return-type of the query (the
* underlying JPA)
* @param tenantJpaType the JPA of the tenant-permission table
* associated
* @param tenantReferenceFieldName the name of the field holding the
* reference to the main-table-id
* @param tenantIdFieldName the name of the field holding the
* tenant-ID
*/
public JpqlDao(final Function entityManagerFactorySupplier, final Class type,
final Class extends BasicPermissionJpa> tenantJpaType, final String tenantReferenceFieldName,
final String tenantIdFieldName) {
super(entityManagerFactorySupplier, type);
this.coreDao.tenantData = new TenantData(tenantJpaType, tenantReferenceFieldName, tenantIdFieldName);
}
/**
* Generates a DAO that lets you build and execute queries.
*
* @param emf the {@link EntityManagerFactory} to use
* @param type the return-type of the query (the underlying JPA)
*/
public JpqlDao(final EntityManagerFactory emf, final Class
type) {
super(emf, type);
}
/**
* Generates a DAO that lets you build and execute queries.
*
* @param entityManagerFactorySupplier a supplier providing the correct
* {@link EntityManagerFactory} based on a
* given {@link Context}
* @param type the return-type of the query (the
* underlying JPA)
*/
public JpqlDao(final Function entityManagerFactorySupplier, final Class type) {
super(entityManagerFactorySupplier, type);
}
/**
* Build a SELECT(*)-like query. The result will be of the underlying generic
* type.
*
* @return a query-builder
*/
public JpaListQueryBuilder
select() {
return new JpaListQueryBuilder<>(emf, this, type);
}
/**
* Build a SELECT(*)-like query. The result will be of the given type (use this
* for a COUNT(*) query, for example).
*
* @param the type the result will be
* @param resultType the type the result will be
* @return a query-builder
*/
public ListQueryBuilder select(final Class resultType) {
return new ListQueryBuilder<>(emf, this, resultType);
}
/**
* Build a SELECT-query with a custom select-clause. The result will be of the
* given type (use this for a COUNT(*) query, for example).
*
* @param the type the result will be
* @param selectClause your custom select-clause (the base-object has the alias
* 'o'. So the default would be "o" internally resulting in
* a "SELECT o")
* @param resultType the type the result will be
* @return a query-builder
*/
public ListQueryBuilder select(final String selectClause, final Class resultType) {
ListQueryBuilder b = new ListQueryBuilder<>(emf, this, resultType);
b.setSelect(selectClause);
return b;
}
/**
* Build a SELECT-query with a custom select-clause. The result will be of the
* underlying generic type.
*
* @param selectClause your custom select-clause (the base-object has the alias
* 'o'. So the default would be "o" internally resulting in
* a "SELECT o")
* @return a query-builder
*/
public JpaListQueryBuilder
select(final String selectClause) {
JpaListQueryBuilder
b = new JpaListQueryBuilder<>(emf, this, type);
b.setSelect(selectClause);
return b;
}
/**
* Get an element by ID.
*
* @param id the ID to fetch.
* @return the element with the given ID or null, if there was no such thing
*/
public SingleQueryBuilder
select(final Long id) {
return new SingleQueryBuilder<>(this, id);
}
/**
* Insert the given entity.
*
* @param entity to insert.
* @return the entity after inserting
*/
public InsertQueryBuilder
insert(final P entity) {
return new InsertQueryBuilder<>(this, entity);
}
/**
* Update the given entity.
*
* @param entity to update.
* @return the entity after updating
*/
public UpdateQueryBuilder
update(final P entity) {
return new UpdateQueryBuilder<>(this, entity);
}
}