Skip to content

Commit 78a44c1

Browse files
committed
(refactor): Simplify Builder property names and merge selectRaw/fromNone into select/from
1 parent 3f57d89 commit 78a44c1

8 files changed

Lines changed: 121 additions & 143 deletions

File tree

src/Query/Builder.php

Lines changed: 56 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ abstract class Builder implements
6363
{
6464
protected string $table = '';
6565

66-
protected string $tableAlias = '';
66+
protected string $alias = '';
6767

6868
/**
6969
* @var array<Query>
@@ -90,7 +90,7 @@ abstract class Builder implements
9090
protected array $joinFilterHooks = [];
9191

9292
/** @var list<array<string, mixed>> */
93-
protected array $pendingRows = [];
93+
protected array $rows = [];
9494

9595
/** @var array<string, string> */
9696
protected array $rawSets = [];
@@ -120,10 +120,10 @@ abstract class Builder implements
120120
protected array $windowDefinitions = [];
121121

122122
/** @var ?array{percent: float, method: string} */
123-
protected ?array $tableSample = null;
123+
protected ?array $sample = null;
124124

125125
/** @var list<CaseExpression> */
126-
protected array $caseSelects = [];
126+
protected array $cases = [];
127127

128128
/** @var array<string, CaseExpression> */
129129
protected array $caseSets = [];
@@ -156,7 +156,7 @@ abstract class Builder implements
156156

157157
protected ?SubSelect $fromSubquery = null;
158158

159-
protected bool $noTable = false;
159+
protected bool $tableless = false;
160160

161161
/** @var list<Condition> */
162162
protected array $rawOrders = [];
@@ -168,7 +168,7 @@ abstract class Builder implements
168168
protected array $rawHavings = [];
169169

170170
/** @var array<int, JoinBuilder> */
171-
protected array $joinBuilders = [];
171+
protected array $joins = [];
172172

173173
/** @var list<ExistsSubquery> */
174174
protected array $existsSubqueries = [];
@@ -185,7 +185,7 @@ abstract class Builder implements
185185
/** @var (\Closure(Plan): (array<mixed>|int))|null */
186186
protected ?\Closure $executor = null;
187187

188-
protected bool $qualifyColumns = false;
188+
protected bool $qualify = false;
189189

190190
/** @var array<string, true> */
191191
protected array $aggregationAliases = [];
@@ -210,7 +210,7 @@ abstract protected function compileRegex(string $attribute, array $values): stri
210210

211211
protected function buildTableClause(): string
212212
{
213-
if ($this->noTable) {
213+
if ($this->tableless) {
214214
return '';
215215
}
216216

@@ -226,12 +226,12 @@ protected function buildTableClause(): string
226226

227227
$sql = 'FROM ' . $this->quote($this->table);
228228

229-
if ($this->tableAlias !== '') {
230-
$sql .= ' AS ' . $this->quote($this->tableAlias);
229+
if ($this->alias !== '') {
230+
$sql .= ' AS ' . $this->quote($this->alias);
231231
}
232232

233-
if ($this->tableSample !== null) {
234-
$sql .= ' TABLESAMPLE ' . $this->tableSample['method'] . '(' . $this->tableSample['percent'] . ')';
233+
if ($this->sample !== null) {
234+
$sql .= ' TABLESAMPLE ' . $this->sample['method'] . '(' . $this->sample['percent'] . ')';
235235
}
236236

237237
return $sql;
@@ -247,25 +247,12 @@ protected function buildAfterJoins(array &$parts, GroupedQueries $grouped): void
247247
// no-op by default
248248
}
249249

250-
public function from(string $table, string $alias = ''): static
250+
public function from(string $table = '', string $alias = ''): static
251251
{
252252
$this->table = $table;
253-
$this->tableAlias = $alias;
254-
$this->fromSubquery = null;
255-
$this->noTable = false;
256-
257-
return $this;
258-
}
259-
260-
/**
261-
* Build a query without a FROM clause (e.g. SELECT 1, SELECT CONNECTION_ID()).
262-
*/
263-
public function fromNone(): static
264-
{
265-
$this->noTable = true;
266-
$this->table = '';
267-
$this->tableAlias = '';
253+
$this->alias = $alias;
268254
$this->fromSubquery = null;
255+
$this->tableless = ($table === '');
269256

270257
return $this;
271258
}
@@ -293,7 +280,7 @@ public function insertAs(string $alias): static
293280
*/
294281
public function set(array $row): static
295282
{
296-
$this->pendingRows[] = $row;
283+
$this->rows[] = $row;
297284

298285
return $this;
299286
}
@@ -445,7 +432,7 @@ public function joinWhere(string $table, Closure $callback, JoinType $type = Joi
445432
}
446433

447434
$index = \count($this->pendingQueries) - 1;
448-
$this->joinBuilders[$index] = $joinBuilder;
435+
$this->joins[$index] = $joinBuilder;
449436

450437
return $this;
451438
}
@@ -473,11 +460,16 @@ public function explain(bool $analyze = false): Plan
473460
}
474461

475462
/**
476-
* @param array<string> $columns
463+
* @param string|array<string> $columns
464+
* @param list<mixed> $bindings
477465
*/
478-
public function select(array $columns): static
466+
public function select(string|array $columns, array $bindings = []): static
479467
{
480-
$this->pendingQueries[] = Query::select($columns);
468+
if (\is_string($columns)) {
469+
$this->rawSelects[] = new Condition($columns, $bindings);
470+
} else {
471+
$this->pendingQueries[] = Query::select($columns);
472+
}
481473

482474
return $this;
483475
}
@@ -801,16 +793,6 @@ public function withRecursiveSeedStep(string $name, self $seed, self $step, arra
801793
return $this;
802794
}
803795

804-
/**
805-
* @param list<mixed> $bindings
806-
*/
807-
public function selectRaw(string $expression, array $bindings = []): static
808-
{
809-
$this->rawSelects[] = new Condition($expression, $bindings);
810-
811-
return $this;
812-
}
813-
814796
public function selectCast(string $column, string $type, string $alias = ''): static
815797
{
816798
$expr = 'CAST(' . $this->resolveAndWrap($column) . ' AS ' . $type . ')';
@@ -838,7 +820,7 @@ public function window(string $name, ?array $partitionBy = null, ?array $orderBy
838820

839821
public function selectCase(CaseExpression $case): static
840822
{
841-
$this->caseSelects[] = $case;
823+
$this->cases[] = $case;
842824

843825
return $this;
844826
}
@@ -959,10 +941,10 @@ public function build(): Plan
959941

960942
$grouped = Query::groupByType($this->pendingQueries);
961943

962-
$this->qualifyColumns = false;
944+
$this->qualify = false;
963945
$this->aggregationAliases = [];
964-
if (! empty($grouped->joins) && $this->tableAlias !== '') {
965-
$this->qualifyColumns = true;
946+
if (! empty($grouped->joins) && $this->alias !== '') {
947+
$this->qualify = true;
966948
foreach ($grouped->aggregations as $agg) {
967949
/** @var string $aggAlias */
968950
$aggAlias = $agg->getValue('');
@@ -1041,7 +1023,7 @@ public function build(): Plan
10411023
}
10421024

10431025
// CASE selects
1044-
foreach ($this->caseSelects as $caseSelect) {
1026+
foreach ($this->cases as $caseSelect) {
10451027
$selectParts[] = $caseSelect->sql;
10461028
foreach ($caseSelect->bindings as $binding) {
10471029
$this->addBinding($binding);
@@ -1072,7 +1054,7 @@ public function build(): Plan
10721054

10731055
foreach ($grouped->joins as $joinIdx => $joinQuery) {
10741056
$pendingIdx = $joinQueryIndices[$joinIdx] ?? -1;
1075-
$joinBuilder = $this->joinBuilders[$pendingIdx] ?? null;
1057+
$joinBuilder = $this->joins[$pendingIdx] ?? null;
10761058

10771059
if ($joinBuilder !== null) {
10781060
$joinSQL = $this->compileJoinWithBuilder($joinQuery, $joinBuilder);
@@ -1147,7 +1129,7 @@ public function build(): Plan
11471129
}
11481130

11491131
foreach ($this->filterHooks as $hook) {
1150-
$condition = $hook->filter($this->tableAlias ?: $this->table);
1132+
$condition = $hook->filter($this->alias ?: $this->table);
11511133
$whereClauses[] = $condition->expression;
11521134
foreach ($condition->bindings as $binding) {
11531135
$this->addBinding($binding);
@@ -1411,7 +1393,7 @@ protected function compileInsertBody(): array
14111393

14121394
$bindings = [];
14131395
$rowPlaceholders = [];
1414-
foreach ($this->pendingRows as $row) {
1396+
foreach ($this->rows as $row) {
14151397
$placeholders = [];
14161398
foreach ($columns as $col) {
14171399
$bindings[] = $row[$col] ?? null;
@@ -1467,8 +1449,8 @@ protected function compileAssignments(): array
14671449
{
14681450
$assignments = [];
14691451

1470-
if (! empty($this->pendingRows)) {
1471-
foreach ($this->pendingRows[0] as $col => $value) {
1452+
if (! empty($this->rows)) {
1453+
foreach ($this->rows[0] as $col => $value) {
14721454
$assignments[] = $this->resolveAndWrap($col) . ' = ?';
14731455
$this->addBinding($value);
14741456
}
@@ -1544,7 +1526,7 @@ protected function compileWhereClauses(array &$parts, ?GroupedQueries $grouped =
15441526
}
15451527

15461528
foreach ($this->filterHooks as $hook) {
1547-
$condition = $hook->filter($this->tableAlias ?: $this->table);
1529+
$condition = $hook->filter($this->alias ?: $this->table);
15481530
$whereClauses[] = $condition->expression;
15491531
foreach ($condition->bindings as $binding) {
15501532
$this->addBinding($binding);
@@ -1623,7 +1605,7 @@ protected function compileVectorOrderExpr(): ?Condition
16231605

16241606
protected function validateTable(): void
16251607
{
1626-
if ($this->noTable) {
1608+
if ($this->tableless) {
16271609
return;
16281610
}
16291611
if ($this->table === '' && $this->fromSubquery === null) {
@@ -1633,11 +1615,11 @@ protected function validateTable(): void
16331615

16341616
protected function validateRows(string $operation): void
16351617
{
1636-
if (empty($this->pendingRows)) {
1618+
if (empty($this->rows)) {
16371619
throw new ValidationException("No rows to {$operation}. Call set() before {$operation}().");
16381620
}
16391621

1640-
foreach ($this->pendingRows as $row) {
1622+
foreach ($this->rows as $row) {
16411623
if (empty($row)) {
16421624
throw new ValidationException('Cannot ' . $operation . ' an empty row. Each set() call must include at least one column.');
16431625
}
@@ -1651,19 +1633,19 @@ protected function validateRows(string $operation): void
16511633
*/
16521634
protected function validateAndGetColumns(): array
16531635
{
1654-
$columns = \array_keys($this->pendingRows[0]);
1636+
$columns = \array_keys($this->rows[0]);
16551637

16561638
foreach ($columns as $col) {
16571639
if ($col === '') {
16581640
throw new ValidationException('Column names must be non-empty strings.');
16591641
}
16601642
}
16611643

1662-
if (\count($this->pendingRows) > 1) {
1644+
if (\count($this->rows) > 1) {
16631645
$expectedKeys = $columns;
16641646
\sort($expectedKeys);
16651647

1666-
foreach ($this->pendingRows as $i => $row) {
1648+
foreach ($this->rows as $i => $row) {
16671649
$rowKeys = \array_keys($row);
16681650
\sort($rowKeys);
16691651

@@ -1689,9 +1671,9 @@ public function reset(): static
16891671
$this->pendingQueries = [];
16901672
$this->bindings = [];
16911673
$this->table = '';
1692-
$this->tableAlias = '';
1674+
$this->alias = '';
16931675
$this->unions = [];
1694-
$this->pendingRows = [];
1676+
$this->rows = [];
16951677
$this->rawSets = [];
16961678
$this->rawSetBindings = [];
16971679
$this->conflictKeys = [];
@@ -1709,17 +1691,17 @@ public function reset(): static
17091691
$this->rawSelects = [];
17101692
$this->windowSelects = [];
17111693
$this->windowDefinitions = [];
1712-
$this->tableSample = null;
1713-
$this->caseSelects = [];
1694+
$this->sample = null;
1695+
$this->cases = [];
17141696
$this->caseSets = [];
17151697
$this->whereInSubqueries = [];
17161698
$this->subSelects = [];
17171699
$this->fromSubquery = null;
1718-
$this->noTable = false;
1700+
$this->tableless = false;
17191701
$this->rawOrders = [];
17201702
$this->rawGroups = [];
17211703
$this->rawHavings = [];
1722-
$this->joinBuilders = [];
1704+
$this->joins = [];
17231705
$this->existsSubqueries = [];
17241706
$this->lateralJoins = [];
17251707
$this->beforeBuildCallbacks = [];
@@ -1746,7 +1728,7 @@ public function __clone(): void
17461728
$this->subSelects = \array_map(fn (SubSelect $s) => new SubSelect(clone $s->subquery, $s->alias), $this->subSelects);
17471729
$this->whereInSubqueries = \array_map(fn (WhereInSubquery $s) => new WhereInSubquery($s->column, clone $s->subquery, $s->not), $this->whereInSubqueries);
17481730
$this->existsSubqueries = \array_map(fn (ExistsSubquery $s) => new ExistsSubquery(clone $s->subquery, $s->not), $this->existsSubqueries);
1749-
$this->joinBuilders = \array_map(fn (JoinBuilder $j) => clone $j, $this->joinBuilders);
1731+
$this->joins = \array_map(fn (JoinBuilder $j) => clone $j, $this->joins);
17501732
$this->pendingQueries = \array_map(fn (Query $q) => clone $q, $this->pendingQueries);
17511733
$this->lateralJoins = \array_map(fn (LateralJoin $l) => new LateralJoin(clone $l->subquery, $l->alias, $l->type), $this->lateralJoins);
17521734
}
@@ -2043,12 +2025,12 @@ protected function resolveAndWrap(string $attribute): string
20432025
{
20442026
$resolved = $this->resolveAttribute($attribute);
20452027

2046-
if ($this->qualifyColumns
2028+
if ($this->qualify
20472029
&& $resolved !== '*'
20482030
&& ! \str_contains($resolved, '.')
20492031
&& ! isset($this->aggregationAliases[$resolved])
20502032
) {
2051-
$resolved = $this->tableAlias . '.' . $resolved;
2033+
$resolved = $this->alias . '.' . $resolved;
20522034
}
20532035

20542036
return $this->quote($resolved);
@@ -2449,15 +2431,15 @@ private function aggregateQueryToAstExpression(Query $query): Expression
24492431

24502432
private function buildAstFrom(): Table|SubquerySource|null
24512433
{
2452-
if ($this->noTable) {
2434+
if ($this->tableless) {
24532435
return null;
24542436
}
24552437

24562438
if ($this->table === '') {
24572439
return null;
24582440
}
24592441

2460-
$alias = $this->tableAlias !== '' ? $this->tableAlias : null;
2442+
$alias = $this->alias !== '' ? $this->alias : null;
24612443
return new Table($this->table, $alias);
24622444
}
24632445

@@ -2836,7 +2818,7 @@ private function applyAstColumns(Select $ast): void
28362818
}
28372819

28382820
$serializer = $this->createAstSerializer();
2839-
$this->selectRaw($serializer->serializeExpression($col));
2821+
$this->select($serializer->serializeExpression($col));
28402822
$hasNonStar = true;
28412823
}
28422824

@@ -2885,7 +2867,7 @@ private function applyAstAggregateColumn(Aliased $aliased): void
28852867
}
28862868

28872869
$serializer = $this->createAstSerializer();
2888-
$this->selectRaw($serializer->serializeExpression($aliased));
2870+
$this->select($serializer->serializeExpression($aliased));
28892871
}
28902872

28912873
private function applyAstUnaliasedFunctionColumn(Func $fn): void
@@ -2913,7 +2895,7 @@ private function applyAstUnaliasedFunctionColumn(Func $fn): void
29132895
}
29142896

29152897
$serializer = $this->createAstSerializer();
2916-
$this->selectRaw($serializer->serializeExpression($fn));
2898+
$this->select($serializer->serializeExpression($fn));
29172899
}
29182900

29192901
private function astFuncArgToAttribute(Func $fn): string

0 commit comments

Comments
 (0)