Source for file Ethna_DB_ADOdb.php

Documentation is available at Ethna_DB_ADOdb.php

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4.  *  Ethna_DB_ADOdb.php
  5.  *
  6.  *  @package    Ethna
  7.  *  @author     halt feits <halt.feits@gmail.com>
  8.  *  @version    $Id$
  9.  */
  10.  
  11. /**
  12.  * ADOdb config setting
  13.  */
  14. define('ADODB_OUTP''ethna_adodb_logger')//disable output error
  15.  
  16. require_once 'adodb/adodb.inc.php';
  17.  
  18. function ethna_adodb_logger ($msg$newline{
  19.     $c =Ethna_Controller::getInstance();
  20.     $logger =$c->getLogger();
  21.     
  22.     $logger->log(LOG_DEBUGstrip_tags(str_replace("\n"""$msg)));
  23. }
  24.  
  25. /**
  26.  *  Ethna_DB_ADOdb
  27.  *
  28.  *  EthnaのフレームワークでADOdbオブジェクトを扱うための抽象クラス
  29.  *
  30.  *  @package    Ethna
  31.  *  @author     halt feits <halt.feits@gmail.com>
  32.  *  @access     public
  33.  */
  34. class Ethna_DB_ADOdb extends Ethna_DB
  35. {
  36.     /**#@+
  37.      *  @access private
  38.      */
  39.  
  40.     /** @var    object  DB              DBオブジェクト */
  41.     var $db;
  42.  
  43.     /** @var    string   dsn */
  44.     var $dsn;
  45.  
  46.     /**#@-*/
  47.  
  48.  
  49.     /**
  50.      *  コンストラクタ
  51.      *
  52.      *  @access public
  53.      *  @param  object  Ethna_Controller    &$controller    コントローラオブジェクト
  54.      *  @param  string  $dsn                                DSN
  55.      *  @param  bool    $persistent                         持続接続設定
  56.      */
  57.     function Ethna_DB_ADOdb(&$controller$dsn$persistent)
  58.     {
  59.         parent::Ethna_DB($controller$dsn$persistent);
  60.  
  61.         $this->logger =$controller->getLogger();
  62.     }
  63.  
  64.     //{{{ connect
  65.     /**
  66.      *  DBに接続する
  67.      *
  68.      *  @access public
  69.      *  @return mixed   0:正常終了 Ethna_Error:エラー
  70.      */
  71.     function connect()
  72.     {
  73.         $dsn $this->parseDSN($this->dsn);
  74.         
  75.         if ($dsn['phptype'== 'sqlite'{
  76.             $path $dsn['database'];
  77.             $this->db ADONewConnection("sqlite");
  78.             $this->db->Connect($path);
  79.         else {
  80.             $this->db ADONewConnection($this->dsn);
  81.         }
  82.  
  83.         if $this->db {
  84.             $this->db->SetFetchMode(ADODB_FETCH_ASSOC);
  85.             return true;
  86.         else {
  87.             return false;
  88.         }    
  89.     }
  90.     //}}}
  91.  
  92.     //{{{ disconnect
  93.     /**
  94.      *  DB接続を切断する
  95.      *
  96.      *  @access public
  97.      */
  98.     function disconnect()
  99.     {
  100.         //$this->db->close();
  101.         return 0;
  102.     }
  103.     //}}}
  104.  
  105.     //{{{ isValid
  106.     /**
  107.      *  DB接続状態を返す
  108.      *
  109.      *  @access public
  110.      *  @return bool    true:正常(接続済み) false:エラー/未接続
  111.      */
  112.     function isValid()
  113.     {
  114.         if is_object($this->db) ) {
  115.             return true;
  116.         else {
  117.             return false;
  118.         }
  119.     }
  120.     //}}}
  121.  
  122.     //{{{ begin
  123.     /**
  124.      *  DBトランザクションを開始する
  125.      *
  126.      *  @access public
  127.      *  @return mixed   0:正常終了 Ethna_Error:エラー
  128.      */
  129.     function begin()
  130.     {
  131.         return $this->db->BeginTrans();
  132.     }
  133.     //}}}
  134.  
  135.     //{{{ rollback
  136.     /**
  137.      *  DBトランザクションを中断する
  138.      *
  139.      *  @access public
  140.      *  @return mixed   0:正常終了 Ethna_Error:エラー
  141.      */
  142.     function rollback()
  143.     {
  144.         $this->db->RollbackTrans();
  145.         return 0;
  146.     }
  147.     //}}}
  148.  
  149.     //{{{ commit
  150.     /**
  151.      *  DBトランザクションを終了する
  152.      *
  153.      *  @access public
  154.      *  @return mixed   0:正常終了 Ethna_Error:エラー
  155.      */
  156.     function commit()
  157.     {
  158.         $this->db->CommitTrans();
  159.         return 0;
  160.     }
  161.     //}}}
  162.  
  163.     //{{{ query
  164.     /**
  165.      *  クエリを発行する
  166.      *
  167.      *  @access public
  168.      *  @param  string  $query  SQL文
  169.      *  @return mixed   DB_Result:結果オブジェクト Ethna_Error:エラー
  170.      */
  171.     function &query($query$inputarr false)
  172.     {
  173.         return $this->_query($query$inputarr);
  174.     }
  175.     //}}}
  176.     
  177.     //{{{ _query
  178.     /**
  179.      *  クエリを発行する
  180.      *
  181.      *  @access private
  182.      *  @param  string  $query  SQL文
  183.      *  @return mixed   DB_Result:結果オブジェクト Ethna_Error:エラー
  184.      */
  185.     function &_query($query$inputarr false)
  186.     {
  187.         $this->logger->log(LOG_DEBUG$query);
  188.         $r =$this->db->execute($query$inputarr);
  189.  
  190.         if ($r === false{
  191.  
  192.             $error Ethna::raiseError('エラー SQL[%s] CODE[%d] MESSAGE[%s]',
  193.                 E_DB_QUERY,
  194.                 $query,
  195.                 $this->db->ErrorNo(),
  196.                 $this->db->ErrorMsg());
  197.  
  198.             return $error;
  199.  
  200.         }
  201.  
  202.         return $r;
  203.     }
  204.     //}}}
  205.  
  206.     //{{{ getAll
  207.     /**
  208.      * getAll
  209.      *
  210.      * @access public
  211.      */
  212.     function getAll($query$inputarr false)
  213.     {
  214.         $this->db->SetFetchMode(ADODB_FETCH_ASSOC);
  215.         return $this->db->getAll($query$inputarr);
  216.     }
  217.     //}}}
  218.  
  219.     //{{{ getOne
  220.     function getOne($query$inputarr false)
  221.     {
  222.         return $this->db->GetOne($query$inputarr);
  223.     }
  224.     //}}}
  225.  
  226.     //{{{ getRow
  227.     function getRow($query$inputarr false)
  228.     {
  229.         return $this->db->GetRow($query$inputarr);
  230.     }
  231.     //}}}
  232.  
  233.     //{{{ getCol
  234.     function getCol($query$inputarr false)
  235.     {
  236.         return $this->db->GetCol($query$inputarr);
  237.     }
  238.     //}}}
  239.  
  240.     //{{{ execute
  241.     function execute($query$inputarr false)
  242.     {
  243.         return $this->db->Execute($query$inputarr);
  244.     }
  245.     //}}}
  246.  
  247.     //{{{ replace
  248.     function replace($table$arrFields$keyCols$autoQuote false)
  249.     {
  250.         return $this->db->Replace($table$arrFields$keyCols$autoQuote);
  251.     }
  252.     //}}}
  253.  
  254.     //{{{ autoExecute
  255.     function autoExecute($table$fields$mode$where false$forceUpdate true$magicq false)
  256.     {
  257.         return $this->db->AutoExecute($table$fields$mode$where$forceUpdate$magicq);
  258.     }
  259.     //}}}
  260.     
  261.     //{{{ pageExecute
  262.     /**
  263.      * pageExecute
  264.      *
  265.      * @param string $query 
  266.      * @param string $nrows 
  267.      * @param integer $page 
  268.      * @param array $inputarr 
  269.      */
  270.     function pageExecute($query$nrows$page$inputarr false)
  271.     {
  272.         return $this->db->PageExecute($query$nrows$page$inputarr);
  273.     }
  274.     //}}}
  275.  
  276.     // {{{ parseDSN()
  277.  
  278.     /**
  279.      * Parse a data source name
  280.      *
  281.      * Additional keys can be added by appending a URI query string to the
  282.      * end of the DSN.
  283.      *
  284.      * The format of the supplied DSN is in its fullest form:
  285.      * <code>
  286.      *  phptype(dbsyntax)://username:password@protocol+hostspec/database?option=8&another=true
  287.      * </code>
  288.      *
  289.      * Most variations are allowed:
  290.      * <code>
  291.      *  phptype://username:password@protocol+hostspec:110//usr/db_file.db?mode=0644
  292.      *  phptype://username:password@hostspec/database_name
  293.      *  phptype://username:password@hostspec
  294.      *  phptype://username@hostspec
  295.      *  phptype://hostspec/database
  296.      *  phptype://hostspec
  297.      *  phptype(dbsyntax)
  298.      *  phptype
  299.      * </code>
  300.      *
  301.      * @param string $dsn Data Source Name to be parsed
  302.      *
  303.      * @return array an associative array with the following keys:
  304.      *   + phptype:  Database backend used in PHP (mysql, odbc etc.)
  305.      *   + dbsyntax: Database used with regards to SQL syntax etc.
  306.      *   + protocol: Communication protocol to use (tcp, unix etc.)
  307.      *   + hostspec: Host specification (hostname[:port])
  308.      *   + database: Database to use on the DBMS server
  309.      *   + username: User name for login
  310.      *   + password: Password for login
  311.      */
  312.     function parseDSN($dsn)
  313.     {
  314.         $parsed array(
  315.             'phptype'  => false,
  316.             'dbsyntax' => false,
  317.             'username' => false,
  318.             'password' => false,
  319.             'protocol' => false,
  320.             'hostspec' => false,
  321.             'port'     => false,
  322.             'socket'   => false,
  323.             'database' => false,
  324.         );
  325.  
  326.         if (is_array($dsn)) {
  327.             $dsn array_merge($parsed$dsn);
  328.             if (!$dsn['dbsyntax']{
  329.                 $dsn['dbsyntax'$dsn['phptype'];
  330.             }
  331.             return $dsn;
  332.         }
  333.  
  334.         // Find phptype and dbsyntax
  335.         if (($pos strpos($dsn'://')) !== false{
  336.             $str substr($dsn0$pos);
  337.             $dsn substr($dsn$pos 3);
  338.         else {
  339.             $str $dsn;
  340.             $dsn null;
  341.         }
  342.  
  343.         // Get phptype and dbsyntax
  344.         // $str => phptype(dbsyntax)
  345.         if (preg_match('|^(.+?)\((.*?)\)$|'$str$arr)) {
  346.             $parsed['phptype']  $arr[1];
  347.             $parsed['dbsyntax'!$arr[2$arr[1$arr[2];
  348.         else {
  349.             $parsed['phptype']  $str;
  350.             $parsed['dbsyntax'$str;
  351.         }
  352.  
  353.         if (!count($dsn)) {
  354.             return $parsed;
  355.         }
  356.  
  357.         // Get (if found): username and password
  358.         // $dsn => username:password@protocol+hostspec/database
  359.         if (($at strrpos($dsn'@')) !== false{
  360.             $str substr($dsn0$at);
  361.             $dsn substr($dsn$at 1);
  362.             if (($pos strpos($str':')) !== false{
  363.                 $parsed['username'rawurldecode(substr($str0$pos));
  364.                 $parsed['password'rawurldecode(substr($str$pos 1));
  365.             else {
  366.                 $parsed['username'rawurldecode($str);
  367.             }
  368.         }
  369.  
  370.         // Find protocol and hostspec
  371.         if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|'$dsn$match)) {
  372.             // $dsn => proto(proto_opts)/database
  373.             $proto      $match[1];
  374.             $proto_opts $match[2$match[2false;
  375.             $dsn        $match[3];
  376.         else {
  377.             // $dsn => protocol+hostspec/database (old format)
  378.             if (strpos($dsn'+'!== false{
  379.                 list($proto$dsnexplode('+'$dsn2);
  380.             }
  381.             if (strpos($dsn'/'!== false{
  382.                 list($proto_opts$dsnexplode('/'$dsn2);
  383.             else {
  384.                 $proto_opts $dsn;
  385.                 $dsn        null;
  386.             }
  387.         }
  388.  
  389.         // process the different protocol options
  390.         $parsed['protocol'(!empty($proto)) $proto 'tcp';
  391.         $proto_opts         rawurldecode($proto_opts);
  392.         if ($parsed['protocol'== 'tcp'{
  393.             if (strpos($proto_opts':'!== false{
  394.                 list($parsed['hostspec']$parsed['port']explode(':'$proto_opts);
  395.             else {
  396.                 $parsed['hostspec'$proto_opts;
  397.             }
  398.         elseif ($parsed['protocol'== 'unix'{
  399.             $parsed['socket'$proto_opts;
  400.         }
  401.  
  402.         // Get dabase if any
  403.         // $dsn => database
  404.         if ($dsn{
  405.             if (($pos strpos($dsn'?')) === false{
  406.                 // /database
  407.                 $parsed['database'rawurldecode($dsn);
  408.             else {
  409.                 // /database?param1=value1&param2=value2
  410.                 $parsed['database'rawurldecode(substr($dsn0$pos));
  411.                 $dsn                substr($dsn$pos 1);
  412.                 if (strpos($dsn'&'!== false{
  413.                     $opts explode('&'$dsn);
  414.                 else // database?param1=value1
  415.                     $opts array($dsn);
  416.                 }
  417.                 foreach ($opts as $opt{
  418.                     list($key$valueexplode('='$opt);
  419.                     if (!isset($parsed[$key])) {
  420.                         // don't allow params overwrite
  421.                         $parsed[$keyrawurldecode($value);
  422.                     }
  423.                 }
  424.             }
  425.         }
  426.  
  427.         return $parsed;
  428.     }
  429.  
  430.     // }}}
  431. }
  432.  
  433. ?>

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