| Total Complexity | 49 |
| Total Lines | 322 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SqlController 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 SqlController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class SqlController extends BaseController |
||
| 13 | { |
||
| 14 | public $controller_name = 'SqlController'; |
||
| 15 | public $query = ''; |
||
| 16 | public $subject = ''; |
||
| 17 | public $start_time; |
||
| 18 | public $duration; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Default method to render the controller according to the action parameter. |
||
| 22 | */ |
||
| 23 | public function render() |
||
| 87 | } |
||
| 88 | |||
| 89 | public function doDefault() |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | private function execute_script() |
||
|
2 ignored issues
–
show
|
|||
| 114 | { |
||
| 115 | $conf = $this->conf; |
||
| 116 | $misc = $this->misc; |
||
| 117 | $lang = $this->lang; |
||
| 118 | $data = $this->misc->getDatabaseAccessor(); |
||
| 119 | $_connection = $this->misc->getConnection(); |
||
| 120 | |||
| 121 | /** |
||
| 122 | * This is a callback function to display the result of each separate query. |
||
| 123 | * |
||
| 124 | * @param ADORecordSet $rs The recordset returned by the script execetor |
||
| 125 | */ |
||
| 126 | $sqlCallback = function ($query, $rs, $lineno) use ($data, $misc, $lang, $_connection) { |
||
| 127 | // Check if $rs is false, if so then there was a fatal error |
||
| 128 | if (false === $rs) { |
||
| 129 | echo htmlspecialchars($_FILES['script']['name']), ':', $lineno, ': ', nl2br(htmlspecialchars($_connection->getLastError())), "<br/>\n"; |
||
| 130 | } else { |
||
| 131 | // Print query results |
||
| 132 | switch (pg_result_status($rs)) { |
||
| 133 | case PGSQL_TUPLES_OK: |
||
| 134 | // If rows returned, then display the results |
||
| 135 | $num_fields = pg_numfields($rs); |
||
| 136 | echo "<p><table>\n<tr>"; |
||
| 137 | for ($k = 0; $k < $num_fields; ++$k) { |
||
| 138 | echo '<th class="data">', $misc->printVal(pg_fieldname($rs, $k)), '</th>'; |
||
| 139 | } |
||
| 140 | |||
| 141 | $i = 0; |
||
| 142 | $row = pg_fetch_row($rs); |
||
| 143 | while (false !== $row) { |
||
| 144 | $id = (0 == ($i % 2) ? '1' : '2'); |
||
| 145 | echo "<tr class=\"data{$id}\">\n"; |
||
| 146 | foreach ($row as $k => $v) { |
||
| 147 | echo '<td style="white-space:nowrap;">', $misc->printVal($v, pg_fieldtype($rs, $k), ['null' => true]), '</td>'; |
||
| 148 | } |
||
| 149 | echo "</tr>\n"; |
||
| 150 | $row = pg_fetch_row($rs); |
||
| 151 | ++$i; |
||
| 152 | } |
||
| 153 | |||
| 154 | echo "</table><br/>\n"; |
||
| 155 | echo $i, " {$lang['strrows']}</p>\n"; |
||
| 156 | |||
| 157 | break; |
||
| 158 | case PGSQL_COMMAND_OK: |
||
| 159 | // If we have the command completion tag |
||
| 160 | if (version_compare(phpversion(), '4.3', '>=')) { |
||
| 161 | echo htmlspecialchars(pg_result_status($rs, PGSQL_STATUS_STRING)), "<br/>\n"; |
||
| 162 | } |
||
| 163 | // Otherwise if any rows have been affected |
||
| 164 | elseif ($data->conn->Affected_Rows() > 0) { |
||
| 165 | echo $data->conn->Affected_Rows(), " {$lang['strrowsaff']}<br/>\n"; |
||
| 166 | } |
||
| 167 | // Otherwise output nothing... |
||
| 168 | break; |
||
| 169 | case PGSQL_EMPTY_QUERY: |
||
| 170 | break; |
||
| 171 | default: |
||
| 172 | break; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | }; |
||
| 176 | |||
| 177 | return $data->executeScript('script', $sqlCallback); |
||
| 178 | } |
||
| 179 | |||
| 180 | private function execute_query() |
||
|
2 ignored issues
–
show
|
|||
| 181 | { |
||
| 182 | $lang = $this->lang; |
||
| 183 | $data = $this->misc->getDatabaseAccessor(); |
||
| 184 | |||
| 185 | // Set fetch mode to NUM so that duplicate field names are properly returned |
||
| 186 | $data->conn->setFetchMode(ADODB_FETCH_NUM); |
||
| 187 | |||
| 188 | $rs = $data->conn->Execute($this->query); |
||
| 189 | |||
| 190 | echo '<form method="post" id="sqlform" action="' . $_SERVER['REQUEST_URI'] . '">'; |
||
| 191 | echo '<textarea width="90%" name="query" id="query" rows="5" cols="100" resizable="true">'; |
||
| 192 | |||
| 193 | echo htmlspecialchars($this->query); |
||
| 194 | echo '</textarea><br>'; |
||
| 195 | echo $this->misc->setForm(); |
||
| 196 | echo '<input type="submit"/></form>'; |
||
| 197 | |||
| 198 | // $rs will only be an object if there is no error |
||
| 199 | if (is_object($rs)) { |
||
| 200 | // Request was run, saving it in history |
||
| 201 | if (!isset($_REQUEST['nohistory'])) { |
||
| 202 | $this->misc->saveScriptHistory($this->query); |
||
| 203 | } |
||
| 204 | |||
| 205 | // Now, depending on what happened do various things |
||
| 206 | |||
| 207 | // First, if rows returned, then display the results |
||
| 208 | if ($rs->recordCount() > 0) { |
||
| 209 | echo "<table>\n<tr>"; |
||
| 210 | foreach ($rs->fields as $k => $v) { |
||
| 211 | $finfo = $rs->fetchField($k); |
||
| 212 | echo '<th class="data">', $this->misc->printVal($finfo->name), '</th>'; |
||
| 213 | } |
||
| 214 | echo "</tr>\n"; |
||
| 215 | $i = 0; |
||
| 216 | while (!$rs->EOF) { |
||
| 217 | $id = (0 == ($i % 2) ? '1' : '2'); |
||
| 218 | echo "<tr class=\"data{$id}\">\n"; |
||
| 219 | foreach ($rs->fields as $k => $v) { |
||
| 220 | $finfo = $rs->fetchField($k); |
||
| 221 | echo '<td style="white-space:nowrap;">', $this->misc->printVal($v, $finfo->type, ['null' => true]), '</td>'; |
||
| 222 | } |
||
| 223 | echo "</tr>\n"; |
||
| 224 | $rs->moveNext(); |
||
| 225 | ++$i; |
||
| 226 | } |
||
| 227 | echo "</table>\n"; |
||
| 228 | echo '<p>', $rs->recordCount(), " {$lang['strrows']}</p>\n"; |
||
| 229 | } elseif ($data->conn->Affected_Rows() > 0) { |
||
| 230 | // Otherwise if any rows have been affected |
||
| 231 | echo '<p>', $data->conn->Affected_Rows(), " {$lang['strrowsaff']}</p>\n"; |
||
| 232 | } else { |
||
| 233 | // Otherwise nodata to print |
||
| 234 | echo '<p>', $lang['strnodata'], "</p>\n"; |
||
| 235 | } |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | private function doFooter($doBody = true, $template = 'footer.twig') |
||
| 334 | } |
||
| 335 | } |
||
| 336 |