/system/database/DB_forge.php

https://github.com/andigehle/CodeIgniter · PHP · 390 lines · 210 code · 54 blank · 126 comment · 36 complexity · 3d2795b930a98a9d07edb62402f34059 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 5.2.4 or newer
  6. *
  7. * NOTICE OF LICENSE
  8. *
  9. * Licensed under the Open Software License version 3.0
  10. *
  11. * This source file is subject to the Open Software License (OSL 3.0) that is
  12. * bundled with this package in the files license.txt / license.rst. It is
  13. * also available through the world wide web at this URL:
  14. * http://opensource.org/licenses/OSL-3.0
  15. * If you did not receive a copy of the license and are unable to obtain it
  16. * through the world wide web, please send an email to
  17. * licensing@ellislab.com so we can send you a copy immediately.
  18. *
  19. * @package CodeIgniter
  20. * @author EllisLab Dev Team
  21. * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
  22. * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
  23. * @link http://codeigniter.com
  24. * @since Version 1.0
  25. * @filesource
  26. */
  27. /**
  28. * Database Forge Class
  29. *
  30. * @category Database
  31. * @author EllisLab Dev Team
  32. * @link http://codeigniter.com/user_guide/database/
  33. */
  34. abstract class CI_DB_forge {
  35. public $fields = array();
  36. public $keys = array();
  37. public $primary_keys = array();
  38. public $db_char_set = '';
  39. // Platform specific SQL strings
  40. protected $_create_database = 'CREATE DATABASE %s';
  41. protected $_drop_database = 'DROP DATABASE %s';
  42. protected $_drop_table = 'DROP TABLE IF EXISTS %s';
  43. protected $_rename_table = 'ALTER TABLE %s RENAME TO %s';
  44. public function __construct()
  45. {
  46. // Assign the main database object to $this->db
  47. $CI =& get_instance();
  48. $this->db =& $CI->db;
  49. log_message('debug', 'Database Forge Class Initialized');
  50. }
  51. // --------------------------------------------------------------------
  52. /**
  53. * Create database
  54. *
  55. * @param string the database name
  56. * @return bool
  57. */
  58. public function create_database($db_name)
  59. {
  60. if ($this->_create_database === FALSE)
  61. {
  62. return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
  63. }
  64. elseif ( ! $this->db->query(sprintf($this->_create_database, $db_name, $this->db->char_set, $this->db->dbcollat)))
  65. {
  66. return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE;
  67. }
  68. return TRUE;
  69. }
  70. // --------------------------------------------------------------------
  71. /**
  72. * Drop database
  73. *
  74. * @param string the database name
  75. * @return bool
  76. */
  77. public function drop_database($db_name)
  78. {
  79. if ($db_name == '')
  80. {
  81. show_error('A table name is required for that operation.');
  82. return FALSE;
  83. }
  84. elseif ($this->_drop_database === FALSE)
  85. {
  86. return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
  87. }
  88. elseif ( ! $this->db->query(sprintf($this->_drop_database, $db_name)))
  89. {
  90. return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE;
  91. }
  92. return TRUE;
  93. }
  94. // --------------------------------------------------------------------
  95. /**
  96. * Add Key
  97. *
  98. * @param string key
  99. * @param string type
  100. * @return object
  101. */
  102. public function add_key($key = '', $primary = FALSE)
  103. {
  104. if (is_array($key))
  105. {
  106. foreach ($key as $one)
  107. {
  108. $this->add_key($one, $primary);
  109. }
  110. return;
  111. }
  112. if ($key == '')
  113. {
  114. show_error('Key information is required for that operation.');
  115. }
  116. if ($primary === TRUE)
  117. {
  118. $this->primary_keys[] = $key;
  119. }
  120. else
  121. {
  122. $this->keys[] = $key;
  123. }
  124. return $this;
  125. }
  126. // --------------------------------------------------------------------
  127. /**
  128. * Add Field
  129. *
  130. * @param string collation
  131. * @return object
  132. */
  133. public function add_field($field = '')
  134. {
  135. if ($field == '')
  136. {
  137. show_error('Field information is required.');
  138. }
  139. if (is_string($field))
  140. {
  141. if ($field === 'id')
  142. {
  143. $this->add_field(array(
  144. 'id' => array(
  145. 'type' => 'INT',
  146. 'constraint' => 9,
  147. 'auto_increment' => TRUE
  148. )
  149. ));
  150. $this->add_key('id', TRUE);
  151. }
  152. else
  153. {
  154. if (strpos($field, ' ') === FALSE)
  155. {
  156. show_error('Field information is required for that operation.');
  157. }
  158. $this->fields[] = $field;
  159. }
  160. }
  161. if (is_array($field))
  162. {
  163. $this->fields = array_merge($this->fields, $field);
  164. }
  165. return $this;
  166. }
  167. // --------------------------------------------------------------------
  168. /**
  169. * Create Table
  170. *
  171. * @param string the table name
  172. * @return bool
  173. */
  174. public function create_table($table = '', $if_not_exists = FALSE)
  175. {
  176. if ($table == '')
  177. {
  178. show_error('A table name is required for that operation.');
  179. }
  180. if (count($this->fields) === 0)
  181. {
  182. show_error('Field information is required.');
  183. }
  184. $sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists);
  185. $this->_reset();
  186. return is_bool($sql) ? $sql : $this->db->query($sql);
  187. }
  188. // --------------------------------------------------------------------
  189. /**
  190. * Drop Table
  191. *
  192. * @param string the table name
  193. * @return bool
  194. */
  195. public function drop_table($table_name)
  196. {
  197. if ($table_name == '')
  198. {
  199. return ($this->db->db_debug) ? $this->db->display_error('db_table_name_required') : FALSE;
  200. }
  201. elseif ($this->_drop_table === FALSE)
  202. {
  203. return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
  204. }
  205. return $this->db->query(sprintf($this->_drop_table, $this->db->escape_identifiers($this->db->dbprefix.$table_name)));
  206. }
  207. // --------------------------------------------------------------------
  208. /**
  209. * Rename Table
  210. *
  211. * @param string the old table name
  212. * @param string the new table name
  213. * @return bool
  214. */
  215. public function rename_table($table_name, $new_table_name)
  216. {
  217. if ($table_name == '' OR $new_table_name == '')
  218. {
  219. show_error('A table name is required for that operation.');
  220. return FALSE;
  221. }
  222. elseif ($this->_rename_table === FALSE)
  223. {
  224. return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
  225. }
  226. return $this->db->query(sprintf($this->_rename_table,
  227. $this->db->escape_identifiers($this->db->dbprefix.$table_name),
  228. $this->db->escape_identifiers($this->db->dbprefix.$new_table_name))
  229. );
  230. }
  231. // --------------------------------------------------------------------
  232. /**
  233. * Column Add
  234. *
  235. * @param string the table name
  236. * @param string the column name
  237. * @param string the column definition
  238. * @return bool
  239. */
  240. public function add_column($table = '', $field = array(), $after_field = '')
  241. {
  242. if ($table == '')
  243. {
  244. show_error('A table name is required for that operation.');
  245. }
  246. // add field info into field array, but we can only do one at a time
  247. // so we cycle through
  248. foreach (array_keys($field) as $k)
  249. {
  250. $this->add_field(array($k => $field[$k]));
  251. if (count($this->fields) == 0)
  252. {
  253. show_error('Field information is required.');
  254. }
  255. $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field);
  256. $this->_reset();
  257. if ($this->db->query($sql) === FALSE)
  258. {
  259. return FALSE;
  260. }
  261. }
  262. return TRUE;
  263. }
  264. // --------------------------------------------------------------------
  265. /**
  266. * Column Drop
  267. *
  268. * @param string the table name
  269. * @param string the column name
  270. * @return bool
  271. */
  272. public function drop_column($table = '', $column_name = '')
  273. {
  274. if ($table == '')
  275. {
  276. show_error('A table name is required for that operation.');
  277. }
  278. if ($column_name == '')
  279. {
  280. show_error('A column name is required for that operation.');
  281. }
  282. return $this->db->query($this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name));
  283. }
  284. // --------------------------------------------------------------------
  285. /**
  286. * Column Modify
  287. *
  288. * @param string the table name
  289. * @param string the column name
  290. * @param string the column definition
  291. * @return bool
  292. */
  293. public function modify_column($table = '', $field = array())
  294. {
  295. if ($table == '')
  296. {
  297. show_error('A table name is required for that operation.');
  298. }
  299. // add field info into field array, but we can only do one at a time
  300. // so we cycle through
  301. foreach (array_keys($field) as $k)
  302. {
  303. // If no name provided, use the current name
  304. if ( ! isset($field[$k]['name']))
  305. {
  306. $field[$k]['name'] = $k;
  307. }
  308. $this->add_field(array($k => $field[$k]));
  309. if (count($this->fields) === 0)
  310. {
  311. show_error('Field information is required.');
  312. }
  313. $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);
  314. $this->_reset();
  315. if ($this->db->query($sql) === FALSE)
  316. {
  317. return FALSE;
  318. }
  319. }
  320. return TRUE;
  321. }
  322. // --------------------------------------------------------------------
  323. /**
  324. * Reset
  325. *
  326. * Resets table creation vars
  327. *
  328. * @return void
  329. */
  330. protected function _reset()
  331. {
  332. $this->fields = $this->keys = $this->primary_keys = array();
  333. }
  334. }
  335. /* End of file DB_forge.php */
  336. /* Location: ./system/database/DB_forge.php */