Make optionalIntForColumn an extension method on FMResultSet.

This commit is contained in:
Brent Simmons 2019-07-08 22:20:46 -07:00
parent c6907b73f9
commit 5d3cd8889f
1 changed files with 14 additions and 17 deletions

View File

@ -1,6 +1,6 @@
// //
// Attachment+Database.swift // Attachment+Database.swift
// Database // NetNewsWire
// //
// Created by Brent Simmons on 7/4/17. // Created by Brent Simmons on 7/4/17.
// Copyright © 2017 Ranchero Software. All rights reserved. // Copyright © 2017 Ranchero Software. All rights reserved.
@ -14,7 +14,6 @@ import RSParser
extension Attachment { extension Attachment {
init?(row: FMResultSet) { init?(row: FMResultSet) {
guard let url = row.string(forColumn: DatabaseKey.url) else { guard let url = row.string(forColumn: DatabaseKey.url) else {
return nil return nil
} }
@ -22,19 +21,17 @@ extension Attachment {
let attachmentID = row.string(forColumn: DatabaseKey.attachmentID) let attachmentID = row.string(forColumn: DatabaseKey.attachmentID)
let mimeType = row.string(forColumn: DatabaseKey.mimeType) let mimeType = row.string(forColumn: DatabaseKey.mimeType)
let title = row.string(forColumn: DatabaseKey.title) let title = row.string(forColumn: DatabaseKey.title)
let sizeInBytes = optionalIntForColumn(row, DatabaseKey.sizeInBytes) let sizeInBytes = row.optionalIntForColumn(DatabaseKey.sizeInBytes)
let durationInSeconds = optionalIntForColumn(row, DatabaseKey.durationInSeconds) let durationInSeconds = row.optionalIntForColumn(DatabaseKey.durationInSeconds)
self.init(attachmentID: attachmentID, url: url, mimeType: mimeType, title: title, sizeInBytes: sizeInBytes, durationInSeconds: durationInSeconds) self.init(attachmentID: attachmentID, url: url, mimeType: mimeType, title: title, sizeInBytes: sizeInBytes, durationInSeconds: durationInSeconds)
} }
init?(parsedAttachment: ParsedAttachment) { init?(parsedAttachment: ParsedAttachment) {
self.init(attachmentID: nil, url: parsedAttachment.url, mimeType: parsedAttachment.mimeType, title: parsedAttachment.title, sizeInBytes: parsedAttachment.sizeInBytes, durationInSeconds: parsedAttachment.durationInSeconds) self.init(attachmentID: nil, url: parsedAttachment.url, mimeType: parsedAttachment.mimeType, title: parsedAttachment.title, sizeInBytes: parsedAttachment.sizeInBytes, durationInSeconds: parsedAttachment.durationInSeconds)
} }
static func attachmentsWithParsedAttachments(_ parsedAttachments: Set<ParsedAttachment>?) -> Set<Attachment>? { static func attachmentsWithParsedAttachments(_ parsedAttachments: Set<ParsedAttachment>?) -> Set<Attachment>? {
guard let parsedAttachments = parsedAttachments else { guard let parsedAttachments = parsedAttachments else {
return nil return nil
} }
@ -42,17 +39,8 @@ extension Attachment {
let attachments = parsedAttachments.compactMap{ Attachment(parsedAttachment: $0) } let attachments = parsedAttachments.compactMap{ Attachment(parsedAttachment: $0) }
return attachments.isEmpty ? nil : Set(attachments) return attachments.isEmpty ? nil : Set(attachments)
} }
} }
private func optionalIntForColumn(_ row: FMResultSet, _ columnName: String) -> Int? {
let intValue = row.long(forColumn: columnName)
if intValue < 1 {
return nil
}
return intValue
}
extension Attachment: DatabaseObject { extension Attachment: DatabaseObject {
@ -79,15 +67,24 @@ extension Attachment: DatabaseObject {
} }
private extension FMResultSet {
func optionalIntForColumn(_ columnName: String) -> Int? {
let intValue = long(forColumn: columnName)
if intValue < 1 {
return nil
}
return intValue
}
}
extension Set where Element == Attachment { extension Set where Element == Attachment {
func databaseDictionaries() -> [DatabaseDictionary] { func databaseDictionaries() -> [DatabaseDictionary] {
return self.compactMap { $0.databaseDictionary() } return self.compactMap { $0.databaseDictionary() }
} }
func databaseObjects() -> [DatabaseObject] { func databaseObjects() -> [DatabaseObject] {
return self.compactMap { $0 as DatabaseObject } return self.compactMap { $0 as DatabaseObject }
} }
} }