<NME> QuerySet.php
<BEF> <?php

namespace Bolt\Storage;

use Doctrine\DBAL\Query\QueryBuilder;

/**
 * This class works keeps a set of queries that will eventually
 * be executed sequentially.
 *
 * @author Ross Riley <riley.ross@gmail.com>
 */
class QuerySet extends \ArrayIterator
{
    protected $resultCallbacks = [];
    protected $lastInsertId;
    protected $parentId;

    /**
     * @param QueryBuilder $qb A QueryBuilder instance
     */
    public function append($qb)
    {
        if (!$qb instanceof QueryBuilder) {
            throw new \InvalidArgumentException('QuerySet will only accept QueryBuilder instances', 1);
        }

        parent::append($qb);
    }

    /**
     * Execute function, iterate the queries, and execute them sequentially.
     *
     * @throws \Exception
     *
     * @return \Doctrine\DBAL\Driver\Statement|int|null
     */
    public function execute()
    {
        $result = null;
        // Only return the result of the primary query
        foreach ($this as $query) {
            /** @var QueryBuilder $query */
            try {
                if ($result === null) {
                    $result = $query->execute();
                    if ($query->getType() === 3) {
                        $this->setLastInsertId($query);
                    }
                    foreach ($this->resultCallbacks as $callback) {
                        $callback($query, $result, $this->getParentId());
                    }
                } else {
                    foreach ($this->resultCallbacks as $callback) {
                        $callback($query, $result, $this->getParentId());
                    }
                    $query->execute();
                }
            } catch (\Exception $e) {
                throw $e;
            }
        }

        return $result;
    }

    /**
     * Add a callback that gets run before a secondary query that passes the supplementary
     * query and the result of the first query into the callable.
     *
     * @param callable $callback [description]
     */
    public function onResult(callable $callback)
    {
        $this->resultCallbacks[] = $callback;
    }

    /**
     * Getter method to return last insert ID from query.
     *
     * @return int|null
     */
    public function getInsertId()
    {
        return $this->lastInsertId;
    }

    /**
     * @TODO Temporary workaround for PostgreSQL databases that don't use a sequence.
     *
     * @param QueryBuilder $query
     */
    private function setLastInsertId(QueryBuilder $query)
    {
        $seq = null;
        if ($query->getConnection()->getDatabasePlatform()->getName() === 'postgresql') {
            $sequences = $query->getConnection()->getSchemaManager()->listSequences();
            $desiredSeq = $query->getQueryPart('from')['table'] . '_id_seq';
            foreach ($sequences as $sequence) {
                if ($desiredSeq === $sequence->getName()) {
                    $seq = $desiredSeq;
                    break;
                }
    {
        $this->parentId = $parentId;
    }
}
        if (!$this->parentId && $this->lastInsertId) {
            return $this->lastInsertId;
        }

        return $this->parentId;
    }

    /**
     * @param mixed $parentId
     */
    public function setParentId($parentId)
    {
        $this->parentId = $parentId;
    }

    /**
     * A helper method to get the primary database query from a set. Normally this points to the first in the set.
     *
     * @return QueryBuilder
     */
    public function getPrimary()
    {
        return $this[0];
    }
}

<MSG> add a helper method to return the primary query from a set

<DFF> @@ -104,4 +104,13 @@ class QuerySet extends \ArrayIterator
     {
         $this->parentId = $parentId;
     }
+
+    /**
+     * A helper method to get the primary database query from a set. Normally this points to the first in the set
+     * @return QueryBuilder
+     */
+    public function getPrimary()
+    {
+        return $this[0];
+    }
 }

