linea21-core
[ class tree: linea21-core ] [ index: linea21-core ] [ all elements ]

Source for file class.mysql.php

Documentation is available at class.mysql.php

  1. <?php
  2. /**
  3.  * @package linea21.core
  4.  * @subpackage system
  5.  * @author linea21 <info@linea21.com>
  6.  * @version $id SVN
  7.  * @access public
  8.  * @license http://opensource.org/licenses/gpl-3.0.html
  9.  *  MySQL data manipulation
  10.  */
  11.  
  12. class mysql {
  13.   /*
  14.    * @parameters
  15.    */
  16.   var $DB_HOST = DB_HOST;
  17.   var $DB_USER = DB_USER;
  18.   var $DB_PASS = DB_PASS;
  19.   var $DB_NAME = DB_NAME;
  20.   var $DB_LINK = "";
  21.   var $DB_ADMINMAIL = DB_ADMINMAIL;
  22.   var $RESULT;
  23.   var $DEBUG;
  24.   protected $dispatcher = null;
  25.   
  26.   public function __construct()
  27.   {
  28.       $this->dispatcher = $GLOBALS['dispatcher'];
  29.   }
  30.  
  31.  
  32.   /**
  33.    * mysql::DBInitialise()
  34.    * Initialisation de la connexion
  35.    *
  36.    * @access public
  37.    * @param string $user : utilisateur base de données
  38.    * @param string $pass : password utilisateur base de données
  39.    * @param string $serveur : serveur de base de données
  40.    * @param string $bdd : nom de la base de données
  41.    * @return boolean 
  42.    */
  43.  
  44.   function DBInitialise($user ''$pass ''$serveur ''$bdd '')
  45.   {
  46.       // Notify the end of the current method
  47.       $this->dispatcher->notify(new sfEvent($this'dbconnector.init'array('user' => $user'password' => $pass'host' => $serveur'db' => $bdd)));
  48.       
  49.     if ($user != ''{
  50.       $this->DB_USER = $user;
  51.     }
  52.     if ($pass != ''{
  53.       $this->DB_PASS = $pass;
  54.     }
  55.     if ($serveur != ''{
  56.       $this->DB_HOST = $serveur;
  57.     }
  58.     if ($bdd != ''{
  59.       $this->DB_NAME = $bdd;
  60.     }
  61.     return true;
  62.   }
  63.  
  64.   /**
  65.    * mysql::DBConnexion()
  66.    * connexion Base de données
  67.    *
  68.    * @access public
  69.    * @param none 
  70.    * @return boolean 
  71.    */
  72.  
  73.   function DBConnexion()
  74.   {
  75.  
  76.       // Notify the end of the current method
  77.       $this->dispatcher->notify(new sfEvent($this'dbconnector.connect'));
  78.        
  79.     $this->DB_LINK = @mysql_connect($this->DB_HOST$this->DB_USER$this->DB_PASSor die($this->DBError(_t('divers','sql_connect_failed')));
  80.     $this->DBSelectDB();
  81.  
  82.     if(defined('MOD_DEBUG'&& MOD_DEBUG == true && isset($GLOBALS['Dbg'])) {
  83.       global $Dbg;
  84.  
  85.       // $Dbg->queryRel('Connecting to <i>'.SQL.'</i> DATABASE [<b>'.$this->DB_NAME.'</b>] dns: '.$this->DB_USER.':'.$this->DB_PASS.'@'.DB_HOST);
  86.       $Dbg->stopTimer();
  87.     }
  88.  
  89.     if(strtolower(CHARSET== 'utf-8'$this->DBQuery("SET NAMES UTF8");
  90.     return true;
  91.   }
  92.  
  93.   /**
  94.    * mysql::DBSelectDB()
  95.    * Sélection de la Base de données
  96.    *
  97.    * @access public
  98.    * @param none 
  99.    * @return boolean 
  100.    */
  101.  
  102.   function DBSelectDB()
  103.   {
  104.       // Notify the end of the current method
  105.       $this->dispatcher->notify(new sfEvent($this'dbconnector.selectDB'));
  106.     mysql_select_db($this->DB_NAME$this->DB_LINKor die($this->DBError(_t('divers','db_connect_failed')));
  107.     return true;
  108.   }
  109.  
  110.   /**
  111.    * mysql::DBError()
  112.    * Gestion des erreurs MySQL
  113.    *
  114.    * @access private
  115.    * @param string $message_err : message retourné par la requête
  116.    * @param string $query : requête provoquant l'erreur
  117.    * @return boolean 
  118.    */
  119.   function DBError($message_err$query = -1)
  120.   {
  121.       // Notify the end of the current method
  122.       $this->dispatcher->notify(new sfEvent($this'dbconnector.error'));
  123.       
  124.     $erreur $message_err "<br />[<em> error n° " @mysql_errno(" : " @mysql_error("</em> ]<br />\n\n";
  125.     if ($query != -1{
  126.       $this->_logDbError($query);
  127.       $erreur .= "SQL query : $query<br />\n";
  128.     }
  129.     $date date("[D d/M/y H:i:s]<br />\n");
  130.     $erreur .= $date;
  131.     $erreur .= "running script :" $_SERVER["SCRIPT_NAME"getHttpParameters()"<br />";
  132.     if(defined('MOD_DEBUG'&& MOD_DEBUG == true{
  133.       echo $erreur;
  134.     }
  135.     if ($this->DB_ADMINMAIL != -1@error_log ($erreur1$this->DB_ADMINMAIL);
  136.   }
  137.  
  138.   /**
  139.    * mysql::DBInsert()
  140.    * Insertion de données dans la BDD
  141.    *
  142.    * @access public
  143.    * @param string $query : requête SQL
  144.    * @param string $returnid : Si $returnid==1 ==> renvoie last_id()
  145.    * @return integer || boolean : $result last_id()
  146.    */
  147.  
  148.   function DBInsert ($query$returnid = -1)
  149.   {
  150.       // Notify the end of the current method
  151.       $this->dispatcher->notify(new sfEvent($this'dbconnector.insert'array('query' => $query'lastid' => $returnid)));
  152.       
  153.     if (!($this->RESULT = $this->DBQuery($query))) return false;
  154.     // Si returnid=1 on renvoie l'id de l'enregistrement
  155.     // A utiliser uniquement si attribut auto_increment
  156.     if ($returnid == 1{
  157.       if (!(mysql_insert_id())) {
  158.         $query "SELECT LAST_INSERT_ID() as last_id";
  159.         $res $this->DBQuery($query);
  160.       else {
  161.         $res mysql_insert_id();
  162.       }
  163.     else {
  164.       $res true;
  165.     }
  166.     return $res;
  167.   }
  168.  
  169.   /**
  170.    * mysql::DBQuery()
  171.    * effectue une requête en tous genres -UPDATE - DELETE - SELECT
  172.    *
  173.    * @access public
  174.    * @param string $query : requête SQL
  175.    * @return boolean : $result
  176.    */
  177.   function DBQuery ($query)
  178.   {
  179.  
  180.       // Notify the end of the current method
  181.       $this->dispatcher->notify(new sfEvent($this'dbconnector.query'array('query' => $query)));
  182.       
  183.     if(defined('MOD_DEBUG'&& MOD_DEBUG == true && isset($GLOBALS['Dbg'])) {
  184.       global $Dbg;
  185.  
  186.       $this->_logDbQuery($query);
  187.       $Dbg->stopTimer();
  188.       $Dbg->query($query);
  189.       $Dbg->stopTimer();
  190.     }
  191.     $this->RESULT = mysql_query($query$this->DB_LINKor die($this->DbError(_t('divers','sql_query_failed')$query));
  192.  
  193.     return $this->RESULT;
  194.   }
  195.  
  196.   /**
  197.    * mysql::DBSelect()
  198.    * Requêtes - SELECT
  199.    *
  200.    * @access public
  201.    * @param string $query : requête SQL
  202.    * @param string $fetch : renvoie des données, valeur ASSOC (default), ARRAY, OBJECT
  203.    * @return array : $result
  204.    */
  205.  
  206.   function DBSelect ($query$fetch 'ASSOC')
  207.   {
  208.  
  209.       // Notify the end of the current method
  210.       $this->dispatcher->notify(new sfEvent($this'dbconnector.select'array('query' => $query'type' => $fetch)));
  211.       
  212.     if (!($this->RESULT = $this->DBQuery($query))) {
  213.       @mysql_free_result($this->RESULT);
  214.       return false;
  215.     }
  216.     if ($fetch == 'ARRAY'{
  217.       $i 0;
  218.       while ($data @mysql_fetch_array($this->RESULT)) {
  219.         $table[$i$data;
  220.         $i++;
  221.       }
  222.     }
  223.     if ($fetch == 'OBJECT'{
  224.       $i 0;
  225.       while ($data @mysql_fetch_object($this->RESULT)) {
  226.         $table[$i$data;
  227.         $i++;
  228.       }
  229.     }
  230.     if ($fetch == 'ASSOC'{
  231.       $i 0;
  232.       while ($data @mysql_fetch_assoc($this->RESULT)) {
  233.         $table[$i$data;
  234.         $i++;
  235.       }
  236.     }
  237.  
  238.     if (!isset($table)) $table 0;
  239.     return $table;
  240.   }
  241.  
  242.   /**
  243.    * mysql::DBescape()
  244.    * Echappement  des variables
  245.    *
  246.    * @access public
  247.    * @param string/array $input 
  248.    * @return string/array : $output
  249.    */
  250.  
  251.   function DBescape ($input)
  252.   {
  253.  
  254.       // Filter data event + return value
  255.       $r $this->dispatcher->filter(new sfEvent($this'dbconnector.escape'array('data' => $input))$input);
  256.       $input $r->getReturnValue();
  257.       
  258.     if(is_array($input)) {
  259.  
  260.       $output array();
  261.       
  262.       foreach ($input as $key => $value{
  263.         if(is_string($value)) {
  264.           if (get_magic_quotes_gpc()) $value=stripslashes($value);
  265.           $output[$keymysql_real_escape_string($value);
  266.         else {
  267.           $output[$key=  $value;
  268.         }
  269.       }
  270.     else {
  271.       
  272.       // it is a simple string
  273.       if(is_string($input)) {
  274.         if (get_magic_quotes_gpc()) $input=stripslashes($input);
  275.         $output mysql_real_escape_string($input);
  276.       else {
  277.         $output =  $input;
  278.       }
  279.     }
  280.     return $output;
  281.      
  282.   }
  283.  
  284.   /**
  285.    * mysql::DBaffectedRow()
  286.    * Retourne le nombre d'enregistrements affecté par la dernière requête
  287.    *
  288.    * @access public
  289.    * @return integer : $res
  290.    */
  291.   function DBaffectedRow()
  292.   {
  293.       // Notify the end of the current method
  294.       $this->dispatcher->notify(new sfEvent($this'dbconnector.affected_rows'));
  295.       
  296.     $res mysql_affected_rows();
  297.     return $res;
  298.   }
  299.  
  300.   /**
  301.    * _logDbQuery()
  302.    * @access private
  303.    * @param string $q 
  304.    * @return void 
  305.    */
  306.   function _logDbQuery($q)
  307.   {
  308.       // Filter data event + return value
  309.       $r $this->dispatcher->filter(new sfEvent($this'dbconnector.log_query'array('query' => $q))$q);
  310.       $q $r->getReturnValue();
  311.       
  312.     if(defined('SQL_LOG_DEBUG'&& SQL_LOG_DEBUG == 1{
  313.  
  314.       $q str_replace(array("\r""\n""\t"),"",$q);
  315.       logfile(LOG_SQL_QUERIESarray($qget_class()));
  316.  
  317.     }
  318.   }
  319.  
  320.   /**
  321.    * _logDbError()
  322.    * @access private
  323.    * @param string $q 
  324.    * @return void 
  325.    */
  326.   function _logDbError($q)
  327.   {
  328.       // Filter data event + return value
  329.       $r $this->dispatcher->filter(new sfEvent($this'dbconnector.log_error'array('query' => $q))$q);
  330.       $q $r->getReturnValue();
  331.       
  332.     $q str_replace(array("\r""\n""\t"),"",$q);
  333.     logfile(LOG_SQL_ERRORSarray($q$_SERVER["SCRIPT_NAME"]get_class()));
  334.   }
  335.   /**
  336.    * mysql::DBclose()
  337.    * Retourne le nombre d'enregistrements affecté par la dernière requête
  338.    *
  339.    * @access public
  340.    * @param string $query : requête SQL
  341.    * @return boolean : $result
  342.    */
  343.   function DBClose()
  344.   {
  345.       // Notify the end of the current method
  346.       $this->dispatcher->notify(new sfEvent($this'dbconnector.close'));
  347.       
  348.     @mysql_close();
  349.   }
  350. }
  351.  
  352. ?>

Documentation generated on Mon, 08 Apr 2013 18:12:27 +0200 by phpDocumentor 1.4.1