/halogy/database/drivers/mysqli/mysqli_forge.php

https://bitbucket.org/haloweb/halogy-1.0/ · PHP · 254 lines · 119 code · 40 blank · 95 comment · 21 complexity · 1c70901e08fa2d9e1dfdf61e00ade20b MD5 · raw file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2009, 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))
  93. {
  94. $sql .= ($attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL';
  95. }
  96. if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
  97. {
  98. $sql .= ' AUTO_INCREMENT';
  99. }
  100. }
  101. // don't add a comma on the end of the last field
  102. if (++$current_field_count < count($fields))
  103. {
  104. $sql .= ',';
  105. }
  106. }
  107. return $sql;
  108. }
  109. // --------------------------------------------------------------------
  110. /**
  111. * Create Table
  112. *
  113. * @access private
  114. * @param string the table name
  115. * @param mixed the fields
  116. * @param mixed primary key(s)
  117. * @param mixed key(s)
  118. * @param boolean should 'IF NOT EXISTS' be added to the SQL
  119. * @return bool
  120. */
  121. function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
  122. {
  123. $sql = 'CREATE TABLE ';
  124. if ($if_not_exists === TRUE)
  125. {
  126. $sql .= 'IF NOT EXISTS ';
  127. }
  128. $sql .= $this->db->_escape_identifiers($table)." (";
  129. $sql .= $this->_process_fields($fields);
  130. if (count($primary_keys) > 0)
  131. {
  132. $key_name = $this->db->_protect_identifiers(implode('_', $primary_keys));
  133. $primary_keys = $this->db->_protect_identifiers($primary_keys);
  134. $sql .= ",\n\tPRIMARY KEY ".$key_name." (" . implode(', ', $primary_keys) . ")";
  135. }
  136. if (is_array($keys) && count($keys) > 0)
  137. {
  138. foreach ($keys as $key)
  139. {
  140. if (is_array($key))
  141. {
  142. $key_name = $this->db->_protect_identifiers(implode('_', $key));
  143. $key = $this->db->_protect_identifiers($key);
  144. }
  145. else
  146. {
  147. $key_name = $this->db->_protect_identifiers($key);
  148. $key = array($key_name);
  149. }
  150. $sql .= ",\n\tKEY {$key_name} (" . implode(', ', $key) . ")";
  151. }
  152. }
  153. $sql .= "\n) DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};";
  154. return $sql;
  155. }
  156. // --------------------------------------------------------------------
  157. /**
  158. * Drop Table
  159. *
  160. * @access private
  161. * @return string
  162. */
  163. function _drop_table($table)
  164. {
  165. return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table);
  166. }
  167. // --------------------------------------------------------------------
  168. /**
  169. * Alter table query
  170. *
  171. * Generates a platform-specific query so that a table can be altered
  172. * Called by add_column(), drop_column(), and column_alter(),
  173. *
  174. * @access private
  175. * @param string the ALTER type (ADD, DROP, CHANGE)
  176. * @param string the column name
  177. * @param array fields
  178. * @param string the field after which we should add the new field
  179. * @return object
  180. */
  181. function _alter_table($alter_type, $table, $fields, $after_field = '')
  182. {
  183. $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ";
  184. // DROP has everything it needs now.
  185. if ($alter_type == 'DROP')
  186. {
  187. return $sql.$this->db->_protect_identifiers($fields);
  188. }
  189. $sql .= $this->_process_fields($fields);
  190. if ($after_field != '')
  191. {
  192. $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
  193. }
  194. return $sql;
  195. }
  196. // --------------------------------------------------------------------
  197. /**
  198. * Rename a table
  199. *
  200. * Generates a platform-specific query so that a table can be renamed
  201. *
  202. * @access private
  203. * @param string the old table name
  204. * @param string the new table name
  205. * @return string
  206. */
  207. function _rename_table($table_name, $new_table_name)
  208. {
  209. $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
  210. return $sql;
  211. }
  212. }
  213. /* End of file mysqli_forge.php */
  214. /* Location: ./system/database/drivers/mysqli/mysqli_forge.php */