|
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 LGPL. 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 $keyName = $tableIndex['name']; |
|
97 $idx = array(); |
|
98 $idx['key_name'] = $keyName; |
|
99 $idx['primary'] = false; |
|
100 $idx['non_unique'] = $tableIndex['unique']?false:true; |
|
101 |
|
102 $stmt = $this->_conn->executeQuery( "PRAGMA INDEX_INFO ( '{$keyName}' )" ); |
|
103 $indexArray = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
|
104 |
|
105 foreach ( $indexArray as $indexColumnRow ) { |
|
106 $idx['column_name'] = $indexColumnRow['name']; |
|
107 $indexBuffer[] = $idx; |
|
108 } |
|
109 } |
|
110 |
|
111 return parent::_getPortableTableIndexesList($indexBuffer, $tableName); |
|
112 } |
|
113 |
|
114 protected function _getPortableTableIndexDefinition($tableIndex) |
|
115 { |
|
116 return array( |
|
117 'name' => $tableIndex['name'], |
|
118 'unique' => (bool) $tableIndex['unique'] |
|
119 ); |
|
120 } |
|
121 |
|
122 protected function _getPortableTableColumnDefinition($tableColumn) |
|
123 { |
|
124 $e = explode('(', $tableColumn['type']); |
|
125 $tableColumn['type'] = $e[0]; |
|
126 if (isset($e[1])) { |
|
127 $length = trim($e[1], ')'); |
|
128 $tableColumn['length'] = $length; |
|
129 } |
|
130 |
|
131 $dbType = strtolower($tableColumn['type']); |
|
132 $length = isset($tableColumn['length']) ? $tableColumn['length'] : null; |
|
133 $unsigned = (boolean) isset($tableColumn['unsigned']) ? $tableColumn['unsigned'] : false; |
|
134 $fixed = false; |
|
135 $type = $this->_platform->getDoctrineTypeMapping($dbType); |
|
136 $default = $tableColumn['dflt_value']; |
|
137 if ($default == 'NULL') { |
|
138 $default = null; |
|
139 } |
|
140 $notnull = (bool) $tableColumn['notnull']; |
|
141 |
|
142 if ( ! isset($tableColumn['name'])) { |
|
143 $tableColumn['name'] = ''; |
|
144 } |
|
145 |
|
146 $precision = null; |
|
147 $scale = null; |
|
148 |
|
149 switch ($dbType) { |
|
150 case 'char': |
|
151 $fixed = true; |
|
152 break; |
|
153 case 'float': |
|
154 case 'double': |
|
155 case 'real': |
|
156 case 'decimal': |
|
157 case 'numeric': |
|
158 if (isset($tableColumn['length'])) { |
|
159 list($precision, $scale) = array_map('trim', explode(', ', $tableColumn['length'])); |
|
160 } |
|
161 $length = null; |
|
162 break; |
|
163 } |
|
164 |
|
165 $options = array( |
|
166 'length' => $length, |
|
167 'unsigned' => (bool) $unsigned, |
|
168 'fixed' => $fixed, |
|
169 'notnull' => $notnull, |
|
170 'default' => $default, |
|
171 'precision' => $precision, |
|
172 '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 protected function _getPortableViewDefinition($view) |
|
180 { |
|
181 return new View($view['name'], $view['sql']); |
|
182 } |
|
183 } |