blob: d2afce9821af8627fb7f1d979286b3c8ebe03454 [file] [log] [blame]
admin61c57172006-10-06 17:29:24 +00001<?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 */
admin9a661812006-10-16 19:02:48 +000025function &DB($params = '', $active_record = FALSE)
admin61c57172006-10-06 17:29:24 +000026{
admin61c57172006-10-06 17:29:24 +000027 // 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
49 // we need to dynamically create a class that extends proper parent class
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
60 if ( ! isset($params['active_r']) OR $params['active_r'] == TRUE)
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';
admin9a661812006-10-16 19:02:48 +000081 $DB =& new $driver($params);
admin0aef2222006-10-12 18:00:22 +000082 return $DB;
admin61c57172006-10-06 17:29:24 +000083}
84
85
86?>