BaseSchema.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * PHP Version 5.4
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 3.0.0
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. namespace Cake\Database\Schema;
  18. use Cake\Database\Driver;
  19. use Cake\Database\Schema\Table;
  20. /**
  21. * Base class for schema implementations.
  22. *
  23. * This class contains methods that are common across
  24. * the various SQL dialects.
  25. */
  26. abstract class BaseSchema {
  27. /**
  28. * The driver instance being used.
  29. *
  30. * @var Cake\Database\Driver
  31. */
  32. protected $_driver;
  33. /**
  34. * Constructor
  35. *
  36. * @param Cake\Database\Driver $driver The driver to use.
  37. * @return void
  38. */
  39. public function __construct(Driver $driver) {
  40. $this->_driver = $driver;
  41. }
  42. /**
  43. * Generate an ON clause for a foreign key.
  44. *
  45. * @param string|null $on The on clause
  46. * @return string
  47. */
  48. protected function _foreignOnClause($on) {
  49. if ($on === Table::ACTION_SET_NULL) {
  50. return 'SET NULL';
  51. }
  52. if ($on === Table::ACTION_CASCADE) {
  53. return 'CASCADE';
  54. }
  55. if ($on === Table::ACTION_RESTRICT) {
  56. return 'RESTRICT';
  57. }
  58. if ($on === Table::ACTION_NO_ACTION) {
  59. return 'NO ACTION';
  60. }
  61. }
  62. /**
  63. * Convert string on clauses to the abstract ones.
  64. *
  65. * @param string $clause
  66. * @return string|null
  67. */
  68. protected function _convertOnClause($clause) {
  69. if ($clause === 'CASCADE' || $clause === 'RESTRICT') {
  70. return strtolower($clause);
  71. }
  72. if ($clause === 'NO ACTION') {
  73. return Table::ACTION_NO_ACTION;
  74. }
  75. return Table::ACTION_SET_NULL;
  76. }
  77. /**
  78. * Generate the SQL to drop a table.
  79. *
  80. * @param Cake\Database\Schema\Table $table Table instance
  81. * @return array SQL statements to drop a table.
  82. */
  83. public function dropTableSql(Table $table) {
  84. $sql = sprintf(
  85. 'DROP TABLE %s',
  86. $this->_driver->quoteIdentifier($table->name())
  87. );
  88. return [$sql];
  89. }
  90. /**
  91. * Generate the SQL to list the tables.
  92. *
  93. * @param array $config The connection configuration to use for
  94. * getting tables from.
  95. * @return array An array of (sql, params) to execute.
  96. */
  97. abstract public function listTablesSql($config);
  98. /**
  99. * Generate the SQL to describe a table.
  100. *
  101. * @param string $name The table name to get information on.
  102. * @param array $config The connection configuration.
  103. * @return array An array of (sql, params) to execute.
  104. */
  105. abstract public function describeTableSql($name, $config);
  106. /**
  107. * Generate the SQL to describe the indexes in a table.
  108. *
  109. * @param string $table The table name to get information on.
  110. * @param array $config The connection configuration.
  111. * @return array An array of (sql, params) to execute.
  112. */
  113. abstract public function describeIndexSql($table, $config);
  114. /**
  115. * Generate the SQL to describe the foreign keys in a table.
  116. *
  117. * @param string $table The table name to get information on.
  118. * @param array $config The connection configuration.
  119. * @return array An array of (sql, params) to execute.
  120. */
  121. abstract public function describeForeignKeySql($table, $config);
  122. /**
  123. * Convert field description results into abstract schema fields.
  124. *
  125. * @param Cake\Database\Schema\Table $table The table object to append fields to.
  126. * @param array $row The row data from `describeTableSql`.
  127. * @return void
  128. */
  129. abstract public function convertFieldDescription(Table $table, $row);
  130. /**
  131. * Convert an index description results into abstract schema indexes or constraints.
  132. *
  133. * @param Cake\Database\Schema\Table $table The table object to append
  134. * an index or constraint to.
  135. * @param array $row The row data from `describeIndexSql`.
  136. * @return void
  137. */
  138. abstract public function convertIndexDescription(Table $table, $row);
  139. /**
  140. * Convert a foreign key description into constraints on the Table object.
  141. *
  142. * @param Cake\Database\Schema\Table $table The table object to append
  143. * a constraint to.
  144. * @param array $row The row data from `describeForeignKeySql`.
  145. * @return void
  146. */
  147. abstract public function convertForeignKeyDescription(Table $table, $row);
  148. /**
  149. * Generate the SQL to create a table.
  150. *
  151. * @param Cake\Database\Schema\Table $table Table instance.
  152. * @param array $columns The columns to go inside the table.
  153. * @param array $constraints The constraints for the table.
  154. * @param array $indexes The indexes for the table.
  155. * @return array SQL statements to create a table.
  156. */
  157. abstract public function createTableSql(Table $table, $columns, $constraints, $indexes);
  158. /**
  159. * Generate the SQL fragment for a single column in a table.
  160. *
  161. * @param Cake\Database\Schema\Table $table The table instance the column is in.
  162. * @param string $name The name of the column.
  163. * @return string SQL fragment.
  164. */
  165. abstract public function columnSql(Table $table, $name);
  166. /**
  167. * Generate the SQL fragments for defining table constraints.
  168. *
  169. * @param Cake\Database\Schema\Table $table The table instance the column is in.
  170. * @param string $name The name of the column.
  171. * @return string SQL fragment.
  172. */
  173. abstract public function constraintSql(Table $table, $name);
  174. /**
  175. * Generate the SQL fragment for a single index in a table.
  176. *
  177. * @param Cake\Database\Schema\Table $table The table object the column is in.
  178. * @param string $name The name of the column.
  179. * @return string SQL fragment.
  180. */
  181. abstract public function indexSql(Table $table, $name);
  182. /**
  183. * Generate the SQL to truncate a table.
  184. *
  185. * @param Cake\Database\Schema\Table $table Table instance.
  186. * @return array SQL statements to truncate a table.
  187. */
  188. abstract public function truncateTableSql(Table $table);
  189. }