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.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in com.hazelcast.com.liance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.com.hazelcast.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 com.hazelcast.org.apache.calcite.avatica.remote;
import com.hazelcast.org.apache.calcite.avatica.AvaticaUtils;
import com.hazelcast.org.apache.calcite.avatica.ColumnMetaData;
import com.hazelcast.org.apache.calcite.avatica.ColumnMetaData.AvaticaType;
import com.hazelcast.org.apache.calcite.avatica.ColumnMetaData.Rep;
import com.hazelcast.org.apache.calcite.avatica.SqlType;
import com.hazelcast.org.apache.calcite.avatica.proto.Common;
import com.hazelcast.org.apache.calcite.avatica.util.ArrayFactoryImpl;
import com.hazelcast.org.apache.calcite.avatica.util.Base64;
import com.hazelcast.org.apache.calcite.avatica.util.ByteString;
import com.hazelcast.org.apache.calcite.avatica.util.DateTimeUtils;
import com.hazelcast.com.fasterxml.jackson.annotation.JsonCreator;
import com.hazelcast.com.fasterxml.jackson.annotation.JsonProperty;
import com.hazelcast.com.google.protobuf.Descriptors.FieldDescriptor;
import com.hazelcast.com.google.protobuf.UnsafeByteOperations;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import static java.nio.charset.StandardCharsets.UTF_8;
/** Value and type.
*
*
There are 3 representations:
*
*
JDBC - the representation used by JDBC get and set methods
*
Serial - suitable for serializing using JSON
*
Local - used by Calcite for efficient com.hazelcast.com.utation
*
*
*
The following table shows the Java type(s) that may represent each SQL
* type in each representation.
*
*
*
SQL types and their representations
*
*
Type
JDBC
Serial
Local
*
*
*
BOOLEAN
boolean
boolean
boolean
*
*
*
BINARY, VARBINARY
byte[]
*
String (base64)
{@link ByteString}
*
*
*
DATE
{@link java.sql.Date}
*
int
int
*
*
*
TIME
{@link java.sql.Time}
*
int
int
*
*
*
DATE
{@link java.sql.Timestamp}
*
long
long
*
*
*
CHAR, VARCHAR
*
String
String
String
*
*
*
TINYINT
byte
Number
byte
*
*
*
SMALLINT
short
Number
short
*
*
*
INTEGER
int
Number
int
*
*
*
BIGINT
long
Number
long
*
*
*
REAL
float
Number
float
*
*
*
FLOAT, DOUBLE
*
double
Number
double
*
*
*
DECIMAL
*
BigDecimal
Number
BigDecimal
*
*
*
ARRAY
*
Array
List<Object>
List<Object>
*
*
*
*
Note:
*
*
*
The various numeric types (TINYINT, SMALLINT, INTEGER, BIGINT, REAL,
* FLOAT, DOUBLE) are represented by {@link Number} in serial format because
* JSON numbers are not strongly typed. A {@code float} value {@code 3.0} is
* transmitted as {@code 3}, and is therefore decoded as an {@code int}.
*
*
The date-time types (DATE, TIME, TIMESTAMP) are represented in JDBC as
* {@link java.sql.Date}, {@link java.sql.Time}, {@link java.sql.Timestamp},
* all sub-classes of {@link java.util.Date}. When they are passed to and
* from the server, they are interpreted in terms of a time zone, by default
* the current connection's time zone. Their serial and local representations
* as {@code int} (days since 1970-01-01 for DATE, milliseconds since
* 00:00:00.000 for TIME), and long (milliseconds since 1970-01-01
* 00:00:00.000 for TIMESTAMP) are easier to work with, because it is clear
* that time zone is not involved.
*
*
BINARY and VARBINARY values are represented as base64-encoded strings
* for serialization over JSON.
*
*/
public class TypedValue {
private static final FieldDescriptor NUMBER_DESCRIPTOR = Common.TypedValue.getDescriptor()
.findFieldByNumber(Common.TypedValue.NUMBER_VALUE_FIELD_NUMBER);
private static final FieldDescriptor STRING_DESCRIPTOR = Common.TypedValue.getDescriptor()
.findFieldByNumber(Common.TypedValue.STRING_VALUE_FIELD_NUMBER);
private static final FieldDescriptor BYTES_DESCRIPTOR = Common.TypedValue.getDescriptor()
.findFieldByNumber(Common.TypedValue.BYTES_VALUE_FIELD_NUMBER);
// If the user sets a `null` Object, it's explicitly null
public static final TypedValue EXPLICIT_NULL =
new TypedValue(ColumnMetaData.Rep.OBJECT, null);
// The user might also implicitly not set a value for a parameter.
public static final Common.TypedValue PROTO_IMPLICIT_NULL =
Common.TypedValue.newBuilder().setImplicitlyNull(true).build();
/** Type of the value. */
public final ColumnMetaData.Rep type;
/** Value.
*
*
Always in a form that can be serialized to JSON by Jackson.
* For example, byte arrays are represented as String. */
public final Object value;
/** Non-null for ARRAYs, the type of the values stored in the ARRAY. Null for all other cases. */
public final ColumnMetaData.Rep com.hazelcast.com.onentType;
private TypedValue(ColumnMetaData.Rep rep, Object value) {
this(rep, null, value);
}
private TypedValue(ColumnMetaData.Rep rep, ColumnMetaData.Rep com.hazelcast.com.onentType, Object value) {
this.type = rep;
this.com.hazelcast.com.onentType = com.hazelcast.com.onentType;
this.value = value;
assert isSerial(rep, value) : "rep: " + rep + ", value: " + value;
}
private boolean isSerial(ColumnMetaData.Rep rep, Object value) {
if (value == null) {
return true;
}
switch (rep) {
case BYTE_STRING:
return value instanceof String;
case JAVA_SQL_DATE:
case JAVA_SQL_TIME:
return value instanceof Integer;
case JAVA_SQL_TIMESTAMP:
case JAVA_UTIL_DATE:
return value instanceof Long;
default:
return true;
}
}
@JsonCreator
public static TypedValue create(@JsonProperty("type") String type,
@JsonProperty("value") Object value) {
if (value == null) {
return EXPLICIT_NULL;
}
ColumnMetaData.Rep rep = ColumnMetaData.Rep.valueOf(type);
return ofLocal(rep, serialToLocal(rep, value));
}
/** Creates a TypedValue from a value in local representation. */
public static TypedValue ofLocal(ColumnMetaData.Rep rep, Object value) {
return new TypedValue(rep, localToSerial(rep, value));
}
/** Creates a TypedValue from a value in serial representation. */
public static TypedValue ofSerial(ColumnMetaData.Rep rep, Object value) {
return new TypedValue(rep, value);
}
/** Creates a TypedValue from a value in JDBC representation. */
public static TypedValue ofJdbc(ColumnMetaData.Rep rep, Object value,
Calendar calendar) {
if (value == null) {
return EXPLICIT_NULL;
}
final Object serialValue;
if (ColumnMetaData.Rep.ARRAY == rep) {
// Sanity check that we were given an Array
if (null != value && !(value instanceof Array)) {
throw new IllegalArgumentException("Provided Rep was ARRAY, but the value was "
+ value.getClass());
}
final Array array = (Array) value;
try {
SqlType type = SqlType.valueOf(array.getBaseType());
serialValue = jdbcToSerial(rep, array, calendar, type);
// Because an Array may have null entries, we must always return the non-primitive type
// variants of the array values.
return new TypedValue(rep, Rep.nonPrimitiveRepOf(type), serialValue);
} catch (SQLException e) {
throw new RuntimeException("Could not extract Array com.hazelcast.com.onent type", e);
}
} else {
serialValue = jdbcToSerial(rep, value, calendar);
}
return new TypedValue(rep, serialValue);
}
/** Creates a TypedValue from a value in JDBC representation,
* deducing its type. */
public static TypedValue ofJdbc(Object value, Calendar calendar) {
if (value == null) {
return EXPLICIT_NULL;
}
final ColumnMetaData.Rep rep = ColumnMetaData.Rep.of(value.getClass());
return new TypedValue(rep, jdbcToSerial(rep, value, calendar));
}
/** Converts the value into the local representation.
*
*
For example, a byte string is represented as a {@link ByteString};
* a long is represented as a {@link Long} (not just some {@link Number}).
*/
public Object toLocal() {
if (value == null) {
return null;
}
return serialToLocal(type, value);
}
/** Converts a value to the exact type required for the given
* representation. */
private static Object serialToLocal(ColumnMetaData.Rep rep, Object value) {
assert value != null;
if (value.getClass() == rep.clazz) {
return value;
}
switch (rep) {
case BYTE:
return ((Number) value).byteValue();
case SHORT:
return ((Number) value).shortValue();
case INTEGER:
case JAVA_SQL_DATE:
case JAVA_SQL_TIME:
return ((Number) value).intValue();
case LONG:
case JAVA_UTIL_DATE:
case JAVA_SQL_TIMESTAMP:
return ((Number) value).longValue();
case FLOAT:
return ((Number) value).floatValue();
case DOUBLE:
return ((Number) value).doubleValue();
case NUMBER:
return value instanceof BigDecimal ? value
: value instanceof BigInteger ? new BigDecimal((BigInteger) value)
: value instanceof Double ? new BigDecimal((Double) value)
: value instanceof Float ? new BigDecimal((Float) value)
: new BigDecimal(((Number) value).longValue());
case BYTE_STRING:
return ByteString.ofBase64((String) value);
case ARRAY:
//List