Skip to content

Commit d67f6a5

Browse files
authored
Merge pull request #79 from vapor/sql
update to SQL protocols
2 parents 0fd51ec + b1ce038 commit d67f6a5

File tree

65 files changed

+1881
-2449
lines changed

Some content is hidden

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

65 files changed

+1881
-2449
lines changed

Package.swift

Lines changed: 6 additions & 3 deletions
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", from: "1.2.0"),
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", from: "2.0.0-beta"),
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

Lines changed: 1 addition & 1 deletion
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

Sources/PostgreSQL/Codable/PostgreSQLQueryEncoder.swift

Lines changed: 0 additions & 96 deletions
This file was deleted.

Sources/PostgreSQL/Codable/PostgreSQLValueEncoder.swift

Lines changed: 34 additions & 21 deletions
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 {
@@ -34,7 +26,23 @@ public struct PostgreSQLValueEncoder {
3426
if let data = encoder.data {
3527
return data
3628
} else {
37-
let type = encoder.array.first?.type ?? .null
29+
let type: PostgreSQLDataFormat
30+
if let present = encoder.array.first?.type {
31+
type = present
32+
} else if
33+
let array = Swift.type(of: encodable) as? AnyArray.Type,
34+
let psql = array.anyElementType as? PostgreSQLDataTypeStaticRepresentable.Type
35+
{
36+
if let format = psql.postgreSQLDataType.dataFormat {
37+
type = format
38+
} else {
39+
WARNING("Could not determine PostgreSQL array data type: \(psql.postgreSQLDataType)")
40+
type = .null
41+
}
42+
} else {
43+
WARNING("Could not determine PostgreSQL array data type: \(Swift.type(of: encodable))")
44+
type = .null
45+
}
3846
// encode array
3947
var data = Data()
4048
data += Data.of(Int32(1).bigEndian) // non-null
@@ -51,7 +59,7 @@ public struct PostgreSQLValueEncoder {
5159
default: data += Data.of(Int32(0).bigEndian)
5260
}
5361
}
54-
return .data(PostgreSQLData(type.arrayType ?? .null, binary: data))
62+
return PostgreSQLData(type.arrayType ?? .null, binary: data)
5563
}
5664
} catch is _KeyedError {
5765
struct AnyEncodable: Encodable {
@@ -64,7 +72,7 @@ public struct PostgreSQLValueEncoder {
6472
try encodable.encode(to: encoder)
6573
}
6674
}
67-
return try .data(PostgreSQLData(.jsonb, binary: [0x01] + JSONEncoder().encode(AnyEncodable(encodable))))
75+
return try PostgreSQLData(.jsonb, binary: [0x01] + JSONEncoder().encode(AnyEncodable(encodable)))
6876
}
6977
}
7078

@@ -74,7 +82,7 @@ public struct PostgreSQLValueEncoder {
7482
private final class _Encoder: Encoder {
7583
let codingPath: [CodingKey] = []
7684
let userInfo: [CodingUserInfoKey: Any] = [:]
77-
var data: PostgreSQLQuery.Value?
85+
var data: PostgreSQLData?
7886
var array: [PostgreSQLData]
7987

8088
init() {
@@ -111,15 +119,10 @@ public struct PostgreSQLValueEncoder {
111119
}
112120

113121
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-
}
118122
if let convertible = value as? PostgreSQLDataConvertible {
119-
encoder.data = try .data(convertible.convertToPostgreSQLData())
123+
encoder.data = try convertible.convertToPostgreSQLData()
120124
return
121125
}
122-
123126
try value.encode(to: encoder)
124127
}
125128
}
@@ -192,3 +195,13 @@ public struct PostgreSQLValueEncoder {
192195
}
193196
}
194197
}
198+
199+
protocol AnyArray {
200+
static var anyElementType: Any.Type { get }
201+
}
202+
203+
extension Array: AnyArray {
204+
static var anyElementType: Any.Type {
205+
return Element.self
206+
}
207+
}

Sources/PostgreSQL/Column/PostgreSQLColumnType.swift

Lines changed: 0 additions & 111 deletions
This file was deleted.

0 commit comments

Comments
 (0)