Coverage for Doctrine_Import_Mssql

Back to coverage report

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