3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Db\Adapter\Platform
;
12 use Zend\Db\Adapter\Driver\DriverInterface
;
13 use Zend\Db\Adapter\Driver\Pdo
;
14 use Zend\Db\Adapter\Driver\Pgsql
;
15 use Zend\Db\Adapter\Exception
;
17 class Postgresql
implements PlatformInterface
19 /** @var resource|\PDO */
20 protected $resource = null;
22 public function __construct($driver = null)
25 $this->setDriver($driver);
30 * @param \Zend\Db\Adapter\Driver\Pgsql\Pgsql|\Zend\Db\Adapter\Driver\Pdo\Pdo|resource|\PDO $driver
31 * @throws \Zend\Db\Adapter\Exception\InvalidArgumentException
34 public function setDriver($driver)
36 if ($driver instanceof Pgsql\Pgsql
37 ||
($driver instanceof Pdo\Pdo
&& $driver->getDatabasePlatformName() == 'Postgresql')
38 ||
(is_resource($driver) && (in_array(get_resource_type($driver), array('pgsql link', 'pgsql link persistent'))))
39 ||
($driver instanceof \PDO
&& $driver->getAttribute(\PDO
::ATTR_DRIVER_NAME
) == 'pgsql')
41 $this->resource = $driver;
45 throw new Exception\
InvalidArgumentException('$driver must be a Pgsql or Postgresql PDO Zend\Db\Adapter\Driver, pgsql link resource or Postgresql PDO instance');
53 public function getName()
59 * Get quote indentifier symbol
63 public function getQuoteIdentifierSymbol()
71 * @param string $identifier
74 public function quoteIdentifier($identifier)
76 return '"' . str_replace('"', '\\' . '"', $identifier) . '"';
80 * Quote identifier chain
82 * @param string|string[] $identifierChain
85 public function quoteIdentifierChain($identifierChain)
87 $identifierChain = str_replace('"', '\\"', $identifierChain);
88 if (is_array($identifierChain)) {
89 $identifierChain = implode('"."', $identifierChain);
91 return '"' . $identifierChain . '"';
95 * Get quote value symbol
99 public function getQuoteValueSymbol()
107 * @param string $value
110 public function quoteValue($value)
112 if ($this->resource instanceof DriverInterface
) {
113 $this->resource = $this->resource->getConnection()->getResource();
115 if (is_resource($this->resource)) {
116 return '\'' . pg_escape_string($this->resource, $value) . '\'';
118 if ($this->resource instanceof \PDO
) {
119 return $this->resource->quote($value);
122 'Attempting to quote a value in ' . __CLASS__
. ' without extension/driver support '
123 . 'can introduce security vulnerabilities in a production environment.'
125 return '\'' . addcslashes($value, "\x00\n\r\\'\"\x1a") . '\'';
129 * Quote Trusted Value
131 * The ability to quote values without notices
136 public function quoteTrustedValue($value)
138 if ($this->resource instanceof DriverInterface
) {
139 $this->resource = $this->resource->getConnection()->getResource();
141 if (is_resource($this->resource)) {
142 return '\'' . pg_escape_string($this->resource, $value) . '\'';
144 if ($this->resource instanceof \PDO
) {
145 return $this->resource->quote($value);
147 return '\'' . addcslashes($value, "\x00\n\r\\'\"\x1a") . '\'';
153 * @param string|string[] $valueList
156 public function quoteValueList($valueList)
158 if (!is_array($valueList)) {
159 return $this->quoteValue($valueList);
162 $value = reset($valueList);
164 $valueList[key($valueList)] = $this->quoteValue($value);
165 } while ($value = next($valueList));
166 return implode(', ', $valueList);
170 * Get identifier separator
174 public function getIdentifierSeparator()
180 * Quote identifier in fragment
182 * @param string $identifier
183 * @param array $safeWords
186 public function quoteIdentifierInFragment($identifier, array $safeWords = array())
188 $parts = preg_split('#([\.\s\W])#', $identifier, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);
190 $safeWords = array_flip($safeWords);
191 $safeWords = array_change_key_case($safeWords, CASE_LOWER
);
193 foreach ($parts as $i => $part) {
194 if ($safeWords && isset($safeWords[strtolower($part)])) {
207 $parts[$i] = '"' . str_replace('"', '\\' . '"', $part) . '"';
210 return implode('', $parts);