ソースを参照

Merge pull request #13696 from bancer/table-schema-3.x

Refactor TableSchema construction to a method in Driver class
Mark Story 6 年 前
コミット
aa9e2c0e90

+ 2 - 0
.gitignore

@@ -37,6 +37,8 @@ Thumbs.db
 *.tmPreferences.cache
 *.tmPreferences.cache
 # Eclipse
 # Eclipse
 .settings/*
 .settings/*
+/.project
+/.buildpath
 # JetBrains, aka PHPStorm, IntelliJ IDEA
 # JetBrains, aka PHPStorm, IntelliJ IDEA
 .idea/*
 .idea/*
 # NetBeans
 # NetBeans

+ 18 - 0
src/Database/Driver.php

@@ -15,6 +15,7 @@
 namespace Cake\Database;
 namespace Cake\Database;
 
 
 use Cake\Database\Query;
 use Cake\Database\Query;
+use Cake\Database\Schema\TableSchema;
 use Cake\Database\Statement\PDOStatement;
 use Cake\Database\Statement\PDOStatement;
 use InvalidArgumentException;
 use InvalidArgumentException;
 use PDO;
 use PDO;
@@ -427,6 +428,23 @@ abstract class Driver implements DriverInterface
     }
     }
 
 
     /**
     /**
+     * Constructs new TableSchema.
+     *
+     * @param string $table The table name.
+     * @param array $columns The list of columns for the schema.
+     * @return \Cake\Database\Schema\TableSchemaInterface
+     */
+    public function newTableSchema($table, array $columns = [])
+    {
+        $className = TableSchema::class;
+        if (isset($this->_config['tableSchema'])) {
+            $className = $this->_config['tableSchema'];
+        }
+
+        return new $className($table, $columns);
+    }
+
+    /**
      * Destructor
      * Destructor
      */
      */
     public function __destruct()
     public function __destruct()

+ 1 - 0
src/Database/DriverInterface.php

@@ -20,6 +20,7 @@ use Cake\Database\Query;
  * Interface for database driver.
  * Interface for database driver.
  *
  *
  * @method $this disableAutoQuoting()
  * @method $this disableAutoQuoting()
