Total Complexity | 54 |
Total Lines | 348 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like RowTrait 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 RowTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | trait RowTrait |
||
13 | { |
||
14 | /** |
||
15 | * Returns a recordset of all columns in a table. |
||
16 | * |
||
17 | * @param string $table The name of a table |
||
18 | * @param array $key The associative array holding the key to retrieve |
||
19 | * |
||
20 | * @return \PHPPgAdmin\ADORecordSet A recordset |
||
21 | */ |
||
22 | public function browseRow($table, $key) |
||
23 | { |
||
24 | $f_schema = $this->_schema; |
||
25 | $this->fieldClean($f_schema); |
||
26 | $this->fieldClean($table); |
||
27 | |||
28 | $sql = "SELECT * FROM \"{$f_schema}\".\"{$table}\""; |
||
29 | if (is_array($key) && sizeof($key) > 0) { |
||
30 | $sql .= ' WHERE true'; |
||
31 | foreach ($key as $k => $v) { |
||
32 | $this->fieldClean($k); |
||
33 | $this->clean($v); |
||
34 | $sql .= " AND \"{$k}\"='{$v}'"; |
||
35 | } |
||
36 | } |
||
37 | |||
38 | return $this->selectSet($sql); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Get the fields for uniquely identifying a row in a table. |
||
43 | * |
||
44 | * @param string $table The table for which to retrieve the identifier |
||
45 | * |
||
46 | * @return array|array<integer,string>|int An array mapping attribute number to attribute name, empty for no identifiers |
||
47 | */ |
||
48 | public function getRowIdentifier($table) |
||
49 | { |
||
50 | $oldtable = $table; |
||
51 | $c_schema = $this->_schema; |
||
52 | $this->clean($c_schema); |
||
53 | $this->clean($table); |
||
54 | |||
55 | $status = $this->beginTransaction(); |
||
56 | if ($status != 0) { |
||
57 | return -1; |
||
58 | } |
||
59 | |||
60 | // Get the first primary or unique index (sorting primary keys first) that |
||
61 | // is NOT a partial index. |
||
62 | $sql = " |
||
63 | SELECT indrelid, indkey |
||
64 | FROM pg_catalog.pg_index |
||
65 | WHERE indisunique AND indrelid=( |
||
66 | SELECT oid FROM pg_catalog.pg_class |
||
67 | WHERE relname='{$table}' AND relnamespace=( |
||
68 | SELECT oid FROM pg_catalog.pg_namespace |
||
69 | WHERE nspname='{$c_schema}' |
||
70 | ) |
||
71 | ) AND indpred IS NULL AND indexprs IS NULL |
||
72 | ORDER BY indisprimary DESC LIMIT 1"; |
||
73 | $rs = $this->selectSet($sql); |
||
74 | |||
75 | // If none, check for an OID column. Even though OIDs can be duplicated, the edit and delete row |
||
76 | // functions check that they're only modiying a single row. Otherwise, return empty array. |
||
77 | if ($rs->RecordCount() == 0) { |
||
78 | // Check for OID column |
||
79 | $temp = []; |
||
80 | if ($this->hasObjectID($table)) { |
||
|
|||
81 | $temp = ['oid']; |
||
82 | } |
||
83 | $this->endTransaction(); |
||
84 | |||
85 | return $temp; |
||
86 | } // Otherwise find the names of the keys |
||
87 | |||
88 | $attnames = $this->getAttributeNames($oldtable, explode(' ', $rs->fields['indkey'])); |
||
89 | if (!is_array($attnames)) { |
||
90 | $this->rollbackTransaction(); |
||
91 | |||
92 | return -1; |
||
93 | } |
||
94 | |||
95 | $this->endTransaction(); |
||
96 | |||
97 | return $attnames; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Adds a new row to a table. |
||
102 | * |
||
103 | * @param string $table The table in which to insert |
||
104 | * @param array $fields Array of given field in values |
||
105 | * @param array $values Array of new values for the row |
||
106 | * @param array $nulls An array mapping column => something if it is to be null |
||
107 | * @param array $format An array of the data type (VALUE or EXPRESSION) |
||
108 | * @param array $types An array of field types |
||
109 | * |
||
110 | * @return int 0 if operation was successful |
||
111 | */ |
||
112 | public function insertRow($table, $fields, $values, $nulls, $format, $types) |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Formats a value or expression for sql purposes. |
||
150 | * |
||
151 | * @param string $type The type of the field |
||
152 | * @param mixed $format VALUE or EXPRESSION |
||
153 | * @param mixed $value The actual value entered in the field. Can be NULL |
||
154 | * |
||
155 | * @return mixed The suitably quoted and escaped value |
||
156 | */ |
||
157 | public function formatValue($type, $format, $value) |
||
158 | { |
||
159 | switch ($type) { |
||
160 | case 'bool': |
||
161 | case 'boolean': |
||
162 | if ($value == 't') { |
||
163 | return 'TRUE'; |
||
164 | } |
||
165 | |||
166 | if ($value == 'f') { |
||
167 | return 'FALSE'; |
||
168 | } |
||
169 | if ($value == '') { |
||
170 | return 'NULL'; |
||
171 | } |
||
172 | |||
173 | return $value; |
||
174 | break; |
||
175 | default: |
||
176 | // Checking variable fields is difficult as there might be a size |
||
177 | // attribute... |
||
178 | if (strpos($type, 'time') === 0) { |
||
179 | // Assume it's one of the time types... |
||
180 | if ($value == '') { |
||
181 | return "''"; |
||
182 | } |
||
183 | |||
184 | if (strcasecmp($value, 'CURRENT_TIMESTAMP') == 0 |
||
185 | || strcasecmp($value, 'CURRENT_TIME') == 0 |
||
186 | || strcasecmp($value, 'CURRENT_DATE') == 0 |
||
187 | || strcasecmp($value, 'LOCALTIME') == 0 |
||
188 | || strcasecmp($value, 'LOCALTIMESTAMP') == 0) { |
||
189 | return $value; |
||
190 | } |
||
191 | if ($format == 'EXPRESSION') { |
||
192 | return $value; |
||
193 | } |
||
194 | $this->clean($value); |
||
195 | |||
196 | return "'{$value}'"; |
||
197 | } |
||
198 | if ($format == 'VALUE') { |
||
199 | $this->clean($value); |
||
200 | |||
201 | return "'{$value}'"; |
||
202 | } |
||
203 | |||
204 | return $value; |
||
205 | } |
||
206 | } |
||
207 | |||
208 | // View functions |
||
209 | |||
210 | /** |
||
211 | * Updates a row in a table. |
||
212 | * |
||
213 | * @param string $table The table in which to update |
||
214 | * @param array $vars An array mapping new values for the row |
||
215 | * @param array $nulls An array mapping column => something if it is to be null |
||
216 | * @param array $format An array of the data type (VALUE or EXPRESSION) |
||
217 | * @param array $types An array of field types |
||
218 | * @param array $keyarr An array mapping column => value to update |
||
219 | * |
||
220 | * @return bool|int 0 success |
||
221 | */ |
||
222 | public function editRow($table, $vars, $nulls, $format, $types, $keyarr) |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Delete a row from a table. |
||
292 | * |
||
293 | * @param string $table The table from which to delete |
||
294 | * @param array $key An array mapping column => value to delete |
||
295 | * @param string $schema the schema of the table |
||
296 | * |
||
297 | * @return bool|int 0 success |
||
298 | */ |
||
299 | public function deleteRow($table, $key, $schema = '') |
||
327 | } |
||
328 | |||
329 | abstract public function fieldClean(&$str); |
||
330 | |||
331 | abstract public function beginTransaction(); |
||
332 | |||
333 | abstract public function rollbackTransaction(); |
||
334 | |||
335 | abstract public function endTransaction(); |
||
336 | |||
337 | abstract public function execute($sql); |
||
338 | |||
339 | abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null); |
||
340 | |||
341 | abstract public function selectSet($sql); |
||
342 | |||
343 | abstract public function clean(&$str); |
||
344 | |||
345 | abstract public function phpBool($parameter); |
||
346 | |||
347 | abstract public function hasCreateTableLikeWithConstraints(); |
||
348 | |||
349 | abstract public function hasCreateTableLikeWithIndexes(); |
||
350 | |||
351 | abstract public function hasTablespaces(); |
||
352 | |||
353 | abstract public function delete($table, $conditions, $schema = ''); |
||
354 | |||
355 | abstract public function fieldArrayClean(&$arr); |
||
356 | |||
357 | abstract public function hasCreateFieldWithConstraints(); |
||
358 | |||
359 | abstract public function getAttributeNames($table, $atts); |
||
360 | } |
||
361 |