admin | 61c5717 | 2006-10-06 17:29:24 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Initialize the database |
| 20 | * |
| 21 | * @category Database |
| 22 | * @author Rick Ellis |
| 23 | * @link http://www.codeigniter.com/user_guide/database/ |
| 24 | */ |
| 25 | function DB($params = '', $return = FALSE, $active_record = FALSE) |
| 26 | { |
admin | 61c5717 | 2006-10-06 17:29:24 +0000 | [diff] [blame] | 27 | // Do we even need to load the database class? |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 28 | if (class_exists('CI_DB') AND $return == FALSE AND $active_record == FALSE) |
admin | 61c5717 | 2006-10-06 17:29:24 +0000 | [diff] [blame] | 29 | { |
| 30 | return FALSE; |
| 31 | } |
| 32 | |
| 33 | // Load the DB config file if a DSN string wasn't passed |
| 34 | if (is_string($params) AND strpos($params, '://') === FALSE) |
| 35 | { |
| 36 | include(APPPATH.'config/database'.EXT); |
| 37 | |
| 38 | $group = ($params == '') ? $active_group : $params; |
| 39 | |
| 40 | if ( ! isset($db[$group])) |
| 41 | { |
| 42 | show_error('You have specified an invalid database connection group: '.$group); |
| 43 | } |
| 44 | |
| 45 | $params = $db[$group]; |
| 46 | } |
| 47 | |
| 48 | // No DB specified yet? Beat them senseless... |
| 49 | if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '') |
| 50 | { |
| 51 | show_error('You have not selected a database type to connect to.'); |
| 52 | } |
| 53 | |
| 54 | // Load the DB classes. Note: Since the active record class is optional |
| 55 | // we need to dynamically create a class that extends proper parent class |
| 56 | // based on whether we're using the active record class or not. |
| 57 | // Kudos to Paul for discovering this clever use of eval() |
| 58 | |
| 59 | if ($active_record == TRUE) |
| 60 | { |
| 61 | $params['active_r'] = TRUE; |
| 62 | } |
| 63 | |
| 64 | require_once(BASEPATH.'database/DB_driver'.EXT); |
| 65 | |
| 66 | if ( ! isset($params['active_r']) OR $params['active_r'] == TRUE) |
| 67 | { |
| 68 | require_once(BASEPATH.'database/DB_active_rec'.EXT); |
| 69 | |
| 70 | if ( ! class_exists('CI_DB')) |
| 71 | { |
| 72 | eval('class CI_DB extends CI_DB_active_record { }'); |
| 73 | } |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | if ( ! class_exists('CI_DB')) |
| 78 | { |
| 79 | eval('class CI_DB extends CI_DB_driver { }'); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT); |
| 84 | |
| 85 | // Instantiate the DB adapter |
| 86 | $driver = 'CI_DB_'.$params['dbdriver'].'_driver'; |
| 87 | $DB = new $driver($params); |
| 88 | |
| 89 | if ($return === TRUE) |
| 90 | { |
| 91 | return $DB; |
| 92 | } |
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 93 | |
| 94 | $CI =& get_instance(); |
| 95 | $CI->db =& $DB; |
admin | 61c5717 | 2006-10-06 17:29:24 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
| 99 | ?> |