DB_forge.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * Database Utility Class
  19. *
  20. * @category Database
  21. * @author EllisLab Dev Team
  22. * @link http://codeigniter.com/user_guide/database/
  23. */
  24. class CI_DB_forge {
  25. var $fields = array();
  26. var $keys = array();
  27. var $primary_keys = array();
  28. var $db_char_set = '';
  29. /**
  30. * Constructor
  31. *
  32. * Grabs the CI super object instance so we can access it.
  33. *
  34. */
  35. function __construct()
  36. {
  37. // Assign the main database object to $this->db
  38. $CI =& get_instance();
  39. $this->db =& $CI->db;
  40. log_message('debug', "Database Forge Class Initialized");
  41. }
  42. // --------------------------------------------------------------------
  43. /**
  44. * Create database
  45. *
  46. * @access public
  47. * @param string the database name
  48. * @return bool
  49. */
  50. function create_database($db_name)
  51. {
  52. $sql = $this->_create_database($db_name);
  53. if (is_bool($sql))
  54. {
  55. return $sql;
  56. }
  57. return $this->db->query($sql);
  58. }
  59. // --------------------------------------------------------------------
  60. /**
  61. * Drop database
  62. *
  63. * @access public
  64. * @param string the database name
  65. * @return bool
  66. */
  67. function drop_database($db_name)
  68. {
  69. $sql = $this->_drop_database($db_name);
  70. if (is_bool($sql))
  71. {
  72. return $sql;
  73. }
  74. return $this->db->query($sql);
  75. }
  76. // --------------------------------------------------------------------
  77. /**
  78. * Add Key
  79. *
  80. * @access public
  81. * @param string key
  82. * @param string type
  83. * @return void
  84. */
  85. function add_key($key = '', $primary = FALSE)
  86. {
  87. if (is_array($key))
  88. {
  89. foreach ($key as $one)
  90. {
  91. $this->add_key($one, $primary);
  92. }
  93. return;
  94. }
  95. if ($key == '')
  96. {
  97. show_error('Key information is required for that operation.');
  98. }
  99. if ($primary === TRUE)
  100. {
  101. $this->primary_keys[] = $key;
  102. }
  103. else
  104. {
  105. $this->keys[] = $key;
  106. }
  107. }
  108. // --------------------------------------------------------------------
  109. /**
  110. * Add Field
  111. *
  112. * @access public
  113. * @param string collation
  114. * @return void
  115. */
  116. function add_field($field = '')
  117. {
  118. if ($field == '')
  119. {
  120. show_error('Field information is required.');
  121. }
  122. if (is_string($field))
  123. {
  124. if ($field == 'id')
  125. {
  126. $this->add_field(array(
  127. 'id' => array(
  128. 'type' => 'INT',
  129. 'constraint' => 9,
  130. 'auto_increment' => TRUE
  131. )
  132. ));
  133. $this->add_key('id', TRUE);
  134. }
  135. else
  136. {
  137. if (strpos($field, ' ') === FALSE)
  138. {
  139. show_error('Field information is required for that operation.');
  140. }
  141. $this->fields[] = $field;
  142. }
  143. }
  144. if (is_array($field))
  145. {
  146. $this->fields = array_merge($this->fields, $field);
  147. }
  148. }
  149. // --------------------------------------------------------------------
  150. /**
  151. * Create Table
  152. *
  153. * @access public
  154. * @param string the table name
  155. * @return bool
  156. */
  157. function create_table($table = '', $if_not_exists = FALSE)
  158. {
  159. if ($table == '')
  160. {
  161. show_error('A table name is required for that operation.');
  162. }
  163. if (count($this->fields) == 0)
  164. {
  165. show_error('Field information is required.');
  166. }
  167. $sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists);
  168. $this->_reset();
  169. return $this->db->query($sql);
  170. }
  171. // --------------------------------------------------------------------
  172. /**
  173. * Drop Table
  174. *
  175. * @access public
  176. * @param string the table name
  177. * @return bool
  178. */
  179. function drop_table($table_name)
  180. {
  181. $sql = $this->_drop_table($this->db->dbprefix.$table_name);
  182. if (is_bool($sql))
  183. {
  184. return $sql;
  185. }
  186. return $this->db->query($sql);
  187. }
  188. // --------------------------------------------------------------------
  189. /**
  190. * Rename Table
  191. *
  192. * @access public
  193. * @param string the old table name
  194. * @param string the new table name
  195. * @return bool
  196. */
  197. function rename_table($table_name, $new_table_name)
  198. {
  199. if ($table_name == '' OR $new_table_name == '')
  200. {
  201. show_error('A table name is required for that operation.');
  202. }
  203. $sql = $this->_rename_table($this->db->dbprefix.$table_name, $this->db->dbprefix.$new_table_name);
  204. return $this->db->query($sql);
  205. }
  206. // --------------------------------------------------------------------
  207. /**
  208. * Column Add
  209. *
  210. * @access public
  211. * @param string the table name
  212. * @param string the column name
  213. * @param string the column definition
  214. * @return bool
  215. */
  216. function add_column($table = '', $field = array(), $after_field = '')
  217. {
  218. if ($table == '')
  219. {
  220. show_error('A table name is required for that operation.');
  221. }
  222. // add field info into field array, but we can only do one at a time
  223. // so we cycle through
  224. foreach ($field as $k => $v)
  225. {
  226. $this->add_field(array($k => $field[$k]));
  227. if (count($this->fields) == 0)
  228. {
  229. show_error('Field information is required.');
  230. }
  231. $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field);
  232. $this->_reset();
  233. if ($this->db->query($sql) === FALSE)
  234. {
  235. return FALSE;
  236. }
  237. }
  238. return TRUE;
  239. }
  240. // --------------------------------------------------------------------
  241. /**
  242. * Column Drop
  243. *
  244. * @access public
  245. * @param string the table name
  246. * @param string the column name
  247. * @return bool
  248. */
  249. function drop_column($table = '', $column_name = '')
  250. {
  251. if ($table == '')
  252. {
  253. show_error('A table name is required for that operation.');
  254. }
  255. if ($column_name == '')
  256. {
  257. show_error('A column name is required for that operation.');
  258. }
  259. $sql = $this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name);
  260. return $this->db->query($sql);
  261. }
  262. // --------------------------------------------------------------------
  263. /**
  264. * Column Modify
  265. *
  266. * @access public
  267. * @param string the table name
  268. * @param string the column name
  269. * @param string the column definition
  270. * @return bool
  271. */
  272. function modify_column($table = '', $field = array())
  273. {
  274. if ($table == '')
  275. {
  276. show_error('A table name is required for that operation.');
  277. }
  278. // add field info into field array, but we can only do one at a time
  279. // so we cycle through
  280. foreach ($field as $k => $v)
  281. {
  282. // If no name provided, use the current name
  283. if ( ! isset($field[$k]['name']))
  284. {
  285. $field[$k]['name'] = $k;
  286. }
  287. $this->add_field(array($k => $field[$k]));
  288. if (count($this->fields) == 0)
  289. {
  290. show_error('Field information is required.');
  291. }
  292. $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);
  293. $this->_reset();
  294. if ($this->db->query($sql) === FALSE)
  295. {
  296. return FALSE;
  297. }
  298. }
  299. return TRUE;
  300. }
  301. // --------------------------------------------------------------------
  302. /**
  303. * Reset
  304. *
  305. * Resets table creation vars
  306. *
  307. * @access private
  308. * @return void
  309. */
  310. function _reset()
  311. {
  312. $this->fields = array();
  313. $this->keys = array();
  314. $this->primary_keys = array();
  315. }
  316. }
  317. /* End of file DB_forge.php */
  318. /* Location: ./system/database/DB_forge.php */