|
| 1 | +// |
| 2 | +// SQLite.swift |
| 3 | +// https://github.com/stephencelis/SQLite.swift |
| 4 | +// Copyright © 2014-2015 Stephen Celis. |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in |
| 14 | +// all copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +// THE SOFTWARE. |
| 23 | +// |
| 24 | +import Foundation |
| 25 | + |
| 26 | +extension QueryType { |
| 27 | + |
| 28 | + /// Sets a `WITH` clause on the query. |
| 29 | + /// |
| 30 | + /// let users = Table("users") |
| 31 | + /// let id = Expression<String>("email") |
| 32 | + /// let name = Expression<String?>("name") |
| 33 | + /// |
| 34 | + /// let userNames = Table("user_names") |
| 35 | + /// userCategories.with(userNames, as: users.select(name)) |
| 36 | + /// // WITH "user_names" as (SELECT "name" FROM "users") SELECT * FROM "user_names" |
| 37 | + /// |
| 38 | + /// - Parameters: |
| 39 | + /// |
| 40 | + /// - alias: A name to assign to the table expression. |
| 41 | + /// |
| 42 | + /// - recursive: Whether to evaluate the expression recursively. |
| 43 | + /// |
| 44 | + /// - hint: Provides a hint to the query planner for how the expression should be implemented. |
| 45 | + /// |
| 46 | + /// - subquery: A query that generates the rows for the table expression. |
| 47 | + /// |
| 48 | + /// - Returns: A query with the given `ORDER BY` clause applied. |
| 49 | + public func with(_ alias: Table, columns: [Expressible]? = nil, recursive: Bool = false, |
| 50 | + hint: MaterializationHint? = nil, as subquery: QueryType) -> Self { |
| 51 | + var query = self |
| 52 | + let clause = WithClauses.Clause(alias: alias, columns: columns, hint: hint, query: subquery) |
| 53 | + query.clauses.with.recursive = query.clauses.with.recursive || recursive |
| 54 | + query.clauses.with.clauses.append(clause) |
| 55 | + return query |
| 56 | + } |
| 57 | + |
| 58 | + /// self.clauses.with transformed to an Expressible |
| 59 | + var withClause: Expressible? { |
| 60 | + guard !clauses.with.clauses.isEmpty else { |
| 61 | + return nil |
| 62 | + } |
| 63 | + |
| 64 | + let innerClauses = ", ".join(clauses.with.clauses.map { (clause) in |
| 65 | + let hintExpr: Expression<Void>? |
| 66 | + if let hint = clause.hint { |
| 67 | + hintExpr = Expression<Void>(literal: hint.rawValue) |
| 68 | + } else { |
| 69 | + hintExpr = nil |
| 70 | + } |
| 71 | + |
| 72 | + let columnExpr: Expression<Void>? |
| 73 | + if let columns = clause.columns { |
| 74 | + columnExpr = "".wrap(", ".join(columns)) |
| 75 | + } else { |
| 76 | + columnExpr = nil |
| 77 | + } |
| 78 | + |
| 79 | + let expressions: [Expressible?] = [ |
| 80 | + clause.alias.tableName(), |
| 81 | + columnExpr, |
| 82 | + Expression<Void>(literal: "AS"), |
| 83 | + hintExpr, |
| 84 | + "".wrap(clause.query) as Expression<Void> |
| 85 | + ] |
| 86 | + |
| 87 | + return " ".join(expressions.compactMap { $0 }) |
| 88 | + }) |
| 89 | + |
| 90 | + return " ".join([ |
| 91 | + Expression<Void>(literal: clauses.with.recursive ? "WITH RECURSIVE" : "WITH"), |
| 92 | + innerClauses |
| 93 | + ]) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/// Materialization hints for `WITH` clause |
| 98 | +public enum MaterializationHint: String { |
| 99 | + |
| 100 | + case materialized = "MATERIALIZED" |
| 101 | + |
| 102 | + case notMaterialized = "NOT MATERIALIZED" |
| 103 | +} |
| 104 | + |
| 105 | +struct WithClauses { |
| 106 | + struct Clause { |
| 107 | + var alias: Table |
| 108 | + var columns: [Expressible]? |
| 109 | + var hint: MaterializationHint? |
| 110 | + var query: QueryType |
| 111 | + } |
| 112 | + /// The `RECURSIVE` flag is applied to the entire `WITH` clause |
| 113 | + var recursive: Bool = false |
| 114 | + |
| 115 | + /// Each `WITH` clause may have multiple subclauses |
| 116 | + var clauses: [Clause] = [] |
| 117 | +} |
0 commit comments