mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
General cleanup:
- rename ValueConversionDebuggingInfo to ValueConversionContext - allow missing value conversion context (when decoding a raw DatabaseValue) - rename ColumnCursor to FastDatabaseValueCursor - rename NullableColumnCursor to FastNullableDatabaseValueCursor
This commit is contained in:
1 parent
1dcb42c540
commit
ef10e2e8ed
13 files changed
+225
-197
No files matched your search
@@ -15,6 +15,15 @@ Release Notes
|
||||
+ @available(*, deprecated)
|
||||
func losslessConvert<T: DatabaseValueConvertible>(sql: String? = nil, arguments: StatementArguments? = nil) -> T?
|
||||
}
|
||||
|
||||
+final class FastDatabaseValueCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> : Cursor { }
|
||||
+@available(*, deprecated, renamed: "FastDatabaseValueCursor")
|
||||
+typealias ColumnCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> = FastDatabaseValueCursor<Value>
|
||||
|
||||
+final class FastNullableDatabaseValueCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> : Cursor { }
|
||||
+@available(*, deprecated, renamed: "FastNullableDatabaseValueCursor")
|
||||
+typealias NullableColumnCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> = FastNullableDatabaseValueCursor<Value>
|
||||
|
||||
```
|
||||
|
||||
### Documentation Diff
|
||||
|
||||
@@ -227,7 +227,7 @@ extension DatabaseValue {
|
||||
/// conversion error
|
||||
@available(*, deprecated)
|
||||
public func losslessConvert<T>(sql: String? = nil, arguments: StatementArguments? = nil) -> T where T : DatabaseValueConvertible {
|
||||
return T.decode(from: self, debugInfo: ValueConversionDebuggingInfo())
|
||||
return T.decode(from: self, conversionContext: nil)
|
||||
}
|
||||
|
||||
/// Converts the database value to the type Optional<T>.
|
||||
@@ -251,7 +251,7 @@ extension DatabaseValue {
|
||||
/// conversion error
|
||||
@available(*, deprecated)
|
||||
public func losslessConvert<T>(sql: String? = nil, arguments: StatementArguments? = nil) -> T? where T : DatabaseValueConvertible {
|
||||
return T.decodeIfPresent(from: self, debugInfo: ValueConversionDebuggingInfo())
|
||||
return T.decodeIfPresent(from: self, conversionContext: nil)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,61 +4,29 @@
|
||||
import SQLite3
|
||||
#endif
|
||||
|
||||
// All primitive value conversion methods.
|
||||
// MARK: - Conversion Context and Errors
|
||||
|
||||
/// A type that helps the user understanding value conversion errors
|
||||
struct ValueConversionDebuggingInfo {
|
||||
enum Source {
|
||||
case statement(SelectStatement)
|
||||
case sql(String, StatementArguments)
|
||||
case row(Row)
|
||||
}
|
||||
enum Column {
|
||||
struct ValueConversionContext {
|
||||
private enum Column {
|
||||
case columnIndex(Int)
|
||||
case columnName(String)
|
||||
}
|
||||
private var source: Source?
|
||||
var row: Row?
|
||||
var sql: String?
|
||||
var arguments: StatementArguments?
|
||||
private var column: Column?
|
||||
|
||||
init(_ source: Source? = nil, _ column: Column? = nil) {
|
||||
self.source = source
|
||||
self.column = column
|
||||
func atColumn(_ columnIndex: Int) -> ValueConversionContext {
|
||||
var result = self
|
||||
result.column = .columnIndex(columnIndex)
|
||||
return result
|
||||
}
|
||||
|
||||
var sql: String? {
|
||||
guard let source = source else { return nil }
|
||||
switch source {
|
||||
case .statement(let statement):
|
||||
return statement.sql
|
||||
case .row(let row):
|
||||
return row.statement?.sql
|
||||
case .sql(let sql, _):
|
||||
return sql
|
||||
}
|
||||
}
|
||||
|
||||
var arguments: StatementArguments? {
|
||||
guard let source = source else { return nil }
|
||||
switch source {
|
||||
case .statement(let statement):
|
||||
return statement.arguments
|
||||
case .row(let row):
|
||||
return row.statement?.arguments
|
||||
case .sql(_, let arguments):
|
||||
return arguments
|
||||
}
|
||||
}
|
||||
|
||||
var row: Row? {
|
||||
guard let source = source else { return nil }
|
||||
switch source {
|
||||
case .statement(let statement):
|
||||
return Row(statement: statement)
|
||||
case .row(let row):
|
||||
return row
|
||||
case .sql:
|
||||
return nil
|
||||
}
|
||||
func atColumn(_ columnName: String) -> ValueConversionContext {
|
||||
var result = self
|
||||
result.column = .columnName(columnName)
|
||||
return result
|
||||
}
|
||||
|
||||
var columnIndex: Int? {
|
||||
@@ -84,35 +52,62 @@ struct ValueConversionDebuggingInfo {
|
||||
}
|
||||
}
|
||||
|
||||
extension ValueConversionContext {
|
||||
init(_ statement: SelectStatement) {
|
||||
self.init(
|
||||
row: Row(statement: statement).copy(),
|
||||
sql: statement.sql,
|
||||
arguments: statement.arguments,
|
||||
column: nil)
|
||||
}
|
||||
|
||||
init(_ row: Row) {
|
||||
if let statement = row.statement {
|
||||
self.init(
|
||||
row: row.copy(),
|
||||
sql: statement.sql,
|
||||
arguments: statement.arguments,
|
||||
column: nil)
|
||||
} else {
|
||||
self.init(
|
||||
row: row.copy(),
|
||||
sql: nil,
|
||||
arguments: nil,
|
||||
column: nil)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// The canonical conversion error message
|
||||
///
|
||||
/// - parameter dbValue: nil means "missing column"
|
||||
func conversionErrorMessage<T>(to: T.Type, from dbValue: DatabaseValue?, debugInfo: ValueConversionDebuggingInfo) -> String {
|
||||
func conversionErrorMessage<T>(to: T.Type, from dbValue: DatabaseValue?, conversionContext: ValueConversionContext?) -> String {
|
||||
var message: String
|
||||
var extras: [String] = []
|
||||
|
||||
if let dbValue = dbValue {
|
||||
message = "could not convert database value \(dbValue) to \(T.self)"
|
||||
if let columnName = debugInfo.columnName {
|
||||
if let columnName = conversionContext?.columnName {
|
||||
extras.append("column: `\(columnName)`")
|
||||
}
|
||||
if let columnIndex = debugInfo.columnIndex {
|
||||
if let columnIndex = conversionContext?.columnIndex {
|
||||
extras.append("column index: \(columnIndex)")
|
||||
}
|
||||
} else {
|
||||
message = "could not read \(T.self) from missing column"
|
||||
if let columnName = debugInfo.columnName {
|
||||
if let columnName = conversionContext?.columnName {
|
||||
message += " `\(columnName)`"
|
||||
}
|
||||
}
|
||||
|
||||
if let row = debugInfo.row {
|
||||
if let row = conversionContext?.row {
|
||||
extras.append("row: \(row)")
|
||||
}
|
||||
|
||||
if let sql = debugInfo.sql {
|
||||
if let sql = conversionContext?.sql {
|
||||
extras.append("statement: `\(sql)`")
|
||||
if let arguments = debugInfo.arguments, arguments.isEmpty == false {
|
||||
if let arguments = conversionContext?.arguments, arguments.isEmpty == false {
|
||||
extras.append("arguments: \(arguments)")
|
||||
}
|
||||
}
|
||||
@@ -125,48 +120,73 @@ func conversionErrorMessage<T>(to: T.Type, from dbValue: DatabaseValue?, debugIn
|
||||
|
||||
/// The canonical conversion fatal error
|
||||
///
|
||||
/// - parameter dbValue: nil means "missing column"
|
||||
func fatalConversionError<T>(to: T.Type, from dbValue: DatabaseValue?, debugInfo: ValueConversionDebuggingInfo, file: StaticString = #file, line: UInt = #line) -> Never {
|
||||
fatalError(conversionErrorMessage(to: T.self, from: dbValue, debugInfo: debugInfo), file: file, line: line)
|
||||
/// - parameter dbValue: nil means "missing column", for consistency with (row["missing"] as DatabaseValue? == nil)
|
||||
func fatalConversionError<T>(to: T.Type, from dbValue: DatabaseValue?, conversionContext: ValueConversionContext?, file: StaticString = #file, line: UInt = #line) -> Never {
|
||||
fatalError(conversionErrorMessage(to: T.self, from: dbValue, conversionContext: conversionContext), file: file, line: line)
|
||||
}
|
||||
|
||||
// MARK: - DatabaseValueConvertible
|
||||
|
||||
extension DatabaseValueConvertible {
|
||||
/// Performs lossless conversion from a database value.
|
||||
static func decode(from dbValue: DatabaseValue, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Self {
|
||||
@inline(__always)
|
||||
static func decode(from dbValue: DatabaseValue, conversionContext: @autoclosure () -> ValueConversionContext?) -> Self {
|
||||
if let value = fromDatabaseValue(dbValue) {
|
||||
return value
|
||||
} else {
|
||||
fatalConversionError(to: Self.self, from: dbValue, debugInfo: debugInfo())
|
||||
fatalConversionError(to: Self.self, from: dbValue, conversionContext: conversionContext())
|
||||
}
|
||||
}
|
||||
|
||||
/// Performs lossless conversion from a database value.
|
||||
static func decodeIfPresent(from dbValue: DatabaseValue, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Self? {
|
||||
@inline(__always)
|
||||
static func decodeIfPresent(from dbValue: DatabaseValue, conversionContext: @autoclosure () -> ValueConversionContext?) -> Self? {
|
||||
// Use fromDatabaseValue before checking for null: this allows DatabaseValue to convert NULL to .null.
|
||||
if let value = fromDatabaseValue(dbValue) {
|
||||
return value
|
||||
} else if dbValue.isNull {
|
||||
return nil
|
||||
} else {
|
||||
fatalConversionError(to: Self.self, from: dbValue, debugInfo: debugInfo())
|
||||
fatalConversionError(to: Self.self, from: dbValue, conversionContext: conversionContext())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension DatabaseValueConvertible where Self: StatementColumnConvertible {
|
||||
extension Row {
|
||||
@inline(__always)
|
||||
func decodeIfPresent<Value: DatabaseValueConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
conversionContext: @autoclosure () -> ValueConversionContext?) -> Value?
|
||||
{
|
||||
return Value.decodeIfPresent(from: impl.databaseValue(atUncheckedIndex: index), conversionContext: conversionContext)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
func decode<Value: DatabaseValueConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
conversionContext: @autoclosure () -> ValueConversionContext?) -> Value
|
||||
{
|
||||
return Value.decode(from: impl.databaseValue(atUncheckedIndex: index), conversionContext: conversionContext)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - DatabaseValueConvertible & StatementColumnConvertible
|
||||
|
||||
extension DatabaseValueConvertible where Self: StatementColumnConvertible {
|
||||
/// Performs lossless conversion from a statement value.
|
||||
@inline(__always)
|
||||
static func decode(from sqliteStatement: SQLiteStatement, index: Int32, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Self {
|
||||
static func fastDecode(from sqliteStatement: SQLiteStatement, index: Int32, conversionContext: @autoclosure () -> ValueConversionContext?) -> Self {
|
||||
if sqlite3_column_type(sqliteStatement, index) == SQLITE_NULL {
|
||||
fatalConversionError(to: Self.self, from: .null, debugInfo: debugInfo())
|
||||
fatalConversionError(to: Self.self, from: .null, conversionContext: conversionContext())
|
||||
}
|
||||
return self.init(sqliteStatement: sqliteStatement, index: index)
|
||||
}
|
||||
|
||||
/// Performs lossless conversion from a statement value.
|
||||
@inline(__always)
|
||||
static func decodeIfPresent(from sqliteStatement: SQLiteStatement, index: Int32) -> Self? {
|
||||
static func fastDecodeIfPresent(from sqliteStatement: SQLiteStatement, index: Int32) -> Self? {
|
||||
if sqlite3_column_type(sqliteStatement, index) == SQLITE_NULL {
|
||||
return nil
|
||||
}
|
||||
@@ -175,46 +195,27 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible {
|
||||
}
|
||||
|
||||
extension Row {
|
||||
|
||||
@inline(__always)
|
||||
func decodeIfPresent<Value: DatabaseValueConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value?
|
||||
{
|
||||
return Value.decodeIfPresent(from: impl.databaseValue(atUncheckedIndex: index), debugInfo: debugInfo)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
func decode<Value: DatabaseValueConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value
|
||||
{
|
||||
return Value.decode(from: impl.databaseValue(atUncheckedIndex: index), debugInfo: debugInfo)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
func fastDecodeIfPresent<Value: DatabaseValueConvertible & StatementColumnConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value?
|
||||
conversionContext: @autoclosure () -> ValueConversionContext?) -> Value?
|
||||
{
|
||||
if let sqliteStatement = sqliteStatement {
|
||||
return Value.decodeIfPresent(from: sqliteStatement, index: Int32(index))
|
||||
return Value.fastDecodeIfPresent(from: sqliteStatement, index: Int32(index))
|
||||
}
|
||||
return impl.fastDecodeIfPresent(Value.self, atUncheckedIndex: index, debugInfo: debugInfo)
|
||||
return impl.fastDecodeIfPresent(Value.self, atUncheckedIndex: index, conversionContext: conversionContext)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
func fastDecode<Value: DatabaseValueConvertible & StatementColumnConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value
|
||||
conversionContext: @autoclosure () -> ValueConversionContext?) -> Value
|
||||
{
|
||||
if let sqliteStatement = sqliteStatement {
|
||||
return Value.decode(from: sqliteStatement, index: Int32(index), debugInfo: debugInfo)
|
||||
return Value.fastDecode(from: sqliteStatement, index: Int32(index), conversionContext: conversionContext)
|
||||
}
|
||||
return impl.fastDecode(Value.self, atUncheckedIndex: index, debugInfo: debugInfo)
|
||||
return impl.fastDecode(Value.self, atUncheckedIndex: index, conversionContext: conversionContext)
|
||||
}
|
||||
}
|
||||
@@ -72,7 +72,7 @@ public final class DatabaseValueCursor<Value: DatabaseValueConvertible> : Cursor
|
||||
let dbValue = DatabaseValue(sqliteStatement: sqliteStatement, index: columnIndex)
|
||||
return Value.decode(
|
||||
from: dbValue,
|
||||
debugInfo: ValueConversionDebuggingInfo(.statement(statement), .columnIndex(Int(columnIndex))))
|
||||
conversionContext: ValueConversionContext(statement).atColumn(Int(columnIndex)))
|
||||
case let code:
|
||||
statement.database.selectStatementDidFail(statement)
|
||||
throw DatabaseError(
|
||||
@@ -118,7 +118,7 @@ public final class NullableDatabaseValueCursor<Value: DatabaseValueConvertible>
|
||||
let dbValue = DatabaseValue(sqliteStatement: sqliteStatement, index: columnIndex)
|
||||
return Value.decodeIfPresent(
|
||||
from: dbValue,
|
||||
debugInfo: ValueConversionDebuggingInfo(.statement(statement), .columnIndex(Int(columnIndex))))
|
||||
conversionContext: ValueConversionContext(statement).atColumn(Int(columnIndex)))
|
||||
case let code:
|
||||
statement.database.selectStatementDidFail(statement)
|
||||
throw DatabaseError(
|
||||
|
||||
+56
-42
@@ -171,7 +171,7 @@ extension Row {
|
||||
/// in performance-critical code because it can avoid decoding database
|
||||
/// values.
|
||||
public func hasNull(atIndex index: Int) -> Bool {
|
||||
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
|
||||
let index = checkedIndex(index)
|
||||
return impl.hasNull(atUncheckedIndex: index)
|
||||
}
|
||||
|
||||
@@ -181,7 +181,7 @@ extension Row {
|
||||
/// Indexes span from 0 for the leftmost column to (row.count - 1) for the
|
||||
/// righmost column.
|
||||
public subscript(_ index: Int) -> DatabaseValueConvertible? {
|
||||
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
|
||||
let index = checkedIndex(index)
|
||||
return impl.databaseValue(atUncheckedIndex: index).storage.value
|
||||
}
|
||||
|
||||
@@ -194,11 +194,11 @@ extension Row {
|
||||
/// value is converted to the requested type `Value`. Should this conversion
|
||||
/// fail, a fatal error is raised.
|
||||
public subscript<Value: DatabaseValueConvertible>(_ index: Int) -> Value? {
|
||||
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
|
||||
let index = checkedIndex(index)
|
||||
return decodeIfPresent(
|
||||
Value.self,
|
||||
atUncheckedIndex: index,
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(self), .columnIndex(index)))
|
||||
conversionContext: ValueConversionContext(self).atColumn(index))
|
||||
}
|
||||
|
||||
/// Returns the value at given index, converted to the requested type.
|
||||
@@ -214,11 +214,11 @@ extension Row {
|
||||
/// StatementColumnConvertible. It *may* trigger SQLite built-in conversions
|
||||
/// (see https://www.sqlite.org/datatype3.html).
|
||||
public subscript<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ index: Int) -> Value? {
|
||||
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
|
||||
let index = checkedIndex(index)
|
||||
return fastDecodeIfPresent(
|
||||
Value.self,
|
||||
atUncheckedIndex: index,
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(self), .columnIndex(index)))
|
||||
conversionContext: ValueConversionContext(self).atColumn(index))
|
||||
}
|
||||
|
||||
/// Returns the value at given index, converted to the requested type.
|
||||
@@ -229,11 +229,11 @@ extension Row {
|
||||
/// This method crashes if the fetched SQLite value is NULL, or if the
|
||||
/// SQLite value can not be converted to `Value`.
|
||||
public subscript<Value: DatabaseValueConvertible>(_ index: Int) -> Value {
|
||||
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
|
||||
let index = checkedIndex(index)
|
||||
return decode(
|
||||
Value.self,
|
||||
atUncheckedIndex: index,
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(self), .columnIndex(index)))
|
||||
conversionContext: ValueConversionContext(self).atColumn(index))
|
||||
}
|
||||
|
||||
/// Returns the value at given index, converted to the requested type.
|
||||
@@ -248,11 +248,11 @@ extension Row {
|
||||
/// StatementColumnConvertible. It *may* trigger SQLite built-in conversions
|
||||
/// (see https://www.sqlite.org/datatype3.html).
|
||||
public subscript<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ index: Int) -> Value {
|
||||
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
|
||||
let index = checkedIndex(index)
|
||||
return fastDecode(
|
||||
Value.self,
|
||||
atUncheckedIndex: index,
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(self), .columnIndex(index)))
|
||||
conversionContext: ValueConversionContext(self).atColumn(index))
|
||||
}
|
||||
|
||||
/// Returns Int64, Double, String, Data or nil, depending on the value
|
||||
@@ -291,7 +291,7 @@ extension Row {
|
||||
return decodeIfPresent(
|
||||
Value.self,
|
||||
atUncheckedIndex: index,
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(self), .columnName(columnName)))
|
||||
conversionContext: ValueConversionContext(self).atColumn(columnName))
|
||||
}
|
||||
|
||||
/// Returns the value at given column, converted to the requested type.
|
||||
@@ -313,7 +313,7 @@ extension Row {
|
||||
return fastDecodeIfPresent(
|
||||
Value.self,
|
||||
atUncheckedIndex: index,
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(self), .columnName(columnName)))
|
||||
conversionContext: ValueConversionContext(self).atColumn(columnName))
|
||||
}
|
||||
|
||||
/// Returns the value at given column, converted to the requested type.
|
||||
@@ -331,12 +331,12 @@ extension Row {
|
||||
fatalConversionError(
|
||||
to: Value.self,
|
||||
from: nil,
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(self), .columnName(columnName)))
|
||||
conversionContext: ValueConversionContext(self).atColumn(columnName))
|
||||
}
|
||||
return decode(
|
||||
Value.self,
|
||||
atUncheckedIndex: index,
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(self), .columnName(columnName)))
|
||||
conversionContext: ValueConversionContext(self).atColumn(columnName))
|
||||
}
|
||||
|
||||
/// Returns the value at given column, converted to the requested type.
|
||||
@@ -358,12 +358,12 @@ extension Row {
|
||||
fatalConversionError(
|
||||
to: Value.self,
|
||||
from: nil,
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(self), .columnName(columnName)))
|
||||
conversionContext: ValueConversionContext(self).atColumn(columnName))
|
||||
}
|
||||
return fastDecode(
|
||||
Value.self,
|
||||
atUncheckedIndex: index,
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(self), .columnName(columnName)))
|
||||
conversionContext: ValueConversionContext(self).atColumn(columnName))
|
||||
}
|
||||
|
||||
/// Returns Int64, Double, String, NSData or nil, depending on the value
|
||||
@@ -446,10 +446,10 @@ extension Row {
|
||||
/// The returned data does not owns its bytes: it must not be used longer
|
||||
/// than the row's lifetime.
|
||||
public func dataNoCopy(atIndex index: Int) -> Data? {
|
||||
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
|
||||
let index = checkedIndex(index)
|
||||
return impl.dataNoCopy(
|
||||
atUncheckedIndex: index,
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(self), .columnIndex(index)))
|
||||
conversionContext: ValueConversionContext(self).atColumn(index))
|
||||
}
|
||||
|
||||
/// Returns the optional Data at given column.
|
||||
@@ -469,7 +469,7 @@ extension Row {
|
||||
}
|
||||
return impl.dataNoCopy(
|
||||
atUncheckedIndex: index,
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(self), .columnName(columnName)))
|
||||
conversionContext: ValueConversionContext(self).atColumn(columnName))
|
||||
}
|
||||
|
||||
/// Returns the optional `NSData` at given column.
|
||||
@@ -486,6 +486,12 @@ extension Row {
|
||||
public func dataNoCopy(_ column: ColumnExpression) -> Data? {
|
||||
return dataNoCopy(named: column.name)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
private func checkedIndex(_ index: Int, file: StaticString = #file, line: UInt = #line) -> Int {
|
||||
GRDBPrecondition(index >= 0 && index < count, "row index out of range", file: file, line: line)
|
||||
return index
|
||||
}
|
||||
}
|
||||
|
||||
extension Row {
|
||||
@@ -940,8 +946,7 @@ extension Row {
|
||||
|
||||
/// Accesses the (ColumnName, DatabaseValue) pair at given index.
|
||||
public subscript(position: RowIndex) -> (String, DatabaseValue) {
|
||||
let index = position.index
|
||||
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
|
||||
let index = checkedIndex(position.index)
|
||||
return (
|
||||
impl.columnName(atUncheckedIndex: index),
|
||||
impl.databaseValue(atUncheckedIndex: index))
|
||||
@@ -1211,9 +1216,9 @@ protocol RowImpl {
|
||||
func columnName(atUncheckedIndex index: Int) -> String
|
||||
func hasNull(atUncheckedIndex index:Int) -> Bool
|
||||
func databaseValue(atUncheckedIndex index: Int) -> DatabaseValue
|
||||
func fastDecode<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ type: Value.Type, atUncheckedIndex index: Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value
|
||||
func fastDecodeIfPresent<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ type: Value.Type, atUncheckedIndex index: Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value?
|
||||
func dataNoCopy(atUncheckedIndex index:Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Data?
|
||||
func fastDecode<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ type: Value.Type, atUncheckedIndex index: Int, conversionContext: @autoclosure () -> ValueConversionContext?) -> Value
|
||||
func fastDecodeIfPresent<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ type: Value.Type, atUncheckedIndex index: Int, conversionContext: @autoclosure () -> ValueConversionContext?) -> Value?
|
||||
func dataNoCopy(atUncheckedIndex index:Int, conversionContext: @autoclosure () -> ValueConversionContext?) -> Data?
|
||||
|
||||
/// Returns the index of the leftmost column that matches *name* (case-insensitive)
|
||||
func index(ofColumn name: String) -> Int?
|
||||
@@ -1226,49 +1231,55 @@ protocol RowImpl {
|
||||
|
||||
extension RowImpl {
|
||||
func copiedRow(_ row: Row) -> Row {
|
||||
// unless customized, assume immutable row (see StatementRowImpl and AdaptedRowImpl for customization)
|
||||
return row
|
||||
}
|
||||
|
||||
func unscopedRow(_ row: Row) -> Row {
|
||||
// unless customized, assume unadapted row (see AdaptedRowImpl for customization)
|
||||
return row
|
||||
}
|
||||
|
||||
func unadaptedRow(_ row: Row) -> Row {
|
||||
// unless customized, assume unadapted row (see AdaptedRowImpl for customization)
|
||||
return row
|
||||
}
|
||||
|
||||
var scopes: Row.ScopesView {
|
||||
// unless customized, assume unuscoped row (see AdaptedRowImpl for customization)
|
||||
return Row.ScopesView()
|
||||
}
|
||||
|
||||
func hasNull(atUncheckedIndex index:Int) -> Bool {
|
||||
// unless customized, use slow check (see StatementRowImpl and AdaptedRowImpl for customization)
|
||||
return databaseValue(atUncheckedIndex: index).isNull
|
||||
}
|
||||
|
||||
func fastDecode<Value: DatabaseValueConvertible & StatementColumnConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value
|
||||
conversionContext: @autoclosure () -> ValueConversionContext?) -> Value
|
||||
{
|
||||
// default implementation is slow
|
||||
return Value.decode(from: databaseValue(atUncheckedIndex: index), debugInfo: debugInfo)
|
||||
// unless customized, use slow decoding (see StatementRowImpl and AdaptedRowImpl for customization)
|
||||
return Value.decode(from: databaseValue(atUncheckedIndex: index), conversionContext: conversionContext)
|
||||
}
|
||||
|
||||
func fastDecodeIfPresent<Value: DatabaseValueConvertible & StatementColumnConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value?
|
||||
conversionContext: @autoclosure () -> ValueConversionContext?) -> Value?
|
||||
{
|
||||
// default implementation is slow
|
||||
return Value.decodeIfPresent(from: databaseValue(atUncheckedIndex: index), debugInfo: debugInfo)
|
||||
// unless customized, use slow decoding (see StatementRowImpl and AdaptedRowImpl for customization)
|
||||
return Value.decodeIfPresent(from: databaseValue(atUncheckedIndex: index), conversionContext: conversionContext)
|
||||
}
|
||||
|
||||
func dataNoCopy(atUncheckedIndex index:Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Data? {
|
||||
// default implementation copies
|
||||
return Data.decodeIfPresent(from: databaseValue(atUncheckedIndex: index), debugInfo: debugInfo)
|
||||
func dataNoCopy(atUncheckedIndex index:Int, conversionContext: @autoclosure () -> ValueConversionContext?) -> Data? {
|
||||
// unless customized, copy data (see StatementRowImpl and AdaptedRowImpl for customization)
|
||||
return Data.decodeIfPresent(from: databaseValue(atUncheckedIndex: index), conversionContext: conversionContext)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: merge with StatementCopyRowImpl eventually?
|
||||
/// See Row.init(dictionary:)
|
||||
private struct ArrayRowImpl : RowImpl {
|
||||
let columns: [(String, DatabaseValue)]
|
||||
@@ -1300,6 +1311,7 @@ private struct ArrayRowImpl : RowImpl {
|
||||
}
|
||||
|
||||
|
||||
// TODO: merge with ArrayRowImpl eventually?
|
||||
/// See Row.init(copiedFromStatementRef:sqliteStatement:)
|
||||
private struct StatementCopyRowImpl : RowImpl {
|
||||
let dbValues: ContiguousArray<DatabaseValue>
|
||||
@@ -1343,7 +1355,7 @@ private struct StatementRowImpl : RowImpl {
|
||||
init(sqliteStatement: SQLiteStatement, statementRef: Unmanaged<SelectStatement>) {
|
||||
self.statementRef = statementRef
|
||||
self.sqliteStatement = sqliteStatement
|
||||
// Optimize row["..."]
|
||||
// Optimize row[columnName]
|
||||
let lowercaseColumnNames = (0..<sqlite3_column_count(sqliteStatement)).map { String(cString: sqlite3_column_name(sqliteStatement, Int32($0))).lowercased() }
|
||||
self.lowercaseColumnIndexes = Dictionary(
|
||||
lowercaseColumnNames
|
||||
@@ -1361,11 +1373,11 @@ private struct StatementRowImpl : RowImpl {
|
||||
}
|
||||
|
||||
func hasNull(atUncheckedIndex index:Int) -> Bool {
|
||||
// Avoid extracting values, because this modifies the statement.
|
||||
// Avoid extracting values, because this modifies the SQLite statement.
|
||||
return sqlite3_column_type(sqliteStatement, Int32(index)) == SQLITE_NULL
|
||||
}
|
||||
|
||||
func dataNoCopy(atUncheckedIndex index:Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Data? {
|
||||
func dataNoCopy(atUncheckedIndex index:Int, conversionContext: @autoclosure () -> ValueConversionContext?) -> Data? {
|
||||
guard sqlite3_column_type(sqliteStatement, Int32(index)) != SQLITE_NULL else {
|
||||
return nil
|
||||
}
|
||||
@@ -1380,18 +1392,20 @@ private struct StatementRowImpl : RowImpl {
|
||||
return DatabaseValue(sqliteStatement: sqliteStatement, index: Int32(index))
|
||||
}
|
||||
|
||||
func fastValue<Value: DatabaseValueConvertible & StatementColumnConvertible>(
|
||||
func fastDecode<Value: DatabaseValueConvertible & StatementColumnConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value
|
||||
conversionContext: @autoclosure () -> ValueConversionContext?) -> Value
|
||||
{
|
||||
return Value.decode(from: sqliteStatement, index: Int32(index), debugInfo: debugInfo)
|
||||
return Value.fastDecode(from: sqliteStatement, index: Int32(index), conversionContext: conversionContext)
|
||||
}
|
||||
|
||||
func fastOptionalValue<Value: DatabaseValueConvertible & StatementColumnConvertible>(
|
||||
func fastDecodeIfPresent<Value: DatabaseValueConvertible & StatementColumnConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value?
|
||||
conversionContext: @autoclosure () -> ValueConversionContext?) -> Value?
|
||||
{
|
||||
return Value.decodeIfPresent(from: sqliteStatement, index: Int32(index))
|
||||
return Value.fastDecodeIfPresent(from: sqliteStatement, index: Int32(index))
|
||||
}
|
||||
|
||||
func columnName(atUncheckedIndex index: Int) -> String {
|
||||
|
||||
@@ -504,27 +504,24 @@ struct AdaptedRowImpl : RowImpl {
|
||||
func fastDecode<Value: DatabaseValueConvertible & StatementColumnConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value
|
||||
conversionContext: @autoclosure () -> ValueConversionContext?) -> Value
|
||||
{
|
||||
let mappedIndex = mapping.baseColumnIndex(atMappingIndex: index)
|
||||
return base.impl.fastDecode(Value.self, atUncheckedIndex: mappedIndex, debugInfo: debugInfo) // base.impl: Demeter violation
|
||||
return base.impl.fastDecode(Value.self, atUncheckedIndex: mappedIndex, conversionContext: conversionContext) // base.impl: Demeter violation
|
||||
}
|
||||
|
||||
func fastDecodeIfPresent<Value: DatabaseValueConvertible & StatementColumnConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value?
|
||||
conversionContext: @autoclosure () -> ValueConversionContext?) -> Value?
|
||||
{
|
||||
let mappedIndex = mapping.baseColumnIndex(atMappingIndex: index)
|
||||
return base.impl.fastDecodeIfPresent(Value.self, atUncheckedIndex: mappedIndex, debugInfo: debugInfo) // base.impl: Demeter violation
|
||||
return base.impl.fastDecodeIfPresent(Value.self, atUncheckedIndex: mappedIndex, conversionContext: conversionContext) // base.impl: Demeter violation
|
||||
}
|
||||
|
||||
func dataNoCopy(
|
||||
atUncheckedIndex index:Int,
|
||||
debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Data?
|
||||
{
|
||||
func dataNoCopy(atUncheckedIndex index:Int, conversionContext: @autoclosure () -> ValueConversionContext?) -> Data? {
|
||||
let mappedIndex = mapping.baseColumnIndex(atMappingIndex: index)
|
||||
return base.impl.dataNoCopy(atUncheckedIndex: mappedIndex, debugInfo: debugInfo) // base.impl: Demeter violation
|
||||
return base.impl.dataNoCopy(atUncheckedIndex: mappedIndex, conversionContext: conversionContext) // base.impl: Demeter violation
|
||||
}
|
||||
|
||||
func columnName(atUncheckedIndex index: Int) -> String {
|
||||
|
||||
@@ -59,7 +59,7 @@ public protocol StatementColumnConvertible {
|
||||
/// print(name)
|
||||
/// }
|
||||
/// }
|
||||
public final class ColumnCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> : Cursor {
|
||||
public final class FastDatabaseValueCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> : Cursor {
|
||||
private let statement: SelectStatement
|
||||
private let sqliteStatement: SQLiteStatement
|
||||
private let columnIndex: Int32
|
||||
@@ -81,10 +81,10 @@ public final class ColumnCursor<Value: DatabaseValueConvertible & StatementColum
|
||||
done = true
|
||||
return nil
|
||||
case SQLITE_ROW:
|
||||
return Value.decode(
|
||||
return Value.fastDecode(
|
||||
from: sqliteStatement,
|
||||
index: columnIndex,
|
||||
debugInfo: ValueConversionDebuggingInfo(.statement(statement), .columnIndex(Int(columnIndex))))
|
||||
conversionContext: ValueConversionContext(statement).atColumn(Int(columnIndex)))
|
||||
case let code:
|
||||
statement.database.selectStatementDidFail(statement)
|
||||
throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments)
|
||||
@@ -92,6 +92,9 @@ public final class ColumnCursor<Value: DatabaseValueConvertible & StatementColum
|
||||
}
|
||||
}
|
||||
|
||||
@available(*, deprecated, renamed: "FastDatabaseValueCursor")
|
||||
public typealias ColumnCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> = FastDatabaseValueCursor<Value>
|
||||
|
||||
/// A cursor of optional database values extracted from a single column.
|
||||
/// For example:
|
||||
///
|
||||
@@ -101,7 +104,7 @@ public final class ColumnCursor<Value: DatabaseValueConvertible & StatementColum
|
||||
/// print(email ?? "<NULL>")
|
||||
/// }
|
||||
/// }
|
||||
public final class NullableColumnCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> : Cursor {
|
||||
public final class FastNullableDatabaseValueCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> : Cursor {
|
||||
private let statement: SelectStatement
|
||||
private let sqliteStatement: SQLiteStatement
|
||||
private let columnIndex: Int32
|
||||
@@ -123,7 +126,7 @@ public final class NullableColumnCursor<Value: DatabaseValueConvertible & Statem
|
||||
done = true
|
||||
return nil
|
||||
case SQLITE_ROW:
|
||||
return Value.decodeIfPresent(from: sqliteStatement, index: columnIndex)
|
||||
return Value.fastDecodeIfPresent(from: sqliteStatement, index: columnIndex)
|
||||
case let code:
|
||||
statement.database.selectStatementDidFail(statement)
|
||||
throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments)
|
||||
@@ -131,6 +134,9 @@ public final class NullableColumnCursor<Value: DatabaseValueConvertible & Statem
|
||||
}
|
||||
}
|
||||
|
||||
@available(*, deprecated, renamed: "FastNullableDatabaseValueCursor")
|
||||
public typealias NullableColumnCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> = FastNullableDatabaseValueCursor<Value>
|
||||
|
||||
/// Types that adopt both DatabaseValueConvertible and
|
||||
/// StatementColumnConvertible can be efficiently initialized from
|
||||
/// database values.
|
||||
@@ -160,8 +166,8 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible {
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: A cursor over fetched values.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> ColumnCursor<Self> {
|
||||
return try ColumnCursor(statement: statement, arguments: arguments, adapter: adapter)
|
||||
public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> FastDatabaseValueCursor<Self> {
|
||||
return try FastDatabaseValueCursor(statement: statement, arguments: arguments, adapter: adapter)
|
||||
}
|
||||
|
||||
/// Returns an array of values fetched from a prepared statement.
|
||||
@@ -192,7 +198,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible {
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
public static func fetchOne(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> Self? {
|
||||
// fetchOne returns nil if there is no row, or if there is a row with a null value
|
||||
let cursor = try NullableColumnCursor<Self>(statement: statement, arguments: arguments, adapter: adapter)
|
||||
let cursor = try FastNullableDatabaseValueCursor<Self>(statement: statement, arguments: arguments, adapter: adapter)
|
||||
return try cursor.next() ?? nil
|
||||
}
|
||||
}
|
||||
@@ -220,7 +226,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible {
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: A cursor over fetched values.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
public static func fetchCursor(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> ColumnCursor<Self> {
|
||||
public static func fetchCursor(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> FastDatabaseValueCursor<Self> {
|
||||
return try fetchCursor(db, SQLRequest<Void>(sql, arguments: arguments, adapter: adapter))
|
||||
}
|
||||
|
||||
@@ -277,7 +283,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible {
|
||||
/// - request: A FetchRequest.
|
||||
/// - returns: A cursor over fetched values.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
public static func fetchCursor<R: FetchRequest>(_ db: Database, _ request: R) throws -> ColumnCursor<Self> {
|
||||
public static func fetchCursor<R: FetchRequest>(_ db: Database, _ request: R) throws -> FastDatabaseValueCursor<Self> {
|
||||
let (statement, adapter) = try request.prepare(db)
|
||||
return try fetchCursor(statement, adapter: adapter)
|
||||
}
|
||||
@@ -333,7 +339,7 @@ extension FetchRequest where RowDecoder: DatabaseValueConvertible & StatementCol
|
||||
/// - parameter db: A database connection.
|
||||
/// - returns: A cursor over fetched values.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
public func fetchCursor(_ db: Database) throws -> ColumnCursor<RowDecoder> {
|
||||
public func fetchCursor(_ db: Database) throws -> FastDatabaseValueCursor<RowDecoder> {
|
||||
return try RowDecoder.fetchCursor(db, self)
|
||||
}
|
||||
|
||||
@@ -399,8 +405,8 @@ extension Optional where Wrapped: DatabaseValueConvertible & StatementColumnConv
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: A cursor over fetched optional values.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> NullableColumnCursor<Wrapped> {
|
||||
return try NullableColumnCursor(statement: statement, arguments: arguments, adapter: adapter)
|
||||
public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> FastNullableDatabaseValueCursor<Wrapped> {
|
||||
return try FastNullableDatabaseValueCursor(statement: statement, arguments: arguments, adapter: adapter)
|
||||
}
|
||||
|
||||
/// Returns an array of optional values fetched from a prepared statement.
|
||||
@@ -442,7 +448,7 @@ extension Optional where Wrapped: DatabaseValueConvertible & StatementColumnConv
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: A cursor over fetched optional values.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
public static func fetchCursor(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> NullableColumnCursor<Wrapped> {
|
||||
public static func fetchCursor(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> FastNullableDatabaseValueCursor<Wrapped> {
|
||||
return try fetchCursor(db, SQLRequest<Void>(sql, arguments: arguments, adapter: adapter))
|
||||
}
|
||||
|
||||
@@ -484,7 +490,7 @@ extension Optional where Wrapped: DatabaseValueConvertible & StatementColumnConv
|
||||
/// - request: A FetchRequest.
|
||||
/// - returns: A cursor over fetched optional values.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
public static func fetchCursor<R: FetchRequest>(_ db: Database, _ request: R) throws -> NullableColumnCursor<Wrapped> {
|
||||
public static func fetchCursor<R: FetchRequest>(_ db: Database, _ request: R) throws -> FastNullableDatabaseValueCursor<Wrapped> {
|
||||
let (statement, adapter) = try request.prepare(db)
|
||||
return try fetchCursor(statement, adapter: adapter)
|
||||
}
|
||||
@@ -525,7 +531,7 @@ extension FetchRequest where RowDecoder: _OptionalProtocol, RowDecoder._Wrapped:
|
||||
/// - parameter db: A database connection.
|
||||
/// - returns: A cursor over fetched values.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
public func fetchCursor(_ db: Database) throws -> NullableColumnCursor<RowDecoder._Wrapped> {
|
||||
public func fetchCursor(_ db: Database) throws -> FastNullableDatabaseValueCursor<RowDecoder._Wrapped> {
|
||||
return try Optional<RowDecoder._Wrapped>.fetchCursor(db, self)
|
||||
}
|
||||
|
||||
|
||||
@@ -75,7 +75,7 @@ private struct RowKeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContainer
|
||||
if let type = T.self as? DatabaseValueConvertible.Type {
|
||||
// Prefer DatabaseValueConvertible decoding over Decodable.
|
||||
// This allows decoding Date from String, or DatabaseValue from NULL.
|
||||
return type.decodeIfPresent(from: dbValue, debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName(key.stringValue))) as! T?
|
||||
return type.decodeIfPresent(from: dbValue, conversionContext: ValueConversionContext(row).atColumn(key.stringValue)) as! T?
|
||||
} else if dbValue.isNull {
|
||||
return nil
|
||||
} else {
|
||||
@@ -114,7 +114,7 @@ private struct RowKeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContainer
|
||||
if let type = T.self as? DatabaseValueConvertible.Type {
|
||||
// Prefer DatabaseValueConvertible decoding over Decodable.
|
||||
// This allows decoding Date from String, or DatabaseValue from NULL.
|
||||
return type.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName(key.stringValue))) as! T
|
||||
return type.decode(from: dbValue, conversionContext: ValueConversionContext(row).atColumn(key.stringValue)) as! T
|
||||
} else {
|
||||
return try T(from: RowDecoder(row: row, codingPath: codingPath + [key]))
|
||||
}
|
||||
@@ -227,7 +227,7 @@ private struct RowSingleValueDecodingContainer: SingleValueDecodingContainer {
|
||||
if let type = T.self as? DatabaseValueConvertible.Type {
|
||||
// Prefer DatabaseValueConvertible decoding over Decodable.
|
||||
// This allows decoding Date from String, or DatabaseValue from NULL.
|
||||
return type.decode(from: row[column.stringValue], debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName(column.stringValue))) as! T
|
||||
return type.decode(from: row[column.stringValue], conversionContext: ValueConversionContext(row).atColumn(column.stringValue)) as! T
|
||||
} else {
|
||||
return try T(from: RowDecoder(row: row, codingPath: [column]))
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ extension Optional : _OptionalProtocol {
|
||||
// MARK: - Internal
|
||||
|
||||
/// Reserved for GRDB: do not use.
|
||||
@inline(__always)
|
||||
func GRDBPrecondition(_ condition: @autoclosure() -> Bool, _ message: @autoclosure() -> String = "", file: StaticString = #file, line: UInt = #line) {
|
||||
/// Custom precondition function which aims at solving
|
||||
/// https://bugs.swift.org/browse/SR-905 and
|
||||
|
||||
@@ -205,7 +205,7 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
var cursor: ColumnCursor<Int>? = nil
|
||||
var cursor: FastDatabaseValueCursor<Int>? = nil
|
||||
do {
|
||||
try! makeDatabasePool().write { db in
|
||||
try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)")
|
||||
|
||||
@@ -120,7 +120,7 @@ class DatabaseQueueReleaseMemoryTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
var cursor: ColumnCursor<Int>? = nil
|
||||
var cursor: FastDatabaseValueCursor<Int>? = nil
|
||||
do {
|
||||
try! makeDatabaseQueue().inDatabase { db in
|
||||
try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)")
|
||||
|
||||
@@ -32,7 +32,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: row["name"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("name"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("name")),
|
||||
"could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL])")
|
||||
|
||||
// _ = try Record.fetchOne(statement)
|
||||
@@ -41,7 +41,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: row["name"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("name"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("name")),
|
||||
"could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL], statement: `SELECT ? AS name`, arguments: [NULL])")
|
||||
}
|
||||
}
|
||||
@@ -57,7 +57,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: row["name"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("name"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("name")),
|
||||
"could not read String from missing column `name` (row: [unused:\"ignored\"])")
|
||||
|
||||
// _ = try Record.fetchOne(statement)
|
||||
@@ -66,7 +66,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: row["name"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("name"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("name")),
|
||||
"could not read String from missing column `name` (row: [unused:\"ignored\"], statement: `SELECT ? AS unused`, arguments: [\"ignored\"])")
|
||||
}
|
||||
}
|
||||
@@ -98,7 +98,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not convert database value \"invalid\" to \(Value.self) (column: `value`, column index: 1, row: [1:1 value:\"invalid\"])")
|
||||
|
||||
// _ = try Record.fetchOne(statement)
|
||||
@@ -107,7 +107,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not convert database value \"invalid\" to \(Value.self) (column: `value`, column index: 1, row: [1:1 value:\"invalid\"], statement: `SELECT 1, ? AS value`, arguments: [\"invalid\"])")
|
||||
}
|
||||
}
|
||||
@@ -123,7 +123,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not read \(Value.self) from missing column `value` (row: [unused:\"ignored\"])")
|
||||
|
||||
// _ = try Record.fetchOne(statement)
|
||||
@@ -132,7 +132,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not read \(Value.self) from missing column `value` (row: [unused:\"ignored\"], statement: `SELECT ? AS unused`, arguments: [\"ignored\"])")
|
||||
}
|
||||
}
|
||||
@@ -157,7 +157,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: row["name"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("name"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("name")),
|
||||
"could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"])")
|
||||
|
||||
// _ = try Record.fetchOne(statement)
|
||||
@@ -166,7 +166,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: row["name"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("name"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("name")),
|
||||
"could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"], statement: `SELECT NULL AS name, ? AS team`, arguments: [\"invalid\"])")
|
||||
}
|
||||
}
|
||||
@@ -182,7 +182,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: row["name"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("name"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("name")),
|
||||
"could not read String from missing column `name` (row: [unused:\"ignored\"])")
|
||||
|
||||
// _ = try Record.fetchOne(statement)
|
||||
@@ -191,7 +191,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: row["name"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("name"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("name")),
|
||||
"could not read String from missing column `name` (row: [unused:\"ignored\"], statement: `SELECT ? AS unused`, arguments: [\"ignored\"])")
|
||||
}
|
||||
}
|
||||
@@ -219,7 +219,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not convert database value \"invalid\" to \(Value.self) (column: `value`, column index: 1, row: [name:NULL value:\"invalid\"])")
|
||||
|
||||
// _ = try Record.fetchOne(statement)
|
||||
@@ -228,7 +228,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not convert database value \"invalid\" to \(Value.self) (column: `value`, column index: 1, row: [name:NULL value:\"invalid\"], statement: `SELECT NULL AS name, ? AS value`, arguments: [\"invalid\"])")
|
||||
}
|
||||
}
|
||||
@@ -244,7 +244,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not read \(Value.self) from missing column `value` (row: [unused:\"ignored\"])")
|
||||
|
||||
// _ = try Record.fetchOne(statement)
|
||||
@@ -253,7 +253,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not read \(Value.self) from missing column `value` (row: [unused:\"ignored\"], statement: `SELECT ? AS unused`, arguments: [\"ignored\"])")
|
||||
}
|
||||
}
|
||||
@@ -281,7 +281,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not convert database value \"invalid\" to \(Value.self) (column: `value`, column index: 1, row: [name:NULL value:\"invalid\"])")
|
||||
|
||||
// _ = try Record.fetchOne(statement)
|
||||
@@ -290,7 +290,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not convert database value \"invalid\" to \(Value.self) (column: `value`, column index: 1, row: [name:NULL value:\"invalid\"], statement: `SELECT NULL AS name, ? AS value`, arguments: [\"invalid\"])")
|
||||
}
|
||||
}
|
||||
@@ -306,7 +306,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not read \(Value.self) from missing column `value` (row: [unused:\"ignored\"])")
|
||||
|
||||
// _ = try Record.fetchOne(statement)
|
||||
@@ -315,7 +315,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("value"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not read \(Value.self) from missing column `value` (row: [unused:\"ignored\"], statement: `SELECT ? AS unused`, arguments: [\"ignored\"])")
|
||||
}
|
||||
}
|
||||
@@ -336,7 +336,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: .null,
|
||||
debugInfo: ValueConversionDebuggingInfo(.statement(statement), .columnIndex(0))),
|
||||
conversionContext: ValueConversionContext(statement).atColumn(0)),
|
||||
"could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"], statement: `SELECT NULL AS name, ? AS team`, arguments: [\"invalid\"])")
|
||||
}
|
||||
|
||||
@@ -345,7 +345,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: row["name"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("name"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("name")),
|
||||
"could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"])")
|
||||
|
||||
// _ = row[0] as String
|
||||
@@ -353,7 +353,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: row[0],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnIndex(0))),
|
||||
conversionContext: ValueConversionContext(row).atColumn(0)),
|
||||
"could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"])")
|
||||
}
|
||||
}
|
||||
@@ -377,7 +377,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: DatabaseValue(sqliteStatement: statement.sqliteStatement, index: 0),
|
||||
debugInfo: ValueConversionDebuggingInfo(.statement(statement), .columnIndex(0))),
|
||||
conversionContext: ValueConversionContext(statement).atColumn(0)),
|
||||
"could not convert database value NULL to \(Value.self) (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"], statement: `SELECT NULL AS name, ? AS team`, arguments: [\"invalid\"])")
|
||||
}
|
||||
|
||||
@@ -389,7 +389,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: DatabaseValue(sqliteStatement: statement.sqliteStatement, index: Int32(columnIndex)),
|
||||
debugInfo: ValueConversionDebuggingInfo(.statement(statement), .columnIndex(columnIndex))),
|
||||
conversionContext: ValueConversionContext(statement).atColumn(columnIndex)),
|
||||
"could not convert database value \"invalid\" to \(Value.self) (column: `team`, column index: 1, row: [name:NULL team:\"invalid\"], statement: `SELECT NULL AS name, ? AS team`, arguments: [\"invalid\"])")
|
||||
}
|
||||
|
||||
@@ -398,7 +398,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["name"],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnName("name"))),
|
||||
conversionContext: ValueConversionContext(row).atColumn("name")),
|
||||
"could not convert database value NULL to \(Value.self) (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"])")
|
||||
|
||||
// _ = row[0] as Value
|
||||
@@ -406,7 +406,7 @@ class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row[0],
|
||||
debugInfo: ValueConversionDebuggingInfo(.row(row), .columnIndex(0))),
|
||||
conversionContext: ValueConversionContext(row).atColumn(0)),
|
||||
"could not convert database value NULL to \(Value.self) (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"])")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase {
|
||||
func testFetchCursor() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
func test(_ cursor: ColumnCursor<Fetched>) throws {
|
||||
func test(_ cursor: FastDatabaseValueCursor<Fetched>) throws {
|
||||
var i = try cursor.next()!
|
||||
XCTAssertEqual(i.int, 1)
|
||||
XCTAssertTrue(i.fast)
|
||||
@@ -120,7 +120,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase {
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
func test(_ cursor: ColumnCursor<Fetched>, sql: String) throws {
|
||||
func test(_ cursor: FastDatabaseValueCursor<Fetched>, sql: String) throws {
|
||||
do {
|
||||
_ = try cursor.next()
|
||||
XCTFail()
|
||||
@@ -161,7 +161,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase {
|
||||
func testFetchCursorCompilationFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
func test(_ cursor: @autoclosure () throws -> ColumnCursor<Fetched>, sql: String) throws {
|
||||
func test(_ cursor: @autoclosure () throws -> FastDatabaseValueCursor<Fetched>, sql: String) throws {
|
||||
do {
|
||||
_ = try cursor()
|
||||
XCTFail()
|
||||
@@ -426,7 +426,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase {
|
||||
func testOptionalFetchCursor() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
func test(_ cursor: NullableColumnCursor<Fetched>) throws {
|
||||
func test(_ cursor: FastNullableDatabaseValueCursor<Fetched>) throws {
|
||||
let i = try cursor.next()!
|
||||
XCTAssertEqual(i!.int, 1)
|
||||
XCTAssertTrue(i!.fast)
|
||||
@@ -456,7 +456,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase {
|
||||
func testOptionalFetchCursorCompilationFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
func test(_ cursor: @autoclosure () throws -> NullableColumnCursor<Fetched>, sql: String) throws {
|
||||
func test(_ cursor: @autoclosure () throws -> FastNullableDatabaseValueCursor<Fetched>, sql: String) throws {
|
||||
do {
|
||||
_ = try cursor()
|
||||
XCTFail()
|
||||
|
||||
Reference in new issue
Block a user