Total Complexity | 60 |
Total Lines | 254 |
Duplicated Lines | 0 % |
Changes | 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 | var $readonly; |
||
28 | |||
29 | function init_conn($user, $passwd, $host, $name, $readonly) { |
||
30 | if (MYSQLI) { |
||
31 | $x = explode(":", $host); |
||
32 | if (sizeof($x)>1) { |
||
33 | $host = $x[0]; |
||
34 | $port = $x[1]; |
||
35 | } else { |
||
36 | $port = null; |
||
37 | } |
||
38 | //if (version_compare(PHP_VERSION, '5.3.0') < 0) { |
||
39 | if (1) { // don't use persistent connections for now |
||
40 | $this->db_conn = @new mysqli($host, $user, $passwd, $name, $port); |
||
|
|||
41 | } else { |
||
42 | $this->db_conn = @new mysqli("p:".$host, $user, $passwd, $name, $port); |
||
43 | } |
||
44 | // mysqli returns an object even if the connection is not established |
||
45 | if (mysqli_connect_error()) { |
||
46 | return false; |
||
47 | } |
||
48 | global $mysqli; |
||
49 | $mysqli = $this->db_conn; |
||
50 | } else { |
||
51 | $this->db_conn = @mysql_pconnect($host, $user, $passwd); |
||
52 | } |
||
53 | if (!$this->db_conn) { |
||
54 | return false; |
||
55 | } |
||
56 | $this->db_name = $name; |
||
57 | $this->readonly = $readonly; |
||
58 | return true; |
||
59 | } |
||
60 | |||
61 | // in keeping with PHP/MySQL convention, return true (nonzero) on success. |
||
62 | // (This is the opposite of the BOINC convention) |
||
63 | // |
||
64 | function do_query($q) { |
||
65 | global $generating_xml; |
||
66 | $q = str_replace('DBNAME', $this->db_name, $q); |
||
67 | //echo "query: $q<br>\n"; |
||
68 | if (MYSQLI) { |
||
69 | $ret = $this->db_conn->query($q); |
||
70 | } else { |
||
71 | $ret = mysql_query($q, $this->db_conn); |
||
72 | } |
||
73 | if (!$ret) { |
||
74 | if (!$generating_xml) { |
||
75 | echo "Database Error<br>\n"; |
||
76 | } |
||
77 | //echo ": ", $this->base_error(), "\n<pre>"; |
||
78 | //var_dump(debug_backtrace()); |
||
79 | //echo "</pre>query: $q\n"; |
||
80 | return null; |
||
81 | } |
||
82 | return $ret; |
||
83 | } |
||
84 | |||
85 | // # rows affected by last query |
||
86 | // |
||
87 | function affected_rows() { |
||
88 | if (MYSQLI) { |
||
89 | return $this->db_conn->affected_rows; |
||
90 | } else { |
||
91 | return mysql_affected_rows($this->db_conn); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | function get_list($table1, $table2, $joinfield1, $joinfield2, $classname, $fields, $where_clause, $order_clause, $limit) { |
||
112 | } |
||
113 | |||
114 | function lookup_fields($table, $classname, $fields, $clause) { |
||
115 | $query = "select $fields from DBNAME.$table where $clause"; |
||
116 | $result = $this->do_query($query); |
||
117 | if (!$result) { |
||
118 | return null; |
||
119 | } |
||
120 | if (MYSQLI) { |
||
121 | $obj = $result->fetch_object($classname); |
||
122 | $result->free(); |
||
123 | } else { |
||
124 | $obj = mysql_fetch_object($result, $classname); |
||
125 | mysql_free_result($result); |
||
126 | } |
||
127 | return $obj; |
||
128 | } |
||
129 | |||
130 | function lookup($table, $classname, $clause) { |
||
131 | return $this->lookup_fields($table, $classname, "*", $clause); |
||
132 | } |
||
133 | |||
134 | function lookup_id($id, $table, $classname) { |
||
135 | $id = (int)$id; |
||
136 | return $this->lookup($table, $classname, "id=$id"); |
||
137 | } |
||
138 | |||
139 | function enum_general($classname, $query) { |
||
140 | $result = $this->do_query($query); |
||
141 | if (!$result) return null; |
||
142 | $x = array(); |
||
143 | if (MYSQLI) { |
||
144 | while ($obj = $result->fetch_object($classname)) { |
||
145 | $x[] = $obj; |
||
146 | } |
||
147 | $result->free(); |
||
148 | } else { |
||
149 | while ($obj = mysql_fetch_object($result, $classname)) { |
||
150 | $x[] = $obj; |
||
151 | } |
||
152 | mysql_free_result($result); |
||
153 | } |
||
154 | return $x; |
||
155 | } |
||
156 | |||
157 | function enum_fields( |
||
166 | } |
||
167 | |||
168 | function enum($table, $classname, $where_clause=null, $order_clause=null) { |
||
169 | return self::enum_fields( |
||
170 | $table, $classname, '*', $where_clause, $order_clause |
||
171 | ); |
||
172 | } |
||
173 | |||
174 | function update($obj, $table, $clause) { |
||
175 | $query = "update DBNAME.$table set $clause where id=$obj->id"; |
||
176 | return $this->do_query($query); |
||
177 | } |
||
178 | function update_aux($table, $clause) { |
||
179 | $query = "update DBNAME.$table set $clause"; |
||
180 | return $this->do_query($query); |
||
181 | } |
||
182 | function insert($table, $clause) { |
||
183 | $query = "insert into DBNAME.$table $clause"; |
||
184 | return $this->do_query($query); |
||
185 | } |
||
186 | function delete($obj, $table) { |
||
187 | $query = "delete from DBNAME.$table where id=$obj->id"; |
||
188 | return $this->do_query($query); |
||
189 | } |
||
190 | function delete_aux($table, $clause) { |
||
191 | $query = "delete from DBNAME.$table where $clause"; |
||
192 | return $this->do_query($query); |
||
193 | } |
||
194 | function insert_id() { |
||
195 | if (MYSQLI) { |
||
196 | return $this->db_conn->insert_id; |
||
197 | } else { |
||
198 | return mysql_insert_id($this->db_conn); |
||
199 | } |
||
200 | } |
||
201 | function get_int($query, $field) { |
||
202 | $result = $this->do_query($query); |
||
203 | if (!$result) error_page("database error on query $query"); |
||
204 | if (MYSQLI) { |
||
205 | $x = $result->fetch_object("StdClass"); |
||
206 | $result->free(); |
||
207 | } else { |
||
208 | $x = mysql_fetch_object($result); |
||
209 | mysql_free_result($result); |
||
210 | } |
||
211 | if ($x) return $x->$field; |
||
212 | return false; |
||
213 | } |
||
214 | function get_double($query, $field) { |
||
215 | $result = $this->do_query($query); |
||
216 | if (!$result) error_page("database error on query $query"); |
||
217 | if (MYSQLI) { |
||
218 | $x = $result->fetch_object("StdClass"); |
||
219 | $result->free(); |
||
220 | } else { |
||
221 | $x = mysql_fetch_object($result); |
||
222 | mysql_free_result($result); |
||
223 | } |
||
224 | if ($x) return (double)$x->$field; |
||
225 | return false; |
||
226 | } |
||
227 | function count($table, $clause="TRUE") { |
||
228 | $query = "select count(*) as total from DBNAME.$table where $clause"; |
||
229 | return $this->get_int($query, 'total'); |
||
230 | } |
||
231 | function sum($table, $field, $clause="") { |
||
232 | $query = "select sum($field) as total from DBNAME.$table $clause"; |
||
233 | return $this->get_double($query, 'total'); |
||
234 | } |
||
235 | function max($table, $field, $clause="") { |
||
236 | $query = "select max($field) as total from DBNAME.$table $clause"; |
||
237 | return $this->get_double($query, 'total'); |
||
238 | } |
||
239 | function percentile($table, $field, $clause, $pct) { |
||
240 | $n = $this->count($table, $clause); |
||
241 | if (!$n) return false; |
||
242 | $m = (int)($n*$pct/100); |
||
243 | $query = "select $field from DBNAME.$table where $clause order by $field limit $m,1"; |
||
244 | return $this->get_double($query, $field); |
||
245 | } |
||
246 | function replace($table, $clause) { |
||
247 | $query = "replace into DBNAME.$table set $clause"; |
||
248 | return $this->do_query($query); |
||
249 | } |
||
250 | function base_escape_string($string) { |
||
251 | if (MYSQLI) { |
||
252 | return $this->db_conn->escape_string($string); |
||
253 | } else { |
||
254 | return mysql_real_escape_string($string); |
||
255 | } |
||
256 | } |
||
257 | function base_error() { |
||
258 | if (MYSQLI) { |
||
259 | return $this->db_conn->error; |
||
260 | } else { |
||
261 | return mysql_error($this->db_conn); |
||
262 | } |
||
263 | } |
||
264 | function base_errno() { |
||
265 | if (MYSQLI) { |
||
266 | return $this->db_conn->errno; |
||
267 | } else { |
||
268 | return mysql_errno($this->db_conn); |
||
269 | } |
||
270 | } |
||
271 | function table_exists($table_name) { |
||
278 | } |
||
279 | } |
||
280 | |||
281 | ?> |
||
282 |