Ticket #410 (closed new feature: fixed)

Opened 8 months ago

Last modified 6 months ago

Création de profils virtuels dans jDb

Reported by: Torgan Assigned to: laurentj
Priority: normal Milestone: Jelix 1.1 beta 1
Component: jelix:db Version: 1.0
Severity: normal Keywords: db virtual profile
Cc: Php version:
Review: review+ Hosting Provider:
Documentation needed: 1 Blocking:

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

410.diff (9.3 kB) - added by laurentj on 02/26/08 16:34:10.
the updated patch

Change History

01/09/08 16:13:17 changed 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;
> 	}		

01/09/08 16:22:08 changed 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;
+	}		
 }
 ?>

01/18/08 22:45:26 changed by laurentj

  • status changed from new to assigned.
  • owner set to laurentj.
  • milestone set to Jelix 1.1.

02/15/08 23:52:35 changed by laurentj

  • review set to review?.

02/26/08 16:34:10 changed by laurentj

  • attachment 410.diff added.

the updated patch

02/26/08 16:36:05 changed by laurentj

  • status changed from assigned to closed.
  • review changed from review? to review+.
  • resolution set to fixed.

Ok. Patch landed in the trunk. svn 779.

02/27/08 14:17:23 changed by laurentj

  • docneeded set to 1.
Download in other formats: Comma-delimited Text Tab-delimited Text RSS Feed