         }
     }
     
-    private init(database: DatabaseWriter, source: DatabaseSource<T>, queue: dispatch_queue_t, isSameRecordBuilder: (Database) -> (T, T) -> Bool) {
+    private init(database: DatabaseWriter, source: DatabaseSource<Record>, queue: dispatch_queue_t, isSameRecordBuilder: (Database) -> (Record, Record) -> Bool) {
         self.source = source
         self.database = database
         self.isSameRecordBuilder = isSameRecordBuilder

    /// The sections
    ///
    /// MUST BE CALLED ON mainQueue (TODO: say it nicely)
    public var sections: [FetchedRecordsSectionInfo<T>] {
        // We only support a single section
        return [FetchedRecordsSectionInfo(controller: self)]
    }

//
import UIKit

/// You use a FetchedRecordsController to feed a UITableView with the results
/// returned from an SQLite request.
///
/// It looks and behaves very much like Core Data's NSFetchedResultsController.
///
/// Given a fetch request, and a type that adopts the RowConvertible protocol,
/// such as a subclass of the Record class, a FetchedRecordsController is able
/// to return the results of the request in a form that is suitable for a
/// UITableView, with one table view row per fetched record.
///
/// FetchedRecordsController can also monitor the results of the fetch request,
/// and notify its delegate of any change. Those changes can be easily turned
/// into animated table view deletions, insertions, updates and moves.
///
/// # Creating the Fetched Records Controller
///
/// You typically create an instance of FetchedRecordsController as a property
/// of a table view controller. When you initialize the fetch records
/// controller, you provide the following information:
///
/// - The type of the fetched records. It must be a type that adopts the
///   RowConvertible protocol, such as a subclass of the Record class.
///
/// - A fetch request. It can be a raw SQL query with its arguments, or a
///   FetchRequest from the GRDB Query Interface.
///
/// - Optionally, a way to tell if two records have the same identity. Without
///   this identity comparison, all record updates are seen as replacements,
///   and your table view updates are less smooth.
///
/// After creating an instance, you invoke `performFetch()` to actually execute
/// the fetch.
///
///     class Person : Record { ... }
///
///     let dbQueue = DatabaseQueue(...)
///     let request = Person.order(SQLColumn("name"))
///     let controller: FetchedRecordsController<Person> = FetchedRecordsController(
///         dbQueue,
///         request,
///         compareRecordsByPrimaryKey: true)
///     controller.performFetch()
///
/// In the example above, two persons are considered identical if they share
/// the same primary key, thanks to the `compareRecordsByPrimaryKey` argument.
/// This initializer argument is only available for types such as
/// Record subclasses that adopt the RowConvertible protocol, and also the
/// Persistable or MutablePersistable protocols.
///
/// If your type only adopts RowConvertible, you need to be more explicit, and
/// provide your own identity comparison function:
///
///     struct Person : RowConvertible {
///         let id: Int64
///         ...
///     }
///
///     let controller: FetchedRecordsController<Person> = FetchedRecordsController(
///         dbQueue,
///         request,
///         isSameRecord: { $0.id == $1.id })
///
/// Instead of a FetchRequest object, you can also provide a raw SQL query:
///
///     let controller: FetchedRecordsController<Person> = FetchedRecordsController(
///         dbQueue,
///         "SELECT * FROM persons ORDER BY name",
///         compareRecordsByPrimaryKey: true)
///
/// The fetch request can involve several database tables:
///
///     let controller: FetchedRecordsController<Person> = FetchedRecordsController(
///         dbQueue,
///         "SELECT persons.*, COUNT(books.id) AS bookCount " +
///         "FROM persons " +
///         "LEFT JOIN books ON books.owner_id = persons.id " +
///         "GROUP BY persons.id " +
///         "ORDER BY persons.name",
///         compareRecordsByPrimaryKey: true)
///
/// # The Controllers's Delegate
///
/// Any change in the database that affects the record set is processed and the
/// records are updated accordingly. The controller notifies the delegate when
/// records change location (see FetchedRecordsControllerDelegate). You
/// typically use these methods to update the display of the table view.
///
///
/// # Implementing the Table View Datasource Methods
///
/// The table view data source asks the fetched records controller to provide
/// relevant information:
///
///     func numberOfSectionsInTableView(tableView: UITableView) -> Int {
///         return fetchedRecordsController.sections.count
///     }
///
///     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
///         return fetchedRecordsController.sections[section].numberOfRecords
///     }
///
///     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
///         let cell = /* Get the cell */
///         let record = fetchedRecordsController.recordAtIndexPath(indexPath)
///         /* Configure the cell */
///         return cell
///     }
///
///
/// # Responding to Changes
///
/// In general, FetchedRecordsController is designed to respond to changes at
/// the database layer, by informing its delegate when database rows change
/// location or values.
///
/// Changes are not reflected until they are applied in the database by a
/// successful transaction. Transactions can be explicit, or implicit:
///
///     try dbQueue.inTransaction { db in
///         /* Change a person's attributes */
///         try person.save(db)
///         return .Commit      // Explicit transaction
///     }
///
///     try dbQueue.inDatavase { db in
///         /* Change a person's attributes */
///         try person.save(db) // Implicit transaction
///     }
///
/// When you apply several changes to the database, you should group them in a
/// single transaction. The controller will then notify its delegate of all
/// changes together.
///
public final class FetchedRecordsController<Record: RowConvertible> {
    
    // MARK: - Initialization
    
    // TODO: document that queue MUST be serial
    public convenience init(_ database: DatabaseWriter, _ sql: String, arguments: StatementArguments? = nil, queue: dispatch_queue_t = dispatch_get_main_queue(), isSameRecord: ((Record, Record) -> Bool)? = nil) {
        let source: DatabaseSource<Record> = .SQL(sql, arguments)
        self.init(database: database, source: source, queue: queue, isSameRecord: isSameRecord)
    }
    
    // TODO: document that queue MUST be serial
    public convenience init<T>(_ database: DatabaseWriter, _ request: FetchRequest<T>, queue: dispatch_queue_t = dispatch_get_main_queue(), isSameRecord: ((Record, Record) -> Bool)? = nil) {
        let request: FetchRequest<Record> = FetchRequest(query: request.query) // Retype the fetch request
        let source = DatabaseSource.FetchRequest(request)
        self.init(database: database, source: source, queue: queue, isSameRecord: isSameRecord)
    }
    
    private convenience init(database: DatabaseWriter, source: DatabaseSource<Record>, queue: dispatch_queue_t, isSameRecord: ((Record, Record) -> Bool)?) {
        if let isSameRecord = isSameRecord {
            self.init(database: database, source: source, queue: queue, isSameRecordBuilder: { _ in isSameRecord })
        } else {
