Browse Source

Moved database related methods to the new
Cake\Database\Schema\TableSchemaInterface.

Robert Pustułka 8 years ago
parent
commit
034dbfb208

+ 30 - 176
src/Database/Schema/TableSchema.php

@@ -17,7 +17,6 @@ namespace Cake\Database\Schema;
 use Cake\Database\Connection;
 use Cake\Database\Exception;
 use Cake\Database\Type;
-use Cake\Datasource\SchemaInterface;
 
 /**
  * Represents a single table in a database schema.
@@ -30,7 +29,7 @@ use Cake\Datasource\SchemaInterface;
  * Schema\Collection objects. They can also be converted into SQL using the
  * createSql(), dropSql() and truncateSql() methods.
  */
-class TableSchema implements SchemaInterface, SqlGeneratorInterface
+class TableSchema implements TableSchemaInterface, SqlGeneratorInterface
 {
 
     /**
@@ -299,9 +298,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Get the name of the table.
-     *
-     * @return string
+     * {@inheritDoc}
      */
     public function name()
     {
@@ -309,32 +306,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Add a column to the table.
-     *
-     * ### Attributes
-     *
-     * Columns can have several attributes:
-     *
-     * - `type` The type of the column. This should be
-     *   one of CakePHP's abstract types.
-     * - `length` The length of the column.
-     * - `precision` The number of decimal places to store
-     *   for float and decimal types.
-     * - `default` The default value of the column.
-     * - `null` Whether or not the column can hold nulls.
-     * - `fixed` Whether or not the column is a fixed length column.
-     *   This is only present/valid with string columns.
-     * - `unsigned` Whether or not the column is an unsigned column.
-     *   This is only present/valid for integer, decimal, float columns.
-     *
-     * In addition to the above keys, the following keys are
-     * implemented in some database dialects, but not all:
-     *
-     * - `comment` The comment for the column.
-     *
-     * @param string $name The name of the column
-     * @param array $attrs The attributes for the column.
-     * @return $this
+     * {@inheritDoc}
      */
     public function addColumn($name, $attrs)
     {
@@ -353,12 +325,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Remove a column from the table schema.
-     *
-     * If the column is not defined in the table, no error will be raised.
-     *
-     * @param string $name The name of the column
-     * @return $this
+     * {@inheritDoc}
      */
     public function removeColumn($name)
     {
@@ -368,9 +335,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Get the column names in the table.
-     *
-     * @return array
+     * {@inheritDoc}
      */
     public function columns()
     {
@@ -390,10 +355,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Get column data in the table.
-     *
-     * @param string $name The column name.
-     * @return array|null Column data or null.
+     * {@inheritDoc}
      */
     public function getColumn($name)
     {
@@ -425,10 +387,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Returns column type or null if a column does not exist.
-     *
-     * @param string $name The column to get the type of.
-     * @return string|null
+     * {@inheritDoc}
      */
     public function getColumnType($name)
     {
@@ -440,11 +399,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Sets the type of a column.
-     *
-     * @param string $name The column to set the type of.
-     * @param string $type The type to set the column to.
-     * @return $this
+     * {@inheritDoc}
      */
     public function setColumnType($name, $type)
     {
@@ -459,10 +414,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Returns true if a column exists in the schema.
-     *
-     * @param string $name Column name.
-     * @return bool
+     * {@inheritDoc}
      */
     public function hasColumn($name)
     {
@@ -470,12 +422,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Returns the base type name for the provided column.
-     * This represent the database type a more complex class is
-     * based upon.
-     *
-     * @param string $column The column name to get the base type from
-     * @return string|null The base type name
+     * {@inheritDoc}
      */
     public function baseColumnType($column)
     {
@@ -497,10 +444,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Returns an array where the keys are the column names in the schema
-     * and the values the database type they have.
-     *
-     * @return array
+     * {@inheritDoc}
      */
     public function typeMap()
     {
@@ -508,12 +452,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Check whether or not a field is nullable
-     *
-     * Missing columns are nullable.
-     *
-     * @param string $name The column to get the type of.
-     * @return bool Whether or not the field is nullable.
+     * {@inheritDoc}
      */
     public function isNullable($name)
     {
@@ -525,9 +464,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Get a hash of columns and their default values.
-     *
-     * @return array
+     * {@inheritDoc}
      */
     public function defaultValues()
     {
@@ -546,19 +483,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Add an index.
-     *
-     * Used to add indexes, and full text indexes in platforms that support
-     * them.
-     *
-     * ### Attributes
-     *
-     * - `type` The type of index being added.
-     * - `columns` The columns in the index.
-     *
-     * @param string $name The name of the index.
-     * @param array $attrs The attributes for the index.
-     * @return $this
+     * {@inheritDoc}
      * @throws \Cake\Database\Exception
      */
     public function addIndex($name, $attrs)
@@ -595,9 +520,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Get the names of all the indexes in the table.
-     *
-     * @return array
+     * {@inheritDoc}
      */
     public function indexes()
     {
@@ -617,10 +540,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Read information about an index based on name.
-     *
-     * @param string $name The name of the index.
-     * @return array|null Array of index data, or null
+     * {@inheritDoc}
      */
     public function getIndex($name)
     {
@@ -632,10 +552,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Get the column(s) used for the primary key.
-     *
-     * @return array Column name(s) for the primary key. An
-     *   empty list will be returned when the table has no primary key.
+     * {@inheritDoc}
      */
     public function primaryKey()
     {
@@ -649,24 +566,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Add a constraint.
-     *
-     * Used to add constraints to a table. For example primary keys, unique
-     * keys and foreign keys.
-     *
-     * ### Attributes
-     *
-     * - `type` The type of constraint being added.
-     * - `columns` The columns in the index.
-     * - `references` The table, column a foreign key references.
-     * - `update` The behavior on update. Options are 'restrict', 'setNull', 'cascade', 'noAction'.
-     * - `delete` The behavior on delete. Options are 'restrict', 'setNull', 'cascade', 'noAction'.
-     *
-     * The default for 'update' & 'delete' is 'cascade'.
-     *
-     * @param string $name The name of the constraint.
-     * @param array $attrs The attributes for the constraint.
-     * @return $this
+     * {@inheritDoc}
      * @throws \Cake\Database\Exception
      */
     public function addConstraint($name, $attrs)
@@ -723,10 +623,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Remove a constraint.
-     *
-     * @param string $name Name of the constraint to remove
-     * @return $this
+     * {@inheritDoc}
      */
     public function dropConstraint($name)
     {
@@ -776,9 +673,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Get the names of all the constraints in the table.
-     *
-     * @return array
+     * {@inheritDoc}
      */
     public function constraints()
     {
@@ -798,10 +693,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Read information about a constraint based on name.
-     *
-     * @param string $name The name of the constraint.
-     * @return array|null Array of constraint data, or null
+     * {@inheritDoc}
      */
     public function getConstraint($name)
     {
@@ -813,13 +705,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Sets the options for a table.
-     *
-     * Table options allow you to set platform specific table level options.
-     * For example the engine type in MySQL.
-     *
-     * @param array $options The options to set, or null to read options.
-     * @return $this
+     * {@inheritDoc}
      */
     public function setOptions($options)
     {
@@ -829,12 +715,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Gets the options for a table.
-     *
-     * Table options allow you to set platform specific table level options.
-     * For example the engine type in MySQL.
-     *
-     * @return array An array of options.
+     * {@inheritDoc}
      */
     public function getOptions()
     {
@@ -861,10 +742,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Sets whether the table is temporary in the database.
-     *
-     * @param bool $temporary Whether or not the table is to be temporary.
-     * @return $this
+     * {@inheritDoc}
      */
     public function setTemporary($temporary)
     {
@@ -874,9 +752,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Gets whether the table is temporary in the database.
-     *
-     * @return bool The current temporary setting.
+     * {@inheritDoc}
      */
     public function isTemporary()
     {
@@ -900,14 +776,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Generate the SQL to create the Table.
-     *
-     * Uses the connection to access the schema dialect
-     * to generate platform specific SQL.
-     *
-     * @param \Cake\Database\Connection $connection The connection to generate SQL for.
-     * @return array List of SQL statements to create the table and the
-     *    required indexes.
+     * {@inheritDoc}
      */
     public function createSql(Connection $connection)
     {
@@ -927,13 +796,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Generate the SQL to drop a table.
-     *
-     * Uses the connection to access the schema dialect to generate platform
-     * specific SQL.
-     *
-     * @param \Cake\Database\Connection $connection The connection to generate SQL for.
-     * @return array SQL to drop a table.
+     * {@inheritDoc}
      */
     public function dropSql(Connection $connection)
     {
@@ -943,10 +806,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Generate the SQL statements to truncate a table
-     *
-     * @param \Cake\Database\Connection $connection The connection to generate SQL for.
-     * @return array SQL to truncate a table.
+     * {@inheritDoc}
      */
     public function truncateSql(Connection $connection)
     {
@@ -956,10 +816,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Generate the SQL statements to add the constraints to the table
-     *
-     * @param \Cake\Database\Connection $connection The connection to generate SQL for.
-     * @return array SQL to add the constraints.
+     * {@inheritDoc}
      */
     public function addConstraintSql(Connection $connection)
     {
@@ -969,10 +826,7 @@ class TableSchema implements SchemaInterface, SqlGeneratorInterface
     }
 
     /**
-     * Generate the SQL statements to drop the constraints to the table
-     *
-     * @param \Cake\Database\Connection $connection The connection to generate SQL for.
-     * @return array SQL to drop a table.
+     * {@inheritDoc}
      */
     public function dropConstraintSql(Connection $connection)
     {

+ 244 - 0
src/Database/Schema/TableSchemaInterface.php

@@ -0,0 +1,244 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.5.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Database\Schema;
+
+use Cake\Datasource\SchemaInterface;
+
+/**
+ *
+ * @author Robert Pustułka <r.pustulka@robotusers.com>
+ */
+interface TableSchemaInterface extends SchemaInterface
+{
+
+    /**
+     * Binary column type
+     *
+     * @var string
+     */
+    const TYPE_BINARY = 'binary';
+
+    /**
+     * Date column type
+     *
+     * @var string
+     */
+    const TYPE_DATE = 'date';
+
+    /**
+     * Datetime column type
+     *
+     * @var string
+     */
+    const TYPE_DATETIME = 'datetime';
+
+    /**
+     * Time column type
+     *
+     * @var string
+     */
+    const TYPE_TIME = 'time';
+
+    /**
+     * Timestamp column type
+     *
+     * @var string
+     */
+    const TYPE_TIMESTAMP = 'timestamp';
+
+    /**
+     * JSON column type
+     *
+     * @var string
+     */
+    const TYPE_JSON = 'json';
+
+    /**
+     * String column type
+     *
+     * @var string
+     */
+    const TYPE_STRING = 'string';
+
+    /**
+     * Text column type
+     *
+     * @var string
+     */
+    const TYPE_TEXT = 'text';
+
+    /**
+     * Tiny Integer column type
+     *
+     * @var string
+     */
+    const TYPE_TINYINTEGER = 'tinyinteger';
+
+    /**
+     * Small Integer column type
+     *
+     * @var string
+     */
+    const TYPE_SMALLINTEGER = 'smallinteger';
+
+    /**
+     * Integer column type
+     *
+     * @var string
+     */
+    const TYPE_INTEGER = 'integer';
+
+    /**
+     * Big Integer column type
+     *
+     * @var string
+     */
+    const TYPE_BIGINTEGER = 'biginteger';
+
+    /**
+     * Float column type
+     *
+     * @var string
+     */
+    const TYPE_FLOAT = 'float';
+
+    /**
+     * Decimal column type
+     *
+     * @var string
+     */
+    const TYPE_DECIMAL = 'decimal';
+
+    /**
+     * Boolean column type
+     *
+     * @var string
+     */
+    const TYPE_BOOLEAN = 'boolean';
+
+    /**
+     * UUID column type
+     *
+     * @var string
+     */
+    const TYPE_UUID = 'uuid';
+
+    /**
+     * Check whether or not a table has an autoIncrement column defined.
+     *
+     * @return bool
+     */
+    public function hasAutoincrement();
+
+    /**
+     * Sets whether the table is temporary in the database.
+     *
+     * @param bool $temporary Whether or not the table is to be temporary.
+     * @return $this
+     */
+    public function setTemporary($temporary);
+
+    /**
+     * Gets whether the table is temporary in the database.
+     *
+     * @return bool The current temporary setting.
+     */
+    public function isTemporary();
+
+    /**
+     * Get the column(s) used for the primary key.
+     *
+     * @return array Column name(s) for the primary key. An
+     *   empty list will be returned when the table has no primary key.
+     */
+    public function primaryKey();
+
+    /**
+     * Add an index.
+     *
+     * Used to add indexes, and full text indexes in platforms that support
+     * them.
+     *
+     * ### Attributes
+     *
+     * - `type` The type of index being added.
+     * - `columns` The columns in the index.
+     *
+     * @param string $name The name of the index.
+     * @param array $attrs The attributes for the index.
+     * @return $this
+     */
+    public function addIndex($name, $attrs);
+
+    /**
+     * Read information about an index based on name.
+     *
+     * @param string $name The name of the index.
+     * @return array|null Array of index data, or null
+     */
+    public function getIndex($name);
+
+    /**
+     * Get the names of all the indexes in the table.
+     *
+     * @return array
+     */
+    public function indexes();
+
+    /**
+     * Add a constraint.
+     *
+     * Used to add constraints to a table. For example primary keys, unique
+     * keys and foreign keys.
+     *
+     * ### Attributes
+     *
+     * - `type` The type of constraint being added.
+     * - `columns` The columns in the index.
+     * - `references` The table, column a foreign key references.
+     * - `update` The behavior on update. Options are 'restrict', 'setNull', 'cascade', 'noAction'.
+     * - `delete` The behavior on delete. Options are 'restrict', 'setNull', 'cascade', 'noAction'.
+     *
+     * The default for 'update' & 'delete' is 'cascade'.
+     *
+     * @param string $name The name of the constraint.
+     * @param array $attrs The attributes for the constraint.
+     * @return $this
+     */
+    public function addConstraint($name, $attrs);
+
+    /**
+     * Read information about a constraint based on name.
+     *
+     * @param string $name The name of the constraint.
+     * @return array|null Array of constraint data, or null
+     */
+    public function getConstraint($name);
+
+    /**
+     * Remove a constraint.
+     *
+     * @param string $name Name of the constraint to remove
+     * @return $this
+     */
+    public function dropConstraint($name);
+
+    /**
+     * Get the names of all the constraints in the table.
+     *
+     * @return array
+     */
+    public function constraints();
+}

+ 0 - 221
src/Datasource/SchemaInterface.php

@@ -21,118 +21,6 @@ interface SchemaInterface
 {
 
     /**
-     * Binary column type
-     *
-     * @var string
-     */
-    const TYPE_BINARY = 'binary';
-
-    /**
-     * Date column type
-     *
-     * @var string
-     */
-    const TYPE_DATE = 'date';
-
-    /**
-     * Datetime column type
-     *
-     * @var string
-     */
-    const TYPE_DATETIME = 'datetime';
-
-    /**
-     * Time column type
-     *
-     * @var string
-     */
-    const TYPE_TIME = 'time';
-
-    /**
-     * Timestamp column type
-     *
-     * @var string
-     */
-    const TYPE_TIMESTAMP = 'timestamp';
-
-    /**
-     * JSON column type
-     *
-     * @var string
-     */
-    const TYPE_JSON = 'json';
-
-    /**
-     * String column type
-     *
-     * @var string
-     */
-    const TYPE_STRING = 'string';
-
-    /**
-     * Text column type
-     *
-     * @var string
-     */
-    const TYPE_TEXT = 'text';
-
-    /**
-     * Tiny Integer column type
-     *
-     * @var string
-     */
-    const TYPE_TINYINTEGER = 'tinyinteger';
-
-    /**
-     * Small Integer column type
-     *
-     * @var string
-     */
-    const TYPE_SMALLINTEGER = 'smallinteger';
-
-    /**
-     * Integer column type
-     *
-     * @var string
-     */
-    const TYPE_INTEGER = 'integer';
-
-    /**
-     * Big Integer column type
-     *
-     * @var string
-     */
-    const TYPE_BIGINTEGER = 'biginteger';
-
-    /**
-     * Float column type
-     *
-     * @var string
-     */
-    const TYPE_FLOAT = 'float';
-
-    /**
-     * Decimal column type
-     *
-     * @var string
-     */
-    const TYPE_DECIMAL = 'decimal';
-
-    /**
-     * Boolean column type
-     *
-     * @var string
-     */
-    const TYPE_BOOLEAN = 'boolean';
-
-    /**
-     * UUID column type
-     *
-     * @var string
-     */
-    const TYPE_UUID = 'uuid';
-
-    /**
      * Get the name of the table.
      *
      * @return string
@@ -255,100 +143,6 @@ interface SchemaInterface
     public function defaultValues();
 
     /**
-     * Add an index.
-     *
-     * Used to add indexes, and full text indexes in platforms that support
-     * them.
-     *
-     * ### Attributes
-     *
-     * - `type` The type of index being added.
-     * - `columns` The columns in the index.
-     *
-     * @param string $name The name of the index.
-     * @param array $attrs The attributes for the index.
-     * @return $this
-     * @throws \Cake\Database\Exception
-     */
-    public function addIndex($name, $attrs);
-
-    /**
-     * Read information about an index based on name.
-     *
-     * @param string $name The name of the index.
-     * @return array|null Array of index data, or null
-     */
-    public function getIndex($name);
-
-    /**
-     * Get the names of all the indexes in the table.
-     *
-     * @return array
-     */
-    public function indexes();
-
-    /**
-     * Get the column(s) used for the primary key.
-     *
-     * @return array Column name(s) for the primary key. An
-     *   empty list will be returned when the table has no primary key.
-     */
-    public function primaryKey();
-
-    /**
-     * Add a constraint.
-     *
-     * Used to add constraints to a table. For example primary keys, unique
-     * keys and foreign keys.
-     *
-     * ### Attributes
-     *
-     * - `type` The type of constraint being added.
-     * - `columns` The columns in the index.
-     * - `references` The table, column a foreign key references.
-     * - `update` The behavior on update. Options are 'restrict', 'setNull', 'cascade', 'noAction'.
-     * - `delete` The behavior on delete. Options are 'restrict', 'setNull', 'cascade', 'noAction'.
-     *
-     * The default for 'update' & 'delete' is 'cascade'.
-     *
-     * @param string $name The name of the constraint.
-     * @param array $attrs The attributes for the constraint.
-     * @return $this
-     * @throws \Cake\Database\Exception
-     */
-    public function addConstraint($name, $attrs);
-
-    /**
-     * Read information about a constraint based on name.
-     *
-     * @param string $name The name of the constraint.
-     * @return array|null Array of constraint data, or null
-     */
-    public function getConstraint($name);
-
-    /**
-     * Remove a constraint.
-     *
-     * @param string $name Name of the constraint to remove
-     * @return $this
-     */
-    public function dropConstraint($name);
-
-    /**
-     * Get the names of all the constraints in the table.
-     *
-     * @return array
-     */
-    public function constraints();
-
-    /**
-     * Check whether or not a table has an autoIncrement column defined.
-     *
-     * @return bool
-     */
-    public function hasAutoincrement();
-
-    /**
      * Sets the options for a table.
      *
      * Table options allow you to set platform specific table level options.
@@ -368,19 +162,4 @@ interface SchemaInterface
      * @return array An array of options.
      */
     public function getOptions();
-
-    /**
-     * Sets whether the table is temporary in the database.
-     *
-     * @param bool $temporary Whether or not the table is to be temporary.
-     * @return $this
-     */
-    public function setTemporary($temporary);
-
-    /**
-     * Gets whether the table is temporary in the database.
-     *
-     * @return bool The current temporary setting.
-     */
-    public function isTemporary();
 }