{'commit': 'diff --git a/src/Storage/QuerySet.php b/src/Storage/QuerySet.php\nindex a86888f69..352dbdc7b 100644\n--- a/src/Storage/QuerySet.php\n+++ b/src/Storage/QuerySet.php\n@@ -104,4 +104,13 @@ class QuerySet extends \\ArrayIterator\n     {\n         $this->parentId = $parentId;\n     }\n+\n+    /**\n+     * A helper method to get the primary database query from a set. Normally this points to the first in the set\n+     * @return QueryBuilder\n+     */\n+    public function getPrimary()\n+    {\n+        return $this[0];\n+    }\n }\n', 'commit_subject': 'add a helper method to return the primary query from a set', 'repo_name': 'bolt/bolt', 'committer': {'name': 'Ross Riley', 'email': 'eebce68ab99567722cee5644714a63dca807990d@gmail.com', 'time_sec': '1454746992', 'tz_offset': '0', 'date': {'seconds': '1454746992'}}, 'commit_tree': '1f6b7e29cf242df824dd967f68bff6018e0b8e43', 'commit_message': 'add a helper method to return the primary query from a set\n', 'language_name': 'Shell', 'license': 'mit', 'hunks': '@@ -104,4 +104,13 @@ class QuerySet extends \\ArrayIterator\n     {\n         $this->parentId = $parentId;\n     }\n+\n+    /**\n+     * A helper method to get the primary database query from a set. Normally this points to the first in the set\n+     * @return QueryBuilder\n+     */\n+    public function getPrimary()\n+    {\n+        return $this[0];\n+    }\n }\n', 'addition_count': 9, 'deletion_count': 0, 'src_file': 'a/src/Storage/QuerySet.php', 'tgt_file': 'b/src/Storage/QuerySet.php', 'file_extension': '.php', 'before_file': "<?php\n\nnamespace Bolt\\Storage;\n\nuse Doctrine\\DBAL\\Query\\QueryBuilder;\n\n/**\n * This class works keeps a set of queries that will eventually\n * be executed sequentially.\n *\n * @author Ross Riley <riley.ross@gmail.com>\n */\nclass QuerySet extends \\ArrayIterator\n{\n    protected $resultCallbacks = [];\n    protected $lastInsertId;\n    protected $parentId;\n\n    /**\n     * @param QueryBuilder $qb A QueryBuilder instance\n     */\n    public function append($qb)\n    {\n        if (!$qb instanceof QueryBuilder) {\n            throw new \\InvalidArgumentException('QuerySet will only accept QueryBuilder instances', 1);\n        }\n\n        parent::append($qb);\n    }\n\n    /**\n     * Execute function, iterate the queries, and execute them sequentially.\n     *\n     * @throws \\Exception\n     *\n     * @return \\Doctrine\\DBAL\\Driver\\Statement|int|null\n     */\n    public function execute()\n    {\n        $result = null;\n        // Only return the result of the primary query\n        foreach ($this as $query) {\n            /** @var QueryBuilder $query */\n            try {\n                if ($result === null) {\n                    $result = $query->execute();\n                    if ($query->getType() === 3) {\n                        $this->setLastInsertId($query);\n                    }\n                    foreach ($this->resultCallbacks as $callback) {\n                        $callback($query, $result, $this->getParentId());\n                    }\n                } else {\n                    foreach ($this->resultCallbacks as $callback) {\n                        $callback($query, $result, $this->getParentId());\n                    }\n                    $query->execute();\n                }\n            } catch (\\Exception $e) {\n                throw $e;\n            }\n        }\n\n        return $result;\n    }\n\n    /**\n     * Add a callback that gets run before a secondary query that passes the supplementary\n     * query and the result of the first query into the callable.\n     *\n     * @param callable $callback [description]\n     */\n    public function onResult(callable $callback)\n    {\n        $this->resultCallbacks[] = $callback;\n    }\n\n    /**\n     * Getter method to return last insert ID from query.\n     *\n     * @return int|null\n     */\n    public function getInsertId()\n    {\n        return $this->lastInsertId;\n    }\n\n    /**\n     * @TODO Temporary workaround for PostgreSQL databases that don't use a sequence.\n     *\n     * @param QueryBuilder $query\n     */\n    private function setLastInsertId(QueryBuilder $query)\n    {\n        $seq = null;\n        if ($query->getConnection()->getDatabasePlatform()->getName() === 'postgresql') {\n            $sequences = $query->getConnection()->getSchemaManager()->listSequences();\n            $desiredSeq = $query->getQueryPart('from')['table'] . '_id_seq';\n            foreach ($sequences as $sequence) {\n                if ($desiredSeq === $sequence->getName()) {\n                    $seq = $desiredSeq;\n                    break;\n                }\n    {\n        $this->parentId = $parentId;\n    }\n}\n        if (!$this->parentId && $this->lastInsertId) {\n            return $this->lastInsertId;\n        }\n\n        return $this->parentId;\n    }\n\n    /**\n     * @param mixed $parentId\n     */\n    public function setParentId($parentId)\n    {\n        $this->parentId = $parentId;\n    }\n\n    /**\n     * A helper method to get the primary database query from a set. Normally this points to the first in the set.\n     *\n     * @return QueryBuilder\n     */\n    public function getPrimary()\n    {\n        return $this[0];\n    }\n}\n", 'message': 'add a helper method to return the primary query from a set\n', 'author_name': 'Ross Riley', 'lang': 'php'}