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