You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
6.0 KiB

7 years ago
  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 - 2017, 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 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.3.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * MySQLi Forge Class
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Drivers
  44. * @category Database
  45. * @author EllisLab Dev Team
  46. * @link https://codeigniter.com/user_guide/database/
  47. */
  48. class CI_DB_mysqli_forge extends CI_DB_forge {
  49. /**
  50. * CREATE DATABASE statement
  51. *
  52. * @var string
  53. */
  54. protected $_create_database = 'CREATE DATABASE %s CHARACTER SET %s COLLATE %s';
  55. /**
  56. * CREATE TABLE keys flag
  57. *
  58. * Whether table keys are created from within the
  59. * CREATE TABLE statement.
  60. *
  61. * @var bool
  62. */
  63. protected $_create_table_keys = TRUE;
  64. /**
  65. * UNSIGNED support
  66. *
  67. * @var array
  68. */
  69. protected $_unsigned = array(
  70. 'TINYINT',
  71. 'SMALLINT',
  72. 'MEDIUMINT',
  73. 'INT',
  74. 'INTEGER',
  75. 'BIGINT',
  76. 'REAL',
  77. 'DOUBLE',
  78. 'DOUBLE PRECISION',
  79. 'FLOAT',
  80. 'DECIMAL',
  81. 'NUMERIC'
  82. );
  83. /**
  84. * NULL value representation in CREATE/ALTER TABLE statements
  85. *
  86. * @var string
  87. */
  88. protected $_null = 'NULL';
  89. // --------------------------------------------------------------------
  90. /**
  91. * CREATE TABLE attributes
  92. *
  93. * @param array $attributes Associative array of table attributes
  94. * @return string
  95. */
  96. protected function _create_table_attr($attributes)
  97. {
  98. $sql = '';
  99. foreach (array_keys($attributes) as $key)
  100. {
  101. if (is_string($key))
  102. {
  103. $sql .= ' '.strtoupper($key).' = '.$attributes[$key];
  104. }
  105. }
  106. if ( ! empty($this->db->char_set) && ! strpos($sql, 'CHARACTER SET') && ! strpos($sql, 'CHARSET'))
  107. {
  108. $sql .= ' DEFAULT CHARACTER SET = '.$this->db->char_set;
  109. }
  110. if ( ! empty($this->db->dbcollat) && ! strpos($sql, 'COLLATE'))
  111. {
  112. $sql .= ' COLLATE = '.$this->db->dbcollat;
  113. }
  114. return $sql;
  115. }
  116. // --------------------------------------------------------------------
  117. /**
  118. * ALTER TABLE
  119. *
  120. * @param string $alter_type ALTER type
  121. * @param string $table Table name
  122. * @param mixed $field Column definition
  123. * @return string|string[]
  124. */
  125. protected function _alter_table($alter_type, $table, $field)
  126. {
  127. if ($alter_type === 'DROP')
  128. {
  129. return parent::_alter_table($alter_type, $table, $field);
  130. }
  131. $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
  132. for ($i = 0, $c = count($field); $i < $c; $i++)
  133. {
  134. if ($field[$i]['_literal'] !== FALSE)
  135. {
  136. $field[$i] = ($alter_type === 'ADD')
  137. ? "\n\tADD ".$field[$i]['_literal']
  138. : "\n\tMODIFY ".$field[$i]['_literal'];
  139. }
  140. else
  141. {
  142. if ($alter_type === 'ADD')
  143. {
  144. $field[$i]['_literal'] = "\n\tADD ";
  145. }
  146. else
  147. {
  148. $field[$i]['_literal'] = empty($field[$i]['new_name']) ? "\n\tMODIFY " : "\n\tCHANGE ";
  149. }
  150. $field[$i] = $field[$i]['_literal'].$this->_process_column($field[$i]);
  151. }
  152. }
  153. return array($sql.implode(',', $field));
  154. }
  155. // --------------------------------------------------------------------
  156. /**
  157. * Process column
  158. *
  159. * @param array $field
  160. * @return string
  161. */
  162. protected function _process_column($field)
  163. {
  164. $extra_clause = isset($field['after'])
  165. ? ' AFTER '.$this->db->escape_identifiers($field['after']) : '';
  166. if (empty($extra_clause) && isset($field['first']) && $field['first'] === TRUE)
  167. {
  168. $extra_clause = ' FIRST';
  169. }
  170. return $this->db->escape_identifiers($field['name'])
  171. .(empty($field['new_name']) ? '' : ' '.$this->db->escape_identifiers($field['new_name']))
  172. .' '.$field['type'].$field['length']
  173. .$field['unsigned']
  174. .$field['null']
  175. .$field['default']
  176. .$field['auto_increment']
  177. .$field['unique']
  178. .(empty($field['comment']) ? '' : ' COMMENT '.$field['comment'])
  179. .$extra_clause;
  180. }
  181. // --------------------------------------------------------------------
  182. /**
  183. * Process indexes
  184. *
  185. * @param string $table (ignored)
  186. * @return string
  187. */
  188. protected function _process_indexes($table)
  189. {
  190. $sql = '';
  191. for ($i = 0, $c = count($this->keys); $i < $c; $i++)
  192. {
  193. if (is_array($this->keys[$i]))
  194. {
  195. for ($i2 = 0, $c2 = count($this->keys[$i]); $i2 < $c2; $i2++)
  196. {
  197. if ( ! isset($this->fields[$this->keys[$i][$i2]]))
  198. {
  199. unset($this->keys[$i][$i2]);
  200. continue;
  201. }
  202. }
  203. }
  204. elseif ( ! isset($this->fields[$this->keys[$i]]))
  205. {
  206. unset($this->keys[$i]);
  207. continue;
  208. }
  209. is_array($this->keys[$i]) OR $this->keys[$i] = array($this->keys[$i]);
  210. $sql .= ",\n\tKEY ".$this->db->escape_identifiers(implode('_', $this->keys[$i]))
  211. .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).')';
  212. }
  213. $this->keys = array();
  214. return $sql;
  215. }
  216. }