-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathSQLiteRow+Database.swift
79 lines (66 loc) · 2.6 KB
/
SQLiteRow+Database.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import Foundation
import SQLiteNIO
import SQLiteKit
import FluentKit
extension SQLRow {
/// Returns a `DatabaseOutput` for this row.
///
/// - Returns: A `DatabaseOutput` instance.
func databaseOutput() -> some DatabaseOutput {
SQLRowDatabaseOutput(row: self, schema: nil)
}
}
/// A `DatabaseOutput` implementation for generic `SQLRow`s. This should really be in FluentSQL.
private struct SQLRowDatabaseOutput: DatabaseOutput {
/// The underlying row.
let row: any SQLRow
/// The most recently set schema value (see `DatabaseOutput.schema(_:)`).
let schema: String?
// See `CustomStringConvertible.description`.
var description: String {
String(describing: self.row)
}
/// Apply the current schema (if any) to the given `FieldKey` and convert to a column name.
private func adjust(key: FieldKey) -> String {
(self.schema.map { .prefix(.prefix(.string($0), "_"), key) } ?? key).description
}
// See `DatabaseOutput.schema(_:)`.
func schema(_ schema: String) -> any DatabaseOutput {
SQLRowDatabaseOutput(row: self.row, schema: schema)
}
// See `DatabaseOutput.contains(_:)`.
func contains(_ key: FieldKey) -> Bool {
self.row.contains(column: self.adjust(key: key))
}
// See `DatabaseOutput.decodeNil(_:)`.
func decodeNil(_ key: FieldKey) throws -> Bool {
try self.row.decodeNil(column: self.adjust(key: key))
}
// See `DatabaseOutput.decode(_:as:)`.
func decode<T: Decodable>(_ key: FieldKey, as: T.Type) throws -> T {
try self.row.decode(column: self.adjust(key: key), as: T.self)
}
}
/// A legacy deprecated conformance of `SQLiteRow` directly to `DatabaseOutput`. This interface exists solely
/// because its absence would be a public API break.
///
/// Do not use these methods.
@available(*, deprecated, message: "Do not use this conformance.")
extension SQLiteNIO.SQLiteRow: FluentKit.DatabaseOutput {
// See `DatabaseOutput.schema(_:)`.
public func schema(_ schema: String) -> any DatabaseOutput {
self.databaseOutput().schema(schema)
}
// See `DatabaseOutput.contains(_:)`.
public func contains(_ key: FieldKey) -> Bool {
self.databaseOutput().contains(key)
}
// See `DatabaseOutput.decodeNil(_:)`.
public func decodeNil(_ key: FieldKey) throws -> Bool {
try self.databaseOutput().decodeNil(key)
}
// See `DatabaseOutput.decode(_:as:)`.
public func decode<T: Decodable>(_ key: FieldKey, as: T.Type) throws -> T {
try self.databaseOutput().decode(key, as: T.self)
}
}