Skip to content

Commit

Permalink
Add convenience extensions to SQLJoinBuilder, SQLSelectBuilder, SQLAl…
Browse files Browse the repository at this point in the history
…ias, and SQLBinaryExpression to simplify SQLKit queries in several common use cases. (#136)
  • Loading branch information
gwynne committed Oct 14, 2021
1 parent f6883b4 commit 8016882
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Sources/SQLKit/Builders/SQLJoinBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,30 @@ extension SQLJoinBuilder {
self.join(table, method: method, on: SQLBinaryExpression(left: left, op: op, right: right))
}

/// Include the given table in the list of those used by the query,
/// performing an explicit join using the given method and condition(s).
/// Tables are joined left to right, in the same order as invocations of
/// `from()` and `join()`. The table specifier is a string assumed to be
/// a valid SQL identifier. The join method may be an arbitrary expression.
/// The condition is a triplet of inputs representing a binary expression.
///
/// - Parameters:
/// - table: The name of the table to join.
/// - method: An expression providing the join method to use.
/// - left: The left side of a binary expression used as a join condition.
/// - op: The operator in a binary expression used as a join condition.
/// - right: The right side of a binary expression used as a join condition.
@discardableResult
public func join(
_ table: String,
method: SQLExpression = SQLJoinMethod.inner,
on left: SQLExpression,
_ op: SQLBinaryOperator,
_ right: SQLExpression
) -> Self {
self.join(SQLIdentifier(table), method: method, on: left, op, right)
}

/// Include the given table in the list of those used by the query,
/// performing an explicit join using the given method and a list of column
/// names to be used as shorthand join conditions. Tables are joined left to
Expand Down
17 changes: 17 additions & 0 deletions Sources/SQLKit/Builders/SQLSelectBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,23 @@ extension SQLSelectBuilder {
return columns.reduce(self) { $0.column($1) }
}

/// Specify a column to retrieve as a `String`, and an alias for it with another `String`.
@discardableResult
public func column(_ column: String, as alias: String) -> Self {
self.column(SQLIdentifier(column), as: SQLIdentifier(alias))
}

/// Specify a column to retrieve as an `SQLExpression`, and an alias for it with a `String`.
@discardableResult
public func column(_ column: SQLExpression, as alias: String) -> Self {
self.column(column, as: SQLIdentifier(alias))
}

/// Specify a column to retrieve as an `SQLExpression`, and an alias for it with another `SQLExpression`.
@discardableResult
public func column(_ column: SQLExpression, as alias: SQLExpression) -> Self {
self.column(SQLAlias(column, as: alias))
}
}

// MARK: From
Expand Down
6 changes: 6 additions & 0 deletions Sources/SQLKit/Query/SQLAlias.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ public struct SQLAlias: SQLExpression {
self.alias.serialize(to: &serializer)
}
}

extension SQLAlias {
public init(_ expression: SQLExpression, as alias: String) {
self.init(expression, as: SQLIdentifier(alias))
}
}
6 changes: 6 additions & 0 deletions Sources/SQLKit/Query/SQLBinaryExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ public struct SQLBinaryExpression: SQLExpression {
self.right.serialize(to: &serializer)
}
}

extension SQLBinaryExpression {
public init(_ left: SQLExpression, _ op: SQLBinaryOperator, _ right: SQLExpression) {
self.init(left: left, op: op, right: right)
}
}

0 comments on commit 8016882

Please sign in to comment.