Skip to content

Commit 7659f62

Browse files
authored
Merge pull request vapor#93 from vapor/sql-2
sql 2 updates
2 parents 826e807 + 8dfbf0f commit 7659f62

File tree

7 files changed

+64
-43
lines changed

7 files changed

+64
-43
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ let package = Package(
2323
.package(url: "https://github.com/apple/swift-nio.git", from: "1.0.0"),
2424

2525
// *️⃣ 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.3"),
26+
.package(url: "https://github.com/vapor/sql.git", from: "2.0.2"),
2727
],
2828
targets: [
2929
.target(name: "PostgreSQL", dependencies: ["Async", "Bits", "Core", "Crypto", "DatabaseKit", "NIO", "Service", "SQL"]),

Sources/PostgreSQL/Connection/PostgreSQLConnection+TableNameCache.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ extension PostgreSQLConnection {
4747
var oid: UInt32
4848
var relname: String
4949
}
50-
return select().column("oid").column("relname").from(PGClass.self).all(decoding: PGClass.self).map { rows in
50+
return select().column("oid").column("relname").from(PGClass.self).all().map { rows in
5151
var cache: [UInt32: String] = [:]
52+
let rows = try rows.map { try self.decode(PGClass.self, from: $0, table: nil) }
5253
for row in rows {
5354
cache[row.oid] = row.relname
5455
}

Sources/PostgreSQL/SQL/PostgreSQLBinaryOperator.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public enum PostgreSQLBinaryOperator: SQLBinaryOperator, Equatable {
2020
/// See `SQLBinaryOperator`.
2121
public static var like: PostgreSQLBinaryOperator { return ._like }
2222

23+
/// See `SQLBinaryOperator`.
24+
public static var notLike: PostgreSQLBinaryOperator { return ._notLike }
25+
2326
/// See `SQLBinaryOperator`.
2427
public static var `in`: PostgreSQLBinaryOperator { return ._in }
2528

@@ -32,6 +35,25 @@ public enum PostgreSQLBinaryOperator: SQLBinaryOperator, Equatable {
3235
/// See `SQLBinaryOperator`.
3336
public static var or: PostgreSQLBinaryOperator { return ._or }
3437

38+
/// See `SQLBinaryOperator`.
39+
public static var concatenate: PostgreSQLBinaryOperator { return ._concatenate }
40+
41+
/// See `SQLBinaryOperator`.
42+
public static var multiply: PostgreSQLBinaryOperator { return ._multiply }
43+
44+
/// See `SQLBinaryOperator`.
45+
public static var divide: PostgreSQLBinaryOperator { return ._divide }
46+
47+
/// See `SQLBinaryOperator`.
48+
public static var modulo: PostgreSQLBinaryOperator { return ._modulo }
49+
50+
/// See `SQLBinaryOperator`.
51+
public static var add: PostgreSQLBinaryOperator { return ._add }
52+
53+
/// See `SQLBinaryOperator`.
54+
public static var subtract: PostgreSQLBinaryOperator { return ._subtract }
55+
56+
3557
/// `||`
3658
case _concatenate
3759

Sources/PostgreSQL/SQL/PostgreSQLDefault.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
public struct PostgreSQLDefaultLiteral: SQLDefaultLiteral {
22
/// See `SQLDefaultLiteral`.
3-
public static func `default`() -> PostgreSQLDefaultLiteral {
3+
public static var `default`: PostgreSQLDefaultLiteral {
44
return .init()
55
}
66

Sources/PostgreSQL/SQL/PostgreSQLDropIndex.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public struct PostgreSQLDropIndex: SQLDropIndex {
1111
}
1212

1313
public final class PostgreSQLDropIndexBuilder<Connection>: SQLQueryBuilder
14-
where Connection: DatabaseQueryable, Connection.Query == PostgreSQLQuery
14+
where Connection: SQLConnection, Connection.Query == PostgreSQLQuery
1515
{
1616
/// `AlterTable` query being built.
1717
public var dropIndex: PostgreSQLDropIndex
@@ -32,7 +32,7 @@ public final class PostgreSQLDropIndexBuilder<Connection>: SQLQueryBuilder
3232
}
3333

3434

35-
extension DatabaseQueryable where Query == PostgreSQLQuery {
35+
extension SQLConnection where Query == PostgreSQLQuery {
3636
public func drop(index identifier: PostgreSQLIdentifier) -> PostgreSQLDropIndexBuilder<Self> {
3737
return .init(PostgreSQLDropIndex(identifier: identifier), on: self)
3838
}

Sources/PostgreSQL/SQL/PostgreSQLGeneric.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public typealias PostgreSQLColumnIdentifier = GenericSQLColumnIdentifier<
2020

2121
/// See `SQLQuery`.
2222
public typealias PostgreSQLCreateIndex = GenericSQLCreateIndex<
23-
PostgreSQLIndexModifier, PostgreSQLIdentifier, PostgreSQLTableIdentifier
23+
PostgreSQLIndexModifier, PostgreSQLIdentifier, PostgreSQLColumnIdentifier
2424
>
2525

2626
/// See `SQLQuery`.
@@ -84,7 +84,7 @@ public typealias PostgreSQLSelect = GenericSQLSelect<
8484
>
8585

8686
/// See `SQLQuery`.
87-
public typealias PostgreSQLSelectExpression = GenericSQLSelectExpression<PostgreSQLExpression, PostgreSQLIdentifier>
87+
public typealias PostgreSQLSelectExpression = GenericSQLSelectExpression<PostgreSQLExpression, PostgreSQLIdentifier, PostgreSQLTableIdentifier>
8888

8989
/// See `SQLQuery`.
9090
public typealias PostgreSQLTableConstraintAlgorithm = GenericSQLTableConstraintAlgorithm<

Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -183,54 +183,52 @@ class PostgreSQLConnectionTests: XCTestCase {
183183
CREATE TABLE "users" ("id" UUID PRIMARY KEY, "name" TEXT NOT NULL, "username" TEXT NOT NULL)
184184
""").wait()
185185
defer { _ = try! client.simpleQuery("DROP TABLE users").wait() }
186-
let _ = try client.query("""
186+
let _ = try client.raw("""
187187
CREATE TABLE "acronyms" ("id" \(idtype) PRIMARY KEY, "short" TEXT NOT NULL, "long" TEXT NOT NULL, "userID" UUID NOT NULL, FOREIGN KEY ("userID") REFERENCES "users" ("id"), FOREIGN KEY ("userID") REFERENCES "users" ("id"))
188-
""").wait()
188+
""").run().wait()
189189
defer { _ = try! client.simpleQuery("DROP TABLE acronyms").wait() }
190-
let _ = try client.query("""
190+
let _ = try client.raw("""
191191
CREATE TABLE "categories" ("id" \(idtype) PRIMARY KEY, "name" TEXT NOT NULL)
192-
""").wait()
192+
""").run().wait()
193193
defer { _ = try! client.simpleQuery("DROP TABLE categories").wait() }
194-
let _ = try client.query("""
194+
let _ = try client.raw("""
195195
CREATE TABLE "acronym+category" ("id" UUID PRIMARY KEY, "acronymID" BIGINT NOT NULL, "categoryID" BIGINT NOT NULL, FOREIGN KEY ("acronymID") REFERENCES "acronyms" ("id"), FOREIGN KEY ("categoryID") REFERENCES "categories" ("id"), FOREIGN KEY ("acronymID") REFERENCES "acronyms" ("id"), FOREIGN KEY ("categoryID") REFERENCES "categories" ("id"))
196-
""").wait()
196+
""").run().wait()
197197
defer { _ = try! client.simpleQuery("DROP TABLE \"acronym+category\"").wait() }
198198

199199
/// INSERT
200200
let userUUID = UUID()
201-
let _ = try client.query(
202-
"""
203-
INSERT INTO "users" ("id", "name", "username") VALUES ($1, $2, $3)
204-
""",
205-
[userUUID, "Vapor Test", "vapor" ]
206-
).wait()
207-
let _ = try client.query(
208-
"""
209-
INSERT INTO "acronyms" ("id", "userID", "short", "long") VALUES ($1, $2, $3, $4)
210-
""",
211-
[1, userUUID, "ilv", "i love vapor"]
212-
).wait()
213-
let _ = try client.query(
214-
"""
215-
INSERT INTO "categories" ("id", "name") VALUES ($1, $2);
216-
""",
217-
[1, "all"]
218-
).wait()
201+
let _ = try client.raw(
202+
"""
203+
INSERT INTO "users" ("id", "name", "username") VALUES ($1, $2, $3)
204+
""")
205+
.bind(userUUID).bind("Vapor Test").bind("vapor")
206+
.run().wait()
207+
let _ = try client.raw(
208+
"""
209+
INSERT INTO "acronyms" ("id", "userID", "short", "long") VALUES ($1, $2, $3, $4)
210+
""")
211+
.bind(1).bind(userUUID).bind("ilv").bind("i love vapor")
212+
.run().wait()
213+
let _ = try client.raw(
214+
"""
215+
INSERT INTO "categories" ("id", "name") VALUES ($1, $2);
216+
""")
217+
.bind(1).bind("all")
218+
.run().wait()
219219

220220

221221
/// SELECT
222-
let acronyms = client.query(
223-
"""
224-
SELECT "acronyms".* FROM "acronyms" WHERE ("acronyms"."id" = $1) LIMIT 1 OFFSET 0
225-
""",
226-
[1]
227-
)
228-
let categories = client.query(
229-
"""
230-
SELECT "categories".* FROM "categories" WHERE ("categories"."id" = $1) LIMIT 1 OFFSET 0
231-
""",
232-
[1]
233-
)
222+
let acronyms = client.raw(
223+
"""
224+
SELECT "acronyms".* FROM "acronyms" WHERE ("acronyms"."id" = $1) LIMIT 1 OFFSET 0
225+
""")
226+
.bind(1).run()
227+
let categories = client.raw(
228+
"""
229+
SELECT "categories".* FROM "categories" WHERE ("categories"."id" = $1) LIMIT 1 OFFSET 0
230+
""")
231+
.bind(1).run()
234232

235233
_ = try acronyms.wait()
236234
_ = try categories.wait()

0 commit comments

Comments
 (0)