Skip to content

Instantly share code, notes, and snippets.

@ishtaka
Last active December 19, 2015 07:09
Show Gist options
  • Save ishtaka/5916752 to your computer and use it in GitHub Desktop.
Save ishtaka/5916752 to your computer and use it in GitHub Desktop.
[PHP]データベースコントローラ(PDO)
<?php
/**
* DBコントローラ(PDO)
*
*/
class DbController
{
protected $repository = null;
/**
* コンストラクタ
* @access public
*/
public function __construct($repository)
{
$this->repository = $repository;
$this->repository->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
/**
* FETCHメソッド
* @access protected
* @param string $sql :SQL文
* @throws LogicException :Error Message
* @return array $fetch :result
*/
protected function fetch($sql)
{
try {
$stmt = $this->repository->query($sql);
$fetch = $stmt->fetch();
} catch (PDOException $e) {
throw new LogicException('Failed: ' . $e->getMessage());
}
return $fetch;
}
/**
* FETCH_ALLメソッド
* @access protected
* @param string $sql :SQL文
* @throws LogicException :Error Message
* @return array $fetchAll :result
*/
protected function fetchAll($sql)
{
try {
$stmt = $this->repository->query($sql);
$fetchAll = $stmt->fetchAll();
} catch (PDOException $e) {
throw new LogicException('Failed: ' . $e->getMessage());
}
return $fetchAll;
}
/**
* FETCHメソッド(プリペアードステートメント)
* @access protected
* @param string $sql :SQL文
* @param array $bindValue :parameter
* @throws LogicException :Error Message
* @return array $fetch :result
*/
protected function fetchPrepare($sql, $bindValue = array())
{
try {
$stmt = $this->repository->prepare($sql);
$this->bind($stmt, $bindValue);
$stmt->execute();
$fetch = $stmt->fetch();
} catch (PDOException $e) {
throw new LogicException('Failed: ' .$e->getMessage());
}
return $fetch;
}
/**
* FETCH_ALLメソッド(プリペアードステートメント)
* @access protected
* @param string $sql :SQL文
* @param array $bindValue :parameter
* @throws LogicException :Error Message
* @return array $fetchAll :result
*/
protected function fetchAllPrepare($sql, $bindValue = array())
{
try {
$stmt = $this->repository->prepare($sql);
$stmt = $this->bind($stmt, $bindValue);
$stmt->execute();
$fetchAll = $stmt->fetchAll();
} catch (PDOException $e) {
throw new LogicException('Failed: ' .$e->getMessage());
}
return $fetchAll;
}
/**
* bindValueメソッド
* @param Object $stmt :PDOStatementオブジェクト
* @param Array $bindValue :parameter
*/
protected function bind($stmt, $bindValue)
{
if (!is_array($bindValue)) {
throw new LogicException('Wrong bindValue Parameter');
}
foreach ($bindValue as $key => $val) {
$stmt->bindValue($key, $val, PDO::PARAM_STR);
}
return $stmt;
}
/**
* Transactionメソッド
* @access protected
* @param string $sql :SQL文
* @throws LogicException :error message
*/
protected function transaction($sql)
{
try {
$this->repository->beginTransaction();
$stmt = $this->repository->exec($sql);
$this->repository->commit();
} catch (PDOException $e) {
$this->repository->rollback();
throw new LogicException("Failed to transaction" . $e->getMessage());
}
}
/**
* Transactionメソッド(プリペアードステートメント)
* @access protected
* @param string $sql :SQL文
* @param array $bindValue :parameter
* @throws LogicException :error message
*/
protected function transactionPrepare($sql, $bindValue = array())
{
try {
$this->repository->beginTransaction();
$stmt = $this->repository->prepare($sql);
$stmt = $this->bind($stmt, $bindValue);
$stmt->execute();
$this->repository->commit();
} catch (PDOException $e) {
$this->repository->rollback();
throw new RuntimeException("Failed to transaction" . $e->getMessage());
}
}
/**
* デストラクタ
* @access public
*/
public function __destruct()
{
unset($this->repository);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment