<commit_before>import Foundation

#if os(OSX)
    import SQLiteMacOSX
#elseif os(iOS)
#if (arch(i386) || arch(x86_64))
    import SQLiteiPhoneSimulator
    #else
    import SQLiteiPhoneOS
#endif
#endif

// MARK: - DatabaseValue

/// DatabaseValue is the intermediate type between SQLite and your values.
///
/// See https://www.sqlite.org/datatype3.html
public struct DatabaseValue {
    
    /// An SQLite storage (NULL, INTEGER, REAL, TEXT, BLOB).
    public enum Storage {
        /// The NULL storage class.
        case Null
        
        /// The INTEGER storage class, wrapping an Int64.
        case Int64(Swift.Int64)
        
        /// The REAL storage class, wrapping a Double.
        case Double(Swift.Double)
        
        /// The TEXT storage class, wrapping a String.
        case String(Swift.String)
        
        /// The BLOB storage class, wrapping NSData.
        case Blob(NSData)
    }
    
    /// The SQLite storage
    public let storage: Storage
    
    /// The NULL DatabaseValue.
    public static let Null = DatabaseValue(storage: .Null)
    
    
    // MARK: - Extracting Value
    
    /// Returns true if databaseValue is NULL.
    public var isNull: Bool {
        switch storage {
        case .Null:
            return true
        default:
            return false
        }
    }
    
    /// Returns Int64, Double, String, NSData or nil.
    public func value() -> DatabaseValueConvertible? {
        switch storage {
        case .Null:
            return nil
        case .Int64(let int64):
            return int64
        case .Double(let double):
            return double
        case .String(let string):
            return string
        case .Blob(let data):
            return data
        }
    }
    
    /// Returns the value, converted to the requested type.
    ///
    /// If the SQLite value is NULL, the result is nil. Otherwise the SQLite
    /// value is converted to the requested type `Value`. Should this conversion
    /// fail, a fatal error is raised.
    ///
    /// If this fatal error is unacceptable to you, use the
    /// failableValue() method.
    ///
    /// - returns: An optional *Value*.
    public func value<Value: DatabaseValueConvertible>() -> Value? {
        if let value = Value.fromDatabaseValue(self) {
            return value
        }
        precondition(isNull, "could not convert \(self) to \(Value.self).")
        return nil
    }
    
    /// Returns the value, converted to the requested type.
    ///
    /// This method crashes if the SQLite value is NULL, or if the SQLite value
    /// can not be converted to `Value`.
    ///
    /// - returns: A *Value*.
    public func value<Value: DatabaseValueConvertible>() -> Value {
        guard let value = Value.fromDatabaseValue(self) as Value? else {
            fatalError("could not convert \(self) to \(Value.self).")
        }
        return value
    }
    
    /// Returns the value, converted to the requested type.
    ///
    /// The result is nil if the SQLite value is NULL, or if the SQLite value
    /// can not be converted to `Value`.
    ///
    /// This method can return nil for non-NULL values. If this is unacceptable
    /// to you, use the value() method.
    ///
    /// - returns: An optional *Value*.
    public func failableValue<Value: DatabaseValueConvertible>() -> Value? {
        return Value.fromDatabaseValue(self)
    }
    
    
    // MARK: - Not Public
    
    init(storage: Storage) {
        // This initializer is not public because Storage is not a safe type:
        // one can create a Storage of zero-length NSData, which is invalid
        // because SQLite can't store zero-length blobs.
        self.storage = storage
    }
    
