Values

  • A native data type that may be stored in an SQLite database.

    See more

    Declaration

    Swift

    public enum DatabaseValue
    extension DatabaseValue: Equatable
    extension DatabaseValue: ExpressibleByNilLiteral
    extension DatabaseValue: ExpressibleByIntegerLiteral
    extension DatabaseValue: ExpressibleByFloatLiteral
    extension DatabaseValue: ExpressibleByStringLiteral
    extension DatabaseValue: ExpressibleByBooleanLiteral
    extension DatabaseValue: CustomStringConvertible
    extension 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 Int64 is:

    extension Int64: ColumnConvertible {
        public init(_ stmt: SQLitePreparedStatement, column idx: Int32) {
           self = sqlite3_column_int64(stmt, idx)
        }
    }
    
    See more

    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 Int64 is:

    extension 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)
            }
        }
    }
    
    See more

    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 ParameterBindable and ColumnConvertible because 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 the sqlite3_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, NSNumber can choose what to store in the database based on the boxed value:

    extension 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")
            }
        }
    }
    
    See more

    Declaration

    Swift

    public protocol DatabaseSerializable : ParameterBindable