Skip to content

Commit 3cc2fe0

Browse files
committed
fix tests + SelectBuilder
1 parent d4fec1e commit 3cc2fe0

File tree

5 files changed

+68
-20
lines changed

5 files changed

+68
-20
lines changed

Diff for: Package.swift

+1-4
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@ let package = Package(
1818

1919
// 📦 Dependency injection / inversion of control framework.
2020
.package(url: "https://github.com/vapor/service.git", from: "1.0.0"),
21-
22-
// *️⃣ Build SQL queries in Swift.
23-
.package(url: "https://github.com/vapor/sql.git", .branch("fluent-gm")),
2421

2522
// Event-driven network application framework for high performance protocol servers & clients, non-blocking.
2623
.package(url: "https://github.com/apple/swift-nio.git", from: "1.0.0"),
2724
],
2825
targets: [
29-
.target(name: "PostgreSQL", dependencies: ["Async", "Bits", "Core", "Crypto", "DatabaseKit", "NIO", "Service", "SQL"]),
26+
.target(name: "PostgreSQL", dependencies: ["Async", "Bits", "Core", "Crypto", "DatabaseKit", "NIO", "Service"]),
3027
.testTarget(name: "PostgreSQLTests", dependencies: ["Core", "PostgreSQL"]),
3128
]
3229
)

Diff for: Sources/PostgreSQL/SQL/PostgreSQLQuery+Expression.swift

