commit | author | age
|
c9462d
|
1 |
<?php |
S |
2 |
// +----------------------------------------------------------------------+ |
|
3 |
// | PHP versions 4 and 5 | |
|
4 |
// +----------------------------------------------------------------------+ |
|
5 |
// | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox, | |
|
6 |
// | Stig. S. Bakken, Lukas Smith, Frank M. Kromann | |
|
7 |
// | All rights reserved. | |
|
8 |
// +----------------------------------------------------------------------+ |
|
9 |
// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB | |
|
10 |
// | API as well as database abstraction for PHP applications. | |
|
11 |
// | This LICENSE is in the BSD license style. | |
|
12 |
// | | |
|
13 |
// | Redistribution and use in source and binary forms, with or without | |
|
14 |
// | modification, are permitted provided that the following conditions | |
|
15 |
// | are met: | |
|
16 |
// | | |
|
17 |
// | Redistributions of source code must retain the above copyright | |
|
18 |
// | notice, this list of conditions and the following disclaimer. | |
|
19 |
// | | |
|
20 |
// | Redistributions in binary form must reproduce the above copyright | |
|
21 |
// | notice, this list of conditions and the following disclaimer in the | |
|
22 |
// | documentation and/or other materials provided with the distribution. | |
|
23 |
// | | |
|
24 |
// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, | |
|
25 |
// | Lukas Smith nor the names of his contributors may be used to endorse | |
|
26 |
// | or promote products derived from this software without specific prior| |
|
27 |
// | written permission. | |
|
28 |
// | | |
|
29 |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
|
30 |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
|
31 |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
|
32 |
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
|
33 |
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
|
34 |
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
|
35 |
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS| |
|
36 |
// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
|
37 |
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
|
38 |
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY| |
|
39 |
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
|
40 |
// | POSSIBILITY OF SUCH DAMAGE. | |
|
41 |
// +----------------------------------------------------------------------+ |
|
42 |
// | Author: Lukas Smith <smith@pooteeweet.org> | |
|
43 |
// +----------------------------------------------------------------------+ |
|
44 |
// |
|
45 |
// $Id$ |
|
46 |
// |
|
47 |
|
|
48 |
require_once 'MDB2/Driver/Reverse/Common.php'; |
|
49 |
|
|
50 |
/** |
|
51 |
* MDB2 MSSQL driver for the schema reverse engineering module |
|
52 |
* |
|
53 |
* @package MDB2 |
|
54 |
* @category Database |
|
55 |
* @author Lukas Smith <smith@dybnet.de> |
|
56 |
*/ |
|
57 |
class MDB2_Driver_Reverse_mssql extends MDB2_Driver_Reverse_Common |
|
58 |
{ |
|
59 |
// }}} |
|
60 |
// {{{ tableInfo() |
|
61 |
|
|
62 |
/** |
|
63 |
* Returns information about a table or a result set |
|
64 |
* |
|
65 |
* NOTE: only supports 'table' and 'flags' if <var>$result</var> |
|
66 |
* is a table name. |
|
67 |
* |
|
68 |
* @param object|string $result MDB2_result object from a query or a |
|
69 |
* string containing the name of a table. |
|
70 |
* While this also accepts a query result |
|
71 |
* resource identifier, this behavior is |
|
72 |
* deprecated. |
|
73 |
* @param int $mode a valid tableInfo mode |
|
74 |
* |
|
75 |
* @return array an associative array with the information requested. |
|
76 |
* A MDB2_Error object on failure. |
|
77 |
* |
|
78 |
* @see MDB2_Driver_Common::tableInfo() |
|
79 |
*/ |
|
80 |
function tableInfo($result, $mode = null) |
|
81 |
{ |
|
82 |
$db =& $this->getDBInstance(); |
|
83 |
if (PEAR::isError($db)) { |
|
84 |
return $db; |
|
85 |
} |
|
86 |
|
|
87 |
if (is_string($result)) { |
|
88 |
/* |
|
89 |
* Probably received a table name. |
|
90 |
* Create a result resource identifier. |
|
91 |
*/ |
|
92 |
$id = $db->_doQuery("SELECT TOP 0 * FROM [$result]"); |
|
93 |
if (PEAR::isError($id)) { |
|
94 |
return $id; |
|
95 |
} |
|
96 |
|
|
97 |
$got_string = true; |
|
98 |
} elseif (MDB2::isResultCommon($result)) { |
|
99 |
/* |
|
100 |
* Probably received a result object. |
|
101 |
* Extract the result resource identifier. |
|
102 |
*/ |
|
103 |
$id = $result->getResource(); |
|
104 |
$got_string = false; |
|
105 |
} else { |
|
106 |
/* |
|
107 |
* Probably received a result resource identifier. |
|
108 |
* Copy it. |
|
109 |
* Deprecated. Here for compatibility only. |
|
110 |
*/ |
|
111 |
$id = $result; |
|
112 |
$got_string = false; |
|
113 |
} |
|
114 |
|
|
115 |
if (!is_resource($id)) { |
|
116 |
return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA); |
|
117 |
} |
|
118 |
|
|
119 |
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { |
|
120 |
if ($db->options['field_case'] == CASE_LOWER) { |
|
121 |
$case_func = 'strtolower'; |
|
122 |
} else { |
|
123 |
$case_func = 'strtoupper'; |
|
124 |
} |
|
125 |
} else { |
|
126 |
$case_func = 'strval'; |
|
127 |
} |
|
128 |
|
|
129 |
$count = @mssql_num_fields($id); |
|
130 |
$res = array(); |
|
131 |
|
|
132 |
if ($mode) { |
|
133 |
$res['num_fields'] = $count; |
|
134 |
} |
|
135 |
|
|
136 |
for ($i = 0; $i < $count; $i++) { |
|
137 |
$res[$i] = array( |
|
138 |
'table' => $got_string ? $case_func($result) : '', |
|
139 |
'name' => $case_func(@mssql_field_name($id, $i)), |
|
140 |
'type' => @mssql_field_type($id, $i), |
|
141 |
'len' => @mssql_field_length($id, $i), |
|
142 |
// We only support flags for table |
|
143 |
'flags' => $got_string |
|
144 |
? $this->_mssql_field_flags($result, @mssql_field_name($id, $i)) : '', |
|
145 |
); |
|
146 |
if ($mode & MDB2_TABLEINFO_ORDER) { |
|
147 |
$res['order'][$res[$i]['name']] = $i; |
|
148 |
} |
|
149 |
if ($mode & MDB2_TABLEINFO_ORDERTABLE) { |
|
150 |
$res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i; |
|
151 |
} |
|
152 |
} |
|
153 |
|
|
154 |
// free the result only if we were called on a table |
|
155 |
if ($got_string) { |
|
156 |
@mssql_free_result($id); |
|
157 |
} |
|
158 |
return $res; |
|
159 |
} |
|
160 |
|
|
161 |
// }}} |
|
162 |
// {{{ _mssql_field_flags() |
|
163 |
|
|
164 |
/** |
|
165 |
* Get a column's flags |
|
166 |
* |
|
167 |
* Supports "not_null", "primary_key", |
|
168 |
* "auto_increment" (mssql identity), "timestamp" (mssql timestamp), |
|
169 |
* "unique_key" (mssql unique index, unique check or primary_key) and |
|
170 |
* "multiple_key" (multikey index) |
|
171 |
* |
|
172 |
* mssql timestamp is NOT similar to the mysql timestamp so this is maybe |
|
173 |
* not useful at all - is the behaviour of mysql_field_flags that primary |
|
174 |
* keys are alway unique? is the interpretation of multiple_key correct? |
|
175 |
* |
|
176 |
* @param string $table the table name |
|
177 |
* @param string $column the field name |
|
178 |
* |
|
179 |
* @return string the flags |
|
180 |
* |
|
181 |
* @access protected |
|
182 |
* @author Joern Barthel <j_barthel@web.de> |
|
183 |
*/ |
|
184 |
function _mssql_field_flags($table, $column) |
|
185 |
{ |
|
186 |
$db =& $this->getDBInstance(); |
|
187 |
if (PEAR::isError($db)) { |
|
188 |
return $db; |
|
189 |
} |
|
190 |
|
|
191 |
static $tableName = null; |
|
192 |
static $flags = array(); |
|
193 |
|
|
194 |
if ($table != $tableName) { |
|
195 |
|
|
196 |
$flags = array(); |
|
197 |
$tableName = $table; |
|
198 |
|
|
199 |
// get unique and primary keys |
|
200 |
$res = $db->queryAll("EXEC SP_HELPINDEX[$table]", null, MDB2_FETCHMODE_ASSOC); |
|
201 |
|
|
202 |
foreach ($res as $val) { |
|
203 |
$keys = explode(', ', $val['index_keys']); |
|
204 |
|
|
205 |
if (sizeof($keys) > 1) { |
|
206 |
foreach ($keys as $key) { |
|
207 |
$this->_add_flag($flags[$key], 'multiple_key'); |
|
208 |
} |
|
209 |
} |
|
210 |
|
|
211 |
if (strpos($val['index_description'], 'primary key')) { |
|
212 |
foreach ($keys as $key) { |
|
213 |
$this->_add_flag($flags[$key], 'primary_key'); |
|
214 |
} |
|
215 |
} elseif (strpos($val['index_description'], 'unique')) { |
|
216 |
foreach ($keys as $key) { |
|
217 |
$this->_add_flag($flags[$key], 'unique_key'); |
|
218 |
} |
|
219 |
} |
|
220 |
} |
|
221 |
|
|
222 |
// get auto_increment, not_null and timestamp |
|
223 |
$res = $db->queryAll("EXEC SP_COLUMNS[$table]", null, MDB2_FETCHMODE_ASSOC); |
|
224 |
|
|
225 |
foreach ($res as $val) { |
|
226 |
$val = array_change_key_case($val, $db->options['field_case']); |
|
227 |
if ($val['nullable'] == '0') { |
|
228 |
$this->_add_flag($flags[$val['column_name']], 'not_null'); |
|
229 |
} |
|
230 |
if (strpos($val['type_name'], 'identity')) { |
|
231 |
$this->_add_flag($flags[$val['column_name']], 'auto_increment'); |
|
232 |
} |
|
233 |
if (strpos($val['type_name'], 'timestamp')) { |
|
234 |
$this->_add_flag($flags[$val['column_name']], 'timestamp'); |
|
235 |
} |
|
236 |
} |
|
237 |
} |
|
238 |
|
|
239 |
if (array_key_exists($column, $flags)) { |
|
240 |
return(implode(' ', $flags[$column])); |
|
241 |
} |
|
242 |
return ''; |
|
243 |
} |
|
244 |
|
|
245 |
// }}} |
|
246 |
// {{{ _add_flag() |
|
247 |
|
|
248 |
/** |
|
249 |
* Adds a string to the flags array if the flag is not yet in there |
|
250 |
* - if there is no flag present the array is created |
|
251 |
* |
|
252 |
* @param array &$array the reference to the flag-array |
|
253 |
* @param string $value the flag value |
|
254 |
* |
|
255 |
* @return void |
|
256 |
* |
|
257 |
* @access protected |
|
258 |
* @author Joern Barthel <j_barthel@web.de> |
|
259 |
*/ |
|
260 |
function _add_flag(&$array, $value) |
|
261 |
{ |
|
262 |
if (!is_array($array)) { |
|
263 |
$array = array($value); |
|
264 |
} elseif (!in_array($value, $array)) { |
|
265 |
array_push($array, $value); |
|
266 |
} |
|
267 |
} |
|
268 |
} |
|
269 |
?> |