Skip to content

Commit 6ec1677

Browse files
committed
update to SQL protocols
1 parent 0fd51ec commit 6ec1677

File tree

59 files changed

+1513
-2507
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1513
-2507
lines changed

Package.swift

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@ let package = Package(
1414
.package(url: "https://github.com/vapor/crypto.git", from: "3.0.0"),
1515

1616
// 🗄 Core services for creating database integrations.
17-
.package(url: "https://github.com/vapor/database-kit.git", from: "1.0.0"),
17+
.package(url: "https://github.com/vapor/database-kit.git", .branch("sql")),
1818

1919
// 📦 Dependency injection / inversion of control framework.
2020
.package(url: "https://github.com/vapor/service.git", from: "1.0.0"),
2121

2222
// Event-driven network application framework for high performance protocol servers & clients, non-blocking.
2323
.package(url: "https://github.com/apple/swift-nio.git", from: "1.0.0"),
24+
25+
// *️⃣ Build SQL queries in Swift. Extensible, protocol-based design that supports DQL, DML, and DDL.
26+
.package(url: "https://github.com/vapor/sql.git", .branch("sql")),
2427
],
2528
targets: [
26-
.target(name: "PostgreSQL", dependencies: ["Async", "Bits", "Core", "Crypto", "DatabaseKit", "NIO", "Service"]),
27-
.testTarget(name: "PostgreSQLTests", dependencies: ["Core", "PostgreSQL"]),
29+
.target(name: "PostgreSQL", dependencies: ["Async", "Bits", "Core", "Crypto", "DatabaseKit", "NIO", "Service", "SQL"]),
30+
.testTarget(name: "PostgreSQLTests", dependencies: ["Core", "PostgreSQL", "SQLBenchmark"]),
2831
]
2932
)