    // SQLite function argument
    init(sqliteValue: SQLiteValue) {
        switch sqlite3_value_type(sqliteValue) {
        case SQLITE_NULL:
            storage = .Null
        case SQLITE_INTEGER:
            storage = .Int64(sqlite3_value_int64(sqliteValue))
        case SQLITE_FLOAT:
            storage = .Double(sqlite3_value_double(sqliteValue))
        case SQLITE_TEXT:
            let cString = UnsafePointer<Int8>(sqlite3_value_text(sqliteValue))
            storage = .String(Swift.String.fromCString(cString)!)
        case SQLITE_BLOB:
            let bytes = sqlite3_value_blob(sqliteValue)
            let length = sqlite3_value_bytes(sqliteValue)<commit_msg>DatabaseValue.value() raises a fatal error when conversion of a non-NULL value fails.<commit_after>import Foundation

#if os(OSX)
    import SQLiteMacOSX
#elseif os(iOS)
#if (arch(i386) || arch(x86_64))
    import SQLiteiPhoneSimulator
    #else
    import SQLiteiPhoneOS
#endif
#endif

// MARK: - DatabaseValue

/// DatabaseValue is the intermediate type between SQLite and your values.
///
/// See https://www.sqlite.org/datatype3.html
public struct DatabaseValue {
    
    /// An SQLite storage (NULL, INTEGER, REAL, TEXT, BLOB).
    public enum Storage {
        /// The NULL storage class.
        case Null
        
        /// The INTEGER storage class, wrapping an Int64.
        case Int64(Swift.Int64)
        
        /// The REAL storage class, wrapping a Double.
        case Double(Swift.Double)
        
        /// The TEXT storage class, wrapping a String.
        case String(Swift.String)
        
        /// The BLOB storage class, wrapping NSData.
        case Blob(NSData)
    }
    
    /// The SQLite storage
    public let storage: Storage
    
    /// The NULL DatabaseValue.
    public static let Null = DatabaseValue(storage: .Null)
    
    
    // MARK: - Extracting Value
    
    /// Returns true if databaseValue is NULL.
    public var isNull: Bool {
        switch storage {
        case .Null:
            return true
        default:
            return false
        }
    }
    
    /// Returns Int64, Double, String, NSData or nil.
    public func value() -> DatabaseValueConvertible? {
        switch storage {
        case .Null:
            return nil
        case .Int64(let int64):
            return int64
        case .Double(let double):
            return double
        case .String(let string):
            return string
        case .Blob(let data):
            return data
        }
    }
    
    /// Returns the value, converted to the requested type.
    ///
    /// If the SQLite value is NULL, the result is nil. Otherwise the SQLite
    /// value is converted to the requested type `Value`. Should this conversion
    /// fail, a fatal error is raised.
    ///
    /// If this fatal error is unacceptable to you, use the
    /// failableValue() method.
    ///
    /// - returns: An optional *Value*.
    @warn_unused_result
    public func value<Value: DatabaseValueConvertible>() -> Value? {
        if let value = Value.fromDatabaseValue(self) {
            return value
        }
        guard isNull else {
            fatalError("could not convert \(self) to \(Value.self).")
        }
        return nil
    }
    
    /// Returns the value, converted to the requested type.
    ///
    /// This method crashes if the SQLite value is NULL, or if the SQLite value
    /// can not be converted to `Value`.
    ///
    /// - returns: A *Value*.
    @warn_unused_result
    public func value<Value: DatabaseValueConvertible>() -> Value {
        guard let value = Value.fromDatabaseValue(self) as Value? else {
            fatalError("could not convert \(self) to \(Value.self).")
        }
        return value
    }
    
    /// Returns the value, converted to the requested type.
    ///
    /// The result is nil if the SQLite value is NULL, or if the SQLite value
    /// can not be converted to `Value`.
    ///
    /// This method can return nil for non-NULL values. If this is unacceptable
    /// to you, use the value() method.
    ///
    /// - returns: An optional *Value*.
    @warn_unused_result
    public func failableValue<Value: DatabaseValueConvertible>() -> Value? {
        return Value.fromDatabaseValue(self)
    }
    
    
    // MARK: - Not Public
    
    init(storage: Storage) {
        // This initializer is not public because Storage is not a safe type:
        // one can create a Storage of zero-length NSData, which is invalid
        // because SQLite can't store zero-length blobs.
        self.storage = storage
    }
    
    // SQLite function argument
    init(sqliteValue: SQLiteValue) {
        switch sqlite3_value_type(sqliteValue) {
        case SQLITE_NULL:
            storage = .Null
        case SQLITE_INTEGER:
            storage = .Int64(sqlite3_value_int64(sqliteValue))
        case SQLITE_FLOAT:
            storage = .Double(sqlite3_value_double(sqliteValue))
        case SQLITE_TEXT:
            let cString = UnsafePointer<Int8>(sqlite3_value_text(sqliteValue))
            storage = .String(Swift.String.fromCString(cString)!)
        case SQLITE_BLOB:
            let bytes = sqlite3_value_blob(sqliteValue)
            let length = sqlite3_value_bytes(sqliteValue)
{'fraction_non_alphanumeric': 0.09362505553087516, 'fraction_numerical': 0.007330075521990226, 'mean_word_length': 1.8825224071702944, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3802726', 'n_tokens_mistral': 2510, 'n_tokens_neox': 2429, 'n_words': 845}