DB.php 4.1 KB

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