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

https://bitbucket.org/gencer/zf2 · PHP · 189 lines · 110 code · 15 blank · 64 comment · 14 complexity · 1393b810dcf0f6ee3998942822e17771 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-2014 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. class Oracle implements PlatformInterface
  11. {
  12. /**
  13. * @var bool
  14. */
  15. protected $quoteIdentifiers = true;
  16. /**
  17. * @param array $options
  18. */
  19. public function __construct($options = array())
  20. {
  21. if (isset($options['quote_identifiers'])
  22. && ($options['quote_identifiers'] == false
  23. || $options['quote_identifiers'] === 'false')
  24. ) {
  25. $this->quoteIdentifiers = false;
  26. }
  27. }
  28. /**
  29. * Get name
  30. *
  31. * @return string
  32. */
  33. public function getName()
  34. {
  35. return 'Oracle';
  36. }
  37. /**
  38. * Get quote identifier symbol
  39. *
  40. * @return string
  41. */
  42. public function getQuoteIdentifierSymbol()
  43. {
  44. return '"';
  45. }
  46. /**
  47. * Quote identifier
  48. *
  49. * @param string $identifier
  50. * @return string
  51. */
  52. public function quoteIdentifier($identifier)
  53. {
  54. if ($this->quoteIdentifiers === false) {
  55. return $identifier;
  56. }
  57. return '"' . str_replace('"', '\\' . '"', $identifier) . '"';
  58. }
  59. /**
  60. * Quote identifier chain
  61. *
  62. * @param string|string[] $identifierChain
  63. * @return string
  64. */
  65. public function quoteIdentifierChain($identifierChain)
  66. {
  67. if ($this->quoteIdentifiers === false) {
  68. return (is_array($identifierChain)) ? implode('.', $identifierChain) : $identifierChain;
  69. }
  70. $identifierChain = str_replace('"', '\\"', $identifierChain);
  71. if (is_array($identifierChain)) {
  72. $identifierChain = implode('"."', $identifierChain);
  73. }
  74. return '"' . $identifierChain . '"';
  75. }
  76. /**
  77. * Get quote value symbol
  78. *
  79. * @return string
  80. */
  81. public function getQuoteValueSymbol()
  82. {
  83. return '\'';
  84. }
  85. /**
  86. * Quote value
  87. *
  88. * @param string $value
  89. * @return string
  90. */
  91. public function quoteValue($value)
  92. {
  93. trigger_error(
  94. 'Attempting to quote a value in ' . __CLASS__ . ' without extension/driver support '
  95. . 'can introduce security vulnerabilities in a production environment.'
  96. );
  97. return '\'' . addcslashes($value, "\x00\n\r\\'\"\x1a") . '\'';
  98. }
  99. /**
  100. * Quote Trusted Value
  101. *
  102. * The ability to quote values without notices
  103. *
  104. * @param $value
  105. * @return mixed
  106. */
  107. public function quoteTrustedValue($value)
  108. {
  109. return '\'' . addcslashes($value, "\x00\n\r\\'\"\x1a") . '\'';
  110. }
  111. /**
  112. * Quote value list
  113. *
  114. * @param string|string[] $valueList
  115. * @return string
  116. */
  117. public function quoteValueList($valueList)
  118. {
  119. if (!is_array($valueList)) {
  120. return $this->quoteValue($valueList);
  121. }
  122. $value = reset($valueList);
  123. do {
  124. $valueList[key($valueList)] = $this->quoteValue($value);
  125. } while ($value = next($valueList));
  126. return implode(', ', $valueList);
  127. }
  128. /**
  129. * Get identifier separator
  130. *
  131. * @return string
  132. */
  133. public function getIdentifierSeparator()
  134. {
  135. return '.';
  136. }
  137. /**
  138. * Quote identifier in fragment
  139. *
  140. * @param string $identifier
  141. * @param array $safeWords
  142. * @return string
  143. */
  144. public function quoteIdentifierInFragment($identifier, array $safeWords = array())
  145. {
  146. if ($this->quoteIdentifiers === false) {
  147. return $identifier;
  148. }
  149. $parts = preg_split('#([\.\s\W])#', $identifier, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  150. if ($safeWords) {
  151. $safeWords = array_flip($safeWords);
  152. $safeWords = array_change_key_case($safeWords, CASE_LOWER);
  153. }
  154. foreach ($parts as $i => $part) {
  155. if ($safeWords && isset($safeWords[strtolower($part)])) {
  156. continue;
  157. }
  158. switch ($part) {
  159. case ' ':
  160. case '.':
  161. case '*':
  162. case 'AS':
  163. case 'As':
  164. case 'aS':
  165. case 'as':
  166. break;
  167. default:
  168. $parts[$i] = '"' . str_replace('"', '\\' . '"', $part) . '"';
  169. }
  170. }
  171. return implode('', $parts);
  172. }
  173. }