Skip to content

Commit

Permalink
Represent query type as enum in QueryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Jul 30, 2022
1 parent b8451b3 commit db5b3c7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
8 changes: 6 additions & 2 deletions UPGRADE.md
Expand Up @@ -299,8 +299,12 @@ The following `QueryBuilder` methods have been removed:

The following `QueryBuilder` constants have been removed:

1. `STATE_DIRTY`,
2. `STATE_CLEAN`.
1. `SELECT`,
2. `DELETE`,
3. `UPDATE`,
4. `INSERT`,
5. `STATE_DIRTY`,
6. `STATE_CLEAN`.

## BC BREAK: Removed the `Constraint` interface.

Expand Down
40 changes: 10 additions & 30 deletions src/Query/QueryBuilder.php
Expand Up @@ -37,26 +37,6 @@
*/
class QueryBuilder
{
/**
* @deprecated
*/
final public const SELECT = 0;

/**
* @deprecated
*/
final public const DELETE = 1;

/**
* @deprecated
*/
final public const UPDATE = 2;

/**
* @deprecated
*/
final public const INSERT = 3;

/**
* The complete SQL string for this query.
*/
Expand All @@ -79,7 +59,7 @@ class QueryBuilder
/**
* The type of query this is. Can be select, update or delete.
*/
private int $type = self::SELECT;
private QueryType $type = QueryType::SELECT;

/**
* The index of the first result to retrieve.
Expand Down Expand Up @@ -348,10 +328,10 @@ public function getSQL(): string
}

$sql = match ($this->type) {
self::INSERT => $this->getSQLForInsert(),
self::DELETE => $this->getSQLForDelete(),
self::UPDATE => $this->getSQLForUpdate(),
default => $this->getSQLForSelect(),
QueryType::INSERT => $this->getSQLForInsert(),
QueryType::DELETE => $this->getSQLForDelete(),
QueryType::UPDATE => $this->getSQLForUpdate(),
QueryType::SELECT => $this->getSQLForSelect(),
};

$this->sql = $sql;
Expand Down Expand Up @@ -539,7 +519,7 @@ public function getMaxResults(): ?int
*/
public function select(string ...$expressions): self
{
$this->type = self::SELECT;
$this->type = QueryType::SELECT;

if (count($expressions) < 1) {
return $this;
Expand Down Expand Up @@ -591,7 +571,7 @@ public function distinct(): self
*/
public function addSelect(string $expression, string ...$expressions): self
{
$this->type = self::SELECT;
$this->type = QueryType::SELECT;

$this->select = array_merge($this->select, [$expression], $expressions);

Expand All @@ -617,7 +597,7 @@ public function addSelect(string $expression, string ...$expressions): self
*/
public function delete(string $table): self
{
$this->type = self::DELETE;
$this->type = QueryType::DELETE;

$this->table = $table;

Expand All @@ -643,7 +623,7 @@ public function delete(string $table): self
*/
public function update(string $table): self
{
$this->type = self::UPDATE;
$this->type = QueryType::UPDATE;

$this->table = $table;

Expand Down Expand Up @@ -673,7 +653,7 @@ public function update(string $table): self
*/
public function insert(string $table): self
{
$this->type = self::INSERT;
$this->type = QueryType::INSERT;

$this->table = $table;

Expand Down
16 changes: 16 additions & 0 deletions src/Query/QueryType.php
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Query;

/**
* @internal
*/
enum QueryType
{
case SELECT;
case DELETE;
case UPDATE;
case INSERT;
}

0 comments on commit db5b3c7

Please sign in to comment.