PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ddl/mysql_sql_generator.php

https://bitbucket.org/moodle/moodle
PHP | 643 lines | 377 code | 70 blank | 196 comment | 65 complexity | 7b322e437e5eacea8878f3641c5bb650 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Mysql specific SQL code generator.
  18. *
  19. * @package core_ddl
  20. * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
  21. * 2001-3001 Eloy Lafuente (stronk7) http://contiento.com
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. require_once($CFG->libdir.'/ddl/sql_generator.php');
  26. /**
  27. * This class generate SQL code to be used against MySQL
  28. * It extends XMLDBgenerator so everything can be
  29. * overridden as needed to generate correct SQL.
  30. *
  31. * @package core_ddl
  32. * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
  33. * 2001-3001 Eloy Lafuente (stronk7) http://contiento.com
  34. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35. */
  36. class mysql_sql_generator extends sql_generator {
  37. // Only set values that are different from the defaults present in XMLDBgenerator
  38. /** @var string Used to quote names. */
  39. public $quote_string = '`';
  40. /** @var string To define the default to set for NOT NULLs CHARs without default (null=do nothing).*/
  41. public $default_for_char = '';
  42. /** @var bool To specify if the generator must use some DEFAULT clause to drop defaults.*/
  43. public $drop_default_value_required = true;
  44. /** @var string The DEFAULT clause required to drop defaults.*/
  45. public $drop_default_value = null;
  46. /** @var string To force primary key names to one string (null=no force).*/
  47. public $primary_key_name = '';
  48. /** @var string Template to drop PKs. 'TABLENAME' and 'KEYNAME' will be replaced from this template.*/
  49. public $drop_primary_key = 'ALTER TABLE TABLENAME DROP PRIMARY KEY';
  50. /** @var string Template to drop UKs. 'TABLENAME' and 'KEYNAME' will be replaced from this template.*/
  51. public $drop_unique_key = 'ALTER TABLE TABLENAME DROP KEY KEYNAME';
  52. /** @var string Template to drop FKs. 'TABLENAME' and 'KEYNAME' will be replaced from this template.*/
  53. public $drop_foreign_key = 'ALTER TABLE TABLENAME DROP FOREIGN KEY KEYNAME';
  54. /** @var bool True if the generator needs to add extra code to generate the sequence fields.*/
  55. public $sequence_extra_code = false;
  56. /** @var string The particular name for inline sequences in this generator.*/
  57. public $sequence_name = 'auto_increment';
  58. public $add_after_clause = true; // Does the generator need to add the after clause for fields
  59. /** @var string Characters to be used as concatenation operator.*/
  60. public $concat_character = null;
  61. /** @var string The SQL template to alter columns where the 'TABLENAME' and 'COLUMNSPECS' keywords are dynamically replaced.*/
  62. public $alter_column_sql = 'ALTER TABLE TABLENAME MODIFY COLUMN COLUMNSPECS';
  63. /** @var string SQL sentence to drop one index where 'TABLENAME', 'INDEXNAME' keywords are dynamically replaced.*/
  64. public $drop_index_sql = 'ALTER TABLE TABLENAME DROP INDEX INDEXNAME';
  65. /** @var string SQL sentence to rename one index where 'TABLENAME', 'OLDINDEXNAME' and 'NEWINDEXNAME' are dynamically replaced.*/
  66. public $rename_index_sql = null;
  67. /** @var string SQL sentence to rename one key 'TABLENAME', 'OLDKEYNAME' and 'NEWKEYNAME' are dynamically replaced.*/
  68. public $rename_key_sql = null;
  69. /** Maximum size of InnoDB row in Antelope file format */
  70. const ANTELOPE_MAX_ROW_SIZE = 8126;
  71. /**
  72. * Reset a sequence to the id field of a table.
  73. *
  74. * @param xmldb_table|string $table name of table or the table object.
  75. * @return array of sql statements
  76. */
  77. public function getResetSequenceSQL($table) {
  78. if ($table instanceof xmldb_table) {
  79. $tablename = $table->getName();
  80. } else {
  81. $tablename = $table;
  82. }
  83. // From http://dev.mysql.com/doc/refman/5.0/en/alter-table.html
  84. $value = (int)$this->mdb->get_field_sql('SELECT MAX(id) FROM {'.$tablename.'}');
  85. $value++;
  86. return array("ALTER TABLE $this->prefix$tablename AUTO_INCREMENT = $value");
  87. }
  88. /**
  89. * Calculate proximate row size when using InnoDB
  90. * tables in Antelope row format.
  91. *
  92. * Note: the returned value is a bit higher to compensate for
  93. * errors and changes of column data types.
  94. *
  95. * @deprecated since Moodle 2.9 MDL-49723 - please do not use this function any more.
  96. */
  97. public function guess_antolope_row_size(array $columns) {
  98. throw new coding_exception('guess_antolope_row_size() can not be used any more, please use guess_antelope_row_size() instead.');
  99. }
  100. /**
  101. * Calculate proximate row size when using InnoDB tables in Antelope row format.
  102. *
  103. * Note: the returned value is a bit higher to compensate for errors and changes of column data types.
  104. *
  105. * @param xmldb_field[]|database_column_info[] $columns
  106. * @return int approximate row size in bytes
  107. */
  108. public function guess_antelope_row_size(array $columns) {
  109. if (empty($columns)) {
  110. return 0;
  111. }
  112. $size = 0;
  113. $first = reset($columns);
  114. if (count($columns) > 1) {
  115. // Do not start with zero because we need to cover changes of field types and
  116. // this calculation is most probably not be accurate.
  117. $size += 1000;
  118. }
  119. if ($first instanceof xmldb_field) {
  120. foreach ($columns as $field) {
  121. switch ($field->getType()) {
  122. case XMLDB_TYPE_TEXT:
  123. $size += 768;
  124. break;
  125. case XMLDB_TYPE_BINARY:
  126. $size += 768;
  127. break;
  128. case XMLDB_TYPE_CHAR:
  129. $bytes = $field->getLength() * 3;
  130. if ($bytes > 768) {
  131. $bytes = 768;
  132. }
  133. $size += $bytes;
  134. break;
  135. default:
  136. // Anything else is usually maximum 8 bytes.
  137. $size += 8;
  138. }
  139. }
  140. } else if ($first instanceof database_column_info) {
  141. foreach ($columns as $column) {
  142. switch ($column->meta_type) {
  143. case 'X':
  144. $size += 768;
  145. break;
  146. case 'B':
  147. $size += 768;
  148. break;
  149. case 'C':
  150. $bytes = $column->max_length * 3;
  151. if ($bytes > 768) {
  152. $bytes = 768;
  153. }
  154. $size += $bytes;
  155. break;
  156. default:
  157. // Anything else is usually maximum 8 bytes.
  158. $size += 8;
  159. }
  160. }
  161. }
  162. return $size;
  163. }
  164. /**
  165. * Given one correct xmldb_table, returns the SQL statements
  166. * to create it (inside one array).
  167. *
  168. * @param xmldb_table $xmldb_table An xmldb_table instance.
  169. * @return array An array of SQL statements, starting with the table creation SQL followed
  170. * by any of its comments, indexes and sequence creation SQL statements.
  171. */
  172. public function getCreateTableSQL($xmldb_table) {
  173. // First find out if want some special db engine.
  174. $engine = $this->mdb->get_dbengine();
  175. // Do we know collation?
  176. $collation = $this->mdb->get_dbcollation();
  177. // Do we need to use compressed format for rows?
  178. $rowformat = "";
  179. $size = $this->guess_antelope_row_size($xmldb_table->getFields());
  180. if ($size > self::ANTELOPE_MAX_ROW_SIZE) {
  181. if ($this->mdb->is_compressed_row_format_supported()) {
  182. $rowformat = "\n ROW_FORMAT=Compressed";
  183. }
  184. }
  185. $utf8mb4rowformat = $this->mdb->get_row_format_sql($engine, $collation);
  186. $rowformat = ($utf8mb4rowformat == '') ? $rowformat : $utf8mb4rowformat;
  187. $sqlarr = parent::getCreateTableSQL($xmldb_table);
  188. // This is a very nasty hack that tries to use just one query per created table
  189. // because MySQL is stupidly slow when modifying empty tables.
  190. // Note: it is safer to inject everything on new lines because there might be some trailing -- comments.
  191. $sqls = array();
  192. $prevcreate = null;
  193. $matches = null;
  194. foreach ($sqlarr as $sql) {
  195. if (preg_match('/^CREATE TABLE ([^ ]+)/', $sql, $matches)) {
  196. $prevcreate = $matches[1];
  197. $sql = preg_replace('/\s*\)\s*$/s', '/*keyblock*/)', $sql);
  198. // Let's inject the extra MySQL tweaks here.
  199. if ($engine) {
  200. $sql .= "\n ENGINE = $engine";
  201. }
  202. if ($collation) {
  203. if (strpos($collation, 'utf8_') === 0) {
  204. $sql .= "\n DEFAULT CHARACTER SET utf8";
  205. }
  206. $sql .= "\n DEFAULT COLLATE = $collation ";
  207. }
  208. if ($rowformat) {
  209. $sql .= $rowformat;
  210. }
  211. $sqls[] = $sql;
  212. continue;
  213. }
  214. if ($prevcreate) {
  215. if (preg_match('/^ALTER TABLE '.$prevcreate.' COMMENT=(.*)$/s', $sql, $matches)) {
  216. $prev = array_pop($sqls);
  217. $prev .= "\n COMMENT=$matches[1]";
  218. $sqls[] = $prev;
  219. continue;
  220. }
  221. if (preg_match('/^CREATE INDEX ([^ ]+) ON '.$prevcreate.' (.*)$/s', $sql, $matches)) {
  222. $prev = array_pop($sqls);
  223. if (strpos($prev, '/*keyblock*/')) {
  224. $prev = str_replace('/*keyblock*/', "\n, KEY $matches[1] $matches[2]/*keyblock*/", $prev);
  225. $sqls[] = $prev;
  226. continue;
  227. } else {
  228. $sqls[] = $prev;
  229. }
  230. }
  231. if (preg_match('/^CREATE UNIQUE INDEX ([^ ]+) ON '.$prevcreate.' (.*)$/s', $sql, $matches)) {
  232. $prev = array_pop($sqls);
  233. if (strpos($prev, '/*keyblock*/')) {
  234. $prev = str_replace('/*keyblock*/', "\n, UNIQUE KEY $matches[1] $matches[2]/*keyblock*/", $prev);
  235. $sqls[] = $prev;
  236. continue;
  237. } else {
  238. $sqls[] = $prev;
  239. }
  240. }
  241. }
  242. $prevcreate = null;
  243. $sqls[] = $sql;
  244. }
  245. foreach ($sqls as $key => $sql) {
  246. $sqls[$key] = str_replace('/*keyblock*/', "\n", $sql);
  247. }
  248. return $sqls;
  249. }
  250. /**
  251. * Given one xmldb_table and one xmldb_field, return the SQL statements needed to add the field to the table.
  252. *
  253. * @param xmldb_table $xmldb_table The table related to $xmldb_field.
  254. * @param xmldb_field $xmldb_field The instance of xmldb_field to create the SQL from.
  255. * @param string $skip_type_clause The type clause on alter columns, NULL by default.
  256. * @param string $skip_default_clause The default clause on alter columns, NULL by default.
  257. * @param string $skip_notnull_clause The null/notnull clause on alter columns, NULL by default.
  258. * @return array The SQL statement for adding a field to the table.
  259. */
  260. public function getAddFieldSQL($xmldb_table, $xmldb_field, $skip_type_clause = NULL, $skip_default_clause = NULL, $skip_notnull_clause = NULL) {
  261. $sqls = parent::getAddFieldSQL($xmldb_table, $xmldb_field, $skip_type_clause, $skip_default_clause, $skip_notnull_clause);
  262. if ($this->table_exists($xmldb_table)) {
  263. $tablename = $xmldb_table->getName();
  264. $size = $this->guess_antelope_row_size($this->mdb->get_columns($tablename));
  265. $size += $this->guess_antelope_row_size(array($xmldb_field));
  266. if ($size > self::ANTELOPE_MAX_ROW_SIZE) {
  267. if ($this->mdb->is_compressed_row_format_supported()) {
  268. $format = strtolower($this->mdb->get_row_format($tablename));
  269. if ($format === 'compact' or $format === 'redundant') {
  270. // Change the format before conversion so that we do not run out of space.
  271. array_unshift($sqls, "ALTER TABLE {$this->prefix}$tablename ROW_FORMAT=Compressed");
  272. }
  273. }
  274. }
  275. }
  276. return $sqls;
  277. }
  278. public function getAlterFieldSQL($xmldb_table, $xmldb_field, $skip_type_clause = NULL, $skip_default_clause = NULL, $skip_notnull_clause = NULL)
  279. {
  280. $tablename = $xmldb_table->getName();
  281. $dbcolumnsinfo = $this->mdb->get_columns($tablename);
  282. if (($this->mdb->has_breaking_change_sqlmode()) &&
  283. ($dbcolumnsinfo[$xmldb_field->getName()]->meta_type == 'X') &&
  284. ($xmldb_field->getType() == XMLDB_TYPE_INTEGER)) {
  285. // Ignore 1292 ER_TRUNCATED_WRONG_VALUE Truncated incorrect INTEGER value: '%s'.
  286. $altercolumnsqlorig = $this->alter_column_sql;
  287. $this->alter_column_sql = str_replace('ALTER TABLE', 'ALTER IGNORE TABLE', $this->alter_column_sql);
  288. $result = parent::getAlterFieldSQL($xmldb_table, $xmldb_field, $skip_type_clause, $skip_default_clause, $skip_notnull_clause);
  289. // Restore the original ALTER SQL statement pattern.
  290. $this->alter_column_sql = $altercolumnsqlorig;
  291. return $result;
  292. }
  293. return parent::getAlterFieldSQL($xmldb_table, $xmldb_field, $skip_type_clause, $skip_default_clause, $skip_notnull_clause);
  294. }
  295. /**
  296. * Given one correct xmldb_table, returns the SQL statements
  297. * to create temporary table (inside one array).
  298. *
  299. * @param xmldb_table $xmldb_table The xmldb_table object instance.
  300. * @return array of sql statements
  301. */
  302. public function getCreateTempTableSQL($xmldb_table) {
  303. // Do we know collation?
  304. $collation = $this->mdb->get_dbcollation();
  305. $this->temptables->add_temptable($xmldb_table->getName());
  306. $sqlarr = parent::getCreateTableSQL($xmldb_table);
  307. // Let's inject the extra MySQL tweaks.
  308. foreach ($sqlarr as $i=>$sql) {
  309. if (strpos($sql, 'CREATE TABLE ') === 0) {
  310. // We do not want the engine hack included in create table SQL.
  311. $sqlarr[$i] = preg_replace('/^CREATE TABLE (.*)/s', 'CREATE TEMPORARY TABLE $1', $sql);
  312. if ($collation) {
  313. if (strpos($collation, 'utf8_') === 0) {
  314. $sqlarr[$i] .= " DEFAULT CHARACTER SET utf8";
  315. }
  316. $sqlarr[$i] .= " DEFAULT COLLATE $collation ROW_FORMAT=DYNAMIC";
  317. }
  318. }
  319. }
  320. return $sqlarr;
  321. }
  322. /**
  323. * Given one correct xmldb_table, returns the SQL statements
  324. * to drop it (inside one array).
  325. *
  326. * @param xmldb_table $xmldb_table The table to drop.
  327. * @return array SQL statement(s) for dropping the specified table.
  328. */
  329. public function getDropTableSQL($xmldb_table) {
  330. $sqlarr = parent::getDropTableSQL($xmldb_table);
  331. if ($this->temptables->is_temptable($xmldb_table->getName())) {
  332. $sqlarr = preg_replace('/^DROP TABLE/', "DROP TEMPORARY TABLE", $sqlarr);
  333. }
  334. return $sqlarr;
  335. }
  336. /**
  337. * Given one XMLDB Type, length and decimals, returns the DB proper SQL type.
  338. *
  339. * @param int $xmldb_type The xmldb_type defined constant. XMLDB_TYPE_INTEGER and other XMLDB_TYPE_* constants.
  340. * @param int $xmldb_length The length of that data type.
  341. * @param int $xmldb_decimals The decimal places of precision of the data type.
  342. * @return string The DB defined data type.
  343. */
  344. public function getTypeSQL($xmldb_type, $xmldb_length=null, $xmldb_decimals=null) {
  345. switch ($xmldb_type) {
  346. case XMLDB_TYPE_INTEGER: // From http://mysql.com/doc/refman/5.0/en/numeric-types.html!
  347. if (empty($xmldb_length)) {
  348. $xmldb_length = 10;
  349. }
  350. if ($xmldb_length > 9) {
  351. $dbtype = 'BIGINT';
  352. } else if ($xmldb_length > 6) {
  353. $dbtype = 'INT';
  354. } else if ($xmldb_length > 4) {
  355. $dbtype = 'MEDIUMINT';
  356. } else if ($xmldb_length > 2) {
  357. $dbtype = 'SMALLINT';
  358. } else {
  359. $dbtype = 'TINYINT';
  360. }
  361. $dbtype .= '(' . $xmldb_length . ')';
  362. break;
  363. case XMLDB_TYPE_NUMBER:
  364. $dbtype = $this->number_type;
  365. if (!empty($xmldb_length)) {
  366. $dbtype .= '(' . $xmldb_length;
  367. if (!empty($xmldb_decimals)) {
  368. $dbtype .= ',' . $xmldb_decimals;
  369. }
  370. $dbtype .= ')';
  371. }
  372. break;
  373. case XMLDB_TYPE_FLOAT:
  374. $dbtype = 'DOUBLE';
  375. if (!empty($xmldb_decimals)) {
  376. if ($xmldb_decimals < 6) {
  377. $dbtype = 'FLOAT';
  378. }
  379. }
  380. if (!empty($xmldb_length)) {
  381. $dbtype .= '(' . $xmldb_length;
  382. if (!empty($xmldb_decimals)) {
  383. $dbtype .= ',' . $xmldb_decimals;
  384. } else {
  385. $dbtype .= ', 0'; // In MySQL, if length is specified, decimals are mandatory for FLOATs
  386. }
  387. $dbtype .= ')';
  388. }
  389. break;
  390. case XMLDB_TYPE_CHAR:
  391. $dbtype = 'VARCHAR';
  392. if (empty($xmldb_length)) {
  393. $xmldb_length='255';
  394. }
  395. $dbtype .= '(' . $xmldb_length . ')';
  396. if ($collation = $this->mdb->get_dbcollation()) {
  397. if (strpos($collation, 'utf8_') === 0) {
  398. $dbtype .= " CHARACTER SET utf8";
  399. }
  400. $dbtype .= " COLLATE $collation";
  401. }
  402. break;
  403. case XMLDB_TYPE_TEXT:
  404. $dbtype = 'LONGTEXT';
  405. if ($collation = $this->mdb->get_dbcollation()) {
  406. if (strpos($collation, 'utf8_') === 0) {
  407. $dbtype .= " CHARACTER SET utf8";
  408. }
  409. $dbtype .= " COLLATE $collation";
  410. }
  411. break;
  412. case XMLDB_TYPE_BINARY:
  413. $dbtype = 'LONGBLOB';
  414. break;
  415. case XMLDB_TYPE_DATETIME:
  416. $dbtype = 'DATETIME';
  417. }
  418. return $dbtype;
  419. }
  420. /**
  421. * Given one xmldb_table and one xmldb_field, return the SQL statements needed to add its default
  422. * (usually invoked from getModifyDefaultSQL()
  423. *
  424. * @param xmldb_table $xmldb_table The xmldb_table object instance.
  425. * @param xmldb_field $xmldb_field The xmldb_field object instance.
  426. * @return array Array of SQL statements to create a field's default.
  427. */
  428. public function getCreateDefaultSQL($xmldb_table, $xmldb_field) {
  429. // Just a wrapper over the getAlterFieldSQL() function for MySQL that
  430. // is capable of handling defaults
  431. return $this->getAlterFieldSQL($xmldb_table, $xmldb_field);
  432. }
  433. /**
  434. * Given one correct xmldb_field and the new name, returns the SQL statements
  435. * to rename it (inside one array).
  436. *
  437. * @param xmldb_table $xmldb_table The table related to $xmldb_field.
  438. * @param xmldb_field $xmldb_field The instance of xmldb_field to get the renamed field from.
  439. * @param string $newname The new name to rename the field to.
  440. * @return array The SQL statements for renaming the field.
  441. */
  442. public function getRenameFieldSQL($xmldb_table, $xmldb_field, $newname) {
  443. // NOTE: MySQL is pretty different from the standard to justify this overloading.
  444. // Need a clone of xmldb_field to perform the change leaving original unmodified
  445. $xmldb_field_clone = clone($xmldb_field);
  446. // Change the name of the field to perform the change
  447. $xmldb_field_clone->setName($newname);
  448. $fieldsql = $this->getFieldSQL($xmldb_table, $xmldb_field_clone);
  449. $sql = 'ALTER TABLE ' . $this->getTableName($xmldb_table) . ' CHANGE ' .
  450. $this->getEncQuoted($xmldb_field->getName()) . ' ' . $fieldsql;
  451. return array($sql);
  452. }
  453. /**
  454. * Given one xmldb_table and one xmldb_field, return the SQL statements needed to drop its default
  455. * (usually invoked from getModifyDefaultSQL()
  456. *
  457. * Note that this method may be dropped in future.
  458. *
  459. * @param xmldb_table $xmldb_table The xmldb_table object instance.
  460. * @param xmldb_field $xmldb_field The xmldb_field object instance.
  461. * @return array Array of SQL statements to create a field's default.
  462. *
  463. * @todo MDL-31147 Moodle 2.1 - Drop getDropDefaultSQL()
  464. */
  465. public function getDropDefaultSQL($xmldb_table, $xmldb_field) {
  466. // Just a wrapper over the getAlterFieldSQL() function for MySQL that
  467. // is capable of handling defaults
  468. return $this->getAlterFieldSQL($xmldb_table, $xmldb_field);
  469. }
  470. /**
  471. * Returns the code (array of statements) needed to add one comment to the table.
  472. *
  473. * @param xmldb_table $xmldb_table The xmldb_table object instance.
  474. * @return array Array of SQL statements to add one comment to the table.
  475. */
  476. function getCommentSQL ($xmldb_table) {
  477. $comment = '';
  478. if ($xmldb_table->getComment()) {
  479. $comment .= 'ALTER TABLE ' . $this->getTableName($xmldb_table);
  480. $comment .= " COMMENT='" . $this->addslashes(substr($xmldb_table->getComment(), 0, 60)) . "'";
  481. }
  482. return array($comment);
  483. }
  484. /**
  485. * Given one object name and it's type (pk, uk, fk, ck, ix, uix, seq, trg).
  486. *
  487. * (MySQL requires the whole xmldb_table object to be specified, so we add it always)
  488. *
  489. * This is invoked from getNameForObject().
  490. * Only some DB have this implemented.
  491. *
  492. * @param string $object_name The object's name to check for.
  493. * @param string $type The object's type (pk, uk, fk, ck, ix, uix, seq, trg).
  494. * @param string $table_name The table's name to check in
  495. * @return bool If such name is currently in use (true) or no (false)
  496. */
  497. public function isNameInUse($object_name, $type, $table_name) {
  498. switch($type) {
  499. case 'ix':
  500. case 'uix':
  501. // First of all, check table exists
  502. $metatables = $this->mdb->get_tables();
  503. if (isset($metatables[$table_name])) {
  504. // Fetch all the indexes in the table
  505. if ($indexes = $this->mdb->get_indexes($table_name)) {
  506. // Look for existing index in array
  507. if (isset($indexes[$object_name])) {
  508. return true;
  509. }
  510. }
  511. }
  512. break;
  513. }
  514. return false; //No name in use found
  515. }
  516. /**
  517. * Returns an array of reserved words (lowercase) for this DB
  518. * @return array An array of database specific reserved words
  519. */
  520. public static function getReservedWords() {
  521. // This file contains the reserved words for MySQL databases.
  522. $reserved_words = array (
  523. // From http://dev.mysql.com/doc/refman/6.0/en/reserved-words.html.
  524. 'accessible', 'add', 'all', 'alter', 'analyze', 'and', 'as', 'asc',
  525. 'asensitive', 'before', 'between', 'bigint', 'binary',
  526. 'blob', 'both', 'by', 'call', 'cascade', 'case', 'change',
  527. 'char', 'character', 'check', 'collate', 'column',
  528. 'condition', 'connection', 'constraint', 'continue',
  529. 'convert', 'create', 'cross', 'current_date', 'current_time',
  530. 'current_timestamp', 'current_user', 'cursor', 'database',
  531. 'databases', 'day_hour', 'day_microsecond',
  532. 'day_minute', 'day_second', 'dec', 'decimal', 'declare',
  533. 'default', 'delayed', 'delete', 'desc', 'describe',
  534. 'deterministic', 'distinct', 'distinctrow', 'div', 'double',
  535. 'drop', 'dual', 'each', 'else', 'elseif', 'enclosed', 'escaped',
  536. 'exists', 'exit', 'explain', 'false', 'fetch', 'float', 'float4',
  537. 'float8', 'for', 'force', 'foreign', 'from', 'fulltext', 'grant',
  538. 'group', 'having', 'high_priority', 'hour_microsecond',
  539. 'hour_minute', 'hour_second', 'if', 'ignore', 'in', 'index',
  540. 'infile', 'inner', 'inout', 'insensitive', 'insert', 'int', 'int1',
  541. 'int2', 'int3', 'int4', 'int8', 'integer', 'interval', 'into', 'is',
  542. 'iterate', 'join', 'key', 'keys', 'kill', 'leading', 'leave', 'left',
  543. 'like', 'limit', 'linear', 'lines', 'load', 'localtime', 'localtimestamp',
  544. 'lock', 'long', 'longblob', 'longtext', 'loop', 'low_priority', 'master_heartbeat_period',
  545. 'master_ssl_verify_server_cert', 'match', 'mediumblob', 'mediumint', 'mediumtext',
  546. 'middleint', 'minute_microsecond', 'minute_second',
  547. 'mod', 'modifies', 'natural', 'not', 'no_write_to_binlog',
  548. 'null', 'numeric', 'on', 'optimize', 'option', 'optionally',
  549. 'or', 'order', 'out', 'outer', 'outfile', 'overwrite', 'precision', 'primary',
  550. 'procedure', 'purge', 'raid0', 'range', 'read', 'read_only', 'read_write', 'reads', 'real',
  551. 'references', 'regexp', 'release', 'rename', 'repeat', 'replace',
  552. 'require', 'restrict', 'return', 'revoke', 'right', 'rlike', 'schema',
  553. 'schemas', 'second_microsecond', 'select', 'sensitive',
  554. 'separator', 'set', 'show', 'smallint', 'soname', 'spatial',
  555. 'specific', 'sql', 'sqlexception', 'sqlstate', 'sqlwarning',
  556. 'sql_big_result', 'sql_calc_found_rows', 'sql_small_result',
  557. 'ssl', 'starting', 'straight_join', 'table', 'terminated', 'then',
  558. 'tinyblob', 'tinyint', 'tinytext', 'to', 'trailing', 'trigger', 'true',
  559. 'undo', 'union', 'unique', 'unlock', 'unsigned', 'update',
  560. 'upgrade', 'usage', 'use', 'using', 'utc_date', 'utc_time',
  561. 'utc_timestamp', 'values', 'varbinary', 'varchar', 'varcharacter',
  562. 'varying', 'when', 'where', 'while', 'with', 'write', 'x509',
  563. 'xor', 'year_month', 'zerofill',
  564. // Added in MySQL 8.0, compared to MySQL 5.7:
  565. // https://dev.mysql.com/doc/refman/8.0/en/keywords.html#keywords-new-in-current-series.
  566. '_filename', 'admin', 'cume_dist', 'dense_rank', 'empty', 'except', 'first_value', 'grouping', 'groups',
  567. 'json_table', 'lag', 'last_value', 'lead', 'nth_value', 'ntile',
  568. 'of', 'over', 'percent_rank', 'persist', 'persist_only', 'rank', 'recursive', 'row_number',
  569. 'system', 'window'
  570. );
  571. return $reserved_words;
  572. }
  573. }