| Total Complexity | 46 |
| Total Lines | 197 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| 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; |
||
| 26 | var $db_name; |
||
| 27 | |||
| 28 | function init_conn($user, $passwd, $host, $name) { |
||
| 29 | $x = explode(":", $host); |
||
| 30 | if (sizeof($x)>1) { |
||
| 31 | $host = $x[0]; |
||
| 32 | $port = $x[1]; |
||
| 33 | } else { |
||
| 34 | $port = null; |
||
| 35 | } |
||
| 36 | if (1) { // don't use persistent connections for now |
||
| 37 | $this->db_conn = @new mysqli( |
||
| 38 | $host, $user, $passwd, $name, $port |
||
|
|
|||
| 39 | ); |
||
| 40 | } else { |
||
| 41 | $this->db_conn = @new mysqli( |
||
| 42 | 'p:'.$host, $user, $passwd, $name, $port |
||
| 43 | ); |
||
| 44 | } |
||
| 45 | if (mysqli_connect_error()) { |
||
| 46 | return false; |
||
| 47 | } |
||
| 48 | global $mysqli; |
||
| 49 | $mysqli = $this->db_conn; |
||
| 50 | if (!$this->db_conn) { |
||
| 51 | return false; |
||
| 52 | } |
||
| 53 | $this->db_name = $name; |
||
| 54 | return true; |
||
| 55 | } |
||
| 56 | |||
| 57 | // in keeping with PHP/MySQL convention, return true (nonzero) on success. |
||
| 58 | // (This is the opposite of the BOINC convention) |
||
| 59 | // |
||
| 60 | function do_query($q) { |
||
| 61 | global $generating_xml; |
||
| 62 | $q = str_replace('DBNAME', $this->db_name, $q); |
||
| 63 | //echo "query: $q<br>\n"; |
||
| 64 | $ret = $this->db_conn->query($q); |
||
| 65 | if (!$ret) { |
||
| 66 | if (!$generating_xml) { |
||
| 67 | echo "Database Error<br>\n"; |
||
| 68 | } |
||
| 69 | //echo ": ", $this->base_error(), "\n<pre>"; |
||
| 70 | //var_dump(debug_backtrace()); |
||
| 71 | //echo "</pre>query: $q\n"; |
||
| 72 | return null; |
||
| 73 | } |
||
| 74 | return $ret; |
||
| 75 | } |
||
| 76 | |||
| 77 | // # rows affected by last query |
||
| 78 | // |
||
| 79 | function affected_rows() { |
||
| 80 | return $this->db_conn->affected_rows; |
||
| 81 | } |
||
| 82 | |||
| 83 | function get_list($table1, $table2, $joinfield1, $joinfield2, $classname, $fields, $where_clause, $order_clause, $limit) { |
||
| 93 | } |
||
| 94 | |||
| 95 | function lookup_fields($table, $classname, $fields, $clause) { |
||
| 96 | $query = "select $fields from DBNAME.$table where $clause"; |
||
| 97 | $result = $this->do_query($query); |
||
| 98 | if (!$result) { |
||
| 99 | return null; |
||
| 100 | } |
||
| 101 | $obj = $result->fetch_object($classname); |
||
| 102 | $result->free(); |
||
| 103 | return $obj; |
||
| 104 | } |
||
| 105 | |||
| 106 | function lookup($table, $classname, $clause) { |
||
| 107 | return $this->lookup_fields($table, $classname, "*", $clause); |
||
| 108 | } |
||
| 109 | |||
| 110 | function lookup_id($id, $table, $classname) { |
||
| 111 | $id = (int)$id; |
||
| 112 | return $this->lookup($table, $classname, "id=$id"); |
||
| 113 | } |
||
| 114 | |||
| 115 | function enum_general($classname, $query) { |
||
| 116 | $result = $this->do_query($query); |
||
| 117 | if (!$result) return null; |
||
| 118 | $x = array(); |
||
| 119 | while ($obj = $result->fetch_object($classname)) { |
||
| 120 | $x[] = $obj; |
||
| 121 | } |
||
| 122 | $result->free(); |
||
| 123 | return $x; |
||
| 124 | } |
||
| 125 | |||
| 126 | function enum_fields( |
||
| 135 | } |
||
| 136 | |||
| 137 | function enum($table, $classname, $where_clause=null, $order_clause=null) { |
||
| 138 | return self::enum_fields( |
||
| 139 | $table, $classname, '*', $where_clause, $order_clause |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | |||
| 143 | function update($obj, $table, $clause) { |
||
| 144 | $query = "update DBNAME.$table set $clause where id=$obj->id"; |
||
| 145 | return $this->do_query($query); |
||
| 146 | } |
||
| 147 | function update_aux($table, $clause) { |
||
| 148 | $query = "update DBNAME.$table set $clause"; |
||
| 149 | return $this->do_query($query); |
||
| 150 | } |
||
| 151 | function insert($table, $clause) { |
||
| 152 | $query = "insert into DBNAME.$table $clause"; |
||
| 153 | return $this->do_query($query); |
||
| 154 | } |
||
| 155 | function delete($obj, $table) { |
||
| 156 | $query = "delete from DBNAME.$table where id=$obj->id"; |
||
| 157 | return $this->do_query($query); |
||
| 158 | } |
||
| 159 | function delete_aux($table, $clause) { |
||
| 160 | $query = "delete from DBNAME.$table where $clause"; |
||
| 161 | return $this->do_query($query); |
||
| 162 | } |
||
| 163 | function insert_id() { |
||
| 164 | return $this->db_conn->insert_id; |
||
| 165 | } |
||
| 166 | function get_int($query, $field) { |
||
| 167 | $result = $this->do_query($query); |
||
| 168 | if (!$result) error_page("database error on query $query"); |
||
| 169 | $x = $result->fetch_object("StdClass"); |
||
| 170 | $result->free(); |
||
| 171 | if ($x) return $x->$field; |
||
| 172 | return false; |
||
| 173 | } |
||
| 174 | function get_double($query, $field) { |
||
| 175 | $result = $this->do_query($query); |
||
| 176 | if (!$result) error_page("database error on query $query"); |
||
| 177 | $x = $result->fetch_object("StdClass"); |
||
| 178 | $result->free(); |
||
| 179 | if ($x) return (double)$x->$field; |
||
| 180 | return false; |
||
| 181 | } |
||
| 182 | function count($table, $clause="TRUE") { |
||
| 183 | $query = "select count(*) as total from DBNAME.$table where $clause"; |
||
| 184 | return $this->get_int($query, 'total'); |
||
| 185 | } |
||
| 186 | function sum($table, $field, $clause="") { |
||
| 187 | $query = "select sum($field) as total from DBNAME.$table $clause"; |
||
| 188 | return $this->get_double($query, 'total'); |
||
| 189 | } |
||
| 190 | function max($table, $field, $clause="") { |
||
| 191 | $query = "select max($field) as total from DBNAME.$table $clause"; |
||
| 192 | return $this->get_double($query, 'total'); |
||
| 193 | } |
||
| 194 | function percentile($table, $field, $clause, $pct) { |
||
| 195 | $n = $this->count($table, $clause); |
||
| 196 | if (!$n) return false; |
||
| 197 | $m = (int)($n*$pct/100); |
||
| 198 | $query = "select $field from DBNAME.$table where $clause order by $field limit $m,1"; |
||
| 199 | return $this->get_double($query, $field); |
||
| 200 | } |
||
| 201 | function replace($table, $clause) { |
||
| 202 | $query = "replace into DBNAME.$table set $clause"; |
||
| 203 | return $this->do_query($query); |
||
| 204 | } |
||
| 205 | function base_escape_string($string) { |
||
| 206 | return $this->db_conn->escape_string($string); |
||
| 207 | } |
||
| 208 | function base_error() { |
||
| 209 | return $this->db_conn->error; |
||
| 210 | } |
||
| 211 | function base_errno() { |
||
| 212 | return $this->db_conn->errno; |
||
| 213 | } |
||
| 214 | function table_exists($table_name) { |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | ?> |
||
| 225 |