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; // a mysqli object |
26
|
|
|
var $db_name; // the 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
|
|
|
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) { |
90
|
|
|
$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"; |
91
|
|
|
$result = $this->do_query($query); |
92
|
|
|
if (!$result) return null; |
93
|
|
|
$x = array(); |
94
|
|
|
while ($obj = $result->fetch_object($classname)) { |
95
|
|
|
$x[] = $obj; |
96
|
|
|
} |
97
|
|
|
$result->free(); |
98
|
|
|
return $x; |
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( |
133
|
|
|
$table, $classname, $fields, $where_clause, $order_clause |
|
|
|
|
134
|
|
|
) { |
135
|
|
|
$x = array(); |
136
|
|
|
if ($where_clause) { |
137
|
|
|
$where_clause = "where $where_clause"; |
138
|
|
|
} |
139
|
|
|
$query = "select $fields from DBNAME.$table $where_clause $order_clause"; |
140
|
|
|
return $this->enum_general($classname,$query); |
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) { |
221
|
|
|
$result = $this->do_query("show tables from DBNAME like '$table_name'"); |
222
|
|
|
if (!$result) error_page("database error on query $query"); |
223
|
|
|
$t = _mysql_fetch_array($result); |
224
|
|
|
_mysql_free_result($result); |
225
|
|
|
if ($t[0] == "$table_name") return true; |
226
|
|
|
return false; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
?> |
231
|
|
|
|