Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: Mssql.php 7675 2010-06-09 16:48:13Z 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.doctrine-project.org>.
20
 */
21
 
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: 7675 $
31
 * @link        www.doctrine-project.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 relations
52
     *
53
     * Expects an array of this format to be returned with all the relationships in it where the key is
54
     * the name of the foreign table, and the value is an array containing the local and foreign column
55
     * name
56
     *
57
     * Array
58
     * (
59
     *   [groups] => Array
60
     *     (
61
     *        [local] => group_id
62
     *        [foreign] => id
63
     *     )
64
     * )
65
     *
66
     * @param string $table     database table name
67
     * @return array
68
     */
69
    public function listTableRelations($tableName)
70
    {
71
        $relations = array();
72
        $sql = 'SELECT o1.name as table_name, c1.name as column_name, o2.name as referenced_table_name, c2.name as referenced_column_name, s.name as constraint_name FROM sysforeignkeys fk	inner join sysobjects o1 on fk.fkeyid = o1.id inner join sysobjects o2 on fk.rkeyid = o2.id inner join syscolumns c1 on c1.id = o1.id and c1.colid = fk.fkey inner join syscolumns c2 on c2.id = o2.id and c2.colid = fk.rkey inner join sysobjects s on fk.constid = s.id AND o1.name = \'' . $tableName . '\'';
73
        $results = $this->conn->fetchAssoc($sql);
74
        foreach ($results as $result)
75
        {
76
            $result = array_change_key_case($result, CASE_LOWER);
77
            $relations[] = array('table'   => $result['referenced_table_name'],
78
                                 'local'   => $result['column_name'],
79
                                 'foreign' => $result['referenced_column_name']);
80
        }
81
        return $relations;
82
    }
83
 
84
    /**
85
     * lists table constraints
86
     *
87
     * @param string $table     database table name
88
     * @return array
89
     */
90
    public function listTableColumns($table)
91
    {
92
        $sql = 'EXEC sp_primary_keys_rowset @table_name = ' . $this->conn->quoteIdentifier($table, true);
93
        $result = $this->conn->fetchAssoc($sql);
94
        $primary = array();
95
        foreach ($result as $key => $val) {
96
            $primary[] = $val['COLUMN_NAME'];
97
        }
98
 
99
        $sql     = 'EXEC sp_columns @table_name = ' . $this->conn->quoteIdentifier($table, true);
100
        $result  = $this->conn->fetchAssoc($sql);
101
        $columns = array();
102
 
103
        foreach ($result as $key => $val) {
104
            $val = array_change_key_case($val, CASE_LOWER);
105
 
106
            if (strstr($val['type_name'], ' ')) {
107
                list($type, $identity) = explode(' ', $val['type_name']);
108
            } else {
109
                $type = $val['type_name'];
110
                $identity = '';
111
            }
112
 
113
            if ($type == 'varchar') {
114
                $type .= '(' . $val['length'] . ')';
115
            }
116
 
117
            $val['type'] = $type;
118
            $val['identity'] = $identity;
119
            $decl = $this->conn->dataDict->getPortableDeclaration($val);
120
 
121
            $isIdentity = (bool) (strtoupper(trim($identity)) == 'IDENTITY');
122
            $isNullable = (bool) (strtoupper(trim($val['is_nullable'])) == 'NO');
123
            $isPrimary = in_array($val['column_name'], $primary);
124
 
125
            $description  = array(
126
                'name'          => $val['column_name'],
127
                'ntype'         => $type,
128
                'type'          => $decl['type'][0],
129
                'alltypes'      => $decl['type'],
130
                'length'        => $decl['length'],
131
                'fixed'         => (bool) $decl['fixed'],
132
                'unsigned'      => (bool) $decl['unsigned'],
133
                'notnull'       => $isIdentity ? true : $isNullable,
134
                'default'       => $val['column_def'],
135
                'primary'       => $isPrimary,
136
                'autoincrement' => $isIdentity,
137
            );
138
 
139
            $columns[$val['column_name']] = $description;
140
        }
141
 
142
        return $columns;
143
    }
144
 
145
    /**
146
     * lists table constraints
147
     *
148
     * @param string $table     database table name
149
     * @return array
150
     */
151
    public function listTableIndexes($table)
152
    {
153
 
154
    }
155
 
156
    /**
157
     * lists tables
158
     *
159
     * @param string|null $database
160
     * @return array
161
     */
162
    public function listTables($database = null)
163
    {
164
        $sql = "SELECT name FROM sysobjects WHERE type = 'U' AND name <> 'dtproperties' AND name <> 'sysdiagrams' ORDER BY name";
165
 
166
        return $this->conn->fetchColumn($sql);
167
    }
168
 
169
    /**
170
     * lists all triggers
171
     *
172
     * @return array
173
     */
174
    public function listTriggers($database = null)
175
    {
176
        $query = "SELECT name FROM sysobjects WHERE xtype = 'TR'";
177
 
178
        $result = $this->conn->fetchColumn($query);
179
 
180
        return $result;
181
    }
182
 
183
    /**
184
     * lists table triggers
185
     *
186
     * @param string $table     database table name
187
     * @return array
188
     */
189
    public function listTableTriggers($table)
190
    {
191
        $table = $this->conn->quote($table, 'text');
192
        $query = "SELECT name FROM sysobjects WHERE xtype = 'TR' AND object_name(parent_obj) = " . $this->conn->quoteIdentifier($table, true);
193
 
194
        $result = $this->conn->fetchColumn($query);
195
 
196
        return $result;
197
    }
198
 
199
    /**
200
     * lists table views
201
     *
202
     * @param string $table     database table name
203
     * @return array
204
     */
205
    public function listTableViews($table)
206
    {
207
        $keyName = 'INDEX_NAME';
208
        $pkName = 'PK_NAME';
209
        if ($this->conn->getAttribute(Doctrine_Core::ATTR_FIELD_CASE) && ($this->conn->getAttribute(Doctrine_Core::ATTR_PORTABILITY) & Doctrine_Core::PORTABILITY_FIX_CASE)) {
210
            if ($this->conn->getAttribute(Doctrine_Core::ATTR_FIELD_CASE) == CASE_LOWER) {
211
                $keyName = strtolower($keyName);
212
                $pkName  = strtolower($pkName);
213
            } else {
214
                $keyName = strtoupper($keyName);
215
                $pkName  = strtoupper($pkName);
216
            }
217
        }
218
        $table = $this->conn->quote($table, 'text');
219
        $query = 'EXEC sp_statistics @table_name = ' . $this->conn->quoteIdentifier($table, true);
220
        $indexes = $this->conn->fetchColumn($query, $keyName);
221
 
222
        $query = 'EXEC sp_pkeys @table_name = ' . $this->conn->quoteIdentifier($table, true);
223
        $pkAll = $this->conn->fetchColumn($query, $pkName);
224
 
225
        $result = array();
226
 
227
        foreach ($indexes as $index) {
228
            if ( ! in_array($index, $pkAll) && $index != null) {
229
                $result[] = $this->conn->formatter->fixIndexName($index);
230
            }
231
        }
232
 
233
        return $result;
234
    }
235
 
236
    /**
237
     * lists database views
238
     *
239
     * @param string|null $database
240
     * @return array
241
     */
242
    public function listViews($database = null)
243
    {
244
        $query = "SELECT name FROM sysobjects WHERE xtype = 'V'";
245
 
246
        return $this->conn->fetchColumn($query);
247
    }
248
}