Skip to content

Commit

Permalink
Rework tracking state in QueryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Jul 31, 2022
1 parent b8b5aa2 commit 1a15fb8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 43 deletions.
7 changes: 6 additions & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,19 @@ DBAL is now tested only with SQL Server 2017 and newer.

The `Statement::execute()` method has been marked private.

## BC BREAK: Removed `QueryBuilder` methods.
## BC BREAK: Removed `QueryBuilder` methods and contstants.

The following `QueryBuilder` methods have been removed:

1. `execute()`,
2. `getState()`,
3. `getType()`.

The following `QueryBuilder` constants have been removed:

1. `STATE_DIRTY`,
2. `STATE_CLEAN`.

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

The `Constraint` interface has been removed. The `ForeignKeyConstraint`, `Index` and `UniqueConstraint` classes
Expand Down
70 changes: 28 additions & 42 deletions src/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,6 @@ class QueryBuilder
*/
final public const INSERT = 3;

/**
* @deprecated
*/
final public const STATE_DIRTY = 0;

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

/**
* The complete SQL string for this query.
*/
Expand All @@ -91,11 +81,6 @@ class QueryBuilder
*/
private int $type = self::SELECT;

/**
* The state of the query object. Can be dirty or clean.
*/
private int $state = self::STATE_CLEAN;

/**
* The index of the first result to retrieve.
*/
Expand Down Expand Up @@ -358,7 +343,7 @@ public function executeStatement(): int|string
*/
public function getSQL(): string
{
if ($this->sql !== null && $this->state === self::STATE_CLEAN) {
if ($this->sql !== null) {
return $this->sql;
}

Expand All @@ -369,8 +354,7 @@ public function getSQL(): string
default => $this->getSQLForSelect(),
};

$this->state = self::STATE_CLEAN;
$this->sql = $sql;
$this->sql = $sql;

return $sql;
}
Expand Down Expand Up @@ -494,9 +478,10 @@ public function getParameterType(int|string $key): int|string|Type|null
*/
public function setFirstResult(int $firstResult): self
{
$this->state = self::STATE_DIRTY;
$this->firstResult = $firstResult;

$this->sql = null;

return $this;
}

Expand All @@ -519,9 +504,10 @@ public function getFirstResult(): int
*/
public function setMaxResults(?int $maxResults): self
{
$this->state = self::STATE_DIRTY;
$this->maxResults = $maxResults;

$this->sql = null;

return $this;
}

Expand Down Expand Up @@ -561,7 +547,7 @@ public function select(string ...$expressions): self

$this->select = $expressions;

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand All @@ -582,7 +568,7 @@ public function distinct(): self
{
$this->distinct = true;

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand All @@ -609,7 +595,7 @@ public function addSelect(string $expression, string ...$expressions): self

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

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand All @@ -635,7 +621,7 @@ public function delete(string $table): self

$this->table = $table;

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand All @@ -661,7 +647,7 @@ public function update(string $table): self

$this->table = $table;

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand Down Expand Up @@ -691,7 +677,7 @@ public function insert(string $table): self

$this->table = $table;

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand All @@ -715,7 +701,7 @@ public function from(string $table, ?string $alias = null): self
{
$this->from[] = new From($table, $alias);

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand Down Expand Up @@ -763,7 +749,7 @@ public function innerJoin(string $fromAlias, string $join, string $alias, ?strin
{
$this->join[$fromAlias][] = Join::inner($join, $alias, $condition);

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand All @@ -789,7 +775,7 @@ public function leftJoin(string $fromAlias, string $join, string $alias, ?string
{
$this->join[$fromAlias][] = Join::left($join, $alias, $condition);

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand All @@ -815,7 +801,7 @@ public function rightJoin(string $fromAlias, string $join, string $alias, ?strin
{
$this->join[$fromAlias][] = Join::right($join, $alias, $condition);

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand All @@ -839,7 +825,7 @@ public function set(string $key, string $value): self
{
$this->set[] = $key . ' = ' . $value;

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand Down Expand Up @@ -875,7 +861,7 @@ public function where(string|CompositeExpression $predicate, string|CompositeExp
{
$this->where = $this->createPredicate($predicate, ...$predicates);

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand Down Expand Up @@ -908,7 +894,7 @@ public function andWhere(string|CompositeExpression $predicate, string|Composite
...$predicates
);

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand Down Expand Up @@ -936,7 +922,7 @@ public function orWhere(string|CompositeExpression $predicate, string|CompositeE
{
$this->where = $this->appendToPredicate($this->where, CompositeExpression::TYPE_OR, $predicate, ...$predicates);

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand All @@ -961,7 +947,7 @@ public function groupBy(string $expression, string ...$expressions): self
{
$this->groupBy = array_merge([$expression], $expressions);

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand All @@ -986,7 +972,7 @@ public function addGroupBy(string $expression, string ...$expressions): self
{
$this->groupBy = array_merge($this->groupBy, [$expression], $expressions);

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand Down Expand Up @@ -1040,7 +1026,7 @@ public function values(array $values): self
{
$this->values = $values;

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand All @@ -1058,7 +1044,7 @@ public function having(string|CompositeExpression $predicate, string|CompositeEx
{
$this->having = $this->createPredicate($predicate, ...$predicates);

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand All @@ -1081,7 +1067,7 @@ public function andHaving(string|CompositeExpression $predicate, string|Composit
...$predicates
);

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand All @@ -1104,7 +1090,7 @@ public function orHaving(string|CompositeExpression $predicate, string|Composite
...$predicates
);

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand Down Expand Up @@ -1163,7 +1149,7 @@ public function orderBy(string $sort, ?string $order = null): self

$this->orderBy = [$orderBy];

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand All @@ -1186,7 +1172,7 @@ public function addOrderBy(string $sort, ?string $order = null): self

$this->orderBy[] = $orderBy;

$this->state = self::STATE_DIRTY;
$this->sql = null;

return $this;
}
Expand Down

0 comments on commit 1a15fb8

Please sign in to comment.