BaseSchema.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Database\Schema;
  16. use Cake\Database\Driver;
  17. /**
  18. * Base class for schema implementations.
  19. *
  20. * This class contains methods that are common across
  21. * the various SQL dialects.
  22. */
  23. abstract class BaseSchema
  24. {
  25. /**
  26. * The driver instance being used.
  27. *
  28. * @var \Cake\Database\Driver
  29. */
  30. protected $_driver;
  31. /**
  32. * Constructor
  33. *
  34. * This constructor will connect the driver so that methods like columnSql() and others
  35. * will fail when the driver has not been connected.
  36. *
  37. * @param \Cake\Database\Driver $driver The driver to use.
  38. */
  39. public function __construct(Driver $driver)
  40. {
  41. $driver->connect();
  42. $this->_driver = $driver;
  43. }
  44. /**
  45. * Generate an ON clause for a foreign key.
  46. *
  47. * @param string|null $on The on clause
  48. * @return string
  49. */
  50. protected function _foreignOnClause($on)
  51. {
  52. if ($on === TableSchema::ACTION_SET_NULL) {
  53. return 'SET NULL';
  54. }
  55. if ($on === TableSchema::ACTION_SET_DEFAULT) {
  56. return 'SET DEFAULT';
  57. }
  58. if ($on === TableSchema::ACTION_CASCADE) {
  59. return 'CASCADE';
  60. }
  61. if ($on === TableSchema::ACTION_RESTRICT) {
  62. return 'RESTRICT';
  63. }
  64. if ($on === TableSchema::ACTION_NO_ACTION) {
  65. return 'NO ACTION';
  66. }
  67. }
  68. /**
  69. * Convert string on clauses to the abstract ones.
  70. *
  71. * @param string $clause The on clause to convert.
  72. * @return string|null
  73. */
  74. protected function _convertOnClause($clause)
  75. {
  76. if ($clause === 'CASCADE' || $clause === 'RESTRICT') {
  77. return strtolower($clause);
  78. }
  79. if ($clause === 'NO ACTION') {
  80. return TableSchema::ACTION_NO_ACTION;
  81. }
  82. return TableSchema::ACTION_SET_NULL;
  83. }
  84. /**
  85. * Convert foreign key constraints references to a valid
  86. * stringified list
  87. *
  88. * @param string|array $references The referenced columns of a foreign key constraint statement
  89. * @return string
  90. */
  91. protected function _convertConstraintColumns($references)
  92. {
  93. if (is_string($references)) {
  94. return $this->_driver->quoteIdentifier($references);
  95. }
  96. return implode(', ', array_map(
  97. [$this->_driver, 'quoteIdentifier'],
  98. $references
  99. ));
  100. }
  101. /**
  102. * Generate the SQL to drop a table.
  103. *
  104. * @param \Cake\Database\Schema\TableSchema $schema Schema instance
  105. * @return array SQL statements to drop a table.
  106. */
  107. public function dropTableSql(TableSchema $schema)
  108. {
  109. $sql = sprintf(
  110. 'DROP TABLE %s',
  111. $this->_driver->quoteIdentifier($schema->name())
  112. );
  113. return [$sql];
  114. }
  115. /**
  116. * Generate the SQL to list the tables.
  117. *
  118. * @param array $config The connection configuration to use for
  119. * getting tables from.
  120. * @return array An array of (sql, params) to execute.
  121. */
  122. abstract public function listTablesSql($config);
  123. /**
  124. * Generate the SQL to describe a table.
  125. *
  126. * @param string $tableName The table name to get information on.
  127. * @param array $config The connection configuration.
  128. * @return array An array of (sql, params) to execute.
  129. */
  130. abstract public function describeColumnSql($tableName, $config);
  131. /**
  132. * Generate the SQL to describe the indexes in a table.
  133. *
  134. * @param string $tableName The table name to get information on.
  135. * @param array $config The connection configuration.
  136. * @return array An array of (sql, params) to execute.
  137. */
  138. abstract public function describeIndexSql($tableName, $config);
  139. /**
  140. * Generate the SQL to describe the foreign keys in a table.
  141. *
  142. * @param string $tableName The table name to get information on.
  143. * @param array $config The connection configuration.
  144. * @return array An array of (sql, params) to execute.
  145. */
  146. abstract public function describeForeignKeySql($tableName, $config);
  147. /**
  148. * Generate the SQL to describe table options
  149. *
  150. * @param string $tableName Table name.
  151. * @param array $config The connection configuration.
  152. * @return array SQL statements to get options for a table.
  153. */
  154. public function describeOptionsSql($tableName, $config)
  155. {
  156. return ['', ''];
  157. }
  158. /**
  159. * Convert field description results into abstract schema fields.
  160. *
  161. * @param \Cake\Database\Schema\TableSchema $schema The table object to append fields to.
  162. * @param array $row The row data from `describeColumnSql`.
  163. * @return void
  164. */
  165. abstract public function convertColumnDescription(TableSchema $schema, $row);
  166. /**
  167. * Convert an index description results into abstract schema indexes or constraints.
  168. *
  169. * @param \Cake\Database\Schema\TableSchema $schema The table object to append
  170. * an index or constraint to.
  171. * @param array $row The row data from `describeIndexSql`.
  172. * @return void
  173. */
  174. abstract public function convertIndexDescription(TableSchema $schema, $row);
  175. /**
  176. * Convert a foreign key description into constraints on the Table object.
  177. *
  178. * @param \Cake\Database\Schema\TableSchema $schema The table object to append
  179. * a constraint to.
  180. * @param array $row The row data from `describeForeignKeySql`.
  181. * @return void
  182. */
  183. abstract public function convertForeignKeyDescription(TableSchema $schema, $row);
  184. /**
  185. * Convert options data into table options.
  186. *
  187. * @param \Cake\Database\Schema\TableSchema $schema Table instance.
  188. * @param array $row The row of data.
  189. * @return void
  190. */
  191. public function convertOptionsDescription(TableSchema $schema, $row)
  192. {
  193. }
  194. /**
  195. * Generate the SQL to create a table.
  196. *
  197. * @param \Cake\Database\Schema\TableSchema $schema Table instance.
  198. * @param array $columns The columns to go inside the table.
  199. * @param array $constraints The constraints for the table.
  200. * @param array $indexes The indexes for the table.
  201. * @return array SQL statements to create a table.
  202. */
  203. abstract public function createTableSql(TableSchema $schema, $columns, $constraints, $indexes);
  204. /**
  205. * Generate the SQL fragment for a single column in a table.
  206. *
  207. * @param \Cake\Database\Schema\TableSchema $schema The table instance the column is in.
  208. * @param string $name The name of the column.
  209. * @return string SQL fragment.
  210. */
  211. abstract public function columnSql(TableSchema $schema, $name);
  212. /**
  213. * Generate the SQL queries needed to add foreign key constraints to the table
  214. *
  215. * @param \Cake\Database\Schema\TableSchema $schema The table instance the foreign key constraints are.
  216. * @return array SQL fragment.
  217. */
  218. abstract public function addConstraintSql(TableSchema $schema);
  219. /**
  220. * Generate the SQL queries needed to drop foreign key constraints from the table
  221. *
  222. * @param \Cake\Database\Schema\TableSchema $schema The table instance the foreign key constraints are.
  223. * @return array SQL fragment.
  224. */
  225. abstract public function dropConstraintSql(TableSchema $schema);
  226. /**
  227. * Generate the SQL fragments for defining table constraints.
  228. *
  229. * @param \Cake\Database\Schema\TableSchema $schema The table instance the column is in.
  230. * @param string $name The name of the column.
  231. * @return string SQL fragment.
  232. */
  233. abstract public function constraintSql(TableSchema $schema, $name);
  234. /**
  235. * Generate the SQL fragment for a single index in a table.
  236. *
  237. * @param \Cake\Database\Schema\TableSchema $schema The table object the column is in.
  238. * @param string $name The name of the column.
  239. * @return string SQL fragment.
  240. */
  241. abstract public function indexSql(TableSchema $schema, $name);
  242. /**
  243. * Generate the SQL to truncate a table.
  244. *
  245. * @param \Cake\Database\Schema\TableSchema $schema Table instance.
  246. * @return array SQL statements to truncate a table.
  247. */
  248. abstract public function truncateTableSql(TableSchema $schema);
  249. }