Skip to content

Commit

Permalink
cakephp 3.7 version
Browse files Browse the repository at this point in the history
  • Loading branch information
skie committed Feb 3, 2020
1 parent 31af591 commit 7a822b1
Show file tree
Hide file tree
Showing 18 changed files with 767 additions and 94 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"require-dev": {
"cakephp/cakephp-codesniffer": "dev-master",
"phpunit/phpunit": "^6.0",
"cakephp/cakephp": "3.6.*"
"cakephp/cakephp": "3.7.*"
},
"repositories": [
{
Expand Down
102 changes: 77 additions & 25 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@

use Cake\Database\Type;

Type::map('cursor', 'CakeDC\OracleDriver\Database\Type\CursorType');
Type::map('cursor', 'CakeDC\OracleDriver\Database\Type\CursorType');
Type::map('boolean', 'CakeDC\OracleDriver\Database\Type\BoolType');
39 changes: 38 additions & 1 deletion src/Database/Dialect/OracleDialectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Cake\Database\ExpressionInterface;
use CakeDC\OracleDriver\Database\Expression\SimpleExpression;
use CakeDC\OracleDriver\Database\OracleCompiler;
use CakeDC\OracleDriver\Database\Oracle12Compiler;
use CakeDC\OracleDriver\Database\Schema\OracleSchema;
use Cake\Database\Expression\FunctionExpression;
use Cake\Database\Query;
Expand Down Expand Up @@ -71,8 +72,15 @@ protected function _selectQueryTranslator($query)
$limit = $query->clause('limit');
$offset = $query->clause('offset');

$config = $query->getConnection()->config();
$serverVersion = $config['server_version'] ?? null;

if ($offset !== null || $limit !== null) {
return $this->_pagingSubquery($query, $limit, $offset);
if ($serverVersion !== null && $serverVersion >= 12) {
return $this->_pagingSubqueryNew($query, $limit, $offset);
} else {
return $this->_pagingSubquery($query, $limit, $offset);
}
}

return $this->_transformDistinct($query);
Expand Down Expand Up @@ -128,6 +136,32 @@ protected function _pagingSubquery($original, $limit, $offset)
return $outer2;
}

/**
* Generate a paging subquery for older versions of Oracle Server.
*
* Prior to Oracle 12 there was no equivalent to LIMIT OFFSET,
* so a subquery must be used.
*
* @param \Cake\Database\Query $original The query to wrap in a subquery.
* @param int $limit The number of rows to fetch.
* @param int $offset The number of rows to offset.
* @return \Cake\Database\Query Modified query object.
*/
protected function _pagingSubqueryNew($original, $limit, $offset)
{

// @todo add hints support for selects

// $components['columns'] = str_replace('select', "select /*+ FIRST_ROWS({$query->limit}) */",
// $components['columns']);

// $offset = $query->offset ?: 0;
// $limit = $query->limit;
// $components['limit'] = "offset $offset rows fetch next $limit rows only";

return $original;
}

/**
* Returns a dictionary of expressions to be transformed when compiling a Query
* to SQL. Array keys are method names to be called in this class
Expand All @@ -152,6 +186,9 @@ protected function _expressionTranslators()
protected function _transformFunctionExpression(FunctionExpression $expression)
{
switch ($expression->getName()) {
case 'RAND':
$expression->setName('DBMS_RANDOM.VALUE');
break;
case 'CONCAT':
$expression->setName('')->setConjunction(' ||');
break;
Expand Down
23 changes: 23 additions & 0 deletions src/Database/Driver/OracleBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@

use CakeDC\OracleDriver\Config\ConfigTrait;
use CakeDC\OracleDriver\Database\Dialect\OracleDialectTrait;
use CakeDC\OracleDriver\Database\Oracle12Compiler;
use CakeDC\OracleDriver\Database\OracleCompiler;
use CakeDC\OracleDriver\Database\Statement\OracleStatement;
use Cake\Database\Driver;
use Cake\Database\Driver\PDODriverTrait;
use Cake\Database\Query;
use Cake\Database\Statement\PDOStatement;
use Cake\Database\ValueBinder;
use Cake\Database\Type;
use Cake\Log\Log;
use Cake\Network\Exception\NotImplementedException;
Expand Down Expand Up @@ -164,6 +168,25 @@ public function prepare($query)
return $statement;
}

/**
* {@inheritDoc}
*/
public function compileQuery(Query $query, ValueBinder $generator)
{
$config = $query->getConnection()->config();
$serverVersion = $config['server_version'] ?? null;

if ($serverVersion !== null && $serverVersion >= 12) {
$processor = new Oracle12Compiler();
} else {
$processor = new OracleCompiler();
}

$translator = $this->queryTranslator($query->type());
$query = $translator($query);

return [$query, $processor->compile($query, $generator)];
}
/**
* Add "FROM DUAL" to SQL statements that are SELECT statements
* with no FROM clause specified
Expand Down
Loading

0 comments on commit 7a822b1

Please sign in to comment.