2013-05-03 17:02:53 +02:00
< ? php
/**
2013-05-16 03:16:56 +02:00
* PHPCI - Continuous Integration for PHP
*
* @ copyright Copyright 2013 , Block 8 Limited .
* @ license https :// github . com / Block8 / PHPCI / blob / master / LICENSE . md
* @ link http :// www . phptesting . org /
*/
2013-05-03 17:02:53 +02:00
namespace PHPCI\Store ;
use PHPCI\Store\Base\BuildStoreBase ;
/**
2013-05-16 16:40:40 +02:00
* Build Store
* @ author Dan Cryer < dan @ block8 . co . uk >
* @ package PHPCI
* @ subpackage Core
*/
2013-05-03 17:02:53 +02:00
class BuildStore extends BuildStoreBase
{
2013-05-22 18:15:02 +02:00
public function getBuildSummary ()
{
2013-05-22 23:11:36 +02:00
$query = 'SELECT COUNT(*) AS cnt FROM build b LEFT JOIN project p on p.id = b.project_id GROUP BY b.project_id ORDER BY p.title ASC, b.id DESC' ;
2013-05-22 18:15:02 +02:00
$stmt = \b8\Database :: getConnection ( 'read' ) -> prepare ( $query );
if ( $stmt -> execute ()) {
$res = $stmt -> fetch ( \PDO :: FETCH_ASSOC );
$count = ( int ) $res [ 'cnt' ];
} else {
$count = 0 ;
}
2013-06-05 07:23:47 +02:00
$query = 'SELECT b.* FROM build b LEFT JOIN project p on p.id = b.project_id ORDER BY p.title ASC, b.id DESC' ;
2013-05-22 18:15:02 +02:00
$stmt = \b8\Database :: getConnection ( 'read' ) -> prepare ( $query );
if ( $stmt -> execute ()) {
$res = $stmt -> fetchAll ( \PDO :: FETCH_ASSOC );
$map = function ( $item ) {
return new \PHPCI\Model\Build ( $item );
};
$rtn = array_map ( $map , $res );
return array ( 'items' => $rtn , 'count' => $count );
} else {
return array ( 'items' => array (), 'count' => 0 );
}
}
2013-10-08 08:21:46 +02:00
public function getMeta ( $key , $projectId , $buildId = null , $numResults = 1 )
{
$and = $numResults > 1 ? ' AND (`build_id` <= :buildId) ' : ' AND (`build_id` = :buildId) ' ;
2013-10-08 14:00:38 +02:00
$query = 'SELECT `build_id`, `meta_key`, `meta_value` FROM `build_meta` WHERE `meta_key` = :key AND `project_id` = :projectId ' . $and . ' ORDER BY id DESC LIMIT :numResults' ;
2013-10-08 08:21:46 +02:00
$stmt = \b8\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 );
if ( $stmt -> execute ()) {
$rtn = $stmt -> fetchAll ( \PDO :: FETCH_ASSOC );
$rtn = array_reverse ( $rtn );
$rtn = array_map ( function ( $item ) {
2013-10-08 14:00:38 +02:00
$item [ 'meta_value' ] = json_decode ( $item [ 'meta_value' ], true );
2013-10-08 08:21:46 +02:00
return $item ;
}, $rtn );
if ( ! count ( $rtn )) {
return null ;
} else {
return $rtn ;
}
} else {
return null ;
}
}
public function setMeta ( $projectId , $buildId , $key , $value )
{
2013-10-08 14:00:38 +02:00
$query = 'REPLACE INTO build_meta (project_id, build_id, `meta_key`, `meta_value`) VALUES (:projectId, :buildId, :key, :value)' ;
2013-10-08 08:21:46 +02:00
$stmt = \b8\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 ( ':value' , $value , \PDO :: PARAM_STR );
if ( $stmt -> execute ()) {
return true ;
} else {
return false ;
}
}
2013-05-16 17:03:34 +02:00
}