mysqli_forge.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
  10. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  11. * @license http://codeigniter.com/user_guide/license.html
  12. * @link http://codeigniter.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * MySQLi Forge Class
  19. *
  20. * @category Database
  21. * @author EllisLab Dev Team
  22. * @link http://codeigniter.com/user_guide/database/
  23. */
  24. class CI_DB_mysqli_forge extends CI_DB_forge {
  25. /**
  26. * Create database
  27. *
  28. * @access private
  29. * @param string the database name
  30. * @return bool
  31. */
  32. function _create_database($name)
  33. {
  34. return "CREATE DATABASE ".$name;
  35. }
  36. // --------------------------------------------------------------------
  37. /**
  38. * Drop database
  39. *
  40. * @access private
  41. * @param string the database name
  42. * @return bool
  43. */
  44. function _drop_database($name)
  45. {
  46. return "DROP DATABASE ".$name;
  47. }
  48. // --------------------------------------------------------------------
  49. /**
  50. * Process Fields
  51. *
  52. * @access private
  53. * @param mixed the fields
  54. * @return string
  55. */
  56. function _process_fields($fields)
  57. {
  58. $current_field_count = 0;
  59. $sql = '';
  60. foreach ($fields as $field=>$attributes)
  61. {
  62. // Numeric field names aren't allowed in databases, so if the key is
  63. // numeric, we know it was assigned by PHP and the developer manually
  64. // entered the field information, so we'll simply add it to the list
  65. if (is_numeric($field))
  66. {
  67. $sql .= "\n\t$attributes";
  68. }
  69. else
  70. {
  71. $attributes = array_change_key_case($attributes, CASE_UPPER);
  72. $sql .= "\n\t".$this->db->_protect_identifiers($field);
  73. if (array_key_exists('NAME', $attributes))
  74. {
  75. $sql .= ' '.$this->db->_protect_identifiers($attributes['NAME']).' ';
  76. }
  77. if (array_key_exists('TYPE', $attributes))
  78. {
  79. $sql .= ' '.$attributes['TYPE'];
  80. }
  81. if (array_key_exists('CONSTRAINT', $attributes))
  82. {
  83. $sql .= '('.$attributes['CONSTRAINT'].')';
  84. }
  85. if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
  86. {
  87. $sql .= ' UNSIGNED';
  88. }
  89. if (array_key_exists('DEFAULT', $attributes))
  90. {
  91. $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
  92. }
  93. if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
  94. {
  95. $sql .= ' NULL';
  96. }
  97. else
  98. {
  99. $sql .= ' NOT NULL';
  100. }
  101. if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
  102. {
  103. $sql .= ' AUTO_INCREMENT';
  104. }
  105. }
  106. // don't add a comma on the end of the last field
  107. if (++$current_field_count < count($fields))
  108. {
  109. $sql .= ',';
  110. }
  111. }
  112. return $sql;
  113. }
  114. // --------------------------------------------------------------------
  115. /**
  116. * Create Table
  117. *
  118. * @access private
  119. * @param string the table name
  120. * @param mixed the fields
  121. * @param mixed primary key(s)
  122. * @param mixed key(s)
  123. * @param boolean should 'IF NOT EXISTS' be added to the SQL
  124. * @return bool
  125. */
  126. function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
  127. {
  128. $sql = 'CREATE TABLE ';
  129. if ($if_not_exists === TRUE)
  130. {
  131. $sql .= 'IF NOT EXISTS ';
  132. }
  133. $sql .= $this->db->_escape_identifiers($table)." (";
  134. $sql .= $this->_process_fields($fields);
  135. if (count($primary_keys) > 0)
  136. {
  137. $key_name = $this->db->_protect_identifiers(implode('_', $primary_keys));
  138. $primary_keys = $this->db->_protect_identifiers($primary_keys);
  139. $sql .= ",\n\tPRIMARY KEY ".$key_name." (" . implode(', ', $primary_keys) . ")";
  140. }
  141. if (is_array($keys) && count($keys) > 0)
  142. {
  143. foreach ($keys as $key)
  144. {
  145. if (is_array($key))
  146. {
  147. $key_name = $this->db->_protect_identifiers(implode('_', $key));
  148. $key = $this->db->_protect_identifiers($key);
  149. }
  150. else
  151. {
  152. $key_name = $this->db->_protect_identifiers($key);
  153. $key = array($key_name);
  154. }
  155. $sql .= ",\n\tKEY {$key_name} (" . implode(', ', $key) . ")";
  156. }
  157. }
  158. $sql .= "\n) DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};";
  159. return $sql;
  160. }
  161. // --------------------------------------------------------------------
  162. /**
  163. * Drop Table
  164. *
  165. * @access private
  166. * @return string
  167. */
  168. function _drop_table($table)
  169. {
  170. return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table);
  171. }
  172. // --------------------------------------------------------------------
  173. /**
  174. * Alter table query
  175. *
  176. * Generates a platform-specific query so that a table can be altered
  177. * Called by add_column(), drop_column(), and column_alter(),
  178. *
  179. * @access private
  180. * @param string the ALTER type (ADD, DROP, CHANGE)
  181. * @param string the column name
  182. * @param array fields
  183. * @param string the field after which we should add the new field
  184. * @return object
  185. */
  186. function _alter_table($alter_type, $table, $fields, $after_field = '')
  187. {
  188. $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ";
  189. // DROP has everything it needs now.
  190. if ($alter_type == 'DROP')
  191. {
  192. return $sql.$this->db->_protect_identifiers($fields);
  193. }
  194. $sql .= $this->_process_fields($fields);
  195. if ($after_field != '')
  196. {
  197. $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
  198. }
  199. return $sql;
  200. }
  201. // --------------------------------------------------------------------
  202. /**
  203. * Rename a table
  204. *
  205. * Generates a platform-specific query so that a table can be renamed
  206. *
  207. * @access private
  208. * @param string the old table name
  209. * @param string the new table name
  210. * @return string
  211. */
  212. function _rename_table($table_name, $new_table_name)
  213. {
  214. $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
  215. return $sql;
  216. }
  217. }
  218. /* End of file mysqli_forge.php */
  219. /* Location: ./system/database/drivers/mysqli/mysqli_forge.php */