Skip to content

Commit 19310d7

Browse files
author
Michael Hoffmann
committed
Add Query::valueBinder() getter
1 parent 25e5dc5 commit 19310d7

File tree

5 files changed

+39
-8
lines changed

5 files changed

+39
-8
lines changed

src/Database/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public function compileQuery(Query $query, ValueBinder $generator)
309309
public function run(Query $query)
310310
{
311311
$statement = $this->prepare($query);
312-
$query->valueBinder()->attachTo($statement);
312+
$query->getValueBinder()->attachTo($statement);
313313
$statement->execute();
314314

315315
return $statement;

src/Database/IdentifierQuoter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __construct(Driver $driver)
5252
*/
5353
public function quote(Query $query)
5454
{
55-
$binder = $query->valueBinder();
55+
$binder = $query->getValueBinder();
5656
$query->valueBinder(false);
5757

5858
if ($query->type() === 'insert') {

src/Database/Query.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public function rowCountAndClose()
275275
public function sql(ValueBinder $generator = null)
276276
{
277277
if (!$generator) {
278-
$generator = $this->valueBinder();
278+
$generator = $this->getValueBinder();
279279
$generator->resetCount();
280280
}
281281

@@ -1801,11 +1801,29 @@ public function traverseExpressions(callable $callback)
18011801
*/
18021802
public function bind($param, $value, $type = 'string')
18031803
{
1804-
$this->valueBinder()->bind($param, $value, $type);
1804+
$this->getValueBinder()->bind($param, $value, $type);
18051805

18061806
return $this;
18071807
}
18081808

1809+
/**
1810+
* Returns the currently used ValueBinder instance.
1811+
*
1812+
* A ValueBinder is responsible for generating query placeholders and temporarily
1813+
* associate values to those placeholders so that they can be passed correctly
1814+
* statement object.
1815+
*
1816+
* @return \Cake\Database\ValueBinder
1817+
*/
1818+
public function getValueBinder()
1819+
{
1820+
if ($this->_valueBinder === null) {
1821+
$this->_valueBinder = new ValueBinder();
1822+
}
1823+
1824+
return $this->_valueBinder;
1825+
}
1826+
18091827
/**
18101828
* Returns the currently used ValueBinder instance. If a value is passed,
18111829
* it will be set as the new instance to be used.
@@ -1814,6 +1832,7 @@ public function bind($param, $value, $type = 'string')
18141832
* associate values to those placeholders so that they can be passed correctly
18151833
* statement object.
18161834
*
1835+
* @deprecated 3.5.0 Use getValueBinder() for the getter part instead.
18171836
* @param \Cake\Database\ValueBinder|null $binder new instance to be set. If no value is passed the
18181837
* default one will be returned
18191838
* @return $this|\Cake\Database\ValueBinder
@@ -2004,7 +2023,7 @@ protected function _dirty()
20042023
$this->_dirty = true;
20052024

20062025
if ($this->_iterator && $this->_valueBinder) {
2007-
$this->valueBinder()->reset();
2026+
$this->getValueBinder()->reset();
20082027
}
20092028
}
20102029

@@ -2065,7 +2084,7 @@ public function __debugInfo()
20652084
throw new RuntimeException($errstr, $errno);
20662085
}, E_ALL);
20672086
$sql = $this->sql();
2068-
$params = $this->valueBinder()->bindings();
2087+
$params = $this->getValueBinder()->bindings();
20692088
} catch (RuntimeException $e) {
20702089
$sql = 'SQL could not be generated for this query as it is incomplete.';
20712090
$params = [];

src/Database/QueryCompiler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ public function compile(Query $query, ValueBinder $generator)
101101

102102
// Propagate bound parameters from sub-queries if the
103103
// placeholders can be found in the SQL statement.
104-
if ($query->valueBinder() !== $generator) {
105-
foreach ($query->valueBinder()->bindings() as $binding) {
104+
if ($query->getValueBinder() !== $generator) {
105+
foreach ($query->getValueBinder()->bindings() as $binding) {
106106
$placeholder = ':' . $binding['placeholder'];
107107
if (preg_match('/' . $placeholder . '(?:\W|$)/', $sql) > 0) {
108108
$generator->bind($placeholder, $binding['value'], $binding['type']);

tests/TestCase/Database/QueryTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4329,6 +4329,18 @@ public function testSelectWithObjFetchType()
43294329
$this->assertEquals($obj, $rows[0]);
43304330
}
43314331

4332+
/**
4333+
* Test getValueBinder()
4334+
*
4335+
* @return void
4336+
*/
4337+
public function testGetValueBinder()
4338+
{
4339+
$query = new Query($this->connection);
4340+
4341+
$this->assertInstanceOf('\Cake\Database\ValueBinder', $query->getValueBinder());
4342+
}
4343+
43324344
/**
43334345
* Assertion for comparing a table's contents with what is in it.
43344346
*

0 commit comments

Comments
 (0)