File tree Expand file tree Collapse file tree 4 files changed +43
-13
lines changed Expand file tree Collapse file tree 4 files changed +43
-13
lines changed Original file line number Diff line number Diff line change @@ -64,31 +64,31 @@ public struct Expression<Datatype>: ExpressionType {
64
64
65
65
}
66
66
67
- public protocol Expressible {
67
+ public protocol Expressible : CustomStringConvertible {
68
68
69
69
var expression : Expression < Void > { get }
70
70
71
71
}
72
72
73
73
extension Expressible {
74
+ public var description : String {
75
+ asSQL ( )
76
+ }
74
77
75
78
// naïve compiler for statements that can’t be bound, e.g., CREATE TABLE
76
79
func asSQL( ) -> String {
77
80
let expressed = expression
78
- var idx = 0
79
- return expressed. template. reduce ( " " ) { template, character in
80
- let transcoded : String
81
+ return expressed. template. reduce ( ( " " , 0 ) ) { memo, character in
82
+ let ( template, index) = memo
81
83
82
84
if character == " ? " {
83
- transcoded = transcode ( expressed. bindings [ idx ] )
84
- idx += 1
85
+ precondition ( index < expressed. bindings. count , " not enough bindings for expression " )
86
+ return ( template + transcode ( expressed . bindings [ index ] ) , index + 1 )
85
87
} else {
86
- transcoded = String ( character)
88
+ return ( template + String( character) , index )
87
89
}
88
- return template + transcoded
89
- }
90
+ } . 0
90
91
}
91
-
92
92
}
93
93
94
94
extension ExpressionType {
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ class SQLiteTestCase: XCTestCase {
12
12
trace = [ String: Int] ( )
13
13
14
14
db. trace { SQL in
15
- print ( SQL)
15
+ // print(" SQL: \(SQL)" )
16
16
self . trace [ SQL, default: 0 ] += 1
17
17
}
18
18
}
Original file line number Diff line number Diff line change 1
1
import XCTest
2
- import SQLite
2
+ @ testable import SQLite
3
3
4
4
class ExpressionTests : XCTestCase {
5
+
6
+ func test_asSQL_expression_bindings( ) {
7
+ let expression = Expression < String > ( " foo ? bar " , [ " baz " ] )
8
+ XCTAssertEqual ( expression. asSQL ( ) , " foo 'baz' bar " )
9
+ }
10
+
11
+ func test_asSQL_expression_bindings_quoting( ) {
12
+ let expression = Expression < String > ( " foo ? bar " , [ " 'baz' " ] )
13
+ XCTAssertEqual ( expression. asSQL ( ) , " foo '''baz''' bar " )
14
+ }
15
+
16
+ func test_expression_custom_string_convertible( ) {
17
+ let expression = Expression < String > ( " foo ? bar " , [ " baz " ] )
18
+ XCTAssertEqual ( expression. asSQL ( ) , expression. description)
19
+ }
20
+
21
+ func test_init_literal( ) {
22
+ let expression = Expression < String > ( literal: " literal " )
23
+ XCTAssertEqual ( expression. template, " literal " )
24
+ }
25
+
26
+ func test_init_identifier( ) {
27
+ let expression = Expression < String > ( " identifier " )
28
+ XCTAssertEqual ( expression. template, " \" identifier \" " )
29
+ }
5
30
}
Original file line number Diff line number Diff line change @@ -192,7 +192,12 @@ class QueryIntegrationTests: SQLiteTestCase {
192
192
let query3 = users. select ( users [ * ] , Expression < Int > ( literal: " 1 AS weight " ) ) . filter ( email == " sally@example.com " )
193
193
let query4 = users. select ( users [ * ] , Expression < Int > ( literal: " 2 AS weight " ) ) . filter ( email == " alice@example.com " )
194
194
195
- print ( query3. union ( query4) . order ( Expression < Int > ( literal: " weight " ) ) . asSQL ( ) )
195
+ let sql = query3. union ( query4) . order ( Expression < Int > ( literal: " weight " ) ) . asSQL ( )
196
+ XCTAssertEqual ( sql,
197
+ """
198
+ SELECT " users " .*, 1 AS weight FROM " users " WHERE ( " email " = 'sally@example.com') UNION \
199
+ SELECT " users " .*, 2 AS weight FROM " users " WHERE ( " email " = 'alice@example.com') ORDER BY weight
200
+ """ )
196
201
197
202
let orderedIDs = try db. prepare ( query3. union ( query4) . order ( Expression < Int > ( literal: " weight " ) , email) ) . map { $0 [ id] }
198
203
XCTAssertEqual ( Array ( expectedIDs. reversed ( ) ) , orderedIDs)
You can’t perform that action at this time.
0 commit comments