blob: b059e76a7285c934963165c4166a2a9fd517e02e [file] [log] [blame]
Derek Allardd2df9bc2007-04-15 17:41:17 +00001<?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 Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Allard6838f002007-10-04 19:29:59 +000010 * @license http://www.codeigniter.com/user_guide/license.html
Derek Allardd2df9bc2007-04-15 17:41:17 +000011 * @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
Derek Allard3d879d52008-01-18 19:41:32 +000022 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +000023 * @link http://www.codeigniter.com/user_guide/database/
24 */
25function &DB($params = '', $active_record = FALSE)
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 Allard39b622d2008-01-16 21:10:09 +000032 if ( ! isset($db) OR count($db) == 0)
Derek Allardd2df9bc2007-04-15 17:41:17 +000033 {
Derek Allard39b622d2008-01-16 21:10:09 +000034 show_error('No database connection settings were found in the database config file.');
Derek Allardd2df9bc2007-04-15 17:41:17 +000035 }
36
Derek Allard39b622d2008-01-16 21:10:09 +000037 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 Allardd2df9bc2007-04-15 17:41:17 +000048 }
49
50 // No DB specified yet? Beat them senseless...
51 if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
52 {
53 show_error('You have not selected a database type to connect to.');
54 }
55
56 // Load the DB classes. Note: Since the active record class is optional
57 // we need to dynamically create a class that extends proper parent class
58 // based on whether we're using the active record class or not.
59 // Kudos to Paul for discovering this clever use of eval()
60
61 if ($active_record == TRUE)
62 {
63 $params['active_r'] = TRUE;
64 }
65
66 require_once(BASEPATH.'database/DB_driver'.EXT);
67
68 if ( ! isset($params['active_r']) OR $params['active_r'] == TRUE)
69 {
70 require_once(BASEPATH.'database/DB_active_rec'.EXT);
71
72 if ( ! class_exists('CI_DB'))
73 {
74 eval('class CI_DB extends CI_DB_active_record { }');
75 }
76 }
77 else
78 {
79 if ( ! class_exists('CI_DB'))
80 {
81 eval('class CI_DB extends CI_DB_driver { }');
82 }
83 }
84
85 require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT);
86
87 // Instantiate the DB adapter
88 $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
Derek Allard39b622d2008-01-16 21:10:09 +000089 $DB =& new $driver($params);
90
91 if ($DB->autoinit == TRUE)
92 {
93 $DB->initialize();
94 }
95
Derek Allardd2df9bc2007-04-15 17:41:17 +000096 return $DB;
97}
98
99
admin61c57172006-10-06 17:29:24 +0000100?>