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