| Total Complexity | 48 |
| Total Lines | 266 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Query 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 Query, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Query |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Prepare a query to insert into db. |
||
| 22 | * |
||
| 23 | * @param |
||
| 24 | * $table Name of tabke |
||
| 25 | * array array(); e.g: |
||
| 26 | * 'name' => 'new name' or $comeformvariable |
||
| 27 | * 'username' => 'new username' or $comeformvariable |
||
| 28 | * |
||
| 29 | * @return query |
||
| 30 | */ |
||
|
|
|||
| 31 | public function insert($params) |
||
| 32 | { |
||
| 33 | if (is_array($params)) { |
||
| 34 | $count_rows = count($params['columns']); |
||
| 35 | $increment = 1; |
||
| 36 | foreach ($params['columns'] as $keys => $value) { |
||
| 37 | for ($i = 1; $i <= $count_rows; $i++) { |
||
| 38 | $data[$keys] = $value; |
||
| 39 | } |
||
| 40 | } |
||
| 41 | foreach ($data as $keys => $values) { |
||
| 42 | if ($increment == $count_rows) { |
||
| 43 | $columns[] = "{$keys} = '{$values}'"; |
||
| 44 | } else { |
||
| 45 | $columns[] = "{$keys} = '{$values}'"; |
||
| 46 | } |
||
| 47 | $increment++; |
||
| 48 | } |
||
| 49 | $columns = implode(' , ', $columns); |
||
| 50 | $query = "INSERT INTO `{$params['table']}`SET {$columns}"; |
||
| 51 | if (isset($params['debug']) and strtolower($params['debug']) === 'on') { |
||
| 52 | var_dump($query); |
||
| 53 | } |
||
| 54 | |||
| 55 | return $query; |
||
| 56 | } else { |
||
| 57 | return false; |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Prepare a query to Update data in database. |
||
| 63 | * |
||
| 64 | * @param array $params; e.g: |
||
| 65 | * 'table' required name of table |
||
| 66 | * 'db_name' => Database name |
||
| 67 | * 'wheres' Specify id or else for updating records |
||
| 68 | * 'columns' => data e.g name=>new name |
||
| 69 | * |
||
| 70 | * @return query |
||
| 71 | */ |
||
| 72 | public function update($params) |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Prepare a query to select data from database. |
||
| 105 | * |
||
| 106 | * @param array array(); |
||
| 107 | * 'table' Names of table |
||
| 108 | * 'db_name' => Database name |
||
| 109 | * 'params' Names of columns which you want to select |
||
| 110 | * 'wheres' Specify a selection criteria to get required records |
||
| 111 | * 'debug' If on var_dump sql query |
||
| 112 | * |
||
| 113 | * @return query |
||
| 114 | */ |
||
| 115 | public function select($params) |
||
| 116 | { |
||
| 117 | if (is_array($params)) { |
||
| 118 | if (!isset($params['params'])) { |
||
| 119 | $columns = '*'; |
||
| 120 | } else { |
||
| 121 | $columns = implode(', ', array_values($params['params'])); |
||
| 122 | } |
||
| 123 | if (isset($params['distinct'])) { |
||
| 124 | $distinct = ' DISTINCT '; |
||
| 125 | } else { |
||
| 126 | $distinct = ''; |
||
| 127 | } |
||
| 128 | $wheres = (isset($params['wheres'])) ? $this->prepareWhere($params['wheres']) : ''; |
||
| 129 | if (isset($params['joins'])) { |
||
| 130 | if (!empty($params['joins'])) { |
||
| 131 | if (!isset($params['joins']['using'])) { |
||
| 132 | $join = ' JOIN '.$params['joins']['table2'].' ON '.$params['joins']['column1'].' = '.$params['joins']['column2']; |
||
| 133 | } else { |
||
| 134 | $join = ' JOIN '.$params['joins']['table2'].' Using '.$params['joins']['using']; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } else { |
||
| 138 | $join = ''; |
||
| 139 | } |
||
| 140 | if (isset($params['limit'])) { |
||
| 141 | if (!empty($params['limit'])) { |
||
| 142 | $limit = ' LIMIT '.$params['limit']['start'].' OFFSET '.$params['limit']['end']; |
||
| 143 | } else { |
||
| 144 | $limit = ''; |
||
| 145 | } |
||
| 146 | } else { |
||
| 147 | $limit = ''; |
||
| 148 | } |
||
| 149 | if (isset($params['order_by'])) { |
||
| 150 | if (!empty($params['order_by'])) { |
||
| 151 | $order_by = ' ORDER BY '.$params['order_by']; |
||
| 152 | } else { |
||
| 153 | $order_by = ''; |
||
| 154 | } |
||
| 155 | } else { |
||
| 156 | $order_by = ''; |
||
| 157 | } |
||
| 158 | $query = "SELECT {$distinct} {$columns} FROM {$params['table']} {$join} {$wheres} {$order_by} {$limit} ;"; |
||
| 159 | if (isset($params['debug']) and strtolower($params['debug']) === 'on') { |
||
| 160 | var_dump($query); |
||
| 161 | } |
||
| 162 | |||
| 163 | return $query; |
||
| 164 | } else { |
||
| 165 | return false; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Prepare a query to delete data from database. |
||
| 171 | * |
||
| 172 | * @param $params array array(); |
||
| 173 | * 'table' Names of table |
||
| 174 | * 'db_name' => Database name |
||
| 175 | * 'wheres' Specify a selection criteria to get required records |
||
| 176 | * |
||
| 177 | * @return query |
||
| 178 | */ |
||
| 179 | public function delete($params) |
||
| 180 | { |
||
| 181 | if (is_array($params)) { |
||
| 182 | if (!empty($params['wheres'])) { |
||
| 183 | $wheres = $this->prepareWhere($params['wheres']); |
||
| 184 | } else { |
||
| 185 | return false; |
||
| 186 | } |
||
| 187 | $query = "DELETE FROM `{$params['table']}` {$wheres};"; |
||
| 188 | if (isset($params['debug']) and strtolower($params['debug']) === 'on') { |
||
| 189 | var_dump($query); |
||
| 190 | } |
||
| 191 | |||
| 192 | return $query; |
||
| 193 | } else { |
||
| 194 | return false; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * prepare the where statement. |
||
| 200 | * |
||
| 201 | * @param 'wheres' Specify a selection criteria to get required records |
||
| 202 | * |
||
| 203 | * @return query |
||
| 204 | */ |
||
| 205 | public function prepareWhere($wheres = null) |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Prepare the use statement. |
||
| 222 | * |
||
| 223 | * @param $name name of database |
||
| 224 | * |
||
| 225 | * @return query |
||
| 226 | */ |
||
| 227 | public function useQuery($name) |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Creating database query if not exists. |
||
| 234 | * |
||
| 235 | * @param $name name of database |
||
| 236 | * |
||
| 237 | * @return query |
||
| 238 | */ |
||
| 239 | public function createDb($name) |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Deleting database query if not exists. |
||
| 252 | * |
||
| 253 | * @param $name name of database |
||
| 254 | * |
||
| 255 | * @return query |
||
| 256 | */ |
||
| 257 | public function deleteDb($name) |
||
| 258 | { |
||
| 259 | if (isset($name) && !empty(trim($name))) { |
||
| 260 | $sql = "DROP DATABASE `{$name}` "; |
||
| 261 | |||
| 262 | return $sql; |
||
| 263 | } else { |
||
| 264 | return false; |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Deleting table query if not exists. |
||
| 270 | * |
||
| 271 | * @param $dbname name of database |
||
| 272 | * $table => $table name |
||
| 273 | * |
||
| 274 | * @return query |
||
| 275 | */ |
||
| 276 | public function deleteTbl($table) |
||
| 284 | } |
||
| 285 | } |
||
| 286 | } |
||
| 287 |