|
| 1 | +/* |
| 2 | + Copyright (c) 2020, 2021, Oracle and/or its affiliates. |
| 3 | +
|
| 4 | + This software is dual-licensed to you under the Universal Permissive License |
| 5 | + (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License |
| 6 | + 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose |
| 7 | + either license. |
| 8 | +
|
| 9 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + you may not use this file except in compliance with the License. |
| 11 | + You may obtain a copy of the License at |
| 12 | +
|
| 13 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +
|
| 15 | + Unless required by applicable law or agreed to in writing, software |
| 16 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + See the License for the specific language governing permissions and |
| 19 | + limitations under the License. |
| 20 | +*/ |
| 21 | +package oracle.r2dbc; |
| 22 | + |
| 23 | +import io.r2dbc.spi.Type; |
| 24 | +import oracle.sql.json.OracleJsonObject; |
| 25 | + |
| 26 | +import java.nio.ByteBuffer; |
| 27 | +import java.sql.RowId; |
| 28 | +import java.time.Duration; |
| 29 | +import java.time.LocalDateTime; |
| 30 | +import java.time.Period; |
| 31 | + |
| 32 | +/** |
| 33 | + * SQL types supported by Oracle Database that are not defined as standard types |
| 34 | + * by {@link io.r2dbc.spi.R2dbcType}. |
| 35 | + */ |
| 36 | +public final class OracleR2dbcTypes { |
| 37 | + |
| 38 | + private OracleR2dbcTypes() {} |
| 39 | + |
| 40 | + /** |
| 41 | + * A 64-bit, double-precision floating-point number data type. |
| 42 | + */ |
| 43 | + public static final Type BINARY_DOUBLE = |
| 44 | + new TypeImpl(Double.class, "BINARY_DOUBLE"); |
| 45 | + |
| 46 | + /** |
| 47 | + * A 32-bit, single-precision floating-point number data type. |
| 48 | + */ |
| 49 | + public static final Type BINARY_FLOAT = |
| 50 | + new TypeImpl(Float.class, "BINARY_FLOAT"); |
| 51 | + |
| 52 | + /** |
| 53 | + * A Binary Large Object (BLOB) as implemented by Oracle Database. The default |
| 54 | + * Java type mapping is {@link io.r2dbc.spi.Blob} rather than |
| 55 | + * {@link java.nio.ByteBuffer}, which is the mapping of the standard |
| 56 | + * {@link io.r2dbc.spi.R2dbcType#BLOB}. |
| 57 | + */ |
| 58 | + public static final Type BLOB = |
| 59 | + new TypeImpl(io.r2dbc.spi.Blob.class, "BLOB"); |
| 60 | + |
| 61 | + /** |
| 62 | + * A Character Large Object (BLOB) as implemented by Oracle Database. The |
| 63 | + * default Java type mapping is {@link io.r2dbc.spi.Clob} rather than |
| 64 | + * {@link String}, which is the mapping of the standard |
| 65 | + * {@link io.r2dbc.spi.R2dbcType#CLOB}. |
| 66 | + */ |
| 67 | + public static final Type CLOB = |
| 68 | + new TypeImpl(io.r2dbc.spi.Clob.class, "CLOB"); |
| 69 | + |
| 70 | + /** |
| 71 | + * Stores a period of time in days, hours, minutes, and seconds. |
| 72 | + */ |
| 73 | + public static final Type INTERVAL_DAY_TO_SECOND = |
| 74 | + new TypeImpl(Duration.class, "INTERVAL DAY TO SECOND"); |
| 75 | + |
| 76 | + /** |
| 77 | + * Stores a period of time in years and months. |
| 78 | + */ |
| 79 | + public static final Type INTERVAL_YEAR_TO_MONTH = |
| 80 | + new TypeImpl(Period.class, "INTERVAL YEAR TO MONTH"); |
| 81 | + |
| 82 | + public static final Type JSON = |
| 83 | + new TypeImpl(OracleJsonObject.class, "JSON"); |
| 84 | + |
| 85 | + /** |
| 86 | + * Character data of variable length up to 2 gigabytes. |
| 87 | + */ |
| 88 | + public static final Type LONG = |
| 89 | + new TypeImpl(String.class, "LONG"); |
| 90 | + |
| 91 | + /** |
| 92 | + * Raw binary data of variable length up to 2 gigabytes. |
| 93 | + */ |
| 94 | + public static final Type LONG_RAW = |
| 95 | + new TypeImpl(ByteBuffer.class, "LONG RAW"); |
| 96 | + |
| 97 | + /** |
| 98 | + * A National Character Large Object (NCLOB) as implemented by Oracle |
| 99 | + * Database. The default Java type mapping is {@link io.r2dbc.spi.Clob} |
| 100 | + * rather than {@link String}, which is the mapping of the standard |
| 101 | + * {@link io.r2dbc.spi.R2dbcType#NCLOB}. |
| 102 | + */ |
| 103 | + public static final Type NCLOB = |
| 104 | + new TypeImpl(io.r2dbc.spi.Clob.class, "NCLOB"); |
| 105 | + |
| 106 | + /** |
| 107 | + * Base 64 string representing the unique address of a row in its table. |
| 108 | + */ |
| 109 | + public static final Type ROWID = |
| 110 | + new TypeImpl(RowId.class, "ROWID"); |
| 111 | + |
| 112 | + /** |
| 113 | + * Timestamp that is converted to the database's timezone when stored, and |
| 114 | + * converted to the local timezone (the session timezone) when retrieved. |
| 115 | + */ |
| 116 | + public static final Type TIMESTAMP_WITH_LOCAL_TIME_ZONE = |
| 117 | + new TypeImpl(LocalDateTime.class, "TIMESTAMP WITH LOCAL TIME ZONE"); |
| 118 | + |
| 119 | + /** |
| 120 | + * Implementation of the {@link Type} SPI. |
| 121 | + */ |
| 122 | + private static final class TypeImpl implements Type { |
| 123 | + |
| 124 | + /** |
| 125 | + * The Java Language mapping of this SQL type. |
| 126 | + */ |
| 127 | + private final Class<?> javaType; |
| 128 | + |
| 129 | + /** |
| 130 | + * The name of this SQL type, as it would appear in a DDL expression. |
| 131 | + */ |
| 132 | + private final String sqlName; |
| 133 | + |
| 134 | + /** |
| 135 | + * Constructs a {@code Type} having a {@code javaType} mapping and |
| 136 | + * {@code sqlName}. |
| 137 | + * @param javaType Java type |
| 138 | + * @param sqlName SQL type name |
| 139 | + */ |
| 140 | + TypeImpl(Class<?> javaType, String sqlName) { |
| 141 | + this.javaType = javaType; |
| 142 | + this.sqlName = sqlName; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * {@inheritDoc} |
| 147 | + * <p> |
| 148 | + * Implements the R2DBC SPI method by returning the default Java type |
| 149 | + * mapping for values of this SQL type. The Java type returned by this |
| 150 | + * method is the type of {@code Object} returned by {@code Row.get |
| 151 | + * (String/int)} when accessing a value of this SQL type. |
| 152 | + * </p> |
| 153 | + */ |
| 154 | + @Override |
| 155 | + public Class<?> getJavaType() { |
| 156 | + return javaType; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * {@inheritDoc} |
| 161 | + * <p> |
| 162 | + * Implements the R2DBC SPI method by returning the name of this SQL type. |
| 163 | + * The name returned by this method is recognized in expressions of a SQL |
| 164 | + * command, for instance: A column definition of a {@code CREATE TABLE} |
| 165 | + * command. |
| 166 | + * </p> |
| 167 | + * |
| 168 | + * @return |
| 169 | + */ |
| 170 | + @Override |
| 171 | + public String getName() { |
| 172 | + return sqlName; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Returns the name of this type. |
| 177 | + * @return Type name |
| 178 | + */ |
| 179 | + @Override |
| 180 | + public String toString() { |
| 181 | + return getName(); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments