Mssql.php 11.5 KB
Newer Older
zYne's avatar
zYne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */
Doctrine::autoload('Doctrine_Export');
/**
zYne's avatar
zYne committed
23
 * Doctrine_Export_Mssql
zYne's avatar
zYne committed
24 25 26 27 28 29 30 31 32 33 34
 *
 * @package     Doctrine
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
 * @author      Frank M. Kromann <frank@kromann.info> (PEAR MDB2 Mssql driver)
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 */
lsmith's avatar
lsmith committed
35 36
class Doctrine_Export_Mssql extends Doctrine_Export
{
zYne's avatar
zYne committed
37 38 39 40 41 42
  /**
     * create a new database
     *
     * @param string $name name of the database that should be created
     * @return void
     */
lsmith's avatar
lsmith committed
43 44
    public function createDatabase($name)
    {
lsmith's avatar
lsmith committed
45
        $name = $this->conn->quoteIdentifier($name, true);
zYne's avatar
zYne committed
46
        $query = "CREATE DATABASE $name";
lsmith's avatar
lsmith committed
47 48 49 50
        if ($this->conn->options['database_device']) {
            $query.= ' ON '.$this->conn->options['database_device'];
            $query.= $this->conn->options['database_size'] ? '=' .
                     $this->conn->options['database_size'] : '';
zYne's avatar
zYne committed
51
        }
lsmith's avatar
lsmith committed
52
        return $this->conn->standaloneQuery($query, null, true);
zYne's avatar
zYne committed
53 54 55 56 57 58 59
    }
    /**
     * drop an existing database
     *
     * @param string $name name of the database that should be dropped
     * @return void
     */
lsmith's avatar
lsmith committed
60 61
    public function dropDatabase($name)
    {
lsmith's avatar
lsmith committed
62
        $name = $this->conn->quoteIdentifier($name, true);
zYne's avatar
zYne committed
63
        return $this->conn->standaloneQuery('DROP DATABASE ' . $name, null, true);
zYne's avatar
zYne committed
64
    }
zYne's avatar
zYne committed
65 66 67 68 69 70 71 72 73 74

