<?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_odbc extends db_accessor {
private $phpext = 'odbc';
private $isconn = FALSE;
private $_HDL;
public function __construct($DBRef){
parent::__construct($DBRef);
}
protected function connect(){
if ( !$this->checkphpext($this->phpext) ){
return errors::getnew('No odbc extention could be found or loaded on this system,
plz contact the administrator and ask for odbc 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['dsn']) ){
return errors::getnew('Configuration for DBRef "'.$this->DBRef.'" is incomplete,
plz set the "dsn" key in your conf for this DBRef.', CORE_LOG_ERR, 'DB');
} elseif ( !isset($conf['user']) ){
return errors::getnew('Configuration for DBRef "'.$this->DBRef.'" is incomplete,
plz set the "user" key in your conf for this DBRef.', CORE_LOG_ERR, 'DB');
} elseif ( !isset($conf['pass']) ){
return errors::getnew('Configuration for DBRef "'.$this->DBRef.'" is incomplete,
plz set the "pass" key in your conf for this DBRef.', CORE_LOG_ERR, 'DB');
} elseif ( !isset($conf['cursor_type']) ){
$conf['cursor_type'] = SQL_CUR_USE_DRIVER;
}
$this->_HDL = @odbc_pconnect($conf['dsn'], $conf['user'], $conf['pass'], $conf['cursor_type']);
if ( !$this->_HDL ){
return errors::getnew('Connection could not be established with odbc database server with DBRef "'.$this->DBRef.'"', CORE_LOG_ERR, 'DB');
}
$this->isconn = TRUE;
return TRUE;
}
protected function disconnect(){
if ( $this->isconnected() ){
$this->isconn = FALSE;
return @odbc_close($this->_HDL);
}
return TRUE;
}
protected function isconnected(){
return $this->isconn;
}
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 = @odbc_exec($this->_HDL, $q) ){
return errors::getnew('SQL query "'.$q.'" has returned an error :
['.odbc_error($this->_HDL).'] '.odbc_errormsg($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){
static $bool=TRUE;
static $result;
if ( $bool ){
$result = $this->sendquery($q);
unset($q);
if ( CORE::isError($result) ){
return $result;
}
}
$bool = FALSE;
$row = @odbc_fetch_array($result);
if ( $row === FALSE ){
$bool = TRUE;
unset($result);
return FALSE;
}
return $row;
}
}