| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: Mysql.php 7644 2010-06-08 15:12:02Z jwage $
|
|
|
4 |
*
|
|
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
16 |
*
|
|
|
17 |
* This software consists of voluntary contributions made by many individuals
|
|
|
18 |
* and is licensed under the LGPL. For more information, see
|
|
|
19 |
* <http://www.doctrine-project.org>.
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* @package Doctrine
|
|
|
24 |
* @subpackage Import
|
|
|
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 |
* @version $Revision: 7644 $
|
|
|
29 |
* @link www.doctrine-project.org
|
|
|
30 |
* @since 1.0
|
|
|
31 |
*/
|
|
|
32 |
class Doctrine_Import_Mysql extends Doctrine_Import
|
|
|
33 |
{
|
|
|
34 |
protected $sql = array(
|
|
|
35 |
'listDatabases' => 'SHOW DATABASES',
|
|
|
36 |
'listTableFields' => 'DESCRIBE %s',
|
|
|
37 |
'listSequences' => 'SHOW TABLES',
|
|
|
38 |
'listTables' => 'SHOW TABLES',
|
|
|
39 |
'listUsers' => 'SELECT DISTINCT USER FROM USER',
|
|
|
40 |
'listViews' => "SHOW FULL TABLES %s WHERE Table_type = 'VIEW'",
|
|
|
41 |
);
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* lists all database sequences
|
|
|
45 |
*
|
|
|
46 |
* @param string|null $database
|
|
|
47 |
* @return array
|
|
|
48 |
*/
|
|
|
49 |
public function listSequences($database = null)
|
|
|
50 |
{
|
|
|
51 |
$query = 'SHOW TABLES';
|
|
|
52 |
if ( ! is_null($database)) {
|
|
|
53 |
$query .= ' FROM ' . $database;
|
|
|
54 |
}
|
|
|
55 |
$tableNames = $this->conn->fetchColumn($query);
|
|
|
56 |
|
|
|
57 |
return array_map(array($this->conn->formatter, 'fixSequenceName'), $tableNames);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* lists table constraints
|
|
|
62 |
*
|
|
|
63 |
* @param string $table database table name
|
|
|
64 |
* @return array
|
|
|
65 |
*/
|
|
|
66 |
public function listTableConstraints($table)
|
|
|
67 |
{
|
|
|
68 |
$keyName = 'Key_name';
|
|
|
69 |
$nonUnique = 'Non_unique';
|
|
|
70 |
if ($this->conn->getAttribute(Doctrine_Core::ATTR_FIELD_CASE) && ($this->conn->getAttribute(Doctrine_Core::ATTR_PORTABILITY) & Doctrine_Core::PORTABILITY_FIX_CASE)) {
|
|
|
71 |
if ($this->conn->getAttribute(Doctrine_Core::ATTR_FIELD_CASE) == CASE_LOWER) {
|
|
|
72 |
$keyName = strtolower($keyName);
|
|
|
73 |
$nonUnique = strtolower($nonUnique);
|
|
|
74 |
} else {
|
|
|
75 |
$keyName = strtoupper($keyName);
|
|
|
76 |
$nonUnique = strtoupper($nonUnique);
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$table = $this->conn->quoteIdentifier($table, true);
|
|
|
81 |
$query = 'SHOW INDEX FROM ' . $table;
|
|
|
82 |
$indexes = $this->conn->fetchAssoc($query);
|
|
|
83 |
|
|
|
84 |
$result = array();
|
|
|
85 |
foreach ($indexes as $indexData) {
|
|
|
86 |
if ( ! $indexData[$nonUnique]) {
|
|
|
87 |
if ($indexData[$keyName] !== 'PRIMARY') {
|
|
|
88 |
$index = $this->conn->formatter->fixIndexName($indexData[$keyName]);
|
|
|
89 |
} else {
|
|
|
90 |
$index = 'PRIMARY';
|
|
|
91 |
}
|
|
|
92 |
if ( ! empty($index)) {
|
|
|
93 |
$result[] = $index;
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
}
|
|
|
97 |
return $result;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* lists table relations
|
|
|
102 |
*
|
|
|
103 |
* Expects an array of this format to be returned with all the relationships in it where the key is
|
|
|
104 |
* the name of the foreign table, and the value is an array containing the local and foreign column
|
|
|
105 |
* name
|
|
|
106 |
*
|
|
|
107 |
* Array
|
|
|
108 |
* (
|
|
|
109 |
* [groups] => Array
|
|
|
110 |
* (
|
|
|
111 |
* [local] => group_id
|
|
|
112 |
* [foreign] => id
|
|
|
113 |
* )
|
|
|
114 |
* )
|
|
|
115 |
*
|
|
|
116 |
* @param string $table database table name
|
|
|
117 |
* @return array
|
|
|
118 |
*/
|
|
|
119 |
public function listTableRelations($tableName)
|
|
|
120 |
{
|
|
|
121 |
$relations = array();
|
|
|
122 |
$sql = "SELECT column_name, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM information_schema.key_column_usage WHERE table_name = '" . $tableName . "' AND table_schema = '" . $this->conn->getDatabaseName() . "' and REFERENCED_COLUMN_NAME is not NULL";
|
|
|
123 |
$results = $this->conn->fetchAssoc($sql);
|
|
|
124 |
foreach ($results as $result)
|
|
|
125 |
{
|
|
|
126 |
$result = array_change_key_case($result, CASE_LOWER);
|
|
|
127 |
$relations[] = array('table' => $result['referenced_table_name'],
|
|
|
128 |
'local' => $result['column_name'],
|
|
|
129 |
'foreign' => $result['referenced_column_name']);
|
|
|
130 |
}
|
|
|
131 |
return $relations;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
/**
|
|
|
135 |
* lists table constraints
|
|
|
136 |
*
|
|
|
137 |
* @param string $table database table name
|
|
|
138 |
* @return array
|
|
|
139 |
*/
|
|
|
140 |
public function listTableColumns($table)
|
|
|
141 |
{
|
|
|
142 |
$sql = 'DESCRIBE ' . $this->conn->quoteIdentifier($table, true);
|
|
|
143 |
$result = $this->conn->fetchAssoc($sql);
|
|
|
144 |
|
|
|
145 |
$description = array();
|
|
|
146 |
$columns = array();
|
|
|
147 |
foreach ($result as $key => $val) {
|
|
|
148 |
|
|
|
149 |
$val = array_change_key_case($val, CASE_LOWER);
|
|
|
150 |
|
|
|
151 |
$decl = $this->conn->dataDict->getPortableDeclaration($val);
|
|
|
152 |
|
|
|
153 |
$values = isset($decl['values']) ? $decl['values'] : array();
|
|
|
154 |
$val['default'] = $val['default'] == 'CURRENT_TIMESTAMP' ? null : $val['default'];
|
|
|
155 |
|
|
|
156 |
$description = array(
|
|
|
157 |
'name' => $val['field'],
|
|
|
158 |
'type' => $decl['type'][0],
|
|
|
159 |
'alltypes' => $decl['type'],
|
|
|
160 |
'ntype' => $val['type'],
|
|
|
161 |
'length' => $decl['length'],
|
|
|
162 |
'fixed' => (bool) $decl['fixed'],
|
|
|
163 |
'unsigned' => (bool) $decl['unsigned'],
|
|
|
164 |
'values' => $values,
|
|
|
165 |
'primary' => (strtolower($val['key']) == 'pri'),
|
|
|
166 |
'default' => $val['default'],
|
|
|
167 |
'notnull' => (bool) ($val['null'] != 'YES'),
|
|
|
168 |
'autoincrement' => (bool) (strpos($val['extra'], 'auto_increment') !== false),
|
|
|
169 |
);
|
|
|
170 |
if (isset($decl['scale'])) {
|
|
|
171 |
$description['scale'] = $decl['scale'];
|
|
|
172 |
}
|
|
|
173 |
$columns[$val['field']] = $description;
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
return $columns;
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
/**
|
|
|
180 |
* lists table constraints
|
|
|
181 |
*
|
|
|
182 |
* @param string $table database table name
|
|
|
183 |
* @return array
|
|
|
184 |
*/
|
|
|
185 |
public function listTableIndexes($table)
|
|
|
186 |
{
|
|
|
187 |
$keyName = 'Key_name';
|
|
|
188 |
$nonUnique = 'Non_unique';
|
|
|
189 |
if ($this->conn->getAttribute(Doctrine_Core::ATTR_FIELD_CASE) && ($this->conn->getAttribute(Doctrine_Core::ATTR_PORTABILITY) & Doctrine_Core::PORTABILITY_FIX_CASE)) {
|
|
|
190 |
if ($this->conn->getAttribute(Doctrine_Core::ATTR_FIELD_CASE) == CASE_LOWER) {
|
|
|
191 |
$keyName = strtolower($keyName);
|
|
|
192 |
$nonUnique = strtolower($nonUnique);
|
|
|
193 |
} else {
|
|
|
194 |
$keyName = strtoupper($keyName);
|
|
|
195 |
$nonUnique = strtoupper($nonUnique);
|
|
|
196 |
}
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
$table = $this->conn->quoteIdentifier($table, true);
|
|
|
200 |
$query = 'SHOW INDEX FROM ' . $table;
|
|
|
201 |
$indexes = $this->conn->fetchAssoc($query);
|
|
|
202 |
|
|
|
203 |
|
|
|
204 |
$result = array();
|
|
|
205 |
foreach ($indexes as $indexData) {
|
|
|
206 |
if ($indexData[$nonUnique] && ($index = $this->conn->formatter->fixIndexName($indexData[$keyName]))) {
|
|
|
207 |
$result[] = $index;
|
|
|
208 |
}
|
|
|
209 |
}
|
|
|
210 |
return $result;
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
/**
|
|
|
214 |
* lists tables
|
|
|
215 |
*
|
|
|
216 |
* @param string|null $database
|
|
|
217 |
* @return array
|
|
|
218 |
*/
|
|
|
219 |
public function listTables($database = null)
|
|
|
220 |
{
|
|
|
221 |
return $this->conn->fetchColumn($this->sql['listTables']);
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
/**
|
|
|
225 |
* lists database views
|
|
|
226 |
*
|
|
|
227 |
* @param string|null $database
|
|
|
228 |
* @return array
|
|
|
229 |
*/
|
|
|
230 |
public function listViews($database = null)
|
|
|
231 |
{
|
|
|
232 |
if (is_null($database)) {
|
|
|
233 |
$query = 'SELECT table_name FROM information_schema.VIEWS';
|
|
|
234 |
} else {
|
|
|
235 |
$query = sprintf($this->sql['listViews'], ' FROM ' . $database);
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
return $this->conn->fetchColumn($query);
|
|
|
239 |
}
|
|
|
240 |
}
|