Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type parameter to getQueryBuilder() #731

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/CakeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use Phinx\Db\Adapter\AdapterInterface;
use Phinx\Db\Adapter\AdapterWrapper;

use function Cake\Core\deprecationWarning;

Check failure on line 23 in src/CakeAdapter.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Expected 0 lines between different types of use statement, found 1.

/**
* Decorates an AdapterInterface in order to proxy some method to the actual
* connection object.
Expand Down Expand Up @@ -76,11 +78,29 @@
/**
* Returns a new Query object
*
* @param string|null $type The query type to get. Defaults to null emitting a deprecation
* @return \Cake\Database\Query
*/
public function getQueryBuilder(): Query
public function getQueryBuilder(?string $type = null): Query
{
return $this->getCakeConnection()->newQuery();
switch ($type) {
case 'delete':
return $this->getCakeConnection()->deleteQuery();
case 'insert':
return $this->getCakeConnection()->insertQuery();
case 'select':
return $this->getCakeConnection()->selectQuery();
case 'update':
return $this->getCakeConnection()->updateQuery();
case null:
default:
deprecationWarning(
'Using getQueryBuilder() with no parameters is deprecated. ' .
"Please provide a query type parameter e.g `getQueryBuilder('select')`"
);

return $this->getCakeConnection()->newQuery();

Check failure on line 102 in src/CakeAdapter.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

DeprecatedMethod

src/CakeAdapter.php:102:52: DeprecatedMethod: The method Cake\Database\Connection::newQuery has been marked as deprecated (see https://psalm.dev/001)
}
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/TestCase/MigrationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
namespace Migrations\Test\TestCase;

use Cake\Core\Plugin;
use Cake\Database\Query\InsertQuery;
use Cake\Database\Query\SelectQuery;
use Cake\Database\Query\UpdateQuery;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;
use Migrations\CakeAdapter;
Expand Down Expand Up @@ -1001,4 +1004,21 @@ public function migrationsProvider()
],
];
}

public function testQueryBuilder(): void
{
$adapter = $this->migrations
->getManager()
->getEnvironment('default')
->getAdapter();

$this->assertInstanceOf(CakeAdapter::class, $adapter);
$this->assertInstanceOf(DeleteQuery::class, $adapter->getQueryBuilder('delete'));
$this->assertInstanceOf(InsertQuery::class, $adapter->getQueryBuilder('insert'));
$this->assertInstanceOf(SelectQuery::class, $adapter->getQueryBuilder('select'));
$this->assertInstanceOf(UpdateQuery::class, $adapter->getQueryBuilder('update'));
$this->deprecated(function () use ($adapter) {
$this->assertInstanceOf(SelectQuery::class, $adapter->getQueryBuilder());
});
}
}
Loading