From c6ae04375049a3f5ad8bd191aad160f111fd5366 Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Thu, 9 Nov 2017 22:48:46 +0700 Subject: [PATCH] Fixed excessive BuildMeta inserts. --- src/B8Framework/Store.php | 2 +- src/PHPCensor/Store/BuildMetaStore.php | 35 ++++++++++++++++++++++++-- src/PHPCensor/Store/BuildStore.php | 30 +++++++++++----------- 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/src/B8Framework/Store.php b/src/B8Framework/Store.php index 2c295799..6ab9a9ff 100644 --- a/src/B8Framework/Store.php +++ b/src/B8Framework/Store.php @@ -53,7 +53,7 @@ abstract class Store $manualWheres = [], $whereType = 'AND' ) { - $query = 'SELECT * FROM {{' . $this->tableName . '}}'; + $query = 'SELECT * FROM {{' . $this->tableName . '}}'; $countQuery = 'SELECT COUNT(*) AS {{count}} FROM {{' . $this->tableName . '}}'; $wheres = []; diff --git a/src/PHPCensor/Store/BuildMetaStore.php b/src/PHPCensor/Store/BuildMetaStore.php index 56915cef..08529889 100644 --- a/src/PHPCensor/Store/BuildMetaStore.php +++ b/src/PHPCensor/Store/BuildMetaStore.php @@ -50,7 +50,7 @@ class BuildMetaStore extends Store public function getById($id, $useConnection = 'read') { if (is_null($id)) { - throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.'); + throw new HttpException('id passed to ' . __FUNCTION__ . ' cannot be null.'); } $query = 'SELECT * FROM {{build_meta}} WHERE {{id}} = :id LIMIT 1'; @@ -66,6 +66,38 @@ class BuildMetaStore extends Store return null; } + /** + * @param integer $buildId + * @param string $key + * + * @return null|BuildMeta + * + * @throws HttpException + */ + public function getByKey($buildId, $key) + { + if (is_null($buildId)) { + throw new HttpException('buildId passed to ' . __FUNCTION__ . ' cannot be null.'); + } + + if (!$key) { + throw new HttpException('key passed to ' . __FUNCTION__ . ' cannot be empty.'); + } + + $query = 'SELECT * FROM {{build_meta}} WHERE {{build_id}} = :build_id AND {{meta_key}} = :meta_key LIMIT 1'; + $stmt = Database::getConnection()->prepareCommon($query); + $stmt->bindValue(':build_id', $buildId); + $stmt->bindValue(':meta_key', $key); + + if ($stmt->execute()) { + if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) { + return new BuildMeta($data); + } + } + + return null; + } + /** * Get multiple BuildMeta by BuildId. * @@ -83,7 +115,6 @@ class BuildMetaStore extends Store throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.'); } - $query = 'SELECT * FROM {{build_meta}} WHERE {{build_id}} = :build_id LIMIT :limit'; $stmt = Database::getConnection($useConnection)->prepareCommon($query); $stmt->bindValue(':build_id', $buildId); diff --git a/src/PHPCensor/Store/BuildStore.php b/src/PHPCensor/Store/BuildStore.php index 2c3feecb..8c9197b2 100644 --- a/src/PHPCensor/Store/BuildStore.php +++ b/src/PHPCensor/Store/BuildStore.php @@ -3,8 +3,10 @@ namespace PHPCensor\Store; use b8\Database; +use b8\Store\Factory; use PHPCensor\Model\Build; use b8\Exception\HttpException; +use PHPCensor\Model\BuildMeta; use PHPCensor\Store; /** @@ -245,7 +247,7 @@ class BuildStore extends Store */ public function getAllProjectsLatestBuilds($limit_by_project = 5, $limit_all = 10) { - // dont fetch log field - contain many data + // don't fetch log field - contain many data $query = ' SELECT {{id}}, @@ -393,7 +395,7 @@ class BuildStore extends Store $query = 'SELECT bm.build_id, bm.meta_key, bm.meta_value FROM {{build_meta}} AS {{bm}} LEFT JOIN {{build}} AS {{b}} ON b.id = bm.build_id - WHERE bm.meta_key = :key AND b.project_id = :projectId'; + WHERE bm.meta_key = :key AND b.project_id = :projectId'; // If we're getting comparative meta data, include previous builds // otherwise just include the specified build ID: @@ -445,24 +447,20 @@ class BuildStore extends Store * @param integer $buildId * @param string $key * @param string $value - * - * @return boolean */ public function setMeta($buildId, $key, $value) { - $cols = '{{build_id}}, {{meta_key}}, {{meta_value}}'; - $query = 'INSERT INTO {{build_meta}} ('.$cols.') VALUES (:buildId, :key, :value)'; - - $stmt = Database::getConnection('read')->prepareCommon($query); - $stmt->bindValue(':key', $key, \PDO::PARAM_STR); - $stmt->bindValue(':buildId', (int)$buildId, \PDO::PARAM_INT); - $stmt->bindValue(':value', $value, \PDO::PARAM_STR); - - if ($stmt->execute()) { - return true; - } else { - return false; + /** @var BuildMetaStore $store */ + $store = Factory::getStore('BuildMeta'); + $meta = $store->getByKey($buildId, $key); + if (is_null($meta)) { + $meta = new BuildMeta(); + $meta->setBuildId($buildId); + $meta->setMetaKey($key); } + $meta->setMetaValue($value); + + $store->save($meta); } /**