Create and use a DatabaseRelatedObjectsTable protocol, which clarifies the relationships between different table types.
This commit is contained in:
parent
a00ac9c2ac
commit
b783d1c062
|
@ -15,7 +15,6 @@ import Data
|
|||
final class ArticlesTable: DatabaseTable {
|
||||
|
||||
let name: String
|
||||
let databaseIDKey = DatabaseKey.articleID
|
||||
private weak var account: Account?
|
||||
private let queue: RSDatabaseQueue
|
||||
private let statusesTable: StatusesTable
|
||||
|
@ -44,21 +43,6 @@ final class ArticlesTable: DatabaseTable {
|
|||
self.attachmentsLookupTable = DatabaseLookupTable(name: DatabaseTableName.attachmentsLookup, objectIDKey: DatabaseKey.articleID, relatedObjectIDKey: DatabaseKey.attachmentID, relatedTable: attachmentsTable, relationshipName: RelationshipName.attachments)
|
||||
}
|
||||
|
||||
// MARK: DatabaseTable Methods
|
||||
|
||||
func objectWithRow(_ row: FMResultSet) -> DatabaseObject? {
|
||||
|
||||
if let article = articleWithRow(row) {
|
||||
return article as DatabaseObject
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func save(_ objects: [DatabaseObject], in database: FMDatabase) {
|
||||
|
||||
// TODO
|
||||
}
|
||||
|
||||
// MARK: Fetching
|
||||
|
||||
func fetchArticles(_ feed: Feed) -> Set<Article> {
|
||||
|
|
|
@ -10,7 +10,7 @@ import Foundation
|
|||
import RSDatabase
|
||||
import Data
|
||||
|
||||
struct AttachmentsTable: DatabaseTable {
|
||||
struct AttachmentsTable: DatabaseRelatedObjectsTable {
|
||||
|
||||
let name: String
|
||||
let databaseIDKey = DatabaseKey.attachmentID
|
||||
|
|
|
@ -17,7 +17,7 @@ import Data
|
|||
// CREATE TABLE if not EXISTS authorLookup (authorID TEXT NOT NULL, articleID TEXT NOT NULL, PRIMARY KEY(authorID, articleID));
|
||||
|
||||
|
||||
struct AuthorsTable: DatabaseTable {
|
||||
struct AuthorsTable: DatabaseRelatedObjectsTable {
|
||||
|
||||
let name: String
|
||||
let databaseIDKey = DatabaseKey.authorID
|
||||
|
|
|
@ -17,7 +17,7 @@ import Data
|
|||
// CREATE TABLE if not EXISTS tags(tagName TEXT NOT NULL, articleID TEXT NOT NULL, PRIMARY KEY(tagName, articleID));
|
||||
// CREATE INDEX if not EXISTS tags_tagName_index on tags (tagName COLLATE NOCASE);
|
||||
|
||||
struct TagsTable: DatabaseTable {
|
||||
struct TagsTable: DatabaseRelatedObjectsTable {
|
||||
|
||||
let name: String
|
||||
let databaseIDKey = DatabaseKey.tagName
|
||||
|
|
|
@ -11,32 +11,10 @@ import Foundation
|
|||
public protocol DatabaseTable {
|
||||
|
||||
var name: String { get }
|
||||
var databaseIDKey: String { get}
|
||||
|
||||
func fetchObjectsWithIDs(_ databaseIDs: Set<String>, in database: FMDatabase) -> [DatabaseObject]
|
||||
func objectsWithResultSet(_ resultSet: FMResultSet) -> [DatabaseObject]
|
||||
func objectWithRow(_ row: FMResultSet) -> DatabaseObject?
|
||||
|
||||
func save(_ objects: [DatabaseObject], in database: FMDatabase)
|
||||
}
|
||||
|
||||
public extension DatabaseTable {
|
||||
|
||||
// MARK: Default implementations
|
||||
|
||||
func fetchObjectsWithIDs(_ databaseIDs: Set<String>, in database: FMDatabase) -> [DatabaseObject] {
|
||||
|
||||
guard let resultSet = selectRowsWhere(key: databaseIDKey, inValues: Array(databaseIDs), in: database) else {
|
||||
return [DatabaseObject]()
|
||||
}
|
||||
return objectsWithResultSet(resultSet)
|
||||
}
|
||||
|
||||
func objectsWithResultSet(_ resultSet: FMResultSet) -> [DatabaseObject] {
|
||||
|
||||
return resultSet.flatMap(objectWithRow)
|
||||
}
|
||||
|
||||
// MARK: Fetching
|
||||
|
||||
public func selectRowsWhere(key: String, equals value: Any, in database: FMDatabase) -> FMResultSet? {
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
84419B051B5ABFF700C26BB2 /* FMResultSet+RSExtras.h in Headers */ = {isa = PBXBuildFile; fileRef = 84419B031B5ABFF700C26BB2 /* FMResultSet+RSExtras.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
84419B061B5ABFF700C26BB2 /* FMResultSet+RSExtras.m in Sources */ = {isa = PBXBuildFile; fileRef = 84419B041B5ABFF700C26BB2 /* FMResultSet+RSExtras.m */; };
|
||||
844D97411F2D32F300CEDDEA /* DatabaseObjectCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 844D97401F2D32F300CEDDEA /* DatabaseObjectCache.swift */; };
|
||||
844ECFB91F5B17F9005E405A /* DatabaseRelatedObjectsTable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 844ECFB81F5B17F9005E405A /* DatabaseRelatedObjectsTable.swift */; };
|
||||
849BF8C61C94FB8E0071D1DA /* libsqlite3.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 849BF8C51C94FB8E0071D1DA /* libsqlite3.tbd */; };
|
||||
84ABC1D11F364B07000DCC55 /* DatabaseLookupTable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84ABC1D01F364B07000DCC55 /* DatabaseLookupTable.swift */; };
|
||||
84ABC1D21F364B07000DCC55 /* DatabaseLookupTable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84ABC1D01F364B07000DCC55 /* DatabaseLookupTable.swift */; };
|
||||
|
@ -73,6 +74,7 @@
|
|||
84419B031B5ABFF700C26BB2 /* FMResultSet+RSExtras.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "FMResultSet+RSExtras.h"; path = "RSDatabase/FMResultSet+RSExtras.h"; sourceTree = "<group>"; };
|
||||
84419B041B5ABFF700C26BB2 /* FMResultSet+RSExtras.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "FMResultSet+RSExtras.m"; path = "RSDatabase/FMResultSet+RSExtras.m"; sourceTree = "<group>"; };
|
||||
844D97401F2D32F300CEDDEA /* DatabaseObjectCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = DatabaseObjectCache.swift; path = RSDatabase/DatabaseObjectCache.swift; sourceTree = "<group>"; };
|
||||
844ECFB81F5B17F9005E405A /* DatabaseRelatedObjectsTable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = DatabaseRelatedObjectsTable.swift; path = RSDatabase/DatabaseRelatedObjectsTable.swift; sourceTree = "<group>"; };
|
||||
849BF8C51C94FB8E0071D1DA /* libsqlite3.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libsqlite3.tbd; path = usr/lib/libsqlite3.tbd; sourceTree = SDKROOT; };
|
||||
84ABC1D01F364B07000DCC55 /* DatabaseLookupTable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = DatabaseLookupTable.swift; path = RSDatabase/DatabaseLookupTable.swift; sourceTree = "<group>"; };
|
||||
84C6DD001F395C13009AFB47 /* DatabaseObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = DatabaseObject.swift; path = RSDatabase/DatabaseObject.swift; sourceTree = "<group>"; };
|
||||
|
@ -160,8 +162,9 @@
|
|||
84419B041B5ABFF700C26BB2 /* FMResultSet+RSExtras.m */,
|
||||
84419AD81B5ABD7400C26BB2 /* NSString+RSDatabase.h */,
|
||||
84419AD91B5ABD7400C26BB2 /* NSString+RSDatabase.m */,
|
||||
840405DA1F1C158C00DF0296 /* DatabaseTable.swift */,
|
||||
84C6DD001F395C13009AFB47 /* DatabaseObject.swift */,
|
||||
840405DA1F1C158C00DF0296 /* DatabaseTable.swift */,
|
||||
844ECFB81F5B17F9005E405A /* DatabaseRelatedObjectsTable.swift */,
|
||||
84ABC1D01F364B07000DCC55 /* DatabaseLookupTable.swift */,
|
||||
844D97401F2D32F300CEDDEA /* DatabaseObjectCache.swift */,
|
||||
84DDF18A1C94FC45005E6CF5 /* FMDB */,
|
||||
|
@ -379,6 +382,7 @@
|
|||
84ABC1D11F364B07000DCC55 /* DatabaseLookupTable.swift in Sources */,
|
||||
84DDF1971C94FC45005E6CF5 /* FMDatabase.m in Sources */,
|
||||
84DDF1A01C94FC45005E6CF5 /* FMResultSet.m in Sources */,
|
||||
844ECFB91F5B17F9005E405A /* DatabaseRelatedObjectsTable.swift in Sources */,
|
||||
84419B061B5ABFF700C26BB2 /* FMResultSet+RSExtras.m in Sources */,
|
||||
84DDF1991C94FC45005E6CF5 /* FMDatabaseAdditions.m in Sources */,
|
||||
844D97411F2D32F300CEDDEA /* DatabaseObjectCache.swift in Sources */,
|
||||
|
|
|
@ -18,10 +18,10 @@ public final class DatabaseLookupTable {
|
|||
private let objectIDKey: String
|
||||
private let relatedObjectIDKey: String
|
||||
private let relationshipName: String
|
||||
private let relatedTable: DatabaseTable
|
||||
private let relatedTable: DatabaseRelatedObjectsTable
|
||||
private let cache: DatabaseLookupTableCache
|
||||
|
||||
public init(name: String, objectIDKey: String, relatedObjectIDKey: String, relatedTable: DatabaseTable, relationshipName: String) {
|
||||
public init(name: String, objectIDKey: String, relatedObjectIDKey: String, relatedTable: DatabaseRelatedObjectsTable, relationshipName: String) {
|
||||
|
||||
self.name = name
|
||||
self.objectIDKey = objectIDKey
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
//
|
||||
// DatabaseRelatedObjectsTable.swift
|
||||
// RSDatabase
|
||||
//
|
||||
// Created by Brent Simmons on 9/2/17.
|
||||
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public protocol DatabaseRelatedObjectsTable: DatabaseTable {
|
||||
|
||||
var databaseIDKey: String { get}
|
||||
|
||||
func fetchObjectsWithIDs(_ databaseIDs: Set<String>, in database: FMDatabase) -> [DatabaseObject]
|
||||
func objectsWithResultSet(_ resultSet: FMResultSet) -> [DatabaseObject]
|
||||
func objectWithRow(_ row: FMResultSet) -> DatabaseObject?
|
||||
|
||||
func save(_ objects: [DatabaseObject], in database: FMDatabase)
|
||||
}
|
||||
|
||||
public extension DatabaseRelatedObjectsTable {
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
//
|
||||
// DatabaseRelatedObjectsTable.swift
|
||||
// RSDatabase
|
||||
//
|
||||
// Created by Brent Simmons on 9/2/17.
|
||||
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public protocol DatabaseRelatedObjectsTable: DatabaseTable {
|
||||
|
||||
var databaseIDKey: String { get}
|
||||
|
||||
func fetchObjectsWithIDs(_ databaseIDs: Set<String>, in database: FMDatabase) -> [DatabaseObject]
|
||||
func objectsWithResultSet(_ resultSet: FMResultSet) -> [DatabaseObject]
|
||||
func objectWithRow(_ row: FMResultSet) -> DatabaseObject?
|
||||
|
||||
func save(_ objects: [DatabaseObject], in database: FMDatabase)
|
||||
}
|
||||
|
||||
public extension DatabaseRelatedObjectsTable {
|
||||
|
||||
// MARK: Default implementations
|
||||
|
||||
func fetchObjectsWithIDs(_ databaseIDs: Set<String>, in database: FMDatabase) -> [DatabaseObject] {
|
||||
|
||||
guard let resultSet = selectRowsWhere(key: databaseIDKey, inValues: Array(databaseIDs), in: database) else {
|
||||
return [DatabaseObject]()
|
||||
}
|
||||
return objectsWithResultSet(resultSet)
|
||||
}
|
||||
|
||||
func objectsWithResultSet(_ resultSet: FMResultSet) -> [DatabaseObject] {
|
||||
|
||||
return resultSet.flatMap(objectWithRow)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue