-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/multirowInsert
- Loading branch information
Showing
10 changed files
with
263 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
|
||
public protocol SQLPartialResultBuilder: AnyObject { | ||
/// Zero or more `ORDER BY` clauses. | ||
var orderBys: [SQLExpression] { get set } | ||
|
||
/// If set, limits the maximum number of results. | ||
var limit: Int? { get set } | ||
|
||
/// If set, offsets the results. | ||
var offset: Int? { get set } | ||
} | ||
|
||
// MARK: - Limit/offset | ||
|
||
extension SQLPartialResultBuilder { | ||
/// Adds a `LIMIT` clause to the query. If called more than once, the last call wins. | ||
/// | ||
/// - Parameter max: Optional maximum limit. If `nil`, any existing limit is removed. | ||
/// - Returns: `self` for chaining. | ||
@discardableResult | ||
public func limit(_ max: Int?) -> Self { | ||
self.limit = max | ||
return self | ||
} | ||
|
||
/// Adds a `OFFSET` clause to the query. If called more than once, the last call wins. | ||
/// | ||
/// - Parameter max: Optional offset. If `nil`, any existing offset is removed. | ||
/// - Returns: `self` for chaining. | ||
@discardableResult | ||
public func offset(_ n: Int?) -> Self { | ||
self.offset = n | ||
return self | ||
} | ||
} | ||
|
||
// MARK: - Order | ||
|
||
extension SQLPartialResultBuilder { | ||
/// Adds an `ORDER BY` clause to the query with the specified column and ordering. | ||
/// | ||
/// - Parameters: | ||
/// - column: Name of column to sort results by. Appended to any previously added orderings. | ||
/// - direction: The sort direction for the column. | ||
/// - Returns: `self` for chaining. | ||
@discardableResult | ||
public func orderBy(_ column: String, _ direction: SQLDirection = .ascending) -> Self { | ||
return self.orderBy(SQLColumn(column), direction) | ||
} | ||
|
||
|
||
/// Adds an `ORDER BY` clause to the query with the specifed expression and ordering. | ||
/// | ||
/// - Parameters: | ||
/// - expression: Expression to sort results by. Appended to any previously added orderings. | ||
/// - direction: An expression describing the sort direction for the ordering expression. | ||
/// - Returns: `self` for chaining. | ||
@discardableResult | ||
public func orderBy(_ expression: SQLExpression, _ direction: SQLExpression) -> Self { | ||
return self.orderBy(SQLOrderBy(expression: expression, direction: direction)) | ||
} | ||
|
||
/// Adds an `ORDER BY` clause to the query using the specified expression. | ||
/// | ||
/// - Parameter expression: Expression to sort results by. Appended to any previously added orderings. | ||
/// - Returns: `self` for chaining. | ||
@discardableResult | ||
public func orderBy(_ expression: SQLExpression) -> Self { | ||
orderBys.append(expression) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,23 @@ | ||
/// General locking expressions for a SQL locking clause. | ||
/// General locking expressions for a SQL locking clause. The actual locking clause syntax | ||
/// for any given SQL dialect is defined by the dialect. | ||
/// | ||
/// SELECT ... FOR UPDATE | ||
/// | ||
/// See `SQLSelectBuilder.for` and `SQLSelect.lockingClause`. | ||
/// See ``SQLSubqueryClauseBuilder/for(_:)`` and ``SQLSelect/lockingClause``. | ||
public enum SQLLockingClause: SQLExpression { | ||
/// `UPDATE` | ||
/// Request an exclusive "writer" lock. | ||
case update | ||
|
||
/// `SHARE` | ||
/// Request a shared "reader" lock. | ||
case share | ||
|
||
/// See `SQLExpression`. | ||
/// See ``SQLExpression/serialize(to:)``. | ||
public func serialize(to serializer: inout SQLSerializer) { | ||
switch self { | ||
case .share: | ||
serializer.write("FOR SHARE") | ||
case .update: | ||
serializer.write("FOR UPDATE") | ||
serializer.statement { | ||
switch self { | ||
case .share: $0.append($0.dialect.sharedSelectLockExpression) | ||
case .update: $0.append($0.dialect.exclusiveSelectLockExpression) | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.