mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
Use "real" column names in examples.
Also, remove "essential".
This commit is contained in:
1 parent
a13c1bec29
commit
8cac458efc
3 files changed
+16
-12
No files matched your search
+12
-8
@@ -674,18 +674,20 @@ extension Row {
|
||||
dataNoCopy(named: column.name)
|
||||
}
|
||||
|
||||
/// Returns the first non-null value, if any. Identical to SQL COALESCE function.
|
||||
/// Returns the first non-null value, if any. Identical to SQL `COALESCE` function.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
/// ```swift
|
||||
/// let myInt: Int? = row.coalesce(["int_A", "int_B"])
|
||||
/// let name: String? = row.coalesce(["nickname", "name"])
|
||||
/// ```
|
||||
///
|
||||
/// Use of `coalesce` is essential, as nil-coalescing row values does not work:
|
||||
/// Prefer `coalesce` to nil-coalescing row values, which does not
|
||||
/// return the expected value:
|
||||
///
|
||||
/// ```swift
|
||||
/// let myInt: Int? = row["int_A"] ?? row["int_B"] // Won't work
|
||||
/// // INCORRECT
|
||||
/// let name: String? = row["nickname"] ?? row["name"]
|
||||
/// ```
|
||||
public func coalesce<T: DatabaseValueConvertible>(
|
||||
_ columns: some Collection<String>
|
||||
@@ -698,18 +700,20 @@ extension Row {
|
||||
return nil
|
||||
}
|
||||
|
||||
/// Returns the first non-null value, if any. Identical to SQL COALESCE function.
|
||||
/// Returns the first non-null value, if any. Identical to SQL `COALESCE` function.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
/// ```swift
|
||||
/// let myInt: Int? = row.coalesce([Column("int_A"), Column("int_B")])
|
||||
/// let name: String? = row.coalesce([Column("nickname"), Column("name")])
|
||||
/// ```
|
||||
///
|
||||
/// Use of `coalesce` is essential, as nil-coalescing row values does not work:
|
||||
/// Prefer `coalesce` to nil-coalescing row values, which does not
|
||||
/// return the expected value:
|
||||
///
|
||||
/// ```swift
|
||||
/// let myInt: Int? = row[Column("int_A")] ?? row[Column("int_B")] // Won't work
|
||||
/// // INCORRECT
|
||||
/// let name: String? = row[Column("nickname")] ?? row[Column("name")]
|
||||
/// ```
|
||||
public func coalesce<T: DatabaseValueConvertible>(
|
||||
_ columns: some Collection<any ColumnExpression>
|
||||
|
||||
@@ -76,7 +76,7 @@ public func cast(_ expression: some SQLSpecificExpressible, as storageClass: Dat
|
||||
/// For example:
|
||||
///
|
||||
/// ```swift
|
||||
/// // COALESCE([value1, value2, ...])
|
||||
/// // COALESCE(value1, value2, ...)
|
||||
/// coalesce([Column("value1"), Column("value2"), ...])
|
||||
/// ```
|
||||
public func coalesce(_ values: some Collection<any SQLSpecificExpressible>) -> SQLExpression {
|
||||
|
||||
@@ -841,11 +841,11 @@ row[...] as Int?
|
||||
> if let int = row[...] as Int? { ... } // GOOD
|
||||
> ```
|
||||
|
||||
> **Warning**: avoid nil-coalescing row values:
|
||||
> **Warning**: avoid nil-coalescing row values, and prefer the `coalesce` method instead:
|
||||
>
|
||||
> ```swift
|
||||
> let myInt: Int? = row["int_A"] ?? row["int_B"] // BAD - doesn't work
|
||||
> let myInt: Int? = row.coalesce(["int_A", "int_B"]) // GOOD
|
||||
> let name: String? = row["nickname"] ?? row["name"] // BAD - doesn't work
|
||||
> let name: String? = row.coalesce(["nickname", "name"]) // GOOD
|
||||
> ```
|
||||
|
||||
Generally speaking, you can extract the type you need, provided it can be converted from the underlying SQLite value:
|
||||
|
||||
Reference in new issue
Block a user