developer.jelix.org is not used any more and exists only for history. Post new tickets on the Github account.
developer.jelix.org n'est plus utilisée, et existe uniquement pour son historique. Postez les nouveaux tickets sur le compte github.

Opened 13 years ago

Closed 13 years ago

Last modified 12 years ago

#410 closed new feature (fixed)

Création de profils virtuels dans jDb

Reported by: Torgan Owned by: laurentj
Priority: normal Milestone: Jelix 1.1 beta 1
Component: jelix:db Version: 1.0
Severity: normal Keywords: db virtual profile
Cc: Blocked By:
Blocking: Documentation needed: no
Hosting Provider: Php version:

Description

Il peut être intéressant de créer des profils à la volée dans jDb. Voici un jDb patché permettant de le faire grâce à la méthode createVirtualProfile. Ce qui a changé :

  • Rajout d'un attribut protégé statique $_profils
  • Modification de la méthode getProfils en conséquence.
  • Création de la méthode statique createVirtualProfile

La mise en place de cette version de jDb (qui finalement change bien peu de choses) nécessite la création de deux locales supplémentaires :

  • jelix~db.error.virtual.profil.no.name
  • jelix~db.error.virtual.profil.invalid.params

Code final de la classe jDb :

class jDb{
        
        static private $_profils =  null;
        
        public static function getConnection($name = null){
                static $cnxPool = array();
                $profil = self::getProfil($name);
                if(!isset($cnxPool[$name])){
                   $cnxPool[$name] = self::_createConnector($profil);
                }
                return $cnxPool[$name];
        }
        public static function getDbWidget($name=null){
                $dbw = new jDbWidget(self::getConnection($name));
                return $dbw;
        }
        public static function getTools($name=null){
                $profil = self::getProfil($name);
                $driver = $profil['driver'];
                if($driver == 'pdo'){
                   preg_match('/^(\w+)\:.*$/',$profil['dsn'], $m);
                   $driver = $m[1];
                }
                global $gJConfig;
                require_once($gJConfig->_pluginsPathList_db[$profil['driver']].$profil['driver'].'.dbtools.php');
                $class = $profil['driver'].'DbTools';
                $cnx = self::getConnection($name);
                $tools = new $class($cnx);
                return $tools;
        }
        public static function getProfil($name='', $nameIsProfilType=false){
                global $gJConfig;
                if(self::$_profils === null){
                   self::$_profils = parse_ini_file(JELIX_APP_CONFIG_PATH.$gJConfig->dbProfils , true);
                }
                if($name == ''){
                        if(isset(self::$_profils['default']))
                                $name=self::$_profils['default'];
                        else
                                throw new jException('jelix~db.error.default.profil.unknow');
                }elseif($nameIsProfilType){
                        if(isset(self::$_profils[$name]) && is_string(self::$_profils[$name])){
                                $name = self::$_profils[$name];
                        }else{
                                throw new jException('jelix~db.error.profil.type.unknow',$name);
                        }
                }
                if(isset(self::$_profils[$name]) && is_array(self::$_profils[$name])){
                   self::$_profils[$name]['name'] = $name;
                   return self::$_profils[$name];
                }else{
                   throw new jException('jelix~db.error.profil.unknow',$name);
                }
        }
        public function testProfil($profil){
                try{
                        self::_createConnector($profil);
                        $ok = true;
                }catch(Exception $e){
                   $ok = false;
                }
                return $ok;
        }
        private static function _createConnector($profil){
                if($profil['driver'] == 'pdo'){
                        $dbh = new jDbPDOConnection($profil);
                        return $dbh;
                }else{
                        global $gJConfig;
                        $p = $gJConfig->_pluginsPathList_db[$profil['driver']].$profil['driver'];
                        require_once($p.'.dbconnection.php');
                        require_once($p.'.dbresultset.php');
                        $class = $profil['driver'].'DbConnection';
                        $dbh = new $class($profil);
                        return $dbh;
                }
        }
        
        public static function createVirtualProfile ($name = null, $params)
        {
                if (is_null ($name))
                {
                   throw new jException('jelix~db.error.virtual.profil.no.name');
                }
                
                if (! is_array ($params))
                {
                   throw new jException('jelix~db.error.virtual.profil.invalid.params');
                }
                
                if (self::$_profils === null)
                {
                        self::$_profils = 
                                parse_ini_file (
                                        JELIX_APP_CONFIG_PATH . $gJConfig->dbProfils,
                                        true);
                }
                
                self::$_profils[$name] = $params;
                
                print_r (self::$_profils);
        }               
}

Attachments (1)

410.diff (9.3 KB) - added by laurentj 13 years ago.
the updated patch

Download all attachments as: .zip

Change History (8)

comment:1 Changed 13 years ago by Torgan

Le diff :

