Source for file Mssql.php

Documentation is available at Mssql.php

  1. <?php
  2. /*
  3.  *  $Id: Mssql.php 1889 2007-06-28 12:11:55Z 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_Import');
  22. /**
  23.  * @package     Doctrine
  24.  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
  25.  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
  26.  * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  27.  * @author      Frank M. Kromann <frank@kromann.info> (PEAR MDB2 Mssql driver)
  28.  * @author      David Coallier <davidc@php.net> (PEAR MDB2 Mssql driver)
  29.  * @version     $Revision: 1889 $
  30.  * @category    Object Relational Mapping
  31.  * @link        www.phpdoctrine.com
  32.  * @since       1.0
  33.  */
  34. {
  35.     /**
  36.      * lists all database sequences
  37.      *
  38.      * @param string|null$database 
  39.      * @return array 
  40.      */
  41.     public function listSequences($database null)
  42.     {
  43.         $query "SELECT name FROM sysobjects WHERE xtype = 'U'";
  44.         $tableNames $this->conn->fetchColumn($query);
  45.  
  46.         return array_map(array($this->conn->formatter'fixSequenceName')$tableNames);
  47.     }
  48.     /**
  49.      * lists table constraints
  50.      *
  51.      * @param string $table     database table name
  52.      * @return array 
  53.      */
  54.     public function listTableColumns($table)
  55.     {
  56.         $sql     'EXEC sp_columns @table_name = ' $this->conn->quoteIdentifier($tabletrue);
  57.         $result  $this->conn->fetchAssoc($sql);
  58.         $columns array();
  59.  
  60.         foreach ($result as $key => $val{
  61.             $val array_change_key_case($valCASE_LOWER);
  62.  
  63.             if (strstr($val['type_name']' ')) {
  64.                 list($type$identityexplode(' '$val['type_name']);
  65.             else {
  66.                 $type $val['type_name'];
  67.                 $identity '';
  68.             }
  69.  
  70.             if ($type == 'varchar'{
  71.                 $type .= '(' $val['length'')';
  72.             }
  73.  
  74.             $decl $this->conn->dataDict->getPortableDeclaration($val);
  75.  
  76.             $description  array(
  77.                 'name'      => $val['column_name'],
  78.                 'ntype'     => $type,
  79.                 'type'      => $decl['type'][0],
  80.                 'alltypes'  => $decl['type'],
  81.                 'length'    => $decl['length'],
  82.                 'fixed'     => $decl['fixed'],
  83.                 'unsigned'  => $decl['unsigned'],
  84.                 'notnull'   => (bool) ($val['is_nullable'=== 'NO'),
  85.                 'default'   => $val['column_def'],
  86.                 'primary'   => (strtolower($identity== 'identity'),
  87.             );
  88.             $columns[$val['column_name']] $description;
  89.         }
  90.  
  91.         return $columns;
  92.     }
  93.     /**
  94.      * lists table constraints
  95.      *
  96.      * @param string $table     database table name
  97.      * @return array 
  98.      */
  99.     public function listTableIndexes($table)
  100.     {
  101.  
  102.     }
  103.     /**
  104.      * lists tables
  105.      *
  106.      * @param string|null$database 
  107.      * @return array 
  108.      */
  109.     public function listTables($database null)
  110.     {
  111.         $sql "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
  112.  
  113.         return $this->conn->fetchColumn($sql);
  114.     }
  115.     /**
  116.      * lists all triggers
  117.      *
  118.      * @return array 
  119.      */
  120.     public function listTriggers($database null)
  121.     {
  122.         $query "SELECT name FROM sysobjects WHERE xtype = 'TR'";
  123.  
  124.         $result $this->conn->fetchColumn($query);
  125.  
  126.         return $result;
  127.     }
  128.     /**
  129.      * lists table triggers
  130.      *
  131.      * @param string $table     database table name
  132.      * @return array 
  133.      */
  134.     public function listTableTriggers($table)
  135.     {
  136.         $table $this->conn->quote($table'text');
  137.         $query "SELECT name FROM sysobjects WHERE xtype = 'TR' AND object_name(parent_obj) = " $table;
  138.  
  139.         $result $this->conn->fetchColumn($query);
  140.  
  141.         return $result;
  142.     }
  143.     /**
  144.      * lists table views
  145.      *
  146.      * @param string $table     database table name
  147.      * @return array 
  148.      */
  149.     public function listTableViews($table)
  150.     {
  151.         $keyName 'INDEX_NAME';
  152.         $pkName 'PK_NAME';
  153.         if ($this->conn->options['portability'Doctrine::PORTABILITY_FIX_CASE{
  154.             if ($this->conn->options['field_case'== CASE_LOWER{
  155.                 $keyName strtolower($keyName);
  156.                 $pkName  strtolower($pkName);
  157.             else {
  158.                 $keyName strtoupper($keyName);
  159.                 $pkName  strtoupper($pkName);
  160.             }
  161.         }
  162.         $table $this->conn->quote($table'text');
  163.         $query 'EXEC sp_statistics @table_name = ' $table;
  164.         $indexes $this->conn->fetchColumn($query$keyName);
  165.  
  166.         $query 'EXEC sp_pkeys @table_name = ' $table;
  167.         $pkAll $this->conn->fetchColumn($query$pkName);
  168.  
  169.         $result array();
  170.  
  171.         foreach ($indexes as $index{
  172.             if (!in_array($index$pkAll&& $index != null{
  173.                 $result[$this->conn->formatter->fixIndexName($index);
  174.             }
  175.         }
  176.  
  177.         return $result;
  178.     }
  179.     /**
  180.      * lists database views
  181.      *
  182.      * @param string|null$database 
  183.      * @return array 
  184.      */
  185.     public function listViews($database null)
  186.     {
  187.         $query "SELECT name FROM sysobjects WHERE xtype = 'V'";
  188.  
  189.         return $this->conn->fetchColumn($query);
  190.     }
  191. }