mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
StatementArguments(), tests, changeling
This commit is contained in:
1 parent
7939ba6e05
commit
30ee538dbd
5 files changed
+95
-13
No files matched your search
@@ -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
|
||||
|
||||
@@ -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 = "<group>"; };
|
||||
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 = "<group>"; };
|
||||
5690C3251D23E6D800E59934 /* FoundationDateComponentsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDateComponentsTests.swift; sourceTree = "<group>"; };
|
||||
5690C3361D23E7D200E59934 /* FoundationDateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDateTests.swift; sourceTree = "<group>"; };
|
||||
5690C33F1D23E82A00E59934 /* Data.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Data.swift; sourceTree = "<group>"; };
|
||||
@@ -3001,6 +3010,7 @@
|
||||
562393681DEE0CD200A6B01F /* FlattenCursorTests.swift */,
|
||||
562393441DEDFE4500A6B01F /* IteratorCursorTests.swift */,
|
||||
562393711DEE104400A6B01F /* MapCursorTests.swift */,
|
||||
568FC2951F572F7500E176A4 /* StatementCursorTests.swift */,
|
||||
);
|
||||
name = Cursor;
|
||||
sourceTree = "<group>";
|
||||
@@ -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 */,
|
||||
|
||||
+15
-13
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user