Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 46 |
Dbal | |
0.00% |
0 / 1 |
|
0.00% |
0 / 11 |
272.00 | |
0.00% |
0 / 46 |
__construct | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
getPdo | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
query | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 4 |
|||
queryOne | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 8 |
|||
queryAsStmt | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 4 |
|||
execute | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 4 |
|||
prepare | |
0.00% |
0 / 1 |
30.00 | |
0.00% |
0 / 14 |
|||
begin | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
rollback | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
commit | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
close | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
<?php | |
namespace Puyo\Db; | |
/** | |
* PDO をさらに抽象化したもの | |
* @uses ext-pdo | |
*/ | |
class Dbal | |
{ | |
/** @var \PDO */ | |
protected $pdo; | |
public function __construct(\PDO $pdo) { | |
$this->pdo = $pdo; | |
} | |
public function getPdo() { | |
return $this->pdo; | |
} | |
/** | |
* @param string $sql | |
* @param string[]|int[] $values | |
* @return array 複数行 | |
*/ | |
public function query($sql, $values=[]) { | |
$stmt = $this->prepare($sql, $values); | |
$stmt->execute(); | |
return $stmt->fetchAll(\PDO::FETCH_ASSOC); | |
} | |
/** | |
* @param string $sql | |
* @param string[]|int[] $values | |
* @return array|null 単一行 | |
*/ | |
public function queryOne($sql, $values=[]) { | |
$stmt = $this->prepare($sql, $values); | |
$stmt->execute(); | |
$firstRow = $stmt->fetch(\PDO::FETCH_ASSOC); | |
if($firstRow === false) { | |
return null; | |
} | |
return $firstRow; | |
} | |
/** | |
* @param string $sql | |
* @param string[]|int[] $values | |
* @return \PDOStatement | |
*/ | |
public function queryAsStmt($sql, $values=[]) { | |
$stmt = $this->prepare($sql, $values); | |
$stmt->execute(); | |
return $stmt; | |
} | |
/** | |
* @param string $sql | |
* @param string[]|int[] $values | |
* @return int 更新件数 | |
*/ | |
public function execute($sql, $values=[]) { | |
$stmt = $this->prepare($sql, $values); | |
$stmt->execute(); | |
return $stmt->rowCount(); | |
} | |
/** | |
* @param string $sql | |
* @param string[]|int[] $values | |
* @return \PDOStatement | |
*/ | |
protected function prepare($sql, $values=[]) { | |
$stmt = $this->pdo->prepare($sql); | |
foreach($values as $name=>$val) { | |
if(is_int($val)) { | |
$stmt->bindValue($name, $val, \PDO::PARAM_INT); | |
} elseif(is_bool($val)) { | |
$stmt->bindValue($name, $val, \PDO::PARAM_BOOL); | |
} elseif(is_null($val)) { | |
$stmt->bindValue($name, $val, \PDO::PARAM_NULL); | |
} else { | |
$stmt->bindValue($name, $val, \PDO::PARAM_STR); | |
} | |
} | |
return $stmt; | |
} | |
public function begin() { | |
return $this->pdo->beginTransaction(); | |
} | |
public function rollback() { | |
return $this->pdo->rollBack(); | |
} | |
public function commit() { | |
return $this->pdo->commit(); | |
} | |
public function close() { | |
$this->pdo = null; | |
} | |
} |