pdo_mysql_forge.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
  33. * @license https://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 3.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * PDO MySQL Forge Class
  41. *
  42. * @category Database
  43. * @author EllisLab Dev Team
  44. * @link https://codeigniter.com/user_guide/database/
  45. */
  46. class CI_DB_pdo_mysql_forge extends CI_DB_pdo_forge {
  47. /**
  48. * CREATE DATABASE statement
  49. *
  50. * @var string
  51. */
  52. protected $_create_database = 'CREATE DATABASE %s CHARACTER SET %s COLLATE %s';
  53. /**
  54. * CREATE TABLE IF statement
  55. *
  56. * @var string
  57. */
  58. protected $_create_table_if = 'CREATE TABLE IF NOT EXISTS';
  59. /**
  60. * CREATE TABLE keys flag
  61. *
  62. * Whether table keys are created from within the
  63. * CREATE TABLE statement.
  64. *
  65. * @var bool
  66. */
  67. protected $_create_table_keys = TRUE;
  68. /**
  69. * DROP TABLE IF statement
  70. *
  71. * @var string
  72. */
  73. protected $_drop_table_if = 'DROP TABLE IF EXISTS';
  74. /**
  75. * UNSIGNED support
  76. *
  77. * @var array
  78. */
  79. protected $_unsigned = array(
  80. 'TINYINT',
  81. 'SMALLINT',
  82. 'MEDIUMINT',
  83. 'INT',
  84. 'INTEGER',
  85. 'BIGINT',
  86. 'REAL',
  87. 'DOUBLE',
  88. 'DOUBLE PRECISION',
  89. 'FLOAT',
  90. 'DECIMAL',
  91. 'NUMERIC'
  92. );
  93. /**
  94. * NULL value representation in CREATE/ALTER TABLE statements
  95. *
  96. * @var string
  97. */
  98. protected $_null = 'NULL';
  99. // --------------------------------------------------------------------
  100. /**
  101. * CREATE TABLE attributes
  102. *
  103. * @param array $attributes Associative array of table attributes
  104. * @return string
  105. */
  106. protected function _create_table_attr($attributes)
  107. {
  108. $sql = '';
  109. foreach (array_keys($attributes) as $key)
  110. {
  111. if (is_string($key))
  112. {
  113. $sql .= ' '.strtoupper($key).' = '.$attributes[$key];
  114. }
  115. }
  116. if ( ! empty($this->db->char_set) && ! strpos($sql, 'CHARACTER SET') && ! strpos($sql, 'CHARSET'))
  117. {
  118. $sql .= ' DEFAULT CHARACTER SET = '.$this->db->char_set;
  119. }
  120. if ( ! empty($this->db->dbcollat) && ! strpos($sql, 'COLLATE'))
  121. {
  122. $sql .= ' COLLATE = '.$this->db->dbcollat;
  123. }
  124. return $sql;
  125. }
  126. // --------------------------------------------------------------------
  127. /**
  128. * ALTER TABLE
  129. *
  130. * @param string $alter_type ALTER type
  131. * @param string $table Table name
  132. * @param mixed $field Column definition
  133. * @return string|string[]
  134. */
  135. protected function _alter_table($alter_type, $table, $field)
  136. {
  137. if ($alter_type === 'DROP')
  138. {
  139. return parent::_alter_table($alter_type, $table, $field);
  140. }
  141. $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
  142. for ($i = 0, $c = count($field); $i < $c; $i++)
  143. {
  144. if ($field[$i]['_literal'] !== FALSE)
  145. {
  146. $field[$i] = ($alter_type === 'ADD')
  147. ? "\n\tADD ".$field[$i]['_literal']
  148. : "\n\tMODIFY ".$field[$i]['_literal'];
  149. }
  150. else
  151. {
  152. if ($alter_type === 'ADD')
  153. {
  154. $field[$i]['_literal'] = "\n\tADD ";
  155. }
  156. else
  157. {
  158. $field[$i]['_literal'] = empty($field[$i]['new_name']) ? "\n\tMODIFY " : "\n\tCHANGE ";
  159. }
  160. $field[$i] = $field[$i]['_literal'].$this->_process_column($field[$i]);
  161. }
  162. }
  163. return array($sql.implode(',', $field));
  164. }
  165. // --------------------------------------------------------------------
  166. /**
  167. * Process column
  168. *
  169. * @param array $field
  170. * @return string
  171. */
  172. protected function _process_column($field)
  173. {
  174. $extra_clause = isset($field['after'])
  175. ? ' AFTER '.$this->db->escape_identifiers($field['after']) : '';
  176. if (empty($extra_clause) && isset($field['first']) && $field['first'] === TRUE)
  177. {
  178. $extra_clause = ' FIRST';
  179. }
  180. return $this->db->escape_identifiers($field['name'])
  181. .(empty($field['new_name']) ? '' : ' '.$this->db->escape_identifiers($field['new_name']))
  182. .' '.$field['type'].$field['length']
  183. .$field['unsigned']
  184. .$field['null']
  185. .$field['default']
  186. .$field['auto_increment']
  187. .$field['unique']
  188. .(empty($field['comment']) ? '' : ' COMMENT '.$field['comment'])
  189. .$extra_clause;
  190. }
  191. // --------------------------------------------------------------------
  192. /**
  193. * Process indexes
  194. *
  195. * @param string $table (ignored)
  196. * @return string
  197. */
  198. protected function _process_indexes($table)
  199. {
  200. $sql = '';
  201. for ($i = 0, $c = count($this->keys); $i < $c; $i++)
  202. {
  203. if (is_array($this->keys[$i]))
  204. {
  205. for ($i2 = 0, $c2 = count($this->keys[$i]); $i2 < $c2; $i2++)
  206. {
  207. if ( ! isset($this->fields[$this->keys[$i][$i2]]))
  208. {
  209. unset($this->keys[$i][$i2]);
  210. continue;
  211. }
  212. }
  213. }
  214. elseif ( ! isset($this->fields[$this->keys[$i]]))
  215. {
  216. unset($this->keys[$i]);
  217. continue;
  218. }
  219. is_array($this->keys[$i]) OR $this->keys[$i] = array($this->keys[$i]);
  220. $sql .= ",\n\tKEY ".$this->db->escape_identifiers(implode('_', $this->keys[$i]))
  221. .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).')';
  222. }
  223. $this->keys = array();
  224. return $sql;
  225. }
  226. }