Skip to content

Commit 87ff20e

Browse files
Clean code QueryBuilder classes. (#557)
1 parent 2cbf418 commit 87ff20e

8 files changed

Lines changed: 371 additions & 349 deletions

src/QueryBuilder/AbstractDDLQueryBuilder.php

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace Yiisoft\Db\QueryBuilder;
66

7-
use Yiisoft\Db\Exception\Exception;
8-
use Yiisoft\Db\Exception\InvalidArgumentException;
9-
use Yiisoft\Db\Exception\InvalidConfigException;
107
use Yiisoft\Db\Exception\NotSupportedException;
118
use Yiisoft\Db\Query\QueryInterface;
129
use Yiisoft\Db\Schema\ColumnSchemaBuilderInterface;
@@ -63,18 +60,11 @@ public function addCommentOnTable(string $table, string $comment): string
6360
. (string) $this->quoter->quoteValue($comment);
6461
}
6562

66-
/**
67-
* @throws NotSupportedException
68-
*/
6963
public function addDefaultValue(string $name, string $table, string $column, mixed $value): string
7064
{
7165
throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.');
7266
}
7367

74-
/**
75-
* @throws Exception
76-
* @throws InvalidArgumentException
77-
*/
7868
public function addForeignKey(
7969
string $name,
8070
string $table,
@@ -108,7 +98,7 @@ public function addPrimaryKey(string $name, string $table, array|string $columns
10898
$columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY);
10999
}
110100

111-
/** @var string[] $columns */
101+
/** @psalm-var string[] $columns */
112102
foreach ($columns as $i => $col) {
113103
$columns[$i] = $this->quoter->quoteColumnName($col);
114104
}
@@ -125,7 +115,7 @@ public function addUnique(string $name, string $table, array|string $columns): s
125115
$columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY);
126116
}
127117

128-
/** @var string[] $columns */
118+
/** @psalm-var string[] $columns */
129119
foreach ($columns as $i => $col) {
130120
$columns[$i] = $this->quoter->quoteColumnName($col);
131121
}
@@ -150,18 +140,11 @@ public function alterColumn(
150140
. $this->queryBuilder->getColumnType($type);
151141
}
152142

153-
/**
154-
* @throws NotSupportedException
155-
*/
156143
public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string
157144
{
158145
throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.');
159146
}
160147

