Fixed excessive BuildMeta inserts.

This commit is contained in:
Dmitry Khomutov 2017-11-09 22:48:46 +07:00
parent cd6bfa5942
commit c6ae043750
No known key found for this signature in database
GPG key ID: EC19426474B37AAC
3 changed files with 48 additions and 19 deletions

View file

@ -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 = [];

View file

@ -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);

View file

@ -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);
}
/**