Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / Schema / SqliteSchemaManager.php
1 <?php
2 /*
3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * This software consists of voluntary contributions made by many individuals
16  * and is licensed under the MIT license. For more information, see
17  * <http://www.doctrine-project.org>.
18  */
19
20 namespace Doctrine\DBAL\Schema;
21
22 /**
23  * SqliteSchemaManager
24  *
25  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
26  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
27  * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
28  * @author      Jonathan H. Wage <jonwage@gmail.com>
29  * @version     $Revision$
30  * @since       2.0
31  */
32 class SqliteSchemaManager extends AbstractSchemaManager
33 {
34     /**
35      * {@inheritdoc}
36      *
37      * @override
38      */
39     public function dropDatabase($database)
40     {
41         if (file_exists($database)) {
42             unlink($database);
43         }
44     }
45
46     /**
47      * {@inheritdoc}
48      *
49      * @override
50      */
51     public function createDatabase($database)
52     {
53         $params = $this->_conn->getParams();
54         $driver = $params['driver'];
55         $options = array(
56             'driver' => $driver,
57             'path' => $database
58         );
59         $conn = \Doctrine\DBAL\DriverManager::getConnection($options);
60         $conn->connect();
61         $conn->close();
62     }
63
64     protected function _getPortableTableDefinition($table)
65     {
66         return $table['name'];
67     }
68
69     /**
70      * @license New BSD License
71      * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html
72      * @param  array $tableIndexes
73      * @param  string $tableName
74      * @return array
75      */
76     protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
77     {
78         $indexBuffer = array();
79
80         // fetch primary
81         $stmt = $this->_conn->executeQuery( "PRAGMA TABLE_INFO ('$tableName')" );
82         $indexArray = $stmt->fetchAll(\PDO::FETCH_ASSOC);
83         foreach($indexArray as $indexColumnRow) {
84             if($indexColumnRow['pk'] == "1") {
85                 $indexBuffer[] = array(
86                     'key_name' => 'primary',
87                     'primary' => true,
88                     'non_unique' => false,
89                     'column_name' => $indexColumnRow['name']
90                 );
91             }
92         }
93
94         // fetch regular indexes
95         foreach($tableIndexes as $tableIndex) {
96             // Ignore indexes with reserved names, e.g. autoindexes
97             if (strpos($tableIndex['name'], 'sqlite_') !== 0) {
98                 $keyName = $tableIndex['name'];
99                 $idx = array();
100                 $idx['key_name'] = $keyName;
101                 $idx['primary'] = false;
102                 $idx['non_unique'] = $tableIndex['unique']?false:true;
103
104                 $stmt = $this->_conn->executeQuery( "PRAGMA INDEX_INFO ( '{$keyName}' )" );
105                 $indexArray = $stmt->fetchAll(\PDO::FETCH_ASSOC);
106
107                 foreach ( $indexArray as $indexColumnRow ) {
108                     $idx['column_name'] = $indexColumnRow['name'];
109                     $indexBuffer[] = $idx;
110                 }
111             }
112         }
113
114         return parent::_getPortableTableIndexesList($indexBuffer, $tableName);
115     }
116
117     protected function _getPortableTableIndexDefinition($tableIndex)
118     {
119         return array(
120             'name' => $tableIndex['name'],
121             'unique' => (bool) $tableIndex['unique']
122         );
123     }
124
125     protected function _getPortableTableColumnDefinition($tableColumn)
126     {
127         $e = explode('(', $tableColumn['type']);
128         $tableColumn['type'] = $e[0];
129         if (isset($e[1])) {
130             $length = trim($e[1], ')');
131             $tableColumn['length'] = $length;
132         }
133
134         $dbType = strtolower($tableColumn['type']);
135         $length = isset($tableColumn['length']) ? $tableColumn['length'] : null;
136         $unsigned = (boolean) isset($tableColumn['unsigned']) ? $tableColumn['unsigned'] : false;
137         $fixed = false;
138         $type = $this->_platform->getDoctrineTypeMapping($dbType);
139         $default = $tableColumn['dflt_value'];
140         if  ($default == 'NULL') {
141             $default = null;
142         }
143         if ($default !== null) {
144             // SQLite returns strings wrapped in single quotes, so we need to strip them
145             $default = preg_replace("/^'(.*)'$/", '\1', $default);
146         }
147         $notnull = (bool) $tableColumn['notnull'];
148
149         if ( ! isset($tableColumn['name'])) {
150             $tableColumn['name'] = '';
151         }
152
153         $precision = null;
154         $scale = null;
155
156         switch ($dbType) {
157             case 'char':
158                 $fixed = true;
159                 break;
160             case 'float':
161             case 'double':
162             case 'real':
163             case 'decimal':
164             case 'numeric':
165                 if (isset($tableColumn['length'])) {
166                     list($precision, $scale) = array_map('trim', explode(', ', $tableColumn['length']));
167                 }
168                 $length = null;
169                 break;
170         }
171
172         $options = array(
173             'length'   => $length,
174             'unsigned' => (bool) $unsigned,
175             'fixed'    => $fixed,
176             'notnull'  => $notnull,
177             'default'  => $default,
178             'precision' => $precision,
179             'scale'     => $scale,
180             'autoincrement' => false,
181         );
182
183         return new Column($tableColumn['name'], \Doctrine\DBAL\Types\Type::getType($type), $options);
184     }
185
186     protected function _getPortableViewDefinition($view)
187     {
188         return new View($view['name'], $view['sql']);
189     }
190 }