DbConn   F
last analyzed

Complexity

Total Complexity 60

Size/Duplication

Total Lines 254
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 152
dl 0
loc 254
rs 3.6
c 0
b 0
f 0
wmc 60

27 Methods

Rating   Name   Duplication   Size   Complexity  
A get_list() 0 17 5
A enum_fields() 0 9 2
A do_query() 0 19 4
A table_exists() 0 7 3
A delete_aux() 0 3 1
A base_escape_string() 0 5 2
A affected_rows() 0 5 2
A enum_general() 0 16 5
A replace() 0 3 1
A base_errno() 0 5 2
A update() 0 3 1
B init_conn() 0 30 6
A insert() 0 3 1
A update_aux() 0 3 1
A sum() 0 3 1
A base_error() 0 5 2
A lookup() 0 2 1
A enum() 0 3 1
A count() 0 3 1
A lookup_fields() 0 14 3
A get_int() 0 12 4
A max() 0 3 1
A percentile() 0 6 2
A lookup_id() 0 3 1
A delete() 0 3 1
A insert_id() 0 5 2
A get_double() 0 12 4

How to fix   Complexity   

Complex Class

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
2
// This file is part of BOINC.
3
// http://boinc.berkeley.edu
4
// Copyright (C) 2008 University of California
5
//
6
// BOINC is free software; you can redistribute it and/or modify it
7
// under the terms of the GNU Lesser General Public License
8
// as published by the Free Software Foundation,
9
// either version 3 of the License, or (at your option) any later version.
10
//
11
// BOINC is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
// See the GNU Lesser General Public License for more details.
15
//
16
// You should have received a copy of the GNU Lesser General Public License
17
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
18
19
require_once("../inc/db.inc");
20
21
// represents a connection to a database.
22
// Intended to be subclassed (e.g., BoincDb, BossaDb)
23
//
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);
0 ignored issues
show
Bug introduced by
It seems like $port can also be of type string; however, parameter $port of mysqli::__construct() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
                $this->db_conn = @new mysqli($host, $user, $passwd, $name, /** @scrutinizer ignore-type */ $port);
Loading history...
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) {
96
        $query = "select $fields from DBNAME.$table1 a join DBNAME.$table2 b on a.$joinfield1=b.$joinfield2 where $where_clause order by $order_clause desc limit $limit";
97
        $result = $this->do_query($query);
98
        if (!$result) return null;
99
        $x = array();
100
        if (MYSQLI) {
101
            while ($obj = $result->fetch_object($classname)) {
102
                $x[] = $obj;
103
            }
104
            $result->free();
105
        } else {
106
            while ($obj = mysql_fetch_object($result, $classname)) {
107
                $x[] = $obj;
108
            }
109
            mysql_free_result($result);
110
        }
111
        return $x;
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(
158
        $table, $classname, $fields, $where_clause, $order_clause
0 ignored issues
show
Coding Style introduced by
Multi-line function declarations must define one parameter per line
Loading history...
159
    ) {
160
        $x = array();
161
        if ($where_clause) {
162
            $where_clause = "where $where_clause";
163
        }
164
        $query = "select $fields from DBNAME.$table $where_clause $order_clause";
165
        return $this->enum_general($classname,$query);
166
    }
167
168
    function enum($table, $classname, $where_clause=null, $order_clause=null) {
169
        return self::enum_fields(
0 ignored issues
show
Bug Best Practice introduced by
The method DbConn::enum_fields() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

169
        return self::/** @scrutinizer ignore-call */ enum_fields(
Loading history...
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) {
272
        $result = $this->do_query("show tables from DBNAME like '$table_name'");
273
        if (!$result) error_page("database error on query $query");
274
        $t = _mysql_fetch_array($result);
275
        _mysql_free_result($result);
276
        if ($t[0] == "$table_name") return true;
277
        return false;
278
    }
279
}
280
281
?>
282