/system/database/DB.php

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