public function update($id, array $record = null)
    {
        $model = is_a($id, $this->model_class) ? $id : null;
        if (null !== $model)
        {
            if ($record !== null)
                throw new DAOException("You should not pass an array of updates when supplying a Model");

            if ($id->getSourceDB() !== $this->db)
                throw new DAOException("Cannot update object - it did not originate in this database");

            $record = $id->getChanges();
            $id = $id->getID();
        }

        if ($record === null)
            throw new DAOException("No update provided");

        // Check if there's anything to update
        if (count($record) === 0)
            return 0;

        $table = $this->table;
        $columns = $table->getColumns();

        $pkey = $table->getPrimaryColumns();
        if ($pkey === null)
            throw new DAOException("Cannot update record without primary key");

        if (is_scalar($id) && count($pkey) === 1)
            foreach ($pkey as $colname => $def)
                $id = [$colname => $id];

        $condition = $this->getSelector($pkey, $id);

        // Remove primary key from the to-be-updated fields
        foreach ($pkey as $column)
            unset($record[$column->getName()]);

        $update = new Query\Update;
        $update->add(new Query\SourceTableClause($this->tablename));
        $update->add(new Query\WhereClause($condition));
        
        foreach ($record as $field => $value)
        {
            if (!isset($columns[$field]))
                throw new DAOException("Invalid field: " . $field);

            $coldef = $columns[$field];
            $value = $coldef->beforeInsertFilter($value);
            $update->add(new Query\UpdateField($field, $value));
        }

        $drv = $this->db->getDriver();
        $rows = $drv->update($update);

        if (null !== $model)
        {
            $model->markClean();
        }

        return $rows;
    }