/*
* $Id: dbi_odbc.php,v 1.2 2001/05/18 12:37:22 cfsl Exp $
*
* This class is based on the Perl DBI.
* New functionality should be added accordingly.
* Please refer to the DBI documentation (perldoc DBI).
*
* 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
*/
class DBI {
var $session;
var $debug = 1;
var $Driver_name = "odbc";
function DBI ($db, $user, $password, $debug = 0) {
$this->session = odbc_pconnect($db, $user, $password);
$this->debug = $debug;
}
function prepare ($query) {
return new STH($this, $this->session, $query, $this->debug);
}
function autocommit ($value) {
return odbc_autocommit ($this->session, $value);
}
function commit () {
return odbc_commit($this->session);
}
function rollback () {
return odbc_rollback($this->session);
}
function quote ($str) {
if (get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return "'".str_replace ("'", "''", $str)."'";
}
function insert_id ($sequence) {
$sth = new STH($this, $this->session, "SELECT $sequence.CURRVAL FROM DUAL", $this->mode, $this->debug);
$sth->execute();
list($res) = $sth->fetchrow_array();
return $res;
}
}
class STH {
var $query;
var $statement;
var $debug;
var $dbi;
var $placeholders;
function STH (&$dbi, &$session, $query, $debug) {
$this->dbi = &$dbi;
$this->query = $query;
$this->debug = $debug;
$this->session = &$session;
$this->statement = odbc_prepare($this->session, $this->query);
return $this->statement;
}
function execute () {
global $SERVER_NAME;
$numargs = func_num_args();
$arg_list = func_get_args();
$parms = array();
for ($i = 0; $i < $numargs; $i++) {
if (is_array($arg_list[$i])) {
while (list($dummy,$parm) = each ($arg_list[$i])) {
array_push($parms, $parm);
}
} else {
array_push($parms,$arg_list[$i]);
}
}
if ($this->debug) { // Log the query
$fd = fopen("/tmp/dbi.$SERVER_NAME.log", "a") or die ("Couldn't append to file");
fputs($fd, date("M d H:i:s",time()).": ".$query."\nParameters: ".join(',',$parms)."\n==================\n");
fclose($fd);
}
if (!odbc_execute($this->statement, $parms)) {
print "
Could not execute SQL query: \"".$query."\"
";
exit;
}
}
function fetchrow_array () {
$res = array();
odbc_fetch_into($this->statement, $res);
return $res;
}
function fetchrow_hash () {
$res = array();
odbc_fetch_into($this->statement, $res);
$res2 = array();
for ($i = 0; $i < sizeof($res); ++$i) {
$res2[odbc_field_name($this->statement, $i + 1)] = $res[$i];
}
return $res2;
}
function rows () {
return odbc_num_rows ($this->statement);
}
function finish () {
odbc_free_result($this->statement);
}
}
?>