Skip to content

Commit

Permalink
Add Query::valueBinder() getter
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Hoffmann committed Apr 20, 2017
1 parent 25e5dc5 commit 19310d7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Database/Connection.php
Expand Up @@ -309,7 +309,7 @@ public function compileQuery(Query $query, ValueBinder $generator)
public function run(Query $query)
{
$statement = $this->prepare($query);
$query->valueBinder()->attachTo($statement);
$query->getValueBinder()->attachTo($statement);
$statement->execute();

return $statement;
Expand Down
2 changes: 1 addition & 1 deletion src/Database/IdentifierQuoter.php
Expand Up @@ -52,7 +52,7 @@ public function __construct(Driver $driver)
*/
public function quote(Query $query)
{
$binder = $query->valueBinder();
$binder = $query->getValueBinder();
$query->valueBinder(false);

if ($query->type() === 'insert') {
Expand Down
27 changes: 23 additions & 4 deletions src/Database/Query.php
Expand Up @@ -275,7 +275,7 @@ public function rowCountAndClose()
public function sql(ValueBinder $generator = null)
{
if (!$generator) {
$generator = $this->valueBinder();
$generator = $this->getValueBinder();
$generator->resetCount();
}

Expand Down Expand Up @@ -1801,11 +1801,29 @@ public function traverseExpressions(callable $callback)
*/
public function bind($param, $value, $type = 'string')
{
$this->valueBinder()->bind($param, $value, $type);
$this->getValueBinder()->bind($param, $value, $type);

return $this;
}

/**
* Returns the currently used ValueBinder instance.
*
* A ValueBinder is responsible for generating query placeholders and temporarily
* associate values to those placeholders so that they can be passed correctly
* statement object.
*
* @return \Cake\Database\ValueBinder
*/
public function getValueBinder()
{
if ($this->_valueBinder === null) {
$this->_valueBinder = new ValueBinder();
}

return $this->_valueBinder;
}

/**
* Returns the currently used ValueBinder instance. If a value is passed,
* it will be set as the new instance to be used.
Expand All @@ -1814,6 +1832,7 @@ public function bind($param, $value, $type = 'string')
* associate values to those placeholders so that they can be passed correctly
* statement object.
*
* @deprecated 3.5.0 Use getValueBinder() for the getter part instead.
* @param \Cake\Database\ValueBinder|null $binder new instance to be set. If no value is passed the
* default one will be returned
* @return $this|\Cake\Database\ValueBinder
Expand Down Expand Up @@ -2004,7 +2023,7 @@ protected function _dirty()
$this->_dirty = true;

if ($this->_iterator && $this->_valueBinder) {
$this->valueBinder()->reset();
$this->getValueBinder()->reset();
}
}

Expand Down Expand Up @@ -2065,7 +2084,7 @@ public function __debugInfo()
throw new RuntimeException($errstr, $errno);
}, E_ALL);
$sql = $this->sql();
$params = $this->valueBinder()->bindings();
$params = $this->getValueBinder()->bindings();
} catch (RuntimeException $e) {
$sql = 'SQL could not be generated for this query as it is incomplete.';
$params = [];
Expand Down
4 changes: 2 additions & 2 deletions src/Database/QueryCompiler.php
Expand Up @@ -101,8 +101,8 @@ public function compile(Query $query, ValueBinder $generator)

// Propagate bound parameters from sub-queries if the
// placeholders can be found in the SQL statement.
if ($query->valueBinder() !== $generator) {
foreach ($query->valueBinder()->bindings() as $binding) {
if ($query->getValueBinder() !== $generator) {
foreach ($query->getValueBinder()->bindings() as $binding) {
$placeholder = ':' . $binding['placeholder'];
if (preg_match('/' . $placeholder . '(?:\W|$)/', $sql) > 0) {
$generator->bind($placeholder, $binding['value'], $binding['type']);
Expand Down
12 changes: 12 additions & 0 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -4329,6 +4329,18 @@ public function testSelectWithObjFetchType()
$this->assertEquals($obj, $rows[0]);
}

/**
* Test getValueBinder()
*
* @return void
*/
public function testGetValueBinder()
{
$query = new Query($this->connection);

$this->assertInstanceOf('\Cake\Database\ValueBinder', $query->getValueBinder());
}

/**
* Assertion for comparing a table's contents with what is in it.
*
Expand Down

0 comments on commit 19310d7

Please sign in to comment.