Skip to content

Commit

Permalink
Fixing more tests for SQLServer, for now modifying a query after being
Browse files Browse the repository at this point in the history
executed is discouraged as cuases prolmes with SQLServer
  • Loading branch information
lorenzo committed Apr 12, 2014
1 parent 24e3623 commit d3d1060
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 28 deletions.
8 changes: 3 additions & 5 deletions src/Database/Dialect/SqlserverDialectTrait.php
@@ -1,7 +1,5 @@
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand All @@ -11,7 +9,7 @@
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @since 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Database\Dialect;
Expand Down Expand Up @@ -51,11 +49,11 @@ protected function _selectQueryTranslator($query) {
$offset = $query->clause('offset');

if ($limit && $offset === null) {
$query->modifier([sprintf('TOP %d', $limit)]);
$query->modifier(['_auto_top_' => sprintf('TOP %d', $limit)]);
$query->limit(null);
}

if ($offset) {
if ($offset !== null) {
$offsetSql = sprintf('%d ROWS', $offset);
if ($limit) {
$offsetSql .= sprintf(' FETCH FIRST %d ROWS ONLY', $limit);
Expand Down
4 changes: 1 addition & 3 deletions src/Database/Driver/Sqlserver.php
@@ -1,7 +1,5 @@
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand All @@ -11,7 +9,7 @@
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @since 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Database\Driver;
Expand Down
2 changes: 2 additions & 0 deletions src/Database/Query.php
Expand Up @@ -449,6 +449,7 @@ public function distinct($on = [], $overwrite = false) {
* @return Query
*/
public function modifier($modifiers, $overwrite = false) {
$this->_dirty();
if ($overwrite) {
$this->_parts['modifier'] = [];
}
Expand Down Expand Up @@ -1152,6 +1153,7 @@ public function page($num) {
* @return Query
*/
public function limit($num) {
$this->_dirty();
if ($num !== null && !is_object($num)) {
$num = (int)$num;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Database/Schema/SqlserverSchema.php
Expand Up @@ -11,7 +11,7 @@
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @since 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Database\Schema;
Expand Down Expand Up @@ -315,7 +315,6 @@ public function columnSql(Table $table, $name) {
}

if ($data['type'] === 'integer' || $data['type'] === 'biginteger') {
$out .= $type;
if ([$name] === $table->primaryKey() || $data['autoIncrement'] === true) {
unset($data['null'], $data['default']);
$out .= ' IDENTITY(1, 1)';
Expand Down
53 changes: 35 additions & 18 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -1087,23 +1087,23 @@ public function testSelectOrderBy() {
$this->assertEquals(['id' => 3], $result->fetch('assoc'));

$expression = $query->newExpr()
->add(['(id + :offset) % 2 = 0']);
->add(['(id + :offset) % 2']);
$result = $query
->order([$expression, 'id' => 'desc'], true)
->bind(':offset', 1, null)
->execute();
$this->assertEquals(['id' => 2], $result->fetch('assoc'));
$this->assertEquals(['id' => 3], $result->fetch('assoc'));
$this->assertEquals(['id' => 1], $result->fetch('assoc'));
$this->assertEquals(['id' => 2], $result->fetch('assoc'));

$result = $query
->order($expression, true)
->order(['id' => 'asc'])
->bind(':offset', 1, null)
->execute();
$this->assertEquals(['id' => 2], $result->fetch('assoc'));
$this->assertEquals(['id' => 1], $result->fetch('assoc'));
$this->assertEquals(['id' => 3], $result->fetch('assoc'));
$this->assertEquals(['id' => 2], $result->fetch('assoc'));
}

/**
Expand Down Expand Up @@ -1137,8 +1137,7 @@ public function testSelectGroup() {
}

/**
* Tests that it is possible to select distinct rows, even filtering by one column
* this is testing that there is an specific implementation for DISTINCT ON
* Tests that it is possible to select distinct rows
*
* @return void
*/
Expand All @@ -1155,7 +1154,19 @@ public function testSelectDistinct() {

$result = $query->select(['id'])->distinct(false)->execute();
$this->assertCount(3, $result);
}

/**
* Tests that it is possible to select distinct rows, even filtering by one column
* this is testing that there is an specific implementation for DISTINCT ON
*
* @return void
*/
public function testSelectDistinctON() {
$this->skipIf(
$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver,
'Not implemented yet in SqlServer'
);
$result = $query->select(['id'])->distinct(['author_id'])->execute();
$this->assertCount(2, $result);
}
Expand Down Expand Up @@ -1344,14 +1355,9 @@ public function testSelectLimit() {
$result = $query->select('id')->from('articles')->limit(1)->execute();
$this->assertCount(1, $result);

$result = $query->limit(null)->execute();
$this->assertCount(3, $result);

$result = $query->limit(2)->execute();
$query = new Query($this->connection);
$result = $query->select('id')->from('articles')->limit(2)->execute();
$this->assertCount(2, $result);

$result = $query->limit(3)->execute();
$this->assertCount(3, $result);
}

/**
Expand All @@ -1367,11 +1373,19 @@ public function testSelectOffset() {
$this->assertCount(1, $result);
$this->assertEquals(['id' => 1], $result->fetch('assoc'));

$result = $query->offset(1)->execute();
$query = new Query($this->connection);
$result = $query->select('id')->from('comments')
->limit(1)
->offset(1)
->execute();
$this->assertCount(1, $result);
$this->assertEquals(['id' => 2], $result->fetch('assoc'));

$result = $query->offset(2)->execute();
$query = new Query($this->connection);
$result = $query->select('id')->from('comments')
->limit(1)
->offset(2)
->execute();
$this->assertCount(1, $result);
$this->assertEquals(['id' => 3], $result->fetch('assoc'));

Expand All @@ -1398,14 +1412,17 @@ public function testSelectPage() {
$query = new Query($this->connection);
$result = $query->select('id')->from('comments')
->limit(1)
->page(1)->execute();
->page(1)
->execute();

$this->assertEquals(0, $query->clause('offset'));
$this->assertCount(1, $result);
$this->assertEquals(['id' => 1], $result->fetch('assoc'));

$result = $query->page(2)->execute();
$this->assertEquals(1, $query->clause('offset'));
$query = new Query($this->connection);
$result = $query->select('id')->from('comments')
->limit(1)
->page(2)
->execute();
$this->assertCount(1, $result);
$this->assertEquals(['id' => 2], $result->fetch('assoc'));

Expand Down

0 comments on commit d3d1060

Please sign in to comment.