Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-nio.git", from: "1.0.0"),

// *️⃣ Build SQL queries in Swift. Extensible, protocol-based design that supports DQL, DML, and DDL.
.package(url: "https://github.com/vapor/sql.git", from: "2.0.0-beta.3"),
.package(url: "https://github.com/vapor/sql.git", from: "2.0.2"),
],
targets: [
.target(name: "PostgreSQL", dependencies: ["Async", "Bits", "Core", "Crypto", "DatabaseKit", "NIO", "Service", "SQL"]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ extension PostgreSQLConnection {
var oid: UInt32
var relname: String
}
return select().column("oid").column("relname").from(PGClass.self).all(decoding: PGClass.self).map { rows in
return select().column("oid").column("relname").from(PGClass.self).all().map { rows in
var cache: [UInt32: String] = [:]
let rows = try rows.map { try self.decode(PGClass.self, from: $0, table: nil) }
for row in rows {
cache[row.oid] = row.relname
}
Expand Down
22 changes: 22 additions & 0 deletions Sources/PostgreSQL/SQL/PostgreSQLBinaryOperator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public enum PostgreSQLBinaryOperator: SQLBinaryOperator, Equatable {
/// See `SQLBinaryOperator`.
public static var like: PostgreSQLBinaryOperator { return ._like }

/// See `SQLBinaryOperator`.
public static var notLike: PostgreSQLBinaryOperator { return ._notLike }

/// See `SQLBinaryOperator`.
public static var `in`: PostgreSQLBinaryOperator { return ._in }

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

/// See `SQLBinaryOperator`.
public static var concatenate: PostgreSQLBinaryOperator { return ._concatenate }

/// See `SQLBinaryOperator`.
public static var multiply: PostgreSQLBinaryOperator { return ._multiply }

/// See `SQLBinaryOperator`.
public static var divide: PostgreSQLBinaryOperator { return ._divide }

/// See `SQLBinaryOperator`.
public static var modulo: PostgreSQLBinaryOperator { return ._modulo }

/// See `SQLBinaryOperator`.
public static var add: PostgreSQLBinaryOperator { return ._add }

/// See `SQLBinaryOperator`.
public static var subtract: PostgreSQLBinaryOperator { return ._subtract }


/// `||`
case _concatenate

Expand Down
2 changes: 1 addition & 1 deletion Sources/PostgreSQL/SQL/PostgreSQLDefault.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
public struct PostgreSQLDefaultLiteral: SQLDefaultLiteral {
/// See `SQLDefaultLiteral`.
public static func `default`() -> PostgreSQLDefaultLiteral {
public static var `default`: PostgreSQLDefaultLiteral {
return .init()
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/PostgreSQL/SQL/PostgreSQLDropIndex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public struct PostgreSQLDropIndex: SQLDropIndex {
}

public final class PostgreSQLDropIndexBuilder<Connection>: SQLQueryBuilder
where Connection: DatabaseQueryable, Connection.Query == PostgreSQLQuery
where Connection: SQLConnection, Connection.Query == PostgreSQLQuery
{
/// `AlterTable` query being built.
public var dropIndex: PostgreSQLDropIndex
Expand All @@ -32,7 +32,7 @@ public final class PostgreSQLDropIndexBuilder<Connection>: SQLQueryBuilder
}


extension DatabaseQueryable where Query == PostgreSQLQuery {
extension SQLConnection where Query == PostgreSQLQuery {
public func drop(index identifier: PostgreSQLIdentifier) -> PostgreSQLDropIndexBuilder<Self> {
return .init(PostgreSQLDropIndex(identifier: identifier), on: self)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/PostgreSQL/SQL/PostgreSQLGeneric.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public typealias PostgreSQLColumnIdentifier = GenericSQLColumnIdentifier<

/// See `SQLQuery`.
public typealias PostgreSQLCreateIndex = GenericSQLCreateIndex<
PostgreSQLIndexModifier, PostgreSQLIdentifier, PostgreSQLTableIdentifier
PostgreSQLIndexModifier, PostgreSQLIdentifier, PostgreSQLColumnIdentifier
>

/// See `SQLQuery`.
Expand Down Expand Up @@ -84,7 +84,7 @@ public typealias PostgreSQLSelect = GenericSQLSelect<
>

/// See `SQLQuery`.
public typealias PostgreSQLSelectExpression = GenericSQLSelectExpression<PostgreSQLExpression, PostgreSQLIdentifier>
public typealias PostgreSQLSelectExpression = GenericSQLSelectExpression<PostgreSQLExpression, PostgreSQLIdentifier, PostgreSQLTableIdentifier>

/// See `SQLQuery`.
public typealias PostgreSQLTableConstraintAlgorithm = GenericSQLTableConstraintAlgorithm<
Expand Down
70 changes: 34 additions & 36 deletions Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,54 +183,52 @@ class PostgreSQLConnectionTests: XCTestCase {
CREATE TABLE "users" ("id" UUID PRIMARY KEY, "name" TEXT NOT NULL, "username" TEXT NOT NULL)
""").wait()
defer { _ = try! client.simpleQuery("DROP TABLE users").wait() }
let _ = try client.query("""
let _ = try client.raw("""
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"))
""").wait()
""").run().wait()
defer { _ = try! client.simpleQuery("DROP TABLE acronyms").wait() }
let _ = try client.query("""
let _ = try client.raw("""
CREATE TABLE "categories" ("id" \(idtype) PRIMARY KEY, "name" TEXT NOT NULL)
""").wait()
""").run().wait()
defer { _ = try! client.simpleQuery("DROP TABLE categories").wait() }
let _ = try client.query("""
let _ = try client.raw("""
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"))
""").wait()
""").run().wait()
defer { _ = try! client.simpleQuery("DROP TABLE \"acronym+category\"").wait() }

/// INSERT
let userUUID = UUID()
let _ = try client.query(
"""
INSERT INTO "users" ("id", "name", "username") VALUES ($1, $2, $3)
""",
[userUUID, "Vapor Test", "vapor" ]
).wait()
let _ = try client.query(
"""
INSERT INTO "acronyms" ("id", "userID", "short", "long") VALUES ($1, $2, $3, $4)
""",
[1, userUUID, "ilv", "i love vapor"]
).wait()
let _ = try client.query(
"""
INSERT INTO "categories" ("id", "name") VALUES ($1, $2);
""",
[1, "all"]
).wait()
let _ = try client.raw(
"""
INSERT INTO "users" ("id", "name", "username") VALUES ($1, $2, $3)
""")
.bind(userUUID).bind("Vapor Test").bind("vapor")
.run().wait()
let _ = try client.raw(
"""
INSERT INTO "acronyms" ("id", "userID", "short", "long") VALUES ($1, $2, $3, $4)
""")
.bind(1).bind(userUUID).bind("ilv").bind("i love vapor")
.run().wait()
let _ = try client.raw(
"""
INSERT INTO "categories" ("id", "name") VALUES ($1, $2);
""")
.bind(1).bind("all")
.run().wait()


/// SELECT
let acronyms = client.query(
"""
SELECT "acronyms".* FROM "acronyms" WHERE ("acronyms"."id" = $1) LIMIT 1 OFFSET 0
""",
[1]
)
let categories = client.query(
"""
SELECT "categories".* FROM "categories" WHERE ("categories"."id" = $1) LIMIT 1 OFFSET 0
""",
[1]
)
let acronyms = client.raw(
"""
SELECT "acronyms".* FROM "acronyms" WHERE ("acronyms"."id" = $1) LIMIT 1 OFFSET 0
""")
.bind(1).run()
let categories = client.raw(
"""
SELECT "categories".* FROM "categories" WHERE ("categories"."id" = $1) LIMIT 1 OFFSET 0
""")
.bind(1).run()

_ = try acronyms.wait()
_ = try categories.wait()
Expand Down