    /**
     * Override the parent method.
     *
     * @return string The string required to be placed between "CREATE" and "TABLE"
     *                to generate a temporary table, if possible.
     */
    public function getTemporaryTableQuery()
    {
        return '';
zYne's avatar
zYne committed
75
    }  
zYne's avatar
zYne committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    /**
     * alter an existing table
     *
     * @param string $name         name of the table that is intended to be changed.
     * @param array $changes     associative array that contains the details of each type
     *                             of change that is intended to be performed. The types of
     *                             changes that are currently supported are defined as follows:
     *
     *                             name
     *
     *                                New name for the table.
     *
     *                            add
     *
     *                                Associative array with the names of fields to be added as
     *                                 indexes of the array. The value of each entry of the array
     *                                 should be set to another associative array with the properties
     *                                 of the fields to be added. The properties of the fields should
     *                                 be the same as defined by the Metabase parser.
     *
     *
     *                            remove
     *
     *                                Associative array with the names of fields to be removed as indexes
     *                                 of the array. Currently the values assigned to each entry are ignored.
     *                                 An empty array should be used for future compatibility.
     *
     *                            rename
     *
     *                                Associative array with the names of fields to be renamed as indexes
     *                                 of the array. The value of each entry of the array should be set to
     *                                 another associative array with the entry named name with the new
     *                                 field name and the entry named Declaration that is expected to contain
     *                                 the portion of the field declaration already in DBMS specific SQL code
     *                                 as it is used in the CREATE TABLE statement.
     *
     *                            change
     *
     *                                Associative array with the names of the fields to be changed as indexes
     *                                 of the array. Keep in mind that if it is intended to change either the
     *                                 name of a field and any other properties, the change array entries
     *                                 should have the new names of the fields as array indexes.
     *
     *                                The value of each entry of the array should be set to another associative
     *                                 array with the properties of the fields to that are meant to be changed as
     *                                 array entries. These entries should be assigned to the new values of the
     *                                 respective properties. The properties of the fields should be the same
     *                                 as defined by the Metabase parser.
     *
     *                            Example
     *                                array(
     *                                    'name' => 'userlist',
     *                                    'add' => array(
     *                                        'quota' => array(
     *                                            'type' => 'integer',
     *                                            'unsigned' => 1
     *                                        )
     *                                    ),
     *                                    'remove' => array(
     *                                        'file_limit' => array(),
     *                                        'time_limit' => array()
     *                                    ),
     *                                    'change' => array(
     *                                        'name' => array(
     *                                            'length' => '20',
     *                                            'definition' => array(
     *                                                'type' => 'text',
     *                                                'length' => 20,
     *                                            ),
     *                                        )
     *                                    ),
     *                                    'rename' => array(
     *                                        'sex' => array(
     *                                            'name' => 'gender',
     *                                            'definition' => array(
     *                                                'type' => 'text',
     *                                                'length' => 1,
     *                                                'default' => 'M',
     *                                            ),
     *                                        )
     *                                    )
     *                                )
     *
     * @param boolean $check     indicates whether the function should just check if the DBMS driver
     *                             can perform the requested table alterations if the value is true or
     *                             actually perform them otherwise.
     * @return void
     */
zYne's avatar
zYne committed
164
    public function alterTable($name, array $changes, $check)
lsmith's avatar
lsmith committed
165
    {
zYne's avatar
zYne committed
166 167
        foreach ($changes as $changeName => $change) {
            switch ($changeName) {
168 169 170 171 172 173 174 175
                case 'add':
                    break;
                case 'remove':
                    break;
                case 'name':
                case 'rename':
                case 'change':
                default:
zYne's avatar
zYne committed
176
                    throw new Doctrine_Export_Exception('alterTable: change type "' . $changeName . '" not yet supported');
zYne's avatar
zYne committed
177 178 179 180
            }
        }

        $query = '';
zYne's avatar
zYne committed
181 182
        if ( ! empty($changes['add']) && is_array($changes['add'])) {
            foreach ($changes['add'] as $fieldName => $field) {
zYne's avatar
zYne committed
183
                if ($query) {
zYne's avatar
zYne committed
184
                    $query .= ', ';
zYne's avatar
zYne committed
185
                }
zYne's avatar
zYne committed
186
                $query .= 'ADD ' . $this->conn->getDeclaration($field['type'], $fieldName, $field);
zYne's avatar
zYne committed
187 188 189
            }
        }

zYne's avatar
zYne committed
190 191
        if ( ! empty($changes['remove']) && is_array($changes['remove'])) {
            foreach ($changes['remove'] as $fieldName => $field) {
zYne's avatar
zYne committed
192
                if ($query) {
zYne's avatar
zYne committed
193
                    $query .= ', ';
zYne's avatar
zYne committed
194
                }
zYne's avatar
zYne committed
195 196
                $field_name = $this->conn->quoteIdentifier($fieldName, true);
                $query .= 'DROP COLUMN ' . $fieldName;
zYne's avatar
zYne committed
197 198 199
            }
        }

zYne's avatar
zYne committed
200
        if ( ! $query) {
lsmith's avatar
lsmith committed
201
            return false;
zYne's avatar
zYne committed
202 203
        }

zYne's avatar
zYne committed
204
        $name = $this->conn->quoteIdentifier($name, true);
zYne's avatar
zYne committed
205
        return $this->conn->exec('ALTER TABLE ' . $name . ' ' . $query);
zYne's avatar
zYne committed
206 207 208 209
    }
    /**
     * create sequence
     *
zYne's avatar
zYne committed
210 211 212 213 214 215 216 217 218
     * @param string $seqName name of the sequence to be created
     * @param string $start start value of the sequence; default is 1
     * @param array     $options  An associative array of table options:
     *                          array(
     *                              'comment' => 'Foo',
     *                              'charset' => 'utf8',
     *                              'collate' => 'utf8_unicode_ci',
     *                          );
     * @return string
zYne's avatar
zYne committed
219
     */
zYne's avatar
zYne committed
220
    public function createSequence($seqName, $start = 1, array $options = array())
lsmith's avatar
lsmith committed
221
    {
zYne's avatar
zYne committed
222 223
        $sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($seqName), true);
        $seqcolName = $this->conn->quoteIdentifier($this->conn->options['seqcol_name'], true);
zYne's avatar
zYne committed
224 225
        $query = 'CREATE TABLE ' . $sequenceName . ' (' . $seqcolName .
                 ' INT PRIMARY KEY CLUSTERED IDENTITY(' . $start . ', 1) NOT NULL)';
zYne's avatar
zYne committed
226

lsmith's avatar
lsmith committed
227
        $res = $this->conn->exec($query);
zYne's avatar
zYne committed
228 229 230 231 232

        if ($start == 1) {
            return true;
        }

lsmith's avatar
lsmith committed
233
        try {
zYne's avatar
zYne committed
234 235
            $query = 'SET IDENTITY_INSERT ' . $sequenceName . ' ON ' .
                     'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES ( ' . $start . ')';
lsmith's avatar
lsmith committed
236 237
            $res = $this->conn->exec($query);
        } catch (Exception $e) {
zYne's avatar
zYne committed
238
            $result = $this->conn->exec('DROP TABLE ' . $sequenceName);
zYne's avatar
zYne committed
239
        }
lsmith's avatar
lsmith committed
240
        return true;
zYne's avatar
zYne committed
241 242 243 244 245 246 247
    }
    /**
     * This function drops an existing sequence
     *
     * @param string $seqName      name of the sequence to be dropped
     * @return void
     */
zYne's avatar
zYne committed
248
    public function dropSequenceSql($seqName)
lsmith's avatar
lsmith committed
249
    {
lsmith's avatar
lsmith committed
250
        $sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($seqName), true);
zYne's avatar
zYne committed
251
        return 'DROP TABLE ' . $sequenceName;
zYne's avatar
zYne committed
252 253
    }
}