Values
-
A native data type that may be stored in an SQLite database.
See moreSeealso
Datatypes In SQLite Version 3Declaration
Swift
public enum DatabaseValueextension DatabaseValue: Equatableextension DatabaseValue: ExpressibleByNilLiteralextension DatabaseValue: ExpressibleByIntegerLiteralextension DatabaseValue: ExpressibleByFloatLiteralextension DatabaseValue: ExpressibleByStringLiteralextension DatabaseValue: ExpressibleByBooleanLiteralextension DatabaseValue: CustomStringConvertibleextension DatabaseValue: ParameterBindable -
A type that can be initialized directly from a column in an SQLite result row.
The implementation should use one of the
sqlite_column_X()functions documented at Result Values From A Query.For example, the implementation for
Int64is:
See moreextension Int64: ColumnConvertible { public init(_ stmt: SQLitePreparedStatement, column idx: Int32) { self = sqlite3_column_int64(stmt, idx) } }Declaration
Swift
public protocol ColumnConvertible -
A type that can bind its value directly to a parameter in an SQLite statement.
The implementation should use one of the
sqlite_bind_X()functions documented at Binding Values To Prepared Statements.For example, the implementation for
Int64is:
See moreextension Int64: ParameterBindable { public func bind(to stmt: SQLitePreparedStatement, parameter idx: Int32) throws { guard sqlite3_bind_int64(stmt, idx, self) == SQLITE_OK else { throw SQLiteError("Error binding Int64 \(self) to parameter \(idx)", takingDescriptionFromStatement: stmt) } } }Declaration
Swift
public protocol ParameterBindable -
A type that may be serialized to and deserialized from a database.
This is a more general method for database storage than
ParameterBindableandColumnConvertiblebecause it allows types to customize their behavior based on the database value’s data type. A database value’s data type is the value returned by thesqlite3_column_type()before any type conversions have taken place.Note
Columns in SQLite have a type affinity (declared type) while stored values have an individual storage class/data type. There are rules for conversion which are documented at Datatypes In SQLite Version 3.For example,
NSNumbercan choose what to store in the database based on the boxed value:
See moreextension NSNumber: DatabaseSerializable { public func serialized() -> DatabaseValue { switch CFNumberGetType(self as CFNumber) { case .sInt8Type, .sInt16Type, .sInt32Type, .charType, .shortType, .intType, .sInt64Type, .longType, .longLongType, .cfIndexType, .nsIntegerType: return DatabaseValue.integer(self.int64Value) case .float32Type, .float64Type, .floatType, .doubleType, .cgFloatType: return DatabaseValue.float(self.doubleValue) } } public static func deserialize(from value: DatabaseValue) throws -> Self { switch value { case .integer(let i): return self.init(value: i) case .float(let f): return self.init(value: f) default: throw DatabaseError("\(value) is not a number") } } }Declaration
Swift
public protocol DatabaseSerializable : ParameterBindable
View on GitHub
Values Reference