vendor/doctrine-dbal/lib/Doctrine/DBAL/Schema/MsSqlSchemaManager.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 /*
       
     3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
     4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
     5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
     6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
     7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
     8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
     9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    14  *
       
    15  * This software consists of voluntary contributions made by many individuals
       
    16  * and is licensed under the LGPL. For more information, see
       
    17  * <http://www.phpdoctrine.org>.
       
    18  */
       
    19 
       
    20 namespace Doctrine\DBAL\Schema;
       
    21 
       
    22 /**
       
    23  * xxx
       
    24  *
       
    25  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
       
    26  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
       
    27  * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
       
    28  * @author      Juozas Kaziukenas <juozas@juokaz.com>
       
    29  * @version     $Revision$
       
    30  * @since       2.0
       
    31  */
       
    32 class MsSqlSchemaManager extends AbstractSchemaManager
       
    33 {
       
    34 
       
    35     /**
       
    36      * @override
       
    37      */
       
    38     protected function _getPortableTableColumnDefinition($tableColumn)
       
    39     {
       
    40         $dbType = strtolower($tableColumn['TYPE_NAME']);
       
    41 
       
    42         $autoincrement = false;
       
    43         if (stripos($dbType, 'identity')) {
       
    44             $dbType = trim(str_ireplace('identity', '', $dbType));
       
    45             $autoincrement = true;
       
    46         }
       
    47 
       
    48         $type = array();
       
    49         $unsigned = $fixed = null;
       
    50 
       
    51         if (!isset($tableColumn['name'])) {
       
    52             $tableColumn['name'] = '';
       
    53         }
       
    54 
       
    55         $default = $tableColumn['COLUMN_DEF'];
       
    56 
       
    57         while ($default != ($default2 = preg_replace("/^\((.*)\)$/", '$1', $default))) {
       
    58             $default = $default2;
       
    59         }
       
    60 
       
    61         $length = (int) $tableColumn['LENGTH'];
       
    62 
       
    63         $type = $this->_platform->getDoctrineTypeMapping($dbType);
       
    64         switch ($type) {
       
    65             case 'char':
       
    66                 if ($tableColumn['LENGTH'] == '1') {
       
    67                     $type = 'boolean';
       
    68                     if (preg_match('/^(is|has)/', $tableColumn['name'])) {
       
    69                         $type = array_reverse($type);
       
    70                     }
       
    71                 }
       
    72                 $fixed = true;
       
    73                 break;
       
    74             case 'text':
       
    75                 $fixed = false;
       
    76                 break;
       
    77         }
       
    78         switch ($dbType) {
       
    79             case 'nchar':
       
    80             case 'nvarchar':
       
    81             case 'ntext':
       
    82                 // Unicode data requires 2 bytes per character
       
    83                 $length = $length / 2;
       
    84                 break;
       
    85         }
       
    86 
       
    87         $options = array(
       
    88             'length' => ($length == 0 || !in_array($type, array('text', 'string'))) ? null : $length,
       
    89             'unsigned' => (bool) $unsigned,
       
    90             'fixed' => (bool) $fixed,
       
    91             'default' => $default !== 'NULL' ? $default : null,
       
    92             'notnull' => (bool) ($tableColumn['IS_NULLABLE'] != 'YES'),
       
    93             'scale' => $tableColumn['SCALE'],
       
    94             'precision' => $tableColumn['PRECISION'],
       
    95             'autoincrement' => $autoincrement,
       
    96         );
       
    97 
       
    98         return new Column($tableColumn['COLUMN_NAME'], \Doctrine\DBAL\Types\Type::getType($type), $options);
       
    99     }
       
   100 
       
   101     /**
       
   102      * @override
       
   103      */
       
   104     protected function _getPortableTableIndexesList($tableIndexRows, $tableName=null)
       
   105     {
       
   106         $result = array();
       
   107         foreach ($tableIndexRows AS $tableIndex) {
       
   108             $indexName = $keyName = $tableIndex['index_name'];
       
   109             if (strpos($tableIndex['index_description'], 'primary key') !== false) {
       
   110                 $keyName = 'primary';
       
   111             }
       
   112             $keyName = strtolower($keyName);
       
   113 
       
   114             $result[$keyName] = array(
       
   115                 'name' => $indexName,
       
   116                 'columns' => explode(', ', $tableIndex['index_keys']),
       
   117                 'unique' => strpos($tableIndex['index_description'], 'unique') !== false,
       
   118                 'primary' => strpos($tableIndex['index_description'], 'primary key') !== false,
       
   119             );
       
   120         }
       
   121 
       
   122         $indexes = array();
       
   123         foreach ($result AS $indexKey => $data) {
       
   124             $indexes[$indexKey] = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']);
       
   125         }
       
   126 
       
   127         return $indexes;
       
   128     }
       
   129 
       
   130     /**
       
   131      * @override
       
   132      */
       
   133     public function _getPortableTableForeignKeyDefinition($tableForeignKey)
       
   134     {
       
   135         return new ForeignKeyConstraint(
       
   136                 (array) $tableForeignKey['ColumnName'],
       
   137                 $tableForeignKey['ReferenceTableName'],
       
   138                 (array) $tableForeignKey['ReferenceColumnName'],
       
   139                 $tableForeignKey['ForeignKey'],
       
   140                 array(
       
   141                     'onUpdate' => str_replace('_', ' ', $tableForeignKey['update_referential_action_desc']),
       
   142                     'onDelete' => str_replace('_', ' ', $tableForeignKey['delete_referential_action_desc']),
       
   143                 )
       
   144         );
       
   145     }
       
   146 
       
   147     /**
       
   148      * @override
       
   149      */
       
   150     protected function _getPortableTableDefinition($table)
       
   151     {
       
   152         return $table['name'];
       
   153     }
       
   154 
       
   155     /**
       
   156      * @override
       
   157      */
       
   158     protected function _getPortableDatabaseDefinition($database)
       
   159     {
       
   160         return $database['name'];
       
   161     }
       
   162 
       
   163     /**
       
   164      * @override
       
   165      */
       
   166     protected function _getPortableViewDefinition($view)
       
   167     {
       
   168         // @todo
       
   169         return new View($view['name'], null);
       
   170     }
       
   171 
       
   172 }