Source for file jDb.class.php
Documentation is available at jDb.class.php
- <?php
- /**
- * @package jelix
- * @subpackage db
- * @author Laurent Jouanneau
- * @contributor Yannick Le Guédart, Laurent Raufaste
- * @copyright 2005-2011 Laurent Jouanneau
- *
- * API ideas of this class were get originally from the Copix project (CopixDbFactory, Copix 2.3dev20050901, http://www.copix.org)
- * No lines of code are copyrighted by CopixTeam
- *
- * @link http://www.jelix.org
- * @licence http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
- */
-
- /**
- *
- */
- require(JELIX_LIB_PATH.'db/jDbConnection.class.php');
- require(JELIX_LIB_PATH.'db/jDbResultSet.class.php');
-
- /**
- * factory for database connector and other db utilities
- * @package jelix
- * @subpackage db
- */
- class jDb {
-
- static private $_profiles = null;
- static private $_cnxPool = array();
-
- /**
- * return a database connector
- * Use a local pool.
- * @param string $name profile name to use. if empty, use the default one
- * @return jDbConnection connector
- */
- public static function getConnection ($name = null){
- $profile = self::getProfile ($name);
-
- if (!$name) {
- // we set the name to avoid two connection for a same profile, when it is the default profile
- // and when we call getConnection two times, one with no name and on with the name
- $name = $profile['name'];
- }
-
- if (!isset(self::$_cnxPool[$name])) {
- self::$_cnxPool[$name] = self::_createConnector($profile);
- }
- return self::$_cnxPool[$name];
- }
-
- /**
- * create a new jDbWidget
- * @param string $name profile name to use. if empty, use the default one
- * @return jDbWidget
- */
- public static function getDbWidget($name=null){
- $dbw = new jDbWidget(self::getConnection($name));
- return $dbw;
- }
-
- /**
- * instancy a jDbTools object
- * @param string $name profile name to use. if empty, use the default one
- * @return jDbTools
- */
- public static function getTools ($name=null){
- $profile = self::getProfile ($name);
-
- $driver = $profile['driver'];
-
- if($driver == 'pdo'){
- preg_match('/^(\w+)\:.*$/',$profile['dsn'], $m);
- $driver = $m[1];
- }
-
- global $gJConfig;
- if(!isset($gJConfig->_pluginsPathList_db[$driver])
- || !file_exists($gJConfig->_pluginsPathList_db[$driver]) ){
- throw new jException('jelix~db.error.driver.notfound', $driver);
- }
- require_once($gJConfig->_pluginsPathList_db[$driver].$driver.'.dbtools.php');
- $class = $driver.'DbTools';
-
- //Création de l'objet
- $cnx = self::getConnection ($name);
- $tools = new $class ($cnx);
- return $tools;
- }
-
- /**
- * load properties of a connector profile
- *
- * a profile is a section in the dbprofils.ini.php file
- *
- * with getProfile('myprofile') (or getProfile('myprofile', false)), you get the profile which
- * has the name "myprofile". this name should correspond to a section name in the ini file
- *
- * with getProfile('myprofiletype',true), it will search a parameter named 'myprofiletype' in the ini file.
- * This parameter should contains a profile name, and the corresponding profile will be loaded.
- *
- * with getProfile(), it will load the default profile, (so the profile of "default" type)
- *
- * @param string $name profile name or profile type to load. if empty, use the default one
- * @param boolean $nameIsProfileType says if the name is a profile name or a profile type. this parameter exists since 1.0b2
- * @return array properties
- */
- public static function getProfile ($name='', $nameIsProfileType=false){
- global $gJConfig;
- if(self::$_profiles === null){
- self::$_profiles = parse_ini_file(JELIX_APP_CONFIG_PATH.$gJConfig->dbProfils , true);
- }
-
- if($name == ''){
- if(isset(self::$_profiles['default']))
- $name=self::$_profiles['default'];
- else
- throw new jException('jelix~db.error.default.profile.unknow');
- }elseif($nameIsProfileType){
- if(isset(self::$_profiles[$name]) && is_string(self::$_profiles[$name])){
- $name = self::$_profiles[$name];
- }else{
- throw new jException('jelix~db.error.profile.type.unknow',$name);
- }
- }
-
- if(isset(self::$_profiles[$name]) && is_array(self::$_profiles[$name])){
- self::$_profiles[$name]['name'] = $name;
- return self::$_profiles[$name];
- }else{
- throw new jException('jelix~db.error.profile.unknow',$name);
- }
- }
-
- /**
- * DEPRECATED. same as getProfile
- * @deprecated
- */
- public static function getProfil ($name='', $nameIsProfileType=false){
- trigger_error("jDb::getProfil() is deprecated, you should use jDb::getProfile()", E_USER_NOTICE);
- return self::getProfile($name, $nameIsProfileType);
- }
-
- /**
- * call it to test a profile (during an install for example)
- * @param array $profile profile properties
- * @return boolean true if properties are ok
- */
- public function testProfile($profile){
- try{
- self::_createConnector ($profile);
- $ok = true;
- }catch(Exception $e){
- $ok = false;
- }
- return $ok;
- }
-
- /**
- * create a connector
- * @param array $profile profile properties
- * @return jDbConnection|jDbPDOConnection database connector
- */
- private static function _createConnector ($profile){
- if($profile['driver'] == 'pdo'){
- $dbh = new jDbPDOConnection($profile);
- return $dbh;
- }else{
- global $gJConfig;
- if(!isset($gJConfig->_pluginsPathList_db[$profile['driver']])
- || !file_exists($gJConfig->_pluginsPathList_db[$profile['driver']]) ){
- throw new jException('jelix~db.error.driver.notfound', $profile['driver']);
- }
- $p = $gJConfig->_pluginsPathList_db[$profile['driver']].$profile['driver'];
- require_once($p.'.dbconnection.php');
- require_once($p.'.dbresultset.php');
-
- //creating of the connection
- $class = $profile['driver'].'DbConnection';
- $dbh = new $class ($profile);
- return $dbh;
- }
- }
-
- public static function createVirtualProfile ($name, $params) {
- global $gJConfig;
- if ($name == '') {
- throw new jException('jelix~db.error.virtual.profile.no.name');
- }
-
- if (! is_array ($params)) {
- throw new jException('jelix~db.error.virtual.profile.invalid.params', $name);
- }
-
- if (self::$_profiles === null) {
- self::$_profiles = parse_ini_file (JELIX_APP_CONFIG_PATH . $gJConfig->dbProfils, true);
- }
- self::$_profiles[$name] = $params;
- unset (self::$_cnxPool[$name]);
- }
-
- /**
- * perform a convertion float to str. It takes care about the decimal separator
- * which should be a '.' for SQL. Because when doing a native convertion float->str,
- * PHP uses the local decimal separator, and so, we don't want that.
- * @since 1.1.11
- */
- public static function floatToStr($value) {
- if (is_float($value)) // this is a float
- return rtrim(sprintf('%.20F', $value), '0'); // %F to not format with the local decimal separator
- else if (is_integer($value))
- return sprintf('%d', $value);
- // this is probably a string, so we expect that it contains a numerical value
- // is_numeric is true if the separator is ok for SQL
- // (is_numeric doesn't accept thousand separators nor other character than '.' as decimal separator)
- else if (is_numeric($value))
- return $value;
-
- // we probably have a malformed float number here
- // if so, floatval will ignore all character after an invalid character (a ',' for example)
- // no warning, no exception here, to keep the same behavior of previous Jelix version
- // in order to no break stable applications.
- // FIXME: do a warning in next versions (> 1.2)
- return (string)(floatval($value));
- }
- }
Documentation generated on Thu, 22 Mar 2012 22:15:15 +0100 by phpDocumentor 1.4.3