<?php

/**
 * Database access adapter for PEAR::DB library
 *
 * @note this class has not been tested with PostgreSQL RDBMS
 *
 * @version DR1.4.2
 */
class PEARDBAdapter extends DBAdapter {



	protected $connection;
	protected $dialect;
	protected $result;



	public function __construct(MDB2_Driver_Common $connection, SQLDialect $dialect) {
		$this->connection = $connection;
		$this->dialect = $dialect;
		$this->result = new ArrayObject();
	}



	public function execute($sql, $sequenceName = null) {
		$sql = trim($sql);
		if (preg_match('/^SELECT/i', $sql)) {
			$out = $this->connection->query($sql);
			if (PEAR::isError($out)) {
				$this->result = new ArrayObject();
			} else {
				$this->result = new ArrayObject($out->fetchAll(MDB2_FETCHMODE_ASSOC));
			}
			return true;
		}
		$out = $this->connection->query($sql);
		if (PEAR::isError($out)) {
			return false;
		}
		if (!$sequenceName or !preg_match('/^INSERT/i', $sql)) {
			return true;
		}
		return $this->connection->lastInsertID();
	}



	public function getIterator() {
		return new ArrayIterator($this->result);
	}



	public function getDialect() {
		return $this->dialect;
	}



} // class PEARDBAdapter

?>