# Source for file Mssql.php

Documentation is available at Mssql.php

1. <?php
2. /*
3. * $Id: Mssql.php 1697 2007-06-14 20:18:25Z 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_Export');
22. /**
23. * Doctrine_Export_Mssql
24. *
25. * @package Doctrine
26. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
27. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
28. * @author Frank M. Kromann <frank@kromann.info> (PEAR MDB2 Mssql driver)
29. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
30. * @category Object Relational Mapping
31. * @link www.phpdoctrine.com
32. * @since 1.0
33. * @version $Revision: 1697 $
34. */
35. class Doctrine_Export_Mssql extends Doctrine_Export
36. {
37. /**
38. * create a new database
39. *
40. * @param string $name name of the database that should be created
41. * @return void
42. */
43. public function createDatabase($name)
44. {
45. $name = $this->conn->quoteIdentifier($name, true);
46. $query = "CREATE DATABASE $name";
47. if ($this->conn->options['database_device']) {
48. $query.= ' ON '.$this->conn->options['database_device'];
49. $query.= $this->conn->options['database_size'] ? '=' .
50. $this->conn->options['database_size'] : '';
51. }
52. return $this->conn->standaloneQuery($query, null, true);
53. }
54. /**
55. * drop an existing database
56. *
57. * @param string $name name of the database that should be dropped
58. * @return void
59. */
60. public function dropDatabase($name)
61. {
62. $name = $this->conn->quoteIdentifier($name, true);
63. return $this->conn->standaloneQuery('DROP DATABASE ' . $name, null, true);
64. }
66. /**
67. * Override the parent method.
68. *
69. * @return string The string required to be placed between "CREATE" and "TABLE"
70. * to generate a temporary table, if possible.
71. */
72. public function getTemporaryTableQuery()
73. {
74. return '';
75. }
76. /**
77. * alter an existing table
78. *
79. * @param string $name name of the table that is intended to be changed.
80. * @param array $changes associative array that contains the details of each type
81. * of change that is intended to be performed. The types of
82. * changes that are currently supported are defined as follows:
83. *
84. * name
85. *
86. * New name for the table.
87. *
88. * add
89. *
90. * Associative array with the names of fields to be added as
91. * indexes of the array. The value of each entry of the array
92. * should be set to another associative array with the properties
93. * of the fields to be added. The properties of the fields should
94. * be the same as defined by the Metabase parser.
95. *
96. *
97. * remove
98. *
99. * Associative array with the names of fields to be removed as indexes
100. * of the array. Currently the values assigned to each entry are ignored.
101. * An empty array should be used for future compatibility.
102. *
103. * rename
104. *
105. * Associative array with the names of fields to be renamed as indexes
106. * of the array. The value of each entry of the array should be set to
107. * another associative array with the entry named name with the new
108. * field name and the entry named Declaration that is expected to contain
109. * the portion of the field declaration already in DBMS specific SQL code
110. * as it is used in the CREATE TABLE statement.
111. *
112. * change
113. *
114. * Associative array with the names of the fields to be changed as indexes
115. * of the array. Keep in mind that if it is intended to change either the
116. * name of a field and any other properties, the change array entries
117. * should have the new names of the fields as array indexes.
118. *
119. * The value of each entry of the array should be set to another associative
120. * array with the properties of the fields to that are meant to be changed as
121. * array entries. These entries should be assigned to the new values of the
122. * respective properties. The properties of the fields should be the same
123. * as defined by the Metabase parser.
124. *
125. * Example
126. * array(
127. * 'name' => 'userlist',
128. * 'add' => array(
129. * 'quota' => array(
130. * 'type' => 'integer',
131. * 'unsigned' => 1
132. * )
133. * ),
134. * 'remove' => array(
135. * 'file_limit' => array(),
136. * 'time_limit' => array()
137. * ),
138. * 'change' => array(
139. * 'name' => array(
140. * 'length' => '20',
141. * 'definition' => array(
142. * 'type' => 'text',
143. * 'length' => 20,
144. * ),
145. * )
146. * ),
147. * 'rename' => array(
148. * 'sex' => array(
149. * 'name' => 'gender',
150. * 'definition' => array(
151. * 'type' => 'text',
152. * 'length' => 1,
153. * 'default' => 'M',
154. * ),
155. * )
156. * )
157. * )
158. *
159. * @param boolean $check indicates whether the function should just check if the DBMS driver
160. * can perform the requested table alterations if the value is true or
161. * actually perform them otherwise.
162. * @return void
163. */
164. public function alterTable($name, array $changes, $check)
165. {
166. foreach ($changes as $changeName => $change) {
167. switch ($changeName) {
168. case 'add':
169. break;
170. case 'remove':
171. break;
172. case 'name':
173. case 'rename':
174. case 'change':
175. default:
176. throw new Doctrine_Export_Exception('alterTable: change type "' . $changeName . '" not yet supported');
177. }
178. }
180. $query = '';
181. if ( ! empty($changes['add']) && is_array($changes['add'])) {
182. foreach ($changes['add'] as $fieldName => $field) {
183. if ($query) {
184. $query .= ', ';
185. }
186. $query .= 'ADD ' . $this->conn->getDeclaration($field['type'], $fieldName, $field);
187. }
188. }
190. if ( ! empty($changes['remove']) && is_array($changes['remove'])) {
191. foreach ($changes['remove'] as $fieldName => $field) {
192. if ($query) {
193. $query .= ', ';
194. }
195. $field_name = $this->conn->quoteIdentifier($fieldName, true);
196. $query .= 'DROP COLUMN ' . $fieldName;
197. }
198. }
200. if ( ! $query) {
201. return false;
202. }
204. $name = $this->conn->quoteIdentifier($name, true);
205. return $this->conn->exec('ALTER TABLE ' . $name . ' ' . $query);
206. }
207. /**
208. * create sequence
209. *
210. * @param string $seqName name of the sequence to be created
211. * @param string $start start value of the sequence; default is 1
212. * @param array $options An associative array of table options:
213. * array(
214. * 'comment' => 'Foo',
215. * 'charset' => 'utf8',
216. * 'collate' => 'utf8_unicode_ci',
217. * );
218. * @return string
219. */
220. public function createSequence($seqName, $start = 1, array $options = array())
221. {
222. $sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($seqName), true);
223. $seqcolName = $this->conn->quoteIdentifier($this->conn->options['seqcol_name'], true);
224. $query = 'CREATE TABLE ' . $sequenceName . ' (' . $seqcolName .
225. ' INT PRIMARY KEY CLUSTERED IDENTITY(' . $start . ', 1) NOT NULL)';
227. $res = $this->conn->exec($query);
229. if ($start == 1) {
230. return true;
231. }
233. try {
234. $query = 'SET IDENTITY_INSERT ' . $sequenceName . ' ON ' .
235. 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES ( ' . $start . ')';
236. $res = $this->conn->exec($query);
237. } catch (Exception $e) {
238. $result = $this->conn->exec('DROP TABLE ' . $sequenceName);
239. }
240. return true;
241. }
242. /**
243. * This function drops an existing sequence
244. *
245. * @param string $seqName name of the sequence to be dropped
246. * @return void
247. */
248. public function dropSequenceSql($seqName)
249. {
250. $sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($seqName), true);
251. return 'DROP TABLE ' . $sequenceName;
252. }
253. }