Browse Source

4.5 - Add SelectQuery shim

This completes the backwards compatibility shim query classes for
Database package.
Mark Story 3 years ago
parent
commit
a1caab37fe

+ 26 - 0
src/Database/Connection.php

@@ -28,6 +28,7 @@ use Cake\Database\Log\LoggingStatement;
 use Cake\Database\Log\QueryLogger;
 use Cake\Database\Query\DeleteQuery;
 use Cake\Database\Query\InsertQuery;
+use Cake\Database\Query\SelectQuery;
 use Cake\Database\Query\UpdateQuery;
 use Cake\Database\Retry\ReconnectStrategy;
 use Cake\Database\Schema\CachedCollection;
@@ -377,6 +378,31 @@ class Connection implements ConnectionInterface
     }
 
     /**
+     * Create a new SelectQuery instance for this connection.
+     *
+     * @param \Cake\Database\ExpressionInterface|\Closure|array|string|float|int $fields Fields/columns list for the query.
+     * @param array|string $table The table or list of tables to query.
+     * @param array<string, string> $types Associative array containing the types to be used for casting.
+     * @return \Cake\Database\Query\SelectQuery
+     */
+    public function selectQuery(
+        $fields = [],
+        array|string $table = [],
+        array $types = []
+    ): SelectQuery {
+        $query = new SelectQuery($this);
+        if ($table) {
+            $query->from($table);
+        }
+        if ($fields) {
+            $query->select($fields, false, $types);
+        }
+        $query->setDefaultTypes($types);
+
+        return $query;
+    }
+
+    /**
      * Executes a SQL statement and returns the Statement object as result.
      *
      * @param string $sql The SQL query to execute.

+ 92 - 0
src/Database/Query/SelectQuery.php

@@ -0,0 +1,92 @@
+<?php
+declare(strict_types=1);
+
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         4.5.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Database\Query;
+
+use Cake\Database\Query;
+
+/**
+ * Select Query forward compatibility shim.
+ */
+class SelectQuery extends Query
+{
+    /**
+     * Type of this query (select, insert, update, delete).
+     *
+     * @var string
+     */
+    protected $_type = 'select';
+
+    /**
+     * @inheritDoc
+     */
+    public function delete(?string $table = null)
+    {
+        $this->_deprecatedMethod('delete()', 'Create your query with deleteQuery() instead.');
+
+        return parent::delete($table);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public function insert(array $columns, array $types = [])
+    {
+        $this->_deprecatedMethod('insert()', 'Create your query with insertQuery() instead.');
+
+        return parent::insert($columns, $types);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public function into(string $table)
+    {
+        $this->_deprecatedMethod('into()', 'Use from() instead.');
+
+        return parent::into($table);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public function values($data)
+    {
+        $this->_deprecatedMethod('values()');
+
+        return parent::values($data);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public function update($table = null)
+    {
+        $this->_deprecatedMethod('update()', 'Create your query with updateQuery() instead.');
+
+        return parent::update($table);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public function set($key, $value = null, $types = [])
+    {
+        $this->_deprecatedMethod('set()');
+
+        return parent::set($key, $value, $types);
+    }
+}

+ 14 - 0
tests/TestCase/Database/ConnectionTest.php

@@ -582,6 +582,20 @@ class ConnectionTest extends TestCase
     }
 
     /**
+     * Test basic selectQuery behavior
+     */
+    public function testSelectQuery(): void
+    {
+        $query = $this->connection->selectQuery(['*'], 'things');
+        $statement = $query->execute();
+        $row = $statement->fetchAssoc();
+        $statement->closeCursor();
+
+        $this->assertArrayHasKey('title', $row);
+        $this->assertArrayHasKey('body', $row);
+    }
+
+    /**
      * Tests that it is possible to use simple database transactions
      */
     public function testSimpleTransactions(): void

+ 2 - 2
tests/TestCase/Database/QueryTests/ForwardsCompatibilityTest.php

@@ -19,6 +19,7 @@ namespace Cake\Test\TestCase\Database\QueryTests;
 use Cake\Database\Connection;
 use Cake\Database\Query\DeleteQuery;
 use Cake\Database\Query\InsertQuery;
+use Cake\Database\Query\SelectQuery;
 use Cake\Database\Query\UpdateQuery;
 use Cake\Datasource\ConnectionManager;
 use Cake\TestSuite\TestCase;
@@ -35,6 +36,7 @@ class ForwardsCompatibilityTest extends TestCase
         return [
             [fn (Connection $connection) => new DeleteQuery($connection), 'delete'],
             [fn (Connection $connection) => new InsertQuery($connection), 'insert'],
+            [fn (Connection $connection) => new SelectQuery($connection), 'select'],
             [fn (Connection $connection) => new UpdateQuery($connection), 'update'],
         ];
     }
@@ -118,11 +120,9 @@ class ForwardsCompatibilityTest extends TestCase
             $this->assertEquals(1, $statement->rowCount());
             $statement->closeCursor();
         };
-        /*
         if ($query instanceof SelectQuery) {
             return $scenario();
         }
-        */
         $this->deprecated($scenario);
     }
 }