Source for file pgsql.dbconnection.php

Documentation is available at pgsql.dbconnection.php

  1. <?php
  2. /**
  3. @package    jelix
  4. @subpackage db_driver
  5. @author     Croes Gérald, Laurent Jouanneau
  6. @contributor Laurent Jouanneau
  7. @contributor Yannick Le Guédart
  8. @contributor Laurent Raufaste
  9. @contributor Julien Issler
  10. @copyright  2001-2005 CopixTeam, 2005-2010 Laurent Jouanneau, 2007-2008 Laurent Raufaste
  11. @copyright  2009 Julien Issler
  12. *  This class was get originally from the Copix project (CopixDBConnectionPostgreSQL, Copix 2.3dev20050901, http://www.copix.org)
  13. *  Few lines of code are still copyrighted 2001-2005 CopixTeam (LGPL licence).
  14. *  Initial authors of this Copix class are Gerald Croes and Laurent Jouanneau,
  15. *  and this class was adapted/improved for Jelix by Laurent Jouanneau
  16. *
  17. @link        http://www.jelix.org
  18. @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  19. */
  20.  
  21. /**
  22.  *
  23.  * @package    jelix
  24.  * @subpackage db_driver
  25.  */
  26. class pgsqlDbConnection extends jDbConnection {
  27.     protected $_charsets =array'UTF-8'=>'UNICODE''ISO-8859-1'=>'LATIN1');
  28.  
  29.     function __construct($profile){
  30.         if(!function_exists('pg_connect')){
  31.             throw new jException('jelix~db.error.nofunction','posgresql');
  32.         }
  33.         parent::__construct($profile);
  34.         if(isset($this->profile['single_transaction']&& ($this->profile['single_transaction'])){
  35.             $this->beginTransaction();
  36.             $this->setAutoCommit(false);
  37.         }
  38.         else
  39.         {
  40.             $this->setAutoCommit(true);
  41.         }
  42.     }
  43.  
  44.     /**
  45.      * enclose the field name
  46.      * @param string $fieldName the field name
  47.      * @return string the enclosed field name
  48.      * @since 1.1.1
  49.      */
  50.     public function encloseName($fieldName){
  51.         return '"'.$fieldName.'"';
  52.     }
  53.  
  54.     function __destruct(){
  55.         if(isset($this->profile['single_transaction']&& ($this->profile['single_transaction'])){
  56.             $this->commit();
  57.         }
  58.         parent::__destruct();
  59.     }
  60.  
  61.     public function beginTransaction (){
  62.         return $this->_doExec('BEGIN');
  63.     }
  64.  
  65.     public function commit (){
  66.         return $this->_doExec('COMMIT');
  67.     }
  68.  
  69.     public function rollback (){
  70.         return $this->_doExec('ROLLBACK');
  71.     }
  72.  
  73.     public function prepare ($query){
  74.         $id=(string)mktime();
  75.         $res pg_prepare($this->_connection$id$query);
  76.         if($res){
  77.             $rsnew pgsqlDbResultSet ($res$id$this->_connection );
  78.         }else{
  79.             throw new jException('jelix~db.error.query.bad',  pg_last_error($this->_connection).'('.$query.')');
  80.         }
  81.         return $rs;
  82.     }
  83.  
  84.     public function errorInfo(){
  85.         return array'HY000' ,pg_last_error($this->_connection)pg_last_error($this->_connection));
  86.     }
  87.  
  88.     public function errorCode(){
  89.         return pg_last_error($this->_connection);
  90.     }
  91.  
  92.     protected function _connect (){
  93.         $funcconnect(isset($this->profile['persistent']&& $this->profile['persistent''pg_pconnect':'pg_connect');
  94.  
  95.         $str '';
  96.  
  97.         // we do a distinction because if the host is given == TCP/IP connection else unix socket
  98.         if($this->profile['host'!= '')
  99.             $str 'host=\''.$this->profile['host'].'\''.$str;
  100.  
  101.         if (isset($this->profile['port'])) {
  102.             $str .= ' port=\''.$this->profile['port'].'\'';
  103.         }
  104.  
  105.         if ($this->profile['database'!= ''{
  106.             $str .= ' dbname=\''.$this->profile['database'].'\'';
  107.         }
  108.  
  109.         // we do isset instead of equality test against an empty string, to allow to specify
  110.         // that we want to use configuration set in environment variables
  111.         if (isset($this->profile['user'])) {
  112.             $str .= ' user=\''.$this->profile['user'].'\'';
  113.         }
  114.  
  115.         if (isset($this->profile['password'])) {
  116.             $str .= ' password=\''.$this->profile['password'].'\'';
  117.         }
  118.  
  119.         if (isset($this->profile['timeout']&& $this->profile['timeout'!= ''{
  120.             $str .= ' connect_timeout=\''.$this->profile['timeout'].'\'';
  121.         }
  122.  
  123.         // let's do the connection
  124.         if ($cnx @$funcconnect ($str)) {
  125.             if (isset($this->profile['force_encoding']&& $this->profile['force_encoding'== true
  126.                && isset($this->_charsets[$GLOBALS['gJConfig']->charset])){
  127.                 pg_set_client_encoding($cnx$this->_charsets[$GLOBALS['gJConfig']->charset]);
  128.             }
  129.         }
  130.         else {
  131.             throw new jException('jelix~db.error.connection',$this->profile['host']);
  132.         }
  133.  
  134.         if (isset($this->profile['search_path']&& trim($this->profile['search_path']!= ''{
  135.             $sql 'SET search_path TO '.$this->profile['search_path'];
  136.             if (@pg_query($cnx$sql)) {
  137.                 throw new jException('jelix~db.error.query.bad',  pg_last_error($cnx).'('.$sql.')');
  138.             }
  139.         }
  140.         return $cnx;
  141.     }
  142.  
  143.     protected function _disconnect (){
  144.         return pg_close ($this->_connection);
  145.     }
  146.  
  147.     protected function _doQuery ($queryString){
  148.         if ($qI @pg_query ($this->_connection$queryString)){
  149.             $rsnew pgsqlDbResultSet ($qI);
  150.             $rs->_connector $this;
  151.         }else{
  152.             $rs false;
  153.             throw new jException('jelix~db.error.query.bad',  pg_last_error($this->_connection).'('.$queryString.')');
  154.         }
  155.         return $rs;
  156.     }
  157.  
  158.     protected function _doExec($query){
  159.         if($rs $this->_doQuery($query)){
  160.             return pg_affected_rows($rs->id());
  161.         }else
  162.             return 0;
  163.     }
  164.  
  165.     protected function _doLimitQuery ($queryString$offset$number){
  166.         if($number 0)
  167.             $number='ALL';
  168.         $queryString.= ' LIMIT '.$number.' OFFSET '.$offset;
  169.         $result $this->_doQuery($queryString);
  170.         return $result;
  171.     }
  172.  
  173.     public function lastInsertId($seqname=''){
  174.  
  175.         if($seqname == ''){
  176.             trigger_error(get_class($this).'::lastInstertId invalide sequence name',E_USER_WARNING);
  177.             return false;
  178.         }
  179.         $cur=$this->query("select currval('$seqname') as id");
  180.         if($cur){
  181.             $res=$cur->fetch();
  182.             if($res)
  183.                 return $res->id;
  184.             else
  185.                 return false;
  186.         }else{
  187.             trigger_error(get_class($this).'::lastInstertId invalide sequence name',E_USER_WARNING);
  188.             return false;
  189.         }
  190.     }
  191.  
  192.     protected function _autoCommitNotify ($state){
  193.  
  194.         $this->_doExec('SET AUTOCOMMIT TO '.($state 'ON' 'OFF'));
  195.     }
  196.  
  197.     protected function _quote($text$binary{
  198.         if ($binary)
  199.             return pg_escape_bytea($this->_connection$text);
  200.         else
  201.             return pg_escape_string($this->_connection$text);
  202.     }
  203. }

Documentation generated on Thu, 22 Mar 2012 22:18:00 +0100 by phpDocumentor 1.4.3