mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
Merge pull request #401 from groue/feature/selectAs
Query Interface: set the selection and the fetched type in a single method call
This commit is contained in:
7 files changed
+318
-41
No files matched your search
+30
-2
@@ -10,8 +10,9 @@ Release Notes
|
||||
|
||||
### New
|
||||
|
||||
- [#397](https://github.com/groue/GRDB.swift/pull/397): JSON encoding and decoding of codable record properties
|
||||
- [#399](https://github.com/groue/GRDB.swift/pull/399): Codable support: customize the `userInfo` context dictionary, and the format of JSON columns
|
||||
- [#397](https://github.com/groue/GRDB.swift/pull/397): Codable records: JSON encoding and decoding of codable record properties
|
||||
- [#399](https://github.com/groue/GRDB.swift/pull/399): Codable records: customize the `userInfo` context dictionary, and the format of JSON columns
|
||||
- [#401](https://github.com/groue/GRDB.swift/pull/401): Query Interface: set the selection and the fetched type in a single method call
|
||||
- [#393](https://github.com/groue/GRDB.swift/pull/393): Upgrade SQLCipher to 3.4.2, enable FTS5 on GRDBCipher and new pod GRDBPlus.
|
||||
- [#384](https://github.com/groue/GRDB.swift/pull/384): Improve database value decoding diagnostics
|
||||
- Cursors of optimized values (Strint, Int, Date, etc.) have been renamed: FastDatabaseValueCursor and FastNullableDatabaseValueCursor replace the deprecated ColumnCursor and NullableColumnCursor.
|
||||
@@ -19,6 +20,8 @@ Release Notes
|
||||
|
||||
### API diff
|
||||
|
||||
Fixes:
|
||||
|
||||
```diff
|
||||
class DatabaseQueue {
|
||||
- func write<T>(_ block: (Database) throws -> T) rethrows -> T {
|
||||
@@ -26,7 +29,11 @@ Release Notes
|
||||
- func unsafeReentrantRead<T>(_ block: (Database) throws -> T) throws -> T {
|
||||
+ func unsafeReentrantRead<T>(_ block: (Database) throws -> T) rethrows -> T {
|
||||
}
|
||||
```
|
||||
|
||||
Enhancements to Codable support:
|
||||
|
||||
```diff
|
||||
protocol FetchableRecord {
|
||||
+ static var databaseDecodingUserInfo: [CodingUserInfoKey: Any] { get }
|
||||
+ static func databaseJSONDecoder(for column: String) -> JSONDecoder
|
||||
@@ -36,7 +43,27 @@ Release Notes
|
||||
+ static var databaseEncodingUserInfo: [CodingUserInfoKey: Any] { get }
|
||||
+ static func databaseJSONEncoder(for column: String) -> JSONEncoder
|
||||
}
|
||||
```
|
||||
|
||||
Query Interface: set the selection and the fetched type in a single method call:
|
||||
|
||||
```diff
|
||||
extension QueryInterfaceRequest {
|
||||
+ func select<RowDecoder>(_ selection: [SQLSelectable], as type: RowDecoder.Type) -> QueryInterfaceRequest<RowDecoder>
|
||||
+ func select<RowDecoder>(_ selection: SQLSelectable..., as type: RowDecoder.Type) -> QueryInterfaceRequest<RowDecoder>
|
||||
+ func select<RowDecoder>(sql: String, arguments: StatementArguments? = nil, as type: RowDecoder.Type) -> QueryInterfaceRequest<RowDecoder>
|
||||
}
|
||||
|
||||
extension TableRecord {
|
||||
+ static func select<RowDecoder>(_ selection: [SQLSelectable], as type: RowDecoder.Type) -> QueryInterfaceRequest<RowDecoder>
|
||||
+ static func select<RowDecoder>(_ selection: SQLSelectable..., as type: RowDecoder.Type) -> QueryInterfaceRequest<RowDecoder>
|
||||
+ static func select<RowDecoder>(sql: String, arguments: StatementArguments? = nil, as type: RowDecoder.Type) -> QueryInterfaceRequest<RowDecoder>
|
||||
}
|
||||
```
|
||||
|
||||
Deprecations:
|
||||
|
||||
```diff
|
||||
+final class FastDatabaseValueCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> : Cursor { }
|
||||
+@available(*, deprecated, renamed: "FastDatabaseValueCursor")
|
||||
+typealias ColumnCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> = FastDatabaseValueCursor<Value>
|
||||
@@ -52,6 +79,7 @@ Release Notes
|
||||
- [Enabling FTS5 Support](README.md#enabling-fts5-support): Procedure for enabling FTS5 support in GRDB.
|
||||
- [Codable Records](README.md#codable-records): Updated documentation for JSON columns, tips, and customization options.
|
||||
- [Record Customization Options](README.md#record-customization-options): A new chapter that gather all your customization options.
|
||||
- [Fetching from Requests](README.md#fetching-from-requests): Enhanced documentation about requests that don't fetch their origin record (such as aggregates, or associated records).
|
||||
|
||||
|
||||
## 3.2.0
|
||||
|
||||
@@ -59,7 +59,7 @@ public protocol Association: DerivableRequest {
|
||||
}
|
||||
|
||||
extension Association {
|
||||
/// Creates an association with a new set of selected columns.
|
||||
/// Creates an association which selects *selection*.
|
||||
///
|
||||
/// struct Player: TableRecord {
|
||||
/// static let team = belongsTo(Team.self)
|
||||
|
||||
@@ -47,7 +47,7 @@ extension QueryInterfaceRequest : DerivableRequest, AggregatingRequest {
|
||||
|
||||
// MARK: Request Derivation
|
||||
|
||||
/// Creates a request with a new set of selected columns.
|
||||
/// Creates a request which selects *selection*.
|
||||
///
|
||||
/// // SELECT id, email FROM player
|
||||
/// var request = Player.all()
|
||||
@@ -63,6 +63,42 @@ extension QueryInterfaceRequest : DerivableRequest, AggregatingRequest {
|
||||
return QueryInterfaceRequest(query: query.select(selection))
|
||||
}
|
||||
|
||||
/// Creates a request which selects *selection*, and fetches values of
|
||||
/// type *type*.
|
||||
///
|
||||
/// try dbQueue.read { db in
|
||||
/// // SELECT max(score) FROM player
|
||||
/// let request = Player.all().select([max(Column("score"))], as: Int.self)
|
||||
/// let maxScore: Int? = try request.fetchOne(db)
|
||||
/// }
|
||||
public func select<RowDecoder>(_ selection: [SQLSelectable], as type: RowDecoder.Type) -> QueryInterfaceRequest<RowDecoder> {
|
||||
return QueryInterfaceRequest<RowDecoder>(query: query.select(selection))
|
||||
}
|
||||
|
||||
/// Creates a request which selects *selection*, and fetches values of
|
||||
/// type *type*.
|
||||
///
|
||||
/// try dbQueue.read { db in
|
||||
/// // SELECT max(score) FROM player
|
||||
/// let request = Player.all().select(max(Column("score")), as: Int.self)
|
||||
/// let maxScore: Int? = try request.fetchOne(db)
|
||||
/// }
|
||||
public func select<RowDecoder>(_ selection: SQLSelectable..., as type: RowDecoder.Type) -> QueryInterfaceRequest<RowDecoder> {
|
||||
return select(selection, as: type)
|
||||
}
|
||||
|
||||
/// Creates a request which selects *sql*, and fetches values of
|
||||
/// type *type*.
|
||||
///
|
||||
/// try dbQueue.read { db in
|
||||
/// // SELECT max(score) FROM player
|
||||
/// let request = Player.all().select(sql: "max(score)", as: Int.self)
|
||||
/// let maxScore: Int? = try request.fetchOne(db)
|
||||
/// }
|
||||
public func select<RowDecoder>(sql: String, arguments: StatementArguments? = nil, as type: RowDecoder.Type) -> QueryInterfaceRequest<RowDecoder> {
|
||||
return select(SQLSelectionLiteral(sql, arguments: arguments), as: type)
|
||||
}
|
||||
|
||||
/// Creates a request which returns distinct rows.
|
||||
///
|
||||
/// // SELECT DISTINCT * FROM player
|
||||
@@ -256,6 +292,42 @@ extension TableRecord {
|
||||
return all().select(sql: sql, arguments: arguments)
|
||||
}
|
||||
|
||||
/// Creates a request which selects *selection*, and fetches values of
|
||||
/// type *type*.
|
||||
///
|
||||
/// try dbQueue.read { db in
|
||||
/// // SELECT max(score) FROM player
|
||||
/// let request = Player.select([max(Column("score"))], as: Int.self)
|
||||
/// let maxScore: Int? = try request.fetchOne(db)
|
||||
/// }
|
||||
public static func select<RowDecoder>(_ selection: [SQLSelectable], as type: RowDecoder.Type) -> QueryInterfaceRequest<RowDecoder> {
|
||||
return all().select(selection, as: type)
|
||||
}
|
||||
|
||||
/// Creates a request which selects *selection*, and fetches values of
|
||||
/// type *type*.
|
||||
///
|
||||
/// try dbQueue.read { db in
|
||||
/// // SELECT max(score) FROM player
|
||||
/// let request = Player.select(max(Column("score")), as: Int.self)
|
||||
/// let maxScore: Int? = try request.fetchOne(db)
|
||||
/// }
|
||||
public static func select<RowDecoder>(_ selection: SQLSelectable..., as type: RowDecoder.Type) -> QueryInterfaceRequest<RowDecoder> {
|
||||
return all().select(selection, as: type)
|
||||
}
|
||||
|
||||
/// Creates a request which selects *sql*, and fetches values of
|
||||
/// type *type*.
|
||||
///
|
||||
/// try dbQueue.read { db in
|
||||
/// // SELECT max(score) FROM player
|
||||
/// let request = Player.select(sql: "max(score)", as: Int.self)
|
||||
/// let maxScore: Int? = try request.fetchOne(db)
|
||||
/// }
|
||||
public static func select<RowDecoder>(sql: String, arguments: StatementArguments? = nil, as type: RowDecoder.Type) -> QueryInterfaceRequest<RowDecoder> {
|
||||
return all().select(sql: sql, arguments: arguments, as: type)
|
||||
}
|
||||
|
||||
/// Creates a request with the provided *predicate*.
|
||||
///
|
||||
/// // SELECT * FROM player WHERE email = 'arthur@example.com'
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
/// The protocol for all requests that can refine their selection.
|
||||
public protocol SelectionRequest {
|
||||
/// Creates a request with a new set of selected columns.
|
||||
/// Creates a request which selects *selection*.
|
||||
///
|
||||
/// // SELECT id, email FROM player
|
||||
/// var request = Player.all()
|
||||
@@ -18,7 +18,7 @@ public protocol SelectionRequest {
|
||||
}
|
||||
|
||||
extension SelectionRequest {
|
||||
/// Creates a request with a new set of selected columns.
|
||||
/// Creates a request which selects *selection*.
|
||||
///
|
||||
/// // SELECT id, email FROM player
|
||||
/// var request = Player.all()
|
||||
@@ -34,7 +34,7 @@ extension SelectionRequest {
|
||||
return select(selection)
|
||||
}
|
||||
|
||||
/// Creates a request with a new set of selected columns.
|
||||
/// Creates a request which selects *sql*.
|
||||
///
|
||||
/// // SELECT id, email FROM player
|
||||
/// var request = Player.all()
|
||||
|
||||
@@ -1810,7 +1810,8 @@ SQLite has the opportunity to perform additional optimizations when aggregates a
|
||||
|
||||
```swift
|
||||
// SELECT maxLength("name") FROM player
|
||||
Int.fetchOne(db, Player.select(maxLength.apply(nameColumn))) // Int?
|
||||
let request = Player.select(maxLength.apply(nameColumn))
|
||||
try Int.fetchOne(db, request) // Int?
|
||||
```
|
||||
|
||||
|
||||
@@ -2622,7 +2623,7 @@ struct Player: Codable, FetchableRecord, PersistableRecord {
|
||||
}
|
||||
|
||||
static var maximumScore: QueryInterfaceRequest<Int> {
|
||||
return select(max(CodingKeys.score)).asRequest(of: Int.self)
|
||||
return select(max(CodingKeys.score), as: Int.self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3425,7 +3426,7 @@ So don't miss the [SQL API](#sqlite-api).
|
||||
- [Expressions](#expressions)
|
||||
- [SQL Operators](#sql-operators)
|
||||
- [SQL Functions](#sql-functions)
|
||||
- [Fetching from Requests](#fetching-from-requests)
|
||||
- [Fetching from Requests]
|
||||
- [Fetching by Key](#fetching-by-key)
|
||||
- [Fetching Aggregated Values](#fetching-aggregated-values)
|
||||
- [Delete Requests](#delete-requests)
|
||||
@@ -3478,7 +3479,7 @@ try db.create(table: "example", temporary: true, ifNotExists: true) { t in
|
||||
|
||||
> :bulb: **Tip**: database table names should be singular, and camel-cased. Make them look like Swift identifiers: `place`, `country`, `postalAddress`, 'httpRequest'.
|
||||
>
|
||||
> This will help you using [Associations](Documentation/AssociationsBasics.md) when you need them. Database table names that follow another naming convention are totally OK, but you will need to perform extra configuration.
|
||||
> This will help you using [Associations] when you need them. Database table names that follow another naming convention are totally OK, but you will need to perform extra configuration.
|
||||
|
||||
**Add regular columns** with their name and eventual type (text, integer, double, numeric, boolean, blob, date and datetime) - see [SQLite data types](https://www.sqlite.org/datatype3.html):
|
||||
|
||||
@@ -3653,13 +3654,18 @@ You can now build requests with the following methods: `all`, `none`, `select`,
|
||||
|
||||
The hidden `rowid` column can be selected as well [when you need it](#the-implicit-rowid-primary-key).
|
||||
|
||||
- `select(...)` defines the selected columns. See [Columns Selected by a Request].
|
||||
- `select(...)` and `select(..., as:)` define the selected columns. See [Columns Selected by a Request].
|
||||
|
||||
```swift
|
||||
// SELECT name FROM player
|
||||
Player.select(nameColumn, as: String.self)
|
||||
```
|
||||
|
||||
- `distinct()` performs uniquing.
|
||||
|
||||
```swift
|
||||
// SELECT DISTINCT name FROM player
|
||||
Player.select(nameColumn).distinct()
|
||||
Player.select(nameColumn, as: String.self).distinct()
|
||||
```
|
||||
|
||||
- `filter(expression)` applies conditions.
|
||||
@@ -3814,17 +3820,14 @@ let request = Player.all()
|
||||
|
||||
**The selection can be changed for each individual requests, or for all requests built from a given type.**
|
||||
|
||||
To specify the selection of a specific request, use the `select` method:
|
||||
The `select(...)` and `select(..., as:)` methods change the selection of a single request (see [Fetching from Requests] for detailed information):
|
||||
|
||||
```swift
|
||||
// SELECT id, name FROM player
|
||||
let request = Player.select(Column("id"), Column("name"))
|
||||
|
||||
// SELECT *, rowid FROM player
|
||||
let request = Player.select(AllColumns(), Column.rowID)
|
||||
let request = Player.select(max(Column("score")))
|
||||
let maxScore: Int? = try Int.fetchOne(db, request)
|
||||
```
|
||||
|
||||
To specify the default selection for all requests built from a type, define the `databaseSelection` property:
|
||||
The default selection for a record type is controlled by the `databaseSelection` property:
|
||||
|
||||
```swift
|
||||
struct RestrictedPlayer : TableRecord {
|
||||
@@ -4040,7 +4043,7 @@ Feed [requests](#requests) with SQL expressions built from your Swift code:
|
||||
Player.select(f.apply(nameColumn))
|
||||
```
|
||||
|
||||
|
||||
|
||||
## Fetching from Requests
|
||||
|
||||
Once you have a request, you can fetch the records at the origin of the request:
|
||||
@@ -4055,8 +4058,6 @@ try request.fetchAll(db) // [Player]
|
||||
try request.fetchOne(db) // Player?
|
||||
```
|
||||
|
||||
See [fetching methods](#fetching-methods) for information about the `fetchCursor`, `fetchAll` and `fetchOne` methods.
|
||||
|
||||
For example:
|
||||
|
||||
```swift
|
||||
@@ -4064,29 +4065,71 @@ let allPlayers = try Player.fetchAll(db) // [Player]
|
||||
let arthur = try Player.filter(nameColumn == "Arthur").fetchOne(db) // Player?
|
||||
```
|
||||
|
||||
See [fetching methods](#fetching-methods) for information about the `fetchCursor`, `fetchAll` and `fetchOne` methods.
|
||||
|
||||
**When the selected columns don't fit the source type**, change your target: any other type that adopts the [FetchableRecord] protocol, plain [database rows](#fetching-rows), and even [values](#values):
|
||||
**You sometimes want to fetch other values**.
|
||||
|
||||
The simplest way is to use the request as an argument to a fetching method of the desired type:
|
||||
|
||||
```swift
|
||||
// Fetch an Int
|
||||
let request = Player.select(max(scoreColumn))
|
||||
let maxScore = try Int.fetchOne(db, request) // Int?
|
||||
|
||||
// Fetch a Row
|
||||
let request = Player.select(min(scoreColumn), max(scoreColumn))
|
||||
let row = try Row.fetchOne(db, request)! // Row
|
||||
let minScore = row[0] as Int?
|
||||
let maxScore = row[1] as Int?
|
||||
```
|
||||
|
||||
You may also use the `asRequest(of:)` method, which lets you alter the type of the request itself, and feed database observation tools like [RxGRDB](http://github.com/RxSwiftCommunity/RxGRDB):
|
||||
When you also want to use database observation tools such as [FetchedRecordsController](#fetchedrecordscontroller) or [RxGRDB](http://github.com/RxSwiftCommunity/RxGRDB), you have to go one step further, and change the type of the request:
|
||||
|
||||
```swift
|
||||
let request = Player.select(max(scoreColumn)).asRequest(of: Int.self)
|
||||
request.rx
|
||||
.fetchOne(in: dbQueue)
|
||||
.subscribe(onNext: { maxScore: Int? in
|
||||
print("Maximum score has changed")
|
||||
})
|
||||
```
|
||||
- When you change the selection, prefer the `select(..., as:)` method:
|
||||
|
||||
```swift
|
||||
// A request of Int
|
||||
let request = Player.select(max(scoreColumn), as: Int.self)
|
||||
|
||||
// Simple fetch
|
||||
let maxScore = try dbQueue.read { db in
|
||||
try request.fetchOne(db) // Int?
|
||||
}
|
||||
|
||||
// Observe with RxGRDB
|
||||
request.rx
|
||||
.fetchOne(in: dbQueue)
|
||||
.subscribe(onNext: { maxScore: Int? in
|
||||
print("The maximum score has changed")
|
||||
})
|
||||
```
|
||||
|
||||
- Otherwise, use `asRequest(of:)`. Here is an example that uses [Associations]:
|
||||
|
||||
```swift
|
||||
struct BookInfo: FetchableRecord, Decodable {
|
||||
var book: Book
|
||||
var author: Author
|
||||
}
|
||||
|
||||
// A request of BookInfo
|
||||
let request = Book
|
||||
.including(required: Book.author)
|
||||
.asRequest(of: BookInfo.self)
|
||||
|
||||
// Simple fetch
|
||||
let bookInfos = try dbQueue.read { db in
|
||||
try request.fetchAll(db) // [BookInfo]
|
||||
}
|
||||
|
||||
// Observe with FetchedRecordsController
|
||||
let controller = FetchedRecordsController(
|
||||
dbQueue,
|
||||
request: request,
|
||||
isSameRecord: { (bookInfo1, bookInfo2) in
|
||||
bookInfo1.book.id == bookInfo2.book.id
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
## Fetching By Key
|
||||
@@ -4342,13 +4385,20 @@ let request = Player.all()
|
||||
try Player.filter(color: .red).fetchAll(db)
|
||||
```
|
||||
|
||||
- The `asRequest(of:)` method changes the type fetched by the request:
|
||||
- The `asRequest(of:)` method changes the type fetched by the request. It is useful, for example, when you use [Associations]:
|
||||
|
||||
```swift
|
||||
// Int?
|
||||
let maxScore = Player.select(max(Column("score")))
|
||||
.asRequest(of: Int.self)
|
||||
.fetchOne(db)
|
||||
struct BookInfo: FetchableRecord, Decodable {
|
||||
var book: Book
|
||||
var author: Author
|
||||
}
|
||||
|
||||
let request = Book
|
||||
.including(required: Book.author)
|
||||
.asRequest(of: BookInfo.self)
|
||||
|
||||
// [BookInfo]
|
||||
try request.fetchAll(db)
|
||||
```
|
||||
|
||||
- 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.
|
||||
@@ -5191,7 +5241,7 @@ LEFT JOIN round ON ...
|
||||
GROUP BY ...
|
||||
```
|
||||
|
||||
We will not talk about *generating* joined queries, which is covered in [Associations](Documentation/AssociationsBasics.md).
|
||||
We will not talk about the *generation* of joined queries, which is covered in [Associations].
|
||||
|
||||
**So what are we talking about?**
|
||||
|
||||
@@ -6652,7 +6702,7 @@ try String.fetchOne(db, "SELECT \(uppercased.name)('Jérôme')")
|
||||
|
||||
Those unicode-aware string functions are also readily available in the [query interface](#sql-functions):
|
||||
|
||||
```
|
||||
```swift
|
||||
Player.select(nameColumn.uppercased)
|
||||
```
|
||||
|
||||
@@ -7240,6 +7290,8 @@ try Player.select(idColumn, nameColumn).fetchAll(db)
|
||||
|
||||
If your Player type can't be built without other columns (it has non-optional properties for other columns), *do define and use a different type*.
|
||||
|
||||
See [Columns Selected by a Request] for more information.
|
||||
|
||||
|
||||
**Don't fetch rows you don't use**
|
||||
|
||||
@@ -7583,10 +7635,12 @@ This protocol has been renamed [TableRecord] in GRDB 3.0.
|
||||
|
||||
This chapter has been renamed [Beyond FetchableRecord].
|
||||
|
||||
[Associations]: Documentation/AssociationsBasics.md
|
||||
[Beyond FetchableRecord]: #beyond-fetchablerecord
|
||||
[Columns Selected by a Request]: #columns-selected-by-a-request
|
||||
[Conflict Resolution]: #conflict-resolution
|
||||
[Customizing the Persistence Methods]: #customizing-the-persistence-methods
|
||||
[Fetching from Requests]: #fetching-from-requests
|
||||
[The Implicit RowID Primary Key]: #the-implicit-rowid-primary-key
|
||||
[The userInfo Dictionary]: #the-userinfo-dictionary
|
||||
[JSON Columns]: #json-columns
|
||||
|
||||
@@ -40,7 +40,13 @@
|
||||
// new
|
||||
request.select(Column("name"), as: String.self)
|
||||
```
|
||||
|
||||
- [ ] select values from a JSON column:
|
||||
|
||||
```swift
|
||||
let nesteds = try Record
|
||||
.select(Column("nested"), as: Nested.self)
|
||||
.fetchAll(db)
|
||||
```
|
||||
|
||||
Swift 4.2
|
||||
|
||||
|
||||
@@ -220,6 +220,123 @@ class QueryInterfaceRequestTests: GRDBTestCase {
|
||||
"SELECT \"name\" FROM \"readers\"")
|
||||
}
|
||||
|
||||
func testSelectAs() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute("INSERT INTO readers (name, age) VALUES (?, ?)", arguments: ["Arthur", 42])
|
||||
|
||||
// select(..., as: String.self)
|
||||
do {
|
||||
// Type.select(..., as:)
|
||||
do {
|
||||
// variadic
|
||||
do {
|
||||
let value = try Reader
|
||||
.select(Col.name, as: String.self)
|
||||
.fetchOne(db)!
|
||||
XCTAssertEqual(value, "Arthur")
|
||||
}
|
||||
// array
|
||||
do {
|
||||
let value = try Reader
|
||||
.select([Col.name], as: String.self)
|
||||
.fetchOne(db)!
|
||||
XCTAssertEqual(value, "Arthur")
|
||||
}
|
||||
// raw sql without argument
|
||||
do {
|
||||
let value = try Reader
|
||||
.select(sql: "name", as: String.self)
|
||||
.fetchOne(db)!
|
||||
XCTAssertEqual(value, "Arthur")
|
||||
}
|
||||
}
|
||||
// request.select(..., as:)
|
||||
do {
|
||||
// variadic
|
||||
do {
|
||||
let value = try Reader
|
||||
.all()
|
||||
.select(Col.name, as: String.self)
|
||||
.fetchOne(db)!
|
||||
XCTAssertEqual(value, "Arthur")
|
||||
}
|
||||
// array
|
||||
do {
|
||||
let value = try Reader
|
||||
.all()
|
||||
.select([Col.name], as: String.self)
|
||||
.fetchOne(db)!
|
||||
XCTAssertEqual(value, "Arthur")
|
||||
}
|
||||
// raw sql without argument
|
||||
do {
|
||||
let value = try Reader
|
||||
.all()
|
||||
.select(sql: "name", as: String.self)
|
||||
.fetchOne(db)!
|
||||
XCTAssertEqual(value, "Arthur")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// select(..., as: Row.self)
|
||||
do {
|
||||
// Type.select(..., as:)
|
||||
do {
|
||||
// variadic
|
||||
do {
|
||||
let value = try Reader
|
||||
.select(Col.name, Col.age, as: Row.self)
|
||||
.fetchOne(db)!
|
||||
XCTAssertEqual(value, ["name": "Arthur", "age": 42])
|
||||
}
|
||||
// array
|
||||
do {
|
||||
let value = try Reader
|
||||
.select([Col.name, Col.age], as: Row.self)
|
||||
.fetchOne(db)!
|
||||
XCTAssertEqual(value, ["name": "Arthur", "age": 42])
|
||||
}
|
||||
// raw sql with named argument
|
||||
do {
|
||||
let value = try Reader
|
||||
.select(sql: "name, :age AS age", arguments: ["age": 22], as: Row.self)
|
||||
.fetchOne(db)!
|
||||
XCTAssertEqual(value, ["name": "Arthur", "age": 22])
|
||||
}
|
||||
}
|
||||
// request.select(..., as:)
|
||||
do {
|
||||
// variadic
|
||||
do {
|
||||
let value = try Reader
|
||||
.all()
|
||||
.select(Col.name, Col.age, as: Row.self)
|
||||
.fetchOne(db)!
|
||||
XCTAssertEqual(value, ["name": "Arthur", "age": 42])
|
||||
}
|
||||
// array
|
||||
do {
|
||||
let value = try Reader
|
||||
.all()
|
||||
.select([Col.name, Col.age], as: Row.self)
|
||||
.fetchOne(db)!
|
||||
XCTAssertEqual(value, ["name": "Arthur", "age": 42])
|
||||
}
|
||||
// raw sql with positional argument
|
||||
do {
|
||||
let value = try Reader
|
||||
.all()
|
||||
.select(sql: "name, ? AS age", arguments: [22], as: Row.self)
|
||||
.fetchOne(db)!
|
||||
XCTAssertEqual(value, ["name": "Arthur", "age": 22])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Distinct
|
||||
|
||||
|
||||
Reference in new issue
Block a user