<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
//v1.3 www.bcoos.net 2006-08 GPL
//v1.2 www.exoops.info 2006/03 GPL
// ------------------------------------------------------------------------- //
//                XOOPS - PHP Content Management System                      //
//                       <http://www.xoops.org/>                             //
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  This program 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 General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
// ------------------------------------------------------------------------- //
// Author of File: 							     //
// Kazumi Ono (http://www.mywebaddons.com/ , http://www.myweb.ne.jp/)        //
// ------------------------------------------------------------------------- //
if ( !defined("SQL_LAYER") ) {
	define("SQL_LAYER", "mysql");
include_once(XOOPS_ROOT_PATH."/class/database.php");
class Database extends AbsDatabase {
	var $conn;
	var $query_log;
function connect($host, $user, $password, $db, $persistent=0) {
	
	if ( $persistent ) { $this->conn = @mysql_pconnect($host, $user, $password);
	} else { $this->conn = @mysql_connect($host, $user, $password); }
	
	if ( !$this->conn ) {
		return false;
		//print("<b>Error Connecting DB</b>: ".$this->error($this->conn).":".$this->errno($this->conn));
	}
		
	$selDB = mysql_select_db($db,$this->conn);
		
	if ( !$selDB ) {
		return false;
		//print("<b>Error Selecting DB</b>: ".$this->errno($this->conn).":".$this->error($this->conn));
	}
	return $this->conn;
}
function close() {
	return mysql_close($this->conn);
}
function query($sql, $limit=0, $start=0) {
if ( ($this->debug & 8) || ($this->debug & 16) ) {
	$this->query_log[] = $sql;
} else {
	$this->query_log[] = 0;
}
if ( !empty($limit) ) {
	if (empty($start)) { $start = 0; }
	$sql = $sql. " LIMIT ".intval($start).",".intval($limit)."";
}
if ($this->debug & 16) {
	if ( function_exists('xdebug_call_function') ) {
		if ( function_exists('xdebug_call_class') ) {
			if ($caller_class = xdebug_call_class()) {
				$output = "<b>".$caller_class." ::</b> ";
	}
		}
		$output .= "<b>".xdebug_call_function()."()</b><br />";
	}
	$output .= count($this->query_log).": ".$sql;
	$this->bg = ($this->bg == 'bg3') ? $this->bg = 'bg1' : $this->bg = 'bg3';
	echo "<div align='left' style='border-top:1px dashed #003030; padding: 3px;' class='".$this->bg."'>".$output."</div>";
}
$result = mysql_query($sql, $this->conn);
return $result;
}
function genId($sequence) {
	return 0; // will use auto_increment
}
function getRowsNum($resource) {
$numrows = mysql_num_rows($resource);
return $numrows;
}
function fetchArray($resource) {
$array = mysql_fetch_array($resource);
return $array;
}
function fetch_object($resource) {
$object = mysql_fetch_object($resource);
return $object;
}
function fetchRow($resource) {
$row = mysql_fetch_row($resource);
return $row;
}
function result($resource,$offset) {
$result = mysql_result($resource,$offset);
return $result;
}
function getInsertId() {
$lastidused = mysql_insert_id($this->conn);
return $lastidused;
}
function getNextId($table,$idfield) {
global $db,$eh;
	$sql = "SELECT ".$idfield." FROM `".$db->prefix($table)."` ORDER BY ".$idfield." DESC LIMIT 1";
	$queryid = $db->query($sql) or $eh->show("0013");
	if ($db->getRowsNum($queryid) == 0 ) { 
		$newid = 1;
		return $newid;
	}
	else {
		$newid =  $db->result($queryid,0);
		$newid++;
		return $newid;
	}
}
		
function error() {
if ($this->debug & 1) {
	$errorstring = mysql_errno($this->conn) . ": " . mysql_error($this->conn);
	return $errorstring;
}
}
} 
} 
?>
