-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for RETURNING clause (#110)
* Support RETURNING statement * Simplified SQLReturning * Updated SQLReturning serialization * Added supportsReturning to SQLDialect * Added SQLReturningBuilder to support returning statement with query builders. * Updated SQLReturningBuilder doc comments
- Loading branch information
1 parent
d044d36
commit 8b82edd
Showing
11 changed files
with
164 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
public protocol SQLReturningBuilder: SQLQueryBuilder { | ||
var returning: SQLReturning? { get set } | ||
} | ||
|
||
extension SQLReturningBuilder { | ||
/// Specify a list of columns to be part of the result set of the query. | ||
/// Each provided name is a string assumed to be a valid SQL identifier and | ||
/// is not qualified. | ||
/// | ||
/// - parameters: | ||
/// - columns: The names of the columns to return. | ||
/// - returns: Self for chaining. | ||
public func returning(_ columns: String...) -> Self { | ||
let sqlColumns = columns.map { (column) -> SQLColumn in | ||
if column == "*" { | ||
return SQLColumn(SQLLiteral.all) | ||
} else { | ||
return SQLColumn(column) | ||
} | ||
} | ||
|
||
self.returning = .init(sqlColumns) | ||
return self | ||
} | ||
|
||
/// Specify a list of columns to be returned as the result of the query. | ||
/// Each input is an arbitrary expression. | ||
/// | ||
/// - parameters: | ||
/// - columns: A list of expressions identifying the columns to return. | ||
/// - returns: Self for chaining. | ||
public func returning(_ columns: SQLExpression...) -> Self { | ||
self.returning = .init(columns) | ||
return self | ||
} | ||
|
||
/// Specify a list of columns to be returned as the result of the query. | ||
/// Each input is an arbitrary expression. | ||
/// | ||
/// - parameters: | ||
/// - column: An array of expressions identifying the columns to return. | ||
/// - returns: Self for chaining. | ||
public func returning(_ columns: [SQLExpression]) -> Self { | ||
self.returning = .init(columns) | ||
return self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/// `RETURNING ...` statement. | ||
/// | ||
public struct SQLReturning: SQLExpression { | ||
public var columns: [SQLExpression] | ||
|
||
/// Creates a new `SQLReturning`. | ||
public init(_ column: SQLColumn) { | ||
self.columns = [column] | ||
} | ||
|
||
/// Creates a new `SQLReturning`. | ||
public init(_ columns: [SQLExpression]) { | ||
self.columns = columns | ||
} | ||
|
||
public func serialize(to serializer: inout SQLSerializer) { | ||
guard serializer.dialect.supportsReturning else { | ||
serializer.database.logger.warning("\(serializer.dialect.name) does not support 'RETURNING' clause, skipping.") | ||
return | ||
} | ||
|
||
guard !columns.isEmpty else { return } | ||
|
||
serializer.statement { | ||
$0.append("RETURNING") | ||
$0.append(SQLList(columns)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters