Source for file Import.php

Documentation is available at Import.php

  1. <?php
  2. /*
  3.  *  $Id: Import.php 2255 2007-08-16 22:42:35Z Jonathan.Wage $
  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_Connection_Module');
  22. /**
  23.  * class Doctrine_Import
  24.  * Main responsible of performing import operation. Delegates database schema
  25.  * reading to a reader object and passes the result to a builder object which
  26.  * builds a Doctrine data model.
  27.  *
  28.  * @package     Doctrine
  29.  * @category    Object Relational Mapping
  30.  * @link        www.phpdoctrine.com
  31.  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
  32.  * @since       1.0
  33.  * @version     $Revision: 2255 $
  34.  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
  35.  * @author      Jukka Hassinen <Jukka.Hassinen@BrainAlliance.com>
  36.  */
  37. {
  38.     protected $sql = array();
  39.     /**
  40.      * lists all databases
  41.      *
  42.      * @return array 
  43.      */
  44.     public function listDatabases()
  45.     {
  46.         if isset($this->sql['listDatabases'])) {
  47.             throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
  48.         }
  49.  
  50.         return $this->conn->fetchColumn($this->sql['listDatabases']);
  51.     }
  52.     /**
  53.      * lists all availible database functions
  54.      *
  55.      * @return array 
  56.      */
  57.     public function listFunctions()
  58.     {
  59.         if isset($this->sql['listFunctions'])) {
  60.             throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
  61.         }
  62.  
  63.         return $this->conn->fetchColumn($this->sql['listFunctions']);
  64.     }
  65.     /**
  66.      * lists all database triggers
  67.      *
  68.      * @param string|null$database 
  69.      * @return array 
  70.      */
  71.     public function listTriggers($database null)
  72.     {
  73.         throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
  74.     }
  75.     /**
  76.      * lists all database sequences
  77.      *
  78.      * @param string|null$database 
  79.      * @return array 
  80.      */
  81.     public function listSequences($database null)
  82.     {
  83.         if isset($this->sql['listSequences'])) {
  84.             throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
  85.         }
  86.  
  87.         return $this->conn->fetchColumn($this->sql['listSequences']);
  88.     }
  89.     /**
  90.      * lists table constraints
  91.      *
  92.      * @param string $table     database table name
  93.      * @return array 
  94.      */
  95.     public function listTableConstraints($table)
  96.     {
  97.         throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
  98.     }
  99.     /**
  100.      * lists table constraints
  101.      *
  102.      * @param string $table     database table name
  103.      * @return array 
  104.      */
  105.     public function listTableColumns($table)
  106.     {
  107.         throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
  108.     }
  109.     /**
  110.      * lists table constraints
  111.      *
  112.      * @param string $table     database table name
  113.      * @return array 
  114.      */
  115.     public function listTableIndexes($table)
  116.     {
  117.         throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
  118.     }
  119.     /**
  120.      * lists tables
  121.      *
  122.      * @param string|null$database 
  123.      * @return array 
  124.      */
  125.     public function listTables($database null)
  126.     {
  127.         throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
  128.     }
  129.     /**
  130.      * lists table triggers
  131.      *
  132.      * @param string $table     database table name
  133.      * @return array 
  134.      */
  135.     public function listTableTriggers($table)
  136.     {
  137.         throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
  138.     }
  139.     /**
  140.      * lists table views
  141.      *
  142.      * @param string $table     database table name
  143.      * @return array 
  144.      */
  145.     public function listTableViews($table)
  146.     {
  147.         throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
  148.     }
  149.     /**
  150.      * lists database users
  151.      *
  152.      * @return array 
  153.      */
  154.     public function listUsers()
  155.     {
  156.         if isset($this->sql['listUsers'])) {
  157.             throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
  158.         }
  159.  
  160.         return $this->conn->fetchColumn($this->sql['listUsers']);
  161.     }
  162.     /**
  163.      * lists database views
  164.      *
  165.      * @param string|null$database 
  166.      * @return array 
  167.      */
  168.     public function listViews($database null)
  169.     {
  170.         if isset($this->sql['listViews'])) {
  171.             throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
  172.         }
  173.  
  174.         return $this->conn->fetchColumn($this->sql['listViews']);
  175.     }
  176.     /**
  177.      * importSchema
  178.      *
  179.      * method for importing existing schema to Doctrine_Record classes
  180.      *
  181.      * @param string $directory 
  182.      * @param array $databases 
  183.      * @return array                the names of the imported classes
  184.      */
  185.     public function importSchema($directoryarray $databases array())
  186.     {
  187.         $builder new Doctrine_Import_Builder();
  188.         $builder->setTargetPath($directory);
  189.  
  190.         $classes array();
  191.         foreach ($this->listTables(as $table{
  192.             $builder->buildRecord(array('tableName' => $table,
  193.                                         'className' => Doctrine::classify($table)),
  194.                                   $this->listTableColumns($table),
  195.                                   array());
  196.         
  197.             $classes[Doctrine::classify($table);
  198.         }
  199.         
  200.         return $classes;
  201.     }
  202. }