From 30ee538dbd9675c9b9e794fce7ccbc6517b402ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 30 Aug 2017 20:07:34 +0200 Subject: [PATCH] StatementArguments(), tests, changeling --- CHANGELOG.md | 32 +++++++++++++++++++ GRDB.xcodeproj/project.pbxproj | 18 +++++++++++ GRDB/Core/Statement.swift | 28 ++++++++-------- Tests/GRDBTests/StatementArgumentsTests.swift | 5 +++ Tests/GRDBTests/StatementCursorTests.swift | 25 +++++++++++++++ 5 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 Tests/GRDBTests/StatementCursorTests.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index 63275e31d..a16bcf252 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -78,6 +78,8 @@ Release Notes ### New +New features have been added in order to plug a few holes and support the [RxGRDB](http://github.com/RxSwiftCommunity/RxGRDB) and [GRDBObjc](http://github.com/groue/GRDBObjc) companion projects: + - Persistable records can export themselves as dictionaries: ```swift @@ -86,6 +88,32 @@ Release Notes print(dict) // Prints {"id": 1, "name": "Arthur", "score": 1000} ``` + + This dictionary supports generic SQLite tools, and is not intended as a general encoding purpose because it contains values of type DatabaseValue. For example, dates are encoded as SQLite-friendly strings. + +- Prepared statements know the index of their columns: + + ```swift + let statement = try db.makeSelectStatement("SELECT a, b FROM t") + statement.index(ofColumn: "b") // 1 + ``` + +- Statement arguments have an empty initializer: `StatementArguments()`. + +- Prepared statements expose a StatementCursor which does not output any value, and simply executes `sqlite3_step()` as it is iterated: + + ```swift + let statement = try db.makeSelectStatement("SELECT ...") + let cursor = statement.cursor() + while let _ = try cursor.next() { ... } + ``` + +- Row cursors and StatementCursor expose their underlying statement: + + ```swift + let rows = try Row.fetchCursor(db, "SELECT ...") + let statement = rows.statement + ``` ### Breaking Changes @@ -167,6 +195,10 @@ Release Notes + func index(ofColumn columnName: String) -> Int? } + struct StatementArguments { ++ init() + } + -extension TableMapping { +extension MutablePersistable { @discardableResult static func deleteAll(_ db: Database) throws -> Int diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index 62d0992fa..d30464dfa 100755 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -777,6 +777,14 @@ 568E1CBA1CB03847008D97A6 /* GRDBCipher.h in Headers */ = {isa = PBXBuildFile; fileRef = 568E1CB81CB03847008D97A6 /* GRDBCipher.h */; settings = {ATTRIBUTES = (Public, ); }; }; 568E5FF91E9267A5002582E0 /* sqlite3.h in Headers */ = {isa = PBXBuildFile; fileRef = 568E5FF81E9267A5002582E0 /* sqlite3.h */; settings = {ATTRIBUTES = (Private, ); }; }; 568E5FFE1E92685C002582E0 /* sqlite3.h in Headers */ = {isa = PBXBuildFile; fileRef = 568E5FFD1E92685C002582E0 /* sqlite3.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 568FC2961F572F7500E176A4 /* StatementCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568FC2951F572F7500E176A4 /* StatementCursorTests.swift */; }; + 568FC2971F572F7500E176A4 /* StatementCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568FC2951F572F7500E176A4 /* StatementCursorTests.swift */; }; + 568FC2981F572F7500E176A4 /* StatementCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568FC2951F572F7500E176A4 /* StatementCursorTests.swift */; }; + 568FC2991F572F7500E176A4 /* StatementCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568FC2951F572F7500E176A4 /* StatementCursorTests.swift */; }; + 568FC29A1F572F7500E176A4 /* StatementCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568FC2951F572F7500E176A4 /* StatementCursorTests.swift */; }; + 568FC29B1F572F7500E176A4 /* StatementCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568FC2951F572F7500E176A4 /* StatementCursorTests.swift */; }; + 568FC29C1F572F7500E176A4 /* StatementCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568FC2951F572F7500E176A4 /* StatementCursorTests.swift */; }; + 568FC29D1F572F7500E176A4 /* StatementCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568FC2951F572F7500E176A4 /* StatementCursorTests.swift */; }; 5690C3271D23E6D800E59934 /* FoundationDateComponentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C3251D23E6D800E59934 /* FoundationDateComponentsTests.swift */; }; 5690C3281D23E6D800E59934 /* FoundationDateComponentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C3251D23E6D800E59934 /* FoundationDateComponentsTests.swift */; }; 5690C3291D23E6D800E59934 /* FoundationDateComponentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C3251D23E6D800E59934 /* FoundationDateComponentsTests.swift */; }; @@ -2084,6 +2092,7 @@ 568E1CB81CB03847008D97A6 /* GRDBCipher.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = GRDBCipher.h; path = SQLCipher/GRDBCipher.h; sourceTree = ""; }; 568E5FF81E9267A5002582E0 /* sqlite3.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = sqlite3.h; path = usr/include/sqlite3.h; sourceTree = SDKROOT; }; 568E5FFD1E92685C002582E0 /* sqlite3.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = sqlite3.h; path = usr/include/sqlite3.h; sourceTree = SDKROOT; }; + 568FC2951F572F7500E176A4 /* StatementCursorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatementCursorTests.swift; sourceTree = ""; }; 5690C3251D23E6D800E59934 /* FoundationDateComponentsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDateComponentsTests.swift; sourceTree = ""; }; 5690C3361D23E7D200E59934 /* FoundationDateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDateTests.swift; sourceTree = ""; }; 5690C33F1D23E82A00E59934 /* Data.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Data.swift; sourceTree = ""; }; @@ -3001,6 +3010,7 @@ 562393681DEE0CD200A6B01F /* FlattenCursorTests.swift */, 562393441DEDFE4500A6B01F /* IteratorCursorTests.swift */, 562393711DEE104400A6B01F /* MapCursorTests.swift */, + 568FC2951F572F7500E176A4 /* StatementCursorTests.swift */, ); name = Cursor; sourceTree = ""; @@ -4127,6 +4137,7 @@ 562393461DEDFE4500A6B01F /* IteratorCursorTests.swift in Sources */, 5698AC971DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */, 5657AB4F1D108BA9006283EF /* FoundationNSNumberTests.swift in Sources */, + 568FC2971F572F7500E176A4 /* StatementCursorTests.swift in Sources */, 56176C631EACCCC7000F3F2B /* FTS5TokenizerTests.swift in Sources */, 5623934F1DEDFEFB00A6B01F /* EnumeratedCursorTests.swift in Sources */, 560FC57C1CB00B880014AA8E /* StatementArgumentsTests.swift in Sources */, @@ -4396,6 +4407,7 @@ 5657AB401D108BA9006283EF /* FoundationNSDataTests.swift in Sources */, 562393471DEDFE4500A6B01F /* IteratorCursorTests.swift in Sources */, 5698AC981DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */, + 568FC2981F572F7500E176A4 /* StatementCursorTests.swift in Sources */, 56176C691EACCCC8000F3F2B /* FTS5TokenizerTests.swift in Sources */, 562393501DEDFEFB00A6B01F /* EnumeratedCursorTests.swift in Sources */, 5671563A1CB16729007DC145 /* StatementArgumentsTests.swift in Sources */, @@ -4624,6 +4636,7 @@ 56AFCA4B1CB1AA9900F48B96 /* MutablePersistableTests.swift in Sources */, 5657AB431D108BA9006283EF /* FoundationNSDataTests.swift in Sources */, 56AFCA4C1CB1AA9900F48B96 /* RowConvertible+QueryInterfaceRequestTests.swift in Sources */, + 568FC29B1F572F7500E176A4 /* StatementCursorTests.swift in Sources */, 56176C751EACCCCA000F3F2B /* FTS5TokenizerTests.swift in Sources */, 56AFCA4D1CB1AA9900F48B96 /* DatabasePoolReadOnlyTests.swift in Sources */, 5657AB631D108BA9006283EF /* FoundationNSURLTests.swift in Sources */, @@ -4761,6 +4774,7 @@ 5690C32C1D23E6D800E59934 /* FoundationDateComponentsTests.swift in Sources */, 56AFCAA21CB1ABC800F48B96 /* StatementArgumentsTests.swift in Sources */, 56AFCAA31CB1ABC800F48B96 /* RecordInitializersTests.swift in Sources */, + 568FC29C1F572F7500E176A4 /* StatementCursorTests.swift in Sources */, 56176C7B1EACCCCB000F3F2B /* FTS5TokenizerTests.swift in Sources */, 566AD8CC1D531BEE002EC1A8 /* TableDefinitionTests.swift in Sources */, 56AFCAA41CB1ABC800F48B96 /* MutablePersistableTests.swift in Sources */, @@ -4988,6 +5002,7 @@ 56A238681B9C74A90082EB20 /* RecordInitializersTests.swift in Sources */, 563363B01C933FF8000BE133 /* MutablePersistableTests.swift in Sources */, 5657AB421D108BA9006283EF /* FoundationNSDataTests.swift in Sources */, + 568FC29A1F572F7500E176A4 /* StatementCursorTests.swift in Sources */, 56176C6B1EACCCC9000F3F2B /* FTS5CustomTokenizerTests.swift in Sources */, 56300B621C53C42C005A543B /* RowConvertible+QueryInterfaceRequestTests.swift in Sources */, 56EA869F1C932597002BB4DF /* DatabasePoolReadOnlyTests.swift in Sources */, @@ -5125,6 +5140,7 @@ 562393301DEDFC5700A6B01F /* AnyCursorTests.swift in Sources */, 56D4965E1D81304E008276D7 /* FoundationNSStringTests.swift in Sources */, 5698AC891DA389380056AF8C /* FTS3TableBuilderTests.swift in Sources */, + 568FC2961F572F7500E176A4 /* StatementCursorTests.swift in Sources */, 56176C591EACCCC7000F3F2B /* FTS5CustomTokenizerTests.swift in Sources */, 56D4968A1D81316E008276D7 /* DatabaseValueConvertibleSubclassTests.swift in Sources */, 56D496601D81304E008276D7 /* FoundationNSUUIDTests.swift in Sources */, @@ -5444,6 +5460,7 @@ 5657AB451D108BA9006283EF /* FoundationNSDataTests.swift in Sources */, F3BA80EA1CFB3016003DC1BA /* RowFromStatementTests.swift in Sources */, F3BA80AF1CFB2FB1003DC1BA /* DatabaseCollationTests.swift in Sources */, + 568FC29D1F572F7500E176A4 /* StatementCursorTests.swift in Sources */, 5657AB651D108BA9006283EF /* FoundationNSURLTests.swift in Sources */, F3BA80F31CFB301D003DC1BA /* StatementColumnConvertibleTests.swift in Sources */, 5657AB6D1D108BA9006283EF /* FoundationURLTests.swift in Sources */, @@ -5672,6 +5689,7 @@ 5657AB511D108BA9006283EF /* FoundationNSNumberTests.swift in Sources */, 562393331DEDFC5700A6B01F /* AnyCursorTests.swift in Sources */, 56B964DB1DA5216B0002DA19 /* FTS5RecordTests.swift in Sources */, + 568FC2991F572F7500E176A4 /* StatementCursorTests.swift in Sources */, F3BA80B01CFB2FB2003DC1BA /* DatabaseCollationTests.swift in Sources */, 56C7A6AE1D2DFF6100EFB0C2 /* FoundationNSDateTests.swift in Sources */, F3BA80F51CFB301E003DC1BA /* StatementColumnConvertibleTests.swift in Sources */, diff --git a/GRDB/Core/Statement.swift b/GRDB/Core/Statement.swift index 855e67bb4..b6d315c40 100644 --- a/GRDB/Core/Statement.swift +++ b/GRDB/Core/Statement.swift @@ -553,9 +553,15 @@ public struct StatementArguments { } + // MARK: Empty Arguments + + /// Creates empty StatementArguments. + public init() { + } + // MARK: Positional Arguments - /// Initializes arguments from a sequence of optional values. + /// Creates statement arguments from a sequence of optional values. /// /// let values: [DatabaseValueConvertible?] = ["foo", 1, nil] /// db.execute("INSERT ... (?,?,?)", arguments: StatementArguments(values)) @@ -566,7 +572,7 @@ public struct StatementArguments { values = sequence.map { $0?.databaseValue ?? .null } } - /// Initializes arguments from a sequence of optional values. + /// Creates statement arguments from a sequence of optional values. /// /// let values: [String] = ["foo", "bar"] /// db.execute("INSERT ... (?,?)", arguments: StatementArguments(values)) @@ -577,9 +583,8 @@ public struct StatementArguments { values = sequence.map { $0.databaseValue } } - /// Initializes arguments from [Any]. - /// - /// The result is nil unless all objects adopt DatabaseValueConvertible. + /// Creates statement arguments from any array. The result is nil unless all + /// array elements adopt DatabaseValueConvertible. /// /// - parameter array: An array /// - returns: A StatementArguments. @@ -597,8 +602,8 @@ public struct StatementArguments { // MARK: Named Arguments - /// Initializes arguments from a sequence of (key, value) dictionary, such as - /// a dictionary. + /// Creates statement arguments from a sequence of (key, value) dictionary, + /// such as a dictionary. /// /// let values: [String: DatabaseValueConvertible?] = ["firstName": nil, "lastName": "Miller"] /// db.execute("INSERT ... (:firstName, :lastName)", arguments: StatementArguments(values)) @@ -609,8 +614,8 @@ public struct StatementArguments { namedValues = dictionary.mapValues { $0?.databaseValue ?? .null } } - /// Initializes arguments from a sequence of (key, value) pairs, such as - /// a dictionary. + /// Creates statement arguments from a sequence of (key, value) pairs, such + /// as a dictionary. /// /// let values: [String: DatabaseValueConvertible?] = ["firstName": nil, "lastName": "Miller"] /// db.execute("INSERT ... (:firstName, :lastName)", arguments: StatementArguments(values)) @@ -621,7 +626,7 @@ public struct StatementArguments { namedValues = Dictionary(uniqueKeysWithValues: sequence.map { ($0.0, $0.1?.databaseValue ?? .null) }) } - /// Initializes arguments from [AnyHashable: Any]. + /// Creates statement arguments from [AnyHashable: Any]. /// /// The result is nil unless all dictionary keys are strings, and values /// adopt DatabaseValueConvertible. @@ -802,9 +807,6 @@ public struct StatementArguments { var values: [DatabaseValue] = [] var namedValues: [String: DatabaseValue] = [:] - init() { - } - mutating func consume(_ statement: Statement, allowingRemainingValues: Bool) throws -> [DatabaseValue] { let initialValuesCount = values.count let bindings = try statement.sqliteArgumentNames.map { argumentName -> DatabaseValue in diff --git a/Tests/GRDBTests/StatementArgumentsTests.swift b/Tests/GRDBTests/StatementArgumentsTests.swift index 8457d7c40..741a833d4 100644 --- a/Tests/GRDBTests/StatementArgumentsTests.swift +++ b/Tests/GRDBTests/StatementArgumentsTests.swift @@ -23,6 +23,11 @@ class StatementArgumentsTests: GRDBTestCase { try migrator.migrate(dbWriter) } + func testEmptyStementArguments() { + let arguments = StatementArguments() + XCTAssert(arguments.isEmpty) + } + func testPositionalStatementArgumentsValidation() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in diff --git a/Tests/GRDBTests/StatementCursorTests.swift b/Tests/GRDBTests/StatementCursorTests.swift new file mode 100644 index 000000000..dfebfa3b8 --- /dev/null +++ b/Tests/GRDBTests/StatementCursorTests.swift @@ -0,0 +1,25 @@ +import XCTest +#if GRDBCIPHER + import GRDBCipher +#elseif GRDBCUSTOMSQLITE + import GRDBCustomSQLite +#else + import GRDB +#endif + +class StatementCursorTests: GRDBTestCase { + + func testStatementCursor() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + let statement = try db.makeSelectStatement("SELECT 1 UNION ALL SELECT 2") + let cursor = statement.cursor() + var i = 0 + while let _ = try cursor.next() { + i = i + 1 + } + XCTAssertEqual(i, 2) + } + } + +}