# Source for file Mysql.php

Documentation is available at Mysql.php

1. <?php
2. /*
3. * $Id: Mysql.php 2081 2007-07-26 19:52:12Z zYne $
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.phpdoctrine.com>.
20. */
21. Doctrine::autoload('Doctrine_Import');
22. /**
23. * @package Doctrine
24. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
25. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
26. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
27. * @version $Revision: 2081 $
28. * @category Object Relational Mapping
29. * @link www.phpdoctrine.com
30. * @since 1.0
31. */
32. class Doctrine_Import_Mysql extends Doctrine_Import
33. {
34. protected $sql = array(
35. 'showDatabases' => '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 %sWHERE Table_type = 'VIEW'",
41. );
42. /**
43. * lists all database sequences
44. *
45. * @param string|null$database
46. * @return array
47. */
48. public function listSequences($database = null)
49. {
50. $query = 'SHOW TABLES';
51. if (!is_null($database)) {
52. $query .= ' FROM ' . $database;
53. }
54. $tableNames = $this->conn->fetchColumn($query);
56. return array_map(array($this->conn, 'fixSequenceName'), $tableNames);
57. }
58. /**
59. * lists table constraints
60. *
61. * @param string $table database table name
62. * @return array
63. */
64. public function listTableConstraints($table)
65. {
66. $keyName = 'Key_name';
67. $nonUnique = 'Non_unique';
68. if ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_FIX_CASE) {
69. if ($this->conn->options['field_case'] == CASE_LOWER) {
70. $keyName = strtolower($keyName);
71. $nonUnique = strtolower($nonUnique);
72. } else {
73. $keyName = strtoupper($keyName);
74. $nonUnique = strtoupper($nonUnique);
75. }
76. }
78. $table = $this->conn->quoteIdentifier($table, true);
79. $query = 'SHOW INDEX FROM ' . $table;
80. $indexes = $this->conn->fetchAssoc($query);
82. $result = array();
83. foreach ($indexes as $indexData) {
84. if (!$indexData[$nonUnique]) {
85. if ($indexData[$keyName] !== 'PRIMARY') {
86. $index = $this->conn->fixIndexName($indexData[$keyName]);
87. } else {
88. $index = 'PRIMARY';
89. }
90. if ( ! empty($index)) {
91. $result[] = $index;
92. }
93. }
94. }
95. return $result;
96. }
97. /**
98. * lists table foreign keys
99. *
100. * @param string $table database table name
101. * @return array
102. */
103. public function listTableForeignKeys($table)
104. {
105. $sql = 'SHOW CREATE TABLE ' . $this->conn->quoteIdentifier($table, true);
106. }
107. /**
108. * lists table constraints
109. *
110. * @param string $table database table name
111. * @return array
112. */
113. public function listTableColumns($table)
114. {
115. $sql = 'DESCRIBE ' . $this->conn->quoteIdentifier($table, true);
116. $result = $this->conn->fetchAssoc($sql);
118. $description = array();
119. foreach ($result as $key => $val) {
121. $val = array_change_key_case($val, CASE_LOWER);
123. $decl = $this->conn->dataDict->getPortableDeclaration($val);
125. $values = isset($decl['values']) ? $decl['values'] : array();
127. $description = array(
128. 'name' => $val['field'],
129. 'type' => $decl['type'][0],
130. 'alltypes' => $decl['type'],
131. 'ntype' => $val['type'],
132. 'length' => $decl['length'],
133. 'fixed' => $decl['fixed'],
134. 'unsigned' => $decl['unsigned'],
135. 'values' => $values,
136. 'primary' => (strtolower($val['key']) == 'pri'),
137. 'default' => $val['default'],
138. 'notnull' => (bool) ($val['null'] != 'YES'),
139. 'autoinc' => (bool) (strpos($val['extra'], 'auto_increment') !== false),
140. );
141. $columns[$val['field']] = $description;
142. }
145. return $columns;
146. }
147. /**
148. * lists table constraints
149. *
150. * @param string $table database table name
151. * @return array
152. */
153. public function listTableIndexes($table)
154. {
155. $keyName = 'Key_name';
156. $nonUnique = 'Non_unique';
157. if ($this->conn->options['portability'] & Doctrine::PORTABILITY_FIX_CASE) {
158. if ($this->conn->options['field_case'] == CASE_LOWER) {
159. $keyName = strtolower($keyName);
160. $nonUnique = strtolower($nonUnique);
161. } else {
162. $keyName = strtoupper($keyName);
163. $nonUnique = strtoupper($nonUnique);
164. }
165. }
167. $table = $this->conn->quoteIdentifier($table, true);
168. $query = 'SHOW INDEX FROM ' . $table;
169. $indexes = $this->conn->fetchAssoc($query);
172. $result = array();
173. foreach ($indexes as $indexData) {
174. if ($indexData[$nonUnique] && ($index = $this->conn->fixIndexName($indexData[$keyName]))) {
175. $result[] = $index;
176. }
177. }
178. return $result;
179. }
180. /**
181. * lists tables
182. *
183. * @param string|null$database
184. * @return array
185. */
186. public function listTables($database = null)
187. {
188. return $this->conn->fetchColumn($this->sql['listTables']);
189. }
190. /**
191. * lists database views
192. *
193. * @param string|null$database
194. * @return array
195. */
196. public function listViews($database = null)
197. {
198. if (!is_null($database)) {
199. $query = sprintf($this->sql['listViews'], ' FROM ' . $database);
200. }
202. return $this->conn->fetchColumn($query);
203. }
204. }