|
22 | 22 | // THE SOFTWARE. |
23 | 23 | // |
24 | 24 |
|
| 25 | +/// A typed SQL expression that wraps a raw string and bindings. |
25 | 26 | public struct Expression<T> { |
26 | 27 |
|
27 | 28 | public let SQL: String |
| 29 | + |
28 | 30 | public let bindings: [Binding?] |
29 | 31 |
|
| 32 | + /// Builds a SQL expression with a literal string and an optional list of |
| 33 | + /// bindings. |
| 34 | + /// |
| 35 | + /// :param: SQL A SQL string. |
| 36 | + /// |
| 37 | + /// :param: bindings Values to be bound to the given SQL. |
30 | 38 | public init(literal SQL: String = "", _ bindings: [Binding?] = []) { |
31 | 39 | (self.SQL, self.bindings) = (SQL, bindings) |
32 | 40 | } |
33 | 41 |
|
| 42 | + /// Builds a SQL expression with a quoted identifier. |
| 43 | + /// |
| 44 | + /// :param: identifier A SQL identifier (*e.g.*, a column name). |
34 | 45 | public init(_ identifier: String) { |
35 | 46 | self.init(literal: quote(identifier: identifier)) |
36 | 47 | } |
37 | 48 |
|
38 | | - public init<V>(_ expression: Expression<V>) { |
39 | | - self.init(literal: expression.SQL, expression.bindings) |
40 | | - } |
41 | | - |
| 49 | + /// Builds a SQL expression with the given value. |
| 50 | + /// |
| 51 | + /// :param: value An encodable SQL value. |
42 | 52 | public init<V: Value>(value: V?) { |
43 | 53 | self.init(binding: value?.datatypeValue) |
44 | 54 | } |
45 | 55 |
|
| 56 | + /// Builds a SQL expression with the given value. |
| 57 | + /// |
| 58 | + /// :param: binding A raw SQL value. |
46 | 59 | private init(binding: Binding?) { |
47 | 60 | self.init(literal: "?", [binding]) |
48 | 61 | } |
49 | 62 |
|
| 63 | + /// Returns an ascending sort version of the expression. |
50 | 64 | public var asc: Expression<()> { |
51 | 65 | return Expression.join(" ", [self, Expression(literal: "ASC")]) |
52 | 66 | } |
53 | 67 |
|
| 68 | + /// Returns an descending sort version of the expression. |
54 | 69 | public var desc: Expression<()> { |
55 | 70 | return Expression.join(" ", [self, Expression(literal: "DESC")]) |
56 | 71 | } |
57 | 72 |
|
| 73 | + internal init<V>(_ expression: Expression<V>) { |
| 74 | + self.init(literal: expression.SQL, expression.bindings) |
| 75 | + } |
| 76 | + |
58 | 77 | internal static func join(separator: String, _ expressions: [Expressible]) -> Expression<()> { |
59 | 78 | var (SQL, bindings) = ([String](), [Binding?]()) |
60 | 79 | for expressible in expressions { |
|
0 commit comments