|
| 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\Dialect; |
| 18 | + |
| 19 | +use Cake\Database\Expression\FunctionExpression; |
| 20 | +use Cake\Database\Expression\UnaryExpression; |
| 21 | +use Cake\Database\Query; |
| 22 | +use Cake\Database\SqlDialectTrait; |
| 23 | + |
| 24 | +/** |
| 25 | + * Contains functions that encapsulates the SQL dialect used by Postgres, |
| 26 | + * including query translators and schema introspection. |
| 27 | + */ |
| 28 | +trait PostgresDialectTrait { |
| 29 | + |
| 30 | + use SqlDialectTrait; |
| 31 | + |
| 32 | +/** |
| 33 | + * String used to start a database identifier quoting to make it safe |
| 34 | + * |
| 35 | + * @var string |
| 36 | + */ |
| 37 | + protected $_startQuote = '"'; |
| 38 | + |
| 39 | +/** |
| 40 | + * String used to end a database identifier quoting to make it safe |
| 41 | + * |
| 42 | + * @var string |
| 43 | + */ |
| 44 | + protected $_endQuote = '"'; |
| 45 | + |
| 46 | +/** |
| 47 | + * The schema dialect class for this driver |
| 48 | + * |
| 49 | + * @var \Cake\Database\Schema\PostgresSchema |
| 50 | + */ |
| 51 | + protected $_schemaDialect; |
| 52 | + |
| 53 | +/** |
| 54 | + * Distinct clause needs no transformation |
| 55 | + * |
| 56 | + * @param Query $query The query to be transformed |
| 57 | + * @return Query |
| 58 | + */ |
| 59 | + protected function _transformDistinct($query) { |
| 60 | + return $query; |
| 61 | + } |
| 62 | + |
| 63 | +/** |
| 64 | + * Modifies the original insert query to append a "RETURNING *" epilogue |
| 65 | + * so that the latest insert id can be retrieved |
| 66 | + * |
| 67 | + * @param \Cake\Database\Query $query |
| 68 | + * @return \Cake\Database\Query |
| 69 | + */ |
| 70 | + protected function _insertQueryTranslator($query) { |
| 71 | + if (!$query->clause('epilog')) { |
| 72 | + $query->epilog('RETURNING *'); |
| 73 | + } |
| 74 | + return $query; |
| 75 | + } |
| 76 | + |
| 77 | +/** |
| 78 | + * Returns an dictionary of expressions to be transformed when compiling a Query |
| 79 | + * to SQL. Array keys are method names to be called in this class |
| 80 | + * |
| 81 | + * @return array |
| 82 | + */ |
| 83 | + protected function _expressionTranslators() { |
| 84 | + $namespace = 'Cake\Database\Expression'; |
| 85 | + return [ |
| 86 | + $namespace . '\FunctionExpression' => '_transformFunctionExpression' |
| 87 | + ]; |
| 88 | + } |
| 89 | + |
| 90 | +/** |
| 91 | + * Receives a FunctionExpression and changes it so that it conforms to this |
| 92 | + * SQL dialect. |
| 93 | + * |
| 94 | + * @param \Cake\Database\Expression\FunctionExpression |
| 95 | + * @return void |
| 96 | + */ |
| 97 | + protected function _transformFunctionExpression(FunctionExpression $expression) { |
| 98 | + switch ($expression->name()) { |
| 99 | + case 'CONCAT': |
| 100 | + // CONCAT function is expressed as exp1 || exp2 |
| 101 | + $expression->name('')->type(' ||'); |
| 102 | + break; |
| 103 | + case 'DATEDIFF': |
| 104 | + $expression |
| 105 | + ->name('') |
| 106 | + ->type('-') |
| 107 | + ->iterateParts(function($p) { |
| 108 | + return new FunctionExpression('DATE', [$p['value']], [$p['type']]); |
| 109 | + }); |
| 110 | + break; |
| 111 | + case 'CURRENT_DATE': |
| 112 | + $time = new FunctionExpression('LOCALTIMESTAMP', [' 0 ' => 'literal']); |
| 113 | + $expression->name('CAST')->type(' AS ')->add([$time, 'date' => 'literal']); |
| 114 | + break; |
| 115 | + case 'CURRENT_TIME': |
| 116 | + $time = new FunctionExpression('LOCALTIMESTAMP', [' 0 ' => 'literal']); |
| 117 | + $expression->name('CAST')->type(' AS ')->add([$time, 'time' => 'literal']); |
| 118 | + break; |
| 119 | + case 'NOW': |
| 120 | + $expression->name('LOCALTIMESTAMP')->add([' 0 ' => 'literal']); |
| 121 | + break; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | +/** |
| 126 | + * Get the schema dialect. |
| 127 | + * |
| 128 | + * Used by Cake\Database\Schema package to reflect schema and |
| 129 | + * generate schema. |
| 130 | + * |
| 131 | + * @return \Cake\Database\Schema\PostgresSchema |
| 132 | + */ |
| 133 | + public function schemaDialect() { |
| 134 | + if (!$this->_schemaDialect) { |
| 135 | + $this->_schemaDialect = new \Cake\Database\Schema\PostgresSchema($this); |
| 136 | + } |
| 137 | + return $this->_schemaDialect; |
| 138 | + } |
| 139 | +} |
0 commit comments