| 
									
										
										
										
											2010-02-24 12:08:53 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @copyright NetMonsters <team@netmonsters.ru> | 
					
						
							|  |  |  |  * @link http://netmonsters.ru | 
					
						
							|  |  |  |  * @package Majestic | 
					
						
							|  |  |  |  * @subpackage db | 
					
						
							|  |  |  |  * @since 2010-02-16 | 
					
						
							|  |  |  |  * @version SVN: $Id$ | 
					
						
							|  |  |  |  * @filesource $URL$ | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Db | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     const FETCH_ASSOC = 2; | 
					
						
							|  |  |  |     const FETCH_NUM   = 3; | 
					
						
							|  |  |  |     const FETCH_BOTH  = 4; | 
					
						
							|  |  |  |     const FETCH_OBJ   = 5; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Databases connections | 
					
						
							|  |  |  |      *  | 
					
						
							|  |  |  |      * @var array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     static protected $connections = array(); | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Connect to database | 
					
						
							|  |  |  |      *  | 
					
						
							|  |  |  |      * @param string $name Database name. If not set 'default' will be used. | 
					
						
							|  |  |  |      * @param array $config Configuration array. | 
					
						
							|  |  |  |      *                        | 
					
						
							|  |  |  |      * @return DbDriver | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |      * @throws InitializationException | 
					
						
							| 
									
										
										
										
											2010-02-24 12:08:53 +00:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2010-03-05 18:41:02 +00:00
										 |  |  |     static public function connect($name = 'default', $config = null) | 
					
						
							| 
									
										
										
										
											2010-02-24 12:08:53 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-03-05 18:41:02 +00:00
										 |  |  |         if (!isset(self::$connections[$name])) { | 
					
						
							| 
									
										
										
										
											2010-02-24 12:08:53 +00:00
										 |  |  |             if (!$config) { | 
					
						
							| 
									
										
										
										
											2011-12-01 19:36:04 +04:00
										 |  |  |                 if (!is_object(Config::get(__CLASS__))) { | 
					
						
							| 
									
										
										
										
											2011-12-01 17:37:47 +04:00
										 |  |  |                      throw new InitializationException('Trying to get property of non-object'); | 
					
						
							| 
									
										
										
										
											2011-11-30 13:26:36 +04:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2010-03-05 18:41:02 +00:00
										 |  |  |                 $config = Config::get(__CLASS__)->$name; | 
					
						
							| 
									
										
										
										
											2010-02-24 12:08:53 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |                  | 
					
						
							| 
									
										
										
										
											2010-03-05 18:41:02 +00:00
										 |  |  |             if (!is_array($config)) { | 
					
						
							| 
									
										
										
										
											2011-11-30 13:26:36 +04:00
										 |  |  |                 throw new InitializationException('Connection parameters must be an array'); | 
					
						
							| 
									
										
										
										
											2010-02-24 12:08:53 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |              | 
					
						
							|  |  |  |             $driver = 'MySQLiDriver'; | 
					
						
							|  |  |  |             if (isset($config['driver'])) { | 
					
						
							|  |  |  |                 $driver = $config['driver']; | 
					
						
							|  |  |  |                 unset($config['driver']); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |              | 
					
						
							|  |  |  |             $connection = new $driver($config); | 
					
						
							|  |  |  |              | 
					
						
							| 
									
										
										
										
											2010-03-05 18:41:02 +00:00
										 |  |  |             if (!$connection instanceof DbDriver) { | 
					
						
							| 
									
										
										
										
											2011-11-30 13:26:36 +04:00
										 |  |  |                 throw new InitializationException('Database driver must extends DbDriver'); | 
					
						
							| 
									
										
										
										
											2010-02-24 12:08:53 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |             self::$connections[$name] = $connection; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return self::$connections[$name]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |