Browse Source

Refactor TableSchema construction to a method in Driver class, #13689

Val Bancer 6 years ago
parent
commit
8512c0fc9d

+ 2 - 0
.gitignore

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

+ 13 - 0
src/Database/Driver.php

@@ -15,6 +15,7 @@
 namespace Cake\Database;
 
 use Cake\Database\Query;
+use Cake\Database\Schema\TableSchema;
 use Cake\Database\Statement\PDOStatement;
 use InvalidArgumentException;
 use PDO;
@@ -427,6 +428,18 @@ 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\TableSchema
+     */
+    public function newTableSchema($table, $columns = [])
+    {
+        return new TableSchema($table, $columns);
+    }
+
+    /**
      * Destructor
      */
     public function __destruct()

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

@@ -93,7 +93,7 @@ class Collection
         if (strpos($name, '.')) {
             list($config['schema'], $name) = explode('.', $name);
         }
-        $table = new TableSchema($name);
+        $table = $this->_connection->getDriver()->newTableSchema($name);
 
         $this->_reflect('Column', $name, $config, $table);
         if (count($table->columns()) === 0) {

+ 6 - 1
src/ORM/Table.php

@@ -20,6 +20,7 @@ use Cake\Core\App;
 use Cake\Database\Schema\TableSchema;
 use Cake\Database\Type;
 use Cake\Datasource\ConnectionInterface;
+use Cake\Datasource\ConnectionManager;
 use Cake\Datasource\EntityInterface;
 use Cake\Datasource\Exception\InvalidPrimaryKeyException;
 use Cake\Datasource\RepositoryInterface;
@@ -583,7 +584,11 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
                 unset($schema['_constraints']);
             }
 
-            $schema = new TableSchema($this->getTable(), $schema);
+            $connection = $this->getConnection();
+            if ($connection === null) {
+                $connection = ConnectionManager::get(static::defaultConnectionName());
+            }
+            $schema = $connection->getDriver()->newTableSchema($this->getTable(), $schema);
 
             foreach ($constraints as $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()
     {
         $connection = ConnectionManager::get($this->connection());
-        $this->_schema = new TableSchema($this->table);
+        $this->_schema = $connection->getDriver()->newTableSchema($this->table);
         foreach ($this->fields as $field => $data) {
             if ($field === '_constraints' || $field === '_indexes' || $field === '_options') {
                 continue;

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

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