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. |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 10 | * @license http://www.codeignitor.com/user_guide/license.html |
admin | 61c5717 | 2006-10-06 17:29:24 +0000 | [diff] [blame] | 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 | */ |
admin | 9a66181 | 2006-10-16 19:02:48 +0000 | [diff] [blame] | 25 | function &DB($params = '', $active_record = FALSE) |
admin | 61c5717 | 2006-10-06 17:29:24 +0000 | [diff] [blame] | 26 | { |
admin | 61c5717 | 2006-10-06 17:29:24 +0000 | [diff] [blame] | 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 | |
| 32 | $group = ($params == '') ? $active_group : $params; |
| 33 | |
| 34 | if ( ! isset($db[$group])) |
| 35 | { |
| 36 | show_error('You have specified an invalid database connection group: '.$group); |
| 37 | } |
| 38 | |
| 39 | $params = $db[$group]; |
| 40 | } |
| 41 | |
| 42 | // No DB specified yet? Beat them senseless... |
| 43 | if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '') |
| 44 | { |
| 45 | show_error('You have not selected a database type to connect to.'); |
| 46 | } |
| 47 | |
| 48 | // Load the DB classes. Note: Since the active record class is optional |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 49 | // we need to dynamically create a class that extends proper parent class |
admin | 61c5717 | 2006-10-06 17:29:24 +0000 | [diff] [blame] | 50 | // based on whether we're using the active record class or not. |
| 51 | // Kudos to Paul for discovering this clever use of eval() |
| 52 | |
| 53 | if ($active_record == TRUE) |
| 54 | { |
| 55 | $params['active_r'] = TRUE; |
| 56 | } |
| 57 | |
| 58 | require_once(BASEPATH.'database/DB_driver'.EXT); |
| 59 | |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 60 | if ( ! isset($params['active_r']) OR $params['active_r'] == TRUE) |
admin | 61c5717 | 2006-10-06 17:29:24 +0000 | [diff] [blame] | 61 | { |
| 62 | require_once(BASEPATH.'database/DB_active_rec'.EXT); |
| 63 | |
| 64 | if ( ! class_exists('CI_DB')) |
| 65 | { |
| 66 | eval('class CI_DB extends CI_DB_active_record { }'); |
| 67 | } |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | if ( ! class_exists('CI_DB')) |
| 72 | { |
| 73 | eval('class CI_DB extends CI_DB_driver { }'); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT); |
| 78 | |
| 79 | // Instantiate the DB adapter |
| 80 | $driver = 'CI_DB_'.$params['dbdriver'].'_driver'; |
admin | 9a66181 | 2006-10-16 19:02:48 +0000 | [diff] [blame] | 81 | $DB =& new $driver($params); |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 82 | return $DB; |
admin | 61c5717 | 2006-10-06 17:29:24 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
| 86 | ?> |