Browse Source

Add Query::valueBinder() getter

Michael Hoffmann 9 years ago
parent
commit
19310d7b4e

+ 1 - 1
src/Database/Connection.php

@@ -309,7 +309,7 @@ class Connection implements ConnectionInterface
     public function run(Query $query)
     {
         $statement = $this->prepare($query);
-        $query->valueBinder()->attachTo($statement);
+        $query->getValueBinder()->attachTo($statement);
         $statement->execute();
 
         return $statement;

+ 1 - 1
src/Database/IdentifierQuoter.php

@@ -52,7 +52,7 @@ class IdentifierQuoter
      */
     public function quote(Query $query)
     {
-        $binder = $query->valueBinder();
+        $binder = $query->getValueBinder();
         $query->valueBinder(false);
 
         if ($query->type() === 'insert') {

+ 23 - 4
src/Database/Query.php

@@ -275,7 +275,7 @@ class Query implements ExpressionInterface, IteratorAggregate
     public function sql(ValueBinder $generator = null)
     {
         if (!$generator) {
-            $generator = $this->valueBinder();
+            $generator = $this->getValueBinder();
             $generator->resetCount();
         }
 
@@ -1801,12 +1801,30 @@ class Query implements ExpressionInterface, IteratorAggregate
      */
     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 @@ class Query implements ExpressionInterface, IteratorAggregate
      * 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 @@ class Query implements ExpressionInterface, IteratorAggregate
         $this->_dirty = true;
 
         if ($this->_iterator && $this->_valueBinder) {
-            $this->valueBinder()->reset();
+            $this->getValueBinder()->reset();
         }
     }
 
@@ -2065,7 +2084,7 @@ class Query implements ExpressionInterface, IteratorAggregate
                 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 = [];

+ 2 - 2
src/Database/QueryCompiler.php

@@ -101,8 +101,8 @@ class QueryCompiler
 
         // 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']);

+ 12 - 0
tests/TestCase/Database/QueryTest.php

@@ -4330,6 +4330,18 @@ class QueryTest extends TestCase
     }
 
     /**
+     * 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.
      *
      * @param string $table