io.helidon.dbclient.DbColumnBase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of helidon-dbclient Show documentation
Show all versions of helidon-dbclient Show documentation
Helidon Database Client API
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* 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.
*/
package io.helidon.dbclient;
import java.util.Optional;
import java.util.function.Function;
import io.helidon.common.GenericType;
import io.helidon.common.mapper.MapperException;
import io.helidon.common.mapper.MapperManager;
import io.helidon.common.mapper.Value;
/**
* Base {@link DbColumn} implementation.
*/
public abstract class DbColumnBase implements DbColumn {
private final Object value;
private final MapperManager mapperManager;
private final String[] mappingQualifiers;
/**
* Create a new instance.
*
* @param value value
* @param mapperManager mapper manager
* @param mappingQualifiers mapping qualifiers
*/
protected DbColumnBase(Object value, MapperManager mapperManager, String... mappingQualifiers) {
this.value = value;
this.mapperManager = mapperManager;
this.mappingQualifiers = mappingQualifiers;
}
/**
* Get raw value of the database column.
*
* @return raw value of the column
*/
protected Object rawValue() {
return value;
}
@Override
public T get(Class type) throws MapperException {
if (null == value) {
return null;
}
if (type.isAssignableFrom(value.getClass())) {
return type.cast(value);
}
return map(value, type);
}
@Override
public T get(GenericType type) throws MapperException {
if (null == value) {
return null;
}
if (type.isClass()) {
Class> theClass = type.rawType();
if (theClass.isAssignableFrom(value.getClass())) {
return type.cast(value);
}
}
return map(value, type);
}
@Override
public Value as(Class type) throws MapperException {
return Value.create(mapperManager, name(), get(type), "dbclient", "column");
}
@Override
public Value as(GenericType type) throws MapperException {
return Value.create(mapperManager, name(), get(type), "dbclient", "column");
}
@Override
public Value as(Function super Object, ? extends N> mapper) {
return Value.create(mapperManager, name(), mapper.apply(value), "dbclient", "column");
}
@Override
public Optional