PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/system/database/DB_forge.php

https://bitbucket.org/code_noodle/scratchpad
PHP | 438 lines | 248 code | 63 blank | 127 comment | 36 complexity | c3da2fc2958c13920df58b7aabc880ed 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. if ( ! empty($this->db->data_cache['db_names']))
  69. {
  70. $this->db->data_cache['db_names'][] = $db_name;
  71. }
  72. return TRUE;
  73. }
  74. // --------------------------------------------------------------------
  75. /**
  76. * Drop database
  77. *
  78. * @param string the database name
  79. * @return bool
  80. */
  81. public function drop_database($db_name)
  82. {
  83. if ($db_name === '')
  84. {
  85. show_error('A table name is required for that operation.');
  86. return FALSE;
  87. }
  88. elseif ($this->_drop_database === FALSE)
  89. {
  90. return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
  91. }
  92. elseif ( ! $this->db->query(sprintf($this->_drop_database, $db_name)))
  93. {
  94. return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE;
  95. }
  96. if ( ! empty($this->db->data_cache['db_names']))
  97. {
  98. $key = array_search(strtolower($db_name), array_map('strtolower', $this->db->data_cache['db_names']), TRUE);
  99. if ($key !== FALSE)
  100. {
  101. unset($this->db->data_cache['db_names'][$key]);
  102. }
  103. }
  104. return TRUE;
  105. }
  106. // --------------------------------------------------------------------
  107. /**
  108. * Add Key
  109. *
  110. * @param string key
  111. * @param string type
  112. * @return object
  113. */
  114. public function add_key($key = '', $primary = FALSE)
  115. {
  116. if (is_array($key))
  117. {
  118. foreach ($key as $one)
  119. {
  120. $this->add_key($one, $primary);
  121. }
  122. return;
  123. }
  124. if ($key === '')
  125. {
  126. show_error('Key information is required for that operation.');
  127. }
  128. if ($primary === TRUE)
  129. {
  130. $this->primary_keys[] = $key;
  131. }
  132. else
  133. {
  134. $this->keys[] = $key;
  135. }
  136. return $this;
  137. }
  138. // --------------------------------------------------------------------
  139. /**
  140. * Add Field
  141. *
  142. * @param string collation
  143. * @return object
  144. */
  145. public function add_field($field = '')
  146. {
  147. if ($field === '')
  148. {
  149. show_error('Field information is required.');
  150. }
  151. if (is_string($field))
  152. {
  153. if ($field === 'id')
  154. {
  155. $this->add_field(array(
  156. 'id' => array(
  157. 'type' => 'INT',
  158. 'constraint' => 9,
  159. 'auto_increment' => TRUE
  160. )
  161. ));
  162. $this->add_key('id', TRUE);
  163. }
  164. else
  165. {
  166. if (strpos($field, ' ') === FALSE)
  167. {
  168. show_error('Field information is required for that operation.');
  169. }
  170. $this->fields[] = $field;
  171. }
  172. }
  173. if (is_array($field))
  174. {
  175. $this->fields = array_merge($this->fields, $field);
  176. }
  177. return $this;
  178. }
  179. // --------------------------------------------------------------------
  180. /**
  181. * Create Table
  182. *
  183. * @param string the table name
  184. * @return bool
  185. */
  186. public function create_table($table = '', $if_not_exists = FALSE)
  187. {
  188. if ($table === '')
  189. {
  190. show_error('A table name is required for that operation.');
  191. }
  192. if (count($this->fields) === 0)
  193. {
  194. show_error('Field information is required.');
  195. }
  196. $sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists);
  197. $this->_reset();
  198. if (is_bool($sql))
  199. {
  200. return $sql;
  201. }
  202. if (($result = $this->db->query($sql)) !== FALSE && ! empty($this->db->data_cache['table_names']))
  203. {
  204. $this->db->data_cache['table_names'][] = $this->db->dbprefix.$table;
  205. }
  206. return $result;
  207. }
  208. // --------------------------------------------------------------------
  209. /**
  210. * Drop Table
  211. *
  212. * @param string the table name
  213. * @return bool
  214. */
  215. public function drop_table($table_name)
  216. {
  217. if ($table_name === '')
  218. {
  219. return ($this->db->db_debug) ? $this->db->display_error('db_table_name_required') : FALSE;
  220. }
  221. elseif ($this->_drop_table === FALSE)
  222. {
  223. return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
  224. }
  225. $result = $this->db->query(sprintf($this->_drop_table, $this->db->escape_identifiers($this->db->dbprefix.$table_name)));
  226. // Update table list cache
  227. if ($result && ! empty($this->db->data_cache['table_names']))
  228. {
  229. $key = array_search(strtolower($this->db->dbprefix.$table_name), array_map('strtolower', $this->db->data_cache['table_names']), TRUE);
  230. if ($key !== FALSE)
  231. {
  232. unset($this->db->data_cache['table_names'][$key]);
  233. }
  234. }
  235. return $result;
  236. }
  237. // --------------------------------------------------------------------
  238. /**
  239. * Rename Table
  240. *
  241. * @param string the old table name
  242. * @param string the new table name
  243. * @return bool
  244. */
  245. public function rename_table($table_name, $new_table_name)
  246. {
  247. if ($table_name === '' OR $new_table_name === '')
  248. {
  249. show_error('A table name is required for that operation.');
  250. return FALSE;
  251. }
  252. elseif ($this->_rename_table === FALSE)
  253. {
  254. return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
  255. }
  256. $result = $this->db->query(sprintf($this->_rename_table,
  257. $this->db->escape_identifiers($this->db->dbprefix.$table_name),
  258. $this->db->escape_identifiers($this->db->dbprefix.$new_table_name))
  259. );
  260. if ($result && ! empty($this->db->data_cache['table_names']))
  261. {
  262. $key = array_search(strtolower($this->db->dbprefix.$table_name), array_map('strtolower', $this->db->data_cache['table_names']), TRUE);
  263. if ($key !== FALSE)
  264. {
  265. $this->db->data_cache['table_names'][$key] = $this->db->dbprefix.$new_table_name;
  266. }
  267. }
  268. return $result;
  269. }
  270. // --------------------------------------------------------------------
  271. /**
  272. * Column Add
  273. *
  274. * @param string the table name
  275. * @param string the column name
  276. * @param string the column definition
  277. * @return bool
  278. */
  279. public function add_column($table = '', $field = array(), $after_field = '')
  280. {
  281. if ($table === '')
  282. {
  283. show_error('A table name is required for that operation.');
  284. }
  285. // add field info into field array, but we can only do one at a time
  286. // so we cycle through
  287. foreach (array_keys($field) as $k)
  288. {
  289. $this->add_field(array($k => $field[$k]));
  290. if (count($this->fields) === 0)
  291. {
  292. show_error('Field information is required.');
  293. }
  294. $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field);
  295. $this->_reset();
  296. if ($this->db->query($sql) === FALSE)
  297. {
  298. return FALSE;
  299. }
  300. }
  301. return TRUE;
  302. }
  303. // --------------------------------------------------------------------
  304. /**
  305. * Column Drop
  306. *
  307. * @param string the table name
  308. * @param string the column name
  309. * @return bool
  310. */
  311. public function drop_column($table = '', $column_name = '')
  312. {
  313. if ($table === '')
  314. {
  315. show_error('A table name is required for that operation.');
  316. }
  317. if ($column_name === '')
  318. {
  319. show_error('A column name is required for that operation.');
  320. }
  321. return $this->db->query($this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name));
  322. }
  323. // --------------------------------------------------------------------
  324. /**
  325. * Column Modify
  326. *
  327. * @param string the table name
  328. * @param string the column name
  329. * @param string the column definition
  330. * @return bool
  331. */
  332. public function modify_column($table = '', $field = array())
  333. {
  334. if ($table === '')
  335. {
  336. show_error('A table name is required for that operation.');
  337. }
  338. // add field info into field array, but we can only do one at a time
  339. // so we cycle through
  340. foreach (array_keys($field) as $k)
  341. {
  342. // If no name provided, use the current name
  343. if ( ! isset($field[$k]['name']))
  344. {
  345. $field[$k]['name'] = $k;
  346. }
  347. $this->add_field(array($k => $field[$k]));
  348. if (count($this->fields) === 0)
  349. {
  350. show_error('Field information is required.');
  351. }
  352. $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);
  353. $this->_reset();
  354. if ($this->db->query($sql) === FALSE)
  355. {
  356. return FALSE;
  357. }
  358. }
  359. return TRUE;
  360. }
  361. // --------------------------------------------------------------------
  362. /**
  363. * Reset
  364. *
  365. * Resets table creation vars
  366. *
  367. * @return void
  368. */
  369. protected function _reset()
  370. {
  371. $this->fields = $this->keys = $this->primary_keys = array();
  372. }
  373. }
  374. /* End of file DB_forge.php */
  375. /* Location: ./system/database/DB_forge.php */