web/lib/Zend/Test/PHPUnit/Db/Metadata/Generic.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Test
       
    17  * @subpackage PHPUnit
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Generic.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Db_Adapter_Abstract
       
    25  */
       
    26 require_once "Zend/Db/Adapter/Abstract.php";
       
    27 
       
    28 /**
       
    29  * @see PHPUnit_Extensions_Database_DB_IMetaData
       
    30  */
       
    31 require_once "PHPUnit/Extensions/Database/DB/IMetaData.php";
       
    32 
       
    33 /**
       
    34  * Generic Metadata accessor for the Zend_Db adapters
       
    35  *
       
    36  * @uses       PHPUnit_Extensions_Database_DB_IMetaData
       
    37  * @category   Zend
       
    38  * @package    Zend_Test
       
    39  * @subpackage PHPUnit
       
    40  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    41  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    42  */
       
    43 class Zend_Test_PHPUnit_Db_Metadata_Generic implements PHPUnit_Extensions_Database_DB_IMetaData
       
    44 {
       
    45     /**
       
    46      * Zend_Db Connection
       
    47      *
       
    48      * @var Zend_Db_Adapter_Abstract
       
    49      */
       
    50     protected $_connection;
       
    51 
       
    52     /**
       
    53      * Schemaname
       
    54      *
       
    55      * @var string
       
    56      */
       
    57     protected $_schema;
       
    58 
       
    59     /**
       
    60      * Cached Table metadata
       
    61      *
       
    62      * @var array
       
    63      */
       
    64     protected $_tableMetadata = array();
       
    65 
       
    66     /**
       
    67      * Creates a new database meta data object using the given pdo connection
       
    68      * and schema name.
       
    69      *
       
    70      * @param PDO $pdo
       
    71      * @param string $schema
       
    72      */
       
    73     public final function __construct(Zend_Db_Adapter_Abstract $db, $schema)
       
    74     {
       
    75         $this->_connection = $db;
       
    76         $this->_schema     = $schema;
       
    77     }
       
    78 
       
    79     /**
       
    80      * List Tables
       
    81      *
       
    82      * @return array
       
    83      */
       
    84     public function getTableNames()
       
    85     {
       
    86         return $this->_connection->listTables();
       
    87     }
       
    88 
       
    89     /**
       
    90      * Get Table information
       
    91      *
       
    92      * @param  string $tableName
       
    93      * @return array
       
    94      */
       
    95     protected function getTableDescription($tableName)
       
    96     {
       
    97         if(!isset($this->_tableMetadata[$tableName])) {
       
    98             $this->_tableMetadata[$tableName] = $this->_connection->describeTable($tableName);
       
    99         }
       
   100         return $this->_tableMetadata[$tableName];
       
   101     }
       
   102 
       
   103     /**
       
   104      * Returns an array containing the names of all the columns in the
       
   105      * $tableName table,
       
   106      *
       
   107      * @param string $tableName
       
   108      * @return array
       
   109      */
       
   110     public function getTableColumns($tableName)
       
   111     {
       
   112         $tableMeta = $this->getTableDescription($tableName);
       
   113         $columns = array_keys($tableMeta);
       
   114         return $columns;
       
   115     }
       
   116 
       
   117     /**
       
   118      * Returns an array containing the names of all the primary key columns in
       
   119      * the $tableName table.
       
   120      *
       
   121      * @param string $tableName
       
   122      * @return array
       
   123      */
       
   124     public function getTablePrimaryKeys($tableName)
       
   125     {
       
   126         $tableMeta = $this->getTableDescription($tableName);
       
   127 
       
   128         $primaryColumnNames = array();
       
   129         foreach($tableMeta AS $column) {
       
   130             if($column['PRIMARY'] == true) {
       
   131                 $primaryColumnNames[] = $column['COLUMN_NAME'];
       
   132             }
       
   133         }
       
   134         return $primaryColumnNames;
       
   135     }
       
   136 
       
   137     /**
       
   138      * Returns the name of the default schema.
       
   139      *
       
   140      * @return string
       
   141      */
       
   142     public function getSchema()
       
   143     {
       
   144         return $this->_schema;
       
   145     }
       
   146 
       
   147     /**
       
   148      * Returns a quoted schema object. (table name, column name, etc)
       
   149      *
       
   150      * @param string $object
       
   151      * @return string
       
   152      */
       
   153     public function quoteSchemaObject($object)
       
   154     {
       
   155         return $this->_connection->quoteIdentifier($object);
       
   156     }
       
   157 
       
   158     /**
       
   159      * Returns true if the rdbms allows cascading
       
   160      *
       
   161      * @return bool
       
   162      */
       
   163     public function allowsCascading()
       
   164     {
       
   165         return false;
       
   166     }
       
   167 }