| 1 | <?php |
| 2 | /* |
| 3 | * $Id: Mssql.php 3018 2007-10-26 14:56:03Z ppetermann $ |
| 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: 3018 $ |
| 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 | $decl = $this->conn->dataDict->getPortableDeclaration($val); |
| 77 | |
| 78 | $description = array( |
| 79 | 'name' => $val['column_name'], |
| 80 | 'ntype' => $type, |
| 81 | 'type' => $decl['type'][0], |
| 82 | 'alltypes' => $decl['type'], |
| 83 | 'length' => $decl['length'], |
| 84 | 'fixed' => $decl['fixed'], |
| 85 | 'unsigned' => $decl['unsigned'], |
| 86 | 'notnull' => (bool) ($val['is_nullable'] === 'NO'), |
| 87 | 'default' => $val['column_def'], |
| 88 | 'primary' => (strtolower($identity) == 'identity'), |
| 89 | ); |
| 90 | $columns[$val['column_name']] = $description; |
| 91 | } |
| 92 | |
| 93 | return $columns; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * lists table constraints |
| 98 | * |
| 99 | * @param string $table database table name |
| 100 | * @return array |
| 101 | */ |
| 102 | public function listTableIndexes($table) |
| 103 | { |
| 104 | |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * lists tables |
| 109 | * |
| 110 | * @param string|null $database |
| 111 | * @return array |
| 112 | */ |
| 113 | public function listTables($database = null) |
| 114 | { |
| 115 | $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name"; |
| 116 | |
| 117 | return $this->conn->fetchColumn($sql); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * lists all triggers |
| 122 | * |
| 123 | * @return array |
| 124 | */ |
| 125 | public function listTriggers($database = null) |
| 126 | { |
| 127 | $query = "SELECT name FROM sysobjects WHERE xtype = 'TR'"; |
| 128 | |
| 129 | $result = $this->conn->fetchColumn($query); |
| 130 | |
| 131 | return $result; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * lists table triggers |
| 136 | * |
| 137 | * @param string $table database table name |
| 138 | * @return array |
| 139 | */ |
| 140 | public function listTableTriggers($table) |
| 141 | { |
| 142 | $table = $this->conn->quote($table, 'text'); |
| 143 | $query = "SELECT name FROM sysobjects WHERE xtype = 'TR' AND object_name(parent_obj) = " . $table; |
| 144 | |
| 145 | $result = $this->conn->fetchColumn($query); |
| 146 | |
| 147 | return $result; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * lists table views |
| 152 | * |
| 153 | * @param string $table database table name |
| 154 | * @return array |
| 155 | */ |
| 156 | public function listTableViews($table) |
| 157 | { |
| 158 | $keyName = 'INDEX_NAME'; |
| 159 | $pkName = 'PK_NAME'; |
| 160 | if ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_FIX_CASE) { |
| 161 | if ($this->conn->getAttribute(Doctrine::ATTR_FIELD_CASE) == CASE_LOWER) { |
| 162 | $keyName = strtolower($keyName); |
| 163 | $pkName = strtolower($pkName); |
| 164 | } else { |
| 165 | $keyName = strtoupper($keyName); |
| 166 | $pkName = strtoupper($pkName); |
| 167 | } |
| 168 | } |
| 169 | $table = $this->conn->quote($table, 'text'); |
| 170 | $query = 'EXEC sp_statistics @table_name = ' . $table; |
| 171 | $indexes = $this->conn->fetchColumn($query, $keyName); |
| 172 | |
| 173 | $query = 'EXEC sp_pkeys @table_name = ' . $table; |
| 174 | $pkAll = $this->conn->fetchColumn($query, $pkName); |
| 175 | |
| 176 | $result = array(); |
| 177 | |
| 178 | foreach ($indexes as $index) { |
| 179 | if ( ! in_array($index, $pkAll) && $index != null) { |
| 180 | $result[] = $this->conn->formatter->fixIndexName($index); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return $result; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * lists database views |
| 189 | * |
| 190 | * @param string|null $database |
| 191 | * @return array |
| 192 | */ |
| 193 | public function listViews($database = null) |
| 194 | { |
| 195 | $query = "SELECT name FROM sysobjects WHERE xtype = 'V'"; |
| 196 | |
| 197 | return $this->conn->fetchColumn($query); |
| 198 | } |
| 199 | } |