Import.php 6.92 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
{
    protected $sql = array();
40

lsmith's avatar
lsmith committed
41 42 43 44 45
    /**
     * lists all databases
     *
     * @return array
     */
lsmith's avatar
lsmith committed
46
    public function listDatabases()
lsmith's avatar
lsmith committed
47 48 49 50 51 52 53
    {
        if ( ! isset($this->sql['listDatabases'])) {
            throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
        }

        return $this->conn->fetchColumn($this->sql['listDatabases']);
    }
54

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

        return $this->conn->fetchColumn($this->sql['listFunctions']);
    }
68

lsmith's avatar
lsmith committed
69 70 71 72 73 74
    /**
     * lists all database triggers
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
75
    public function listTriggers($database = null)
lsmith's avatar
lsmith committed
76 77 78
    {
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
    }
79

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

        return $this->conn->fetchColumn($this->sql['listSequences']);
    }
94

lsmith's avatar
lsmith committed
95 96 97 98 99 100
    /**
     * lists table constraints
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
101
    public function listTableConstraints($table)
lsmith's avatar
lsmith committed
102 103 104
    {
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
    }
105

lsmith's avatar
lsmith committed
106 107 108 109 110 111
    /**
     * lists table constraints
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
112
    public function listTableColumns($table)
lsmith's avatar
lsmith committed
113 114 115
    {
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
    }
116

lsmith's avatar
lsmith committed
117 118 119 120 121 122
    /**
     * lists table constraints
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
123
    public function listTableIndexes($table)
lsmith's avatar
lsmith committed
124 125 126
    {
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
    }
127

lsmith's avatar
lsmith committed
128 129 130 131 132 133
    /**
     * lists tables
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
134
    public function listTables($database = null)
lsmith's avatar
lsmith committed
135 136 137
    {
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
    }
138

lsmith's avatar
lsmith committed
139 140 141 142 143 144
    /**
     * lists table triggers
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
145
    public function listTableTriggers($table)
lsmith's avatar
lsmith committed
146 147 148
    {
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
    }
149

lsmith's avatar
lsmith committed
150 151 152 153 154 155
    /**
     * lists table views
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
156
    public function listTableViews($table)
lsmith's avatar
lsmith committed
157 158 159
    {
        throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
    }
160

lsmith's avatar
lsmith committed
161 162 163 164 165
    /**
     * lists database users
     *
     * @return array
     */
lsmith's avatar
lsmith committed
166
    public function listUsers()
lsmith's avatar
lsmith committed
167 168 169 170 171 172 173
    {
        if ( ! isset($this->sql['listUsers'])) {
            throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
        }

        return $this->conn->fetchColumn($this->sql['listUsers']);
    }
174

lsmith's avatar
lsmith committed
175 176 177 178 179 180
    /**
     * lists database views
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
181
    public function listViews($database = null)
lsmith's avatar
lsmith committed
182 183 184 185 186 187 188
    {
        if ( ! isset($this->sql['listViews'])) {
            throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
        }

        return $this->conn->fetchColumn($this->sql['listViews']);
    }
189

zYne's avatar
zYne committed
190
    /**
191
     * importSchema
zYne's avatar
zYne committed
192 193 194 195 196
     *
     * method for importing existing schema to Doctrine_Record classes
     *
     * @param string $directory
     * @param array $databases
zYne's avatar
zYne committed
197
     * @return array                the names of the imported classes
zYne's avatar
zYne committed
198
     */
199
    public function importSchema($directory, array $databases = array(), array $options = array())
zYne's avatar
zYne committed
200
    {
201 202
        $connections = Doctrine_Manager::getInstance()->getConnections();
        
203 204 205
        foreach ($connections as $name => $connection) {
          // Limit the databases to the ones specified by $databases.
          // Check only happens if array is not empty
206
          if ( ! empty($databases) && !in_array($name, $databases)) {
207 208 209
            continue;
          }
          
210 211
          $builder = new Doctrine_Import_Builder();
          $builder->setTargetPath($directory);
212
          $builder->setOptions($options);
zYne's avatar
zYne committed
213

214 215 216 217 218 219
          $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
220
        
221 222
              $classes[] = Doctrine::classify($table);
          }
zYne's avatar
zYne committed
223
        }
zYne's avatar
zYne committed
224 225
        
        return $classes;
zYne's avatar
zYne committed
226
    }
227
}