Source for file Mysql.php

Documentation is available at Mysql.php

  1. <?php
  2. /*
  3.  *  $Id: Mysql.php 2081 2007-07-26 19:52:12Z zYne $
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the LGPL. For more information, see
  19.  * <http://www.phpdoctrine.com>.
  20.  */
  21. Doctrine::autoload('Doctrine_Import');
  22. /**
  23.  * @package     Doctrine
  24.  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
  25.  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
  26.  * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  27.  * @version     $Revision: 2081 $
  28.  * @category    Object Relational Mapping
  29.  * @link        www.phpdoctrine.com
  30.  * @since       1.0
  31.  */
  32. {
  33.     protected $sql  = array(
  34.                             'showDatabases'   => 'SHOW DATABASES',
  35.                             'listTableFields' => 'DESCRIBE %s',
  36.                             'listSequences'   => 'SHOW TABLES',
  37.                             'listTables'      => 'SHOW TABLES',
  38.                             'listUsers'       => 'SELECT DISTINCT USER FROM USER',
  39.                             'listViews'       => "SHOW FULL TABLES %sWHERE Table_type = 'VIEW'",
  40.                             );
  41.     /**
  42.      * lists all database sequences
  43.      *
  44.      * @param string|null$database 
  45.      * @return array 
  46.      */
  47.     public function listSequences($database null)
  48.     {
  49.         $query 'SHOW TABLES';
  50.         if (!is_null($database)) {
  51.             $query .= ' FROM ' $database;
  52.         }
  53.         $tableNames $this->conn->fetchColumn($query);
  54.  
  55.         return array_map(array($this->conn'fixSequenceName')$tableNames);
  56.     }
  57.     /**
  58.      * lists table constraints
  59.      *
  60.      * @param string $table     database table name
  61.      * @return array 
  62.      */
  63.     public function listTableConstraints($table)
  64.     {
  65.         $keyName 'Key_name';
  66.         $nonUnique 'Non_unique';
  67.         if ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITYDoctrine::PORTABILITY_FIX_CASE{
  68.             if ($this->conn->options['field_case'== CASE_LOWER{
  69.                 $keyName strtolower($keyName);
  70.                 $nonUnique strtolower($nonUnique);
  71.             else {
  72.                 $keyName strtoupper($keyName);
  73.                 $nonUnique strtoupper($nonUnique);
  74.             }
  75.         }
  76.  
  77.         $table $this->conn->quoteIdentifier($tabletrue);
  78.         $query 'SHOW INDEX FROM ' $table;
  79.         $indexes $this->conn->fetchAssoc($query);
  80.  
  81.         $result array();
  82.         foreach ($indexes as $indexData{
  83.             if (!$indexData[$nonUnique]{
  84.                 if ($indexData[$keyName!== 'PRIMARY'{
  85.                     $index $this->conn->fixIndexName($indexData[$keyName]);
  86.                 else {
  87.                     $index 'PRIMARY';
  88.                 }
  89.                 if empty($index)) {
  90.                     $result[$index;
  91.                 }
  92.             }
  93.         }
  94.         return $result;
  95.     }
  96.     /**
  97.      * lists table foreign keys
  98.      *
  99.      * @param string $table     database table name
  100.      * @return array 
  101.      */
  102.     public function listTableForeignKeys($table
  103.     {
  104.         $sql 'SHOW CREATE TABLE ' $this->conn->quoteIdentifier($tabletrue);    
  105.     }
  106.     /**
  107.      * lists table constraints
  108.      *
  109.      * @param string $table     database table name
  110.      * @return array 
  111.      */
  112.     public function listTableColumns($table)
  113.     {
  114.         $sql 'DESCRIBE ' $this->conn->quoteIdentifier($tabletrue);
  115.         $result $this->conn->fetchAssoc($sql);
  116.  
  117.         $description array();
  118.         foreach ($result as $key => $val{
  119.  
  120.             $val array_change_key_case($valCASE_LOWER);
  121.  
  122.             $decl $this->conn->dataDict->getPortableDeclaration($val);
  123.  
  124.             $values = isset($decl['values']$decl['values'array();
  125.  
  126.             $description array(
  127.                 'name'      => $val['field'],
  128.                 'type'      => $decl['type'][0],
  129.                 'alltypes'  => $decl['type'],
  130.                 'ntype'     => $val['type'],
  131.                 'length'    => $decl['length'],
  132.                 'fixed'     => $decl['fixed'],
  133.                 'unsigned'  => $decl['unsigned'],
  134.                 'values'    => $values,
  135.                 'primary'   => (strtolower($val['key']== 'pri'),
  136.                 'default'   => $val['default'],
  137.                 'notnull'   => (bool) ($val['null'!= 'YES'),
  138.                 'autoinc'   => (bool) (strpos($val['extra']'auto_increment'!== false),
  139.             );
  140.             $columns[$val['field']] $description;
  141.         }
  142.  
  143.  
  144.         return $columns;
  145.     }
  146.     /**
  147.      * lists table constraints
  148.      *
  149.      * @param string $table     database table name
  150.      * @return array 
  151.      */
  152.     public function listTableIndexes($table)
  153.     {
  154.         $keyName 'Key_name';
  155.         $nonUnique 'Non_unique';
  156.         if ($this->conn->options['portability'Doctrine::PORTABILITY_FIX_CASE{
  157.             if ($this->conn->options['field_case'== CASE_LOWER{
  158.                 $keyName strtolower($keyName);
  159.                 $nonUnique strtolower($nonUnique);
  160.             else {
  161.                 $keyName strtoupper($keyName);
  162.                 $nonUnique strtoupper($nonUnique);
  163.             }
  164.         }
  165.  
  166.         $table $this->conn->quoteIdentifier($tabletrue);
  167.         $query 'SHOW INDEX FROM ' $table;
  168.         $indexes $this->conn->fetchAssoc($query);
  169.  
  170.  
  171.         $result array();
  172.         foreach ($indexes as $indexData{
  173.             if ($indexData[$nonUnique&& ($index $this->conn->fixIndexName($indexData[$keyName]))) {
  174.                 $result[$index;
  175.             }
  176.         }
  177.         return $result;
  178.     }
  179.     /**
  180.      * lists tables
  181.      *
  182.      * @param string|null$database 
  183.      * @return array 
  184.      */
  185.     public function listTables($database null)
  186.     {
  187.         return $this->conn->fetchColumn($this->sql['listTables']);
  188.     }
  189.     /**
  190.      * lists database views
  191.      *
  192.      * @param string|null$database 
  193.      * @return array 
  194.      */
  195.     public function listViews($database null)
  196.     {
  197.         if (!is_null($database)) {
  198.             $query sprintf($this->sql['listViews']' FROM ' $database);
  199.         }
  200.  
  201.         return $this->conn->fetchColumn($query);
  202.     }
  203. }