SqliteSchemaManager.php 5.81 KB
Newer Older
romanb's avatar
romanb committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php
/*
 * 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
17
 * <http://www.doctrine-project.org>.
romanb's avatar
romanb committed
18 19
 */

20
namespace Doctrine\DBAL\Schema;
romanb's avatar
romanb committed
21 22

/**
23
 * SqliteSchemaManager
romanb's avatar
romanb committed
24 25 26 27
 *
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
28
 * @author      Jonathan H. Wage <jonwage@gmail.com>
romanb's avatar
romanb committed
29 30 31
 * @version     $Revision$
 * @since       2.0
 */
32
class SqliteSchemaManager extends AbstractSchemaManager
33
{
romanb's avatar
romanb committed
34 35 36 37 38 39
    /**
     * {@inheritdoc}
     * 
     * @override
     */
    public function dropDatabase($database)
40
    {
41 42
        if (file_exists($database)) {
            unlink($database);
43 44 45
        }
    }

romanb's avatar
romanb committed
46 47 48 49 50 51
    /**
     * {@inheritdoc}
     * 
     * @override
     */
    public function createDatabase($database)
52
    {
jwage's avatar
jwage committed
53 54 55 56 57 58 59 60 61
        $params = $this->_conn->getParams();
        $driver = $params['driver'];
        $options = array(
            'driver' => $driver,
            'path' => $database
        );
        $conn = \Doctrine\DBAL\DriverManager::getConnection($options);
        $conn->connect();
        $conn->close();
62 63 64 65 66 67 68
    }

    protected function _getPortableTableDefinition($table)
    {
        return $table['name'];
    }

69 70 71 72 73 74 75 76 77 78 79 80
    /**
     * @license New BSD License
     * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html
     * @param  array $tableIndexes
     * @param  string $tableName
     * @return array
     */
    protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
    {
        $indexBuffer = array();

        // fetch primary
81
        $stmt = $this->_conn->executeQuery( "PRAGMA TABLE_INFO ('$tableName')" );
82
        $indexArray = $stmt->fetchAll(\PDO::FETCH_ASSOC);
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
        foreach($indexArray AS $indexColumnRow) {
            if($indexColumnRow['pk'] == "1") {
                $indexBuffer[] = array(
                    'key_name' => 'primary',
                    'primary' => true,
                    'non_unique' => false,
                    'column_name' => $indexColumnRow['name']
                );
            }
        }

        // fetch regular indexes
        foreach($tableIndexes AS $tableIndex) {
            $keyName = $tableIndex['name'];
            $idx = array();
            $idx['key_name'] = $keyName;
            $idx['primary'] = false;
            $idx['non_unique'] = $tableIndex['unique']?false:true;

102
            $stmt = $this->_conn->executeQuery( "PRAGMA INDEX_INFO ( '{$keyName}' )" );
103
            $indexArray = $stmt->fetchAll(\PDO::FETCH_ASSOC);
104 105 106 107 108 109 110 111 112 113

            foreach ( $indexArray as $indexColumnRow ) {
                $idx['column_name'] = $indexColumnRow['name'];
                $indexBuffer[] = $idx;
            }
        }

        return parent::_getPortableTableIndexesList($indexBuffer, $tableName);
    }

114 115 116 117 118 119 120 121
    protected function _getPortableTableIndexDefinition($tableIndex)
    {
        return array(
            'name' => $tableIndex['name'],
            'unique' => (bool) $tableIndex['unique']
        );
    }

122 123 124 125 126 127 128 129 130 131 132 133 134
    protected function _getPortableTableColumnDefinition($tableColumn)
    {
        $e = explode('(', $tableColumn['type']);
        $tableColumn['type'] = $e[0];
        if (isset($e[1])) {
            $length = trim($e[1], ')');
            $tableColumn['length'] = $length;
        }

        $dbType = strtolower($tableColumn['type']);
        $length = isset($tableColumn['length']) ? $tableColumn['length'] : null;
        $unsigned = (boolean) isset($tableColumn['unsigned']) ? $tableColumn['unsigned'] : false;
        $fixed = false;
135
        $type = $this->_platform->getDoctrineTypeMapping($dbType);
136 137 138 139 140 141 142 143 144 145
        $default = $tableColumn['dflt_value'];
        if  ($default == 'NULL') {
            $default = null;
        }
        $notnull = (bool) $tableColumn['notnull'];

        if ( ! isset($tableColumn['name'])) {
            $tableColumn['name'] = '';
        }

146 147 148
        $precision = null;
        $scale = null;

149 150
        switch ($dbType) {
            case 'char':
151
                $fixed = true;
152 153 154 155 156 157
                break;
            case 'float':
            case 'double':
            case 'real':
            case 'decimal':
            case 'numeric':
158 159 160
                if (isset($tableColumn['length'])) {
                    list($precision, $scale) = array_map('trim', explode(', ', $tableColumn['length']));
                }
161 162 163 164
                $length = null;
                break;
        }

165
        $options = array(
166 167 168 169 170 171 172
            'length'   => $length,
            'unsigned' => (bool) $unsigned,
            'fixed'    => $fixed,
            'notnull'  => $notnull,
            'default'  => $default,
            'precision' => $precision,
            'scale'     => $scale,
173
            'autoincrement' => (bool) $tableColumn['pk'],
174
        );
175

176
        return new Column($tableColumn['name'], \Doctrine\DBAL\Types\Type::getType($type), $options);
177
    }
178 179 180 181 182

    protected function _getPortableViewDefinition($view)
    {
        return new View($view['name'], $view['sql']);
    }
183
}