/system/database/DB.php

https://github.com/guoguo/CodeIgniter · PHP · 174 lines · 94 code · 27 blank · 53 comment · 26 complexity · cdf7202f4801d5119c70d443e62dd558 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.1.6 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 - 2011, 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. /**
  29. * Initialize the database
  30. *
  31. * @category Database
  32. * @author EllisLab Dev Team
  33. * @link http://codeigniter.com/user_guide/database/
  34. * @param string
  35. * @param bool Determines if active record should be used or not
  36. */
  37. function &DB($params = '', $active_record_override = NULL)
  38. {
  39. // Load the DB config file if a DSN string wasn't passed
  40. if (is_string($params) AND strpos($params, '://') === FALSE)
  41. {
  42. // Is the config file in the environment folder?
  43. if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/database.php'))
  44. {
  45. if ( ! file_exists($file_path = APPPATH.'config/database.php'))
  46. {
  47. show_error('The configuration file database.php does not exist.');
  48. }
  49. }
  50. include($file_path);
  51. if ( ! isset($db) OR count($db) == 0)
  52. {
  53. show_error('No database connection settings were found in the database config file.');
  54. }
  55. if ($params != '')
  56. {
  57. $active_group = $params;
  58. }
  59. if ( ! isset($active_group) OR ! isset($db[$active_group]))
  60. {
  61. show_error('You have specified an invalid database connection group.');
  62. }
  63. $params = $db[$active_group];
  64. }
  65. elseif (is_string($params))
  66. {
  67. /* parse the URL from the DSN string
  68. * Database settings can be passed as discreet
  69. * parameters or as a data source name in the first
  70. * parameter. DSNs must have this prototype:
  71. * $dsn = 'driver://username:password@hostname/database';
  72. */
  73. if (($dns = @parse_url($params)) === FALSE)
  74. {
  75. show_error('Invalid DB Connection String');
  76. }
  77. $params = array(
  78. 'dbdriver' => $dns['scheme'],
  79. 'hostname' => (isset($dns['host'])) ? rawurldecode($dns['host']) : '',
  80. 'username' => (isset($dns['user'])) ? rawurldecode($dns['user']) : '',
  81. 'password' => (isset($dns['pass'])) ? rawurldecode($dns['pass']) : '',
  82. 'database' => (isset($dns['path'])) ? rawurldecode(substr($dns['path'], 1)) : ''
  83. );
  84. // were additional config items set?
  85. if (isset($dns['query']))
  86. {
  87. parse_str($dns['query'], $extra);
  88. foreach ($extra as $key => $val)
  89. {
  90. // booleans please
  91. if (strtoupper($val) == "TRUE")
  92. {
  93. $val = TRUE;
  94. }
  95. elseif (strtoupper($val) == "FALSE")
  96. {
  97. $val = FALSE;
  98. }
  99. $params[$key] = $val;
  100. }
  101. }
  102. }
  103. // No DB specified yet? Beat them senseless...
  104. if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
  105. {
  106. show_error('You have not selected a database type to connect to.');
  107. }
  108. // Load the DB classes. Note: Since the active record class is optional
  109. // we need to dynamically create a class that extends proper parent class
  110. // based on whether we're using the active record class or not.
  111. // Kudos to Paul for discovering this clever use of eval()
  112. if ($active_record_override !== NULL)
  113. {
  114. $active_record = $active_record_override;
  115. }
  116. require_once(BASEPATH.'database/DB_driver.php');
  117. if ( ! isset($active_record) OR $active_record == TRUE)
  118. {
  119. require_once(BASEPATH.'database/DB_active_rec.php');
  120. if ( ! class_exists('CI_DB'))
  121. {
  122. eval('class CI_DB extends CI_DB_active_record { }');
  123. }
  124. }
  125. else
  126. {
  127. if ( ! class_exists('CI_DB'))
  128. {
  129. eval('class CI_DB extends CI_DB_driver { }');
  130. }
  131. }
  132. require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver.php');
  133. // Instantiate the DB adapter
  134. $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
  135. $DB = new $driver($params);
  136. if ($DB->autoinit == TRUE)
  137. {
  138. $DB->initialize();
  139. }
  140. if (isset($params['stricton']) && $params['stricton'] == TRUE)
  141. {
  142. $DB->query('SET SESSION sql_mode="STRICT_ALL_TABLES"');
  143. }
  144. return $DB;
  145. }
  146. /* End of file DB.php */
  147. /* Location: ./system/database/DB.php */