Passed
Pull Request — master (#5695)
by
unknown
11:53
created

DbConn::init_conn()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 20
nc 12
nop 4
dl 0
loc 27
rs 9.2888
c 1
b 0
f 0
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
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
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

38
                $host, $user, $passwd, $name, /** @scrutinizer ignore-type */ $port
Loading history...
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) {
84
        $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";
85
        $result = $this->do_query($query);
86
        if (!$result) return null;
87
        $x = array();
88
        while ($obj = $result->fetch_object($classname)) {
89
            $x[] = $obj;
90
        }
91
        $result->free();
92
        return $x;
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(
127
        $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...
128
    ) {
129
        $x = array();
130
        if ($where_clause) {
131
            $where_clause = "where $where_clause";
132
        }
133
        $query = "select $fields from DBNAME.$table $where_clause $order_clause";
134
        return $this->enum_general($classname,$query);
135
    }
136
137
    function enum($table, $classname, $where_clause=null, $order_clause=null) {
138
        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

138
        return self::/** @scrutinizer ignore-call */ enum_fields(
Loading history...
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) {
215
        $result = $this->do_query("show tables from DBNAME like '$table_name'");
216
        if (!$result) error_page("database error on query $query");
217
        $t = _mysql_fetch_array($result);
218
        _mysql_free_result($result);
219
        if ($t[0] == "$table_name") return true;
220
        return false;
221
    }
222
}
223
224
?>
225