<?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_fbsql extends db_accessor {
private $phpext = 'fbsql';
private $_HDL;
public function __construct($DBRef){
parent::__construct($DBRef);
}
public function connect(){
if ( !CORE::isphpModLoaded($this->phpext) ){
if ( ! CORE::LoadphpMod($this->phpext) ){
return errors::getnew('No FrontBase extention could be found or loaded on this system,
plz contact the administrator and ask for fbsql 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');
}
if ( isset($conf['host']) ){
if ( isset($conf['port']) ){
//we happend port to host if set
$conf['host'] .= ':'.$conf['port'];
}
if ( isset($conf['user']) ){
if ( !isset($conf['pass']) ){
//host, user but no pass
$this->_HDL = @fbsql_pconnect($conf['host'], $conf['user']);
} else {
//host, user, pass
$this->_HDL = @fbsql_pconnect($conf['host'], $conf['user'], $conf['pass']);
}
} else {
//host only
$this->_HDL = @fbsql_pconnect($conf['host']);
}
} else {
//nothing we try login with default settings
$this->_HDL = @fbsql_pconnect();
}
if ( !$this->_HDL ){
return errors::getnew('Connection could not be established with mysql database server with DBRef "'.$this->DBRef.'"', CORE_LOG_ERR, 'DB');
}
if ( isset($conf['database']) ){
if ( ! fbsql_select_db($conf['database'], $this->_HDL) ){
return errors::getnew('Could not use the database "'.$conf['database'].'" using DBRef "'.$this->DBRef.'" :
['.fbsql_errno($this->_HDL).'] '.fbsql_error($this->_HDL), CORE_LOG_ERR, 'DB');
}
}
return TRUE;
}
protected function disconnect(){
if ( $this->isconnected() ){
return @fbsql_close($this->_HDL);
}
return TRUE;
}
protected function isconnected(){
if ( !$this->checkphpext($this->phpext) ){ return FALSE; }
return @mysql_ping($this->_HDL);
}
protected 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 = @fbsql_query($this->_HDL, $q) ){
return errors::getnew('SQL query "'.$q.'" has returned an error :
['.fbsql_errno($this->_HDL).'] '.fbsql_error($this->_HDL), CORE_LOG_ERR, 'DB');
}
return $result;
}
protected function insert($q){
$result = $this->sendquery($q);
unset($q);
if ( CORE::isError($result) ){
return $result;
}
return TRUE;
}
protected function update($q){
$result = $this->sendquery($q);
unset($q);
if ( CORE::isError($result) ){
return $result;
}
return TRUE;
}
protected function delete($q){
$result = $this->sendquery($q);
unset($q);
if ( CORE::isError($result) ){
return $result;
}
return TRUE;
}
protected function getone($q){
$result = $this->sendquery($q);
unset($q);
if ( CORE::isError($result) ){
return $result;
}
//dunno what to return need to print
}
protected function getall($q){
$result = $this->sendquery($q);
unset($q);
if ( CORE::isError($result) ){
return $result;
}
//dunno what to return need to print
}
protected function fetch($q){
//one static by,
static $bool=TRUE;
static $result;
if ( $bool ){
$result = $this->sendquery($q);
unset($q);
if ( CORE::isError($result) ){
return $result;
}
}
$bool = FALSE;
$row = @fbsql_fetch_array($result);
if ( $row === FALSE ){
$bool = TRUE;
unset($result);
return FALSE;
}
return $row;
}
}