/system/database/DB_forge.php

https://github.com/mkhairul/Presta · PHP · 346 lines · 160 code · 56 blank · 130 comment · 33 complexity · dab6179ff385c672d77eb999a6a14c53 MD5 · raw file

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