-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgresDatabaseQuery.swift
More file actions
159 lines (135 loc) · 4.08 KB
/
PostgresDatabaseQuery.swift
File metadata and controls
159 lines (135 loc) · 4.08 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// PostgresDatabaseQuery.swift
// feather-postgres-database
//
// Created by Tibor Bödecs on 2026. 01. 10.
//
import FeatherDatabase
import PostgresNIO
public struct PostgresDatabaseQuery: DatabaseQuery {
/// The bindings type for Postgres queries.
///
/// This type represents parameter bindings for PostgresNIO.
public typealias Bindings = PostgresBindings
/// The SQL text to execute.
///
/// This is the raw SQL string for the query.
public var sql: String {
query.sql
}
/// The bound parameters for the SQL text.
///
/// This exposes the underlying `binds` storage.
public var bindings: PostgresBindings {
query.binds
}
var query: PostgresQuery
}
extension PostgresDatabaseQuery: ExpressibleByStringInterpolation {
public init(
stringLiteral value: String
) {
self.init(query: .init(stringLiteral: value))
}
public init(
stringInterpolation value: StringInterpolation
) {
self.init(
query: .init(
unsafeSQL: value.sql,
binds: value.binds
)
)
}
}
extension PostgresDatabaseQuery {
// NOTE: source derived from postgres-nio
public struct StringInterpolation: StringInterpolationProtocol, Sendable {
public typealias StringLiteralType = String
@usableFromInline
var sql: String
@usableFromInline
var binds: PostgresBindings
public init(
literalCapacity: Int,
interpolationCount: Int
) {
self.sql = ""
self.binds = PostgresBindings(capacity: interpolationCount)
}
public mutating func appendLiteral(
_ literal: String
) {
self.sql.append(contentsOf: literal)
}
@inlinable
public mutating func appendInterpolation<
Value: PostgresThrowingDynamicTypeEncodable
>(
_ value: Value
) throws {
try self.binds.append(value, context: .default)
self.sql.append(contentsOf: "$\(self.binds.count)")
}
@inlinable
public mutating func appendInterpolation<
Value: PostgresThrowingDynamicTypeEncodable
>(
_ value: Value?
) throws {
switch value {
case .none:
self.binds.appendNull()
case .some(let value):
try self.binds.append(value, context: .default)
}
self.sql.append(contentsOf: "$\(self.binds.count)")
}
@inlinable
public mutating func appendInterpolation<
Value: PostgresDynamicTypeEncodable
>(
_ value: Value
) {
self.binds.append(value, context: .default)
self.sql.append(contentsOf: "$\(self.binds.count)")
}
@inlinable
public mutating func appendInterpolation<
Value: PostgresDynamicTypeEncodable
>(
_ value: Value?
) {
switch value {
case .none:
self.binds.appendNull()
case .some(let value):
self.binds.append(value, context: .default)
}
self.sql.append(contentsOf: "$\(self.binds.count)")
}
@inlinable
public mutating func appendInterpolation<
Value: PostgresThrowingDynamicTypeEncodable,
JSONEncoder: PostgresJSONEncoder
>(
_ value: Value,
context: PostgresEncodingContext<JSONEncoder>
) throws {
try self.binds.append(value, context: context)
self.sql.append(contentsOf: "$\(self.binds.count)")
}
@inlinable
public mutating func appendInterpolation(
unescaped interpolated: String
) {
self.sql.append(contentsOf: interpolated)
}
@inlinable
public mutating func appendInterpolation(
unescaped interpolated: Int
) {
self.sql.append(contentsOf: String(interpolated))
}
}
}