org.jooq.impl.DAOImpl Maven / Gradle / Ivy
/*
* Copyright (c) 2009-2016, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.jooq.impl.DSL.row;
import static org.jooq.impl.DSL.using;
import static org.jooq.impl.Tools.EMPTY_RECORD;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import org.jooq.Condition;
import org.jooq.Configuration;
import org.jooq.DAO;
import org.jooq.Field;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.SQLDialect;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.UniqueKey;
import org.jooq.UpdatableRecord;
import org.jooq.conf.Settings;
/**
* A common base implementation for generated {@link DAO}.
*
* Unlike many other elements in the jOOQ API, DAO
may be used in
* the context of Spring, CDI, or EJB lifecycle management. This means that no
* methods in the DAO
type hierarchy must be made final. See also
* https://github.com/jOOQ/
* jOOQ/issues/4696 for more details.
*
* @author Lukas Eder
*/
public abstract class DAOImpl, P, T> implements DAO {
private final Table table;
private final Class type;
private RecordMapper mapper;
private Configuration configuration;
// -------------------------------------------------------------------------
// XXX: Constructors and initialisation
// -------------------------------------------------------------------------
protected DAOImpl(Table table, Class type) {
this(table, type, null);
}
protected DAOImpl(Table table, Class type, Configuration configuration) {
this.table = table;
this.type = type;
setConfiguration(configuration);
}
/**
* Inject a configuration.
*
* This method is maintained to be able to configure a DAO
* using Spring. It is not exposed in the public API.
*/
public /* non-final */ void setConfiguration(Configuration configuration) {
this.configuration = configuration;
this.mapper = Tools.configuration(configuration).recordMapperProvider().provide(table.recordType(), type);
}
@Override
public /* non-final */ Configuration configuration() {
return configuration;
}
@Override
public /* non-final */ Settings settings() {
return Tools.settings(configuration());
}
@Override
public /* non-final */ SQLDialect dialect() {
return Tools.configuration(configuration()).dialect();
}
@Override
public /* non-final */ SQLDialect family() {
return dialect().family();
}
/**
* {@inheritDoc}
*
* Subclasses may override this method to provide custom implementations.
*/
@Override
public /* non-final */ RecordMapper mapper() {
return mapper;
}
// -------------------------------------------------------------------------
// XXX: DAO API
// -------------------------------------------------------------------------
@Override
public /* non-final */ void insert(P object) {
insert(singletonList(object));
}
@SuppressWarnings("unchecked")
@Override
public /* non-final */ void insert(P... objects) {
insert(asList(objects));
}
@Override
public /* non-final */ void insert(Collection objects) {
// Execute a batch INSERT
if (objects.size() > 1) {
using(configuration).batchInsert(records(objects, false)).execute();
}
// Execute a regular INSERT
else if (objects.size() == 1) {
records(objects, false).get(0).insert();
}
}
@Override
public /* non-final */ void update(P object) {
update(singletonList(object));
}
@SuppressWarnings("unchecked")
@Override
public /* non-final */ void update(P... objects) {
update(asList(objects));
}
@Override
public /* non-final */ void update(Collection
objects) {
// Execute a batch UPDATE
if (objects.size() > 1) {
using(configuration).batchUpdate(records(objects, true)).execute();
}
// Execute a regular UPDATE
else if (objects.size() == 1) {
records(objects, true).get(0).update();
}
}
@Override
public /* non-final */ void delete(P object) {
delete(singletonList(object));
}
@SuppressWarnings("unchecked")
@Override
public /* non-final */ void delete(P... objects) {
delete(asList(objects));
}
@Override
public /* non-final */ void delete(Collection
objects) {
// Execute a batch DELETE
if (objects.size() > 1) {
using(configuration).batchDelete(records(objects, true)).execute();
}
// Execute a regular DELETE
else if (objects.size() == 1) {
records(objects, true).get(0).delete();
}
}
@SuppressWarnings("unchecked")
@Override
public /* non-final */ void deleteById(T... ids) {
deleteById(asList(ids));
}
@Override
public /* non-final */ void deleteById(Collection ids) {
Field>[] pk = pk();
if (pk != null) {
using(configuration).delete(table).where(equal(pk, ids)).execute();
}
}
@Override
public /* non-final */ boolean exists(P object) {
return existsById(getId(object));
}
@Override
public /* non-final */ boolean existsById(T id) {
Field>[] pk = pk();
if (pk != null) {
return using(configuration)
.selectCount()
.from(table)
.where(equal(pk, id))
.fetchOne(0, Integer.class) > 0;
}
else {
return false;
}
}
@Override
public /* non-final */ long count() {
return using(configuration)
.selectCount()
.from(table)
.fetchOne(0, Long.class);
}
@Override
public /* non-final */ List findAll() {
return using(configuration)
.selectFrom(table)
.fetch()
.map(mapper());
}
@Override
public /* non-final */ P findById(T id) {
Field>[] pk = pk();
R record = null;
if (pk != null) {
record = using(configuration)
.selectFrom(table)
.where(equal(pk, id))
.fetchOne();
}
return record == null ? null : mapper().map(record);
}
@SuppressWarnings("unchecked")
@Override
public /* non-final */ List fetch(Field field, Z... values) {
return using(configuration)
.selectFrom(table)
.where(field.in(values))
.fetch()
.map(mapper());
}
@Override
public /* non-final */ P fetchOne(Field field, Z value) {
R record = using(configuration)
.selectFrom(table)
.where(field.equal(value))
.fetchOne();
return record == null ? null : mapper().map(record);
}
@Override
public /* non-final */ Optional fetchOptional(Field field, Z value) {
return Optional.ofNullable(fetchOne(field, value));
}
@Override
public /* non-final */ Table getTable() {
return table;
}
@Override
public /* non-final */ Class getType() {
return type;
}
// ------------------------------------------------------------------------
// XXX: Template methods for generated subclasses
// ------------------------------------------------------------------------
protected abstract T getId(P object);
@SuppressWarnings("unchecked")
protected /* non-final */ T compositeKeyRecord(Object... values) {
UniqueKey key = table.getPrimaryKey();
if (key == null)
return null;
TableField[] fields = (TableField[]) key.getFieldsArray();
Record result = DSL.using(configuration)
.newRecord(fields);
for (int i = 0; i < values.length; i++)
result.set(fields[i], fields[i].getDataType().convert(values[i]));
return (T) result;
}
// ------------------------------------------------------------------------
// XXX: Private utility methods
// ------------------------------------------------------------------------
@SuppressWarnings("unchecked")
private /* non-final */ Condition equal(Field>[] pk, T id) {
if (pk.length == 1) {
return ((Field