PhpMyObject
[ class tree: PhpMyObject ] [ index: PhpMyObject ] [ all elements ]

Source for file PMO_Dbms_Mysql.php

Documentation is available at PMO_Dbms_Mysql.php

  1. <?php
  2. /**
  3.  * This file contains the PMO_Dbms_Mysql driver class.
  4.  *
  5.  * This file is part of the PhpMyObject project,
  6.  * an Object-Relational Mapping (ORM) system.
  7.  * 
  8.  * For questions, help, comments, discussion, etc., please join our
  9.  * forum at {@link http://www.developpez.net/forums/forumdisplay.php?f=770}
  10.  * or our mailing list at {@link http://groups.google.com/group/pmo-dev}.
  11.  *
  12.  * PhpMyObject is free software: you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation, either version 3 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with this program.  If not, see {@link http://www.gnu.org/licenses/}.
  24.  *
  25.  * @package        PhpMyObject
  26.  * @subpackage PMO_Core
  27.  * @author        Nicolas Boiteux <nicolas_boiteux@yahoo.fr>
  28.  * @link            http://pmo.developpez.com/
  29.  * @since        PhpMyObject v0.1
  30.  * @version        $Revision: $
  31.  * @copyright    Copyright (C) 2007-2008 Nicolas Boiteux
  32.  * @license        GPLv3 {@link http://www.gnu.org/licenses/gpl}
  33.  * @filesource
  34.  *
  35.  */ 
  36.  
  37. /**
  38.  * This class implements the MySql driver.
  39.  * 
  40.  * @package        PhpMyObject
  41.  * @subpackage PMO_Core
  42.  */
  43. class PMO_Dbms_Mysql extends PMO_MyDbms {
  44.  
  45.     /**
  46.      * a result resource returned by a MySql query
  47.      * @var resource 
  48.      */
  49.     protected $result;
  50.  
  51.     /**
  52.      * the constructor
  53.      *
  54.      * if a mysql link is provided, it will be used.
  55.      *
  56.      * @param resource $mysqllink            a standrd MySql link returned by mysql_query()
  57.      */
  58.     public function __construct($mysqllink NULL{
  59.         if(isset($mysqllink))
  60.             $this->setDB($mysqllink);
  61.     }
  62.  
  63.     /**
  64.      * establishes a connection with the database server and the database
  65.      *
  66.      * @param array $authdb            the database connection information, e.g. host,
  67.      *                                         user name and password, database name
  68.      * @throws Exception                if we cannot connect to the database server or
  69.      *                                         the actual database
  70.      * @see PMO_MyConfig
  71.      */
  72.     public function connect(array $authdb){
  73.         $this->setDB(mysql_connect($authdb['host']$authdb['user']$authdb['pass']));
  74.         if(!$this->getDB())
  75.             throw new Exception(mysql_error());
  76.         
  77.     if(!mysql_select_db($authdb['base']$this->getDB()))
  78.             throw new Exception(mysql_error());
  79.     }
  80.  
  81.     /**
  82.      * closes the database connetion
  83.      */
  84.     public function __destruct({
  85.         @mysql_close($this->getDB());
  86.     }
  87.  
  88.     /**
  89.      * execute a SQL query against the database
  90.      *
  91.      * @param string $query            the SQL query to execute against the database
  92.      * @return bool                    TRUE is the query returned some results
  93.      * @throws Exception                if no result were returned by the query
  94.      */
  95.     public function query($query){
  96.         $this->result mysql_query($query);
  97.         if($this->result)
  98.             return TRUE;
  99.         else
  100.             throw new Exception($query." ".mysql_error());
  101.     }
  102.  
  103.     /**
  104.      * returns the next row as an associative array
  105.      *
  106.      * @return array 
  107.      */
  108.     public function fetchArray({
  109.             return mysql_fetch_assoc($this->result);
  110.     }
  111.  
  112.     /**
  113.      * returns an array containing the table properties
  114.      *
  115.      * @param string $table            the table name to look for
  116.      * @return array 
  117.      */
  118.     public function getTableDesc($table{
  119.         $sql sprintf('DESC %s'addslashes($table));
  120.         $this->query($sql);
  121.         
  122.         while($dbresult $this->fetchArray()){
  123.             $tmparray[array("Field"=>$dbresult['Field']
  124.                                 "Type" => $this->translateType($dbresult['Type'])
  125.                                 "Null" => $dbresult['Null']
  126.                                 "Key"=>$dbresult['Key']
  127.                                 "Default"=>$dbresult['Default']
  128.                                 "Extra"=>$dbresult['Extra'],
  129.                                 "Perm"=>"rw");
  130.         }
  131.         return $tmparray;
  132.     }
  133.  
  134.     /**
  135.      * returns the last inserted id
  136.      *
  137.      * @return int 
  138.      */
  139.     public function getLastId({
  140.         return mysql_insert_id();
  141.     }
  142.  
  143.     /**
  144.      * starts a MySql transaction
  145.      *
  146.      * @todo need to check if we already are in an open transaction
  147.      *             and throw an exception if so
  148.      */
  149.     public function beginTransaction(){
  150.         mysql_query("BEGIN"$this->getDb())
  151.     }
  152.  
  153.     /**
  154.      * rolls back a transaction
  155.      *
  156.      * @todo need to check if we are in a transaction and throw
  157.      *             an exception if we are not
  158.      */
  159.     public function rollback(){
  160.         mysql_query("ROLLBACK"$this->getDb())
  161.     }
  162.     
  163.     /**
  164.      * commits a transaction
  165.      *
  166.      * @todo need to check if we are in a transaction and throw
  167.      *             an exception if we are not
  168.      */
  169.     public function commit(){
  170.         mysql_query("COMMIT"$this->getDb())
  171.     }    
  172.     
  173. }
  174. ?>

Documentation generated on Wed, 15 Oct 2008 09:15:40 -0400 by phpDocumentor 1.4.1