<?php

/**
 * Database access adapter for PDO library
 *
 * @version DR1.4.2
 */
class PDOAdapter extends DBAdapter {



	protected $pdo;
	protected $dialect;
	protected $result;



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



	public function execute($sql, $sequenceName = null) {
		$sql = trim($sql);
		if (preg_match('/^SELECT/i', $sql)) {
			$out = $this->pdo->query($sql);
			if ($out instanceof PDOStatement) {
				$this->result = new ArrayObject($out->fetchAll(PDO::FETCH_ASSOC));
				$out->closeCursor();
			} else {
				$this->result = new ArrayObject();
			}
			return true;
		}
		$out = $this->pdo->exec($sql);
		if ($out === false) {
			return false;
		}
		if (!$sequenceName or !preg_match('/^INSERT/i', $sql)) {
			return true;
		}
		return $this->pdo->lastInsertId($sequenceName);
	}



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



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



} // class PDOAdapter

?>