commit | author | age
|
95ebbc
|
1 |
<?php
|
T |
2 |
// +----------------------------------------------------------------------+
|
|
3 |
// | PHP versions 4 and 5 |
|
|
4 |
// +----------------------------------------------------------------------+
|
|
5 |
// | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
|
|
6 |
// | Stig. S. Bakken, Lukas Smith, Frank M. Kromann, Lorenzo Alberton |
|
|
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 |
// | Authors: Lukas Smith <smith@pooteeweet.org> |
|
|
43 |
// | Lorenzo Alberton <l.alberton@quipo.it> |
|
|
44 |
// +----------------------------------------------------------------------+
|
|
45 |
//
|
d1403f
|
46 |
// $Id: mssql.php,v 1.49 2008/02/17 15:30:57 quipo Exp $
|
95ebbc
|
47 |
//
|
T |
48 |
|
|
49 |
require_once 'MDB2/Driver/Reverse/Common.php';
|
|
50 |
|
|
51 |
/**
|
|
52 |
* MDB2 MSSQL driver for the schema reverse engineering module
|
|
53 |
*
|
|
54 |
* @package MDB2
|
|
55 |
* @category Database
|
|
56 |
* @author Lukas Smith <smith@dybnet.de>
|
|
57 |
* @author Lorenzo Alberton <l.alberton@quipo.it>
|
|
58 |
*/
|
|
59 |
class MDB2_Driver_Reverse_mssql extends MDB2_Driver_Reverse_Common
|
|
60 |
{
|
|
61 |
// {{{ getTableFieldDefinition()
|
|
62 |
|
|
63 |
/**
|
|
64 |
* Get the structure of a field into an array
|
|
65 |
*
|
|
66 |
* @param string $table_name name of table that should be used in method
|
|
67 |
* @param string $field_name name of field that should be used in method
|
|
68 |
* @return mixed data array on success, a MDB2 error on failure
|
|
69 |
* @access public
|
|
70 |
*/
|
|
71 |
function getTableFieldDefinition($table_name, $field_name)
|
|
72 |
{
|
|
73 |
$db =& $this->getDBInstance();
|
|
74 |
if (PEAR::isError($db)) {
|
|
75 |
return $db;
|
|
76 |
}
|
|
77 |
|
|
78 |
$result = $db->loadModule('Datatype', null, true);
|
|
79 |
if (PEAR::isError($result)) {
|
|
80 |
return $result;
|
|
81 |
}
|
|
82 |
|
|
83 |
list($schema, $table) = $this->splitTableSchema($table_name);
|
|
84 |
|
|
85 |
$table = $db->quoteIdentifier($table, true);
|
|
86 |
$fldname = $db->quoteIdentifier($field_name, true);
|
|
87 |
|
|
88 |
$query = "SELECT t.table_name,
|
|
89 |
c.column_name 'name',
|
|
90 |
c.data_type 'type',
|
|
91 |
CASE c.is_nullable WHEN 'YES' THEN 1 ELSE 0 END AS 'is_nullable',
|
|
92 |
c.column_default,
|
|
93 |
c.character_maximum_length 'length',
|
|
94 |
c.numeric_precision,
|
|
95 |
c.numeric_scale,
|
|
96 |
c.character_set_name,
|
|
97 |
c.collation_name
|
|
98 |
FROM INFORMATION_SCHEMA.TABLES t,
|
|
99 |
INFORMATION_SCHEMA.COLUMNS c
|
|
100 |
WHERE t.table_name = c.table_name
|
|
101 |
AND t.table_name = '$table'
|
|
102 |
AND c.column_name = '$fldname'";
|
|
103 |
if (!empty($schema)) {
|
|
104 |
$query .= " AND t.table_schema = '" .$db->quoteIdentifier($schema, true) ."'";
|
|
105 |
}
|
|
106 |
$query .= ' ORDER BY t.table_name';
|
|
107 |
$column = $db->queryRow($query, null, MDB2_FETCHMODE_ASSOC);
|
|
108 |
if (PEAR::isError($column)) {
|
|
109 |
return $column;
|
|
110 |
}
|
|
111 |
if (empty($column)) {
|
|
112 |
return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
|
|
113 |
'it was not specified an existing table column', __FUNCTION__);
|
|
114 |
}
|
|
115 |
|
|
116 |
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
|
|
117 |
if ($db->options['field_case'] == CASE_LOWER) {
|
|
118 |
$column['name'] = strtolower($column['name']);
|
|
119 |
} else {
|
|
120 |
$column['name'] = strtoupper($column['name']);
|
|
121 |
}
|
|
122 |
} else {
|
|
123 |
$column = array_change_key_case($column, $db->options['field_case']);
|
|
124 |
}
|
|
125 |
$mapped_datatype = $db->datatype->mapNativeDatatype($column);
|
|
126 |
if (PEAR::isError($mapped_datatype)) {
|
|
127 |
return $mapped_datatype;
|
|
128 |
}
|
|
129 |
list($types, $length, $unsigned, $fixed) = $mapped_datatype;
|
|
130 |
$notnull = true;
|
|
131 |
if ($column['is_nullable']) {
|
|
132 |
$notnull = false;
|
|
133 |
}
|
|
134 |
$default = false;
|
|
135 |
if (array_key_exists('column_default', $column)) {
|
|
136 |
$default = $column['column_default'];
|
|
137 |
if (is_null($default) && $notnull) {
|
|
138 |
$default = '';
|
|
139 |
} elseif (strlen($default) > 4
|
|
140 |
&& substr($default, 0, 1) == '('
|
|
141 |
&& substr($default, -1, 1) == ')'
|
|
142 |
) {
|
|
143 |
//mssql wraps the default value in parentheses: "((1234))", "(NULL)"
|
|
144 |
$default = trim($default, '()');
|
|
145 |
if ($default == 'NULL') {
|
|
146 |
$default = null;
|
|
147 |
}
|
|
148 |
}
|
|
149 |
}
|
|
150 |
$definition[0] = array(
|
|
151 |
'notnull' => $notnull,
|
|
152 |
'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i', '\\1', $column['type'])
|
|
153 |
);
|
|
154 |
if (!is_null($length)) {
|
|
155 |
$definition[0]['length'] = $length;
|
|
156 |
}
|
|
157 |
if (!is_null($unsigned)) {
|
|
158 |
$definition[0]['unsigned'] = $unsigned;
|
|
159 |
}
|
|
160 |
if (!is_null($fixed)) {
|
|
161 |
$definition[0]['fixed'] = $fixed;
|
|
162 |
}
|
|
163 |
if ($default !== false) {
|
|
164 |
$definition[0]['default'] = $default;
|
|
165 |
}
|
|
166 |
foreach ($types as $key => $type) {
|
|
167 |
$definition[$key] = $definition[0];
|
|
168 |
if ($type == 'clob' || $type == 'blob') {
|
|
169 |
unset($definition[$key]['default']);
|
|
170 |
}
|
|
171 |
$definition[$key]['type'] = $type;
|
|
172 |
$definition[$key]['mdb2type'] = $type;
|
|
173 |
}
|
|
174 |
return $definition;
|
|
175 |
}
|
|
176 |
|
|
177 |
// }}}
|
|
178 |
// {{{ getTableIndexDefinition()
|
|
179 |
|
|
180 |
/**
|
|
181 |
* Get the structure of an index into an array
|
|
182 |
*
|
|
183 |
* @param string $table_name name of table that should be used in method
|
|
184 |
* @param string $index_name name of index that should be used in method
|
|
185 |
* @return mixed data array on success, a MDB2 error on failure
|
|
186 |
* @access public
|
|
187 |
*/
|
|
188 |
function getTableIndexDefinition($table_name, $index_name)
|
|
189 |
{
|
|
190 |
$db =& $this->getDBInstance();
|
|
191 |
if (PEAR::isError($db)) {
|
|
192 |
return $db;
|
|
193 |
}
|
|
194 |
|
|
195 |
list($schema, $table) = $this->splitTableSchema($table_name);
|
|
196 |
|
|
197 |
$table = $db->quoteIdentifier($table, true);
|
|
198 |
//$idxname = $db->quoteIdentifier($index_name, true);
|
|
199 |
|
|
200 |
$query = "SELECT OBJECT_NAME(i.id) tablename,
|
|
201 |
i.name indexname,
|
|
202 |
c.name field_name,
|
|
203 |
CASE INDEXKEY_PROPERTY(i.id, i.indid, ik.keyno, 'IsDescending')
|
|
204 |
WHEN 1 THEN 'DESC' ELSE 'ASC'
|
|
205 |
END 'collation',
|
|
206 |
ik.keyno 'position'
|
|
207 |
FROM sysindexes i
|
|
208 |
JOIN sysindexkeys ik ON ik.id = i.id AND ik.indid = i.indid
|
|
209 |
JOIN syscolumns c ON c.id = ik.id AND c.colid = ik.colid
|
|
210 |
WHERE OBJECT_NAME(i.id) = '$table'
|
|
211 |
AND i.name = '%s'
|
|
212 |
AND NOT EXISTS (
|
|
213 |
SELECT *
|
|
214 |
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE k
|
|
215 |
WHERE k.table_name = OBJECT_NAME(i.id)
|
|
216 |
AND k.constraint_name = i.name";
|
|
217 |
if (!empty($schema)) {
|
|
218 |
$query .= " AND k.table_schema = '" .$db->quoteIdentifier($schema, true) ."'";
|
|
219 |
}
|
|
220 |
$query .= ')
|
|
221 |
ORDER BY tablename, indexname, ik.keyno';
|
|
222 |
|
|
223 |
$index_name_mdb2 = $db->getIndexName($index_name);
|
|
224 |
$result = $db->queryRow(sprintf($query, $index_name_mdb2));
|
|
225 |
if (!PEAR::isError($result) && !is_null($result)) {
|
|
226 |
// apply 'idxname_format' only if the query succeeded, otherwise
|
|
227 |
// fallback to the given $index_name, without transformation
|
|
228 |
$index_name = $index_name_mdb2;
|
|
229 |
}
|
|
230 |
$result = $db->query(sprintf($query, $index_name));
|
|
231 |
if (PEAR::isError($result)) {
|
|
232 |
return $result;
|
|
233 |
}
|
|
234 |
|
|
235 |
$definition = array();
|
|
236 |
while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
|
|
237 |
$column_name = $row['field_name'];
|
|
238 |
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
|
|
239 |
if ($db->options['field_case'] == CASE_LOWER) {
|
|
240 |
$column_name = strtolower($column_name);
|
|
241 |
} else {
|
|
242 |
$column_name = strtoupper($column_name);
|
|
243 |
}
|
|
244 |
}
|
|
245 |
$definition['fields'][$column_name] = array(
|
|
246 |
'position' => (int)$row['position'],
|
|
247 |
);
|
|
248 |
if (!empty($row['collation'])) {
|
|
249 |
$definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'ASC'
|
|
250 |
? 'ascending' : 'descending');
|
|
251 |
}
|
|
252 |
}
|
|
253 |
$result->free();
|
|
254 |
if (empty($definition['fields'])) {
|
|
255 |
return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
|
|
256 |
'it was not specified an existing table index', __FUNCTION__);
|
|
257 |
}
|
|
258 |
return $definition;
|
|
259 |
}
|
|
260 |
|
|
261 |
// }}}
|
|
262 |
// {{{ getTableConstraintDefinition()
|
|
263 |
|
|
264 |
/**
|
|
265 |
* Get the structure of a constraint into an array
|
|
266 |
*
|
|
267 |
* @param string $table_name name of table that should be used in method
|
|
268 |
* @param string $constraint_name name of constraint that should be used in method
|
|
269 |
* @return mixed data array on success, a MDB2 error on failure
|
|
270 |
* @access public
|
|
271 |
*/
|
|
272 |
function getTableConstraintDefinition($table_name, $constraint_name)
|
|
273 |
{
|
|
274 |
$db =& $this->getDBInstance();
|
|
275 |
if (PEAR::isError($db)) {
|
|
276 |
return $db;
|
|
277 |
}
|
|
278 |
|
|
279 |
list($schema, $table) = $this->splitTableSchema($table_name);
|
|
280 |
|
|
281 |
$table = $db->quoteIdentifier($table, true);
|
|
282 |
$query = "SELECT k.table_name,
|
|
283 |
k.column_name field_name,
|
|
284 |
CASE c.constraint_type WHEN 'PRIMARY KEY' THEN 1 ELSE 0 END 'primary',
|
|
285 |
CASE c.constraint_type WHEN 'UNIQUE' THEN 1 ELSE 0 END 'unique',
|
|
286 |
CASE c.constraint_type WHEN 'FOREIGN KEY' THEN 1 ELSE 0 END 'foreign',
|
|
287 |
CASE c.constraint_type WHEN 'CHECK' THEN 1 ELSE 0 END 'check',
|
|
288 |
CASE c.is_deferrable WHEN 'NO' THEN 0 ELSE 1 END 'deferrable',
|
|
289 |
CASE c.initially_deferred WHEN 'NO' THEN 0 ELSE 1 END 'initiallydeferred',
|
|
290 |
rc.match_option 'match',
|
|
291 |
rc.update_rule 'onupdate',
|
|
292 |
rc.delete_rule 'ondelete',
|
|
293 |
kcu.table_name 'references_table',
|
|
294 |
kcu.column_name 'references_field',
|
|
295 |
k.ordinal_position 'field_position'
|
|
296 |
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE k
|
|
297 |
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS c
|
|
298 |
ON k.table_name = c.table_name
|
|
299 |
AND k.table_schema = c.table_schema
|
|
300 |
AND k.table_catalog = c.table_catalog
|
|
301 |
AND k.constraint_catalog = c.constraint_catalog
|
|
302 |
AND k.constraint_name = c.constraint_name
|
|
303 |
LEFT JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
|
|
304 |
ON rc.constraint_schema = c.constraint_schema
|
|
305 |
AND rc.constraint_catalog = c.constraint_catalog
|
|
306 |
AND rc.constraint_name = c.constraint_name
|
|
307 |
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
|
|
308 |
ON rc.unique_constraint_schema = kcu.constraint_schema
|
|
309 |
AND rc.unique_constraint_catalog = kcu.constraint_catalog
|
|
310 |
AND rc.unique_constraint_name = kcu.constraint_name
|
|
311 |
AND k.ordinal_position = kcu.ordinal_position
|
|
312 |
WHERE k.constraint_catalog = DB_NAME()
|
|
313 |
AND k.table_name = '$table'
|
|
314 |
AND k.constraint_name = '%s'";
|
|
315 |
if (!empty($schema)) {
|
|
316 |
$query .= " AND k.table_schema = '" .$db->quoteIdentifier($schema, true) ."'";
|
|
317 |
}
|
|
318 |
$query .= ' ORDER BY k.constraint_name,
|
|
319 |
k.ordinal_position';
|
|
320 |
|
|
321 |
$constraint_name_mdb2 = $db->getIndexName($constraint_name);
|
|
322 |
$result = $db->queryRow(sprintf($query, $constraint_name_mdb2));
|
|
323 |
if (!PEAR::isError($result) && !is_null($result)) {
|
|
324 |
// apply 'idxname_format' only if the query succeeded, otherwise
|
|
325 |
// fallback to the given $index_name, without transformation
|
|
326 |
$constraint_name = $constraint_name_mdb2;
|
|
327 |
}
|
|
328 |
$result = $db->query(sprintf($query, $constraint_name));
|
|
329 |
if (PEAR::isError($result)) {
|
|
330 |
return $result;
|
|
331 |
}
|
|
332 |
|
|
333 |
$definition = array(
|
|
334 |
'fields' => array()
|
|
335 |
);
|
|
336 |
while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
|
|
337 |
$row = array_change_key_case($row, CASE_LOWER);
|
|
338 |
$column_name = $row['field_name'];
|
|
339 |
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
|
|
340 |
if ($db->options['field_case'] == CASE_LOWER) {
|
|
341 |
$column_name = strtolower($column_name);
|
|
342 |
} else {
|
|
343 |
$column_name = strtoupper($column_name);
|
|
344 |
}
|
|
345 |
}
|
|
346 |
$definition['fields'][$column_name] = array(
|
|
347 |
'position' => (int)$row['field_position']
|
|
348 |
);
|
|
349 |
if ($row['foreign']) {
|
|
350 |
$ref_column_name = $row['references_field'];
|
|
351 |
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
|
|
352 |
if ($db->options['field_case'] == CASE_LOWER) {
|
|
353 |
$ref_column_name = strtolower($ref_column_name);
|
|
354 |
} else {
|
|
355 |
$ref_column_name = strtoupper($ref_column_name);
|
|
356 |
}
|
|
357 |
}
|
|
358 |
$definition['references']['table'] = $row['references_table'];
|
|
359 |
$definition['references']['fields'][$ref_column_name] = array(
|
|
360 |
'position' => (int)$row['field_position']
|
|
361 |
);
|
|
362 |
}
|
|
363 |
//collation?!?
|
|
364 |
/*
|
|
365 |
if (!empty($row['collation'])) {
|
|
366 |
$definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'ASC'
|
|
367 |
? 'ascending' : 'descending');
|
|
368 |
}
|
|
369 |
*/
|
|
370 |
$lastrow = $row;
|
|
371 |
// otherwise $row is no longer usable on exit from loop
|
|
372 |
}
|
|
373 |
$result->free();
|
|
374 |
if (empty($definition['fields'])) {
|
|
375 |
return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
|
|
376 |
$constraint_name . ' is not an existing table constraint', __FUNCTION__);
|
|
377 |
}
|
|
378 |
|
|
379 |
$definition['primary'] = (boolean)$lastrow['primary'];
|
|
380 |
$definition['unique'] = (boolean)$lastrow['unique'];
|
|
381 |
$definition['foreign'] = (boolean)$lastrow['foreign'];
|
|
382 |
$definition['check'] = (boolean)$lastrow['check'];
|
|
383 |
$definition['deferrable'] = (boolean)$lastrow['deferrable'];
|
|
384 |
$definition['initiallydeferred'] = (boolean)$lastrow['initiallydeferred'];
|
|
385 |
$definition['onupdate'] = $lastrow['onupdate'];
|
|
386 |
$definition['ondelete'] = $lastrow['ondelete'];
|
|
387 |
$definition['match'] = $lastrow['match'];
|
|
388 |
|
|
389 |
return $definition;
|
|
390 |
}
|
|
391 |
|
|
392 |
// }}}
|
|
393 |
// {{{ getTriggerDefinition()
|
|
394 |
|
|
395 |
/**
|
|
396 |
* Get the structure of a trigger into an array
|
|
397 |
*
|
|
398 |
* EXPERIMENTAL
|
|
399 |
*
|
|
400 |
* WARNING: this function is experimental and may change the returned value
|
|
401 |
* at any time until labelled as non-experimental
|
|
402 |
*
|
|
403 |
* @param string $trigger name of trigger that should be used in method
|
|
404 |
* @return mixed data array on success, a MDB2 error on failure
|
|
405 |
* @access public
|
|
406 |
*/
|
|
407 |
function getTriggerDefinition($trigger)
|
|
408 |
{
|
|
409 |
$db =& $this->getDBInstance();
|
|
410 |
if (PEAR::isError($db)) {
|
|
411 |
return $db;
|
|
412 |
}
|
|
413 |
|
|
414 |
$query = "SELECT sys1.name trigger_name,
|
|
415 |
sys2.name table_name,
|
|
416 |
c.text trigger_body,
|
|
417 |
c.encrypted is_encripted,
|
|
418 |
CASE
|
|
419 |
WHEN OBJECTPROPERTY(sys1.id, 'ExecIsTriggerDisabled') = 1
|
|
420 |
THEN 0 ELSE 1
|
|
421 |
END trigger_enabled,
|
|
422 |
CASE
|
|
423 |
WHEN OBJECTPROPERTY(sys1.id, 'ExecIsInsertTrigger') = 1
|
|
424 |
THEN 'INSERT'
|
|
425 |
WHEN OBJECTPROPERTY(sys1.id, 'ExecIsUpdateTrigger') = 1
|
|
426 |
THEN 'UPDATE'
|
|
427 |
WHEN OBJECTPROPERTY(sys1.id, 'ExecIsDeleteTrigger') = 1
|
|
428 |
THEN 'DELETE'
|
|
429 |
END trigger_event,
|
|
430 |
CASE WHEN OBJECTPROPERTY(sys1.id, 'ExecIsInsteadOfTrigger') = 1
|
|
431 |
THEN 'INSTEAD OF' ELSE 'AFTER'
|
|
432 |
END trigger_type,
|
|
433 |
'' trigger_comment
|
|
434 |
FROM sysobjects sys1
|
|
435 |
JOIN sysobjects sys2 ON sys1.parent_obj = sys2.id
|
|
436 |
JOIN syscomments c ON sys1.id = c.id
|
|
437 |
WHERE sys1.xtype = 'TR'
|
|
438 |
AND sys1.name = ". $db->quote($trigger, 'text');
|
|
439 |
|
|
440 |
$types = array(
|
|
441 |
'trigger_name' => 'text',
|
|
442 |
'table_name' => 'text',
|
|
443 |
'trigger_body' => 'text',
|
|
444 |
'trigger_type' => 'text',
|
|
445 |
'trigger_event' => 'text',
|
|
446 |
'trigger_comment' => 'text',
|
|
447 |
'trigger_enabled' => 'boolean',
|
|
448 |
'is_encripted' => 'boolean',
|
|
449 |
);
|
|
450 |
|
|
451 |
$def = $db->queryRow($query, $types, MDB2_FETCHMODE_ASSOC);
|
|
452 |
if (PEAR::isError($def)) {
|
|
453 |
return $def;
|
|
454 |
}
|
|
455 |
$trg_body = $db->queryCol('EXEC sp_helptext '. $db->quote($trigger, 'text'), 'text');
|
|
456 |
if (!PEAR::isError($trg_body)) {
|
d1403f
|
457 |
$def['trigger_body'] = implode(' ', $trg_body);
|
95ebbc
|
458 |
}
|
T |
459 |
return $def;
|
|
460 |
}
|
|
461 |
|
|
462 |
// }}}
|
|
463 |
// {{{ tableInfo()
|
|
464 |
|
|
465 |
/**
|
|
466 |
* Returns information about a table or a result set
|
|
467 |
*
|
|
468 |
* NOTE: only supports 'table' and 'flags' if <var>$result</var>
|
|
469 |
* is a table name.
|
|
470 |
*
|
|
471 |
* @param object|string $result MDB2_result object from a query or a
|
|
472 |
* string containing the name of a table.
|
|
473 |
* While this also accepts a query result
|
|
474 |
* resource identifier, this behavior is
|
|
475 |
* deprecated.
|
|
476 |
* @param int $mode a valid tableInfo mode
|
|
477 |
*
|
|
478 |
* @return array an associative array with the information requested.
|
|
479 |
* A MDB2_Error object on failure.
|
|
480 |
*
|
|
481 |
* @see MDB2_Driver_Common::tableInfo()
|
|
482 |
*/
|
|
483 |
function tableInfo($result, $mode = null)
|
|
484 |
{
|
|
485 |
if (is_string($result)) {
|
|
486 |
return parent::tableInfo($result, $mode);
|
|
487 |
}
|
|
488 |
|
|
489 |
$db =& $this->getDBInstance();
|
|
490 |
if (PEAR::isError($db)) {
|
|
491 |
return $db;
|
|
492 |
}
|
|
493 |
|
|
494 |
$resource = MDB2::isResultCommon($result) ? $result->getResource() : $result;
|
|
495 |
if (!is_resource($resource)) {
|
|
496 |
return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
|
|
497 |
'Could not generate result resource', __FUNCTION__);
|
|
498 |
}
|
|
499 |
|
|
500 |
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
|
|
501 |
if ($db->options['field_case'] == CASE_LOWER) {
|
|
502 |
$case_func = 'strtolower';
|
|
503 |
} else {
|
|
504 |
$case_func = 'strtoupper';
|
|
505 |
}
|
|
506 |
} else {
|
|
507 |
$case_func = 'strval';
|
|
508 |
}
|
|
509 |
|
|
510 |
$count = @mssql_num_fields($resource);
|
|
511 |
$res = array();
|
|
512 |
|
|
513 |
if ($mode) {
|
|
514 |
$res['num_fields'] = $count;
|
|
515 |
}
|
|
516 |
|
|
517 |
$db->loadModule('Datatype', null, true);
|
|
518 |
for ($i = 0; $i < $count; $i++) {
|
|
519 |
$res[$i] = array(
|
|
520 |
'table' => '',
|
|
521 |
'name' => $case_func(@mssql_field_name($resource, $i)),
|
|
522 |
'type' => @mssql_field_type($resource, $i),
|
|
523 |
'length' => @mssql_field_length($resource, $i),
|
|
524 |
'flags' => '',
|
|
525 |
);
|
|
526 |
$mdb2type_info = $db->datatype->mapNativeDatatype($res[$i]);
|
|
527 |
if (PEAR::isError($mdb2type_info)) {
|
|
528 |
return $mdb2type_info;
|
|
529 |
}
|
|
530 |
$res[$i]['mdb2type'] = $mdb2type_info[0][0];
|
|
531 |
if ($mode & MDB2_TABLEINFO_ORDER) {
|
|
532 |
$res['order'][$res[$i]['name']] = $i;
|
|
533 |
}
|
|
534 |
if ($mode & MDB2_TABLEINFO_ORDERTABLE) {
|
|
535 |
$res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
|
|
536 |
}
|
|
537 |
}
|
|
538 |
|
|
539 |
return $res;
|
|
540 |
}
|
|
541 |
|
|
542 |
// }}}
|
|
543 |
// {{{ _mssql_field_flags()
|
|
544 |
|
|
545 |
/**
|
|
546 |
* Get a column's flags
|
|
547 |
*
|
|
548 |
* Supports "not_null", "primary_key",
|
|
549 |
* "auto_increment" (mssql identity), "timestamp" (mssql timestamp),
|
|
550 |
* "unique_key" (mssql unique index, unique check or primary_key) and
|
|
551 |
* "multiple_key" (multikey index)
|
|
552 |
*
|
|
553 |
* mssql timestamp is NOT similar to the mysql timestamp so this is maybe
|
|
554 |
* not useful at all - is the behaviour of mysql_field_flags that primary
|
|
555 |
* keys are alway unique? is the interpretation of multiple_key correct?
|
|
556 |
*
|
|
557 |
* @param string $table the table name
|
|
558 |
* @param string $column the field name
|
|
559 |
*
|
|
560 |
* @return string the flags
|
|
561 |
*
|
|
562 |
* @access protected
|
|
563 |
* @author Joern Barthel <j_barthel@web.de>
|
|
564 |
*/
|
|
565 |
function _mssql_field_flags($table, $column)
|
|
566 |
{
|
|
567 |
$db =& $this->getDBInstance();
|
|
568 |
if (PEAR::isError($db)) {
|
|
569 |
return $db;
|
|
570 |
}
|
|
571 |
|
|
572 |
static $tableName = null;
|
|
573 |
static $flags = array();
|
|
574 |
|
|
575 |
if ($table != $tableName) {
|
|
576 |
|
|
577 |
$flags = array();
|
|
578 |
$tableName = $table;
|
|
579 |
|
|
580 |
// get unique and primary keys
|
|
581 |
$res = $db->queryAll("EXEC SP_HELPINDEX[$table]", null, MDB2_FETCHMODE_ASSOC);
|
|
582 |
|
|
583 |
foreach ($res as $val) {
|
|
584 |
$val = array_change_key_case($val, CASE_LOWER);
|
|
585 |
$keys = explode(', ', $val['index_keys']);
|
|
586 |
|
|
587 |
if (sizeof($keys) > 1) {
|
|
588 |
foreach ($keys as $key) {
|
|
589 |
$this->_add_flag($flags[$key], 'multiple_key');
|
|
590 |
}
|
|
591 |
}
|
|
592 |
|
|
593 |
if (strpos($val['index_description'], 'primary key')) {
|
|
594 |
foreach ($keys as $key) {
|
|
595 |
$this->_add_flag($flags[$key], 'primary_key');
|
|
596 |
}
|
|
597 |
} elseif (strpos($val['index_description'], 'unique')) {
|
|
598 |
foreach ($keys as $key) {
|
|
599 |
$this->_add_flag($flags[$key], 'unique_key');
|
|
600 |
}
|
|
601 |
}
|
|
602 |
}
|
|
603 |
|
|
604 |
// get auto_increment, not_null and timestamp
|
|
605 |
$res = $db->queryAll("EXEC SP_COLUMNS[$table]", null, MDB2_FETCHMODE_ASSOC);
|
|
606 |
|
|
607 |
foreach ($res as $val) {
|
|
608 |
$val = array_change_key_case($val, CASE_LOWER);
|
|
609 |
if ($val['nullable'] == '0') {
|
|
610 |
$this->_add_flag($flags[$val['column_name']], 'not_null');
|
|
611 |
}
|
|
612 |
if (strpos($val['type_name'], 'identity')) {
|
|
613 |
$this->_add_flag($flags[$val['column_name']], 'auto_increment');
|
|
614 |
}
|
|
615 |
if (strpos($val['type_name'], 'timestamp')) {
|
|
616 |
$this->_add_flag($flags[$val['column_name']], 'timestamp');
|
|
617 |
}
|
|
618 |
}
|
|
619 |
}
|
|
620 |
|
|
621 |
if (!empty($flags[$column])) {
|
|
622 |
return(implode(' ', $flags[$column]));
|
|
623 |
}
|
|
624 |
return '';
|
|
625 |
}
|
|
626 |
|
|
627 |
// }}}
|
|
628 |
// {{{ _add_flag()
|
|
629 |
|
|
630 |
/**
|
|
631 |
* Adds a string to the flags array if the flag is not yet in there
|
|
632 |
* - if there is no flag present the array is created
|
|
633 |
*
|
|
634 |
* @param array &$array the reference to the flag-array
|
|
635 |
* @param string $value the flag value
|
|
636 |
*
|
|
637 |
* @return void
|
|
638 |
*
|
|
639 |
* @access protected
|
|
640 |
* @author Joern Barthel <j_barthel@web.de>
|
|
641 |
*/
|
|
642 |
function _add_flag(&$array, $value)
|
|
643 |
{
|
|
644 |
if (!is_array($array)) {
|
|
645 |
$array = array($value);
|
|
646 |
} elseif (!in_array($value, $array)) {
|
|
647 |
array_push($array, $value);
|
|
648 |
}
|
|
649 |
}
|
|
650 |
|
|
651 |
// }}}
|
|
652 |
}
|
|
653 |
?> |