<?php
/*========================================================*\
||########################################################||
||# #||
||# WB News v2.0.0 #||
||# ---------------------------------------------------- #||
||# Copyright (c) 2004-2007 #||
||# Created: 22nd Dec 2006 #||
||# Filename: DBase.php #||
||# #||
||########################################################||
/*========================================================*/
/**
* @author $Author: pmcilwaine $
* @version $Id: DBase.php,v 1.1.2.2.2.1 2008/07/14 11:08:07 pmcilwaine Exp $
*/
class DBase extends DB_Sql
{
/**
* Setup database activity
*
* @param String $dbhost
* @param String $dbuser
* @param String $dbpass
* @param String $dbname
* @param String $dbport
*/
function DBase( $dbhost, $dbuser, $dbpass, $dbname, $dbport=NULL )
{
if ( !parent::DB_Sql( $dbhost, $dbuser, $dbpass, $dbname, $dbport ) )
{
// we need to kill whats happening, perhaps send something to
// webmaster
}
}
/**
*
* TODO fix param list
*
* @param String $table
* @param String $cond
* @param String $order
* @param int $limit
* @param int $offset
* @param String $groupby
* @return array
*/
function ListBy( $table, $cond=NULL, $fields=array("id"), $order="", $limit="", $offset = 0, $groupby="" )
{
$table = is_array($table) ? join( ", ", $table ) : $table;
$fieldsql = is_array( $fields ) ? join(", ", $fields ) : $fields ;
$sql = "SELECT $fieldsql FROM $table";
if ( NULL != $cond )
{
$sql .= " WHERE $cond";
}
if ( $groupby )
{
$sql .=" GROUP BY $groupby";
}
if ( $order )
{
$sql .= " ORDER BY $order";
}
if ( $limit )
{
$limit = intval($limit);
$sql .= " LIMIT $limit";
}
if ( $offset )
{
$offset = intval($offset);
$sql .= " OFFSET $offset";
}
if ( !$this->query( $sql ) )
{
return FALSE;
}
$row = 0;
$ret = array();
while ( $this->next_record() )
{
foreach ( $this->current_record as $key => $value )
{
$ret[$row][$key] = $value;
}
$row++;
}
return $ret;
}
/**
* Method different from ListBy because we can join tables
*
* TODO fix comments + anything that used old param list
*
* @param Array $table allow multiple tables in FROM clause
* @param Array $joins the actual joins 3 valid types
* leftjoin => array()
* rightjoin => array()
* join => array()
* @param String $cond
* @param String $order
* @param int $limit
* @param int $offset
* @param String $groupby
* @return array
*/
function ListByJoin( $table = array(), $fields=array("id"), $cond="", $joins = array(), $order="", $limit="", $offset = 0, $groupby="" )
{
$table = is_array( $table ) ? join( ", ", $table ) : $table;
$fieldsql = is_array( $fields ) ? join(", ", $fields ) : $fields;
$sql = "SELECT $fieldsql FROM $table";
if ( array_key_exists( "leftjoin", $joins ) && is_array($joins["leftjoin"]) )
{
foreach ( $joins["leftjoin"] as $join )
{
$sql .= " LEFT JOIN $join";
}
}
if ( array_key_exists( "rightjoin", $joins ) && is_array($joins["rightjoin"]) )
{
foreach ( $joins["rightjoin"] as $join )
{
$sql .= " RIGHT JOIN $join";
}
}
if ( array_key_exists( "join", $joins ) && is_array($joins["join"]) )
{
foreach ( $joins["join"] as $join )
{
$sql .= " JOIN $join";
}
}
if ( $cond )
{
$sql .= " WHERE $cond";
}
if ( $groupby )
{
$sql .=" GROUP BY $groupby";
}
if ( $order )
{
$sql .= " ORDER BY $order";
}
if ( $limit )
{
$limit = intval($limit);
$sql .= " LIMIT $limit";
}
if ( $offset )
{
$offset = intval($offset);
$sql .= " OFFSET $offset";
}
if ( !$this->query( $sql ) )
{
return FALSE;
}
$row = 0;
$ret = array();
while ( $this->next_record() )
{
foreach ( $this->current_record as $key => $value )
{
$ret[$row][$key] = $value;
}
$row++;
}
return $ret;
}
/**
*
* @param mixed $table
* @param String $cond
* @return int
*/
function CountBy( $table, $cond=NULL, $joins=NULL, $alias=NULL, $id="id" )
{
$formtables = is_array( $table ) ? join( ", ", $table ) : $table;
$alias = NULL != $alias ? "$alias." : NULL;
$id = addslashes( $id );
$sql = "SELECT COUNT(" . $alias . "$id) as total FROM $formtables";
if ( isset($joins["leftjoin"]) )
{
foreach ( $joins["leftjoin"] as $join )
{
$sql .= " LEFT JOIN $join";
}
}
if ( isset($joins["rightjoin"]) )
{
foreach ( $joins["rightjoin"] as $join )
{
$sql .= " RIGHT JOIN $join";
}
}
if ( isset($joins["join"]) )
{
foreach ( $joins["join"] as $join )
{
$sql .= " JOIN $join";
}
}
if ( $cond )
{
$sql .= " WHERE $cond";
}
if ( !$this->query( $sql ) )
{
return 0;
}
if ( $this->next_record() )
{
return $this->field( "total" );
}
return 0;
}
/**
* Begin a transaction.
*
* @return bool
*/
function BeginTransaction()
{
return $this->query( "BEGIN" );
}
/**
* Commit or Rollback a transaction
*
* @param bool $commit If true commit data if false rollback as if nothing happened
* @return bool
*/
function EndTransaction( $commit=TRUE )
{
if ( $commit )
{
return $this->query( "COMMIT" );
}
else
{
return $this->query( "ROLLBACK" );
}
}
}
return;
?>