From 481f8d7d4ca106f99b83e4efba4c8070f588748c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= Date: Thu, 18 Dec 2014 09:48:45 +0100 Subject: [PATCH] In build view, only show meta of builds for the branch of the selected build. --- PHPCI/Controller/BuildController.php | 2 +- PHPCI/Store/BuildStore.php | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/PHPCI/Controller/BuildController.php b/PHPCI/Controller/BuildController.php index cc2f9cb3..18377ee9 100644 --- a/PHPCI/Controller/BuildController.php +++ b/PHPCI/Controller/BuildController.php @@ -120,7 +120,7 @@ class BuildController extends \PHPCI\Controller $data = null; if ($key && $build) { - $data = $this->buildStore->getMeta($key, $build->getProjectId(), $buildId, $numBuilds); + $data = $this->buildStore->getMeta($key, $build->getProjectId(), $buildId, $build->getBranch(), $numBuilds); } die(json_encode($data)); diff --git a/PHPCI/Store/BuildStore.php b/PHPCI/Store/BuildStore.php index fc445f54..4f849fa6 100644 --- a/PHPCI/Store/BuildStore.php +++ b/PHPCI/Store/BuildStore.php @@ -114,21 +114,28 @@ class BuildStore extends BuildStoreBase * @param $key * @param $projectId * @param null $buildId + * @param null $branch * @param int $numResults * @return array|null */ - public function getMeta($key, $projectId, $buildId = null, $numResults = 1) + public function getMeta($key, $projectId, $buildId = null, $branch = null, $numResults = 1) { - $select = '`build_id`, `meta_key`, `meta_value`'; - $and = $numResults > 1 ? ' AND (`build_id` <= :buildId) ' : ' AND (`build_id` = :buildId) '; - $where = '`meta_key` = :key AND `project_id` = :projectId ' . $and; - $query = 'SELECT '.$select.' FROM `build_meta` WHERE '.$where.' ORDER BY id DESC LIMIT :numResults'; + $select = '`bm`.`build_id`, `bm`.`meta_key`, `bm`.`meta_value`'; + $and = $numResults > 1 ? ' AND (`bm`.`build_id` <= :buildId) ' : ' AND (`bm`.`build_id` = :buildId) '; + $where = '`bm`.`meta_key` = :key AND `bm`.`project_id` = :projectId ' . $and; + $from = ' `build_meta` AS `bm`'; + if($branch !== null) { + $where .= ' AND `b`.`branch` = :branch AND `b`.`id`= `bm`.`build_id` '; + $from .= ', `build` AS `b`'; + } + $query = 'SELECT '.$select.' FROM '.$from.' WHERE '.$where.' ORDER BY `bm`.id DESC LIMIT :numResults'; $stmt = Database::getConnection('read')->prepare($query); $stmt->bindValue(':key', $key, \PDO::PARAM_STR); $stmt->bindValue(':projectId', (int)$projectId, \PDO::PARAM_INT); $stmt->bindValue(':buildId', (int)$buildId, \PDO::PARAM_INT); $stmt->bindValue(':numResults', (int)$numResults, \PDO::PARAM_INT); + $stmt->bindValue(':branch', $branch, \PDO::PARAM_STR); if ($stmt->execute()) { $rtn = $stmt->fetchAll(\PDO::FETCH_ASSOC);