/convert/system/database/DB.php

https://github.com/usagi-project/mynets1 · PHP · 123 lines · 68 code · 21 blank · 34 comment · 20 complexity · 8d483ade8460d1378da7092e659159c1 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) 2006, 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 = FALSE)
  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. else if (!is_array($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['host'], 1)) : ''
  61. );
  62. }
  63. // No DB specified yet? Beat them senseless...
  64. if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
  65. {
  66. show_error('You have not selected a database type to connect to.');
  67. }
  68. // Load the DB classes. Note: Since the active record class is optional
  69. // we need to dynamically create a class that extends proper parent class
  70. // based on whether we're using the active record class or not.
  71. // Kudos to Paul for discovering this clever use of eval()
  72. if ($active_record_override == TRUE)
  73. {
  74. $active_record = TRUE;
  75. }
  76. require_once(BASEPATH.'database/DB_driver'.EXT);
  77. if (! isset($active_record) OR $active_record == TRUE)
  78. {
  79. require_once(BASEPATH.'database/DB_active_rec'.EXT);
  80. if ( ! class_exists('CI_DB'))
  81. {
  82. eval('class CI_DB extends CI_DB_active_record { }');
  83. }
  84. }
  85. else
  86. {
  87. if ( ! class_exists('CI_DB'))
  88. {
  89. eval('class CI_DB extends CI_DB_driver { }');
  90. }
  91. }
  92. require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT);
  93. // Instantiate the DB adapter
  94. $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
  95. $DB =& new $driver($params);
  96. if ($DB->autoinit == TRUE)
  97. {
  98. $DB->initialize();
  99. }
  100. return $DB;
  101. }
  102. ?>