| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?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\Dialect;
- use Cake\Database\Expression\FunctionExpression;
- use Cake\Database\Expression\UnaryExpression;
- use Cake\Database\Query;
- use Cake\Database\SqlDialectTrait;
- /**
- * Contains functions that encapsulates the SQL dialect used by Postgres,
- * including query translators and schema introspection.
- */
- trait PostgresDialectTrait {
- use SqlDialectTrait;
- /**
- * String used to start a database identifier quoting to make it safe
- *
- * @var string
- */
- protected $_startQuote = '"';
- /**
- * String used to end a database identifier quoting to make it safe
- *
- * @var string
- */
- protected $_endQuote = '"';
- /**
- * Distinct clause needs no transformation
- *
- * @param Query $query The query to be transformed
- * @return Query
- */
- protected function _transformDistinct($query) {
- return $query;
- }
- /**
- * Modifies the original insert query to append a "RETURNING *" epilogue
- * so that the latest insert id can be retrieved
- *
- * @param Cake\Database\Query $query
- * @return Cake\Database\Query
- */
- protected function _insertQueryTranslator($query) {
- if (!$query->clause('epilog')) {
- $query->epilog('RETURNING *');
- }
- return $query;
- }
- /**
- * Returns an dictionary of expressions to be transformed when compiling a Query
- * to SQL. Array keys are method names to be called in this class
- *
- * @return array
- */
- protected function _expressionTranslators() {
- $namespace = 'Cake\Database\Expression';
- return [
- $namespace . '\FunctionExpression' => '_transformFunctionExpression'
- ];
- }
- /**
- * Receives a FunctionExpression and changes it so that it conforms to this
- * SQL dialect.
- *
- * @param Cake\Database\Expression\FunctionExpression
- * @return void
- */
- protected function _transformFunctionExpression(FunctionExpression $expression) {
- switch ($expression->name()) {
- case 'CONCAT':
- // CONCAT function is expressed as exp1 || exp2
- $expression->name('')->type(' ||');
- break;
- case 'DATEDIFF':
- $expression
- ->name('')
- ->type('-')
- ->iterateParts(function($p) {
- return new FunctionExpression('DATE', [$p['value']], [$p['type']]);
- });
- break;
- case 'CURRENT_DATE':
- $time = new FunctionExpression('LOCALTIMESTAMP', [' 0 ' => 'literal']);
- $expression->name('CAST')->type(' AS ')->add([$time, 'date' => 'literal']);
- break;
- case 'CURRENT_TIME':
- $time = new FunctionExpression('LOCALTIMESTAMP', [' 0 ' => 'literal']);
- $expression->name('CAST')->type(' AS ')->add([$time, 'time' => 'literal']);
- break;
- case 'NOW':
- $expression->name('LOCALTIMESTAMP')->add([' 0 ' => 'literal']);
- break;
- }
- }
- /**
- * Get the schema dialect.
- *
- * Used by Cake\Schema package to reflect schema and
- * generate schema.
- *
- * @return Cake\Database\Schema\PostgresSchema
- */
- public function schemaDialect() {
- return new \Cake\Database\Schema\PostgresSchema($this);
- }
- }
|