Source for file jDb.class.php

Documentation is available at jDb.class.php

  1. <?php
  2. /**
  3. @package     jelix
  4. @subpackage  db
  5. @author      Laurent Jouanneau
  6. @contributor Yannick Le Guédart, Laurent Raufaste, Julien Issler
  7. @copyright   2005-2011 Laurent Jouanneau
  8. @copyright   2011 Julien Issler
  9. *
  10. *  API ideas of this class were get originally from the Copix project (CopixDbFactory, Copix 2.3dev20050901, http://www.copix.org)
  11. *  No lines of code are copyrighted by CopixTeam
  12. *
  13. @link      http://www.jelix.org
  14. @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  15. */
  16.  
  17. /**
  18.  *
  19.  */
  20. require(JELIX_LIB_PATH.'db/jDbConnection.class.php');
  21. require(JELIX_LIB_PATH.'db/jDbResultSet.class.php');
  22.  
  23. /**
  24.  * class that handles a sql query for a logger
  25.  */
  26. class jSQLLogMessage extends jLogMessage {
  27.  
  28.     protected $startTime = 0;
  29.     protected $endTime = 0;
  30.     protected $trace = array();
  31.  
  32.     public function __construct($message{
  33.         $this->category = 'sql';
  34.         $this->message = $message;
  35.         $this->startTime = microtime(true);
  36.  
  37.         $this->trace = debug_backtrace();
  38.         array_shift($this->trace)// remove the current __construct call
  39.     }
  40.  
  41.     public function endQuery({
  42.         $this->endTime = microtime(true);
  43.     }
  44.  
  45.     public function getTrace({
  46.         return $this->trace;
  47.     }
  48.  
  49.     public function getTime({
  50.         return $this->endTime - $this->startTime;
  51.     }
  52.  
  53.     public function getDao({
  54.         foreach ($this->trace as $t{
  55.             if (isset($t['class'])) {
  56.                 $dao '';
  57.                 $class $t['class'];
  58.                 if ($class == 'jDaoFactoryBase'{
  59.                     if (isset($t['object'])) {
  60.                         $class get_class($t['object']);
  61.                     }
  62.                     else {
  63.                         $class 'jDaoFactoryBase';
  64.                         $dao 'unknow dao, jDaoFactoryBase';
  65.                     }
  66.                 }
  67.                 if(preg_match('/^cDao_(.+)_Jx_(.+)_Jx_(.+)$/'$class$m)) {
  68.                     $dao $m[1].'~'.$m[2];
  69.                 }
  70.                 if ($dao && isset($t['function'])) {
  71.                     $dao.= '::'.$t['function'].'()';
  72.                 }
  73.                 if($dao)
  74.                     return $dao;
  75.             }
  76.         }
  77.         return '';
  78.     }
  79.  
  80.     public function getFormatedMessage({
  81.         $message $this->message."\n".$this->getTime().'ms';
  82.         $dao $this->getDao();
  83.         if ($dao)
  84.             $message.=', from dao:'.$dao."\n";
  85.  
  86.         $traceLog="";
  87.         foreach($this->trace as $k=>$t){
  88.             $traceLog.="\n\t$k\t".(isset($t['class'])?$t['class'].$t['type']:'').$t['function']."()\t";
  89.             $traceLog.=(isset($t['file'])?$t['file']:'[php]').' : '.(isset($t['line'])?$t['line']:'');
  90.         }
  91.  
  92.         return $message.$traceLog;
  93.     }
  94. }
  95.  
  96.  
  97. /**
  98.  * factory for database connector and other db utilities
  99.  * @package  jelix
  100.  * @subpackage db
  101.  */
  102. class jDb {
  103.  
  104.     /**
  105.     * return a database connector. It uses a temporay pool of connection to reuse
  106.     * currently opened connections.
  107.     *
  108.     * @param string  $name  profile name to use. if empty, use the default one
  109.     * @return jDbConnection  the connector
  110.     */
  111.     public static function getConnection ($name ''{
  112.         return jProfiles::getOrStoreInPool('jdb'$namearray('jDb''_createConnector'));
  113.     }
  114.  
  115.     /**
  116.      * create a new jDbWidget
  117.      * @param string  $name  profile name to use. if empty, use the default one
  118.      * @return jDbWidget 
  119.      */
  120.     public static function getDbWidget ($name null{
  121.         $dbw new jDbWidget(self::getConnection($name));
  122.         return $dbw;
  123.     }
  124.  
  125.     /**
  126.     * instancy a jDbTools object. Use jDbConnection::tools() instead.
  127.     * @param string $name profile name to use. if empty, use the default one
  128.     * @return jDbTools 
  129.     * @deprecated since 1.2
  130.     */
  131.     public static function getTools ($name null{
  132.         $cnx self::getConnection ($name);
  133.         return $cnx->tools();
  134.     }
  135.  
  136.     /**
  137.     * load properties of a connector profile
  138.     *
  139.     * a profile is a section in the profiles.ini.php file
  140.     *
  141.     * the given name can be a profile name (it should correspond to a section name
  142.     * in the ini file), or an alias of a profile. An alias is a parameter name
  143.     * in the global section of the ini file, and the value of this parameter
  144.     * should be a profile name.
  145.     *
  146.     * @param string   $name  profile name or alias of a profile name. if empty, use the default profile
  147.     * @param boolean  $noDefault  if true and if the profile doesn't exist, throw an error instead of getting the default profile
  148.     * @return array  properties
  149.     * @deprecated use jProfiles::get instead
  150.     */
  151.     public static function getProfile ($name=''$noDefault false{
  152.         return jProfiles::get('jdb'$name$noDefault);
  153.     }
  154.  
  155.     /**
  156.      * call it to test a profile (during an install for example)
  157.      * @param array  $profile  profile properties
  158.      * @return boolean  true if properties are ok
  159.      */
  160.     public function testProfile ($profile{
  161.         try {
  162.             self::_createConnector ($profile);
  163.             $ok true;
  164.         }
  165.         catch(Exception $e{
  166.             $ok false;
  167.         }
  168.         return $ok;
  169.     }
  170.  
  171.     /**
  172.     * create a connector. internal use (callback method for jProfiles)
  173.     * @param array  $profile  profile properties
  174.     * @return jDbConnection|jDbPDOConnection database connector
  175.     */
  176.     public static function _createConnector ($profile{
  177.         if ($profile['driver'== 'pdo' || (isset($profile['usepdo']&& $profile['usepdo'])) {
  178.             $dbh new jDbPDOConnection($profile);
  179.             return $dbh;
  180.         }
  181.         else {
  182.             $dbh jApp::loadPlugin($profile['driver']'db''.dbconnection.php'$profile['driver'].'DbConnection'$profile);
  183.             if (is_null($dbh))
  184.                 throw new jException('jelix~db.error.driver.notfound'$profile['driver']);
  185.             return $dbh;
  186.         }
  187.     }
  188.  
  189.     /**
  190.      * create a temporary new profile
  191.      * @param string $name the name of the profile
  192.      * @param array|string$params parameters of the profile. key=parameter name, value=parameter value.
  193.      *                       same kind of parameters we found in profiles.ini.php
  194.      *                       we can also indicate a name of an other profile, to create an alias
  195.      * @deprecated since 1.3, use jProfiles::createVirtualProfile instead
  196.      */
  197.     public static function createVirtualProfile ($name$params{
  198.         jProfiles::createVirtualProfile('jdb',$name$params);
  199.     }
  200.  
  201.     /**
  202.      * clear the loaded profiles to force to reload the db profiles file.
  203.      * WARNING: it closes all opened connections !
  204.      * @since 1.2
  205.      * @deprecated since 1.3, use jProfiles::clear instead
  206.      */
  207.     public static function clearProfiles({
  208.         jProfiles::clear();
  209.     }
  210.  
  211.     /**
  212.      * perform a convertion float to str. It takes care about the decimal separator
  213.      * which should be a '.' for SQL. Because when doing a native convertion float->str,
  214.      * PHP uses the local decimal separator, and so, we don't want that.
  215.      * @since 1.1.11
  216.      */
  217.     public static function floatToStr($value{
  218.         if (is_float($value)) // this is a float
  219.             return rtrim(sprintf('%.20F'$value)'0')// %F to not format with the local decimal separator
  220.         else if (is_integer($value))
  221.             return sprintf('%d'$value);
  222.         // this is probably a string, so we expect that it contains a numerical value
  223.         // is_numeric is true if the separator is ok for SQL
  224.         // (is_numeric doesn't accept thousand separators nor other character than '.' as decimal separator)
  225.         else if (is_numeric($value))
  226.             return $value;
  227.  
  228.         // we probably have a malformed float number here
  229.         // if so, floatval will ignore all character after an invalid character (a ',' for example)
  230.         // no warning, no exception here, to keep the same behavior of previous Jelix version
  231.         // in order to no break stable applications.
  232.         // FIXME: do a warning in next versions (> 1.2)
  233.         return (string)(floatval($value));
  234.     }
  235. }

Documentation generated on Mon, 19 Sep 2011 14:12:25 +0200 by phpDocumentor 1.4.3