Mssql.php 6.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
<?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_Import');
/**
 * @package     Doctrine
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @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)
 * @author      David Coallier <davidc@php.net> (PEAR MDB2 Mssql driver)
 * @version     $Revision$
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 */
lsmith's avatar
lsmith committed
34 35
class Doctrine_Import_Mssql extends Doctrine_Import
{
36 37 38 39 40 41
    /**
     * lists all database sequences
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
42 43
    public function listSequences($database = null)
    {
44
        $query = "SELECT name FROM sysobjects WHERE xtype = 'U'";
zYne's avatar
zYne committed
45
        $tableNames = $this->conn->fetchColumn($query);
lsmith's avatar
lsmith committed
46

zYne's avatar
zYne committed
47
        return array_map(array($this->conn->formatter, 'fixSequenceName'), $tableNames);
48 49 50 51 52 53 54
    }
    /**
     * lists table constraints
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
55 56
    public function listTableColumns($table)
    {
57
        $sql     = 'EXEC sp_columns @table_name = ' . $this->conn->quoteIdentifier($table, true);
zYne's avatar
zYne committed
58
        $result  = $this->conn->fetchAssoc($sql);
59 60 61 62 63 64 65 66 67 68 69
        $columns = array();

        foreach ($result as $key => $val) {
            if (strstr($val['type_name'], ' ')) {
                list($type, $identity) = explode(' ', $val['type_name']);
            } else {
                $type = $val['type_name'];
                $identity = '';
            }

            if ($type == 'varchar') {
zYne's avatar
zYne committed
70
                $type .= '(' . $val['length'] . ')';
71 72
            }

zYne's avatar
zYne committed
73 74
            $decl = $this->conn->dataDict->getPortableDeclaration($val);

75
            $description  = array(
zYne's avatar
zYne committed
76 77
                'name'      => $val['column_name'],
                'type'      => $type,
zYne's avatar
zYne committed
78 79 80 81
                'ptype'     => $decl['type'],
                'length'    => $decl['length'],
                'fixed'     => $decl['fixed'],
                'unsigned'  => $decl['unsigned'],
zYne's avatar
zYne committed
82 83 84
                'notnull'   => (bool) ($val['is_nullable'] === 'NO'),
                'default'   => $val['column_def'],
                'primary'   => (strtolower($identity) == 'identity'),
85
            );
zYne's avatar
zYne committed
86
            $columns[$val['column_name']] = $description;
87 88 89 90 91 92 93 94 95 96
        }

        return $columns;
    }
    /**
     * lists table constraints
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
97 98 99
    public function listTableIndexes($table)
    {

100 101 102 103 104 105 106
    }
    /**
     * lists tables
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
107 108
    public function listTables($database = null)
    {
109 110
        $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";

111 112 113 114 115 116 117
        return $this->conn->fetchColumn($sql);
    }
    /**
     * lists all triggers
     *
     * @return array
     */
zYne's avatar
zYne committed
118
    public function listTriggers($database = null)
119 120 121 122 123 124
    {
        $query = "SELECT name FROM sysobjects WHERE xtype = 'TR'";

        $result = $this->conn->fetchColumn($query);

        return $result;
125 126 127 128 129 130 131
    }
    /**
     * lists table triggers
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
132 133
    public function listTableTriggers($table)
    {
lsmith's avatar
lsmith committed
134
        $table = $this->conn->quote($table, 'text');
135
        $query = "SELECT name FROM sysobjects WHERE xtype = 'TR' AND object_name(parent_obj) = " . $table;
136

lsmith's avatar
lsmith committed
137
        $result = $this->conn->fetchColumn($query);
138 139 140 141 142 143 144 145 146

        return $result;
    }
    /**
     * lists table views
     *
     * @param string $table     database table name
     * @return array
     */
lsmith's avatar
lsmith committed
147 148
    public function listTableViews($table)
    {
149 150
        $keyName = 'INDEX_NAME';
        $pkName = 'PK_NAME';
lsmith's avatar
lsmith committed
151 152
        if ($this->conn->options['portability'] & Doctrine::PORTABILITY_FIX_CASE) {
            if ($this->conn->options['field_case'] == CASE_LOWER) {
153 154 155 156 157 158 159
                $keyName = strtolower($keyName);
                $pkName  = strtolower($pkName);
            } else {
                $keyName = strtoupper($keyName);
                $pkName  = strtoupper($pkName);
            }
        }
lsmith's avatar
lsmith committed
160
        $table = $this->conn->quote($table, 'text');
161
        $query = 'EXEC sp_statistics @table_name = ' . $table;
162
        $indexes = $this->conn->fetchColumn($query, $keyName);
163 164

        $query = 'EXEC sp_pkeys @table_name = ' . $table;
165 166
        $pkAll = $this->conn->fetchColumn($query, $pkName);

167
        $result = array();
168

169 170
        foreach ($indexes as $index) {
            if (!in_array($index, $pkAll) && $index != null) {
zYne's avatar
zYne committed
171
                $result[] = $this->conn->formatter->fixIndexName($index);
172 173 174
            }
        }

zYne's avatar
zYne committed
175
        return $result;
176 177 178 179 180 181 182
    }
    /**
     * lists database views
     *
     * @param string|null $database
     * @return array
     */
lsmith's avatar
lsmith committed
183 184
    public function listViews($database = null)
    {
185 186
        $query = "SELECT name FROM sysobjects WHERE xtype = 'V'";

zYne's avatar
zYne committed
187
        return $this->conn->fetchColumn($query);
188 189
    }
}