/library/Zend/Db/Adapter/Platform/Postgresql.php

https://bitbucket.org/saifshuvo/zf2 · PHP · 213 lines · 132 code · 17 blank · 64 comment · 20 complexity · aef5e08cb9a067997dab563c3b6929e9 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  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. */
  9. namespace Zend\Db\Adapter\Platform;
  10. use Zend\Db\Adapter\Driver\DriverInterface;
  11. use Zend\Db\Adapter\Driver\Pdo;
  12. use Zend\Db\Adapter\Driver\Pgsql;
  13. use Zend\Db\Adapter\Exception;
  14. class Postgresql implements PlatformInterface
  15. {
  16. /** @var resource|\PDO */
  17. protected $resource = null;
  18. public function __construct($driver = null)
  19. {
  20. if ($driver) {
  21. $this->setDriver($driver);
  22. }
  23. }
  24. /**
  25. * @param \Zend\Db\Adapter\Driver\Pgsql\Pgsql|\Zend\Db\Adapter\Driver\Pdo\Pdo|resource|\PDO $driver
  26. * @throws \Zend\Db\Adapter\Exception\InvalidArgumentException
  27. * @return $this
  28. */
  29. public function setDriver($driver)
  30. {
  31. if ($driver instanceof Pgsql\Pgsql
  32. || ($driver instanceof Pdo\Pdo && $driver->getDatabasePlatformName() == 'Postgresql')
  33. || (is_resource($driver) && (in_array(get_resource_type($driver), array('pgsql link', 'pgsql link persistent'))))
  34. || ($driver instanceof \PDO && $driver->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'pgsql')
  35. ) {
  36. $this->resource = $driver;
  37. return $this;
  38. }
  39. throw new Exception\InvalidArgumentException('$driver must be a Pgsql or Postgresql PDO Zend\Db\Adapter\Driver, pgsql link resource or Postgresql PDO instance');
  40. }
  41. /**
  42. * Get name
  43. *
  44. * @return string
  45. */
  46. public function getName()
  47. {
  48. return 'PostgreSQL';
  49. }
  50. /**
  51. * Get quote indentifier symbol
  52. *
  53. * @return string
  54. */
  55. public function getQuoteIdentifierSymbol()
  56. {
  57. return '"';
  58. }
  59. /**
  60. * Quote identifier
  61. *
  62. * @param string $identifier
  63. * @return string
  64. */
  65. public function quoteIdentifier($identifier)
  66. {
  67. return '"' . str_replace('"', '\\' . '"', $identifier) . '"';
  68. }
  69. /**
  70. * Quote identifier chain
  71. *
  72. * @param string|string[] $identifierChain
  73. * @return string
  74. */
  75. public function quoteIdentifierChain($identifierChain)
  76. {
  77. $identifierChain = str_replace('"', '\\"', $identifierChain);
  78. if (is_array($identifierChain)) {
  79. $identifierChain = implode('"."', $identifierChain);
  80. }
  81. return '"' . $identifierChain . '"';
  82. }
  83. /**
  84. * Get quote value symbol
  85. *
  86. * @return string
  87. */
  88. public function getQuoteValueSymbol()
  89. {
  90. return '\'';
  91. }
  92. /**
  93. * Quote value
  94. *
  95. * @param string $value
  96. * @return string
  97. */
  98. public function quoteValue($value)
  99. {
  100. if ($this->resource instanceof DriverInterface) {
  101. $this->resource = $this->resource->getConnection()->getResource();
  102. }
  103. if (is_resource($this->resource)) {
  104. return '\'' . pg_escape_string($this->resource, $value) . '\'';
  105. }
  106. if ($this->resource instanceof \PDO) {
  107. return $this->resource->quote($value);
  108. }
  109. trigger_error(
  110. 'Attempting to quote a value in ' . __CLASS__ . ' without extension/driver support '
  111. . 'can introduce security vulnerabilities in a production environment.'
  112. );
  113. return '\'' . addcslashes($value, "\x00\n\r\\'\"\x1a") . '\'';
  114. }
  115. /**
  116. * Quote Trusted Value
  117. *
  118. * The ability to quote values without notices
  119. *
  120. * @param $value
  121. * @return mixed
  122. */
  123. public function quoteTrustedValue($value)
  124. {
  125. if ($this->resource instanceof DriverInterface) {
  126. $this->resource = $this->resource->getConnection()->getResource();
  127. }
  128. if (is_resource($this->resource)) {
  129. return '\'' . pg_escape_string($this->resource, $value) . '\'';
  130. }
  131. if ($this->resource instanceof \PDO) {
  132. return $this->resource->quote($value);
  133. }
  134. return '\'' . addcslashes($value, "\x00\n\r\\'\"\x1a") . '\'';
  135. }
  136. /**
  137. * Quote value list
  138. *
  139. * @param string|string[] $valueList
  140. * @return string
  141. */
  142. public function quoteValueList($valueList)
  143. {
  144. if (!is_array($valueList)) {
  145. return $this->quoteValue($valueList);
  146. }
  147. $value = reset($valueList);
  148. do {
  149. $valueList[key($valueList)] = $this->quoteValue($value);
  150. } while ($value = next($valueList));
  151. return implode(', ', $valueList);
  152. }
  153. /**
  154. * Get identifier separator
  155. *
  156. * @return string
  157. */
  158. public function getIdentifierSeparator()
  159. {
  160. return '.';
  161. }
  162. /**
  163. * Quote identifier in fragment
  164. *
  165. * @param string $identifier
  166. * @param array $safeWords
  167. * @return string
  168. */
  169. public function quoteIdentifierInFragment($identifier, array $safeWords = array())
  170. {
  171. $parts = preg_split('#([\.\s\W])#', $identifier, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  172. if ($safeWords) {
  173. $safeWords = array_flip($safeWords);
  174. $safeWords = array_change_key_case($safeWords, CASE_LOWER);
  175. }
  176. foreach ($parts as $i => $part) {
  177. if ($safeWords && isset($safeWords[strtolower($part)])) {
  178. continue;
  179. }
  180. switch ($part) {
  181. case ' ':
  182. case '.':
  183. case '*':
  184. case 'AS':
  185. case 'As':
  186. case 'aS':
  187. case 'as':
  188. break;
  189. default:
  190. $parts[$i] = '"' . str_replace('"', '\\' . '"', $part) . '"';
  191. }
  192. }
  193. return implode('', $parts);
  194. }
  195. }