+8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
extension PostgreSQLQuery {
22
public enum Key {
3+
public static func function(_ name: String, _ parameters: [Expression] = [], as alias: String? = nil) -> Key {
4+
return .expression(.function(.init(name: name, parameters: parameters)), alias: alias)
5+
}
6+
37
public static func expression(_ expression: Expression) -> Key {
48
return .expression(expression, alias: nil)
59
}
610

11+
public static var version: Key {
12+
return .expression(.function(.init(name: "version", parameters: [])))
13+
}
14+
715
/// *
816
case all
917
case expression(Expression, alias: String?)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
extension PostgreSQLQuery {
2+
public final class SelectBuilder<D> where D: Decodable {
3+
public var connection: PostgreSQLConnection
4+
public var select: Select
5+
public init(decoding: D.Type, on connection: PostgreSQLConnection) {
6+
self.select = .init(tables: [])
7+
self.connection = connection
8+
}
9+
10+
public func from(_ tables: TableName...) -> Self {
11+
select.tables += tables
12+
return self
13+
}
14+
15+
public func keys(_ keys: Key...) -> Self {
16+
select.keys += keys
17+
return self
18+
}
19+
20+
public func all() -> Future<[D]> {
21+
var results: [D] = []
22+
return run { results.append($0) }.map { results }
23+
}
24+
25+
public func run(into handler: @escaping (D) throws -> ()) -> Future<Void> {
26+
return connection.query(.select(select)) { row in
27+
let result = try PostgreSQLRowDecoder().decode(D.self, from: row)
28+
try handler(result)
29+
}
30+
}
31+
}
32+
}
33+
34+
extension PostgreSQLConnection {
35+
public func select<D>(_ type: D.Type) -> PostgreSQLQuery.SelectBuilder<D>
36+
where D: Decodable
37+
{
38+
return .init(decoding: D.self, on: self)
39+
}
40+
}

Diff for: Sources/PostgreSQL/Utilities/Exports.swift

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
@_exported import DatabaseKit
2-
@_exported import SQL

Diff for: Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift

+19-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class PostgreSQLConnectionTests: XCTestCase {
88

99
func testVersion() throws {
1010
let client = try PostgreSQLConnection.makeTest(transport: .cleartext)
11-
let results = try client.simpleQuery(.select(.version), decoding: VersionMetadata.self).wait()
11+
let results = try client.select(VersionMetadata.self).keys(.version).all().wait()
1212
XCTAssertTrue(results[0].version.contains("10."))
1313
}
1414

@@ -84,22 +84,22 @@ class PostgreSQLConnectionTests: XCTestCase {
8484
let client = try PostgreSQLConnection.makeTest(transport: .cleartext)
8585
do {
8686
// simple query
87-
let results = try client.simpleQuery(.select([.all], from: "pg_type"), decoding: PGType.self).wait()
87+
let results = try client.simpleQuery(.select(.all, from: ["pg_type"]), decoding: PGType.self).wait()
8888
XCTAssert(results.count >= 350, "Results count not large enough: \(results.count)")
8989
}
9090
do {
9191
// query: default
92-
let results = try client.query(.select([.all], from: "pg_type"), decoding: PGType.self).wait()
92+
let results = try client.select(PGType.self).from("pg_type").all().wait()
9393
XCTAssert(results.count >= 350, "Results count not large enough: \(results.count)")
9494
}
9595
do {
9696
// query: binary
97-
let results = try client.query(.select([.all], from: "pg_type"), resultFormat: .binary, decoding: PGType.self).wait()
97+
let results = try client.query(.select(.all, from: ["pg_type"]), resultFormat: .binary, decoding: PGType.self).wait()
9898
XCTAssert(results.count >= 350, "Results count not large enough: \(results.count)")
9999
}
100100
do {
101101
// query: text
102-
let results = try client.query(.select([.all], from: "pg_type"), resultFormat: .text, decoding: PGType.self).wait()
102+
let results = try client.query(.select(.all, from: ["pg_type"]), resultFormat: .text, decoding: PGType.self).wait()
103103
XCTAssert(results.count >= 350, "Results count not large enough: \(results.count)")
104104
}
105105
}
@@ -122,7 +122,7 @@ class PostgreSQLConnectionTests: XCTestCase {
122122
let hello = Hello(message: "Hello, world!")
123123
_ = try client.query(.insert(into: "foo", values: ["id": .bind(1), "dict": .bind(hello)])).wait()
124124

125-
let fetch = try client.query(.select([.all], from: "foo"), decoding: Foo.self).wait()
125+
let fetch = try client.select(Foo.self).from("foo").all().wait()
126126
switch fetch.count {
127127
case 1:
128128
XCTAssertEqual(fetch[0].id, 1)
@@ -335,7 +335,7 @@ class PostgreSQLConnectionTests: XCTestCase {
335335
_ = try conn.query(.insert(into: "types", values:
336336
PostgreSQLQueryEncoder().encode(typesA)
337337
)).wait()
338-
let rows = try conn.query(.select([.all], from: "types")).wait()
338+
let rows = try conn.query(.select(.all, from: ["types"])).wait()
339339
switch rows.count {
340340
case 1:
341341
let typesB = try PostgreSQLRowDecoder().decode(Types.self, from: rows[0])
@@ -370,12 +370,14 @@ class PostgreSQLConnectionTests: XCTestCase {
370370
let save = try conn.query(.insert(into: "users", values: ["name": .bind("vapor")])).wait()
371371
XCTAssertEqual(save.count, 0)
372372

373-
let search = try conn.query(.select([.all], from: "users"
374-
// , predicates: [.predicate("name", .equal, .bind("vapor"))]
373+
let search = try conn.query(.select(
374+
.all,
375+
from: ["users"],
376+
predicate: .predicate("name", .equal, .bind("vapor"))
375377
)).wait()
376378
XCTAssertEqual(search.count, 1)
377379

378-
try conn.query(.select(["id", "name"], from: "users")) { row in
380+
try conn.query(.select("id", "name", from: ["users"])) { row in
379381
print(row)
380382
}.wait()
381383
}
@@ -399,12 +401,14 @@ class PostgreSQLConnectionTests: XCTestCase {
399401
], returning: "id")).wait()
400402
XCTAssertEqual(save.count, 1)
401403

402-
let search = try conn.query(.select([.all], from: "users"
403-
//, where: [.predicate("name", .equal, .bind("vapor"))]
404+
let search = try conn.query(.select(
405+
.all,
406+
from: ["users"],
407+
predicate: .predicate("name", .equal, .bind("vapor"))
404408
)).wait()
405409
XCTAssertEqual(search.count, 1)
406410

407-
try conn.query(.select(["id", "name", "pet"], from: "users")) { row in
411+
try conn.query(.select("id", "name", "pet", from: ["users"])) { row in
408412
print(row)
409413
}.wait()
410414
}
@@ -424,7 +428,7 @@ class PostgreSQLConnectionTests: XCTestCase {
424428
PostgreSQLQueryEncoder().encode(time)
425429
)).wait()
426430

427-
let fetch = try conn.query(.select([.all], from: "timetest"), decoding: Time.self).wait()
431+
let fetch: [Time] = try conn.select(Time.self).from("timetest").all().wait()
428432
switch fetch.count {
429433
case 1:
430434
XCTAssertEqual(fetch[0], time)
@@ -451,7 +455,7 @@ class PostgreSQLConnectionTests: XCTestCase {
451455
struct Sum: Decodable {
452456
var sum: Double
453457
}
454-
let rows = try conn.query(.select(.function("SUM", 3.14, as: "sum")), decoding: Sum.self).wait()
458+
let rows: [Sum] = try conn.select(Sum.self).keys(.function("SUM", [3.14], as: "sum")).all().wait()
455459
switch rows.count {
456460
case 1: XCTAssertEqual(rows[0].sum, 3.14)
457461
default: XCTFail("invalid row count")

0 commit comments

Comments
 (0)