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