| Total Complexity | 48 |
| Total Lines | 203 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DbConn 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 DbConn, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class DbConn { |
||
| 25 | var $db_conn; // a mysqli object |
||
| 26 | var $db_name; // the DB name |
||
| 27 | |||
| 28 | function init_conn($user, $passwd, $host, $name) { |
||
| 55 | } |
||
| 56 | |||
| 57 | function close() { |
||
| 58 | if ($this->db_conn) { |
||
| 59 | $this->db_conn->close(); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | // in keeping with PHP/MySQL convention, return true (nonzero) on success. |
||
| 64 | // (This is the opposite of the BOINC convention) |
||
| 65 | // |
||
| 66 | function do_query($q) { |
||
| 67 | global $generating_xml; |
||
| 68 | $q = str_replace('DBNAME', $this->db_name, $q); |
||
| 69 | //echo "query: $q<br>\n"; |
||
| 70 | $ret = $this->db_conn->query($q); |
||
| 71 | if (!$ret) { |
||
| 72 | if (!$generating_xml) { |
||
| 73 | echo "Database Error<br>\n"; |
||
| 74 | } |
||
| 75 | //echo ": ", $this->base_error(), "\n<pre>"; |
||
| 76 | //var_dump(debug_backtrace()); |
||
| 77 | //echo "</pre>query: $q\n"; |
||
| 78 | return null; |
||
| 79 | } |
||
| 80 | return $ret; |
||
| 81 | } |
||
| 82 | |||
| 83 | // # rows affected by last query |
||
| 84 | // |
||
| 85 | function affected_rows() { |
||
| 86 | return $this->db_conn->affected_rows; |
||
| 87 | } |
||
| 88 | |||
| 89 | function get_list($table1, $table2, $joinfield1, $joinfield2, $classname, $fields, $where_clause, $order_clause, $limit) { |
||
| 99 | } |
||
| 100 | |||
| 101 | function lookup_fields($table, $classname, $fields, $clause) { |
||
| 102 | $query = "select $fields from DBNAME.$table where $clause"; |
||
| 103 | $result = $this->do_query($query); |
||
| 104 | if (!$result) { |
||
| 105 | return null; |
||
| 106 | } |
||
| 107 | $obj = $result->fetch_object($classname); |
||
| 108 | $result->free(); |
||
| 109 | return $obj; |
||
| 110 | } |
||
| 111 | |||
| 112 | function lookup($table, $classname, $clause) { |
||
| 113 | return $this->lookup_fields($table, $classname, "*", $clause); |
||
| 114 | } |
||
| 115 | |||
| 116 | function lookup_id($id, $table, $classname) { |
||
| 117 | $id = (int)$id; |
||
| 118 | return $this->lookup($table, $classname, "id=$id"); |
||
| 119 | } |
||
| 120 | |||
| 121 | function enum_general($classname, $query) { |
||
| 122 | $result = $this->do_query($query); |
||
| 123 | if (!$result) return null; |
||
| 124 | $x = array(); |
||
| 125 | while ($obj = $result->fetch_object($classname)) { |
||
| 126 | $x[] = $obj; |
||
| 127 | } |
||
| 128 | $result->free(); |
||
| 129 | return $x; |
||
| 130 | } |
||
| 131 | |||
| 132 | function enum_fields( |
||
| 141 | } |
||
| 142 | |||
| 143 | function enum($table, $classname, $where_clause=null, $order_clause=null) { |
||
| 144 | return self::enum_fields( |
||
| 145 | $table, $classname, '*', $where_clause, $order_clause |
||
| 146 | ); |
||
| 147 | } |
||
| 148 | |||
| 149 | function update($obj, $table, $clause) { |
||
| 150 | $query = "update DBNAME.$table set $clause where id=$obj->id"; |
||
| 151 | return $this->do_query($query); |
||
| 152 | } |
||
| 153 | function update_aux($table, $clause) { |
||
| 154 | $query = "update DBNAME.$table set $clause"; |
||
| 155 | return $this->do_query($query); |
||
| 156 | } |
||
| 157 | function insert($table, $clause) { |
||
| 158 | $query = "insert into DBNAME.$table $clause"; |
||
| 159 | return $this->do_query($query); |
||
| 160 | } |
||
| 161 | function delete($obj, $table) { |
||
| 162 | $query = "delete from DBNAME.$table where id=$obj->id"; |
||
| 163 | return $this->do_query($query); |
||
| 164 | } |
||
| 165 | function delete_aux($table, $clause) { |
||
| 166 | $query = "delete from DBNAME.$table where $clause"; |
||
| 167 | return $this->do_query($query); |
||
| 168 | } |
||
| 169 | function insert_id() { |
||
| 170 | return $this->db_conn->insert_id; |
||
| 171 | } |
||
| 172 | function get_int($query, $field) { |
||
| 173 | $result = $this->do_query($query); |
||
| 174 | if (!$result) error_page("database error on query $query"); |
||
| 175 | $x = $result->fetch_object("StdClass"); |
||
| 176 | $result->free(); |
||
| 177 | if ($x) return $x->$field; |
||
| 178 | return false; |
||
| 179 | } |
||
| 180 | function get_double($query, $field) { |
||
| 181 | $result = $this->do_query($query); |
||
| 182 | if (!$result) error_page("database error on query $query"); |
||
| 183 | $x = $result->fetch_object("StdClass"); |
||
| 184 | $result->free(); |
||
| 185 | if ($x) return (double)$x->$field; |
||
| 186 | return false; |
||
| 187 | } |
||
| 188 | function count($table, $clause="TRUE") { |
||
| 189 | $query = "select count(*) as total from DBNAME.$table where $clause"; |
||
| 190 | return $this->get_int($query, 'total'); |
||
| 191 | } |
||
| 192 | function sum($table, $field, $clause="") { |
||
| 193 | $query = "select sum($field) as total from DBNAME.$table $clause"; |
||
| 194 | return $this->get_double($query, 'total'); |
||
| 195 | } |
||
| 196 | function max($table, $field, $clause="") { |
||
| 197 | $query = "select max($field) as total from DBNAME.$table $clause"; |
||
| 198 | return $this->get_double($query, 'total'); |
||
| 199 | } |
||
| 200 | function percentile($table, $field, $clause, $pct) { |
||
| 201 | $n = $this->count($table, $clause); |
||
| 202 | if (!$n) return false; |
||
| 203 | $m = (int)($n*$pct/100); |
||
| 204 | $query = "select $field from DBNAME.$table where $clause order by $field limit $m,1"; |
||
| 205 | return $this->get_double($query, $field); |
||
| 206 | } |
||
| 207 | function replace($table, $clause) { |
||
| 208 | $query = "replace into DBNAME.$table set $clause"; |
||
| 209 | return $this->do_query($query); |
||
| 210 | } |
||
| 211 | function base_escape_string($string) { |
||
| 212 | return $this->db_conn->escape_string($string); |
||
| 213 | } |
||
| 214 | function base_error() { |
||
| 215 | return $this->db_conn->error; |
||
| 216 | } |
||
| 217 | function base_errno() { |
||
| 218 | return $this->db_conn->errno; |
||
| 219 | } |
||
| 220 | function table_exists($table_name) { |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | ?> |
||
| 231 |