Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Db / Adapter / Platform / Postgresql.php
blob71247a5fc4140cf364082366835a97f67498a5f9
1 <?php
2 /**
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
8 */
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)
24 if ($driver) {
25 $this->setDriver($driver);
29 /**
30 * @param \Zend\Db\Adapter\Driver\Pgsql\Pgsql|\Zend\Db\Adapter\Driver\Pdo\Pdo|resource|\PDO $driver
31 * @throws \Zend\Db\Adapter\Exception\InvalidArgumentException
32 * @return $this
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')
40 ) {
41 $this->resource = $driver;
42 return $this;
45 throw new Exception\InvalidArgumentException('$driver must be a Pgsql or Postgresql PDO Zend\Db\Adapter\Driver, pgsql link resource or Postgresql PDO instance');
48 /**
49 * Get name
51 * @return string
53 public function getName()
55 return 'PostgreSQL';
58 /**
59 * Get quote indentifier symbol
61 * @return string
63 public function getQuoteIdentifierSymbol()
65 return '"';
68 /**
69 * Quote identifier
71 * @param string $identifier
72 * @return string
74 public function quoteIdentifier($identifier)
76 return '"' . str_replace('"', '\\' . '"', $identifier) . '"';
79 /**
80 * Quote identifier chain
82 * @param string|string[] $identifierChain
83 * @return string
85 public function quoteIdentifierChain($identifierChain)
87 $identifierChain = str_replace('"', '\\"', $identifierChain);
88 if (is_array($identifierChain)) {
89 $identifierChain = implode('"."', $identifierChain);
91 return '"' . $identifierChain . '"';
94 /**
95 * Get quote value symbol
97 * @return string
99 public function getQuoteValueSymbol()
101 return '\'';
105 * Quote value
107 * @param string $value
108 * @return string
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);
121 trigger_error(
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
133 * @param $value
134 * @return mixed
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") . '\'';
151 * Quote value list
153 * @param string|string[] $valueList
154 * @return string
156 public function quoteValueList($valueList)
158 if (!is_array($valueList)) {
159 return $this->quoteValue($valueList);
162 $value = reset($valueList);
163 do {
164 $valueList[key($valueList)] = $this->quoteValue($value);
165 } while ($value = next($valueList));
166 return implode(', ', $valueList);
170 * Get identifier separator
172 * @return string
174 public function getIdentifierSeparator()
176 return '.';
180 * Quote identifier in fragment
182 * @param string $identifier
183 * @param array $safeWords
184 * @return string
186 public function quoteIdentifierInFragment($identifier, array $safeWords = array())
188 $parts = preg_split('#([\.\s\W])#', $identifier, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
189 if ($safeWords) {
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)])) {
195 continue;
197 switch ($part) {
198 case ' ':
199 case '.':
200 case '*':
201 case 'AS':
202 case 'As':
203 case 'aS':
204 case 'as':
205 break;
206 default:
207 $parts[$i] = '"' . str_replace('"', '\\' . '"', $part) . '"';
210 return implode('', $parts);