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

Source for file EDB_CUBRID.php

Documentation is available at EDB_CUBRID.php

  1. <?php
  2. /**
  3.  * Project: EDB_CUBRID :: CUBRID abstraction layer
  4.  * File:    EDB/EDB_CUBRID.php
  5.  *
  6.  * The EDB_CUBRID class is cubrid abstraction layer that used internally
  7.  * on EDB class.
  8.  *
  9.  * @category    Database
  10.  * @package     EDB
  11.  * @subpackage  EDB_ABSTRACT
  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.  * CUBRID engine for EDB API
  22.  *
  23.  * This class support abstracttion DB layer for CUBRID Engine
  24.  *
  25.  * @package     EDB
  26.  */
  27. Class EDB_CUBRID extends EDB_Common {
  28.     // {{{ properties
  29.     /**#@+
  30.      * @access private
  31.      */
  32.     /**
  33.      * db handler of EDB_CUBRID class
  34.      * @var    object 
  35.      */
  36.     private $db;
  37.     /**
  38.      * The number of query parameter
  39.      * @var    integer 
  40.      */
  41.     private $pno 0;
  42.     /**
  43.      * Blob information
  44.      * @var    array 
  45.      */
  46.     private $lob array ();
  47.     /**
  48.      * Default transaction status
  49.      * @var    bool 
  50.      */
  51.     private $trstatus false;
  52.     /**#@-*/
  53.     // }}}
  54.  
  55.     // {{{ (object) EDB_CUBRID::__construct ($host, $user, $pass, $db)
  56.     /** 
  57.      * Instantiates an EDB_CUBRID object and opens an cubrid database
  58.      *
  59.      * For examples:
  60.      * <code>
  61.      * $db = new EDB_CUBRID ('cubrid://localhost', 'user', 'host', 'database');
  62.      * $db = new EDB_CUBRID ('cubrid://localhost:33000', 'user', 'host', 'database');
  63.      * $db = new EDB_CUBRID ('cubrid://localhost:33000?autocommit=false', 'user', 'host', 'database');
  64.      * </code>
  65.      *
  66.      * If you add prefix 'p~' before host, you can connect with persistent
  67.      * connection.
  68.      *
  69.      * For Examples:
  70.      * <code>
  71.      * $db = new EDB_CUBRID ('cubrid://p~localhost', 'user', 'host', 'database');
  72.      * </code>
  73.      *
  74.      * @access public
  75.      * @return EDB_CUBRID 
  76.      * @param  string  $hostname cubrid host
  77.      * @param  string  $user     cubrid user
  78.      * @param  string  $password cubrid password
  79.      * @param  string  $database cubrid database
  80.      */
  81.     function __construct ({
  82.         $_argv func_get_args ();
  83.         $argv is_array ($_argv[0]$_argv[0$_argv;;
  84.  
  85.         if extension_loaded ('cubrid') )
  86.             throw new myException ('CUBRID extension is not loaded on PHP!'E_USER_ERROR);
  87.  
  88.         $o = (object) array (
  89.             'host' => preg_replace ('!^cubrid://!'''$argv[0]),
  90.             'user' => $argv[1],
  91.             'pass' => $argv[2],
  92.             'db'   => $argv[3]
  93.         );
  94.  
  95.         if preg_match ('/\?.*/'$o->host$matches) ) {
  96.             $o->opt $matches[0];
  97.             $o->host preg_replace ('/\?.*/'''$o->host);
  98.         }
  99.  
  100.         if preg_match ('/^([^:]+):(.+)/'$o->host$matches) ) {
  101.             if is_numeric ($matches[2]) )
  102.                 $o->host .= ':33000';
  103.         else
  104.             $o->host .= ':33000';
  105.  
  106.         if preg_match ('/^p~/'$o->host) ) {
  107.             $func 'cubrid_pconnect_with_url';
  108.             $o->host preg_replace ('/^p~/'''$o->host);
  109.         else
  110.             $func 'cubrid_connect_with_url';
  111.  
  112.         $url sprintf ('CUBRID:%s:%s:::%s'$o->host$o->db$o->opt);
  113.  
  114.         try {
  115.             $this->db $func ($url$o->user$o->pass);
  116.             $this->trstatus cubrid_get_autocommit ($this->db);
  117.         catch Exception $e {
  118.             throw new myException ($e->getMessage ()$e->getCode()$e);
  119.         }
  120.     }
  121.     // }}}
  122.  
  123.     // {{{ (string) EDB_CUBRID::get_charset (void)
  124.     /** 
  125.      * Get character set of current database
  126.      *
  127.      * CUBRID extension don't support this function
  128.      *
  129.      * @access public
  130.      * @return string Current character set name on DB
  131.      */
  132.     function get_charset ({
  133.         try {
  134.             return cubrid_get_charset ($this->db);
  135.         catch Exception $e {
  136.             throw new myException ($e->getMessage ()$e->getCode()$e);
  137.             return false;
  138.         }
  139.     }
  140.     // }}}
  141.  
  142.     // {{{ (bool) EDB_CUBRID::set_charset ($charset)
  143.     /** 
  144.      * Set character set of current database
  145.      *
  146.      * This method is always returned true because CUBRID don't support
  147.      * charset settings.
  148.      *
  149.      * @access public
  150.      * @return bool    always return true
  151.      * @param  string  name of character set that supported from database
  152.      */
  153.     function set_charset ($char{
  154.         return true;
  155.     }
  156.     // }}}
  157.  
  158.     // {{{ (string) EDB_CUBRID::escape ($string)
  159.     /** 
  160.      * Escape special characters in a string for use in an SQL statement
  161.      *
  162.      * @access public
  163.      * @return string 
  164.      * @param  string  The string that is to be escaped.
  165.      */
  166.     function escape ($string{
  167.         return cubrid_real_escape_string ($string$this->db);
  168.     }
  169.     // }}}
  170.  
  171.     // {{{ (int) EDB_CUBRID::query ($query, $param_type, $param1, $param2 ...)
  172.     /** 
  173.      * Performs a query on the database
  174.      *
  175.      * @access public
  176.      * @return integer The number of affected rows or false
  177.      * @param  string  $query The query strings
  178.      * @param  string  $type  (optional) Bind parameter type. See also
  179.      *  {@link http://php.net/manual/en/cubridi-stmt.bind-param.php cubridi_stmt::bind_param}.
  180.      *  <code>
  181.      *  i => integer
  182.      *  d => double
  183.      *  s => string
  184.      *  b => blob
  185.      *  </code>
  186.      * @param  mixed   $param1 (optional) Bind parameter 1
  187.      * @param  mixed   $param2,... (optional) Bind parameter 2 ..
  188.      */
  189.     function query ({
  190.         $_argv func_get_args ();
  191.         $argv is_array ($_argv[0]$_argv[0$_argv;;
  192.  
  193.         $this->error null;
  194.  
  195.         $sql array_shift ($argv);
  196.         $this->pno count ($argv$this->get_param_number ($sql0;
  197.  
  198.         if $this->free )
  199.             $this->free_result ();
  200.  
  201.         // store query in log variable
  202.         $this->queryLog[$sql;
  203.  
  204.         if $this->pno++ == // no bind query
  205.             $this->no_bind_query ($sql);
  206.         else // bind query
  207.             $this->bind_query ($sql$argv);
  208.  
  209.         if preg_match ('/^(update|insert|delete|replace)/i'trim ($sql)) ) {
  210.             /* Insert or update, or delete query */
  211.             return cubrid_affected_rows ($this->db);
  212.         else if preg_match ('/create|drop/i'trim ($sql)) ) {
  213.             return 1;
  214.         }
  215.  
  216.         # Only select
  217.         if preg_match ('/^select/i'trim ($sql)) ) {
  218.             $fno $this->num_fields ();
  219.             for $i=0$i<$fno$i++ {
  220.                 $type $this->field_type ($i);
  221.                 if $type == 'blob' || $type == 'clob' )
  222.                     $lob .= ':' $this->field_name ($i);
  223.             }
  224.  
  225.             $lob substr ($lob1);
  226.             if preg_match ('/:/'$lob) )
  227.                 $this->lob preg_split ('/:/'$lob);
  228.             else
  229.                 $this->lob array ($lob);
  230.         }
  231.  
  232.         return cubrid_num_rows ($this->result);
  233.     }
  234.     // }}}
  235.  
  236.     // {{{ (string) EDB_CUBRID::lastId (void)
  237.     /**
  238.      * 가장 마지막 입력 row ID를 반환한다.
  239.      *
  240.      * @since  2.0.4
  241.      * @access public
  242.      * @return string|false
  243.      */
  244.     function lastId ({
  245.         return qubrid_insert_id ($this->db);
  246.     }
  247.     // }}}
  248.  
  249.     // {{{ (bool) EDB_CUBRID::seek ($offset)
  250.     /**
  251.      * Move the cursor in the result
  252.      *
  253.      * @access public
  254.      * @return boolean 
  255.      * @param  Number of units you want to move the cursor.
  256.      */
  257.     function seek ($offset{
  258.         if is_resource ($this->result) )
  259.             return false;
  260.  
  261.         try {
  262.             // cubrid_move_cursor은 시작점이 0이 아니라 1이라서
  263.             // 호환성에 문제가 있어 cubrid_data_seek를 사용한다.
  264.             //return cubrid_move_cursor ($this->result, $offset, CUBRID_CURSOR_FIRST);
  265.             return cubrid_data_seek ($this->result$offset);
  266.         catch Exception $e {
  267.             throw new myException ($e->getMessage ()$e->getCode()$e);
  268.             return false;
  269.         }
  270.     }
  271.     // }}}
  272.  
  273.     // {{{ (object) EDB_CUBRID::fetch (void)
  274.     /**
  275.      * Fetch a result row as an associative object
  276.      *
  277.      * @access public
  278.      * @return stdClass|falseThe object of fetched a result row or false
  279.      * @param  boolean (optional) 수행후 result를 free 한다. 기본값: false
  280.      *                  EDB >= 2.0.3
  281.      */
  282.     function fetch ($free false{
  283.         try {
  284.             $r cubrid_fetch ($this->resultCUBRID_OBJECT CUBRID_LOB);
  285.  
  286.             if $r === null )
  287.                 $r false;
  288.  
  289.             foreach $this->lob as $keyname {
  290.                 if is_resource ($r->$keyname) ) {
  291.                     $len cubrid_lob2_size64 ($r->$keyname);
  292.                     $r->$keyname cubrid_lob2_read ($r->$keyname$len);
  293.                 }
  294.             }
  295.  
  296.             if $free )
  297.                 $this->free_result ();
  298.  
  299.             return $r;
  300.         catch Exception $e {
  301.             throw new myException ($e->getMessage ()$e->getCode()$e);
  302.             return false;
  303.         }
  304.     }
  305.     // }}}
  306.  
  307.     // {{{ (array) EDB_CUBRID::fetch_all ($free = true)
  308.     /**
  309.      * Fetch all result rows as an associative object
  310.      *
  311.      * @access public
  312.      * @return array The fetched object result rows
  313.      * @param  boolean (optional) free result set after fetch.
  314.      *                  Defaluts is true.
  315.      */
  316.     function fetch_all ($free true{
  317.         $row array ();
  318.  
  319.         while ( ($r $this->fetch ()) !== false )
  320.             $row[$r;
  321.  
  322.         if $free )
  323.             $this->free_result ();
  324.  
  325.         return $row;
  326.     }
  327.     // }}}
  328.  
  329.     // {{{ (bool) EDB_CUBRID::free_result (void)
  330.     /**
  331.      * Frees stored result memory for the given statement handle
  332.      *
  333.      * @access public
  334.      * @return boolean 
  335.      * @param  void 
  336.      */
  337.     function free_result ({
  338.         if $this->free return true;
  339.         $this->free = false;
  340.         $this->lob array ();
  341.  
  342.         try {
  343.             if is_resource ($this->result) )
  344.                 return true;
  345.  
  346.             #return cubrid_free_result ($this->result);
  347.             return cubrid_close_request ($this->result);
  348.         catch Exception $e {
  349.             throw new myException ($e->getMessage ()$e->getCode()$e);
  350.             return false;
  351.         }
  352.     }
  353.     // }}}
  354.  
  355.     // {{{ (string) EDB_CUBRID::field_name ($index)
  356.     /**
  357.      * Return the name of the specified field index
  358.      *
  359.      * @access public
  360.      * @return string|false
  361.      * @param  integer The numerical field offset. The field_offset starts
  362.      *                  at 0. If field_offset does not exist, return false
  363.      *                  and an error of level E_WARNING is also issued.
  364.      * @see http://php.net/manual/en/function.cubrid-field-name.php cubrid_field_name()
  365.      */
  366.     function field_name ($index{
  367.         try {
  368.             if is_resource ($this->result) )
  369.                 return false;
  370.             return cubrid_field_name ($this->result$index);
  371.         catch Exception $e {
  372.             throw new myException ($e->getMessage ()$e->getCode()$e);
  373.             return false;
  374.         }
  375.     }
  376.     // }}}
  377.  
  378.     // {{{ (string) EDB_CUBRID::field_type ($index)
  379.     /**
  380.      * Get the type of the specified field in a result
  381.      *
  382.      * @access public
  383.      * @return string|false
  384.      * @param  integer The numerical field offset. The field_offset starts
  385.      *                  at 0. If field_offset does not exist, return false
  386.      *                  and an error of level E_WARNING is also issued.
  387.      * @see http://php.net/manual/en/function.cubrid-field-type.php cubrid_field_type()
  388.      */
  389.     function field_type ($index{
  390.         try {
  391.             if is_resource ($this->result) )
  392.                 return false;
  393.  
  394.             return cubrid_field_type ($this->result$index);
  395.         catch Exception $e {
  396.             throw new myException ($e->getMessage ()$e->getCode()$e);
  397.             return false;
  398.         }
  399.     }
  400.     // }}}
  401.  
  402.     // {{{ (int) EDB_CUBRID::num_fields (void)
  403.     /**
  404.      * Return the number of columns in the result set
  405.      *
  406.      * @access public
  407.      * @return integer|falsereturn -1 if SQL sentence is not SELECT.
  408.      * @see http://php.net/manual/en/function.cubrid-num-fields.php cubrid_num_fileds()
  409.      */
  410.     function num_fields ({
  411.         try {
  412.             if is_resource ($this->result) )
  413.                 return false;
  414.             return cubrid_num_cols ($this->result);
  415.         catch Exception $e {
  416.             throw new myException ($e->getMessage ()$e->getCode()$e);
  417.             return false;
  418.         }
  419.     }
  420.     // }}}
  421.  
  422.     // {{{ (void) EDB_CUBRID::trstart (void)
  423.     /**
  424.      * DB transaction을 시작한다.
  425.      *
  426.      * @access public
  427.      * @return void 
  428.      */
  429.     function trstart ({
  430.         if $this->trstatus === true )
  431.             cubrid_set_autocommit ($this->dbCUBRID_AUTOCOMMIT_FALSE);
  432.     }
  433.     // }}}
  434.  
  435.     // {{{ (void) EDB_CUBRID::trend ($v)
  436.     /**
  437.      * DB transaction을 종료한다.
  438.      *
  439.      * @access public
  440.      * @return void 
  441.      * @param bool false일경우 rollback을 수행한다.
  442.      */
  443.     function trend ($v true{
  444.         if $v === true )
  445.             cubrid_commit ($this->db);
  446.         else
  447.             cubrid_rollback ($this->db);
  448.  
  449.         $mode ($this->trstatus === trueCUBRID_AUTOCOMMIT_TRUE CUBRID_AUTOCOMMIT_FALSE;
  450.         cubrid_set_autocommit ($this->db$mode);
  451.     }
  452.     // }}}
  453.  
  454.     // {{{ (void) EDB_CUBRID::close (void)
  455.     /**
  456.      * Close the db handle
  457.      *
  458.      * @access public
  459.      * @return void 
  460.      * @param  void 
  461.      */
  462.     function close ({
  463.         if is_resource ($this->db) ) {
  464.             cubrid_disconnect ($this->db);
  465.             unset ($this->db);
  466.         }
  467.     }
  468.     // }}}
  469.  
  470.     /*
  471.      * Priavte functions
  472.      */
  473.     // {{{ private (int) EDB_CUBRID::no_bind_query ($sql)
  474.     /** 
  475.      * Performs a query on the database
  476.      *
  477.      * @access private
  478.      * @return integer The number of affected rows or false
  479.      * @param  string  The query strings
  480.      */
  481.     private function no_bind_query ($sql{
  482.         try {
  483.             $this->result = cubrid_query ($sql$this->db);
  484.             if cubrid_error_code () ) {
  485.                 $this->free = false;
  486.                 throw new myException (cubrid_error_msg ()E_USER_WARNING);
  487.                 return false;
  488.             }
  489.         catch Exception $e {
  490.             $this->free = false;
  491.             throw new myException ($e->getMessage ()$e->getCode()$e);
  492.             return false;
  493.         }
  494.  
  495.         $this->switch_freemark ();
  496.     }
  497.     // }}}
  498.  
  499.     // {{{ private (int) EDB_CUBRID::bind_query ($sql, $parameters)
  500.     /** 
  501.      * Performs a bind query on the database
  502.      *
  503.      * @access private
  504.      * @return integer The number of affected rows or false
  505.      * @param  string  The query strings
  506.      * @param  array   (optional) Bind parameter type
  507.      */
  508.     private function bind_query ($sql$params{
  509.         if $this->pno != count ($params|| $this->check_param ($params=== false {
  510.             throw new myBException (
  511.                 'Number of elements in query doesn\'t match number of bind variables',
  512.                 E_USER_WARNING
  513.             );
  514.             return false;
  515.         }
  516.  
  517.         try {
  518.             $this->result = cubrid_prepare ($this->db$sql);
  519.  
  520.             for $i=1$i<$this->pno$i++ {
  521.                 switch ($params[0][$i-1]{
  522.                     case 'b' :
  523.                         cubrid_lob2_bind (
  524.                             $this->result,
  525.                             $i,
  526.                             is_object ($params[$i]$params[$i]->data $params[$i],
  527.                             'BLOB'
  528.                         );
  529.                         break;
  530.                     case 'c' :
  531.                         cubrid_lob2_bind (
  532.                             $this->result,
  533.                             $i,
  534.                             is_object ($params[$i]$params[$i]->data $params[$i],
  535.                             'CLOB'
  536.                         );
  537.                         break;
  538.                     default :
  539.                         cubrid_bind ($this->result$i$params[$i]);
  540.                 }
  541.             }
  542.  
  543.             cubrid_execute ($this->result);
  544.  
  545.             $this->switch_freemark ();
  546.         catch Exception $e {
  547.             throw new myException ($e->getMessage ()$e->getCode()$e);
  548.             return false;
  549.         }
  550.     }
  551.     // }}}
  552.  
  553.     function __destruct ({
  554.         try {
  555.             $this->free_result ();
  556.             $this->close ();
  557.         catch Exception $e }
  558.     }
  559. }
  560.  
  561. /*
  562.  * Local variables:
  563.  * tab-width: 4
  564.  * c-basic-offset: 4
  565.  * End:
  566.  * vim: set filetype=php noet sw=4 ts=4 fdm=marker:
  567.  * vim600: noet sw=4 ts=4 fdm=marker
  568.  * vim<600: noet sw=4 ts=4
  569.  */
  570. ?>

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