DB.php 4.2 KB

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