2023-03-11 12:04:29 +03:00
< ? php
/**
* Zend Framework ( http :// framework . zend . com / )
*
* @ link http :// github . com / zendframework / zf2 for the canonical source repository
2023-04-01 09:03:34 +03:00
* @ copyright Copyright ( c ) 2005 - 2015 Zend Technologies USA Inc . ( http :// www . zend . com )
2023-03-11 12:04:29 +03:00
* @ license http :// framework . zend . com / license / new - bsd New BSD License
*/
namespace Zend\Db\Adapter\Platform ;
use Zend\Db\Adapter\Driver\DriverInterface ;
use Zend\Db\Adapter\Driver\Pdo ;
use Zend\Db\Adapter\Driver\Pgsql ;
use Zend\Db\Adapter\Exception ;
2023-04-01 09:03:34 +03:00
class Postgresql extends AbstractPlatform
2023-03-11 12:04:29 +03:00
{
2023-04-01 09:03:34 +03:00
/**
* Overrides value from AbstractPlatform to use proper escaping for Postgres
*
* @ var string
*/
protected $quoteIdentifierTo = '""' ;
/**
* @ var resource | \PDO
*/
2023-03-11 12:04:29 +03:00
protected $resource = null ;
2023-04-01 09:03:34 +03:00
/**
* @ param null | \Zend\Db\Adapter\Driver\Pgsql\Pgsql | \Zend\Db\Adapter\Driver\Pdo\Pdo | resource | \PDO $driver
*/
2023-03-11 12:04:29 +03:00
public function __construct ( $driver = null )
{
if ( $driver ) {
$this -> setDriver ( $driver );
}
}
/**
* @ param \Zend\Db\Adapter\Driver\Pgsql\Pgsql | \Zend\Db\Adapter\Driver\Pdo\Pdo | resource | \PDO $driver
* @ throws \Zend\Db\Adapter\Exception\InvalidArgumentException
* @ return $this
*/
public function setDriver ( $driver )
{
if ( $driver instanceof Pgsql\Pgsql
|| ( $driver instanceof Pdo\Pdo && $driver -> getDatabasePlatformName () == 'Postgresql' )
|| ( is_resource ( $driver ) && ( in_array ( get_resource_type ( $driver ), array ( 'pgsql link' , 'pgsql link persistent' ))))
|| ( $driver instanceof \PDO && $driver -> getAttribute ( \PDO :: ATTR_DRIVER_NAME ) == 'pgsql' )
) {
$this -> resource = $driver ;
return $this ;
}
throw new Exception\InvalidArgumentException ( '$driver must be a Pgsql or Postgresql PDO Zend\Db\Adapter\Driver, pgsql link resource or Postgresql PDO instance' );
}
/**
2023-04-01 09:03:34 +03:00
* { @ inheritDoc }
2023-03-11 12:04:29 +03:00
*/
public function getName ()
{
return 'PostgreSQL' ;
}
/**
2023-04-01 09:03:34 +03:00
* { @ inheritDoc }
2023-03-11 12:04:29 +03:00
*/
public function quoteIdentifierChain ( $identifierChain )
{
2023-04-01 09:03:34 +03:00
return '"' . implode ( '"."' , ( array ) str_replace ( '"' , '""' , $identifierChain )) . '"' ;
2023-03-11 12:04:29 +03:00
}
/**
2023-04-01 09:03:34 +03:00
* { @ inheritDoc }
2023-03-11 12:04:29 +03:00
*/
public function quoteValue ( $value )
{
if ( $this -> resource instanceof DriverInterface ) {
$this -> resource = $this -> resource -> getConnection () -> getResource ();
}
if ( is_resource ( $this -> resource )) {
return '\'' . pg_escape_string ( $this -> resource , $value ) . '\'' ;
}
if ( $this -> resource instanceof \PDO ) {
return $this -> resource -> quote ( $value );
}
2023-04-01 09:03:34 +03:00
return 'E' . parent :: quoteValue ( $value );
2023-03-11 12:04:29 +03:00
}
/**
2023-04-01 09:03:34 +03:00
* { @ inheritDoc }
2023-03-11 12:04:29 +03:00
*/
public function quoteTrustedValue ( $value )
{
if ( $this -> resource instanceof DriverInterface ) {
$this -> resource = $this -> resource -> getConnection () -> getResource ();
}
if ( is_resource ( $this -> resource )) {
return '\'' . pg_escape_string ( $this -> resource , $value ) . '\'' ;
}
if ( $this -> resource instanceof \PDO ) {
return $this -> resource -> quote ( $value );
}
2023-04-01 09:03:34 +03:00
return 'E' . parent :: quoteTrustedValue ( $value );
2023-03-11 12:04:29 +03:00
}
}