Import.php 6.58 KB
Newer Older
lsmith's avatar
lsmith committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
<?php
/*
 *  $Id$
 *
 * 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
 * <http://www.phpdoctrine.com>.
 */
Doctrine::autoload('Doctrine_Connection_Module');
/**
 * class Doctrine_Import
 * Main responsible of performing import operation. Delegates database schema
 * reading to a reader object and passes the result to a builder object which
 * builds a Doctrine data model.
 *
 * @package     Doctrine
29
 * @subpackage  Import
lsmith's avatar
lsmith committed
30 31 32 33 34 35 36
 * @link        www.phpdoctrine.com
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @since       1.0
 * @version     $Revision$
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @author      Jukka Hassinen <Jukka.Hassinen@BrainAlliance.com>
 */
lsmith's avatar
lsmith committed
37
class Doctrine_Import extends Doctrine_Connection_Module
lsmith's avatar
lsmith committed
38 39 40 41 42 43 44
{
    protected $sql = array();
    /**
     * lists all databases
     *
     * @return array
     */
lsmith's avatar
lsmith committed
45
    public function listDatabases()
lsmith's avatar
lsmith committed
46 47 48 49 50 51 52 53 54 55 56 57
    {
        if ( ! isset($this->sql['listDatabases'])) {
            throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
        }

        return $this->conn->fetchColumn($this->sql['listDatabases']);
    }
    /**
     * lists all availible database functions
     *
     * @return array
     */
lsmith's avatar
lsmith committed
58
    public function listFunctions()
lsmith's avatar
lsmith committed
59 60 61 62 63 64 65 66 67 68 69 70 71
    {
        if ( ! isset($this->sql['listFunctions'])) {
            throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
        }

        return $this->conn->fetchColumn($this->sql['listFunctions']);
    }
    /**
     * lists all database triggers
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
72
    public function listTriggers($database = null)
lsmith's avatar
lsmith committed
73 74 75 76 77 78 79 80 81
    {
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
    }
    /**
     * lists all database sequences
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
82
    public function listSequences($database = null)
lsmith's avatar
lsmith committed
83 84 85 86 87 88 89 90 91 92 93 94 95
    {
        if ( ! isset($this->sql['listSequences'])) {
            throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
        }

        return $this->conn->fetchColumn($this->sql['listSequences']);
    }
    /**
     * lists table constraints
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
96
    public function listTableConstraints($table)
lsmith's avatar
lsmith committed
97 98 99 100 101 102 103 104 105
    {
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
    }
    /**
     * lists table constraints
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
106
    public function listTableColumns($table)
lsmith's avatar
lsmith committed
107 108 109 110 111 112 113 114 115
    {
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
    }
    /**
     * lists table constraints
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
116
    public function listTableIndexes($table)
lsmith's avatar
lsmith committed
117 118 119 120 121 122 123 124 125
    {
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
    }
    /**
     * lists tables
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
126
    public function listTables($database = null)
lsmith's avatar
lsmith committed
127 128 129 130 131 132 133 134 135
    {
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
    }
    /**
     * lists table triggers
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
136
    public function listTableTriggers($table)
lsmith's avatar
lsmith committed
137 138 139 140 141 142 143 144 145
    {
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
    }
    /**
     * lists table views
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
146
    public function listTableViews($table)
lsmith's avatar
lsmith committed
147 148 149 150 151 152 153 154
    {
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
    }
    /**
     * lists database users
     *
     * @return array
     */
lsmith's avatar
lsmith committed
155
    public function listUsers()
lsmith's avatar
lsmith committed
156 157 158 159 160 161 162 163 164 165 166 167 168
    {
        if ( ! isset($this->sql['listUsers'])) {
            throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
        }

        return $this->conn->fetchColumn($this->sql['listUsers']);
    }
    /**
     * lists database views
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
169
    public function listViews($database = null)
lsmith's avatar
lsmith committed
170 171 172 173 174 175 176
    {
        if ( ! isset($this->sql['listViews'])) {
            throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
        }

        return $this->conn->fetchColumn($this->sql['listViews']);
    }
zYne's avatar
zYne committed
177
    /**
178
     * importSchema
zYne's avatar
zYne committed
179 180 181 182 183
     *
     * method for importing existing schema to Doctrine_Record classes
     *
     * @param string $directory
     * @param array $databases
zYne's avatar
zYne committed
184
     * @return array                the names of the imported classes
zYne's avatar
zYne committed
185
     */
186
    public function importSchema($directory, array $databases = array())
zYne's avatar
zYne committed
187
    {
188 189 190 191 192
        $connections = Doctrine_Manager::getInstance()->getConnections();
        
        foreach ($connections as $connection) {
          $builder = new Doctrine_Import_Builder();
          $builder->setTargetPath($directory);
zYne's avatar
zYne committed
193

194 195 196 197 198 199
          $classes = array();
          foreach ($connection->import->listTables() as $table) {
              $builder->buildRecord(array('tableName' => $table,
                                          'className' => Doctrine::classify($table)),
                                          $connection->import->listTableColumns($table),
                                          array());
zYne's avatar
zYne committed
200
        
201 202
              $classes[] = Doctrine::classify($table);
          }
zYne's avatar
zYne committed
203
        }
zYne's avatar
zYne committed
204 205
        
        return $classes;
zYne's avatar
zYne committed
206
    }
207
}