161-
/**
162-
* @throws Exception
163-
* @throws InvalidArgumentException
164-
*/
165148
public function createIndex(
166149
string $name,
167150
string $table,
@@ -197,12 +180,6 @@ public function createTable(string $table, array $columns, string $options = nul
197180
return $options === null ? $sql : $sql . ' ' . $options;
198181
}
199182

200-
/**
201-
* @throws Exception
202-
* @throws InvalidArgumentException
203-
* @throws InvalidConfigException
204-
* @throws NotSupportedException
205-
*/
206183
public function createView(string $viewName, QueryInterface|string $subQuery): string
207184
{
208185
if ($subQuery instanceof QueryInterface) {
@@ -252,9 +229,6 @@ public function dropCommentFromTable(string $table): string
252229
. ' IS NULL';
253230
}
254231

255-
/**
256-
* @throws NotSupportedException
257-
*/
258232
public function dropDefaultValue(string $name, string $table): string
259233
{
260234
throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.');

src/QueryBuilder/AbstractDMLQueryBuilder.php

Lines changed: 64 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,19 @@
2121
use function array_combine;
2222
use function array_diff;
2323
use function array_filter;
24+
use function array_keys;
2425
use function array_map;
2526
use function array_merge;
2627
use function array_unique;
2728
use function array_values;
29+
use function count;
2830
use function implode;
2931
use function in_array;
3032
use function is_array;
3133
use function is_string;
3234
use function json_encode;
3335
use function preg_match;
36+
use function sort;
3437

3538
abstract class AbstractDMLQueryBuilder implements DMLQueryBuilderInterface
3639
{
@@ -41,10 +44,6 @@ public function __construct(
4144
) {
4245
}
4346

44-
/**
45-
* @psalm-param string[] $columns
46-
* @psalm-suppress MixedArrayOffset
47-
*/
4847
public function batchInsert(string $table, array $columns, iterable|Generator $rows, array &$params = []): string
4948
{
5049
if (empty($rows)) {
@@ -65,7 +64,7 @@ public function batchInsert(string $table, array $columns, iterable|Generator $r
6564
$placeholders = [];
6665
foreach ($row as $index => $value) {
6766
if (isset($columns[$index], $mappedNames[$columns[$index]], $columnSchemas[$mappedNames[$columns[$index]]])) {
68-
/** @var mixed $value */
67+
/** @psalm-var mixed $value */
6968
$value = $this->getTypecastValue($value, $columnSchemas[$mappedNames[$columns[$index]]]);
7069
}
7170

@@ -91,9 +90,6 @@ public function batchInsert(string $table, array $columns, iterable|Generator $r
9190
. ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', $values);
9291
}
9392

94-
/**
95-
* @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException
96-
*/
9793
public function delete(string $table, array|string $condition, array &$params): string
9894
{
9995
$sql = 'DELETE FROM ' . $this->quoter->quoteTableName($table);
@@ -117,38 +113,27 @@ public function insert(string $table, QueryInterface|array $columns, array &$par
117113
. (!empty($placeholders) ? ' VALUES (' . implode(', ', $placeholders) . ')' : $values);
118114
}
119115

120-
/**
121-
* @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException
122-
*/
123116
public function insertWithReturningPks(string $table, QueryInterface|array $columns, array &$params = []): string
124117
{
125118
throw new NotSupportedException(__METHOD__ . '() is not supported by this DBMS.');
126119
}
127120

128-
/**
129-
* @throws NotSupportedException
130-
*/
131121
public function resetSequence(string $tableName, int|string|null $value = null): string
132122
{
133123
throw new NotSupportedException(__METHOD__ . '() is not supported by this DBMS.');
134124
}
135125

136-
/**
137-
* @psalm-suppress MixedArgument
138-
*/
139126
public function update(string $table, array $columns, array|string $condition, array &$params = []): string
140127
{
141128
/** @psalm-var string[] $lines */
142129
[$lines, $params] = $this->prepareUpdateSets($table, $columns, $params);
143130
$sql = 'UPDATE ' . $this->quoter->quoteTableName($table) . ' SET ' . implode(', ', $lines);
131+
/** @psalm-var array $params */
144132
$where = $this->queryBuilder->buildWhere($condition, $params);
145133

146134
return $where === '' ? $sql : $sql . ' ' . $where;
147135
}
148136

149-
/**
150-
* @throws NotSupportedException
151-
*/
152137
public function upsert(
153138
string $table,
154139
QueryInterface|array $insertColumns,
@@ -162,12 +147,15 @@ public function upsert(
162147
* Prepare select-subQuery and field names for INSERT INTO ... SELECT SQL statement.
163148
*
164149
* @param QueryInterface $columns Object, which represents select query.
165-
* @param array $params the parameters to be bound to the generated SQL statement. These parameters will be included
150+
* @param array $params The parameters to be bound to the generated SQL statement. These parameters will be included
166151
* in the result with the additional parameters generated during the query building process.
167152
*
168-
* @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException
153+
* @throws Exception
154+
* @throws InvalidArgumentException
155+
* @throws InvalidConfigException
156+
* @throws NotSupportedException
169157
*
170-
* @return array array of column names, values and params.
158+
* @return array Array of column names, values and params.
171159
*/
172160
protected function prepareInsertSelectSubQuery(QueryInterface $columns, array $params = []): array
173161
{
@@ -201,6 +189,14 @@ protected function prepareInsertSelectSubQuery(QueryInterface $columns, array $p
201189
return [$names, $values, $params];
202190
}
203191

192+
/**
193+
* Prepare column names and placeholders for INSERT SQL statement.
194+
*
195+
* @throws Exception
196+
* @throws InvalidConfigException
197+
* @throws InvalidArgumentException
198+
* @throws NotSupportedException
199+
*/
204200
protected function prepareInsertValues(string $table, array|QueryInterface $columns, array $params = []): array
205201
{
206202
$tableSchema = $this->schema->getTableSchema($table);
@@ -214,7 +210,7 @@ protected function prepareInsertValues(string $table, array|QueryInterface $colu
214210
} else {
215211
$columns = $this->normalizeColumnNames($table, $columns);
216212
/**
217-
* @var mixed $value
213+
* @psalm-var mixed $value
218214
* @psalm-var array<string, mixed> $columns
219215
*/
220216
foreach ($columns as $name => $value) {
@@ -233,14 +229,19 @@ protected function prepareInsertValues(string $table, array|QueryInterface $colu
233229
return [$names, $placeholders, $values, $params];
234230
}
235231

232+
/**
233+
* Prepare column names and placeholders for UPDATE SQL statement.
234+
*
235+
* @throws Exception
236+
* @throws InvalidConfigException
237+
* @throws InvalidArgumentException
238+
* @throws NotSupportedException
239+
*/
236240
protected function prepareUpdateSets(string $table, array $columns, array $params = []): array
237241
{
238242
$tableSchema = $this->schema->getTableSchema($table);
239-
240243
$columnSchemas = $tableSchema !== null ? $tableSchema->getColumns() : [];
241-
242244
$sets = [];
243-
244245
$columns = $this->normalizeColumnNames($table, $columns);
245246

246247
/**
@@ -263,9 +264,15 @@ protected function prepareUpdateSets(string $table, array $columns, array $param
263264
}
264265

265266
/**
266-
* @psalm-param Constraint[] $constraints
267+
* Prepare column names and placeholders for UPSERT SQL statement.
267268
*
268-
* @throws Exception|InvalidArgumentException|InvalidConfigException|JsonException|NotSupportedException
269+
* @throws Exception
270+
* @throws InvalidArgumentException
271+
* @throws InvalidConfigException
272+
* @throws JsonException
273+
* @throws NotSupportedException
274+
*
275+
* @psalm-param Constraint[] $constraints
269276
*/
270277
protected function prepareUpsertColumns(
271278
string $table,
@@ -313,19 +320,19 @@ protected function prepareUpsertColumns(
313320
*
314321
* The column list will be unique by column names.
315322
*
316-
* @param string $name table name. The table name may contain schema name if any. Do not quote the table name.
317-
* @param string[] $columns source column list.
318-
* @param Constraint[] $constraints this parameter optionally receives a matched constraint list. The constraints
323+
* @param string $name The table name, may contain schema name if any. Do not quote the table name.
324+
* @param string[] $columns Source column list.
325+
* @param array $constraints This parameter optionally receives a matched constraint list. The constraints
319326
* will be unique by their column names.
320327
*
321328
* @throws JsonException
322329
*
323-
* @return array column list.
324-
* @psalm-suppress ReferenceConstraintViolation
330+
* @return array The column list.
331+
*
332+
* @psalm-param Constraint[] $constraints
325333
*/
326334
private function getTableUniqueColumnNames(string $name, array $columns, array &$constraints = []): array
327335
{
328-
$constraints = [];
329336
$primaryKey = $this->schema->getTablePrimaryKey($name);
330337

331338
if ($primaryKey !== null) {
@@ -343,7 +350,11 @@ private function getTableUniqueColumnNames(string $name, array $columns, array &
343350

344351
$constraints = array_merge($constraints, $this->schema->getTableUniques($name));
345352

346-
/** Remove duplicates */
353+
/**
354+
* Remove duplicates
355+
*
356+
* @psalm-var Constraint[] $constraints
357+
*/
347358
$constraints = array_combine(
348359
array_map(
349360
static function (Constraint $constraint) {
@@ -386,10 +397,13 @@ static function (Constraint $constraint) use ($quoter, $columns, &$columnNames)
386397
)
387398
);
388399

389-
/** @psalm-var array $columnNames */
400+
/** @psalm-var Constraint[] $columnNames */
390401
return array_unique($columnNames);
391402
}
392403

404+
/**
405+
* @return mixed The typecast value of the given column.
406+
*/
393407
protected function getTypecastValue(mixed $value, ColumnSchemaInterface $columnSchema = null): mixed
394408
{
395409
if ($columnSchema) {
@@ -400,31 +414,30 @@ protected function getTypecastValue(mixed $value, ColumnSchemaInterface $columnS
400414
}
401415

402416
/**
403-
* Normalizes column names
417+
* Normalizes the column names for the given table.
404418
*
405-
* @param string $table the table that data will be saved into.
406-
* @param array $columns the column data (name => value) to be saved into the table or instance of
407-
* {@see QueryInterface} to perform INSERT INTO ... SELECT SQL statement. Passing of
408-
* {@see QueryInterface}.
419+
* @param string $table The table that data will be saved into.
420+
* @param array $columns The column data (name => value) to be saved into the table or instance of
421+
* {@see QueryInterface} to perform INSERT INTO ... SELECT SQL statement. Passing of {@see QueryInterface}.
409422
*
410-
* @return array normalized columns.
423+
* @return array The normalized column names (name => value).
411424
*/
412425
protected function normalizeColumnNames(string $table, array $columns): array
413426
{
414427
/** @var string[] $columnsList */
415428
$columnsList = array_keys($columns);
416429
$mappedNames = $this->getNormalizeColumnNames($table, $columnsList);
417430

418-
/** @psalm-var mixed[] $normalizedColumns */
431+
/** @psalm-var array $normalizedColumns */
419432
$normalizedColumns = [];
420433

421434
/**
422-
* @var string $name
423-
* @var mixed $value
435+
* @psalm-var string $name
436+
* @psalm-var mixed $value
424437
*/
425438
foreach ($columns as $name => $value) {
426439
$mappedName = $mappedNames[$name] ?? $name;
427-
/** @psalm-suppress MixedAssignment */
440+
/** @psalm-var mixed */
428441
$normalizedColumns[$mappedName] = $value;
429442
}
430443

@@ -434,10 +447,11 @@ protected function normalizeColumnNames(string $table, array $columns): array
434447
/**
435448
* Get map of normalized columns
436449
*
437-
* @param string $table
438-
* @param string[] $columns
450+
* @param string $table The table that data will be saved into.
451+
* @param string[] $columns The column data (name => value) to be saved into the table or instance of
452+
* {@see QueryInterface} to perform INSERT INTO ... SELECT SQL statement. Passing of {@see QueryInterface}.
439453
*
440-
* @return string[]
454+
* @return string[] Map of normalized columns.
441455
*/
442456
protected function getNormalizeColumnNames(string $table, array $columns): array
443457
{

0 commit comments

Comments
 (0)