2007-10-26 21:14:35 +00:00
|
|
|
<?php
|
2008-08-05 22:43:14 +00:00
|
|
|
// This file is part of BOINC.
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2008 University of California
|
|
|
|
//
|
|
|
|
// BOINC is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Lesser General Public License
|
|
|
|
// as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2007-10-26 21:14:35 +00:00
|
|
|
|
2009-09-28 04:24:18 +00:00
|
|
|
// Should not be enabled for public code
|
2007-10-30 17:54:29 +00:00
|
|
|
//ini_set('display_errors', 'stdout');
|
|
|
|
//error_reporting(E_ALL);
|
2007-10-26 21:14:35 +00:00
|
|
|
|
2008-10-04 23:44:24 +00:00
|
|
|
/// represents a connection to a database.
|
2007-10-26 21:14:35 +00:00
|
|
|
// Intended to be subclassed (e.g., BoincDb, BossaDb)
|
|
|
|
|
|
|
|
class DbConn {
|
2007-10-29 04:02:41 +00:00
|
|
|
var $db_conn;
|
|
|
|
var $db_name;
|
2007-10-26 21:14:35 +00:00
|
|
|
|
2007-10-29 16:38:25 +00:00
|
|
|
function init_conn($user, $passwd, $host, $name) {
|
|
|
|
$this->db_conn = mysql_pconnect($host, $user, $passwd);
|
2007-10-26 21:14:35 +00:00
|
|
|
if (!$this->db_conn) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-10-29 16:38:25 +00:00
|
|
|
$this->db_name = $name;
|
2007-10-26 21:14:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-25 05:54:09 +00:00
|
|
|
function do_query($q) {
|
2007-11-29 23:26:49 +00:00
|
|
|
//echo "query: $q<br>\n";
|
2007-10-28 15:03:14 +00:00
|
|
|
$ret = mysql_query($q, $this->db_conn);
|
|
|
|
if (!$ret) {
|
2007-11-29 23:26:49 +00:00
|
|
|
echo "Database Error<br>\n";
|
2007-11-12 16:00:37 +00:00
|
|
|
//echo ": ", mysql_error(), "\n<pre>";
|
2007-11-05 23:55:33 +00:00
|
|
|
//var_dump(debug_backtrace());
|
2007-11-12 16:00:37 +00:00
|
|
|
//echo "</pre>query: $q\n";
|
|
|
|
return null;
|
2007-10-28 15:03:14 +00:00
|
|
|
}
|
|
|
|
return $ret;
|
2007-10-26 21:14:35 +00:00
|
|
|
}
|
|
|
|
|
2008-12-16 23:59:04 +00:00
|
|
|
function affected_rows() {
|
|
|
|
return mysql_affected_rows($this->db_conn);
|
|
|
|
}
|
|
|
|
|
2007-12-23 23:09:10 +00:00
|
|
|
function lookup_fields($table, $classname, $fields, $clause) {
|
2010-11-25 05:54:09 +00:00
|
|
|
$query = "select $fields from ".$this->db_name.".$table where $clause";
|
2007-12-23 23:09:10 +00:00
|
|
|
$result = $this->do_query($query);
|
|
|
|
if (!$result) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$obj = mysql_fetch_object($result, $classname);
|
|
|
|
mysql_free_result($result);
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
2007-10-26 21:14:35 +00:00
|
|
|
function lookup($table, $classname, $clause) {
|
2010-11-25 05:54:09 +00:00
|
|
|
$query = "select * from ".$this->db_name.".$table where $clause";
|
2007-10-26 21:14:35 +00:00
|
|
|
$result = $this->do_query($query);
|
|
|
|
if (!$result) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$obj = mysql_fetch_object($result, $classname);
|
|
|
|
mysql_free_result($result);
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
function lookup_id($id, $table, $classname) {
|
|
|
|
return $this->lookup($table, $classname, "id=$id");
|
|
|
|
}
|
|
|
|
|
2007-11-10 00:32:42 +00:00
|
|
|
function enum_general($classname, $query) {
|
|
|
|
$result = $this->do_query($query);
|
|
|
|
if (!$result) return null;
|
2010-01-15 23:08:55 +00:00
|
|
|
$x = array();
|
2007-11-10 00:32:42 +00:00
|
|
|
while ($obj = mysql_fetch_object($result, $classname)) {
|
|
|
|
$x[] = $obj;
|
|
|
|
}
|
|
|
|
mysql_free_result($result);
|
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
|
2007-11-05 23:55:33 +00:00
|
|
|
function enum_fields(
|
|
|
|
$table, $classname, $fields, $where_clause, $order_clause
|
|
|
|
) {
|
2007-10-26 21:14:35 +00:00
|
|
|
$x = array();
|
2007-11-05 23:55:33 +00:00
|
|
|
if ($where_clause) {
|
|
|
|
$where_clause = "where $where_clause";
|
2007-10-26 21:14:35 +00:00
|
|
|
}
|
2010-11-25 05:54:09 +00:00
|
|
|
$query = "select $fields from ".$this->db_name.".$table $where_clause $order_clause";
|
2007-10-26 21:14:35 +00:00
|
|
|
$result = $this->do_query($query);
|
|
|
|
if (!$result) return null;
|
2010-01-15 23:08:55 +00:00
|
|
|
$x = array();
|
2007-10-26 21:14:35 +00:00
|
|
|
while ($obj = mysql_fetch_object($result, $classname)) {
|
|
|
|
$x[] = $obj;
|
|
|
|
}
|
|
|
|
mysql_free_result($result);
|
|
|
|
return $x;
|
|
|
|
}
|
2007-11-05 23:55:33 +00:00
|
|
|
function enum($table, $classname, $where_clause=null, $order_clause=null) {
|
|
|
|
return self::enum_fields(
|
|
|
|
$table, $classname, '*', $where_clause, $order_clause
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-10-26 21:14:35 +00:00
|
|
|
function update($obj, $table, $clause) {
|
2010-11-25 05:54:09 +00:00
|
|
|
$query = "update ".$this->db_name.".$table set $clause where id=$obj->id";
|
2007-10-26 21:14:35 +00:00
|
|
|
return $this->do_query($query);
|
|
|
|
}
|
2007-10-29 04:02:41 +00:00
|
|
|
function update_aux($table, $clause) {
|
2010-11-25 05:54:09 +00:00
|
|
|
$query = "update ".$this->db_name.".$table set $clause";
|
2007-10-28 15:03:14 +00:00
|
|
|
return $this->do_query($query);
|
|
|
|
}
|
|
|
|
function insert($table, $clause) {
|
2010-11-25 05:54:09 +00:00
|
|
|
$query = "insert into ".$this->db_name.".$table $clause";
|
2007-10-28 15:03:14 +00:00
|
|
|
return $this->do_query($query);
|
|
|
|
}
|
2007-10-27 20:38:12 +00:00
|
|
|
function delete($obj, $table) {
|
2010-11-25 05:54:09 +00:00
|
|
|
$query = "delete from ".$this->db_name.".$table where id=$obj->id";
|
2007-10-27 20:38:12 +00:00
|
|
|
return $this->do_query($query);
|
|
|
|
}
|
2007-11-07 17:23:29 +00:00
|
|
|
function delete_aux($table, $clause) {
|
2010-11-25 05:54:09 +00:00
|
|
|
$query = "delete from ".$this->db_name.".$table where $clause";
|
2007-11-07 17:23:29 +00:00
|
|
|
return $this->do_query($query);
|
|
|
|
}
|
2007-10-26 21:14:35 +00:00
|
|
|
function insert_id() {
|
|
|
|
return mysql_insert_id($this->db_conn);
|
|
|
|
}
|
|
|
|
function count($table, $clause) {
|
2010-11-25 05:54:09 +00:00
|
|
|
$query = "select count(*) as total from ".$this->db_name.".$table where $clause";
|
2007-10-26 21:14:35 +00:00
|
|
|
$result = $this->do_query($query);
|
|
|
|
$cnt = mysql_fetch_object($result);
|
2008-11-02 20:15:06 +00:00
|
|
|
mysql_free_result($result);
|
2007-10-26 21:14:35 +00:00
|
|
|
if ($cnt) return $cnt->total;
|
|
|
|
return null;
|
|
|
|
}
|
2011-06-21 22:56:15 +00:00
|
|
|
function sum($table, $field, $clause="") {
|
|
|
|
$query = "select sum($field) as total from ".$this->db_name.".$table $clause";
|
|
|
|
$result = $this->do_query($query);
|
|
|
|
$cnt = mysql_fetch_object($result);
|
|
|
|
mysql_free_result($result);
|
|
|
|
if ($cnt) return (double)$cnt->total;
|
|
|
|
return null;
|
|
|
|
}
|
2007-11-10 00:32:42 +00:00
|
|
|
function replace($table, $clause) {
|
2010-11-25 05:54:09 +00:00
|
|
|
$query = "replace into ".$this->db_name.".$table set $clause";
|
2007-11-10 00:32:42 +00:00
|
|
|
return $this->do_query($query);
|
|
|
|
}
|
2007-10-27 20:38:12 +00:00
|
|
|
function base_escape_string($string) {
|
2009-01-20 21:31:13 +00:00
|
|
|
return mysql_real_escape_string($string);
|
2007-10-27 20:38:12 +00:00
|
|
|
}
|
2007-11-12 22:28:17 +00:00
|
|
|
function base_error() {
|
|
|
|
return mysql_error($this->db_conn);
|
|
|
|
}
|
2008-06-30 23:05:16 +00:00
|
|
|
function table_exists($table_name) {
|
2010-11-25 05:54:09 +00:00
|
|
|
$result = $this->do_query("show tables from ".$this->db_name." like '$table_name'");
|
2008-08-14 15:46:30 +00:00
|
|
|
$t = mysql_fetch_array($result);
|
2008-06-30 23:05:16 +00:00
|
|
|
mysql_free_result($result);
|
2008-08-14 15:46:30 +00:00
|
|
|
if ($t[0] == "$table_name") return true;
|
2008-06-30 23:05:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-10-26 21:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|