Skip to content

Commit 598bc21

Browse files
authored
merge postgres-kit (#108)
1 parent da340d8 commit 598bc21

File tree

11 files changed

+1824
-18
lines changed

11 files changed

+1824
-18
lines changed

Package.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,22 @@ let package = Package(
88
],
99
dependencies: [
1010
.package(url: "https://github.com/vapor/fluent-kit.git", .branch("master")),
11-
.package(url: "https://github.com/vapor/postgresql.git", .branch("master")),
11+
.package(url: "https://github.com/vapor/nio-postgres.git", .branch("master")),
12+
.package(url: "https://github.com/vapor/sql.git", .branch("master")),
13+
.package(url: "https://github.com/vapor/nio-kit.git", .branch("master")),
1214
],
1315
targets: [
14-
.target(name: "FluentPostgresDriver", dependencies: ["FluentKit", "FluentSQL", "PostgresKit"]),
15-
.testTarget(name: "FluentPostgresDriverTests", dependencies: ["FluentBenchmark", "FluentPostgresDriver"]),
16+
.target(name: "FluentPostgresDriver", dependencies: [
17+
"FluentKit",
18+
"FluentSQL",
19+
"NIOKit",
20+
"NIOPostgres",
21+
"SQLKit"
22+
]),
23+
.testTarget(name: "FluentPostgresDriverTests", dependencies: [
24+
"FluentBenchmark",
25+
"FluentPostgresDriver",
26+
"SQLKitBenchmark"
27+
]),
1628
]
1729
)
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
@_exported import FluentKit
2-
@_exported import PostgresKit
2+
@_exported import SQLKit
3+
@_exported import NIOKit
4+
@_exported import NIOPostgres

Sources/FluentPostgresDriver/Postgres+Fluent.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ extension ConnectionPool: Database where Source.Connection: Database {
3535
return self.withConnection { $0.execute(query, onOutput) }
3636
}
3737

38-
public func close() -> EventLoopFuture<Void> {
39-
#warning("TODO: implement connectionPool.close()")
40-
fatalError("")
41-
}
42-
4338
public func transaction<T>(_ closure: @escaping (Database) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
4439
return self.withConnection { conn in
4540
return closure(conn)
@@ -84,11 +79,6 @@ extension PostgresConnection: Database {
8479
fatalError("unexpected output")
8580
}
8681
}
87-
88-
public func close() -> EventLoopFuture<Void> {
89-
#warning("TODO: implement connectionPool.close()")
90-
fatalError("")
91-
}
9282
}
9383

