| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: Mssql.php 7660 2010-06-08 18:30:22Z 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 |
* Doctrine_Export_Mssql
|
|
|
24 |
*
|
|
|
25 |
* @package Doctrine
|
|
|
26 |
* @subpackage Export
|
|
|
27 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
28 |
* @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
|
|
|
29 |
* @author Frank M. Kromann <frank@kromann.info> (PEAR MDB2 Mssql driver)
|
|
|
30 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
31 |
* @link www.doctrine-project.org
|
|
|
32 |
* @since 1.0
|
|
|
33 |
* @version $Revision: 7660 $
|
|
|
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 |
$options = $this->conn->getOptions();
|
|
|
48 |
if (isset($options['database_device']) && $options['database_device']) {
|
|
|
49 |
$query.= ' ON '.$this->conn->options['database_device'];
|
|
|
50 |
$query.= $this->conn->options['database_size'] ? '=' .
|
|
|
51 |
$this->conn->options['database_size'] : '';
|
|
|
52 |
}
|
|
|
53 |
return $this->conn->standaloneQuery($query, array(), true);
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* drop an existing database
|
|
|
58 |
*
|
|
|
59 |
* @param string $name name of the database that should be dropped
|
|
|
60 |
* @return void
|
|
|
61 |
*/
|
|
|
62 |
public function dropDatabase($name)
|
|
|
63 |
{
|
|
|
64 |
$name = $this->conn->quoteIdentifier($name, true);
|
|
|
65 |
return $this->conn->standaloneQuery('DROP DATABASE ' . $name, array(), true);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Override the parent method.
|
|
|
70 |
*
|
|
|
71 |
* @return string The string required to be placed between "CREATE" and "TABLE"
|
|
|
72 |
* to generate a temporary table, if possible.
|
|
|
73 |
*/
|
|
|
74 |
public function getTemporaryTableQuery()
|
|
|
75 |
{
|
|
|
76 |
return '';
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
public function dropIndexSql($table, $name)
|
|
|
80 |
{
|
|
|
81 |
$name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name));
|
|
|
82 |
$table = $this->conn->quoteIdentifier($table);
|
|
|
83 |
|
|
|
84 |
return 'DROP INDEX ' . $name . ' ON ' . $table;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* alter an existing table
|
|
|
89 |
*
|
|
|
90 |
* @param string $name name of the table that is intended to be changed.
|
|
|
91 |
* @param array $changes associative array that contains the details of each type
|
|
|
92 |
* of change that is intended to be performed. The types of
|
|
|
93 |
* changes that are currently supported are defined as follows:
|
|
|
94 |
*
|
|
|
95 |
* name
|
|
|
96 |
*
|
|
|
97 |
* New name for the table.
|
|
|
98 |
*
|
|
|
99 |
* add
|
|
|
100 |
*
|
|
|
101 |
* Associative array with the names of fields to be added as
|
|
|
102 |
* indexes of the array. The value of each entry of the array
|
|
|
103 |
* should be set to another associative array with the properties
|
|
|
104 |
* of the fields to be added. The properties of the fields should
|
|
|
105 |
* be the same as defined by the Metabase parser.
|
|
|
106 |
*
|
|
|
107 |
*
|
|
|
108 |
* remove
|
|
|
109 |
*
|
|
|
110 |
* Associative array with the names of fields to be removed as indexes
|
|
|
111 |
* of the array. Currently the values assigned to each entry are ignored.
|
|
|
112 |
* An empty array should be used for future compatibility.
|
|
|
113 |
*
|
|
|
114 |
* rename
|
|
|
115 |
*
|
|
|
116 |
* Associative array with the names of fields to be renamed as indexes
|
|
|
117 |
* of the array. The value of each entry of the array should be set to
|
|
|
118 |
* another associative array with the entry named name with the new
|
|
|
119 |
* field name and the entry named Declaration that is expected to contain
|
|
|
120 |
* the portion of the field declaration already in DBMS specific SQL code
|
|
|
121 |
* as it is used in the CREATE TABLE statement.
|
|
|
122 |
*
|
|
|
123 |
* change
|
|
|
124 |
*
|
|
|
125 |
* Associative array with the names of the fields to be changed as indexes
|
|
|
126 |
* of the array. Keep in mind that if it is intended to change either the
|
|
|
127 |
* name of a field and any other properties, the change array entries
|
|
|
128 |
* should have the new names of the fields as array indexes.
|
|
|
129 |
*
|
|
|
130 |
* The value of each entry of the array should be set to another associative
|
|
|
131 |
* array with the properties of the fields to that are meant to be changed as
|
|
|
132 |
* array entries. These entries should be assigned to the new values of the
|
|
|
133 |
* respective properties. The properties of the fields should be the same
|
|
|
134 |
* as defined by the Metabase parser.
|
|
|
135 |
*
|
|
|
136 |
* Example
|
|
|
137 |
* array(
|
|
|
138 |
* 'name' => 'userlist',
|
|
|
139 |
* 'add' => array(
|
|
|
140 |
* 'quota' => array(
|
|
|
141 |
* 'type' => 'integer',
|
|
|
142 |
* 'unsigned' => 1
|
|
|
143 |
* )
|
|
|
144 |
* ),
|
|
|
145 |
* 'remove' => array(
|
|
|
146 |
* 'file_limit' => array(),
|
|
|
147 |
* 'time_limit' => array()
|
|
|
148 |
* ),
|
|
|
149 |
* 'change' => array(
|
|
|
150 |
* 'name' => array(
|
|
|
151 |
* 'length' => '20',
|
|
|
152 |
* 'definition' => array(
|
|
|
153 |
* 'type' => 'text',
|
|
|
154 |
* 'length' => 20,
|
|
|
155 |
* ),
|
|
|
156 |
* )
|
|
|
157 |
* ),
|
|
|
158 |
* 'rename' => array(
|
|
|
159 |
* 'sex' => array(
|
|
|
160 |
* 'name' => 'gender',
|
|
|
161 |
* 'definition' => array(
|
|
|
162 |
* 'type' => 'text',
|
|
|
163 |
* 'length' => 1,
|
|
|
164 |
* 'default' => 'M',
|
|
|
165 |
* ),
|
|
|
166 |
* )
|
|
|
167 |
* )
|
|
|
168 |
* )
|
|
|
169 |
*
|
|
|
170 |
* @param boolean $check indicates whether the function should just check if the DBMS driver
|
|
|
171 |
* can perform the requested table alterations if the value is true or
|
|
|
172 |
* actually perform them otherwise.
|
|
|
173 |
* @return void
|
|
|
174 |
*/
|
|
|
175 |
public function alterTable($name, array $changes, $check = false)
|
|
|
176 |
{
|
|
|
177 |
if ( !$name ) {
|
|
|
178 |
throw new Doctrine_Export_Exception('no valid table name specified');
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
foreach ($changes as $changeName => $change) {
|
|
|
182 |
switch ($changeName) {
|
|
|
183 |
case 'add':
|
|
|
184 |
case 'remove':
|
|
|
185 |
case 'name':
|
|
|
186 |
case 'rename':
|
|
|
187 |
case 'change':
|
|
|
188 |
break;
|
|
|
189 |
default:
|
|
|
190 |
throw new Doctrine_Export_Exception('alterTable: change type "' . $changeName . '" not yet supported');
|
|
|
191 |
}
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
if ($check) {
|
|
|
195 |
return true;
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
|
|
|
199 |
$query = '';
|
|
|
200 |
$postQueries = ''; //SQL Server uses a stored procedure to rename objects
|
|
|
201 |
|
|
|
202 |
if ( ! empty($changes['name'])) {
|
|
|
203 |
$changeName = $this->conn->quoteIdentifier($changes['name'], true);
|
|
|
204 |
|
|
|
205 |
$postQueries .= sprintf(
|
|
|
206 |
"EXECUTE sp_RENAME '%s', '%s';",
|
|
|
207 |
$this->conn->quoteIdentifier($name),
|
|
|
208 |
$changeName
|
|
|
209 |
);
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
//ADD TABLE
|
|
|
213 |
if ( ! empty($changes['add']) && is_array($changes['add'])) {
|
|
|
214 |
foreach ($changes['add'] as $fieldName => $field) {
|
|
|
215 |
if ($query) {
|
|
|
216 |
$query .= ', ';
|
|
|
217 |
}
|
|
|
218 |
$query .= 'ADD ' . $this->getDeclaration($fieldName, $field);
|
|
|
219 |
}
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
//REMOVE TABLE
|
|
|
223 |
if ( ! empty($changes['remove']) && is_array($changes['remove'])) {
|
|
|
224 |
if ($query) {
|
|
|
225 |
$query .= ', ';
|
|
|
226 |
}
|
|
|
227 |
$query .= 'DROP COLUMN ';
|
|
|
228 |
|
|
|
229 |
$dropped = array();
|
|
|
230 |
foreach ($changes['remove'] as $fieldName => $field) {
|
|
|
231 |
|
|
|
232 |
$fieldName = $this->conn->quoteIdentifier($fieldName, true);
|
|
|
233 |
$dropped[] = $fieldName;
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
$query .= implode(', ', $dropped) . ' ';
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
$rename = array();
|
|
|
240 |
if ( ! empty($changes['rename']) && is_array($changes['rename'])) {
|
|
|
241 |
foreach ($changes['rename'] as $fieldName => $field) {
|
|
|
242 |
$rename[$field['name']] = $fieldName;
|
|
|
243 |
}
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
//CHANGE (COLUMN DEFINITION)
|
|
|
247 |
if ( ! empty($changes['change']) && is_array($changes['change'])) {
|
|
|
248 |
if ($query) {
|
|
|
249 |
$query.= ', ';
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
$query .= "ALTER COLUMN ";
|
|
|
253 |
|
|
|
254 |
$altered = array();
|
|
|
255 |
foreach ($changes['change'] as $fieldName => $field) {
|
|
|
256 |
if (isset($rename[$fieldName])) {
|
|
|
257 |
$oldFieldName = $rename[$fieldName];
|
|
|
258 |
unset($rename[$fieldName]);
|
|
|
259 |
} else {
|
|
|
260 |
$oldFieldName = $fieldName;
|
|
|
261 |
}
|
|
|
262 |
$oldFieldName = $this->conn->quoteIdentifier($oldFieldName, true);
|
|
|
263 |
|
|
|
264 |
$declaration = $this->getDeclaration($fieldName, $field['definition']);
|
|
|
265 |
|
|
|
266 |
if (preg_match('/(CONSTRAINT\s+([^\s]*)\s+DEFAULT\s+([^\s]*)\s*)|(DEFAULT\s+([^\s]*)\s*)/', $declaration, $matches)) {
|
|
|
267 |
// Remove the default constraint declaration from the statement
|
|
|
268 |
$altered[] = str_replace($matches[0], '', $declaration);
|
|
|
269 |
|
|
|
270 |
if (count($matches) === 6) {
|
|
|
271 |
// No constraint name provided. Try to make sure it's unique
|
|
|
272 |
$defaultName = 'DF__' . $name . '__' . $fieldName . '__' . mt_rand();
|
|
|
273 |
$defaultValue = $matches[5];
|
|
|
274 |
} else {
|
|
|
275 |
$defaultName = $matches[2];
|
|
|
276 |
$defaultValue = $matches[3];
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
$postQueries .= sprintf(
|
|
|
280 |
' ALTER TABLE %s ADD CONSTRAINT %s DEFAULT (%s) FOR %s',
|
|
|
281 |
$name,
|
|
|
282 |
$defaultName,
|
|
|
283 |
$defaultValue,
|
|
|
284 |
$fieldName
|
|
|
285 |
);
|
|
|
286 |
} else {
|
|
|
287 |
$altered[] = $declaration;
|
|
|
288 |
}
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
$query .= implode(sprintf(
|
|
|
292 |
"; ALTER TABLE %s ALTER COLUMN ",
|
|
|
293 |
$this->conn->quoteIdentifier($name, true)
|
|
|
294 |
), $altered) . ' ';
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
//RENAME (COLUMN)
|
|
|
298 |
if ( ! empty($rename) && is_array($rename)) {
|
|
|
299 |
foreach ($rename as $renameName => $renamedField) {
|
|
|
300 |
|
|
|
301 |
$field = $changes['rename'][$renamedField];
|
|
|
302 |
$renamedField = $this->conn->quoteIdentifier($renamedField);
|
|
|
303 |
|
|
|
304 |
$postQueries .= sprintf(
|
|
|
305 |
"EXECUTE sp_RENAME '%s.%s', '%s', 'COLUMN';",
|
|
|
306 |
$this->conn->quoteIdentifier($name),
|
|
|
307 |
$renamedField,
|
|
|
308 |
$this->conn->quoteIdentifier($field['name'], true)
|
|
|
309 |
);
|
|
|
310 |
}
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
if ( ! $query && ! $postQueries) {
|
|
|
314 |
return false;
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
$name = $this->conn->quoteIdentifier($name, true);
|
|
|
318 |
|
|
|
319 |
$finalQuery = '';
|
|
|
320 |
if ($query) {
|
|
|
321 |
$finalQuery .= 'ALTER TABLE ' . $name . ' ' . trim($query) . ';';
|
|
|
322 |
}
|
|
|
323 |
|
|
|
324 |
if ($postQueries) {
|
|
|
325 |
$finalQuery .= $postQueries;
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
return $this->conn->exec($finalQuery);
|
|
|
329 |
}
|
|
|
330 |
|
|
|
331 |
/**
|
|
|
332 |
* create sequence
|
|
|
333 |
*
|
|
|
334 |
* @param string $seqName name of the sequence to be created
|
|
|
335 |
* @param string $start start value of the sequence; default is 1
|
|
|
336 |
* @param array $options An associative array of table options:
|
|
|
337 |
* array(
|
|
|
338 |
* 'comment' => 'Foo',
|
|
|
339 |
* 'charset' => 'utf8',
|
|
|
340 |
* 'collate' => 'utf8_unicode_ci',
|
|
|
341 |
* );
|
|
|
342 |
* @return string
|
|
|
343 |
*/
|
|
|
344 |
public function createSequence($seqName, $start = 1, array $options = array())
|
|
|
345 |
{
|
|
|
346 |
$sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($seqName), true);
|
|
|
347 |
$seqcolName = $this->conn->quoteIdentifier($this->conn->options['seqcol_name'], true);
|
|
|
348 |
$query = 'CREATE TABLE ' . $sequenceName . ' (' . $seqcolName .
|
|
|
349 |
' INT PRIMARY KEY CLUSTERED IDENTITY(' . $start . ', 1) NOT NULL)';
|
|
|
350 |
|
|
|
351 |
$res = $this->conn->exec($query);
|
|
|
352 |
|
|
|
353 |
if ($start == 1) {
|
|
|
354 |
return true;
|
|
|
355 |
}
|
|
|
356 |
|
|
|
357 |
try {
|
|
|
358 |
$query = 'SET IDENTITY_INSERT ' . $sequenceName . ' ON ' .
|
|
|
359 |
'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES ( ' . $start . ')';
|
|
|
360 |
$res = $this->conn->exec($query);
|
|
|
361 |
} catch (Exception $e) {
|
|
|
362 |
$result = $this->conn->exec('DROP TABLE ' . $sequenceName);
|
|
|
363 |
}
|
|
|
364 |
return true;
|
|
|
365 |
}
|
|
|
366 |
|
|
|
367 |
/**
|
|
|
368 |
* This function drops an existing sequence
|
|
|
369 |
*
|
|
|
370 |
* @param string $seqName name of the sequence to be dropped
|
|
|
371 |
* @return void
|
|
|
372 |
*/
|
|
|
373 |
public function dropSequenceSql($seqName)
|
|
|
374 |
{
|
|
|
375 |
$sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($seqName), true);
|
|
|
376 |
return 'DROP TABLE ' . $sequenceName;
|
|
|
377 |
}
|
|
|
378 |
|
|
|
379 |
/**
|
|
|
380 |
* create a new table
|
|
|
381 |
*
|
|
|
382 |
* @param string $name Name of the database that should be created
|
|
|
383 |
* @param array $fields Associative array that contains the definition of each field of the new table
|
|
|
384 |
* The indexes of the array entries are the names of the fields of the table an
|
|
|
385 |
* the array entry values are associative arrays like those that are meant to be
|
|
|
386 |
* passed with the field definitions to get[Type]Declaration() functions.
|
|
|
387 |
* array(
|
|
|
388 |
* 'id' => array(
|
|
|
389 |
* 'type' => 'integer',
|
|
|
390 |
* 'unsigned' => 1
|
|
|
391 |
* 'notnull' => 1
|
|
|
392 |
* 'default' => 0
|
|
|
393 |
* ),
|
|
|
394 |
* 'name' => array(
|
|
|
395 |
* 'type' => 'text',
|
|
|
396 |
* 'length' => 12
|
|
|
397 |
* ),
|
|
|
398 |
* 'password' => array(
|
|
|
399 |
* 'type' => 'text',
|
|
|
400 |
* 'length' => 12
|
|
|
401 |
* )
|
|
|
402 |
* );
|
|
|
403 |
* @param array $options An associative array of table options:
|
|
|
404 |
*
|
|
|
405 |
* @return string
|
|
|
406 |
*/
|
|
|
407 |
public function createTableSql($name, array $fields, array $options = array())
|
|
|
408 |
{
|
|
|
409 |
if ( ! $name) {
|
|
|
410 |
throw new Doctrine_Export_Exception('no valid table name specified');
|
|
|
411 |
}
|
|
|
412 |
|
|
|
413 |
if (empty($fields)) {
|
|
|
414 |
throw new Doctrine_Export_Exception('no fields specified for table ' . $name);
|
|
|
415 |
}
|
|
|
416 |
|
|
|
417 |
// Use field declaration of primary if the primary option not set
|
|
|
418 |
if ( ! isset($options['primary'])) {
|
|
|
419 |
foreach ($fields as $fieldName => $fieldData) {
|
|
|
420 |
if (isset($fieldData['primary']) && $fieldData['primary']) {
|
|
|
421 |
$options['primary'][$fieldName] = $fieldName;
|
|
|
422 |
}
|
|
|
423 |
}
|
|
|
424 |
}
|
|
|
425 |
|
|
|
426 |
if (isset($options['primary'])) {
|
|
|
427 |
foreach ($options['primary'] as $fieldName) {
|
|
|
428 |
if (isset($fields[$fieldName])) {
|
|
|
429 |
$fields[$fieldName]['notnull'] = true; //Silently forcing NOT NULL as MSSQL will kill a query that has a nullable PK
|
|
|
430 |
}
|
|
|
431 |
}
|
|
|
432 |
}
|
|
|
433 |
|
|
|
434 |
$queryFields = $this->getFieldDeclarationList($fields);
|
|
|
435 |
|
|
|
436 |
if (isset($options['primary']) && ! empty($options['primary'])) {
|
|
|
437 |
$primaryKeys = array_map(array($this->conn, 'quoteIdentifier'), array_values($options['primary']));
|
|
|
438 |
$queryFields .= ', PRIMARY KEY(' . implode(', ', $primaryKeys) . ')';
|
|
|
439 |
}
|
|
|
440 |
|
|
|
441 |
$query = 'CREATE TABLE ' . $this->conn->quoteIdentifier($name, true) . ' (' . $queryFields;
|
|
|
442 |
|
|
|
443 |
$check = $this->getCheckDeclaration($fields);
|
|
|
444 |
|
|
|
445 |
if ( ! empty($check)) {
|
|
|
446 |
$query .= ', ' . $check;
|
|
|
447 |
}
|
|
|
448 |
|
|
|
449 |
$query .= ')';
|
|
|
450 |
|
|
|
451 |
$sql[] = $query;
|
|
|
452 |
|
|
|
453 |
if (isset($options['indexes']) && ! empty($options['indexes'])) {
|
|
|
454 |
foreach($options['indexes'] as $index => $definition) {
|
|
|
455 |
if (is_array($definition)) {
|
|
|
456 |
$sql[] = $this->createIndexSql($name,$index, $definition);
|
|
|
457 |
}
|
|
|
458 |
}
|
|
|
459 |
}
|
|
|
460 |
|
|
|
461 |
if (isset($options['foreignKeys'])) {
|
|
|
462 |
foreach ((array) $options['foreignKeys'] as $k => $definition) {
|
|
|
463 |
if (is_array($definition)) {
|
|
|
464 |
$sql[] = $this->createForeignKeySql($name, $definition);
|
|
|
465 |
}
|
|
|
466 |
}
|
|
|
467 |
}
|
|
|
468 |
|
|
|
469 |
return $sql;
|
|
|
470 |
}
|
|
|
471 |
|
|
|
472 |
/**
|
|
|
473 |
* getNotNullFieldDeclaration
|
|
|
474 |
* Obtain DBMS specific SQL code portion needed to set a NOT NULL
|
|
|
475 |
* declaration to be used in statements like CREATE TABLE.
|
|
|
476 |
*
|
|
|
477 |
* @param array $field field definition array
|
|
|
478 |
* @return string DBMS specific SQL code portion needed to set a default value
|
|
|
479 |
*/
|
|
|
480 |
public function getNotNullFieldDeclaration(array $definition)
|
|
|
481 |
{
|
|
|
482 |
return (
|
|
|
483 |
(isset($definition['notnull']) && $definition['notnull']) ||
|
|
|
484 |
(isset($definition['primary']) && $definition['primary'])
|
|
|
485 |
) ? ' NOT NULL' : ' NULL';
|
|
|
486 |
}
|
|
|
487 |
|
|
|
488 |
/**
|
|
|
489 |
* @see Doctrine_Export::getDefaultFieldDeclaration
|
|
|
490 |
*
|
|
|
491 |
* @param array $field field definition array
|
|
|
492 |
* @return string DBMS specific SQL code portion needed to set a default value
|
|
|
493 |
*/
|
|
|
494 |
public function getDefaultFieldDeclaration($field)
|
|
|
495 |
{
|
|
|
496 |
$default = '';
|
|
|
497 |
|
|
|
498 |
if (array_key_exists('default', $field)) {
|
|
|
499 |
if ($field['default'] === '') {
|
|
|
500 |
$field['default'] = empty($field['notnull'])
|
|
|
501 |
? null : $this->valid_default_values[$field['type']];
|
|
|
502 |
|
|
|
503 |
if ($field['default'] === '' &&
|
|
|
504 |
($this->conn->getAttribute(Doctrine_Core::ATTR_PORTABILITY) & Doctrine_Core::PORTABILITY_EMPTY_TO_NULL)) {
|
|
|
505 |
$field['default'] = null;
|
|
|
506 |
}
|
|
|
507 |
}
|
|
|
508 |
|
|
|
509 |
if ($field['type'] === 'boolean') {
|
|
|
510 |
$field['default'] = $this->conn->convertBooleans($field['default']);
|
|
|
511 |
}
|
|
|
512 |
|
|
|
513 |
if (array_key_exists('defaultConstraintName', $field)) {
|
|
|
514 |
$default .= ' CONSTRAINT ' . $field['defaultConstraintName'];
|
|
|
515 |
}
|
|
|
516 |
|
|
|
517 |
$default .= ' DEFAULT ' . (is_null($field['default'])
|
|
|
518 |
? 'NULL'
|
|
|
519 |
: $this->conn->quote($field['default'], $field['type']));
|
|
|
520 |
}
|
|
|
521 |
|
|
|
522 |
return $default;
|
|
|
523 |
}
|
|
|
524 |
}
|