diff --git a/Sources/SQLKit/Builders/SQLJoinBuilder.swift b/Sources/SQLKit/Builders/SQLJoinBuilder.swift index 98fa8c9a..b5cd503e 100644 --- a/Sources/SQLKit/Builders/SQLJoinBuilder.swift +++ b/Sources/SQLKit/Builders/SQLJoinBuilder.swift @@ -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 diff --git a/Sources/SQLKit/Builders/SQLSelectBuilder.swift b/Sources/SQLKit/Builders/SQLSelectBuilder.swift index d622c1c5..df42d032 100644 --- a/Sources/SQLKit/Builders/SQLSelectBuilder.swift +++ b/Sources/SQLKit/Builders/SQLSelectBuilder.swift @@ -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 diff --git a/Sources/SQLKit/Query/SQLAlias.swift b/Sources/SQLKit/Query/SQLAlias.swift index b045412c..d17402b1 100644 --- a/Sources/SQLKit/Query/SQLAlias.swift +++ b/Sources/SQLKit/Query/SQLAlias.swift @@ -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)) + } +} diff --git a/Sources/SQLKit/Query/SQLBinaryExpression.swift b/Sources/SQLKit/Query/SQLBinaryExpression.swift index 22507c34..ebdcc4e6 100644 --- a/Sources/SQLKit/Query/SQLBinaryExpression.swift +++ b/Sources/SQLKit/Query/SQLBinaryExpression.swift @@ -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) + } +}