Skip to content

Commit

Permalink
Introduce getQueryBuilder aliases for specific database actions
Browse files Browse the repository at this point in the history
  • Loading branch information
wallacio committed Nov 13, 2023
1 parent 0f43ba2 commit 55601f6
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Phinx/Db/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
39 changes: 39 additions & 0 deletions src/Phinx/Db/Adapter/AdapterWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
39 changes: 39 additions & 0 deletions src/Phinx/Db/Adapter/PdoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
38 changes: 38 additions & 0 deletions src/Phinx/Migration/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down
50 changes: 50 additions & 0 deletions src/Phinx/Migration/MigrationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Check failure on line 183 in src/Phinx/Migration/MigrationInterface.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Method Phinx\Migration\MigrationInterface::getSelectBuilder() has invalid return type Cake\Database\SelectQuery.

Check failure on line 183 in src/Phinx/Migration/MigrationInterface.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

PHPDoc tag @return with type Cake\Database\SelectQuery is not subtype of native type Cake\Database\Query\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;

Check failure on line 194 in src/Phinx/Migration/MigrationInterface.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Method Phinx\Migration\MigrationInterface::getInsertBuilder() has invalid return type Cake\Database\InsertQuery.

Check failure on line 194 in src/Phinx/Migration/MigrationInterface.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

PHPDoc tag @return with type Cake\Database\InsertQuery is not subtype of native type Cake\Database\Query\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;

Check failure on line 205 in src/Phinx/Migration/MigrationInterface.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Method Phinx\Migration\MigrationInterface::getUpdateBuilder() has invalid return type Cake\Database\UpdateQuery.

Check failure on line 205 in src/Phinx/Migration/MigrationInterface.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

PHPDoc tag @return with type Cake\Database\UpdateQuery is not subtype of native type Cake\Database\Query\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;

Check failure on line 216 in src/Phinx/Migration/MigrationInterface.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Method Phinx\Migration\MigrationInterface::getDeleteBuilder() has invalid return type Cake\Database\DeleteQuery.

Check failure on line 216 in src/Phinx/Migration/MigrationInterface.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

PHPDoc tag @return with type Cake\Database\DeleteQuery is not subtype of native type Cake\Database\Query\DeleteQuery.

/**
* Executes a query and returns only one row as an array.
*
Expand Down

0 comments on commit 55601f6

Please sign in to comment.