+ * @method \Cake\Database\Schema\TableSchemaInterface newTableSchema()
  */
  */
 interface DriverInterface
 interface DriverInterface
 {
 {

+ 2 - 2
src/Database/Schema/Collection.php

@@ -93,7 +93,7 @@ class Collection
         if (strpos($name, '.')) {
         if (strpos($name, '.')) {
             list($config['schema'], $name) = explode('.', $name);
             list($config['schema'], $name) = explode('.', $name);
         }
         }
-        $table = new TableSchema($name);
+        $table = $this->_connection->getDriver()->newTableSchema($name);
 
 
         $this->_reflect('Column', $name, $config, $table);
         $this->_reflect('Column', $name, $config, $table);
         if (count($table->columns()) === 0) {
         if (count($table->columns()) === 0) {
@@ -113,7 +113,7 @@ class Collection
      * @param string $stage The stage name.
      * @param string $stage The stage name.
      * @param string $name The table name.
      * @param string $name The table name.
      * @param array $config The config data.
      * @param array $config The config data.
-     * @param \Cake\Database\Schema\TableSchema $schema The table instance
+     * @param \Cake\Database\Schema\TableSchemaInterface $schema The table schema instance.
      * @return void
      * @return void
      * @throws \Cake\Database\Exception on query failure.
      * @throws \Cake\Database\Exception on query failure.
      */
      */

+ 1 - 0
src/Datasource/ConnectionInterface.php

@@ -18,6 +18,7 @@ namespace Cake\Datasource;
  * This interface defines the methods you can depend on in
  * This interface defines the methods you can depend on in
  * a connection.
  * a connection.
  *
  *
+ * @method object getDriver() Gets the driver instance.
  * @method object getLogger() Get the current logger instance
  * @method object getLogger() Get the current logger instance
  * @method $this setLogger($logger) Set the current logger.
  * @method $this setLogger($logger) Set the current logger.
  * @method bool supportsDynamicConstraints()
  * @method bool supportsDynamicConstraints()

+ 6 - 1
src/ORM/Table.php

@@ -20,6 +20,7 @@ use Cake\Core\App;
 use Cake\Database\Schema\TableSchema;
 use Cake\Database\Schema\TableSchema;
 use Cake\Database\Type;
 use Cake\Database\Type;
 use Cake\Datasource\ConnectionInterface;
 use Cake\Datasource\ConnectionInterface;
+use Cake\Datasource\ConnectionManager;
 use Cake\Datasource\EntityInterface;
 use Cake\Datasource\EntityInterface;
 use Cake\Datasource\Exception\InvalidPrimaryKeyException;
 use Cake\Datasource\Exception\InvalidPrimaryKeyException;
 use Cake\Datasource\RepositoryInterface;
 use Cake\Datasource\RepositoryInterface;
@@ -523,6 +524,10 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
      */
      */
     public function getConnection()
     public function getConnection()
     {
     {
+        if (!$this->_connection) {
+            $this->_connection = ConnectionManager::get(static::defaultConnectionName());
+        }
+
         return $this->_connection;
         return $this->_connection;
     }
     }
 
 
@@ -583,7 +588,7 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
                 unset($schema['_constraints']);
                 unset($schema['_constraints']);
             }
             }
 
 
-            $schema = new TableSchema($this->getTable(), $schema);
+            $schema = $this->getConnection()->getDriver()->newTableSchema($this->getTable(), $schema);
 
 
             foreach ($constraints as $name => $value) {
             foreach ($constraints as $name => $value) {
                 $schema->addConstraint($name, $value);
                 $schema->addConstraint($name, $value);

+ 1 - 1
src/TestSuite/Fixture/TestFixture.php

@@ -179,7 +179,7 @@ class TestFixture implements FixtureInterface, TableSchemaInterface, TableSchema
     protected function _schemaFromFields()
     protected function _schemaFromFields()
     {
     {
         $connection = ConnectionManager::get($this->connection());
         $connection = ConnectionManager::get($this->connection());
-        $this->_schema = new TableSchema($this->table);
+        $this->_schema = $connection->getDriver()->newTableSchema($this->table);
         foreach ($this->fields as $field => $data) {
         foreach ($this->fields as $field => $data) {
             if ($field === '_constraints' || $field === '_indexes' || $field === '_options') {
             if ($field === '_constraints' || $field === '_indexes' || $field === '_options') {
                 continue;
                 continue;

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

@@ -18,6 +18,7 @@ use Cake\Database\Driver;
 use Cake\Database\Driver\Mysql;
 use Cake\Database\Driver\Mysql;
 use Cake\Database\Query;
 use Cake\Database\Query;
 use Cake\Database\QueryCompiler;
 use Cake\Database\QueryCompiler;
+use Cake\Database\Schema\TableSchema;
 use Cake\Database\ValueBinder;
 use Cake\Database\ValueBinder;
 use Cake\TestSuite\TestCase;
 use Cake\TestSuite\TestCase;
 use PDO;
 use PDO;
@@ -268,6 +269,19 @@ class DriverTest extends TestCase
     }
     }
 
 
     /**
     /**
+     * Test newTableSchema().
+     *
+     * @return void
+     */
+    public function testNewTableSchema()
+    {
+        $tableName = 'articles';
+        $actual = $this->driver->newTableSchema($tableName);
+        $this->assertInstanceOf(TableSchema::class, $actual);
+        $this->assertEquals($tableName, $actual->name());
+    }
+
+    /**
      * Test __destruct().
      * Test __destruct().
      *
      *
      * @return void
      * @return void

+ 0 - 2
tests/TestCase/ORM/TableTest.php

@@ -248,7 +248,6 @@ class TableTest extends TestCase
     {
     {
         $this->deprecated(function () {
         $this->deprecated(function () {
             $table = new Table(['table' => 'users']);
             $table = new Table(['table' => 'users']);
-            $this->assertNull($table->connection());
             $table->connection($this->connection);
             $table->connection($this->connection);
             $this->assertSame($this->connection, $table->connection());
             $this->assertSame($this->connection, $table->connection());
         });
         });
@@ -262,7 +261,6 @@ class TableTest extends TestCase
     public function testSetConnection()
     public function testSetConnection()
     {
     {
         $table = new Table(['table' => 'users']);
         $table = new Table(['table' => 'users']);
-        $this->assertNull($table->getConnection());
         $this->assertSame($table, $table->setConnection($this->connection));
         $this->assertSame($table, $table->setConnection($this->connection));
         $this->assertSame($this->connection, $table->getConnection());
         $this->assertSame($this->connection, $table->getConnection());
     }
     }