# Source for file Mssql.php

Documentation is available at Mssql.php

1. <?php
2. /*
3. * $Id: Mssql.php 1889 2007-06-28 12:11:55Z 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. * @author Frank M. Kromann <frank@kromann.info> (PEAR MDB2 Mssql driver)
28. * @author David Coallier <davidc@php.net> (PEAR MDB2 Mssql driver)
29. * @version $Revision: 1889 $
30. * @category Object Relational Mapping
31. * @link www.phpdoctrine.com
32. * @since 1.0
33. */
34. class Doctrine_Import_Mssql extends Doctrine_Import
35. {
36. /**
37. * lists all database sequences
38. *
39. * @param string|null$database
40. * @return array
41. */
42. public function listSequences($database = null)
43. {
44. $query = "SELECT name FROM sysobjects WHERE xtype = 'U'";
45. $tableNames = $this->conn->fetchColumn($query);
47. return array_map(array($this->conn->formatter, 'fixSequenceName'), $tableNames);
48. }
49. /**
50. * lists table constraints
51. *
52. * @param string $table database table name
53. * @return array
54. */
55. public function listTableColumns($table)
56. {
57. $sql = 'EXEC sp_columns @table_name = ' . $this->conn->quoteIdentifier($table, true);
58. $result = $this->conn->fetchAssoc($sql);
59. $columns = array();
61. foreach ($result as $key => $val) {
62. $val = array_change_key_case($val, CASE_LOWER);
64. if (strstr($val['type_name'], ' ')) {
65. list($type, $identity) = explode(' ', $val['type_name']);
66. } else {
67. $type = $val['type_name'];
68. $identity = '';
69. }
71. if ($type == 'varchar') {
72. $type .= '(' . $val['length'] . ')';
73. }
75. $decl = $this->conn->dataDict->getPortableDeclaration($val);
77. $description = array(
78. 'name' => $val['column_name'],
79. 'ntype' => $type,
80. 'type' => $decl['type'][0],
81. 'alltypes' => $decl['type'],
82. 'length' => $decl['length'],
83. 'fixed' => $decl['fixed'],
84. 'unsigned' => $decl['unsigned'],
85. 'notnull' => (bool) ($val['is_nullable'] === 'NO'),
86. 'default' => $val['column_def'],
87. 'primary' => (strtolower($identity) == 'identity'),
88. );
89. $columns[$val['column_name']] = $description;
90. }
92. return $columns;
93. }
94. /**
95. * lists table constraints
96. *
97. * @param string $table database table name
98. * @return array
99. */
100. public function listTableIndexes($table)
101. {
103. }
104. /**
105. * lists tables
106. *
107. * @param string|null$database
108. * @return array
109. */
110. public function listTables($database = null)
111. {
112. $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
114. return $this->conn->fetchColumn($sql);
115. }
116. /**
117. * lists all triggers
118. *
119. * @return array
120. */
121. public function listTriggers($database = null)
122. {
123. $query = "SELECT name FROM sysobjects WHERE xtype = 'TR'";
125. $result = $this->conn->fetchColumn($query);
127. return $result;
128. }
129. /**
130. * lists table triggers
131. *
132. * @param string $table database table name
133. * @return array
134. */
135. public function listTableTriggers($table)
136. {
137. $table = $this->conn->quote($table, 'text');
138. $query = "SELECT name FROM sysobjects WHERE xtype = 'TR' AND object_name(parent_obj) = " . $table;
140. $result = $this->conn->fetchColumn($query);
142. return $result;
143. }
144. /**
145. * lists table views
146. *
147. * @param string $table database table name
148. * @return array
149. */
150. public function listTableViews($table)
151. {
152. $keyName = 'INDEX_NAME';
153. $pkName = 'PK_NAME';
154. if ($this->conn->options['portability'] & Doctrine::PORTABILITY_FIX_CASE) {
155. if ($this->conn->options['field_case'] == CASE_LOWER) {
156. $keyName = strtolower($keyName);
157. $pkName = strtolower($pkName);
158. } else {
159. $keyName = strtoupper($keyName);
160. $pkName = strtoupper($pkName);
161. }
162. }
163. $table = $this->conn->quote($table, 'text');
164. $query = 'EXEC sp_statistics @table_name = ' . $table;
165. $indexes = $this->conn->fetchColumn($query, $keyName);
167. $query = 'EXEC sp_pkeys @table_name = ' . $table;
168. $pkAll = $this->conn->fetchColumn($query, $pkName);
170. $result = array();
172. foreach ($indexes as $index) {
173. if (!in_array($index, $pkAll) && $index != null) {
174. $result[] = $this->conn->formatter->fixIndexName($index);
175. }
176. }
178. return $result;
179. }
180. /**
181. * lists database views
182. *
183. * @param string|null$database
184. * @return array
185. */
186. public function listViews($database = null)
187. {
188. $query = "SELECT name FROM sysobjects WHERE xtype = 'V'";
190. return $this->conn->fetchColumn($query);
191. }
192. }