Source for file DB.php

Documentation is available at DB.php

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4.  *  DB.php
  5.  *
  6.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  7.  *  @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
  8.  *  @package    Ethna
  9.  *  @version    $Id: bc26055c2e52c8ef683e4a50b33b3fa9dd41a345 $
  10.  */
  11.  
  12. /**
  13.  *  Ethna用DB抽象クラス
  14.  *
  15.  *  EthnaのフレームワークでDBオブジェクトを扱うための抽象クラス
  16.  *  (のつもり...あぁすばらしきPHP 4)
  17.  *
  18.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  19.  *  @access     public
  20.  *  @package    Ethna
  21.  */
  22. class Ethna_DB
  23. {
  24.     /**#@+
  25.      *  @access private
  26.      */
  27.  
  28.     /**
  29.      * @XXX stay public because of B.C.
  30.      * @protected    object  DB              DBオブジェクト
  31.      */
  32.     public $db;
  33.  
  34.     /** @protected    array   トランザクション管理スタック */
  35.     protected $transaction = array();
  36.  
  37.     /**#@-*/
  38.  
  39.  
  40.     /**
  41.      *  Ethna_DBクラスのコンストラクタ
  42.      *
  43.      *  @access public
  44.      *  @param  object  Ethna_Controller    $controller    コントローラオブジェクト
  45.      *  @param  string  $dsn                                DSN
  46.      *  @param  bool    $persistent                         持続接続設定
  47.      */
  48.     public function __construct($controller$dsn$persistent)
  49.     {
  50.         $this->dsn $dsn;
  51.         $this->persistent $persistent;
  52.     }
  53.  
  54.     /**
  55.      *  DBに接続する
  56.      *
  57.      *  @access public
  58.      *  @return mixed   0:正常終了 Ethna_Error:エラー
  59.      */
  60.     public function connect()
  61.     {
  62.     }
  63.  
  64.     /**
  65.      *  DB接続を切断する
  66.      *
  67.      *  @access public
  68.      */
  69.     public function disconnect()
  70.     {
  71.     }
  72.  
  73.     /**
  74.      *  DB接続状態を返す
  75.      *
  76.      *  @access public
  77.      *  @return bool    true:正常(接続済み) false:エラー/未接続
  78.      */
  79.     public function isValid()
  80.     {
  81.     }
  82.  
  83.     /**
  84.      *  DBトランザクションを開始する
  85.      *
  86.      *  @access public
  87.      *  @return mixed   0:正常終了 Ethna_Error:エラー
  88.      */
  89.     public function begin()
  90.     {
  91.     }
  92.  
  93.     /**
  94.      *  DBトランザクションを中断する
  95.      *
  96.      *  @access public
  97.      *  @return mixed   0:正常終了 Ethna_Error:エラー
  98.      */
  99.     public function rollback()
  100.     {
  101.     }
  102.  
  103.     /**
  104.      *  DBトランザクションを終了する
  105.      *
  106.      *  @access public
  107.      *  @return mixed   0:正常終了 Ethna_Error:エラー
  108.      */
  109.     public function commit()
  110.     {
  111.     }
  112.  
  113.     /**
  114.      *  テーブル定義情報を取得する
  115.      *
  116.      *  @access public
  117.      *  @return mixed   array: PEAR::DBに準じたメタデータ
  118.      *                   Ethna_Error::エラー
  119.      */
  120.     public function getMetaData()
  121.     {
  122.         //   このメソッドはAppObject
  123.         //   との連携に必要。
  124.     }
  125.  
  126.     /**
  127.      *  DSNを取得する
  128.      *
  129.      *  @access public
  130.      *  @return string  DSN
  131.      */
  132.     public function getDSN()
  133.     {
  134.         return $this->dsn;
  135.     }
  136.  
  137.     // {{{ parseDSN()
  138.     /**
  139.      * Parse a data source name
  140.      *
  141.      * Additional keys can be added by appending a URI query string to the
  142.      * end of the DSN.
  143.      *
  144.      * The format of the supplied DSN is in its fullest form:
  145.      * <code>
  146.      *  phptype(dbsyntax)://username:password@protocol+hostspec/database?option=8&another=true
  147.      * </code>
  148.      *
  149.      * Most variations are allowed:
  150.      * <code>
  151.      *  phptype://username:password@protocol+hostspec:110//usr/db_file.db?mode=0644
  152.      *  phptype://username:password@hostspec/database_name
  153.      *  phptype://username:password@hostspec
  154.      *  phptype://username@hostspec
  155.      *  phptype://hostspec/database
  156.      *  phptype://hostspec
  157.      *  phptype(dbsyntax)
  158.      *  phptype
  159.      * </code>
  160.      *
  161.      * @param string $dsn Data Source Name to be parsed
  162.      *
  163.      * @return array an associative array with the following keys:
  164.      *   + phptype:  Database backend used in PHP (mysql, odbc etc.)
  165.      *   + dbsyntax: Database used with regards to SQL syntax etc.
  166.      *   + protocol: Communication protocol to use (tcp, unix etc.)
  167.      *   + hostspec: Host specification (hostname[:port])
  168.      *   + database: Database to use on the DBMS server
  169.      *   + username: User name for login
  170.      *   + password: Password for login
  171.      */
  172.     public function parseDSN($dsn)
  173.     {
  174.         $parsed array(
  175.             'phptype'  => false,
  176.             'dbsyntax' => false,
  177.             'username' => false,
  178.             'password' => false,
  179.             'protocol' => false,
  180.             'hostspec' => false,
  181.             'port'     => false,
  182.             'socket'   => false,
  183.             'database' => false,
  184.         );
  185.  
  186.         if (is_array($dsn)) {
  187.             $dsn array_merge($parsed$dsn);
  188.             if (!$dsn['dbsyntax']{
  189.                 $dsn['dbsyntax'$dsn['phptype'];
  190.             }
  191.             return $dsn;
  192.         }
  193.  
  194.         // Find phptype and dbsyntax
  195.         if (($pos strpos($dsn'://')) !== false{
  196.             $str substr($dsn0$pos);
  197.             $dsn substr($dsn$pos 3);
  198.         else {
  199.             $str $dsn;
  200.             $dsn null;
  201.         }
  202.  
  203.         // Get phptype and dbsyntax
  204.         // $str => phptype(dbsyntax)
  205.         if (preg_match('|^(.+?)\((.*?)\)$|'$str$arr)) {
  206.             $parsed['phptype']  $arr[1];
  207.             $parsed['dbsyntax'!$arr[2$arr[1$arr[2];
  208.         else {
  209.             $parsed['phptype']  $str;
  210.             $parsed['dbsyntax'$str;
  211.         }
  212.  
  213.         if (!count($dsn)) {
  214.             return $parsed;
  215.         }
  216.  
  217.         // Get (if found): username and password
  218.         // $dsn => username:password@protocol+hostspec/database
  219.         if (($at strrpos($dsn'@')) !== false{
  220.             $str substr($dsn0$at);
  221.             $dsn substr($dsn$at 1);
  222.             if (($pos strpos($str':')) !== false{
  223.                 $parsed['username'rawurldecode(substr($str0$pos));
  224.                 $parsed['password'rawurldecode(substr($str$pos 1));
  225.             else {
  226.                 $parsed['username'rawurldecode($str);
  227.             }
  228.         }
  229.  
  230.         // Find protocol and hostspec
  231.         if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|'$dsn$match)) {
  232.             // $dsn => proto(proto_opts)/database
  233.             $proto      $match[1];
  234.             $proto_opts $match[2$match[2false;
  235.             $dsn        $match[3];
  236.         else {
  237.             // $dsn => protocol+hostspec/database (old format)
  238.             if (strpos($dsn'+'!== false{
  239.                 list($proto$dsnexplode('+'$dsn2);
  240.             }
  241.             if (strpos($dsn'/'!== false{
  242.                 list($proto_opts$dsnexplode('/'$dsn2);
  243.             else {
  244.                 $proto_opts $dsn;
  245.                 $dsn        null;
  246.             }
  247.         }
  248.  
  249.         // process the different protocol options
  250.         $parsed['protocol'(!empty($proto)) $proto 'tcp';
  251.         $proto_opts         rawurldecode($proto_opts);
  252.         if ($parsed['protocol'== 'tcp'{
  253.             if (strpos($proto_opts':'!== false{
  254.                 list($parsed['hostspec']$parsed['port']explode(':'$proto_opts);
  255.             else {
  256.                 $parsed['hostspec'$proto_opts;
  257.             }
  258.         elseif ($parsed['protocol'== 'unix'{
  259.             $parsed['socket'$proto_opts;
  260.         }
  261.  
  262.         // Get dabase if any
  263.         // $dsn => database
  264.         if ($dsn{
  265.             if (($pos strpos($dsn'?')) === false{
  266.                 // /database
  267.                 $parsed['database'rawurldecode($dsn);
  268.             else {
  269.                 // /database?param1=value1&param2=value2
  270.                 $parsed['database'rawurldecode(substr($dsn0$pos));
  271.                 $dsn                substr($dsn$pos 1);
  272.                 if (strpos($dsn'&'!== false{
  273.                     $opts explode('&'$dsn);
  274.                 else // database?param1=value1
  275.                     $opts array($dsn);
  276.                 }
  277.                 foreach ($opts as $opt{
  278.                     list($key$valueexplode('='$opt);
  279.                     if (!isset($parsed[$key])) {
  280.                         // don't allow params overwrite
  281.                         $parsed[$keyrawurldecode($value);
  282.                     }
  283.                 }
  284.             }
  285.         }
  286.  
  287.         return $parsed;
  288.     }
  289.     // }}}
  290. }

Documentation generated on Fri, 11 Nov 2011 03:57:57 +0900 by phpDocumentor 1.4.3