     
     
     // MARK: - Configuration
-
-    /// The SQL query
-    public let sql: String?
-    
-    /// The FetchRequest
-    public let fetchRequest: FetchRequest<T>?
+    
+    /// The source
+    private let source: Source<T>
 
     /// The databaseWriter
     public let database: DatabaseWriter

    // MARK: - Not public
    
    func fetch() -> [T] {
        return database.read { db in self.fetchInDatabase(db) }
    }
    
    func fetchInDatabase(db: Database) -> [T] {
        if let fetchRequest = self.fetchRequest {
            return fetchRequest.fetchAll(db)
        } else if let sql = self.sql {
            return T.fetchAll(db, sql)
        } else {
            fatalError("Can't fetch without sql or fetchRequest")
        }
    }
    
    static func computeChanges(fromRows s: [T], toRows t: [T]) -> [ResultChange<T>] {


public typealias FetchedResult = protocol<RowConvertible, TableMapping, Equatable>

private enum Source<T: FetchedResult> {
    case SQL(String)
    case FetchRequest(GRDB.FetchRequest<T>)

    func fetchAll(db: DatabaseReader) -> [T] {
        switch self {
        case .SQL(let sql):
            return T.fetchAll(db, sql)
        case .FetchRequest(let fetchRequest):
            return fetchRequest.fetchAll(db)
        }
    }
}

public class FetchedResultsController<T: FetchedResult> {
    
    // MARK: - Initialization
    public init(database: DatabaseWriter, sql: String) {
        self.source = .SQL(sql)
        self.database = database
        database.addTransactionObserver(self)
    }
    
    public init(database: DatabaseWriter, fetchRequest: FetchRequest<T>) {
        self.source = .FetchRequest(fetchRequest)
        self.database = database
        database.addTransactionObserver(self)
    }
