Source for file DBConn.php

Documentation is available at DBConn.php

  1. <?php
  2. /**
  3.  * Teeple2 - PHP5 Web Application Framework inspired by Seasar2
  4.  *
  5.  * PHP versions 5
  6.  *
  7.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  8.  * that is available through the world-wide-web at the following URI:
  9.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  10.  * the PHP License and are unable to obtain it through the web, please
  11.  * send a note to license@php.net so we can mail you a copy immediately.
  12.  *
  13.  * @package     teeple
  14.  * @author      Mitsutaka Sato <miztaka@gmail.com>
  15.  * @license     http://www.php.net/license/3_0.txt  PHP License 3.0
  16.  */
  17.  
  18. /**
  19.  * DB接続を表すオブジェクトです。
  20.  * 
  21.  * @package teeple
  22.  */
  23. class Teeple_DBConn {
  24.  
  25.     /**
  26.      * PDOオブジェクトを格納します。
  27.      */
  28.     private $pdo;
  29.     
  30.     private $id;
  31.     
  32.     /**
  33.      * @var Logger 
  34.      */
  35.     private $log;
  36.     
  37.     /**
  38.      * コンストラクタです。
  39.      * 
  40.      * @param string $dsn DSN文字列
  41.      * @param string $user user名
  42.      * @param string $pass password
  43.      */
  44.     public function __construct($dsn$user$pass{
  45.         $this->log LoggerManager::getLogger(get_class($this));
  46.         $this->pdo new PDO($dsn$user$pass);
  47.         $this->pdo->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
  48.         // TODO 是非は微妙
  49.         $this->pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERYTRUE);
  50.         $this->pdo->exec('SET CHARACTER SET utf8');
  51.         $this->id mt_rand();
  52.         $this->log->debug("DBConnを作成しました。({$this->id})");
  53.     }
  54.  
  55.     /**
  56.      * トランザクションを開始します。
  57.      */
  58.     public function beginTransaction() {
  59.         $this->pdo->beginTransaction();
  60.         $this->log->debug("コネクション({$this->id})のトランザクションを開始しました。");
  61.     }
  62.         
  63.     /**
  64.      * トランザクションをコミットします。
  65.      */
  66.     public function commit() {
  67.         $this->pdo->commit();
  68.         $this->log->debug("コネクション({$this->id})をコミットしました。");
  69.     }
  70.     
  71.     /**
  72.      * トランザクションをロールバックします。
  73.      */
  74.     public function rollback() {
  75.         $this->pdo->rollBack();
  76.         $this->log->debug("コネクション({$this->id})をロールバックしました。");
  77.     }        
  78.  
  79.     /**
  80.      * DBを取得します。
  81.      * 
  82.      * @return Object PDOオブジェクト。
  83.      */
  84.     public function getDB() {
  85.         return $this->pdo;
  86.     }
  87.     
  88.     /**
  89.      * コネクションをクローズします。
  90.      * 
  91.      */
  92.     public function close() {
  93.         $this->pdo NULL;
  94.     }
  95. }
  96.  

Documentation generated on Mon, 26 Apr 2010 08:59:42 +0900 by phpDocumentor 1.4.3