| Conditions | 15 |
| Paths | 2187 |
| Total Lines | 111 |
| Code Lines | 71 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 113 | function wggithub_check_db($module) |
||
| 114 | { |
||
| 115 | $ret = true; |
||
| 116 | //insert here code for database check |
||
| 117 | |||
| 118 | // Example: update table (add new field) |
||
| 119 | $table = $GLOBALS['xoopsDB']->prefix('wggithub_repositories'); |
||
| 120 | $field = 'repo_release'; |
||
| 121 | $check = $GLOBALS['xoopsDB']->queryF('SHOW COLUMNS FROM `' . $table . "` LIKE '" . $field . "'"); |
||
| 122 | $numRows = $GLOBALS['xoopsDB']->getRowsNum($check); |
||
| 123 | if (!$numRows) { |
||
| 124 | $sql = "ALTER TABLE `$table` ADD `$field` INT(1) NOT NULL DEFAULT '0' AFTER `repo_htmlurl`;"; |
||
| 125 | if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) { |
||
| 126 | xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql); |
||
| 127 | $module->setErrors("Error when adding '$field' to table '$table'."); |
||
| 128 | $ret = false; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | // Example: update table (add new field) |
||
| 133 | $table = $GLOBALS['xoopsDB']->prefix('wggithub_repositories'); |
||
| 134 | $field = 'repo_prerelease'; |
||
| 135 | $check = $GLOBALS['xoopsDB']->queryF('SHOW COLUMNS FROM `' . $table . "` LIKE '" . $field . "'"); |
||
| 136 | $numRows = $GLOBALS['xoopsDB']->getRowsNum($check); |
||
| 137 | if (!$numRows) { |
||
| 138 | $sql = "ALTER TABLE `$table` ADD `$field` INT(1) NOT NULL DEFAULT '0' AFTER `repo_htmlurl`;"; |
||
| 139 | if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) { |
||
| 140 | xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql); |
||
| 141 | $module->setErrors("Error when adding '$field' to table '$table'."); |
||
| 142 | $ret = false; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | // Example: update table (add new field) |
||
| 146 | $table = $GLOBALS['xoopsDB']->prefix('wggithub_repositories'); |
||
| 147 | $field = 'repo_readme'; |
||
| 148 | $check = $GLOBALS['xoopsDB']->queryF('SHOW COLUMNS FROM `' . $table . "` LIKE '" . $field . "'"); |
||
| 149 | $numRows = $GLOBALS['xoopsDB']->getRowsNum($check); |
||
| 150 | if (!$numRows) { |
||
| 151 | $sql = "ALTER TABLE `$table` ADD `$field` INT(1) NOT NULL DEFAULT '0' AFTER `repo_htmlurl`;"; |
||
| 152 | if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) { |
||
| 153 | xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql); |
||
| 154 | $module->setErrors("Error when adding '$field' to table '$table'."); |
||
| 155 | $ret = false; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | // Example: update table (add new field) |
||
| 160 | $table = $GLOBALS['xoopsDB']->prefix('wggithub_directories'); |
||
| 161 | $field = 'dir_filterrelease'; |
||
| 162 | $check = $GLOBALS['xoopsDB']->queryF('SHOW COLUMNS FROM `' . $table . "` LIKE '" . $field . "'"); |
||
| 163 | $numRows = $GLOBALS['xoopsDB']->getRowsNum($check); |
||
| 164 | if (!$numRows) { |
||
| 165 | $sql = "ALTER TABLE `$table` ADD `$field` INT(1) NOT NULL DEFAULT '0' AFTER `dir_online`;"; |
||
| 166 | if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) { |
||
| 167 | xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql); |
||
| 168 | $module->setErrors("Error when adding '$field' to table '$table'."); |
||
| 169 | $ret = false; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | // Example: create new table |
||
| 174 | $table = $GLOBALS['xoopsDB']->prefix('wggithub_logs'); |
||
| 175 | $check = $GLOBALS['xoopsDB']->queryF("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='$table'"); |
||
| 176 | $numRows = $GLOBALS['xoopsDB']->getRowsNum($check); |
||
| 177 | if (!$numRows) { |
||
| 178 | // create new table |
||
| 179 | $sql = "CREATE TABLE `$table` ( |
||
| 180 | `log_id` INT(8) UNSIGNED NOT NULL AUTO_INCREMENT, |
||
| 181 | `log_type` INT(1) NOT NULL DEFAULT '0', |
||
| 182 | `log_details` VARCHAR(255) NOT NULL DEFAULT '', |
||
| 183 | `log_result` TEXT NOT NULL , |
||
| 184 | `log_datecreated` INT(11) NOT NULL DEFAULT '0', |
||
| 185 | `log_submitter` INT(10) NOT NULL DEFAULT '0', |
||
| 186 | PRIMARY KEY (`log_id`) |
||
| 187 | ) ENGINE=InnoDB;"; |
||
| 188 | if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) { |
||
| 189 | xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql); |
||
| 190 | $module->setErrors("Error when creating table '$table'."); |
||
| 191 | $ret = false; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | // Example: update table (add new field) |
||
| 196 | $table = $GLOBALS['xoopsDB']->prefix('wggithub_directories'); |
||
| 197 | $field = 'dir_content'; |
||
| 198 | $check = $GLOBALS['xoopsDB']->queryF('SHOW COLUMNS FROM `' . $table . "` LIKE '" . $field . "'"); |
||
| 199 | $numRows = $GLOBALS['xoopsDB']->getRowsNum($check); |
||
| 200 | if (!$numRows) { |
||
| 201 | $sql = "ALTER TABLE `$table` ADD `$field` INT(10) NOT NULL DEFAULT '0' AFTER `dir_type`;"; |
||
| 202 | if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) { |
||
| 203 | xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql); |
||
| 204 | $module->setErrors("Error when adding '$field' to table '$table'."); |
||
| 205 | $ret = false; |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | // Example: update table (add new field) |
||
| 210 | $table = $GLOBALS['xoopsDB']->prefix('wggithub_directories'); |
||
| 211 | $field = 'dir_descr'; |
||
| 212 | $check = $GLOBALS['xoopsDB']->queryF('SHOW COLUMNS FROM `' . $table . "` LIKE '" . $field . "'"); |
||
| 213 | $numRows = $GLOBALS['xoopsDB']->getRowsNum($check); |
||
| 214 | if (!$numRows) { |
||
| 215 | $sql = "ALTER TABLE `$table` ADD `$field` TEXT NOT NULL AFTER `dir_type`;"; |
||
| 216 | if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) { |
||
| 217 | xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql); |
||
| 218 | $module->setErrors("Error when adding '$field' to table '$table'."); |
||
| 219 | $ret = false; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | return $ret; |
||
| 224 | } |
||
| 225 |