Permalink
Browse files

PDO: Multiple results

  • Loading branch information...
1 parent d1e86ec commit 6b17789e1fa87f701377eaf3b8ee2026db01ff37 @cbandy committed Aug 17, 2010
Showing with 100 additions and 0 deletions.
  1. +44 −0 classes/database/pdo.php
  2. +56 −0 classes/database/pdo/result/iterator.php
View
@@ -268,6 +268,50 @@ public function execute_insert($statement)
return array($this->execute_command($statement), $this->_connection->lastInsertId());
}
+ /**
+ * Execute a SQL statement or compound statement with multiple results.
+ *
+ * Not all drivers support this method.
+ *
+ * @throws Database_Exception
+ * @param string $statement SQL statement(s)
+ * @param mixed $as_object Result object class, TRUE for stdClass, FALSE for associative array
+ * @return Database_PDO_Result_Iterator Forward-only iterator over the results
+ */
+ public function execute_multiple($statement, $as_object = FALSE)
+ {
+ if (empty($statement))
+ return NULL;
+
+ $this->_connection or $this->connect();
+
+ if ( ! empty($this->_config['profiling']))
+ {
+ $benchmark = Profiler::start("Database ($this->_instance)", $statement);
+ }
+
+ try
+ {
+ $statement = $this->_connection->query($statement);
+ }
+ catch (PDOException $e)
+ {
+ if (isset($benchmark))
+ {
+ Profiler::delete($benchmark);
+ }
+
+ throw new Database_Exception(':error', array(':error' => $e->getMessage()));
+ }
+
+ if (isset($benchmark))
+ {
+ Profiler::stop($benchmark);
+ }
+
+ return new Database_PDO_Result_Iterator($statement, $as_object);
+ }
+
public function execute_query($statement, $as_object = FALSE)
{
if (empty($statement))
@@ -0,0 +1,56 @@
+<?php
+
+/**
+ * @package RealDatabase
+ * @subpackage PDO
+ * @category Result Sets
+ *
+ * @author Chris Bandy
+ * @copyright (c) 2010 Chris Bandy
+ * @license http://www.opensource.org/licenses/isc-license.txt
+ */
+class Database_PDO_Result_Iterator extends Database_Result_Iterator
+{
+ /**
+ * @var mixed Result object class
+ */
+ protected $_as_object;
+
+ /**
+ * @var PDOStatement Executed statement
+ */
+ protected $_statement;
+
+ /**
+ * @param PDOStatement $statement Executed statement
+ * @param mixed $as_object Result object class, TRUE for stdClass, FALSE for associative array
+ */
+ public function __construct($statement, $as_object)
+ {
+ $this->_as_object = $as_object;
+ $this->_statement = $statement;
+ }
+
+ public function current()
+ {
+ if ($this->_statement->columnCount() === 0)
+ return $this->_statement->rowCount();
+
+ return new Database_PDO_Result($this->_statement, $this->_as_object);
+ }
+
+ public function next()
+ {
+ if ( ! $this->_statement->nextRowset())
+ {
+ $this->_statement = NULL;
+ }
+
+ return parent::next();
+ }
+
+ public function valid()
+ {
+ return ($this->_statement !== NULL);
+ }
+}

0 comments on commit 6b17789

Please sign in to comment.