Request.asSQLRequest(_:cached:)

This commit is contained in:
Gwendal Roué committed 2017-10-23 19:20:38 +02:00
1 parent 8427b8b668
commit 1981adeb22
4 files changed
+63 -8

No files matched your search

+21
View File
@@ -7,11 +7,32 @@ Release Notes
- GRDBCipher can now build from command line ([groue/sqlcipher/pull/1](https://github.com/groue/sqlcipher/pull/1) by [Darren Clark](https://github.com/darrenclark))
- Upgrade custom SQLite builds to [v3.20.1](http://www.sqlite.org/changes.html) (thanks to [@swiftlyfalling](https://github.com/swiftlyfalling/SQLiteLib)).
- The new method `Request.asSQLRequest` allows to inspect the sql and arguments of any request:
```swift
let request = Player.all()
let sql = try request.asSQLRequest().sql
print(sql) // Prints "SELECT * FROM players"
```
- StatementArguments adopts Equatable
**Fixed**
- `DROP TABLE` statements would not drop temporary tables.
### API diff
```diff
extension Request {
+ func asSQLRequest(_ db: Database, cached: Bool = true) throws -> SQLRequest
}
struct SQLRequest {
+ let sql: String
+ let arguments: StatementArguments?
+ let adapter: RowAdapter?
}
```
## 2.0.3
+17 -5
View File
@@ -36,6 +36,18 @@ extension Request {
let sql = "SELECT COUNT(*) FROM (\(statement.sql))"
return try Int.fetchOne(db, sql, arguments: statement.arguments)!
}
/// Returns an SQLRequest.
///
/// - parameters:
/// - db: A database connection.
/// - cached: Defaults to false. If true, the request reuses a cached
/// prepared statement.
/// - returns: A SQLRequest
public func asSQLRequest(_ db: Database, cached: Bool = true) throws -> SQLRequest {
let (statement, adapter) = try prepare(db)
return SQLRequest(statement.sql, arguments: statement.arguments, adapter: adapter, cached: cached)
}
}
extension Request {
@@ -126,6 +138,11 @@ public struct AnyRequest : Request {
/// A Request built from raw SQL.
public struct SQLRequest : Request {
public let sql: String
public let arguments: StatementArguments?
public let adapter: RowAdapter?
private let statementCacheName: Database.StatementCacheName?
/// Creates a new request from an SQL string, optional arguments, and
/// optional row adapter.
///
@@ -178,11 +195,6 @@ public struct SQLRequest : Request {
}
return (statement, adapter)
}
private let sql: String
private let arguments: StatementArguments?
private let adapter: RowAdapter?
private let statementCacheName: Database.StatementCacheName?
}
/// The protocol for requests that know how to decode database rows.
+10 -3
View File
@@ -574,6 +574,8 @@ public final class UpdateStatement : Statement {
/// print(row)
/// // Prints <Row two:2 foo:"foo" one:1 foo2:"foo" bar:"bar">
public struct StatementArguments {
var values: [DatabaseValue] = []
var namedValues: [String: DatabaseValue] = [:]
public var isEmpty: Bool {
return values.isEmpty && namedValues.isEmpty
@@ -831,9 +833,6 @@ public struct StatementArguments {
// MARK: Not Public
var values: [DatabaseValue] = []
var namedValues: [String: DatabaseValue] = [:]
mutating func consume(_ statement: Statement, allowingRemainingValues: Bool) throws -> [DatabaseValue] {
let initialValuesCount = values.count
let bindings = try statement.sqliteArgumentNames.map { argumentName -> DatabaseValue in
@@ -889,6 +888,14 @@ extension StatementArguments : CustomStringConvertible {
}
}
extension StatementArguments : Equatable {
public static func == (lhs: StatementArguments, rhs: StatementArguments) -> Bool {
if lhs.values != rhs.values { return false }
if lhs.namedValues != rhs.namedValues { return false }
return true
}
}
/// A thread-unsafe statement cache
struct StatementCache {
unowned let db: Database
+15
View File
@@ -50,4 +50,19 @@ class SQLRequestTests: GRDBTestCase {
XCTAssertTrue(statement1 === statement2)
}
}
func testAsSQLRequest() throws {
struct CustomRequest: Request {
func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?) {
return try SQLRequest("SELECT ?, ?", arguments: [1, "foo"]).prepare(db)
}
}
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
let request = CustomRequest()
let sqlRequest = try request.asSQLRequest(db)
XCTAssertEqual(sqlRequest.sql, "SELECT ?, ?")
XCTAssertEqual(sqlRequest.arguments, [1, "foo"])
}
}
}