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
2014-09-28 19:15:26 +00:00
require_once ( " ../inc/db.inc " );
2014-09-24 17:37:10 +00:00
// represents a connection to a database.
// Intended to be subclassed (e.g., BoincDb, BossaDb)
//
2007-10-26 21:14:35 +00:00
class DbConn {
2007-10-29 04:02:41 +00:00
var $db_conn ;
var $db_name ;
2015-10-19 21:02:55 +00:00
var $readonly ;
2007-10-26 21:14:35 +00:00
2015-10-19 21:02:55 +00:00
function init_conn ( $user , $passwd , $host , $name , $readonly ) {
2014-03-21 08:08:47 +00:00
if ( MYSQLI ) {
2014-10-02 07:54:17 +00:00
$x = explode ( " : " , $host );
2014-10-01 15:31:38 +00:00
if ( sizeof ( $x ) > 1 ) {
$host = $x [ 0 ];
$port = $x [ 1 ];
} else {
$port = null ;
}
2014-10-22 05:12:03 +00:00
//if (version_compare(PHP_VERSION, '5.3.0') < 0) {
if ( 1 ) { // don't use persistent connections for now
2015-08-06 17:46:59 +00:00
$this -> db_conn = @ new mysqli ( $host , $user , $passwd , $name , $port );
2014-04-11 18:02:12 +00:00
} else {
2015-08-06 17:46:59 +00:00
$this -> db_conn = @ new mysqli ( " p: " . $host , $user , $passwd , $name , $port );
2014-04-11 18:02:12 +00:00
}
2015-11-18 09:44:46 +00:00
// mysqli returns an object even if the connection is not established
if ( mysqli_connect_error ()) {
return false ;
}
2014-09-04 19:00:09 +00:00
global $mysqli ;
$mysqli = $this -> db_conn ;
2014-03-21 08:08:47 +00:00
} else {
2015-08-04 17:57:05 +00:00
$this -> db_conn = @ mysql_pconnect ( $host , $user , $passwd );
2014-03-21 08:08:47 +00:00
}
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 ;
2015-10-19 21:02:55 +00:00
$this -> readonly = $readonly ;
2007-10-26 21:14:35 +00:00
return true ;
}
2013-11-23 06:46:19 +00:00
// in keeping with PHP/MySQL convention, return true (nonzero) on success.
// (This is the opposite of the BOINC convention)
//
2010-11-25 05:54:09 +00:00
function do_query ( $q ) {
2013-02-19 20:16:21 +00:00
global $generating_xml ;
2014-01-14 05:52:55 +00:00
$q = str_replace ( 'DBNAME' , $this -> db_name , $q );
2007-11-29 23:26:49 +00:00
//echo "query: $q<br>\n";
2014-03-21 08:08:47 +00:00
if ( MYSQLI ) {
$ret = $this -> db_conn -> query ( $q );
} else {
$ret = mysql_query ( $q , $this -> db_conn );
}
2007-10-28 15:03:14 +00:00
if ( ! $ret ) {
2013-02-19 20:16:21 +00:00
if ( ! $generating_xml ) {
echo " Database Error<br> \n " ;
}
2014-03-21 08:08:47 +00:00
//echo ": ", $this->base_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
}
2013-12-17 03:22:34 +00:00
// # rows affected by last query
//
2008-12-16 23:59:04 +00:00
function affected_rows () {
2014-03-21 08:08:47 +00:00
if ( MYSQLI ) {
2018-04-06 18:30:01 +00:00
return $this -> db_conn -> affected_rows ;
2014-03-21 08:08:47 +00:00
} else {
return mysql_affected_rows ( $this -> db_conn );
}
2008-12-16 23:59:04 +00:00
}
2017-03-27 11:46:55 +00:00
function get_list ( $table1 , $table2 , $joinfield1 , $joinfield2 , $classname , $fields , $where_clause , $order_clause , $limit ) {
2017-03-23 14:27:25 +00:00
$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 " ;
$result = $this -> do_query ( $query );
if ( ! $result ) return null ;
$x = array ();
if ( MYSQLI ) {
while ( $obj = $result -> fetch_object ( $classname )) {
$x [] = $obj ;
}
$result -> free ();
} else {
while ( $obj = mysql_fetch_object ( $result , $classname )) {
$x [] = $obj ;
}
mysql_free_result ( $result );
}
return $x ;
}
2007-12-23 23:09:10 +00:00
function lookup_fields ( $table , $classname , $fields , $clause ) {
2014-01-14 05:52:55 +00:00
$query = " select $fields from DBNAME. $table where $clause " ;
2007-12-23 23:09:10 +00:00
$result = $this -> do_query ( $query );
if ( ! $result ) {
return null ;
}
2014-03-21 08:08:47 +00:00
if ( MYSQLI ) {
$obj = $result -> fetch_object ( $classname );
$result -> free ();
} else {
$obj = mysql_fetch_object ( $result , $classname );
mysql_free_result ( $result );
}
2007-12-23 23:09:10 +00:00
return $obj ;
}
2007-10-26 21:14:35 +00:00
function lookup ( $table , $classname , $clause ) {
2014-03-21 08:08:47 +00:00
return $this -> lookup_fields ( $table , $classname , " * " , $clause );
2007-10-26 21:14:35 +00:00
}
function lookup_id ( $id , $table , $classname ) {
2014-11-21 23:37:40 +00:00
$id = ( int ) $id ;
2007-10-26 21:14:35 +00:00
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 ();
2014-03-21 08:08:47 +00:00
if ( MYSQLI ) {
while ( $obj = $result -> fetch_object ( $classname )) {
$x [] = $obj ;
}
$result -> free ();
} else {
while ( $obj = mysql_fetch_object ( $result , $classname )) {
$x [] = $obj ;
}
mysql_free_result ( $result );
2007-11-10 00:32:42 +00:00
}
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
}
2014-01-14 05:52:55 +00:00
$query = " select $fields from DBNAME. $table $where_clause $order_clause " ;
2014-03-21 08:08:47 +00:00
return $this -> enum_general ( $classname , $query );
2007-10-26 21:14:35 +00:00
}
2014-03-21 08:08:47 +00:00
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 ) {
2014-01-14 05:52:55 +00:00
$query = " update DBNAME. $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 ) {
2014-01-14 05:52:55 +00:00
$query = " update DBNAME. $table set $clause " ;
2007-10-28 15:03:14 +00:00
return $this -> do_query ( $query );
}
function insert ( $table , $clause ) {
2014-01-14 05:52:55 +00:00
$query = " insert into DBNAME. $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 ) {
2014-01-14 05:52:55 +00:00
$query = " delete from DBNAME. $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 ) {
2014-01-14 05:52:55 +00:00
$query = " delete from DBNAME. $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 () {
2014-03-21 08:08:47 +00:00
if ( MYSQLI ) {
return $this -> db_conn -> insert_id ;
} else {
return mysql_insert_id ( $this -> db_conn );
}
2007-10-26 21:14:35 +00:00
}
2014-01-14 05:52:55 +00:00
function get_int ( $query , $field ) {
2007-10-26 21:14:35 +00:00
$result = $this -> do_query ( $query );
2014-06-19 22:22:52 +00:00
if ( ! $result ) error_page ( " database error on query $query " );
2014-03-22 16:04:58 +00:00
if ( MYSQLI ) {
$x = $result -> fetch_object ( " StdClass " );
$result -> free ();
} else {
$x = mysql_fetch_object ( $result );
mysql_free_result ( $result );
}
2014-01-14 05:52:55 +00:00
if ( $x ) return $x -> $field ;
return false ;
2007-10-26 21:14:35 +00:00
}
2014-01-14 05:52:55 +00:00
function get_double ( $query , $field ) {
2011-06-21 22:56:15 +00:00
$result = $this -> do_query ( $query );
2014-06-19 22:22:52 +00:00
if ( ! $result ) error_page ( " database error on query $query " );
2014-03-22 16:04:58 +00:00
if ( MYSQLI ) {
$x = $result -> fetch_object ( " StdClass " );
$result -> free ();
} else {
$x = mysql_fetch_object ( $result );
mysql_free_result ( $result );
}
2014-01-14 05:52:55 +00:00
if ( $x ) return ( double ) $x -> $field ;
return false ;
}
2014-03-21 08:08:47 +00:00
function count ( $table , $clause = " TRUE " ) {
2014-01-14 05:52:55 +00:00
$query = " select count(*) as total from DBNAME. $table where $clause " ;
return $this -> get_int ( $query , 'total' );
}
function sum ( $table , $field , $clause = " " ) {
$query = " select sum( $field ) as total from DBNAME. $table $clause " ;
return $this -> get_double ( $query , 'total' );
2011-06-21 22:56:15 +00:00
}
2012-05-25 18:44:56 +00:00
function max ( $table , $field , $clause = " " ) {
2014-01-14 05:52:55 +00:00
$query = " select max( $field ) as total from DBNAME. $table $clause " ;
return $this -> get_double ( $query , 'total' );
2012-05-25 18:44:56 +00:00
}
2013-12-20 23:03:24 +00:00
function percentile ( $table , $field , $clause , $pct ) {
$n = $this -> count ( $table , $clause );
if ( ! $n ) return false ;
$m = ( int )( $n * $pct / 100 );
2014-01-14 05:52:55 +00:00
$query = " select $field from DBNAME. $table where $clause order by $field limit $m ,1 " ;
return $this -> get_double ( $query , $field );
2013-12-20 23:03:24 +00:00
}
2007-11-10 00:32:42 +00:00
function replace ( $table , $clause ) {
2014-01-14 05:52:55 +00:00
$query = " replace into DBNAME. $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 ) {
2014-03-21 08:08:47 +00:00
if ( MYSQLI ) {
return $this -> db_conn -> escape_string ( $string );
} else {
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 () {
2014-03-21 08:08:47 +00:00
if ( MYSQLI ) {
return $this -> db_conn -> error ;
} else {
return mysql_error ( $this -> db_conn );
}
2007-11-12 22:28:17 +00:00
}
2015-12-07 14:43:13 +00:00
function base_errno () {
if ( MYSQLI ) {
return $this -> db_conn -> errno ;
} else {
return mysql_errno ( $this -> db_conn );
}
}
2008-06-30 23:05:16 +00:00
function table_exists ( $table_name ) {
2014-01-14 05:52:55 +00:00
$result = $this -> do_query ( " show tables from DBNAME like ' $table_name ' " );
2014-06-19 22:22:52 +00:00
if ( ! $result ) error_page ( " database error on query $query " );
2015-07-02 17:55:59 +00:00
$t = _mysql_fetch_array ( $result );
_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
}
?>