diff --git a/CHANGELOG.md b/CHANGELOG.md index 516804547..aacc01622 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: #### 5.x Releases -- `5.0.0` Betas - [5.0.0-beta](#500-beta) | [5.0.0-beta.2](#500-beta2) | [5.0.0-beta.3](#500-beta3) | [5.0.0-beta.4](#500-beta4) | [5.0.0-beta.5](#500-beta5) | [5.0.0-beta.6](#500-beta6) | [5.0.0-beta.7](#500-beta7) +- `5.0.0` Betas - [5.0.0-beta](#500-beta) | [5.0.0-beta.2](#500-beta2) | [5.0.0-beta.3](#500-beta3) | [5.0.0-beta.4](#500-beta4) | [5.0.0-beta.5](#500-beta5) | [5.0.0-beta.6](#500-beta6) | [5.0.0-beta.7](#500-beta7) | [5.0.0-beta.7](#500-beta8) #### 4.x Releases @@ -75,6 +75,13 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: --> +## 5.0.0-beta.8 + +Released July 19, 2020 • [diff](https://github.com/groue/GRDB.swift/compare/v5.0.0-beta.7...v5.0.0-beta.8) + +- **New**: [#811](https://github.com/groue/GRDB.swift/pull/811): Introduce RenameColumnAdapter + + ## 5.0.0-beta.7 Released July 14, 2020 • [diff](https://github.com/groue/GRDB.swift/compare/v5.0.0-beta.6...v5.0.0-beta.7) diff --git a/Documentation/FullTextSearch.md b/Documentation/FullTextSearch.md index ed49c0a6c..3ac780708 100644 --- a/Documentation/FullTextSearch.md +++ b/Documentation/FullTextSearch.md @@ -280,7 +280,7 @@ let pattern = FTS3Pattern(matchingAnyTokenIn: "") // nil let pattern = FTS3Pattern(matchingAnyTokenIn: "*") // nil ``` -FTS3Pattern are regular [values](../README.md#values). You can use them as query [arguments](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Structs/StatementArguments.html): +FTS3Pattern are regular [values](../README.md#values). You can use them as query [arguments](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Structs/StatementArguments.html): ```swift let documents = try Document.fetchAll(db, @@ -529,7 +529,7 @@ let pattern = FTS5Pattern(matchingAnyTokenIn: "") // nil let pattern = FTS5Pattern(matchingAnyTokenIn: "*") // nil ``` -FTS5Pattern are regular [values](../README.md#values). You can use them as query [arguments](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Structs/StatementArguments.html): +FTS5Pattern are regular [values](../README.md#values). You can use them as query [arguments](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Structs/StatementArguments.html): ```swift let documents = try Document.fetchAll(db, diff --git a/Documentation/Migrations.md b/Documentation/Migrations.md index fe24ede11..331fab06b 100644 --- a/Documentation/Migrations.md +++ b/Documentation/Migrations.md @@ -68,7 +68,7 @@ try dbQueue.read { db in } ``` -See the [DatabaseMigrator reference](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Structs/DatabaseMigrator.html) for more migrator methods. +See the [DatabaseMigrator reference](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Structs/DatabaseMigrator.html) for more migrator methods. ## The `eraseDatabaseOnSchemaChange` Option diff --git a/GRDB.swift.podspec b/GRDB.swift.podspec index cd59e2b75..48ada8829 100644 --- a/GRDB.swift.podspec +++ b/GRDB.swift.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'GRDB.swift' - s.version = '5.0.0-beta.7' + s.version = '5.0.0-beta.8' s.license = { :type => 'MIT', :file => 'LICENSE' } s.summary = 'A toolkit for SQLite databases, with a focus on application development.' diff --git a/GRDB/Core/RowAdapter.swift b/GRDB/Core/RowAdapter.swift index 78ee04305..0181c9200 100644 --- a/GRDB/Core/RowAdapter.swift +++ b/GRDB/Core/RowAdapter.swift @@ -325,7 +325,7 @@ public struct RangeRowAdapter: RowAdapter { } } -/// ScopeAdapter is a row adapter that lets you define scopes on rows. +/// `ScopeAdapter` is a row adapter that lets you define scopes on rows. /// /// // Two adapters /// let fooAdapter = ColumnMapping(["value": "foo"]) @@ -423,6 +423,31 @@ struct ChainedAdapter: RowAdapter { } } +/// `RenameColumnAdapter` is a row adapter that renames columns. +/// +/// For example: +/// +/// let adapter = RenameColumnAdapter { $0 + "rrr" } +/// let sql = "SELECT 'foo' AS foo, 'bar' AS bar, 'baz' AS baz" +/// +/// // [foorrr:"foo", barrrr:"bar", bazrrr:"baz"] +/// try Row.fetchOne(db, sql: sql, adapter: adapter) +public struct RenameColumnAdapter: RowAdapter { + let transform: (String) -> String + + /// Creates a `RenameColumnAdapter` adapter that renames columns according to the + /// provided transform function. + public init(_ transform: @escaping (String) -> String) { + self.transform = transform + } + + /// :nodoc: + public func _layoutedAdapter(from layout: _RowLayout) throws -> _LayoutedRowAdapter { + let layoutColumns = layout._layoutColumns.map { (index, column) in (index, transform(column)) } + return _LayoutedColumnMapping(layoutColumns: layoutColumns) + } +} + extension Row { /// Creates a row from a base row and a statement adapter convenience init(base: Row, adapter: _LayoutedRowAdapter) { diff --git a/GRDB/Core/SQLLiteral.swift b/GRDB/Core/SQLLiteral.swift index a3c392894..82357211a 100644 --- a/GRDB/Core/SQLLiteral.swift +++ b/GRDB/Core/SQLLiteral.swift @@ -20,6 +20,15 @@ public struct SQLLiteral { case selectable(SQLSelectable) case orderingTerm(SQLOrderingTerm) + var isEmpty: Bool { + switch self { + case let .sql(sql, _): + return sql.isEmpty + default: + return false + } + } + fileprivate func sql(_ context: SQLGenerationContext) throws -> String { switch self { case let .sql(sql, arguments): @@ -88,6 +97,11 @@ public struct SQLLiteral { self.init(elements: [.expression(expression)]) } + /// Returns true if this literal generates an empty SQL string + public var isEmpty: Bool { + elements.allSatisfy(\.isEmpty) + } + /// Turn a SQLLiteral into raw SQL and arguments. /// /// - parameter db: A database connection. diff --git a/Makefile b/Makefile index 170cdfb81..c1ff3a999 100644 --- a/Makefile +++ b/Makefile @@ -401,10 +401,10 @@ ifdef JAZZY --author 'Gwendal Roué' \ --author_url https://github.com/groue \ --github_url https://github.com/groue/GRDB.swift \ - --github-file-prefix https://github.com/groue/GRDB.swift/tree/v5.0.0-beta.7 \ - --module-version 5.0.0-beta.7 \ + --github-file-prefix https://github.com/groue/GRDB.swift/tree/v5.0.0-beta.8 \ + --module-version 5.0.0-beta.8 \ --module GRDB \ - --root-url http://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/ \ + --root-url http://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/ \ --output Documentation/Reference \ --xcodebuild-arguments -project,GRDB.xcodeproj,-scheme,GRDBiOS else diff --git a/README.md b/README.md index 27abc8df0..a0cf61de8 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,13 @@ --- -**Latest release**: July 14, 2020 • version 5.0.0-beta.7 • [CHANGELOG](CHANGELOG.md) • [Migrating From GRDB 4 to GRDB 5](Documentation/GRDB5MigrationGuide.md) +**Latest release**: July 19, 2020 • version 5.0.0-beta.8 • [CHANGELOG](CHANGELOG.md) • [Migrating From GRDB 4 to GRDB 5](Documentation/GRDB5MigrationGuide.md) **Requirements**: iOS 10.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+ • Swift 5.2+ / Xcode 11.4+ | Swift version | GRDB version | | ------------- | ----------------------------------------------------------- | -| **Swift 5.2** | **v5.0.0-beta.7**, [v4.14.0](https://github.com/groue/GRDB.swift/tree/v4.14.0)| +| **Swift 5.2** | **v5.0.0-beta.8**, [v4.14.0](https://github.com/groue/GRDB.swift/tree/v4.14.0)| | Swift 5.1 | [v4.14.0](https://github.com/groue/GRDB.swift/tree/v4.14.0) | | Swift 5 | [v4.14.0](https://github.com/groue/GRDB.swift/tree/v4.14.0) | | Swift 4.2 | [v4.14.0](https://github.com/groue/GRDB.swift/tree/v4.14.0) | @@ -269,7 +269,7 @@ Documentation #### Reference -- [GRDB Reference](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/index.html) (generated by [Jazzy](https://github.com/realm/jazzy)) +- [GRDB Reference](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/index.html) (generated by [Jazzy](https://github.com/realm/jazzy)) #### Getting Started @@ -471,7 +471,7 @@ let dbQueue = try DatabaseQueue( configuration: config) ``` -See [Configuration](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Structs/Configuration.html) for more details. +See [Configuration](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Structs/Configuration.html) for more details. ## Database Pools @@ -551,7 +551,7 @@ let dbPool = try DatabasePool( configuration: config) ``` -See [Configuration](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Structs/Configuration.html) for more details. +See [Configuration](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Structs/Configuration.html) for more details. Database pools are more memory-hungry than database queues. See [Memory Management](#memory-management) for more information. @@ -611,7 +611,7 @@ try dbQueue.write { db in } ``` -The `?` and colon-prefixed keys like `:score` in the SQL query are the **statements arguments**. You pass arguments with arrays or dictionaries, as in the example above. See [Values](#values) for more information on supported arguments types (Bool, Int, String, Date, Swift enums, etc.), and [StatementArguments](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Structs/StatementArguments.html) for a detailed documentation of SQLite arguments. +The `?` and colon-prefixed keys like `:score` in the SQL query are the **statements arguments**. You pass arguments with arrays or dictionaries, as in the example above. See [Values](#values) for more information on supported arguments types (Bool, Int, String, Date, Swift enums, etc.), and [StatementArguments](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Structs/StatementArguments.html) for a detailed documentation of SQLite arguments. You can also embed query arguments right into your SQL queries, with the `literal` argument label, as in the example below. See [SQL Interpolation] for more details. @@ -814,7 +814,7 @@ try dbQueue.read { db in - **Cursors are granted with direct access to SQLite,** unlike arrays and sets that have to take the time to copy database values. If you look after extra performance, you may prefer cursors. -- **Cursors adopt the [Cursor](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Protocols/Cursor.html) protocol, which looks a lot like standard [lazy sequences](https://developer.apple.com/reference/swift/lazysequenceprotocol) of Swift.** As such, cursors come with many convenience methods: `compactMap`, `contains`, `dropFirst`, `dropLast`, `drop(while:)`, `enumerated`, `filter`, `first`, `flatMap`, `forEach`, `joined`, `joined(separator:)`, `max`, `max(by:)`, `min`, `min(by:)`, `map`, `prefix`, `prefix(while:)`, `reduce`, `reduce(into:)`, `suffix`: +- **Cursors adopt the [Cursor](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Protocols/Cursor.html) protocol, which looks a lot like standard [lazy sequences](https://developer.apple.com/reference/swift/lazysequenceprotocol) of Swift.** As such, cursors come with many convenience methods: `compactMap`, `contains`, `dropFirst`, `dropLast`, `drop(while:)`, `enumerated`, `filter`, `first`, `flatMap`, `forEach`, `joined`, `joined(separator:)`, `max`, `max(by:)`, `min`, `min(by:)`, `map`, `prefix`, `prefix(while:)`, `reduce`, `reduce(into:)`, `suffix`: ```swift // Prints all Github links @@ -897,7 +897,7 @@ let rows = try Row.fetchAll(db, arguments: ["name": "Arthur"]) ``` -See [Values](#values) for more information on supported arguments types (Bool, Int, String, Date, Swift enums, etc.), and [StatementArguments](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Structs/StatementArguments.html) for a detailed documentation of SQLite arguments. +See [Values](#values) for more information on supported arguments types (Bool, Int, String, Date, Swift enums, etc.), and [StatementArguments](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Structs/StatementArguments.html) for a detailed documentation of SQLite arguments. Unlike row arrays that contain copies of the database rows, row cursors are close to the SQLite metal, and require a little care: @@ -1182,7 +1182,7 @@ GRDB ships with built-in support for the following value types: - Generally speaking, all types that adopt the [DatabaseValueConvertible](#custom-value-types) protocol. -Values can be used as [statement arguments](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Structs/StatementArguments.html): +Values can be used as [statement arguments](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Structs/StatementArguments.html): ```swift let url: URL = ... @@ -1589,7 +1589,7 @@ try dbQueue.inDatabase { db in // or dbPool.writeWithoutTransaction } ``` -Transactions can't be left opened unless you set the [allowsUnsafeTransactions](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Structs/Configuration.html) configuration flag: +Transactions can't be left opened unless you set the [allowsUnsafeTransactions](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Structs/Configuration.html) configuration flag: ```swift // fatal error: A transaction has been left opened at the end of a database access @@ -1703,7 +1703,7 @@ try dbQueue.write { db in } ``` -The `?` and colon-prefixed keys like `:name` in the SQL query are the statement arguments. You set them with arrays or dictionaries (arguments are actually of type [StatementArguments](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Structs/StatementArguments.html), which happens to adopt the ExpressibleByArrayLiteral and ExpressibleByDictionaryLiteral protocols). +The `?` and colon-prefixed keys like `:name` in the SQL query are the statement arguments. You set them with arrays or dictionaries (arguments are actually of type [StatementArguments](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Structs/StatementArguments.html), which happens to adopt the ExpressibleByArrayLiteral and ExpressibleByDictionaryLiteral protocols). ```swift updateStatement.arguments = ["name": "Arthur", "score": 1000] @@ -1956,45 +1956,51 @@ They basically help two incompatible row interfaces to work together. For exampl In this case, the `ColumnMapping` row adapter comes in handy: ```swift -// Fetch a 'produced' column, and consume a 'consumed' column: +// Turn the 'produced' column into 'consumed': let adapter = ColumnMapping(["consumed": "produced"]) let row = try Row.fetchOne(db, sql: "SELECT 'Hello' AS produced", adapter: adapter)! +row // [consumed:"Hello"] row["consumed"] // "Hello" -row["produced"] // nil ``` +[Record types](#records) are typical row consumers that expect database rows to have a specific layout so that they can decode them: + +```swift +struct MyRecord: Decodable, FetchableRecord { + var consumed: String +} +let record = try MyRecord.fetchOne(db, sql: "SELECT 'Hello' AS produced", adapter: adapter)! +print(record.consumed) // "Hello" +``` + +There are several situations where row adapters are useful: + +- They help disambiguate columns with identical names, which may happen when you select columns from several tables. See [Joined Queries Support](#joined-queries-support) for an example. + +- They help when SQLite outputs unexpected column names, which may happen with some subqueries. See [RenameColumnAdapter](#renamecolumnadapter) for an example. + Available row adapters are described below. +- [ColumnMapping](#columnmapping) +- [EmptyRowAdapter](#emptyrowadapter) +- [RangeRowAdapter](#rangerowadapter) +- [RenameColumnAdapter](#renamecolumnadapter) +- [ScopeAdapter](#scopeadapter) +- [SuffixRowAdapter](#suffixrowadapter) + ### ColumnMapping -ColumnMapping renames columns. Build one with a dictionary whose keys are adapted column names, and values the column names in the raw row: +`ColumnMapping renames columns. Build one with a dictionary whose keys are adapted column names, and values the column names in the raw row: ```swift -// [newName:"Hello"] -let adapter = ColumnMapping(["newName": "oldName"]) -let row = try Row.fetchOne(db, sql: "SELECT 'Hello' AS oldName", adapter: adapter)! -``` - -### SuffixRowAdapter - -`SuffixRowAdapter` hides the first columns in a row: - -```swift -// [b:1 c:2] -let adapter = SuffixRowAdapter(fromIndex: 1) +// [newA:0, newB:1] +let adapter = ColumnMapping(["newA": "a", "newB": "b"]) let row = try Row.fetchOne(db, sql: "SELECT 0 AS a, 1 AS b, 2 AS c", adapter: adapter)! ``` -### RangeRowAdapter +Note that columns that are not present in the dictionary are not present in the resulting adapted row. -`RangeRowAdapter` only exposes a range of columns. - -```swift -// [b:1] -let adapter = RangeRowAdapter(1..<2) -let row = try Row.fetchOne(db, sql: "SELECT 0 AS a, 1 AS b, 2 AS c", adapter: adapter)! -``` ### EmptyRowAdapter @@ -2009,6 +2015,44 @@ row.isEmpty // true This limit adapter may turn out useful in some narrow use cases. You'll be happy to find it when you need it. +### RangeRowAdapter + +`RangeRowAdapter` only exposes a range of columns. + +```swift +// [b:1] +let adapter = RangeRowAdapter(1..<2) +let row = try Row.fetchOne(db, sql: "SELECT 0 AS a, 1 AS b, 2 AS c", adapter: adapter)! +``` + + +### RenameColumnAdapter + +`RenameColumnAdapter` lets you transform column names with a function: + +```swift +// [arrr:0, brrr:1, crrr:2] +let adapter = RenameColumnAdapter { column in column + "rrr" } +let row = try Row.fetchOne(db, sql: "SELECT 0 AS a, 1 AS b, 2 AS c", adapter: adapter)! +``` + +This adapter may turn out useful, for example, when subqueries contain duplicated column names: + +```swift +let sql = "SELECT * FROM (SELECT 1 AS id, 2 AS id)" + +// Prints ["id", "id:1"] +// Note the "id:1" column, generated by SQLite. +let row = try Row.fetchOne(db, sql: sql)! +print(Array(row.columnNames)) + +// Drop the `:...` suffix, and prints ["id", "id"] +let adapter = RenameColumnAdapter { String($0.prefix(while: { $0 != ":" })) } +let adaptedRow = try Row.fetchOne(db, sql: sql, adapter: adapter)! +print(Array(adaptedRow.columnNames)) +``` + + ### ScopeAdapter `ScopeAdapter` defines *row scopes*: @@ -2066,6 +2110,17 @@ row.scopes["remainder"] // [c:2 d:3] To see how `ScopeAdapter` can be used, see [Joined Queries Support](#joined-queries-support). +### SuffixRowAdapter + +`SuffixRowAdapter` hides the first columns in a row: + +```swift +// [b:1 c:2] +let adapter = SuffixRowAdapter(fromIndex: 1) +let row = try Row.fetchOne(db, sql: "SELECT 0 AS a, 1 AS b, 2 AS c", adapter: adapter)! +``` + + ## Raw SQLite Pointers **If not all SQLite APIs are exposed in GRDB, you can still use the [SQLite C Interface](https://www.sqlite.org/c3ref/intro.html) and call [SQLite C functions](https://www.sqlite.org/c3ref/funclist.html).** @@ -2419,7 +2474,7 @@ try Place.fetchSet(db, sql: "SELECT ...", arguments:...) // Set try Place.fetchOne(db, sql: "SELECT ...", arguments:...) // Place? ``` -See [fetching methods](#fetching-methods) for information about the `fetchCursor`, `fetchAll`, `fetchSet` and `fetchOne` methods. See [StatementArguments](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Structs/StatementArguments.html) for more information about the query arguments. +See [fetching methods](#fetching-methods) for information about the `fetchCursor`, `fetchAll`, `fetchSet` and `fetchOne` methods. See [StatementArguments](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Structs/StatementArguments.html) for more information about the query arguments. > :point_up: **Note**: for performance reasons, the same row argument to `init(row:)` is reused during the iteration of a fetch query. If you want to keep the row for later use, make sure to store a copy: `self.row = row.copy()`. @@ -2800,7 +2855,7 @@ protocol EncodableRecord { } ``` -See [DatabaseDateDecodingStrategy](https://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Enums/DatabaseDateDecodingStrategy.html), [DatabaseDateEncodingStrategy](https://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Enums/DatabaseDateEncodingStrategy.html), and [DatabaseUUIDEncodingStrategy](https://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Enums/DatabaseUUIDEncodingStrategy.html) to learn about all available strategies. +See [DatabaseDateDecodingStrategy](https://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Enums/DatabaseDateDecodingStrategy.html), [DatabaseDateEncodingStrategy](https://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Enums/DatabaseDateEncodingStrategy.html), and [DatabaseUUIDEncodingStrategy](https://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Enums/DatabaseUUIDEncodingStrategy.html) to learn about all available strategies. > :point_up: **Note**: there is no customization of uuid decoding, because UUID can already decode all its encoded variants (16-bytes blobs, and uuid strings). @@ -4198,7 +4253,7 @@ Player // SELECT * FROM player ``` -Raw SQL snippets are also accepted, with eventual [arguments](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Structs/StatementArguments.html): +Raw SQL snippets are also accepted, with eventual [arguments](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Structs/StatementArguments.html): ```swift // SELECT DATE(creationDate), COUNT(*) FROM player WHERE name = 'Arthur' GROUP BY date(creationDate) @@ -4806,7 +4861,7 @@ try Player.customRequest().fetchAll(db) // [Player] **To build custom requests**, you can use one of the built-in requests or derive requests from other requests. -- [SQLRequest](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Structs/SQLRequest.html) is a fetch request built from raw SQL. For example: +- [SQLRequest] is a fetch request built from raw SQL. For example: ```swift extension Player { @@ -4849,7 +4904,7 @@ try Player.customRequest().fetchAll(db) // [Player] - The `adapted(_:)` method eases the consumption of complex rows with [row adapters](#row-adapters). See [Joined Queries Support](#joined-queries-support) for some sample code that uses this method. -- [AnyFetchRequest](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Structs/AnyFetchRequest.html): a type-erased request. +- [AnyFetchRequest](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Structs/AnyFetchRequest.html): a type-erased request. ## Joined Queries Support @@ -6009,7 +6064,7 @@ After `stopObservingDatabaseChangesUntilNextTransaction()`, the `databaseDidChan ### DatabaseRegion -**[DatabaseRegion](https://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Structs/DatabaseRegion.html) is a type that helps observing changes in the results of a database [request](#requests)**. +**[DatabaseRegion](https://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Structs/DatabaseRegion.html) is a type that helps observing changes in the results of a database [request](#requests)**. A request knows which database modifications can impact its results. It can communicate this information to [transaction observers](#transactionobserver-protocol) by the way of a DatabaseRegion. @@ -6019,7 +6074,7 @@ DatabaseRegion fuels, for example, [ValueObservation and DatabaseRegionObservati For example, if you observe the region of `Player.select(max(Column("score")))`, then you'll get be notified of all changes performed on the `score` column of the `player` table (updates, insertions and deletions), even if they do not modify the value of the maximum score. However, you will not get any notification for changes performed on other database tables, or updates to other columns of the player table. -For more details, see the [reference](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Structs/DatabaseRegion.html#/s:4GRDB14DatabaseRegionV10isModified2bySbAA0B5EventV_tF). +For more details, see the [reference](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Structs/DatabaseRegion.html#/s:4GRDB14DatabaseRegionV10isModified2bySbAA0B5EventV_tF). #### The DatabaseRegionConvertible Protocol @@ -7116,7 +7171,7 @@ try snapshot2.read { db in ### DatabaseWriter and DatabaseReader Protocols -Both DatabaseQueue and DatabasePool adopt the [DatabaseReader](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Protocols/DatabaseReader.html) and [DatabaseWriter](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.7/Protocols/DatabaseWriter.html) protocols. DatabaseSnapshot adopts DatabaseReader only. +Both DatabaseQueue and DatabasePool adopt the [DatabaseReader](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Protocols/DatabaseReader.html) and [DatabaseWriter](http://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Protocols/DatabaseWriter.html) protocols. DatabaseSnapshot adopts DatabaseReader only. These protocols provide a unified API that let you write generic code that targets all concurrency modes. They fuel, for example: @@ -7846,4 +7901,5 @@ This chapter has been superseded by [ValueObservation] and [DatabaseRegionObserv [Demo Applications]: Documentation/DemoApps/README.md [Sharing a Database]: Documentation/SharingADatabase.md [FAQ]: #faq -[Database Observation]: #database-changes-observation \ No newline at end of file +[Database Observation]: #database-changes-observation +[SQLRequest]: http://groue.github.io/GRDB.swift/docs/5.0.0-beta.8/Structs/SQLRequest.html diff --git a/Support/Info.plist b/Support/Info.plist index e5aee7ebb..deb05ed7a 100644 --- a/Support/Info.plist +++ b/Support/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 5.0.0-beta.7 + 5.0.0-beta.8 CFBundleSignature ???? CFBundleVersion diff --git a/Tests/GRDBTests/RowAdapterTests.swift b/Tests/GRDBTests/RowAdapterTests.swift index 177644dc3..24560203f 100644 --- a/Tests/GRDBTests/RowAdapterTests.swift +++ b/Tests/GRDBTests/RowAdapterTests.swift @@ -826,4 +826,26 @@ class AdapterRowTests : RowTestCase { """) } } + + func testRenameColumnAdapter() throws { + // Test RenameColumn with the use case it was introduced for: + // columns that have a `:NNN` suffix. + // See https://github.com/groue/GRDB.swift/issues/810 + let request: SQLRequest = #"SELECT 1 AS "id:1", 'foo' AS name, 2 AS "id:2""# + let adaptedRequest = request + .adapted { _ in RenameColumnAdapter { String($0.prefix(while: { $0 != ":" })) } } + .adapted { _ in + let adapters = splittingRowAdapters(columnCounts: [2, 1]) + return ScopeAdapter([ + "a": adapters[0], + "b": adapters[1], + ]) + } + let row = try makeDatabaseQueue().read(adaptedRequest.fetchOne)! + + XCTAssertEqual(row.unscoped, ["id": 1, "name": "foo", "id": 2]) + XCTAssertEqual(row.unadapted, ["id:1": 1, "name": "foo", "id:2": 2]) + XCTAssertEqual(row.scopes["a"], ["id": 1, "name": "foo"]) + XCTAssertEqual(row.scopes["b"], ["id": 2]) + } } diff --git a/Tests/GRDBTests/SQLLiteralTests.swift b/Tests/GRDBTests/SQLLiteralTests.swift index 8ad00c00d..7a1c95b57 100644 --- a/Tests/GRDBTests/SQLLiteralTests.swift +++ b/Tests/GRDBTests/SQLLiteralTests.swift @@ -738,4 +738,10 @@ extension SQLLiteralTests { } } } + + func testIsEmpty() { + XCTAssertTrue(SQLLiteral(elements: []).isEmpty) + XCTAssertTrue(SQLLiteral(sql: "").isEmpty) + XCTAssertTrue(SQLLiteral("").isEmpty) + } }