2017-07-13 22:38:47 +02:00
|
|
|
|
//
|
2017-07-29 21:08:10 +02:00
|
|
|
|
// AuthorsTable.swift
|
2017-07-13 22:38:47 +02:00
|
|
|
|
// Database
|
|
|
|
|
//
|
|
|
|
|
// Created by Brent Simmons on 7/13/17.
|
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
2017-08-04 06:10:01 +02:00
|
|
|
|
import RSDatabase
|
2017-07-13 22:38:47 +02:00
|
|
|
|
import Data
|
|
|
|
|
|
2017-08-06 21:37:47 +02:00
|
|
|
|
// article->authors is a many-to-many relationship.
|
|
|
|
|
// There’s a lookup table relating authorID and articleID.
|
|
|
|
|
//
|
2017-08-21 02:46:15 +02:00
|
|
|
|
// CREATE TABLE if not EXISTS authors (authorID TEXT NOT NULL PRIMARY KEY, name TEXT, url TEXT, avatarURL TEXT, emailAddress TEXT);
|
2017-08-06 21:37:47 +02:00
|
|
|
|
// CREATE TABLE if not EXISTS authorLookup (authorID TEXT NOT NULL, articleID TEXT NOT NULL, PRIMARY KEY(authorID, articleID));
|
|
|
|
|
|
|
|
|
|
|
2017-08-21 00:56:58 +02:00
|
|
|
|
struct AuthorsTable: DatabaseTable {
|
|
|
|
|
|
2017-07-29 21:08:10 +02:00
|
|
|
|
let name: String
|
2017-08-21 02:46:15 +02:00
|
|
|
|
let databaseIDKey = DatabaseKey.authorID
|
|
|
|
|
private let cache = DatabaseObjectCache()
|
2017-07-29 21:08:10 +02:00
|
|
|
|
|
2017-08-21 00:56:58 +02:00
|
|
|
|
init(name: String) {
|
2017-07-29 21:08:10 +02:00
|
|
|
|
|
|
|
|
|
self.name = name
|
|
|
|
|
}
|
2017-08-21 00:56:58 +02:00
|
|
|
|
|
|
|
|
|
// MARK: DatabaseTable Methods
|
|
|
|
|
|
2017-08-21 02:46:15 +02:00
|
|
|
|
func objectWithRow(_ row: FMResultSet) -> DatabaseObject? {
|
2017-08-21 07:43:46 +02:00
|
|
|
|
|
|
|
|
|
if let author = authorWithRow(row) {
|
|
|
|
|
return author as DatabaseObject
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2017-08-21 00:56:58 +02:00
|
|
|
|
}
|
2017-08-21 07:43:46 +02:00
|
|
|
|
|
|
|
|
|
func save(_ objects: [DatabaseObject], in database: FMDatabase) {
|
|
|
|
|
// TODO
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-06 21:37:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private extension AuthorsTable {
|
|
|
|
|
|
|
|
|
|
func authorWithRow(_ row: FMResultSet) -> Author? {
|
2017-08-04 06:10:01 +02:00
|
|
|
|
|
2017-08-21 02:46:15 +02:00
|
|
|
|
guard let authorID = row.string(forColumn: DatabaseKey.authorID) else {
|
2017-08-04 06:10:01 +02:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-21 07:43:46 +02:00
|
|
|
|
if let cachedAuthor = cache[authorID] as? Author {
|
2017-08-04 06:10:01 +02:00
|
|
|
|
return cachedAuthor
|
2017-07-13 22:38:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-21 02:46:15 +02:00
|
|
|
|
guard let author = Author(authorID: authorID, row: row) else {
|
2017-07-13 22:38:47 +02:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-08-04 06:10:01 +02:00
|
|
|
|
|
2017-08-21 02:46:15 +02:00
|
|
|
|
cache[authorID] = author
|
2017-07-13 22:38:47 +02:00
|
|
|
|
return author
|
|
|
|
|
}
|
|
|
|
|
}
|