Db.php 3.17 KB
Newer Older
lsmith's avatar
lsmith committed
1 2
<?php
/*
romanb's avatar
romanb committed
3
 *  $Id: Db.php 3882 2008-02-22 18:11:35Z jwage $
lsmith's avatar
lsmith committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.phpdoctrine.org>.
lsmith's avatar
lsmith committed
20 21 22 23 24 25
 */
Doctrine::autoload('Doctrine_Import_Reader');
/**
 * class Doctrine_Import_Reader_Db
 * Reads a database using the given PDO connection and constructs a database
 * schema
26
 *
lsmith's avatar
lsmith committed
27
 * @package     Doctrine
28
 * @subpackage  Import
29
 * @link        www.phpdoctrine.org
lsmith's avatar
lsmith committed
30 31
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @since       1.0
romanb's avatar
romanb committed
32
 * @version     $Revision: 3882 $
lsmith's avatar
lsmith committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 */
class Doctrine_Import_Reader_Db extends Doctrine_Import_Reader
{
    /** Aggregations: */

    /** Compositions: */

     /*** Attributes: ***/

    /**
     * @access private
     */
    private $pdo;

    /**
     *
     * @param object pdo      * @return
     * @access public
     */
lsmith's avatar
lsmith committed
53 54
    public function setPdo( $pdo )
    {
lsmith's avatar
lsmith committed
55 56 57 58 59 60 61 62

    } // end of member function setPdo

    /**
     *
     * @return Doctrine_Schema
     * @access public
     */
63
    public function read()
lsmith's avatar
lsmith committed
64 65 66 67 68 69 70 71 72
    {
        $dataDict = Doctrine_Manager::getInstance()->getCurrentConnection()->getDataDict();

        $schema = new Doctrine_Schema(); /* @todo FIXME i am incomplete*/
        $db = new Doctrine_Schema_Database();
        $schema->addDatabase($db);

        $dbName = 'XXtest'; // @todo FIXME where should we get

lsmith's avatar
lsmith committed
73
        $this->conn->set("name",$dbName);
lsmith's avatar
lsmith committed
74
        $tableNames = $dataDict->listTables();
75
        foreach ($tableNames as $tableName) {
lsmith's avatar
lsmith committed
76 77 78
            $table = new Doctrine_Schema_Table();
            $table->set("name",$tableName);
            $tableColumns = $dataDict->listTableColumns($tableName);
79
            foreach ($tableColumns as $tableColumn) {
lsmith's avatar
lsmith committed
80 81
                $table->addColumn($tableColumn);
            }
lsmith's avatar
lsmith committed
82
            $this->conn->addTable($table);
83 84
            if ($fks = $dataDict->listTableConstraints($tableName)) {
                foreach ($fks as $fk) {
lsmith's avatar
lsmith committed
85 86 87 88 89 90 91 92 93
                    $relation = new Doctrine_Schema_Relation();
                    $relation->setRelationBetween($fk['referencingColumn'],$fk['referencedTable'],$fk['referencedColumn']);
                    $table->setRelation($relation);
                }
            }
        }

        return $schema;
    }
94
}