]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/database/DBManagerFactory.php
Release 6.2.0
[Github/sugarcrm.git] / include / database / DBManagerFactory.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 /*********************************************************************************
39
40 * Description: This file generates the appropriate manager for the database
41
42 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
43 * All Rights Reserved.
44 * Contributor(s): ______________________________________..
45 ********************************************************************************/
46
47 require_once('include/database/DBManager.php');
48
49 class DBManagerFactory
50 {
51     /** 
52          * Returns a reference to the DB object for instance $instanceName, or the default 
53      * instance if one is not specified
54      *
55      * @param  string $instanceName optional, name of the instance
56      * @return object DBManager instance 
57      */
58         public static function getInstance(
59         $instanceName = ''
60         )
61     {
62         global $sugar_config, $dbinstances;
63         static $count, $old_count;
64
65         $instanceName = 'db';
66         $config = $sugar_config['dbconfig'];
67         if(!isset($dbinstances)){
68             $dbinstances = array();
69         }
70         //fall back to the default instance name
71         if(empty($sugar_config['db'][$instanceName])){
72                 $instanceName = '';
73         }
74         if(!isset($dbinstances[$instanceName])){
75             $my_db_manager = 'MysqlManager';
76             if( $config['db_type'] == "mysql" ) {
77                 if ((!isset($sugar_config['mysqli_disabled'])
78                             || $sugar_config['mysqli_disabled'] == false) 
79                     && function_exists('mysqli_connect')) {
80                     $my_db_manager = 'MysqliManager';
81                 }
82             }
83             if( $config['db_type'] == "oci8" ){
84             }
85             elseif( $config['db_type'] == "mssql" ){
86                 if ( function_exists('sqlsrv_connect')
87                         && (empty($config['db_mssql_force_driver']) || $config['db_mssql_force_driver'] == 'sqlsrv' ))
88                         $my_db_manager = 'SqlsrvManager';
89                 elseif (is_freetds() 
90                         && (empty($config['db_mssql_force_driver']) || $config['db_mssql_force_driver'] == 'freetds' ))
91                     $my_db_manager = 'FreeTDSManager';
92                 else
93                     $my_db_manager = 'MssqlManager';
94             }
95             $GLOBALS['log']->info("using $my_db_manager DBManager backend");
96             if(!empty($config['db_manager'])){
97                 $my_db_manager = $config['db_manager'];
98             }
99
100                 require_once("include/database/{$my_db_manager}.php");
101                 $dbinstances[$instanceName] = new $my_db_manager();
102                 $dbinstances[$instanceName]->getHelper();
103                 $dbinstances[$instanceName]->connect($config, true);
104                 $dbinstances[$instanceName]->count_id = $count;
105                 $dbinstances[$instanceName]->references = 0;
106                 $dbinstances[$instanceName]->getHelper()->db = $dbinstances[$instanceName];           
107         }
108         else {
109             $old_count++;
110             $dbinstances[$instanceName]->references = $old_count;
111         }
112         return $dbinstances[$instanceName];
113     }
114     
115     /**
116      * Returns an instance of the helper class
117      *
118      * @deprecated
119      * @return object DBHelper instance
120      */
121     public static function getHelperInstance()
122     {
123         $GLOBALS['log']->info('call to DBManagerFactory::getHelperInstance() is deprecated');
124         return self::getInstance()->getHelper();
125     }
126     
127     /**
128      * Loads the DBManager and DBHelper instance class files
129      *
130      * @deprecated
131      * @param string $class_name
132      */
133     public static function load_db_manager_class(
134         $class_name
135         )
136     {
137         $GLOBALS['log']->info('call to DBManagerFactory::load_db_manager_class() is deprecated');
138         if( is_file("include/database/{$class_name}.php") && !class_exists($class_name))
139             require_once("include/database/{$class_name}.php");
140         
141         $class_name = str_ireplace('Manager','Helper',$class_name);
142         
143         if( is_file("include/database/{$class_name}.php") && !class_exists($class_name))
144             require_once("include/database/{$class_name}.php");
145     }
146                 
147 }
148
149 ?>