Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.sap.cds.jdbc.generic.AbstractValueBinder Maven / Gradle / Ivy
/************************************************************************
* © 2021-2024 SAP SE or an SAP affiliate company. All rights reserved. *
************************************************************************/
package com.sap.cds.jdbc.generic;
import static com.sap.cds.util.CdsTypeUtils.dateTime;
import static com.sap.cds.util.CdsTypeUtils.timestamp;
import java.io.InputStream;
import java.io.Reader;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.TimeZone;
import com.sap.cds.CdsVector;
import com.sap.cds.jdbc.spi.ValueBinder;
import com.sap.cds.reflect.CdsBaseType;
public abstract class AbstractValueBinder implements ValueBinder {
protected AbstractValueBinder(int timestampFractionalSeconds, TimeZone timeZone) {
this.timestampFractionalSeconds = timestampFractionalSeconds;
this.localCalendar = ThreadLocal.withInitial(() -> Calendar.getInstance(timeZone));
}
protected static final ThreadLocal UTC = // NOSONAR
ThreadLocal.withInitial(() -> Calendar.getInstance(TimeZone.getTimeZone("UTC")));
private final int timestampFractionalSeconds;
private final ThreadLocal localCalendar;
@Override
@SuppressWarnings("unchecked")
public Getter getter(CdsBaseType cdsType, boolean isMediaType) {
if (cdsType != null) {
return (Getter) getValueFromResult(cdsType, isMediaType);
}
return (result, i) -> (T) result.getObject(i);
}
@Override
@SuppressWarnings("unchecked")
public T getValue(ResultSet result, int i, CdsBaseType cdsType, boolean isMediaType) throws SQLException {
return (T) getter(cdsType, isMediaType).get(result, i);
}
@Override
public void setValue(PreparedStatement pstmt, int i, CdsBaseType cdsType, Object value) throws SQLException {
setter(cdsType).set(pstmt, i, value);
}
@Override
public Setter setter(CdsBaseType cdsType) {
if (cdsType == null) {
return this::setValue;
}
switch (cdsType) {
case DATE:
return this::setLocalDate;
case DATETIME:
return (pstmt, i, val) -> setInstant(pstmt, i, dateTime(val));
case TIME:
return this::setLocalTime;
case TIMESTAMP:
return (pstmt, i, val) -> setInstant(pstmt, i,
timestamp(val, timestampFractionalSeconds));
case LARGE_BINARY:
return this::setLargeBinary;
case HANA_CLOB:
case LARGE_STRING:
return this::setLargeString;
case VECTOR:
return this::setRealVector;
default:
return PreparedStatement::setObject;
}
}
protected void setValue(PreparedStatement pstmt, int i, Object value) throws SQLException {
if (value instanceof Instant instant) {
setInstant(pstmt, i, instant);
} else if (value instanceof LocalDate date) {
setLocalDate(pstmt, i, date);
} else if (value instanceof LocalTime time) {
setLocalTime(pstmt, i, time);
} else if (value instanceof ZonedDateTime time) {
setZonedDateTime(pstmt, i, time);
} else if (value instanceof Timestamp timestamp) {
setInstant(pstmt, i, timestamp.toInstant());
} else if (value instanceof java.sql.Time time) {
setLocalTime(pstmt, i, time.toLocalTime());
} else if (value instanceof java.sql.Date date) {
setLocalDate(pstmt, i, date.toLocalDate());
} else if (value instanceof byte[] bytes) {
pstmt.setBytes(i, bytes);
} else if (value instanceof Reader reader) {
setLargeString(pstmt, i, reader);
} else if (value instanceof InputStream stream) {
setLargeBinary(pstmt, i, stream);
} else {
pstmt.setObject(i, value);
}
}
protected void setRealVector(PreparedStatement pstmt, int i, Object vector) throws SQLException {
throw new UnsupportedOperationException("cds.Vector is not supported on this data store");
}
protected CdsVector getRealVector(ResultSet result, int i) throws SQLException {
throw new UnsupportedOperationException("cds.Vector is not supported on this data store");
}
protected void setLocalTime(PreparedStatement pstmt, int i, Object localTime) throws SQLException {
if (localTime instanceof LocalTime time) {
setLocalTime(pstmt, i, time);
} else if (localTime instanceof String string) {
setLocalTime(pstmt, i, LocalTime.parse(string));
} else if (localTime instanceof java.sql.Time time) {
setLocalTime(pstmt, i, time.toLocalTime());
} else {
pstmt.setObject(i, localTime);
}
}
protected abstract void setLocalTime(PreparedStatement pstmt, int i, LocalTime localTime) throws SQLException;
protected void setLocalDate(PreparedStatement pstmt, int i, Object localDate) throws SQLException {
if (localDate instanceof LocalDate date) {
setLocalDate(pstmt, i, date);
} else if (localDate instanceof String string) {
setLocalDate(pstmt, i, LocalDate.parse(string));
} else if (localDate instanceof java.sql.Date date) {
setLocalDate(pstmt, i, date.toLocalDate());
} else {
pstmt.setObject(i, localDate);
}
}
protected abstract void setLocalDate(PreparedStatement pstmt, int i, LocalDate localDate) throws SQLException;
protected abstract void setInstant(PreparedStatement pstmt, int i, Instant instant) throws SQLException;
private void setLargeBinary(PreparedStatement pstmt, int i, Object largeBin) throws SQLException {
if (largeBin instanceof InputStream stream) {
setLargeBinary(pstmt, i, stream);
} else {
pstmt.setObject(i, largeBin);
}
}
protected abstract void setLargeBinary(PreparedStatement result, int i, InputStream stream) throws SQLException;
private void setZonedDateTime(PreparedStatement pstmt, int i, ZonedDateTime zonedDateTime) throws SQLException {
setInstant(pstmt, i, dateTime(zonedDateTime.toInstant()));
}
protected abstract LocalTime getLocalTime(ResultSet result, int i) throws SQLException;
protected abstract LocalDate getLocalDate(ResultSet result, int i) throws SQLException;
protected abstract Instant getInstant(ResultSet result, int i) throws SQLException;
private void setLargeString(PreparedStatement pstmt, int i, Object largeStr) throws SQLException {
if (largeStr instanceof Reader reader) {
setLargeString(pstmt, i, reader);
} else {
setString(pstmt, i, largeStr);
}
}
protected abstract void setLargeString(PreparedStatement result, int i, Reader reader) throws SQLException;
protected abstract Reader getLargeString(ResultSet result, int i) throws SQLException;
protected abstract InputStream getLargeBinary(ResultSet result, int i) throws SQLException;
protected Boolean getBoolean(ResultSet result, int i) throws SQLException {
boolean bool = result.getBoolean(i);
return result.wasNull() ? null : bool;
}
protected Double getDouble(ResultSet result, int i) throws SQLException {
double dbl = result.getDouble(i);
return result.wasNull() ? null : dbl;
}
protected Short getShort(ResultSet result, int i) throws SQLException {
short int16 = result.getShort(i);
return result.wasNull() ? null : int16;
}
protected Integer getInt(ResultSet result, int i) throws SQLException {
int int32 = result.getInt(i);
return result.wasNull() ? null : int32;
}
protected Long getLong(ResultSet result, int i) throws SQLException {
long int64 = result.getLong(i);
return result.wasNull() ? null : int64;
}
private void setString(PreparedStatement pstmt, int i, Object string) throws SQLException {
pstmt.setString(i, (String) string);
}
protected Float getFloat(ResultSet result, int i) throws SQLException {
float real = result.getFloat(i);
return result.wasNull() ? null : real;
}
@SuppressWarnings("deprecation")
private Getter> getValueFromResult(CdsBaseType cdsType, boolean isMediaType) {
switch (cdsType) {
case BOOLEAN:
return this::getBoolean;
case DATE:
return this::getLocalDate;
case DATETIME:
return (result, i) -> dateTime(getInstant(result, i));
case DECIMAL:
case HANA_SMALLDECIMAL:
case DECIMAL_FLOAT:
return ResultSet::getBigDecimal;
case DOUBLE:
return this::getDouble;
case INTEGER:
case INT32:
return this::getInt;
case INTEGER64:
case INT64:
return this::getLong;
case LARGE_BINARY:
if (isMediaType) {
return this::getLargeBinary;
}
case HANA_BINARY:
case BINARY:
return ResultSet::getBytes;
case HANA_CLOB:
case LARGE_STRING:
if (isMediaType) {
return this::getLargeString;
}
case STRING:
return ResultSet::getString;
case TIME:
return this::getLocalTime;
case TIMESTAMP:
return (result, i) -> timestamp(getInstant(result, i), timestampFractionalSeconds);
case UINT8:
case INT16:
case HANA_TINYINT:
case HANA_SMALLINT:
return this::getShort;
case HANA_REAL:
return this::getFloat;
case VECTOR:
return this::getRealVector;
default:
return (result, i) -> result.getObject(i);
}
}
protected Calendar getCalendarForDefaultTimeZone() {
return localCalendar.get();
}
}