<?php
/**
*
* @author Benjamin Gillissen <hide@address.com>
*
* **************************************************************
Copyright (C) 2009 Benjamin Gillissen
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 at:
http://www.gnu.org/copyleft/gpl.html
* **************************************************************
*/
class db_mysql extends db_accessor {
private $phpext = 'mysql';
private $_HDL;
public function __construct($DBRef){
parent::__construct($DBRef);
}
public function connect(){
if ( isset($this->_HDL) ){
//if ( $this->_HDL !== FALSE ){ return TRUE;}
}
if ( !CORE::isphpModLoaded($this->phpext) ){
if ( ! CORE::LoadphpMod($this->phpext) ){
return errors::getnew('No mysql extention could be found or loaded on this system,'.
' plz contact the administrator and ask for mysql support in php or'.
' simply use an other vendor.', CORE_LOG_ERR, 'DB');
}
}
if ( !$conf = $this->getconf() ){
return errors::getnew('No configuration could be loaded for DBRef "'.$this->DBRef.'",'.
' plz check your db.conf.php file or load a valid db configuration with that reference.', CORE_LOG_ERR, 'DB');
}
//echo '<pre>';print_r($conf);echo'</pre>';
if ( isset($conf['hostspec']) ){
if ( isset($conf['port']) ){
//we happend port to host if set
$conf['hostspec'] .= ':'.$conf['port'];
}
if ( isset($conf['username']) ){
if ( !isset($conf['password']) ){
//host, user but no pass
$this->_HDL = @mysql_pconnect($conf['hostspec'], $conf['username']);
} else {
//echo 'host, user, pass';
$this->_HDL = @mysql_pconnect($conf['hostspec'], $conf['username'], $conf['password']);
}
} else {
//host only
$this->_HDL = @mysql_pconnect($conf['hostspec']);
}
} else {
//nothing we try login with default settings
$this->_HDL = @mysql_pconnect();
}
if ( !$this->_HDL ){
return errors::getnew('Connection could not be established with mysql database server with DBRef "'.$this->DBRef.'" :'.
'['.mysql_errno().'] '.mysql_error(), CORE_LOG_ERR, 'DB');
}
if ( isset($conf['database']) ){
if ( ! mysql_select_db($conf['database'], $this->_HDL) ){
return errors::getnew('Could not use the database "'.$conf['database'].'" using DBRef "'.$this->DBRef.'" :'.
'['.mysql_errno($this->_HDL).'] '.mysql_error($this->_HDL), CORE_LOG_ERR, 'DB');
}
}
errors::raise('Database Connection established to DBRef : '.$this->DBRef, CORE_LOG_DEBUG, 'DB');
return TRUE;
}
public function disconnect(){
if ( $this->isconnected() ){
return @mysql_close($this->_HDL);
}
return TRUE;
}
public function isconnected(){
if ( !CORE::isphpModLoaded($this->phpext) ){ return FALSE; }
return @mysql_ping($this->_HDL);
}
public function checkval($val){
return mysql_real_escape_string($val);
}
private function sendquery($q){
if ( empty($q) ){
return errors::getnew('SQL query is empty', CORE_LOG_WARNING, 'DB');
}
if ( !$this->isconnected() ){
$try = $this->connect();
if ( CORE::isError($try) ){
return errors::getnew('You are about to execute an SQL query on a handler that as not yet been connected, the trouble is that it was not able to connect to the server :
'.$try->getmsg(), CORE_LOG_WARNING, 'DB');
}
}
if ( !$result = mysql_query($q, $this->_HDL) ){
return errors::getnew('SQL query has returned an error : '.
'['.mysql_errno($this->_HDL).'] '.mysql_error($this->_HDL), CORE_LOG_ERR, 'DB');
}
return $result;
}
public function create($q){
$result = $this->sendquery($q);
unset($q);
if ( CORE::isError($result) ){
return $result;
}
return TRUE;
}
public function insert($q){
$result = $this->sendquery($q);
unset($q);
if ( CORE::isError($result) ){
return $result;
}
return TRUE;
}
public function update($q){
$result = $this->sendquery($q);
unset($q);
if ( CORE::isError($result) ){
return $result;
}
return TRUE;
}
public function delete($q){
$result = $this->sendquery($q);
unset($q);
if ( CORE::isError($result) ){
return $result;
}
return TRUE;
}
public function drop($q){
$result = $this->sendquery($q);
unset($q);
if ( CORE::isError($result) ){
return $result;
}
return TRUE;
}
public function getonefield($q){
$result = $this->sendquery($q);
unset($q);
if ( CORE::isError($result) ){
return $result;
}
$buf = mysql_fetch_array($result);
return $buf[0];
}
public function getonerow($q){
$result = $this->sendquery($q);
unset($q);
if ( CORE::isError($result) ){
return $result;
}
$r = mysql_fetch_assoc($result);
return $r;
}
public function fetchrow($q){
static $HDL;
$hash = CORE::hash($q);
if ( !isset($HDL[$hash]) ){
$HDL[$hash] = $this->sendquery($q);
unset($q);
if ( CORE::isError($HDL[$hash]) ){
$b=$HDL[$hash];
unset($HDL[$hash]);
return $b;
}
}
$row = mysql_fetch_assoc($HDL[$hash]);
if ( $row === FALSE ){
unset($HDL[$hash]);
return FALSE;
}
return $row;
}
public function table_exists($table){
$q = "SHOW TABLES";
$result = $this->sendquery($q);
while ($row=mysql_fetch_array($result, MYSQL_NUM)) {
if ($row[0] == $table){ return TRUE; }
}
return FALSE;
}
}