9484
private struct PostgresReturning: SQLExpression {
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import Foundation
2+
3+
#warning("TODO: move to codable kit")
4+
struct DecoderUnwrapper: Decodable {
5+
let decoder: Decoder
6+
init(from decoder: Decoder) {
7+
self.decoder = decoder
8+
}
9+
}
10+
11+
public struct PostgresDataDecoder {
12+
public init() {}
13+
14+
public func decode<T>(_ type: T.Type, from data: PostgresData) throws -> T
15+
where T: Decodable
16+
{
17+
return try T.init(from: _Decoder(data: data))
18+
}
19+
20+
#warning("TODO: finish implementing")
21+
22+
private final class _Decoder: Decoder {
23+
var codingPath: [CodingKey] {
24+
return []
25+
}
26+
27+
var userInfo: [CodingUserInfoKey : Any] {
28+
return [:]
29+
}
30+
31+
let data: PostgresData
32+
init(data: PostgresData) {
33+
self.data = data
34+
}
35+
36+
func unkeyedContainer() throws -> UnkeyedDecodingContainer {
37+
fatalError()
38+
}
39+
40+
func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key : CodingKey {
41+
#warning("TODO: use NIOFoundationCompat")
42+
var buffer = self.data.value!
43+
let data = buffer.readBytes(length: buffer.readableBytes)!
44+
let unwrapper = try JSONDecoder().decode(DecoderUnwrapper.self, from: Data(data))
45+
return try unwrapper.decoder.container(keyedBy: Key.self)
46+
}
47+
48+
func singleValueContainer() throws -> SingleValueDecodingContainer {
49+
return _SingleValueDecoder(self)
50+
}
51+
}
52+
53+
private struct _SingleValueDecoder: SingleValueDecodingContainer {
54+
var codingPath: [CodingKey] {
55+
return self.decoder.codingPath
56+
}
57+
let decoder: _Decoder
58+
init(_ decoder: _Decoder) {
59+
self.decoder = decoder
60+
}
61+
62+
func decodeNil() -> Bool {
63+
return self.decoder.data.value == nil
64+
}
65+
66+
func decode(_ type: Bool.Type) throws -> Bool {
67+
fatalError()
68+
}
69+
70+
func decode(_ type: String.Type) throws -> String {
71+
return self.decoder.data.string!
72+
}
73+
74+
func decode(_ type: Double.Type) throws -> Double {
75+
return self.decoder.data.double!
76+
}
77+
78+
func decode(_ type: Float.Type) throws -> Float {
79+
return self.decoder.data.float!
80+
}
81+
82+
func decode(_ type: Int.Type) throws -> Int {
83+
return self.decoder.data.int!
84+
}
85+
86+
func decode(_ type: Int8.Type) throws -> Int8 {
87+
fatalError()
88+
}
89+
90+
func decode(_ type: Int16.Type) throws -> Int16 {
91+
fatalError()
92+
}
93+
94+
func decode(_ type: Int32.Type) throws -> Int32 {
95+
fatalError()
96+
}
97+
98+
func decode(_ type: Int64.Type) throws -> Int64 {
99+
return self.decoder.data.int64!
100+
}
101+
102+
func decode(_ type: UInt.Type) throws -> UInt {
103+
fatalError()
104+
}
105+
106+
func decode(_ type: UInt8.Type) throws -> UInt8 {
107+
fatalError()
108+
}
109+
110+
func decode(_ type: UInt16.Type) throws -> UInt16 {
111+
fatalError()
112+
}
113+
114+
func decode(_ type: UInt32.Type) throws -> UInt32 {
115+
fatalError()
116+
}
117+
118+
func decode(_ type: UInt64.Type) throws -> UInt64 {
119+
fatalError()
120+
}
121+
122+
func decode<T>(_ type: T.Type) throws -> T where T : Decodable {
123+
return try T.init(from: self.decoder)
124+
}
125+
126+
}
127+
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
import Foundation
2+
3+
public struct PostgresDataEncoder {
4+
public init() { }
5+
6+
public func encode(_ type: Encodable) throws -> PostgresData {
7+
if let custom = type as? PostgresDataCustomConvertible {
8+
return custom.postgresData ?? .null
9+
} else {
10+
do {
11+
let encoder = _Encoder()
12+
try type.encode(to: encoder)
13+
return encoder.data
14+
} catch is DoJSON {
15+
let json = JSONEncoder()
16+
let data = try json.encode(Wrapper(type))
17+
var buffer = ByteBufferAllocator().buffer(capacity: data.count)
18+
#warning("TODO: use nio foundation compat write")
19+
buffer.writeBytes(data)
20+
return PostgresData(type: .jsonb, value: buffer)
21+
}
22+
}
23+
}
24+
25+
private final class _Encoder: Encoder {
26+
var codingPath: [CodingKey] {
27+
return []
28+
}
29+
30+
var userInfo: [CodingUserInfoKey : Any] {
31+
return [:]
32+
}
33+
var data: PostgresData
34+
init() {
35+
self.data = .null
36+
}
37+
38+
func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key : CodingKey {
39+
return .init(_KeyedValueEncoder(self))
40+
}
41+
42+
func unkeyedContainer() -> UnkeyedEncodingContainer {
43+
fatalError()
44+
}
45+
46+
func singleValueContainer() -> SingleValueEncodingContainer {
47+
return _SingleValueEncoder(self)
48+
}
49+
}
50+
51+
#warning("TODO: fix fatal errors")
52+
53+
struct DoJSON: Error {}
54+
55+
#warning("TODO: move to encodable kit")
56+
struct Wrapper: Encodable {
57+
let encodable: Encodable
58+
init(_ encodable: Encodable) {
59+
self.encodable = encodable
60+
}
61+
func encode(to encoder: Encoder) throws {
62+
try self.encodable.encode(to: encoder)
63+
}
64+
}
65+
66+
private struct _KeyedValueEncoder<Key>: KeyedEncodingContainerProtocol where Key: CodingKey {
67+
var codingPath: [CodingKey] {
68+
return self.encoder.codingPath
69+
}
70+
71+
let encoder: _Encoder
72+
init(_ encoder: _Encoder) {
73+
self.encoder = encoder
74+
}
75+
76+
mutating func encodeNil(forKey key: Key) throws {
77+
fatalError()
78+
}
79+
80+
mutating func encode<T>(_ value: T, forKey key: Key) throws where T : Encodable {
81+
throw DoJSON()
82+
}
83+
84+
mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> where NestedKey : CodingKey {
85+
fatalError()
86+
}
87+
88+
mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
89+
fatalError()
90+
}
91+
92+
mutating func superEncoder() -> Encoder {
93+
fatalError()
94+
}
95+
96+
mutating func superEncoder(forKey key: Key) -> Encoder {
97+
fatalError()
98+
}
99+
}
100+
101+
102+
private struct _SingleValueEncoder: SingleValueEncodingContainer {
103+
var codingPath: [CodingKey] {
104+
return self.encoder.codingPath
105+
}
106+
107+
let encoder: _Encoder
108+
init(_ encoder: _Encoder) {
109+
self.encoder = encoder
110+
}
111+
112+
mutating func encodeNil() throws {
113+
// data already null
114+
}
115+
116+
mutating func encode(_ value: Bool) throws {
117+
switch value {
118+
case true:
119+
self.encoder.data = PostgresData(int: 1)
120+
case false:
121+
self.encoder.data = PostgresData(int: 0)
122+
}
123+
}
124+
125+
mutating func encode(_ value: String) throws {
126+
self.encoder.data = PostgresData(string: value)
127+
}
128+
129+
mutating func encode(_ value: Double) throws {
130+
self.encoder.data = PostgresData(double: value)
131+
}
132+
133+
mutating func encode(_ value: Float) throws {
134+
self.encoder.data = PostgresData(float: value)
135+
}
136+
137+
mutating func encode(_ value: Int) throws {
138+
self.encoder.data = PostgresData(int: value)
139+
}
140+
141+
mutating func encode(_ value: Int8) throws {
142+
fatalError()
143+
}
144+
145+
mutating func encode(_ value: Int16) throws {
146+
fatalError()
147+
}
148+
149+
mutating func encode(_ value: Int32) throws {
150+
fatalError()
151+
}
152+
153+
mutating func encode(_ value: Int64) throws {
154+
self.encoder.data = PostgresData(int64: value)
155+
}
156+
157+
mutating func encode(_ value: UInt) throws {
158+
fatalError()
159+
}
160+
161+
mutating func encode(_ value: UInt8) throws {
162+
fatalError()
163+
}
164+
165+
mutating func encode(_ value: UInt16) throws {
166+
fatalError()
167+
}
168+
169+
mutating func encode(_ value: UInt32) throws {
170+
fatalError()
171+
}
172+
173+
mutating func encode(_ value: UInt64) throws {
174+
fatalError()
175+
}
176+
177+
mutating func encode<T>(_ value: T) throws where T : Encodable {
178+
try value.encode(to: self.encoder)
179+
}
180+
}
181+
}

0 commit comments

Comments
 (0)