|
| 1 | +// |
| 2 | +// SQLAny.swift |
| 3 | +// Feather |
| 4 | +// |
| 5 | +// Created by Wes Wickwire on 5/10/25. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import SQLite3 |
| 10 | + |
| 11 | +/// SQLite supports an `ANY` type. Mapping to a `Swift.Any` would |
| 12 | +/// not be a smart idea and would not implement things like |
| 13 | +/// `Hashable` and `Sendable`. This wraps any possible value that |
| 14 | +/// SQLite can throw at us. |
| 15 | +/// |
| 16 | +/// SQLite type can be `NULL` but that is not acknowledged here |
| 17 | +/// and nullability is handled at the constraint level like |
| 18 | +/// every other type. |
| 19 | +public enum SQLAny: Sendable, Hashable { |
| 20 | + /// Maps to `TEXT` |
| 21 | + case string(String) |
| 22 | + /// Maps to `INTEGER` and `INT` |
| 23 | + case int(Int) |
| 24 | + /// Maps to `REAL` |
| 25 | + case double(Double) |
| 26 | + /// Maps to `BLOB` |
| 27 | + case data(Data) |
| 28 | + |
| 29 | + /// The value if it is a `.string` |
| 30 | + public var string: String? { |
| 31 | + guard case let .string(value) = self else { return nil } |
| 32 | + return value |
| 33 | + } |
| 34 | + |
| 35 | + /// The value if it is a `.int` |
| 36 | + public var int: Int? { |
| 37 | + guard case let .int(value) = self else { return nil } |
| 38 | + return value |
| 39 | + } |
| 40 | + |
| 41 | + /// The value if it is a `.double` |
| 42 | + public var double: Double? { |
| 43 | + guard case let .double(value) = self else { return nil } |
| 44 | + return value |
| 45 | + } |
| 46 | + |
| 47 | + /// The value if it is a `.data` |
| 48 | + public var data: Data? { |
| 49 | + guard case let .data(value) = self else { return nil } |
| 50 | + return value |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +extension SQLAny: CustomStringConvertible { |
| 55 | + public var description: String { |
| 56 | + switch self { |
| 57 | + case .string(let string): string |
| 58 | + case .int(let int): int.description |
| 59 | + case .double(let double): double.description |
| 60 | + case .data(let data): data.description |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +extension SQLAny: ExpressibleByStringLiteral { |
| 66 | + public init(stringLiteral value: StringLiteralType) { |
| 67 | + self = .string(value) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +extension SQLAny: ExpressibleByIntegerLiteral { |
| 72 | + public init(integerLiteral value: IntegerLiteralType) { |
| 73 | + self = .int(value) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +extension SQLAny: ExpressibleByFloatLiteral { |
| 78 | + public init(floatLiteral value: FloatLiteralType) { |
| 79 | + self = .double(value) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +extension SQLAny: DatabasePrimitive { |
| 84 | + @inlinable public init(from cursor: OpaquePointer, at index: Int32) throws(FeatherError) { |
| 85 | + let type = sqlite3_column_type(cursor, index) |
| 86 | + switch type { |
| 87 | + case SQLITE_TEXT: self = try .string(String(from: cursor, at: index)) |
| 88 | + case SQLITE_INTEGER: self = try .int(Int(from: cursor, at: index)) |
| 89 | + case SQLITE_FLOAT: self = try .double(Double(from: cursor, at: index)) |
| 90 | + case SQLITE_BLOB: self = try .data(Data(from: cursor, at: index)) |
| 91 | + case SQLITE_NULL: throw .decodingError("Expected non nil value for `ANY` column type") |
| 92 | + default: fatalError("Unknown column type code: \(type)") |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @inlinable public func bind(to statement: OpaquePointer, at index: Int32) throws(FeatherError) { |
| 97 | + switch self { |
| 98 | + case .string(let string): try string.bind(to: statement, at: index) |
| 99 | + case .int(let int): try int.bind(to: statement, at: index) |
| 100 | + case .double(let double): try double.bind(to: statement, at: index) |
| 101 | + case .data(let data): try data.bind(to: statement, at: index) |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments