pdo_forge.php 6.1 KB

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