/*
* $Id: dbi_mysql.php,v 1.5 2001/04/03 14:48:31 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 {
// Unauthorized
var $Driver_name = "mySQL";
var $Name;
var $session;
var $debug = 0;
var $err;
var $errstr="";
var $RaiseError = 1;
function DBI ($db, $user, $password) {
$this->session = mysql_pconnect('localhost', $user, $password);
mysql_select_db($db,$this->session);
$this->Name = $db;
}
function prepare ($query) {
return new STH($this, $this->session, $query, $this->debug);
}
function dbh_do($query) {
// --OBS-- returns -1 on failure, 0 is a valid return from this function!
$res = mysql_query($query, $this-> session);
if ($res) {
$this->err = 0;
$this->errstr = "";
return mysql_affected_rows($this->session);
} else {
$this->err = mysql_errno($this->session);
$this->errstr = mysql_error($this->session);
return -1;
}
}
function insert_id() {
return mysql_insert_id($this->session);
}
function autocommit ($value) {
echo "
Warning: Transactions not supported by this driver.
";
return 0;
}
function commit () {
echo "
Warning: Transactions not supported by this driver.
";
return 0;
}
function rollback () {
echo "
Warning: Transactions not supported by this driver.
";
return 0;
}
function quote ($str) {
if (get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return "'".AddSlashes($str)."'";
}
}
class STH {
var $query;
var $debug;
var $session;
var $res;
var $row;
var $placeholders;
var $dbi;
function STH (&$dbi, $session, $query, $debug) {
$this->dbi = &$dbi;
$this->session = $session;
$this->query = $query;
$this->debug = $debug;
// Scan for placeholders
$this->placeholders = array();
$quote = '';
for ($i = 0; $i < strlen($query); ++$i) {
if ($query[$i] == "'") {
if (empty($quote)) {
$quote = "'";
} elseif ($quote == "'") {
$quote = '';
}
} elseif ($query[$i] == '"') {
if (empty($quote)) {
$quote = '"';
} elseif ($quote == '"') {
$quote = '';
}
} elseif ($query[$i] == '?') {
if (empty($quote)) {
array_push($this->placeholders, $i);
}
}
}
}
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 (sizeof($parms) != sizeof($this->placeholders)) {
print "
SQL Query contains ".sizeof($this->placeholders)." placeholders but ".sizeof($parms)." was passed
";
exit;
}
if (sizeof($parms) > 0) {
$query = substr($this->query, 0, $this->placeholders[0]);
for ($i = 0; $i < sizeof($parms) - 1; ++$i) {
$query .= $this->dbi->quote($parms[$i]) . substr($this->query, $this->placeholders[$i] + 1, $this->placeholders[$i + 1] - $this->placeholders[$i] - 1);
}
$query .= $this->dbi->quote($parms[$i]) . substr($this->query, $this->placeholders[$i] + 1);
} else {
$query = $this->query;
}
if ($this->debug) { // Log the query
$fd = fopen("/tmp/dbi.$SERVER_NAME.log", "a") or die ("Couldn't append to file");
fputs($fd, $this->query."\n==================\n");
fclose($fd);
}
$this->res = @mysql_query($query, $this->session);
if (!$this->res) {
if ($this->dbi->RaiseError) {
print "
Could not execute SQL query: \" ". $query . "\"
";
print "
".mysql_errno($this->session) . " " .mysql_error($this->session)."
";
exit;
}
$this->dbi->errstr = mysql_error($this->session);
$this->dbi->err = mysql_errno($this->session);
}
$this->row = 0;
return $this->res;
}
function fetchrow_array () {
return mysql_fetch_row($this->res);
$this->row++;
}
function fetchrow_hash () {
return mysql_fetch_array($this->res);
$this->row++;
}
function finish () {
mysql_free_result($this->res);
}
function rows () {
return mysql_num_rows($this->res);
}
}
?>