Total Complexity | 55 |
Total Lines | 368 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like DMLQueryBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DMLQueryBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | abstract class DMLQueryBuilder implements DMLQueryBuilderInterface |
||
36 | { |
||
37 | public function __construct( |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @psalm-suppress MixedArrayOffset |
||
46 | */ |
||
47 | public function batchInsert(string $table, array $columns, iterable|Generator $rows, array &$params = []): string |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
||
95 | */ |
||
96 | public function delete(string $table, array|string $condition, array &$params): string |
||
102 | } |
||
103 | |||
104 | public function insert(string $table, QueryInterface|array $columns, array &$params = []): string |
||
105 | { |
||
106 | /** |
||
107 | * @psalm-var string[] $names |
||
108 | * @psalm-var string[] $placeholders |
||
109 | * @psalm-var string $values |
||
110 | */ |
||
111 | [$names, $placeholders, $values, $params] = $this->prepareInsertValues($table, $columns, $params); |
||
112 | |||
113 | return 'INSERT INTO ' |
||
114 | . $this->quoter->quoteTableName($table) |
||
115 | . (!empty($names) ? ' (' . implode(', ', $names) . ')' : '') |
||
116 | . (!empty($placeholders) ? ' VALUES (' . implode(', ', $placeholders) . ')' : $values); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
||
121 | */ |
||
122 | public function insertEx(string $table, QueryInterface|array $columns, array &$params = []): string |
||
123 | { |
||
124 | throw new NotSupportedException(__METHOD__ . '() is not supported by this DBMS.'); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @throws NotSupportedException |
||
129 | */ |
||
130 | public function resetSequence(string $tableName, int|string|null $value = null): string |
||
131 | { |
||
132 | throw new NotSupportedException(__METHOD__ . '() is not supported by this DBMS.'); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @psalm-suppress MixedArgument |
||
137 | */ |
||
138 | public function update(string $table, array $columns, array|string $condition, array &$params = []): string |
||
139 | { |
||
140 | /** @psalm-var string[] $lines */ |
||
141 | [$lines, $params] = $this->prepareUpdateSets($table, $columns, $params); |
||
142 | $sql = 'UPDATE ' . $this->quoter->quoteTableName($table) . ' SET ' . implode(', ', $lines); |
||
143 | $where = $this->queryBuilder->buildWhere($condition, $params); |
||
144 | |||
145 | return $where === '' ? $sql : $sql . ' ' . $where; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @throws NotSupportedException |
||
150 | */ |
||
151 | public function upsert( |
||
152 | string $table, |
||
153 | QueryInterface|array $insertColumns, |
||
154 | bool|array $updateColumns, |
||
155 | array &$params |
||
156 | ): string { |
||
157 | throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.'); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Prepare select-subquery and field names for INSERT INTO ... SELECT SQL statement. |
||
162 | * |
||
163 | * @param QueryInterface $columns Object, which represents select query. |
||
164 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will be included |
||
165 | * in the result with the additional parameters generated during the query building process. |
||
166 | * |
||
167 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
||
168 | * |
||
169 | * @return array array of column names, values and params. |
||
170 | */ |
||
171 | protected function prepareInsertSelectSubQuery(QueryInterface $columns, array $params = []): array |
||
195 | } |
||
196 | |||
197 | protected function prepareInsertValues(string $table, array|QueryInterface $columns, array $params = []): array |
||
198 | { |
||
199 | $tableSchema = $this->schema->getTableSchema($table); |
||
200 | $columnSchemas = $tableSchema !== null ? $tableSchema->getColumns() : []; |
||
226 | } |
||
227 | |||
228 | protected function prepareUpdateSets(string $table, array $columns, array $params = []): array |
||
229 | { |
||
230 | $tableSchema = $this->schema->getTableSchema($table); |
||
231 | |||
232 | $columnSchemas = $tableSchema !== null ? $tableSchema->getColumns() : []; |
||
233 | |||
234 | $sets = []; |
||
235 | |||
236 | /** |
||
237 | * @psalm-var array<string, mixed> $columns |
||
238 | * @psalm-var mixed $value |
||
239 | */ |
||
240 | foreach ($columns as $name => $value) { |
||
241 | /** @psalm-var mixed $value */ |
||
242 | $value = isset($columnSchemas[$name]) ? $columnSchemas[$name]->dbTypecast($value) : $value; |
||
243 | if ($value instanceof ExpressionInterface) { |
||
244 | $placeholder = $this->queryBuilder->buildExpression($value, $params); |
||
245 | } else { |
||
246 | $placeholder = $this->queryBuilder->bindParam($value, $params); |
||
247 | } |
||
248 | |||
249 | $sets[] = $this->quoter->quoteColumnName($name) . '=' . $placeholder; |
||
250 | } |
||
251 | |||
252 | return [$sets, $params]; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * @psalm-param Constraint[] $constraints |
||
257 | * |
||
258 | * @throws Exception|InvalidArgumentException|InvalidConfigException|JsonException|NotSupportedException |
||
259 | */ |
||
260 | protected function prepareUpsertColumns( |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Returns all column names belonging to constraints enforcing uniqueness (`PRIMARY KEY`, `UNIQUE INDEX`, etc.) |
||
294 | * for the named table removing constraints which did not cover the specified column list. |
||
295 | * |
||
296 | * The column list will be unique by column names. |
||
297 | * |
||
298 | * @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
||
299 | * @param string[] $columns source column list. |
||
300 | * @param Constraint[] $constraints this parameter optionally receives a matched constraint list. The constraints |
||
301 | * will be unique by their column names. |
||
302 | * |
||
303 | * @throws JsonException |
||
304 | * |
||
305 | * @return array column list. |
||
306 | * @psalm-suppress ReferenceConstraintViolation |
||
307 | */ |
||
308 | private function getTableUniqueColumnNames(string $name, array $columns, array &$constraints = []): array |
||
394 | } |
||
395 | |||
396 | protected function getTypecastValue(mixed $value, ColumnSchemaInterface $columnSchema = null): mixed |
||
405 |