PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/system/database/DB_forge.php

https://gitlab.com/fredec/ionizecms-1.0.8.x
PHP | 382 lines | 186 code | 65 blank | 131 comment | 37 complexity | dddf428a7c8d2264a6c4f284d0b7fb88 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 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, 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 (is_array($key))
  87. {
  88. foreach ($key as $one)
  89. {
  90. $this->add_key($one, $primary);
  91. }
  92. return;
  93. }
  94. if ($key == '')
  95. {
  96. show_error('Key information is required for that operation.');
  97. }
  98. if ($primary === TRUE)
  99. {
  100. $this->primary_keys[] = $key;
  101. }
  102. else
  103. {
  104. $this->keys[] = $key;
  105. }
  106. }
  107. // --------------------------------------------------------------------
  108. /**
  109. * Add Field
  110. *
  111. * @access public
  112. * @param string collation
  113. * @return void
  114. */
  115. function add_field($field = '')
  116. {
  117. if ($field == '')
  118. {
  119. show_error('Field information is required.');
  120. }
  121. if (is_string($field))
  122. {
  123. if ($field == 'id')
  124. {
  125. $this->add_field(array(
  126. 'id' => array(
  127. 'type' => 'INT',
  128. 'constraint' => 9,
  129. 'auto_increment' => TRUE
  130. )
  131. ));
  132. $this->add_key('id', TRUE);
  133. }
  134. else
  135. {
  136. if (strpos($field, ' ') === FALSE)
  137. {
  138. show_error('Field information is required for that operation.');
  139. }
  140. $this->fields[] = $field;
  141. }
  142. }
  143. if (is_array($field))
  144. {
  145. $this->fields = array_merge($this->fields, $field);
  146. }
  147. }
  148. // --------------------------------------------------------------------
  149. /**
  150. * Create Table
  151. *
  152. * @access public
  153. * @param string the table name
  154. * @return bool
  155. */
  156. function create_table($table = '', $if_not_exists = FALSE)
  157. {
  158. if ($table == '')
  159. {
  160. show_error('A table name is required for that operation.');
  161. }
  162. if (count($this->fields) == 0)
  163. {
  164. show_error('Field information is required.');
  165. }
  166. $sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists);
  167. $this->_reset();
  168. return $this->db->query($sql);
  169. }
  170. // --------------------------------------------------------------------
  171. /**
  172. * Drop Table
  173. *
  174. * @access public
  175. * @param string the table name
  176. * @return bool
  177. */
  178. function drop_table($table_name)
  179. {
  180. $sql = $this->_drop_table($this->db->dbprefix.$table_name);
  181. if (is_bool($sql))
  182. {
  183. return $sql;
  184. }
  185. return $this->db->query($sql);
  186. }
  187. // --------------------------------------------------------------------
  188. /**
  189. * Rename Table
  190. *
  191. * @access public
  192. * @param string the old table name
  193. * @param string the new table name
  194. * @return bool
  195. */
  196. function rename_table($table_name, $new_table_name)
  197. {
  198. if ($table_name == '' OR $new_table_name == '')
  199. {
  200. show_error('A table name is required for that operation.');
  201. }
  202. $sql = $this->_rename_table($table_name, $new_table_name);
  203. return $this->db->query($sql);
  204. }
  205. // --------------------------------------------------------------------
  206. /**
  207. * Column Add
  208. *
  209. * @access public
  210. * @param string the table name
  211. * @param string the column name
  212. * @param string the column definition
  213. * @return bool
  214. */
  215. function add_column($table = '', $field = array(), $after_field = '')
  216. {
  217. if ($table == '')
  218. {
  219. show_error('A table name is required for that operation.');
  220. }
  221. // add field info into field array, but we can only do one at a time
  222. // so we cycle through
  223. foreach ($field as $k => $v)
  224. {
  225. $this->add_field(array($k => $field[$k]));
  226. if (count($this->fields) == 0)
  227. {
  228. show_error('Field information is required.');
  229. }
  230. $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field);
  231. $this->_reset();
  232. if ($this->db->query($sql) === FALSE)
  233. {
  234. return FALSE;
  235. }
  236. }
  237. return TRUE;
  238. }
  239. // --------------------------------------------------------------------
  240. /**
  241. * Column Drop
  242. *
  243. * @access public
  244. * @param string the table name
  245. * @param string the column name
  246. * @return bool
  247. */
  248. function drop_column($table = '', $column_name = '')
  249. {
  250. if ($table == '')
  251. {
  252. show_error('A table name is required for that operation.');
  253. }
  254. if ($column_name == '')
  255. {
  256. show_error('A column name is required for that operation.');
  257. }
  258. $sql = $this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name);
  259. return $this->db->query($sql);
  260. }
  261. // --------------------------------------------------------------------
  262. /**
  263. * Column Modify
  264. *
  265. * @access public
  266. * @param string the table name
  267. * @param string the column name
  268. * @param string the column definition
  269. * @return bool
  270. */
  271. function modify_column($table = '', $field = array())
  272. {
  273. if ($table == '')
  274. {
  275. show_error('A table name is required for that operation.');
  276. }
  277. // add field info into field array, but we can only do one at a time
  278. // so we cycle through
  279. foreach ($field as $k => $v)
  280. {
  281. // If no name provided, use the current name
  282. if ( ! isset($field[$k]['name']))
  283. {
  284. $field[$k]['name'] = $k;
  285. }
  286. $this->add_field(array($k => $field[$k]));
  287. if (count($this->fields) == 0)
  288. {
  289. show_error('Field information is required.');
  290. }
  291. $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);
  292. $this->_reset();
  293. if ($this->db->query($sql) === FALSE)
  294. {
  295. return FALSE;
  296. }
  297. }
  298. return TRUE;
  299. }
  300. // --------------------------------------------------------------------
  301. /**
  302. * Reset
  303. *
  304. * Resets table creation vars
  305. *
  306. * @access private
  307. * @return void
  308. */
  309. function _reset()
  310. {
  311. $this->fields = array();
  312. $this->keys = array();
  313. $this->primary_keys = array();
  314. }
  315. }
  316. /* End of file DB_forge.php */
  317. /* Location: ./system/database/DB_forge.php */