From 19310d7b4ea35fb6ad0108ff45223a544fb6aeb7 Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Thu, 20 Apr 2017 20:06:01 +0200 Subject: [PATCH] Add Query::valueBinder() getter --- src/Database/Connection.php | 2 +- src/Database/IdentifierQuoter.php | 2 +- src/Database/Query.php | 27 +++++++++++++++++++++++---- src/Database/QueryCompiler.php | 4 ++-- tests/TestCase/Database/QueryTest.php | 12 ++++++++++++ 5 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/Database/Connection.php b/src/Database/Connection.php index 3dc67d84722..dfd3b34d088 100644 --- a/src/Database/Connection.php +++ b/src/Database/Connection.php @@ -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; diff --git a/src/Database/IdentifierQuoter.php b/src/Database/IdentifierQuoter.php index d79c45e6182..9ba205ff4b8 100644 --- a/src/Database/IdentifierQuoter.php +++ b/src/Database/IdentifierQuoter.php @@ -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') { diff --git a/src/Database/Query.php b/src/Database/Query.php index f4b10d26291..e4513a2cf4c 100644 --- a/src/Database/Query.php +++ b/src/Database/Query.php @@ -275,7 +275,7 @@ public function rowCountAndClose() public function sql(ValueBinder $generator = null) { if (!$generator) { - $generator = $this->valueBinder(); + $generator = $this->getValueBinder(); $generator->resetCount(); } @@ -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. @@ -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 @@ -2004,7 +2023,7 @@ protected function _dirty() $this->_dirty = true; if ($this->_iterator && $this->_valueBinder) { - $this->valueBinder()->reset(); + $this->getValueBinder()->reset(); } } @@ -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 = []; diff --git a/src/Database/QueryCompiler.php b/src/Database/QueryCompiler.php index 7b8d4e84bb0..c39e431d9e1 100644 --- a/src/Database/QueryCompiler.php +++ b/src/Database/QueryCompiler.php @@ -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']); diff --git a/tests/TestCase/Database/QueryTest.php b/tests/TestCase/Database/QueryTest.php index afd38fa2110..2c11a0e29ce 100644 --- a/tests/TestCase/Database/QueryTest.php +++ b/tests/TestCase/Database/QueryTest.php @@ -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. *