Skip to content

Commit

Permalink
Merge PHLAK:hotfix/gravatar-trim-strtolower into develop
Browse files Browse the repository at this point in the history
Pull request zendframework#4976
  • Loading branch information
EvanDotPro committed Aug 16, 2013
2 parents 3138073 + 8af59fc commit c21cd36
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 19 deletions.
94 changes: 75 additions & 19 deletions library/Zend/Db/Adapter/Driver/Sqlsrv/Statement.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Zend Framework (http://framework.zend.com/)
*
Expand Down Expand Up @@ -63,6 +64,12 @@ class Statement implements StatementInterface, Profiler\ProfilerAwareInterface
*/
protected $isPrepared = false;

/**
*
* @var array
*/
protected $options = array();

/**
* Set driver
*
Expand Down Expand Up @@ -181,7 +188,7 @@ public function getSql()
}

/**
* @param string $sql
* @param string $sql
* @throws Exception\RuntimeException
* @return Statement
*/
Expand All @@ -193,13 +200,11 @@ public function prepare($sql = null)
$sql = ($sql) ?: $this->sql;

$pRef = &$this->parameterReferences;
for ($position = 0, $count = substr_count($sql, '?'); $position < $count; $position++) {
$pRef[$position] = array('', SQLSRV_PARAM_IN, null, null);
}

$this->resource = sqlsrv_prepare($this->sqlsrv, $sql, $pRef);
$this->resource = sqlsrv_prepare($this->sqlsrv, $sql, $pRef, $this->options);

$this->isPrepared = true;

return $this;
}

Expand All @@ -220,9 +225,6 @@ public function isPrepared()
*/
public function execute($parameters = null)
{
if (!$this->isPrepared) {
$this->prepare();
}

/** START Standard ParameterContainer Merging Block */
if (!$this->parameterContainer instanceof ParameterContainer) {
Expand All @@ -242,6 +244,9 @@ public function execute($parameters = null)
$this->bindParametersFromContainer();
}
/** END Standard ParameterContainer Merging Block */
if (!$this->isPrepared) {
$this->prepare();
}

if ($this->profiler) {
$this->profiler->profilerStart($this);
Expand Down Expand Up @@ -271,19 +276,70 @@ public function execute($parameters = null)
*/
protected function bindParametersFromContainer()
{
$values = $this->parameterContainer->getPositionalArray();
$parameters = $this->parameterContainer->getNamedArray();

$position = 0;
foreach ($values as $value) {
$this->parameterReferences[$position++][0] = $value;
foreach ($parameters as $key => &$value) {
if ($this->parameterContainer->offsetHasErrata($key)) {
$errata = $this->parameterContainer->offsetGetErrata($key);
switch ($errata) {
case ParameterContainer::TYPE_BINARY:
$params = array();
$params[] = $value;
$params[] = \SQLSRV_PARAM_IN;
$params[] = \SQLSRV_PHPTYPE_STREAM(\SQLSRV_ENC_BINARY);
$params[] = \SQLSRV_SQLTYPE_VARBINARY('max');
$this->parameterReferences[$position++] = $params;
break;
default:
$params = array($value, \SQLSRV_PARAM_IN, null, null);
$this->parameterReferences[$position++] = $params;
}
} elseif (is_array($value)) {
$this->parameterReferences[$position++] = $value;
} else {
$params = array($value, \SQLSRV_PARAM_IN, null, null);
$this->parameterReferences[$position++] = $params;
}
}
}

// @todo bind errata
//foreach ($this->parameterContainer as $name => &$value) {
// $p[$position][0] = $value;
// $position++;
// if ($this->parameterContainer->offsetHasErrata($name)) {
// $p[$position][3] = $this->parameterContainer->offsetGetErrata($name);
// }
//}
public function setQueryTimeout($queryTimeout)
{
if (is_int($queryTimeout)) {
$this->options['QueryTimeout'] = $queryTimeout;
} else {
$message = 'Invalid argument provided to ';
$message.= __METHOD__ . ' method in class ' . __CLASS__;
throw new Exception\InvalidArgumentException($message);
}
}

public function setSendStreamParamsAtExec($sendStreamParamsAtExec)
{
if (is_bool($sendStreamParamsAtExec)) {
$this->options['SendStreamParamsAtExec'] = $sendStreamParamsAtExec;
} else {
$message = 'Invalid argument provided to ';
$message.= __METHOD__ . ' method in class ' . __CLASS__;
throw new Exception\InvalidArgumentException($message);
}
}

public function setScrollable($scrollable)
{
switch ($scrollable) {
case \SQLSRV_CURSOR_FORWARD:
case \SQLSRV_CURSOR_STATIC:
case \SQLSRV_CURSOR_DYNAMIC:
case \SQLSRV_CURSOR_KEYSET:
$this->options['Scrollable'] = $scrollable;
break;
default:
$message = 'Invalid argument provided to ';
$message.= __METHOD__ . ' method in class ' . __CLASS__;
throw new Exception\InvalidArgumentException($message);
}
}

}
4 changes: 4 additions & 0 deletions library/Zend/Db/TableGateway/AbstractTableGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ protected function executeSelect(Select $select)

// prepare and execute
$statement = $this->sql->prepareStatementForSqlObject($select);
$this->featureSet->apply('preSelectExecute', array($statement));
$result = $statement->execute();

// build result set
Expand Down Expand Up @@ -288,6 +289,7 @@ protected function executeInsert(Insert $insert)
$this->featureSet->apply('preInsert', array($insert));

$statement = $this->sql->prepareStatementForSqlObject($insert);
$this->featureSet->apply('preInsertExecute', array($statement));
$result = $statement->execute();
$this->lastInsertValue = $this->adapter->getDriver()->getConnection()->getLastGeneratedValue();

Expand Down Expand Up @@ -348,6 +350,7 @@ protected function executeUpdate(Update $update)
$this->featureSet->apply('preUpdate', array($update));

$statement = $this->sql->prepareStatementForSqlObject($update);
$this->featureSet->apply('preUpdateExecute', array($statement));
$result = $statement->execute();

// apply postUpdate features
Expand Down Expand Up @@ -404,6 +407,7 @@ protected function executeDelete(Delete $delete)
$this->featureSet->apply('preDelete', array($delete));

$statement = $this->sql->prepareStatementForSqlObject($delete);
$this->featureSet->apply('preDeleteExecute', array($statement));
$result = $statement->execute();

// apply postDelete features
Expand Down
27 changes: 27 additions & 0 deletions library/Zend/Db/TableGateway/Feature/StatementOptionsFeature.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Zend\Db\Table\Feature;

use Zend\Db\Adapter\Driver\StatementInterface;

class StatementOptionsFeature extends AbstractFeature
{

public function preSelectExecute(StatementInterface $statement)
{
$adapter = $this->tableGateway->getAdapter();
$platform = $adapter->getPlatform();
$platformName = $platform->getName();

switch ($platformName) {
case "SQLServer":
//Necessary for ResultSet count to work
$statement->setScrollable(\SQLSRV_CURSOR_STATIC);
break;
}
}

// public function preInsertExecute(StatementInterface $statement){}
// public function preUpdateExecute(StatementInterface $statement){}
// public function preDeleteExecute(StatementInterface $statement){}
}

0 comments on commit c21cd36

Please sign in to comment.