Skip to content

Commit 813c344

Browse files
committed
Add type parameter to getQueryBuilder()
This aligns the interface we have in the 4.x branch, and also gives a solution to #651 for applications that want to not have any deprecations.
1 parent d1147b2 commit 813c344

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/CakeAdapter.php

+22-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use Phinx\Db\Adapter\AdapterInterface;
2121
use Phinx\Db\Adapter\AdapterWrapper;
2222

23+
use function Cake\Core\deprecationWarning;
24+
2325
/**
2426
* Decorates an AdapterInterface in order to proxy some method to the actual
2527
* connection object.
@@ -76,11 +78,29 @@ public function getCakeConnection()
7678
/**
7779
* Returns a new Query object
7880
*
81+
* @param string|null $type The query type to get. Defaults to null emitting a deprecation
7982
* @return \Cake\Database\Query
8083
*/
81-
public function getQueryBuilder(): Query
84+
public function getQueryBuilder(?string $type = null): Query
8285
{
83-
return $this->getCakeConnection()->newQuery();
86+
switch ($type) {
87+
case 'delete':
88+
return $this->getCakeConnection()->deleteQuery();
89+
case 'insert':
90+
return $this->getCakeConnection()->insertQuery();
91+
case 'select':
92+
return $this->getCakeConnection()->selectQuery();
93+
case 'update':
94+
return $this->getCakeConnection()->updateQuery();
95+
case null:
96+
default:
97+
deprecationWarning(
98+
'Using getQueryBuilder() with no parameters is deprecated. ' .
99+
"Please provide a query type parameter e.g `getQueryBuilder('select')`"
100+
);
101+
102+
return $this->getCakeConnection()->newQuery();
103+
}
84104
}
85105

86106
/**

tests/TestCase/MigrationsTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
namespace Migrations\Test\TestCase;
1515

1616
use Cake\Core\Plugin;
17+
use Cake\Database\Query\InsertQuery;
18+
use Cake\Database\Query\SelectQuery;
19+
use Cake\Database\Query\UpdateQuery;
1720
use Cake\Datasource\ConnectionManager;
1821
use Cake\TestSuite\TestCase;
1922
use Migrations\CakeAdapter;
@@ -1001,4 +1004,21 @@ public function migrationsProvider()
10011004
],
10021005
];
10031006
}
1007+
1008+
public function testQueryBuilder(): void
1009+
{
1010+
$adapter = $this->migrations
1011+
->getManager()
1012+
->getEnvironment('default')
1013+
->getAdapter();
1014+
1015+
$this->assertInstanceOf(CakeAdapter::class, $adapter);
1016+
$this->assertInstanceOf(DeleteQuery::class, $adapter->getQueryBuilder('delete'));
1017+
$this->assertInstanceOf(InsertQuery::class, $adapter->getQueryBuilder('insert'));
1018+
$this->assertInstanceOf(SelectQuery::class, $adapter->getQueryBuilder('select'));
1019+
$this->assertInstanceOf(UpdateQuery::class, $adapter->getQueryBuilder('update'));
1020+
$this->deprecated(function () use ($adapter) {
1021+
$this->assertInstanceOf(SelectQuery::class, $adapter->getQueryBuilder());
1022+
});
1023+
}
10041024
}

0 commit comments

Comments
 (0)