Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 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
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 12 | * @since Version 1.0
|
| 13 | * @filesource
|
| 14 | */
|
| 15 |
|
| 16 | // ------------------------------------------------------------------------
|
| 17 |
|
| 18 | /**
|
| 19 | * Initialize the database
|
| 20 | *
|
| 21 | * @category Database
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 22 | * @author ExpressionEngine Dev Team
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com/user_guide/database/
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 24 | */
|
Derek Jones | 75d568d | 2008-01-30 20:26:25 +0000 | [diff] [blame] | 25 | function &DB($params = '', $active_record_override = FALSE)
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 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 | include(APPPATH.'config/database'.EXT);
|
| 31 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 32 | if ( ! isset($db) OR count($db) == 0)
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 33 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 34 | show_error('No database connection settings were found in the database config file.');
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 35 | }
|
| 36 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 37 | if ($params != '')
|
| 38 | {
|
| 39 | $active_group = $params;
|
| 40 | }
|
| 41 |
|
| 42 | if ( ! isset($active_group) OR ! isset($db[$active_group]))
|
| 43 | {
|
| 44 | show_error('You have specified an invalid database connection group.');
|
| 45 | }
|
| 46 |
|
| 47 | $params = $db[$active_group];
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 48 | }
|
Derek Jones | 63eac3e | 2008-02-14 20:23:37 +0000 | [diff] [blame] | 49 | elseif (is_string($params))
|
Derek Jones | 7dd3838 | 2008-02-13 04:52:58 +0000 | [diff] [blame] | 50 | {
|
| 51 |
|
| 52 | /* parse the URL from the DSN string
|
| 53 | * Database settings can be passed as discreet
|
| 54 | * parameters or as a data source name in the first
|
| 55 | * parameter. DSNs must have this prototype:
|
| 56 | * $dsn = 'driver://username:password@hostname/database';
|
| 57 | */
|
| 58 |
|
| 59 | if (($dns = @parse_url($params)) === FALSE)
|
| 60 | {
|
| 61 | show_error('Invalid DB Connection String');
|
| 62 | }
|
| 63 |
|
| 64 | $params = array(
|
| 65 | 'dbdriver' => $dns['scheme'],
|
| 66 | 'hostname' => (isset($dns['host'])) ? rawurldecode($dns['host']) : '',
|
| 67 | 'username' => (isset($dns['user'])) ? rawurldecode($dns['user']) : '',
|
| 68 | 'password' => (isset($dns['pass'])) ? rawurldecode($dns['pass']) : '',
|
| 69 | 'database' => (isset($dns['path'])) ? rawurldecode(substr($dns['host'], 1)) : ''
|
| 70 | );
|
| 71 | }
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 72 |
|
| 73 | // No DB specified yet? Beat them senseless...
|
| 74 | if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
|
| 75 | {
|
| 76 | show_error('You have not selected a database type to connect to.');
|
| 77 | }
|
| 78 |
|
| 79 | // Load the DB classes. Note: Since the active record class is optional
|
| 80 | // we need to dynamically create a class that extends proper parent class
|
| 81 | // based on whether we're using the active record class or not.
|
| 82 | // Kudos to Paul for discovering this clever use of eval()
|
| 83 |
|
Derek Jones | 75d568d | 2008-01-30 20:26:25 +0000 | [diff] [blame] | 84 | if ($active_record_override == TRUE)
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 85 | {
|
Derek Jones | 75d568d | 2008-01-30 20:26:25 +0000 | [diff] [blame] | 86 | $active_record = TRUE;
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 87 | }
|
| 88 |
|
| 89 | require_once(BASEPATH.'database/DB_driver'.EXT);
|
| 90 |
|
Derek Jones | 4e1241b | 2008-01-30 21:00:33 +0000 | [diff] [blame] | 91 | if (! isset($active_record) OR $active_record == TRUE)
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 92 | {
|
| 93 | require_once(BASEPATH.'database/DB_active_rec'.EXT);
|
| 94 |
|
| 95 | if ( ! class_exists('CI_DB'))
|
| 96 | {
|
| 97 | eval('class CI_DB extends CI_DB_active_record { }');
|
| 98 | }
|
| 99 | }
|
| 100 | else
|
| 101 | {
|
| 102 | if ( ! class_exists('CI_DB'))
|
| 103 | {
|
| 104 | eval('class CI_DB extends CI_DB_driver { }');
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT);
|
| 109 |
|
| 110 | // Instantiate the DB adapter
|
| 111 | $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 112 | $DB =& new $driver($params);
|
| 113 |
|
| 114 | if ($DB->autoinit == TRUE)
|
| 115 | {
|
| 116 | $DB->initialize();
|
| 117 | }
|
| 118 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 119 | return $DB;
|
| 120 | }
|
| 121 |
|
| 122 |
|
admin | 61c5717 | 2006-10-06 17:29:24 +0000 | [diff] [blame] | 123 | ?> |