Sources/PostgreSQL/Codable/PostgreSQLDataDecoder.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public struct PostgreSQLDataDecoder {
5656
/// Unknown
5757
let _ = value.extract(Int32.self).bigEndian
5858
/// The big-endian array element type
59-
let type: PostgreSQLDataType = .init(value.extract(Int32.self).bigEndian)
59+
let type: PostgreSQLDataFormat = .init(value.extract(Int32.self).bigEndian)
6060
/// The big-endian length of the array
6161
let count = value.extract(Int32.self).bigEndian
6262
/// The big-endian number of dimensions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,96 @@
1-
/// Encodes `Encodable` objects to PostgreSQL row data.
2-
public struct PostgreSQLQueryEncoder {
3-
/// Creates a new `PostgreSQLRowEncoder`.
4-
public init() { }
5-
6-
/// Encodes an `Encodable` object to `[String: PostgreSQLQuery.DML.Value]`.
7-
///
8-
/// - parameters:
9-
/// - encodable: Item to encode.
10-
public func encode<E>(_ encodable: E) throws -> [String: PostgreSQLQuery.Value]
11-
where E: Encodable
12-
{
13-
let encoder = _Encoder()
14-
try encodable.encode(to: encoder)
15-
return encoder.row
16-
}
17-
18-
// MARK: Private
19-
20-
private final class _Encoder: Encoder {
21-
let codingPath: [CodingKey] = []
22-
var userInfo: [CodingUserInfoKey: Any] = [:]
23-
var row: [String: PostgreSQLQuery.Value]
24-
25-
init() {
26-
self.row = [:]
27-
}
28-
29-
func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key : CodingKey {
30-
return .init(_KeyedEncodingContainer(encoder: self))
31-
}
32-
33-
func unkeyedContainer() -> UnkeyedEncodingContainer {
34-
fatalError()
35-
}
36-
37-
func singleValueContainer() -> SingleValueEncodingContainer {
38-
fatalError()
39-
}
40-
}
41-
42-
private struct _KeyedEncodingContainer<Key>: KeyedEncodingContainerProtocol where Key: CodingKey {
43-
let codingPath: [CodingKey] = []
44-
let encoder: _Encoder
45-
init(encoder: _Encoder) {
46-
self.encoder = encoder
47-
}
48-
49-
mutating func encodeNil(forKey key: Key) throws {
50-
encoder.row[key.stringValue] = .null
51-
}
52-
53-
mutating func encode<T>(_ encodable: T, forKey key: Key) throws where T : Encodable {
54-
encoder.row[key.stringValue] = try PostgreSQLValueEncoder().encode(encodable)
55-
}
56-
57-
mutating func _encodeIfPresent<T>(_ value: T?, forKey key: Key) throws where T : Encodable {
58-
if let value = value {
59-
try encode(value, forKey: key)
60-
} else {
61-
try encodeNil(forKey: key)
62-
}
63-
}
64-
65-
mutating func encodeIfPresent<T>(_ value: T?, forKey key: Key) throws where T : Encodable { try _encodeIfPresent(value, forKey: key)}
66-
mutating func encodeIfPresent(_ value: Int?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
67-
mutating func encodeIfPresent(_ value: Int8?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
68-
mutating func encodeIfPresent(_ value: Int16?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
69-
mutating func encodeIfPresent(_ value: Int32?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
70-
mutating func encodeIfPresent(_ value: Int64?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
71-
mutating func encodeIfPresent(_ value: UInt?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
72-
mutating func encodeIfPresent(_ value: UInt16?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
73-
mutating func encodeIfPresent(_ value: UInt32?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
74-
mutating func encodeIfPresent(_ value: UInt64?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
75-
mutating func encodeIfPresent(_ value: Double?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
76-
mutating func encodeIfPresent(_ value: Float?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
77-
mutating func encodeIfPresent(_ value: String?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
78-
mutating func encodeIfPresent(_ value: Bool?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
79-
80-
mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> where NestedKey : CodingKey {
81-
fatalError()
82-
}
83-
84-
mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
85-
fatalError()
86-
}
87-
88-
mutating func superEncoder() -> Encoder {
89-
fatalError()
90-
}
91-
92-
mutating func superEncoder(forKey key: Key) -> Encoder {
93-
fatalError()
94-
}
95-
}
96-
}
1+
///// Encodes `Encodable` objects to PostgreSQL row data.
2+
//public struct PostgreSQLQueryEncoder {
3+
// /// Creates a new `PostgreSQLRowEncoder`.
4+
// public init() { }
5+
//
6+
// /// Encodes an `Encodable` object to `[String: PostgreSQLQuery.DML.Value]`.
7+
// ///
8+
// /// - parameters:
9+
// /// - encodable: Item to encode.
10+
// public func encode<E>(_ encodable: E) throws -> [String: PostgreSQLQuery.Value]
11+
// where E: Encodable
12+
// {
13+
// let encoder = _Encoder()
14+
// try encodable.encode(to: encoder)
15+
// return encoder.row
16+
// }
17+
//
18+
// // MARK: Private
19+
//
20+
// private final class _Encoder: Encoder {
21+
// let codingPath: [CodingKey] = []
22+
// var userInfo: [CodingUserInfoKey: Any] = [:]
23+
// var row: [String: PostgreSQLQuery.Value]
24+
//
25+
// init() {
26+
// self.row = [:]
27+
// }
28+
//
29+
// func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key : CodingKey {
30+
// return .init(_KeyedEncodingContainer(encoder: self))
31+
// }
32+
//
33+
// func unkeyedContainer() -> UnkeyedEncodingContainer {
34+
// fatalError()
35+
// }
36+
//
37+
// func singleValueContainer() -> SingleValueEncodingContainer {
38+
// fatalError()
39+
// }
40+
// }
41+
//
42+
// private struct _KeyedEncodingContainer<Key>: KeyedEncodingContainerProtocol where Key: CodingKey {
43+
// let codingPath: [CodingKey] = []
44+
// let encoder: _Encoder
45+
// init(encoder: _Encoder) {
46+
// self.encoder = encoder
47+
// }
48+
//
49+
// mutating func encodeNil(forKey key: Key) throws {
50+
// encoder.row[key.stringValue] = .null
51+
// }
52+
//
53+
// mutating func encode<T>(_ encodable: T, forKey key: Key) throws where T : Encodable {
54+
// encoder.row[key.stringValue] = try PostgreSQLValueEncoder().encode(encodable)
55+
// }
56+
//
57+
// mutating func _encodeIfPresent<T>(_ value: T?, forKey key: Key) throws where T : Encodable {
58+
// if let value = value {
59+
// try encode(value, forKey: key)
60+
// } else {
61+
// try encodeNil(forKey: key)
62+
// }
63+
// }
64+
//
65+
// mutating func encodeIfPresent<T>(_ value: T?, forKey key: Key) throws where T : Encodable { try _encodeIfPresent(value, forKey: key)}
66+
// mutating func encodeIfPresent(_ value: Int?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
67+
// mutating func encodeIfPresent(_ value: Int8?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
68+
// mutating func encodeIfPresent(_ value: Int16?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
69+
// mutating func encodeIfPresent(_ value: Int32?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
70+
// mutating func encodeIfPresent(_ value: Int64?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
71+
// mutating func encodeIfPresent(_ value: UInt?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
72+
// mutating func encodeIfPresent(_ value: UInt16?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
73+
// mutating func encodeIfPresent(_ value: UInt32?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
74+
// mutating func encodeIfPresent(_ value: UInt64?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
75+
// mutating func encodeIfPresent(_ value: Double?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
76+
// mutating func encodeIfPresent(_ value: Float?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
77+
// mutating func encodeIfPresent(_ value: String?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
78+
// mutating func encodeIfPresent(_ value: Bool?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) }
79+
//
80+
// mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> where NestedKey : CodingKey {
81+
// fatalError()
82+
// }
83+
//
84+
// mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
85+
// fatalError()
86+
// }
87+
//
88+
// mutating func superEncoder() -> Encoder {
89+
// fatalError()
90+
// }
91+
//
92+
// mutating func superEncoder(forKey key: Key) -> Encoder {
93+
// fatalError()
94+
// }
95+
// }
96+
//}

Sources/PostgreSQL/Codable/PostgreSQLValueEncoder.swift

+7-20
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
public protocol PostgreSQLValueRepresentable {
2-
var postgreSQLValue: PostgreSQLQuery.Value { get }
3-
}
4-
5-
61
/// Converts `Encodable` objects to `PostgreSQLData`.
72
///
83
/// let data = try PostgreSQLDataEncoder().encode("hello")
94
/// print(data) // PostgreSQLData
105
///
11-
public struct PostgreSQLValueEncoder {
6+
public struct PostgreSQLDataEncoder {
127
/// Creates a new `PostgreSQLDataEncoder`.
138
public init() { }
149

@@ -20,12 +15,9 @@ public struct PostgreSQLValueEncoder {
2015
/// - parameters:
2116
/// - encodable: `Encodable` object to encode.
2217
/// - returns: Encoded `PostgreSQLData`.
23-
public func encode(_ encodable: Encodable) throws -> PostgreSQLQuery.Value {
24-
if let psql = encodable as? PostgreSQLValueRepresentable {
25-
return psql.postgreSQLValue
26-
}
18+
public func encode(_ encodable: Encodable) throws -> PostgreSQLData {
2719
if let convertible = encodable as? PostgreSQLDataConvertible {
28-
return try .data(convertible.convertToPostgreSQLData())
20+
return try convertible.convertToPostgreSQLData()
2921
}
3022

3123
do {
@@ -51,7 +43,7 @@ public struct PostgreSQLValueEncoder {
5143
default: data += Data.of(Int32(0).bigEndian)
5244
}
5345
}
54-
return .data(PostgreSQLData(type.arrayType ?? .null, binary: data))
46+
return PostgreSQLData(type.arrayType ?? .null, binary: data)
5547
}
5648
} catch is _KeyedError {
5749
struct AnyEncodable: Encodable {
@@ -64,7 +56,7 @@ public struct PostgreSQLValueEncoder {
6456
try encodable.encode(to: encoder)
6557
}
6658
}
67-
return try .data(PostgreSQLData(.jsonb, binary: [0x01] + JSONEncoder().encode(AnyEncodable(encodable))))
59+
return try PostgreSQLData(.jsonb, binary: [0x01] + JSONEncoder().encode(AnyEncodable(encodable)))
6860
}
6961
}
7062

@@ -74,7 +66,7 @@ public struct PostgreSQLValueEncoder {
7466
private final class _Encoder: Encoder {
7567
let codingPath: [CodingKey] = []
7668
let userInfo: [CodingUserInfoKey: Any] = [:]
77-
var data: PostgreSQLQuery.Value?
69+
var data: PostgreSQLData?
7870
var array: [PostgreSQLData]
7971

8072
init() {
@@ -111,15 +103,10 @@ public struct PostgreSQLValueEncoder {
111103
}
112104

113105
mutating func encode<T>(_ value: T) throws where T : Encodable {
114-
if let psql = value as? PostgreSQLValueRepresentable {
115-
encoder.data = psql.postgreSQLValue
116-
return
117-
}
118106
if let convertible = value as? PostgreSQLDataConvertible {
119-
encoder.data = try .data(convertible.convertToPostgreSQLData())
107+
encoder.data = try convertible.convertToPostgreSQLData()
120108
return
121109
}
122-
123110
try value.encode(to: encoder)
124111
}
125112
}

0 commit comments

Comments
 (0)