158a159,161
> 	
> 	static private $_profils =  null;
> 	
186d188
< 		static $profils = null;
188,189c190,191
< 		if($profils === null){
< 		   $profils = parse_ini_file(JELIX_APP_CONFIG_PATH.$gJConfig->dbProfils , true);
---
> 		if(self::$_profils === null){
> 		   self::$_profils = parse_ini_file(JELIX_APP_CONFIG_PATH.$gJConfig->dbProfils , true);
192,193c194,195
< 			if(isset($profils['default']))
< 				$name=$profils['default'];
---
> 			if(isset(self::$_profils['default']))
> 				$name=self::$_profils['default'];
197,198c199,200
< 			if(isset($profils[$name]) && is_string($profils[$name])){
< 				$name = $profils[$name];
---
> 			if(isset(self::$_profils[$name]) && is_string(self::$_profils[$name])){
> 				$name = self::$_profils[$name];
203,205c205,207
< 		if(isset($profils[$name]) && is_array($profils[$name])){
< 		   $profils[$name]['name'] = $name;
< 		   return $profils[$name];
---
> 		if(isset(self::$_profils[$name]) && is_array(self::$_profils[$name])){
> 		   self::$_profils[$name]['name'] = $name;
> 		   return self::$_profils[$name];
232a235,257
> 	
> 	public static function createVirtualProfile ($name = null, $params)
> 	{
> 		if (is_null ($name))
> 		{
> 		   throw new jException('jelix~db.error.virtual.profil.no.name');
> 		}
> 		
> 		if (! is_array ($params))
> 		{
> 		   throw new jException('jelix~db.error.virtual.profil.invalid.params');
> 		}
> 		
> 		if (self::$_profils === null)
> 		{
> 			self::$_profils = 
> 				parse_ini_file (
> 					JELIX_APP_CONFIG_PATH . $gJConfig->dbProfils,
> 					true);
> 		}
> 		
> 		self::$_profils[$name] = $params;
> 	}		

comment:2 Changed 13 years ago by Torgan

Et maintenant un diff de notre temps :)

--- jDb.class.php.orig	2008-01-09 16:11:13.000000000 +0100
+++ jDb.class.php.new	2008-01-09 16:12:00.000000000 +0100
@@ -156,6 +156,9 @@
 	 }
 }
 class jDb{
+	
+	static private $_profils =  null;
+	
 	public static function getConnection($name = null){
 		static $cnxPool = array();
 		$profil = self::getProfil($name);
@@ -183,26 +186,25 @@
 		return $tools;
 	}
 	public static function getProfil($name='', $nameIsProfilType=false){
-		static $profils = null;
 		global $gJConfig;
-		if($profils === null){
-		   $profils = parse_ini_file(JELIX_APP_CONFIG_PATH.$gJConfig->dbProfils , true);
+		if(self::$_profils === null){
+		   self::$_profils = parse_ini_file(JELIX_APP_CONFIG_PATH.$gJConfig->dbProfils , true);
 		}
 		if($name == ''){
-			if(isset($profils['default']))
-				$name=$profils['default'];
+			if(isset(self::$_profils['default']))
+				$name=self::$_profils['default'];
 			else
 				throw new jException('jelix~db.error.default.profil.unknow');
 		}elseif($nameIsProfilType){
-			if(isset($profils[$name]) && is_string($profils[$name])){
-				$name = $profils[$name];
+			if(isset(self::$_profils[$name]) && is_string(self::$_profils[$name])){
+				$name = self::$_profils[$name];
 			}else{
 				throw new jException('jelix~db.error.profil.type.unknow',$name);
 			}
 		}
-		if(isset($profils[$name]) && is_array($profils[$name])){
-		   $profils[$name]['name'] = $name;
-		   return $profils[$name];
+		if(isset(self::$_profils[$name]) && is_array(self::$_profils[$name])){
+		   self::$_profils[$name]['name'] = $name;
+		   return self::$_profils[$name];
 		}else{
 		   throw new jException('jelix~db.error.profil.unknow',$name);
 		}
@@ -230,5 +232,28 @@
 			return $dbh;
 		}
 	}
+	
+	public static function createVirtualProfile ($name = null, $params)
+	{
+		if (is_null ($name))
+		{
+		   throw new jException('jelix~db.error.virtual.profil.no.name');
+		}
+		
+		if (! is_array ($params))
+		{
+		   throw new jException('jelix~db.error.virtual.profil.invalid.params');
+		}
+		
+		if (self::$_profils === null)
+		{
+			self::$_profils = 
+				parse_ini_file (
+					JELIX_APP_CONFIG_PATH . $gJConfig->dbProfils,
+					true);
+		}
+		
+		self::$_profils[$name] = $params;
+	}		
 }
 ?>

comment:3 Changed 13 years ago by laurentj

  • Milestone set to Jelix 1.1
  • Owner set to laurentj
  • Status changed from new to assigned

comment:4 Changed 13 years ago by laurentj

  • review set to review?

Changed 13 years ago by laurentj

the updated patch

comment:5 Changed 13 years ago by laurentj

  • Resolution set to fixed
  • review changed from review? to review+
  • Status changed from assigned to closed

Ok. Patch landed in the trunk. svn 779.

comment:6 Changed 13 years ago by laurentj

  • Documentation needed set

comment:7 Changed 12 years ago by laurentj

  • Documentation needed unset
Note: See TracTickets for help on using tickets.