From 55601f67d4a1f6b9f4965d9949b125f23eae9a78 Mon Sep 17 00:00:00 2001 From: wallacio Date: Mon, 13 Nov 2023 15:36:59 +0000 Subject: [PATCH] Introduce getQueryBuilder aliases for specific database actions --- src/Phinx/Db/Adapter/AdapterInterface.php | 34 +++++++++++++++ src/Phinx/Db/Adapter/AdapterWrapper.php | 39 +++++++++++++++++ src/Phinx/Db/Adapter/PdoAdapter.php | 39 +++++++++++++++++ src/Phinx/Migration/AbstractMigration.php | 38 ++++++++++++++++ src/Phinx/Migration/MigrationInterface.php | 50 ++++++++++++++++++++++ 5 files changed, 200 insertions(+) diff --git a/src/Phinx/Db/Adapter/AdapterInterface.php b/src/Phinx/Db/Adapter/AdapterInterface.php index a42da2a02..54db1a826 100644 --- a/src/Phinx/Db/Adapter/AdapterInterface.php +++ b/src/Phinx/Db/Adapter/AdapterInterface.php @@ -9,6 +9,12 @@ namespace Phinx\Db\Adapter; use Cake\Database\Query; +use Cake\Database\Query\{ + DeleteQuery, + InsertQuery, + SelectQuery, + UpdateQuery +}; use Phinx\Db\Table\Column; use Phinx\Db\Table\Table; use Phinx\Migration\MigrationInterface; @@ -287,6 +293,34 @@ public function executeActions(Table $table, array $actions): void; */ public function getQueryBuilder(string $type): Query; + /** + * Return a new SelectQuery object + * + * @return \Cake\Database\Query\SelectQuery + */ + public function getSelectBuilder(): SelectQuery; + + /** + * Return a new InsertQuery object + * + * @return \Cake\Database\Query\InsertQuery + */ + public function getInsertBuilder(): InsertQuery; + + /** + * Return a new UpdateQuery object + * + * @return \Cake\Database\Query\UpdateQuery + */ + public function getUpdateBuilder(): UpdateQuery; + + /** + * Return a new DeleteQuery object + * + * @return \Cake\Database\Query\DeleteQuery + */ + public function getDeleteBuilder(): DeleteQuery; + /** * Executes a SQL statement. * diff --git a/src/Phinx/Db/Adapter/AdapterWrapper.php b/src/Phinx/Db/Adapter/AdapterWrapper.php index 3d4909335..2bfaddd40 100644 --- a/src/Phinx/Db/Adapter/AdapterWrapper.php +++ b/src/Phinx/Db/Adapter/AdapterWrapper.php @@ -9,6 +9,12 @@ namespace Phinx\Db\Adapter; use Cake\Database\Query; +use Cake\Database\Query\{ + SelectQuery, + InsertQuery, + UpdateQuery, + DeleteQuery +}; use PDO; use Phinx\Db\Table\Column; use Phinx\Db\Table\Table; @@ -485,4 +491,37 @@ public function getQueryBuilder(string $type): Query { return $this->getAdapter()->getQueryBuilder($type); } + + + /** + * @inheritDoc + */ + public function getSelectBuilder(): SelectQuery + { + return $this->getAdapter()->getSelectBuilder(); + } + + /** + * @inheritDoc + */ + public function getInsertBuilder(): InsertQuery + { + return $this->getAdapter()->getInsertBuilder(); + } + + /** + * @inheritDoc + */ + public function getUpdateBuilder(): UpdateQuery + { + return $this->getAdapter()->getUpdateBuilder(); + } + + /** + * @inheritDoc + */ + public function getDeleteBuilder(): DeleteQuery + { + return $this->getAdapter()->getDeleteBuilder(); + } } diff --git a/src/Phinx/Db/Adapter/PdoAdapter.php b/src/Phinx/Db/Adapter/PdoAdapter.php index 6b79c2acf..874a48aa0 100644 --- a/src/Phinx/Db/Adapter/PdoAdapter.php +++ b/src/Phinx/Db/Adapter/PdoAdapter.php @@ -10,7 +10,14 @@ use BadMethodCallException; use Cake\Database\Connection; +use Cake\Database\Exception\DatabaseException; use Cake\Database\Query; +use Cake\Database\Query\{ + SelectQuery, + InsertQuery, + UpdateQuery, + DeleteQuery +}; use InvalidArgumentException; use PDO; use PDOException; @@ -244,6 +251,38 @@ public function getQueryBuilder(string $type): Query }; } + /** + * @inheritDoc + */ + public function getSelectBuilder(): SelectQuery + { + return $this->getDecoratedConnection()->selectQuery(); + } + + /** + * @inheritDoc + */ + public function getInsertBuilder(): InsertQuery + { + return $this->getDecoratedConnection()->insertQuery(); + } + + /** + * @inheritDoc + */ + public function getUpdateBuilder(): UpdateQuery + { + return $this->getDecoratedConnection()->updateQuery(); + } + + /** + * @inheritDoc + */ + public function getDeleteBuilder(): DeleteQuery + { + return $this->getDecoratedConnection()->deleteQuery(); + } + /** * Executes a query and returns PDOStatement. * diff --git a/src/Phinx/Migration/AbstractMigration.php b/src/Phinx/Migration/AbstractMigration.php index b567ba8ae..8d84336ac 100644 --- a/src/Phinx/Migration/AbstractMigration.php +++ b/src/Phinx/Migration/AbstractMigration.php @@ -9,6 +9,12 @@ namespace Phinx\Migration; use Cake\Database\Query; +use Cake\Database\Query\{ + SelectQuery, + InsertQuery, + UpdateQuery, + DeleteQuery +}; use Phinx\Db\Adapter\AdapterInterface; use Phinx\Db\Table; use RuntimeException; @@ -214,6 +220,38 @@ public function getQueryBuilder(string $type): Query return $this->getAdapter()->getQueryBuilder($type); } + /** + * @inheritDoc + */ + public function getSelectBuilder(): SelectQuery + { + return $this->getAdapter()->getSelectBuilder(); + } + + /** + * @inheritDoc + */ + public function getInsertBuilder(): InsertQuery + { + return $this->getAdapter()->getInsertBuilder(); + } + + /** + * @inheritDoc + */ + public function getUpdateBuilder(): UpdateQuery + { + return $this->getAdapter()->getUpdateBuilder(); + } + + /** + * @inheritDoc + */ + public function getDeleteBuilder(): DeleteQuery + { + return $this->getAdapter()->getDeleteBuilder(); + } + /** * @inheritDoc */ diff --git a/src/Phinx/Migration/MigrationInterface.php b/src/Phinx/Migration/MigrationInterface.php index 1a7a9c637..0eb00cbee 100644 --- a/src/Phinx/Migration/MigrationInterface.php +++ b/src/Phinx/Migration/MigrationInterface.php @@ -9,6 +9,12 @@ namespace Phinx\Migration; use Cake\Database\Query; +use Cake\Database\Query\{ + SelectQuery, + InsertQuery, + UpdateQuery, + DeleteQuery +}; use Phinx\Db\Adapter\AdapterInterface; use Phinx\Db\Table; use Symfony\Component\Console\Input\InputInterface; @@ -165,6 +171,50 @@ public function query(string $sql, array $params = []): mixed; */ public function getQueryBuilder(string $type): Query; + /** + * Returns a new SelectQuery object that can be used to build complex + * SELECT queries and execute them against the current database. + * + * Queries executed through the query builder are always sent to the database, regardless of the + * the dry-run settings. + * + * @return \Cake\Database\SelectQuery + */ + public function getSelectBuilder(): SelectQuery; + + /** + * Returns a new InsertQuery object that can be used to build complex + * INSERT queries and execute them against the current database. + * + * Queries executed through the query builder are always sent to the database, regardless of the + * the dry-run settings. + * + * @return \Cake\Database\InsertQuery + */ + public function getInsertBuilder(): InsertQuery; + + /** + * Returns a new UpdateQuery object that can be used to build complex + * UPDATE queries and execute them against the current database. + * + * Queries executed through the query builder are always sent to the database, regardless of the + * the dry-run settings. + * + * @return \Cake\Database\UpdateQuery + */ + public function getUpdateBuilder(): UpdateQuery; + + /** + * Returns a new DeleteQuery object that can be used to build complex + * DELETE queries and execute them against the current database. + * + * Queries executed through the query builder are always sent to the database, regardless of the + * the dry-run settings. + * + * @return \Cake\Database\DeleteQuery + */ + public function getDeleteBuilder(): DeleteQuery; + /** * Executes a query and returns only one row as an array. *