Commit 6489854c authored by Laurent's avatar Laurent
Browse files

Allow ZF to not create prepared statements when not needed

See http://framework.zend.com/issues/browse/ZF-1398
parent bd12b3a7
......@@ -131,6 +131,12 @@ abstract class Zend_Db_Adapter_Abstract
Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE
);
/**
* @var boolean Defines whether the adapter uses prepared statemtents.
* The default is 'true' to avoid a BC-break.
*/
protected $_usePreparedStatements = true;
/**
* Constructor.
*
......@@ -406,7 +412,7 @@ abstract class Zend_Db_Adapter_Abstract
* @param mixed $sql The SQL statement with placeholders.
* May be a string or Zend_Db_Select.
* @param mixed $bind An array of data to bind to the placeholders.
* @return Zend_Db_Statement_Interface
* @return mixed Zend_Db_Statement_Interface or query result.
*/
public function query($sql, $bind = array())
{
......@@ -425,13 +431,21 @@ abstract class Zend_Db_Adapter_Abstract
$bind = array($bind);
}
// prepare and execute the statement with profiling
$stmt = $this->prepare($sql);
$stmt->execute($bind);
if ($this->_usePreparedStatements || !empty($bind)) {
// prepare and execute the statement with profiling
$stmt = $this->prepare($sql);
$stmt->execute($bind);
// return the results embedded in the prepared statement object
$stmt->setFetchMode($this->_fetchMode);
return $stmt;
}
// return the results embedded in the prepared statement object
$stmt->setFetchMode($this->_fetchMode);
return $stmt;
return new Zend_Db_Statement_Mysqli_Unprepared(
$this->getConnection()->query($sql),
$this->_fetchMode
);
}
/**
......@@ -1021,6 +1035,24 @@ abstract class Zend_Db_Adapter_Abstract
return $value;
}
/**
* Enable or disable prepared statements here.
*
* @param bool $use 'true' or 'false'.
*
* @return self
* @uses self::$_usePreparedStatements
*/
public function usePrepared($use)
{
if ($use === true) {
$this->_usePreparedStatements = $use;
return $this;
}
$this->_usePreparedStatements = false;
return $this;
}
/**
* Abstract Methods
*/
......
<?php
/**
* @see Zend_Db_Statement
*/
//require_once 'Zend/Db/Statement.php';
class Zend_Db_Statement_Mysqli_Unprepared extends Zend_Db_Statement
{
protected $_fetchMode, $_result;
public function __construct($result, $fetchMode)
{
$this->_result = $result;
$this->_fetchMode = $fetchMode;
}
public function fetchAll($fetchMode = null, $col = null)
{
$data = array();
while ($row = $this->fetch($fetchMode)) {
array_push($data, $row);
}
return $data;
}
public function fetchColumn($col = 0)
{
$row = $this->fetch(Zend_Db::FETCH_NUM);
if (isset($row[$col])) {
return $row[$col];
}
return false; // FIXME
}
public function fetch($fetchMode = null, $cursor = null, $offset = null)
{
if ($fetchMode === null) {
$fetchMode = $this->_fetchMode;
}
switch ($fetchMode) {
default:
case Zend_Db::FETCH_BOTH:
$fetchMode = MYSQLI_BOTH;
break;
case Zend_Db::FETCH_ASSOC:
$fetchMode = MYSQLI_ASSOC;
break;
case Zend_Db::FETCH_NUM:
$fetchMode = MYSQLI_NUM;
break;
}
return $this->_result->fetch_array($fetchMode);
}
public function __call($method, $args)
{
die("$method");
}
public function closeCursor() {
return true;
}
public function columnCount() {
return 0;
}
public function errorCode() {
return '';
}
public function errorInfo() {
return '';
}
/**
* Retrieves the next rowset (result set) for a SQL statement that has
* multiple result sets. An example is a stored procedure that returns
* the results of multiple queries.
*
* @return bool
* @throws Zend_Db_Statement_Mysqli_Exception
*/
public function nextRowset()
{
/**
* @see Zend_Db_Statement_Mysqli_Exception
*/
require_once 'Zend/Db/Statement/Mysqli/Exception.php';
throw new Zend_Db_Statement_Mysqli_Exception(__FUNCTION__.'() is not implemented');
}
/**
* Returns the number of rows affected by the execution of the
* last INSERT, DELETE, or UPDATE statement executed by this
* statement object.
*
* @return int The number of rows affected.
*/
public function rowCount()
{
if (!$this->_adapter) {
return false;
}
$mysqli = $this->_adapter->getConnection();
return $mysqli->affected_rows;
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment