SqlDialectTrait.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * PHP Version 5.4
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 3.0.0
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. namespace Cake\Database;
  18. trait SqlDialectTrait {
  19. /**
  20. * Quotes a database identifier (a column name, table name, etc..) to
  21. * be used safely in queries without the risk of using reserver words
  22. *
  23. * @param string $identifier
  24. * @return string
  25. */
  26. public function quoteIdentifier($identifier) {
  27. $identifier = trim($identifier);
  28. if ($identifier === '*') {
  29. return '*';
  30. }
  31. if (preg_match('/^[\w-]+(?:\.[^ \*]*)*$/', $identifier)) { // string, string.string
  32. if (strpos($identifier, '.') === false) { // string
  33. return $this->_startQuote . $identifier . $this->_endQuote;
  34. }
  35. $items = explode('.', $identifier);
  36. return $this->_startQuote . implode($this->_endQuote . '.' . $this->_startQuote, $items) . $this->_endQuote;
  37. }
  38. if (preg_match('/^[\w-]+\.\*$/', $identifier)) { // string.*
  39. return $this->_startQuote . str_replace('.*', $this->_endQuote . '.*', $identifier);
  40. }
  41. if (preg_match('/^([\w-]+)\((.*)\)$/', $identifier, $matches)) { // Functions
  42. return $matches[1] . '(' . $this->quoteIdentifier($matches[2]) . ')';
  43. }
  44. if (preg_match('/^([\w-]+(\.[\w-]+|\(.*\))*)\s+AS\s*([\w-]+)$/i', $identifier, $matches)) {
  45. return preg_replace(
  46. '/\s{2,}/', ' ', $this->quoteIdentifier($matches[1]) . ' AS ' . $this->quoteIdentifier($matches[3])
  47. );
  48. }
  49. if (preg_match('/^[\w-_\s]*[\w-_]+/', $identifier)) {
  50. return $this->_startQuote . $identifier . $this->_endQuote;
  51. }
  52. return $identifier;
  53. }
  54. /**
  55. * Returns a callable function that will be used to transform a passed Query object.
  56. * This function, in turn, will return an instance of a Query object that has been
  57. * transformed to accommodate any specificities of the SQL dialect in use.
  58. *
  59. * @param string $type the type of query to be transformed
  60. * (select, insert, update, delete)
  61. * @return callable
  62. */
  63. public function queryTranslator($type) {
  64. return function($query) use ($type) {
  65. $query = $this->{'_' . $type . 'QueryTranslator'}($query);
  66. if (!$this->_expressionTranslators()) {
  67. return $query;
  68. }
  69. $query->traverseExpressions(function($expression) {
  70. foreach ($this->_expressionTranslators() as $class => $method) {
  71. if ($expression instanceof $class) {
  72. $this->{$method}($expression);
  73. }
  74. }
  75. });
  76. return $query;
  77. };
  78. }
  79. /**
  80. * Returns an associative array of methods that will transform Expression
  81. * objects to conform with the specific SQL dialect. Keys are class names
  82. * and values a method in this class.
  83. *
  84. * @return void
  85. */
  86. protected function _expressionTranslators() {
  87. return [];
  88. }
  89. /**
  90. * Apply translation steps to select queries.
  91. *
  92. * @param Query $query The query to translate
  93. * @return Query The modified query
  94. */
  95. protected function _selectQueryTranslator($query) {
  96. if (is_array($query->clause('distinct'))) {
  97. $query->group($query->clause('distinct'), true);
  98. $query->distinct(false);
  99. }
  100. return $query;
  101. }
  102. /**
  103. * Apply translation steps to delete queries.
  104. *
  105. * @param Query $query The query to translate
  106. * @return Query The modified query
  107. */
  108. protected function _deleteQueryTranslator($query) {
  109. return $query;
  110. }
  111. /**
  112. * Apply translation steps to update queries.
  113. *
  114. * @param Query $query The query to translate
  115. * @return Query The modified query
  116. */
  117. protected function _updateQueryTranslator($query) {
  118. return $query;
  119. }
  120. /**
  121. * Apply translation steps to insert queries.
  122. *
  123. * @param Query $query The query to translate
  124. * @return Query The modified query
  125. */
  126. protected function _insertQueryTranslator($query) {
  127. return $query;
  128. }
  129. /**
  130. * Returns a SQL snippet for creating a new transaction savepoint
  131. *
  132. * @param string save point name
  133. * @return string
  134. */
  135. public function savePointSQL($name) {
  136. return 'SAVEPOINT LEVEL' . $name;
  137. }
  138. /**
  139. * Returns a SQL snippet for releasing a previously created save point
  140. *
  141. * @param string save point name
  142. * @return string
  143. */
  144. public function releaseSavePointSQL($name) {
  145. return 'RELEASE SAVEPOINT LEVEL' . $name;
  146. }
  147. /**
  148. * Returns a SQL snippet for rollbacking a previously created save point
  149. *
  150. * @param string save point name
  151. * @return string
  152. */
  153. public function rollbackSavePointSQL($name) {
  154. return 'ROLLBACK TO SAVEPOINT LEVEL' . $name;
  155. }
  156. }