|
| 1 | +/* |
| 2 | + * Copyright 2025 SOFTNETWORK |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package app.softnetwork.elastic.sql.`type` |
| 18 | + |
| 19 | +/** Runtime value type inference and coercion utilities. |
| 20 | + * |
| 21 | + * These functions operate on plain Scala/Java runtime values (not Painless scripts) and are |
| 22 | + * independent of any JDBC or Arrow-specific API. Both the JDBC driver and the Arrow Flight SQL |
| 23 | + * server delegate to this object for type inference and value coercion. |
| 24 | + * |
| 25 | + * JDBC-specific mappings (`toJdbcType`, `toJdbcTypeName`, `coerceValue`, etc.) remain in the |
| 26 | + * `driver` module's `TypeMapping` object, which delegates here for the general-purpose methods. |
| 27 | + */ |
| 28 | +object ValueCoercion { |
| 29 | + |
| 30 | + // ─── Type inference ───────────────────────────────────────────────────────── |
| 31 | + |
| 32 | + /** Infer the [[SQLType]] from a runtime value. */ |
| 33 | + def inferType(value: Any): SQLType = value match { |
| 34 | + case null => SQLTypes.Null |
| 35 | + case _: Int => SQLTypes.Int |
| 36 | + case _: Long => SQLTypes.BigInt |
| 37 | + case _: Double => SQLTypes.Double |
| 38 | + case _: Float => SQLTypes.Real |
| 39 | + case _: Boolean => SQLTypes.Boolean |
| 40 | + case _: Short => SQLTypes.SmallInt |
| 41 | + case _: Byte => SQLTypes.TinyInt |
| 42 | + case _: java.math.BigDecimal => SQLTypes.Double |
| 43 | + case _: BigDecimal => SQLTypes.Double |
| 44 | + case _: java.sql.Date => SQLTypes.Date |
| 45 | + case _: java.sql.Time => SQLTypes.Time |
| 46 | + case _: java.sql.Timestamp => SQLTypes.Timestamp |
| 47 | + case _: java.time.LocalDate => SQLTypes.Date |
| 48 | + case _: java.time.LocalTime => SQLTypes.Time |
| 49 | + case _: java.time.LocalDateTime => SQLTypes.Timestamp |
| 50 | + case _: java.time.Instant => SQLTypes.Timestamp |
| 51 | + case _: java.time.ZonedDateTime => SQLTypes.Timestamp |
| 52 | + case _: java.time.temporal.TemporalAccessor => SQLTypes.Timestamp |
| 53 | + case _: Seq[_] => SQLTypes.Array(SQLTypes.Any) |
| 54 | + case _: Map[_, _] => SQLTypes.Struct |
| 55 | + case _: Array[Byte] => SQLTypes.VarBinary |
| 56 | + case _: String => SQLTypes.Varchar |
| 57 | + case _: Number => SQLTypes.Double |
| 58 | + case _ => SQLTypes.Varchar |
| 59 | + } |
| 60 | + |
| 61 | + // ─── Coercions ─────────────────────────────────────────────────────────────── |
| 62 | + |
| 63 | + def coerceToString(value: Any): String = value match { |
| 64 | + case null => null |
| 65 | + case s: String => s |
| 66 | + case seq: Seq[_] => seq.mkString("[", ", ", "]") |
| 67 | + case map: Map[_, _] => map.map { case (k, v) => s"$k: $v" }.mkString("{", ", ", "}") |
| 68 | + case other => other.toString |
| 69 | + } |
| 70 | + |
| 71 | + def coerceToInt(value: Any): java.lang.Integer = value match { |
| 72 | + case null => null |
| 73 | + case n: Number => n.intValue() |
| 74 | + case s: String => java.lang.Integer.valueOf(s) |
| 75 | + case b: Boolean => if (b) 1 else 0 |
| 76 | + case _ => |
| 77 | + throw new java.sql.SQLException(s"Cannot convert ${value.getClass.getName} to INT") |
| 78 | + } |
| 79 | + |
| 80 | + def coerceToLong(value: Any): java.lang.Long = value match { |
| 81 | + case null => null |
| 82 | + case n: Number => n.longValue() |
| 83 | + case s: String => java.lang.Long.valueOf(s) |
| 84 | + case b: Boolean => if (b) 1L else 0L |
| 85 | + case _ => |
| 86 | + throw new java.sql.SQLException(s"Cannot convert ${value.getClass.getName} to BIGINT") |
| 87 | + } |
| 88 | + |
| 89 | + def coerceToDouble(value: Any): java.lang.Double = value match { |
| 90 | + case null => null |
| 91 | + case n: Number => n.doubleValue() |
| 92 | + case s: String => java.lang.Double.valueOf(s) |
| 93 | + case _ => |
| 94 | + throw new java.sql.SQLException(s"Cannot convert ${value.getClass.getName} to DOUBLE") |
| 95 | + } |
| 96 | + |
| 97 | + def coerceToFloat(value: Any): java.lang.Float = value match { |
| 98 | + case null => null |
| 99 | + case n: Number => n.floatValue() |
| 100 | + case s: String => java.lang.Float.valueOf(s) |
| 101 | + case _ => |
| 102 | + throw new java.sql.SQLException(s"Cannot convert ${value.getClass.getName} to REAL") |
| 103 | + } |
| 104 | + |
| 105 | + def coerceToBoolean(value: Any): java.lang.Boolean = value match { |
| 106 | + case null => null |
| 107 | + case b: Boolean => b |
| 108 | + case n: Number => n.intValue() != 0 |
| 109 | + case s: String => java.lang.Boolean.valueOf(s) |
| 110 | + case _ => |
| 111 | + throw new java.sql.SQLException(s"Cannot convert ${value.getClass.getName} to BOOLEAN") |
| 112 | + } |
| 113 | + |
| 114 | + def coerceToByte(value: Any): java.lang.Byte = value match { |
| 115 | + case null => null |
| 116 | + case n: Number => n.byteValue() |
| 117 | + case s: String => java.lang.Byte.valueOf(s) |
| 118 | + case _ => |
| 119 | + throw new java.sql.SQLException(s"Cannot convert ${value.getClass.getName} to TINYINT") |
| 120 | + } |
| 121 | + |
| 122 | + def coerceToShort(value: Any): java.lang.Short = value match { |
| 123 | + case null => null |
| 124 | + case n: Number => n.shortValue() |
| 125 | + case s: String => java.lang.Short.valueOf(s) |
| 126 | + case _ => |
| 127 | + throw new java.sql.SQLException(s"Cannot convert ${value.getClass.getName} to SMALLINT") |
| 128 | + } |
| 129 | + |
| 130 | + def coerceToBigDecimal(value: Any): java.math.BigDecimal = value match { |
| 131 | + case null => null |
| 132 | + case bd: java.math.BigDecimal => bd |
| 133 | + case bd: BigDecimal => bd.bigDecimal |
| 134 | + case n: Number => java.math.BigDecimal.valueOf(n.doubleValue()) |
| 135 | + case s: String => new java.math.BigDecimal(s) |
| 136 | + case _ => |
| 137 | + throw new java.sql.SQLException(s"Cannot convert ${value.getClass.getName} to DECIMAL") |
| 138 | + } |
| 139 | + |
| 140 | + def coerceToDate(value: Any): java.sql.Date = value match { |
| 141 | + case null => null |
| 142 | + case d: java.sql.Date => d |
| 143 | + case ts: java.sql.Timestamp => new java.sql.Date(ts.getTime) |
| 144 | + case ld: java.time.LocalDate => java.sql.Date.valueOf(ld) |
| 145 | + case ldt: java.time.LocalDateTime => java.sql.Date.valueOf(ldt.toLocalDate) |
| 146 | + case zdt: java.time.ZonedDateTime => java.sql.Date.valueOf(zdt.toLocalDate) |
| 147 | + case i: java.time.Instant => new java.sql.Date(i.toEpochMilli) |
| 148 | + case t: java.time.temporal.TemporalAccessor => |
| 149 | + try { |
| 150 | + java.sql.Date.valueOf(java.time.LocalDate.from(t)) |
| 151 | + } catch { |
| 152 | + case _: Exception => throw new java.sql.SQLException("Cannot convert temporal to DATE") |
| 153 | + } |
| 154 | + case s: String => |
| 155 | + try { java.sql.Date.valueOf(s) } |
| 156 | + catch { case _: Exception => throw new java.sql.SQLException(s"Cannot parse '$s' as DATE") } |
| 157 | + case _ => |
| 158 | + throw new java.sql.SQLException(s"Cannot convert ${value.getClass.getName} to DATE") |
| 159 | + } |
| 160 | + |
| 161 | + def coerceToTime(value: Any): java.sql.Time = value match { |
| 162 | + case null => null |
| 163 | + case t: java.sql.Time => t |
| 164 | + case lt: java.time.LocalTime => java.sql.Time.valueOf(lt) |
| 165 | + case ldt: java.time.LocalDateTime => java.sql.Time.valueOf(ldt.toLocalTime) |
| 166 | + case s: String => |
| 167 | + try { java.sql.Time.valueOf(s) } |
| 168 | + catch { case _: Exception => throw new java.sql.SQLException(s"Cannot parse '$s' as TIME") } |
| 169 | + case _ => |
| 170 | + throw new java.sql.SQLException(s"Cannot convert ${value.getClass.getName} to TIME") |
| 171 | + } |
| 172 | + |
| 173 | + def coerceToTimestamp(value: Any): java.sql.Timestamp = value match { |
| 174 | + case null => null |
| 175 | + case ts: java.sql.Timestamp => ts |
| 176 | + case d: java.sql.Date => new java.sql.Timestamp(d.getTime) |
| 177 | + case i: java.time.Instant => java.sql.Timestamp.from(i) |
| 178 | + case ldt: java.time.LocalDateTime => java.sql.Timestamp.valueOf(ldt) |
| 179 | + case zdt: java.time.ZonedDateTime => java.sql.Timestamp.from(zdt.toInstant) |
| 180 | + case ld: java.time.LocalDate => java.sql.Timestamp.valueOf(ld.atStartOfDay()) |
| 181 | + case t: java.time.temporal.TemporalAccessor => |
| 182 | + try { |
| 183 | + java.sql.Timestamp.from(java.time.Instant.from(t)) |
| 184 | + } catch { |
| 185 | + case _: Exception => |
| 186 | + try { |
| 187 | + java.sql.Timestamp.valueOf(java.time.LocalDateTime.from(t)) |
| 188 | + } catch { |
| 189 | + case _: Exception => |
| 190 | + throw new java.sql.SQLException("Cannot convert temporal to TIMESTAMP") |
| 191 | + } |
| 192 | + } |
| 193 | + case s: String => |
| 194 | + try { java.sql.Timestamp.valueOf(s) } |
| 195 | + catch { |
| 196 | + case _: Exception => |
| 197 | + try { java.sql.Timestamp.from(java.time.Instant.parse(s)) } |
| 198 | + catch { |
| 199 | + case _: Exception => |
| 200 | + throw new java.sql.SQLException(s"Cannot parse '$s' as TIMESTAMP") |
| 201 | + } |
| 202 | + } |
| 203 | + case n: Number => new java.sql.Timestamp(n.longValue()) |
| 204 | + case _ => |
| 205 | + throw new java.sql.SQLException(s"Cannot convert ${value.getClass.getName} to TIMESTAMP") |
| 206 | + } |
| 207 | +} |
0 commit comments