| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- /**
- * PHP Version 5.4
- *
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since CakePHP(tm) v 3.0.0
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- namespace Cake\Database;
- trait SqlDialectTrait {
- /**
- * Quotes a database identifier (a column name, table name, etc..) to
- * be used safely in queries without the risk of using reserver words
- *
- * @param string $identifier
- * @return string
- */
- public function quoteIdentifier($identifier) {
- $identifier = trim($identifier);
- if ($identifier === '*') {
- return '*';
- }
- if (preg_match('/^[\w-]+(?:\.[^ \*]*)*$/', $identifier)) { // string, string.string
- if (strpos($identifier, '.') === false) { // string
- return $this->_startQuote . $identifier . $this->_endQuote;
- }
- $items = explode('.', $identifier);
- return $this->_startQuote . implode($this->_endQuote . '.' . $this->_startQuote, $items) . $this->_endQuote;
- }
- if (preg_match('/^[\w-]+\.\*$/', $identifier)) { // string.*
- return $this->_startQuote . str_replace('.*', $this->_endQuote . '.*', $identifier);
- }
- if (preg_match('/^([\w-]+)\((.*)\)$/', $identifier, $matches)) { // Functions
- return $matches[1] . '(' . $this->quoteIdentifier($matches[2]) . ')';
- }
- if (preg_match('/^([\w-]+(\.[\w-]+|\(.*\))*)\s+AS\s*([\w-]+)$/i', $identifier, $matches)) {
- return preg_replace(
- '/\s{2,}/', ' ', $this->quoteIdentifier($matches[1]) . ' AS ' . $this->quoteIdentifier($matches[3])
- );
- }
- if (preg_match('/^[\w-_\s]*[\w-_]+/', $identifier)) {
- return $this->_startQuote . $identifier . $this->_endQuote;
- }
- return $identifier;
- }
- /**
- * Returns a callable function that will be used to transform a passed Query object.
- * This function, in turn, will return an instance of a Query object that has been
- * transformed to accommodate any specificities of the SQL dialect in use.
- *
- * @param string $type the type of query to be transformed
- * (select, insert, update, delete)
- * @return callable
- */
- public function queryTranslator($type) {
- return function($query) use ($type) {
- $query = $this->{'_' . $type . 'QueryTranslator'}($query);
- if (!$this->_expressionTranslators()) {
- return $query;
- }
- $query->traverseExpressions(function($expression) {
- foreach ($this->_expressionTranslators() as $class => $method) {
- if ($expression instanceof $class) {
- $this->{$method}($expression);
- }
- }
- });
- return $query;
- };
- }
- /**
- * Returns an associative array of methods that will transform Expression
- * objects to conform with the specific SQL dialect. Keys are class names
- * and values a method in this class.
- *
- * @return void
- */
- protected function _expressionTranslators() {
- return [];
- }
- /**
- * Apply translation steps to select queries.
- *
- * @param Query $query The query to translate
- * @return Query The modified query
- */
- protected function _selectQueryTranslator($query) {
- if (is_array($query->clause('distinct'))) {
- $query->group($query->clause('distinct'), true);
- $query->distinct(false);
- }
- return $query;
- }
- /**
- * Apply translation steps to delete queries.
- *
- * @param Query $query The query to translate
- * @return Query The modified query
- */
- protected function _deleteQueryTranslator($query) {
- return $query;
- }
- /**
- * Apply translation steps to update queries.
- *
- * @param Query $query The query to translate
- * @return Query The modified query
- */
- protected function _updateQueryTranslator($query) {
- return $query;
- }
- /**
- * Apply translation steps to insert queries.
- *
- * @param Query $query The query to translate
- * @return Query The modified query
- */
- protected function _insertQueryTranslator($query) {
- return $query;
- }
- /**
- * Returns a SQL snippet for creating a new transaction savepoint
- *
- * @param string save point name
- * @return string
- */
- public function savePointSQL($name) {
- return 'SAVEPOINT LEVEL' . $name;
- }
- /**
- * Returns a SQL snippet for releasing a previously created save point
- *
- * @param string save point name
- * @return string
- */
- public function releaseSavePointSQL($name) {
- return 'RELEASE SAVEPOINT LEVEL' . $name;
- }
- /**
- * Returns a SQL snippet for rollbacking a previously created save point
- *
- * @param string save point name
- * @return string
- */
- public function rollbackSavePointSQL($name) {
- return 'ROLLBACK TO SAVEPOINT LEVEL' . $name;
- }
- }
|