org.jooq.impl.AbstractRecord Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of payment-retries-plugin Show documentation
Show all versions of payment-retries-plugin Show documentation
Kill Bill Payment Retries plugin
The newest version!
/*
* 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 org.jooq.conf.SettingsTools.updatablePrimaryKeys;
import static org.jooq.impl.Tools.getAnnotatedGetter;
import static org.jooq.impl.Tools.getAnnotatedMembers;
import static org.jooq.impl.Tools.getMatchingGetter;
import static org.jooq.impl.Tools.getMatchingMembers;
import static org.jooq.impl.Tools.hasColumnAnnotations;
import static org.jooq.impl.Tools.indexOrFail;
import static org.jooq.impl.Tools.resetChangedOnNotNull;
import static org.jooq.impl.Tools.settings;
import static org.jooq.impl.Tools.ThreadGuard.Guard.RECORD_TOSTRING;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import java.util.stream.Stream;
import org.jooq.Attachable;
import org.jooq.Converter;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.Record1;
import org.jooq.Record10;
import org.jooq.Record11;
import org.jooq.Record12;
import org.jooq.Record13;
import org.jooq.Record14;
import org.jooq.Record15;
import org.jooq.Record16;
import org.jooq.Record17;
import org.jooq.Record18;
import org.jooq.Record19;
import org.jooq.Record2;
import org.jooq.Record20;
import org.jooq.Record21;
import org.jooq.Record22;
import org.jooq.Record3;
import org.jooq.Record4;
import org.jooq.Record5;
import org.jooq.Record6;
import org.jooq.Record7;
import org.jooq.Record8;
import org.jooq.Record9;
import org.jooq.RecordMapper;
import org.jooq.Result;
import org.jooq.Table;
import org.jooq.UniqueKey;
import org.jooq.exception.InvalidResultException;
import org.jooq.exception.MappingException;
import org.jooq.impl.Tools.ThreadGuard;
import org.jooq.impl.Tools.ThreadGuard.GuardedOperation;
import org.jooq.tools.Convert;
import org.jooq.tools.StringUtils;
/**
* A general base class for all {@link Record} types
*
* @author Lukas Eder
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
abstract class AbstractRecord extends AbstractStore implements Record {
/**
* Generated UID
*/
private static final long serialVersionUID = -6052512608911220404L;
final RowImpl fields;
final Object[] values;
final Object[] originals;
final BitSet changed;
boolean fetched;
AbstractRecord(Collection extends Field>> fields) {
this(new RowImpl(fields));
}
AbstractRecord(Field>... fields) {
this(new RowImpl(fields));
}
AbstractRecord(RowImpl fields) {
int size = fields.size();
this.fields = fields;
this.values = new Object[size];
this.originals = new Object[size];
this.changed = new BitSet(size);
}
// ------------------------------------------------------------------------
// XXX: Attachable API
// ------------------------------------------------------------------------
@Override
final List getAttachables() {
List result = null;
int size = size();
for (int i = 0; i < size; i++) {
if (values[i] instanceof Attachable) {
if (result == null)
result = new ArrayList();
result.add((Attachable) values[i]);
}
}
return result == null ? Collections.emptyList() : result;
}
// ------------------------------------------------------------------------
// XXX: FieldProvider API
// ------------------------------------------------------------------------
@Override
public final Field field(Field field) {
return fieldsRow().field(field);
}
@Override
public final Field> field(String name) {
return fieldsRow().field(name);
}
@Override
public final Field> field(Name name) {
return fieldsRow().field(name);
}
@Override
public final Field> field(int index) {
return index >= 0 && index < fields.size() ? fields.field(index) : null;
}
@Override
public final Field>[] fields() {
return fields.fields();
}
@Override
public final Field>[] fields(Field>... f) {
return fields.fields(f);
}
@Override
public final Field>[] fields(String... fieldNames) {
return fields.fields(fieldNames);
}
@Override
public final Field>[] fields(Name... fieldNames) {
return fields.fields(fieldNames);
}
@Override
public final Field>[] fields(int... fieldIndexes) {
return fields.fields(fieldIndexes);
}
// ------------------------------------------------------------------------
// XXX: Record API
// ------------------------------------------------------------------------
@Override
public final int size() {
return fields.size();
}
@Override
public final T get(Field field) {
return (T) get(indexOrFail(fieldsRow(), field));
}
@Override
public final T get(Field> field, Class extends T> type) {
return Convert.convert(get(field), type);
}
@Override
public final U get(Field field, Converter super T, ? extends U> converter) {
return converter.from(get(field));
}
@Override
public final Object get(int index) {
return values[safeIndex(index)];
}
@Override
public final T get(int index, Class extends T> type) {
return Convert.convert(get(index), type);
}
@Override
public final U get(int index, Converter, ? extends U> converter) {
return Convert.convert(get(index), converter);
}
@Override
public final Object get(String fieldName) {
return get(indexOrFail(fieldsRow(), fieldName));
}
@Override
public final T get(String fieldName, Class extends T> type) {
return Convert.convert(get(fieldName), type);
}
@Override
public final U get(String fieldName, Converter, ? extends U> converter) {
return Convert.convert(get(fieldName), converter);
}
@Override
public final Object get(Name fieldName) {
return get(indexOrFail(fieldsRow(), fieldName));
}
@Override
public final T get(Name fieldName, Class extends T> type) {
return Convert.convert(get(fieldName), type);
}
@Override
public final U get(Name fieldName, Converter, ? extends U> converter) {
return Convert.convert(get(fieldName), converter);
}
/**
* Subclasses may type-unsafely set a value to a record index. This method
* takes care of converting the value to the appropriate type.
*
* @deprecated - Use {@link AbstractRecord#set(int, Object)} instead
*/
@Deprecated
protected final void setValue(int index, Object value) {
set(index, value);
}
protected final void set(int index, Object value) {
set(index, (Field) field(index), value);
}
@Override
public final void set(Field field, T value) {
set(indexOrFail(fields, field), field, value);
}
private final void set(int index, Field field, T value) {
// Relevant issues documenting this method's behaviour:
// [#945] Avoid bugs resulting from setting the same value twice
// [#948] To allow for controlling the number of hard-parses
// To allow for explicitly overriding default values
// [#979] Avoid modifying chnaged flag on unchanged primary key values
UniqueKey> key = getPrimaryKey();
// Normal fields' changed flag is always set to true
if (key == null || !key.getFields().contains(field)) {
changed.set(index);
}
// The primary key's changed flag might've been set previously
else if (changed.get(index)) {
changed.set(index);
}
// [#2764] Users may override updatability of primary key values
else if (updatablePrimaryKeys(settings(this))) {
changed.set(index);
}
// [#2698] If the primary key has not yet been set
else if (originals[index] == null) {
changed.set(index);
}
// [#979] If the primary key is being changed, all other fields' flags
// need to be set to true for in case this record is stored again, an
// INSERT statement will thus be issued
else {
// [#945] Be sure that changed is never reset to false
changed.set(index, changed.get(index) || !StringUtils.equals(values[index], value));
if (changed.get(index)) {
changed(true);
}
}
values[index] = value;
}
@Override
public final void set(Field field, U value, Converter extends T, ? super U> converter) {
set(field, converter.to(value));
}
@Override
public /* non-final */ Record with(Field field, T value) {
set(field, value);
return this;
}
@Override
public Record with(Field field, U value, Converter extends T, ? super U> converter) {
set(field, value, converter);
return this;
}
final void setValues(Field>[] fields, AbstractRecord record) {
fetched = record.fetched;
for (Field> field : fields) {
int targetIndex = indexOrFail(fieldsRow(), field);
int sourceIndex = indexOrFail(record.fieldsRow(), field);
values[targetIndex] = record.get(sourceIndex);
originals[targetIndex] = record.original(sourceIndex);
changed.set(targetIndex, record.changed(sourceIndex));
}
}
final void intern0(int fieldIndex) {
safeIndex(fieldIndex);
if (field(fieldIndex).getType() == String.class) {
values[fieldIndex] = ((String) values[fieldIndex]).intern();
originals[fieldIndex] = ((String) originals[fieldIndex]).intern();
}
}
final int safeIndex(int index) {
if (index >= 0 && index < values.length)
return index;
throw new IllegalArgumentException("No field at index " + index + " in Record type " + fieldsRow());
}
/**
* Subclasses may override this
*/
UniqueKey> getPrimaryKey() {
return null;
}
/*
* This method is overridden covariantly by TableRecordImpl
*/
@Override
public Record original() {
return Tools.newRecord(fetched, (Class) getClass(), fields.fields.fields, configuration())
.operate(new RecordOperation() {
@Override
public AbstractRecord operate(AbstractRecord record) throws RuntimeException {
for (int i = 0; i < originals.length; i++) {
record.values[i] = originals[i];
record.originals[i] = originals[i];
}
return record;
}
});
}
@Override
public final T original(Field field) {
return (T) original(indexOrFail(fieldsRow(), field));
}
@Override
public final Object original(int fieldIndex) {
return originals[safeIndex(fieldIndex)];
}
@Override
public final Object original(String fieldName) {
return original(indexOrFail(fieldsRow(), fieldName));
}
@Override
public final Object original(Name fieldName) {
return original(indexOrFail(fieldsRow(), fieldName));
}
@Override
public final boolean changed() {
return !changed.isEmpty();
}
@Override
public final boolean changed(Field> field) {
return changed(indexOrFail(fieldsRow(), field));
}
@Override
public final boolean changed(int fieldIndex) {
return changed.get(safeIndex(fieldIndex));
}
@Override
public final boolean changed(String fieldName) {
return changed(indexOrFail(fieldsRow(), fieldName));
}
@Override
public final boolean changed(Name fieldName) {
return changed(indexOrFail(fieldsRow(), fieldName));
}
@Override
public final void changed(boolean c) {
changed.set(0, values.length, c);
// [#1995] If a value is meant to be "unchanged", the "original" should
// match the supposedly "unchanged" value.
if (!c) {
System.arraycopy(values, 0, originals, 0, values.length);
}
}
@Override
public final void changed(Field> field, boolean c) {
changed(indexOrFail(fieldsRow(), field), c);
}
@Override
public final void changed(int fieldIndex, boolean c) {
safeIndex(fieldIndex);
changed.set(fieldIndex, c);
// [#1995] If a value is meant to be "unchanged", the "original" should
// match the supposedly "unchanged" value.
if (!c)
originals[fieldIndex] = values[fieldIndex];
}
@Override
public final void changed(String fieldName, boolean c) {
changed(indexOrFail(fieldsRow(), fieldName), c);
}
@Override
public final void changed(Name fieldName, boolean c) {
changed(indexOrFail(fieldsRow(), fieldName), c);
}
@Override
public final void reset() {
changed.clear();
System.arraycopy(originals, 0, values, 0, originals.length);
}
@Override
public final void reset(Field> field) {
reset(indexOrFail(fieldsRow(), field));
}
@Override
public final void reset(int fieldIndex) {
safeIndex(fieldIndex);
changed.clear(fieldIndex);
values[fieldIndex] = originals[fieldIndex];
}
@Override
public final void reset(String fieldName) {
reset(indexOrFail(fieldsRow(), fieldName));
}
@Override
public final void reset(Name fieldName) {
reset(indexOrFail(fieldsRow(), fieldName));
}
@Override
public final Object[] intoArray() {
return into(Object[].class);
}
@Override
public final List