mysql_forge.php 6.3 KB

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