PageRenderTime 69ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/system/database/drivers/mysqli/mysqli_forge.php

http://github.com/philsturgeon/codeigniter-restserver
PHP | 258 lines | 123 code | 40 blank | 95 comment | 22 complexity | 91ee4f164227f69c3126376c33833870 MD5 | raw file
Possible License(s): MIT
  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. * MySQLi Forge Class
  18. *
  19. * @category Database
  20. * @author ExpressionEngine Dev Team
  21. * @link http://codeigniter.com/user_guide/database/
  22. */
  23. class CI_DB_mysqli_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. }
  80. if (array_key_exists('CONSTRAINT', $attributes))
  81. {
  82. $sql .= '('.$attributes['CONSTRAINT'].')';
  83. }
  84. if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
  85. {
  86. $sql .= ' UNSIGNED';
  87. }
  88. if (array_key_exists('DEFAULT', $attributes))
  89. {
  90. $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
  91. }
  92. if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
  93. {
  94. $sql .= ' NULL';
  95. }
  96. else
  97. {
  98. $sql .= ' NOT NULL';
  99. }
  100. if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
  101. {
  102. $sql .= ' AUTO_INCREMENT';
  103. }
  104. }
  105. // don't add a comma on the end of the last field
  106. if (++$current_field_count < count($fields))
  107. {
  108. $sql .= ',';
  109. }
  110. }
  111. return $sql;
  112. }
  113. // --------------------------------------------------------------------
  114. /**
  115. * Create Table
  116. *
  117. * @access private
  118. * @param string the table name
  119. * @param mixed the fields
  120. * @param mixed primary key(s)
  121. * @param mixed key(s)
  122. * @param boolean should 'IF NOT EXISTS' be added to the SQL
  123. * @return bool
  124. */
  125. function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
  126. {
  127. $sql = 'CREATE TABLE ';
  128. if ($if_not_exists === TRUE)
  129. {
  130. $sql .= 'IF NOT EXISTS ';
  131. }
  132. $sql .= $this->db->_escape_identifiers($table)." (";
  133. $sql .= $this->_process_fields($fields);
  134. if (count($primary_keys) > 0)
  135. {
  136. $key_name = $this->db->_protect_identifiers(implode('_', $primary_keys));
  137. $primary_keys = $this->db->_protect_identifiers($primary_keys);
  138. $sql .= ",\n\tPRIMARY KEY ".$key_name." (" . implode(', ', $primary_keys) . ")";
  139. }
  140. if (is_array($keys) && count($keys) > 0)
  141. {
  142. foreach ($keys as $key)
  143. {
  144. if (is_array($key))
  145. {
  146. $key_name = $this->db->_protect_identifiers(implode('_', $key));
  147. $key = $this->db->_protect_identifiers($key);
  148. }
  149. else
  150. {
  151. $key_name = $this->db->_protect_identifiers($key);
  152. $key = array($key_name);
  153. }
  154. $sql .= ",\n\tKEY {$key_name} (" . implode(', ', $key) . ")";
  155. }
  156. }
  157. $sql .= "\n) DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};";
  158. return $sql;
  159. }
  160. // --------------------------------------------------------------------
  161. /**
  162. * Drop Table
  163. *
  164. * @access private
  165. * @return string
  166. */
  167. function _drop_table($table)
  168. {
  169. return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table);
  170. }
  171. // --------------------------------------------------------------------
  172. /**
  173. * Alter table query
  174. *
  175. * Generates a platform-specific query so that a table can be altered
  176. * Called by add_column(), drop_column(), and column_alter(),
  177. *
  178. * @access private
  179. * @param string the ALTER type (ADD, DROP, CHANGE)
  180. * @param string the column name
  181. * @param array fields
  182. * @param string the field after which we should add the new field
  183. * @return object
  184. */
  185. function _alter_table($alter_type, $table, $fields, $after_field = '')
  186. {
  187. $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ";
  188. // DROP has everything it needs now.
  189. if ($alter_type == 'DROP')
  190. {
  191. return $sql.$this->db->_protect_identifiers($fields);
  192. }
  193. $sql .= $this->_process_fields($fields);
  194. if ($after_field != '')
  195. {
  196. $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
  197. }
  198. return $sql;
  199. }
  200. // --------------------------------------------------------------------
  201. /**
  202. * Rename a table
  203. *
  204. * Generates a platform-specific query so that a table can be renamed
  205. *
  206. * @access private
  207. * @param string the old table name
  208. * @param string the new table name
  209. * @return string
  210. */
  211. function _rename_table($table_name, $new_table_name)
  212. {
  213. $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
  214. return $sql;
  215. }
  216. }
  217. /* End of file mysqli_forge.php */
  218. /* Location: ./system/database/drivers/mysqli/mysqli_forge.php */