EDB
[ class tree: EDB ] [ index: EDB ] [ all elements ]

Source for file EDB_Common.php

Documentation is available at EDB_Common.php

  1. <?php
  2. /**
  3.  * Project: EDB_Common :: Common API for EDB class
  4.  * File:    EDB/EDB_Common.php
  5.  *
  6.  * The EDB_Common class is common api that used internally
  7.  * on EDB class.
  8.  *
  9.  * @category    Database
  10.  * @package     EDB
  11.  * @subpackage  EDB_Common
  12.  * @author      JoungKyun.Kim <http://oops.org>
  13.  * @copyright   (c) 2018, JoungKyun.Kim
  14.  * @license     BSD License
  15.  * @version     $Id$
  16.  * @link        http://pear.oops.org/package/EDB
  17.  * @filesource
  18.  */
  19.  
  20. /**
  21.  * Common API of EDB
  22.  *
  23.  * @package EDB
  24.  */
  25. Class EDB_Common {
  26.     // {{{ properties
  27.     /**
  28.      * Store sql query in session
  29.      * @access public
  30.      * @var array 
  31.      */
  32.     public $queryLog = null;
  33.     /**
  34.      * Result marking for free
  35.      * @access private
  36.      * @var    boolean 
  37.      */
  38.     protected $free = false;
  39.     /**
  40.      * DB result handler
  41.      * @access private
  42.      * @var    object 
  43.      */
  44.     protected $result;
  45.     // }}}
  46.  
  47.     // {{{ (bool) EDB_Common:: file_exists (void)
  48.     /**
  49.      * Checks whether a file or directory exists
  50.      * 
  51.      * If don't find file, and re-search include_path
  52.      *
  53.      * @access public
  54.      * @return boolean Returns TRUE if the file or directory specified by filename exists; FALSE otherwise.
  55.      * @param  string  Path to the file or directory.
  56.      */
  57.     function file_exists ($file{
  58.         if file_exists ($file) )
  59.             return true;
  60.  
  61.         try {
  62.             $buf ini_get ('include_path');
  63.         catch Exception $e {
  64.             # for AnNyung LInux
  65.             $buf ___ini_get ('include_path');
  66.         }
  67.  
  68.         $path preg_split ('/:/'$buf);
  69.         //array_shift ($path);
  70.         foreach $path as $dir {
  71.             if file_exists ($dir '/' $file) )
  72.                 return true;
  73.         }
  74.  
  75.         return false;
  76.     }
  77.     // }}}
  78.  
  79.     // {{{ (int) EDB_Common:: switch_freemark (void)
  80.     /**
  81.      * Change free marking
  82.      *
  83.      * @access public
  84.      * @return void 
  85.      * @param  void 
  86.      */
  87.     function switch_freemark ({
  88.         if $this->free )
  89.             $this->free = true;
  90.         else
  91.             $this->free = false;
  92.     }
  93.     // }}}
  94.  
  95.     // {{{ (int) EDB_Common:: get_param_number ($sql)
  96.     /**
  97.      * Get number of query parameters
  98.      *
  99.      * @access public
  100.      * @return integer The number of parameters
  101.      * @param  string Bind query string
  102.      */
  103.     function get_param_number (&$sql$type ''{
  104.         $sql preg_replace ('/[\x5c]\?/''=-=-'$sql);
  105.         $r strlen (preg_replace ('/[^?]/'''$sql));
  106.  
  107.         switch ($type{
  108.             case 'pgsql' :
  109.                 for $i=0$i<$r$i++ )
  110.                     $sql preg_replace ('/\?/''\$' ($i+1)$sql1);
  111.                 break;
  112.             //case 'sqlrelay' :
  113.             //    for ( $i=0; $i<$r; $i++ )
  114.             //        $sql = preg_replace ('/\?/', ':param' . ($i + 1), $sql, 1);
  115.             //    break;
  116.         }
  117.         $sql preg_replace ('/=-=-/''\?'$sql);
  118.  
  119.         return $r;
  120.     }
  121.     // }}}
  122.  
  123.     // {{{ (bool) EDB_Common::check_param ($parameters)
  124.     /**
  125.      * Check parameter type and parameters
  126.      *
  127.      * @access public
  128.      * @return bool 
  129.      * @param  array The parameter of bind query
  130.      */
  131.     function check_param ($param{
  132.         if is_array ($param) )
  133.             return false;
  134.  
  135.         if count ($param)
  136.             return false;
  137.  
  138.         $type array_shift ($param);
  139.         $len strlen ($type);
  140.         if $len != count ($param) )
  141.             return false;
  142.  
  143.         for $i=0$i<$len$i++ {
  144.             $no $i 1;
  145.             switch ($type[$i]{
  146.                 case 'i' // integer
  147.                     if is_numeric ($param[$i]=== false {
  148.                         throw new myException (
  149.                             "The ${no}th parameter type of query is not numeric type",
  150.                             E_USER_ERROR
  151.                         );
  152.                         return false;
  153.                     }
  154.                     break;
  155.                 case 'f' // float, double
  156.                     if is_numeric ($param[$i]!== false && is_float ($param[$i]!== false {
  157.                         throw new myException (
  158.                             "The ${no}th parameter type of query is not double type",
  159.                             E_USER_ERROR
  160.                         );
  161.                         return false;
  162.                     }
  163.                     break;
  164.                 case 'n' // null
  165.                     if $param[$i{
  166.                         throw new myException (
  167.                             "The ${no}th parameter type of query is not null type",
  168.                             E_USER_ERROR
  169.                         );
  170.                         return false;
  171.                     }
  172.                     break;
  173.                 case 'b' // blob
  174.                 case 'c' // clob
  175.                 case 's' // string. by pass
  176.                     break;
  177.                 default :
  178.                     throw new myException (
  179.                         "The ${no}th parameter type of query is unsupported type",
  180.                         E_USER_ERROR
  181.                     );
  182.                     return false;
  183.             }
  184.         }
  185.  
  186.         return true;
  187.     }
  188.     // }}}
  189.  
  190.     // {{{ (string) EDB_Common::bind_param ($sql, $param)
  191.     /**
  192.      * replace bind parameters to parameter's value
  193.      * 
  194.      * @access public
  195.      * @return string 
  196.      * @param  string SQL query statement
  197.      * @param  array  array of parameter values
  198.      */
  199.     function bind_param ($sql$params{
  200.         if is_array ($params) )
  201.             return $sql;
  202.  
  203.         $types array_shift ($params);
  204.         $c count ($params);
  205.  
  206.         $sql preg_replace ('/[\x5c]\?/''=-=-'$sql);
  207.         for $i=0$i<$c$i++ {
  208.             if strncmp ('unquote:'$params[$i]8) ) {
  209.                 $params[$isubstr ($params[$i]8);
  210.                 $buf preg_replace ('/\?/''%s'$sql1);
  211.             else
  212.                 $buf preg_replace ('/\?/'"'%s'"$sql1);
  213.             $sql sprintf ($buf$params[$i]);
  214.         }
  215.         $sql preg_replace ('/=-=-/''\?'$sql);
  216.  
  217.         return $sql;
  218.     }
  219.     // }}}
  220. }
  221.  
  222. /*
  223.  * Local variables:
  224.  * tab-width: 4
  225.  * c-basic-offset: 4
  226.  * End:
  227.  * vim: set filetype=php noet sw=4 ts=4 fdm=marker:
  228.  * vim600: noet sw=4 ts=4 fdm=marker
  229.  * vim<600: noet sw=4 ts=4
  230.  */
  231. ?>

Documentation generated on Tue, 14 May 2019 01:59:47 +0900 by phpDocumentor 1.4.4