thomascube
2006-05-01 6204390af16bcf50f82da61a1aefc2ad0c0adf94
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 InterbaseBase driver for the reverse engineering module
52  *
53  * @package MDB2
54  * @category Database
55  * @author  Lukas Smith <smith@dybnet.de>
56  */
57 class MDB2_Driver_Reverse_ibase 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 * FROM $result WHERE 1=0");
93             if (PEAR::isError($id)) {
94                 return $id;
95             }
96             $got_string = true;
97         } elseif (MDB2::isResultCommon($result)) {
98             /*
99              * Probably received a result object.
100              * Extract the result resource identifier.
101              */
102             $id = $result->getResource();
103             $got_string = false;
104         } else {
105             /*
106              * Probably received a result resource identifier.
107              * Copy it.
108              * Deprecated.  Here for compatibility only.
109              */
110             $id = $result;
111             $got_string = false;
112         }
113
114         if (!is_resource($id)) {
115             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA);
116         }
117
118         if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
119             if ($db->options['field_case'] == CASE_LOWER) {
120                 $case_func = 'strtolower';
121             } else {
122                 $case_func = 'strtoupper';
123             }
124         } else {
125             $case_func = 'strval';
126         }
127
128         $count = @ibase_num_fields($id);
129         $res   = array();
130
131         if ($mode) {
132             $res['num_fields'] = $count;
133         }
134
135         for ($i = 0; $i < $count; $i++) {
136             $info = @ibase_field_info($id, $i);
137             $res[$i] = array(
138                 'table' => $got_string ? $case_func($result) : '',
139                 'name'  => $case_func($info['name']),
140                 'type'  => $info['type'],
141                 'len'   => $info['length'],
142                 'flags' => ($got_string)
143                             ? $this->_ibaseFieldFlags($info['name'], $result) : '',
144             );
145             if ($mode & MDB2_TABLEINFO_ORDER) {
146                 $res['order'][$res[$i]['name']] = $i;
147             }
148             if ($mode & MDB2_TABLEINFO_ORDERTABLE) {
149                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
150             }
151         }
152
153         // free the result only if we were called on a table
154         if ($got_string) {
155             @ibase_free_result($id);
156         }
157         return $res;
158     }
159
160     // }}}
161     // {{{ _ibaseFieldFlags()
162
163     /**
164      * Get the column's flags
165      *
166      * Supports "primary_key", "unique_key", "not_null", "default",
167      * "computed" and "blob".
168      *
169      * @param string $field_name  the name of the field
170      * @param string $table_name  the name of the table
171      *
172      * @return string  the flags
173      *
174      * @access protected
175      */
176     function _ibaseFieldFlags($field_name, $table_name)
177     {
178         $db =& $this->getDBInstance();
179         if (PEAR::isError($db)) {
180             return $db;
181         }
182
183         $query = 'SELECT R.RDB$CONSTRAINT_TYPE CTYPE'
184                .' FROM RDB$INDEX_SEGMENTS I'
185                .'  JOIN RDB$RELATION_CONSTRAINTS R ON I.RDB$INDEX_NAME=R.RDB$INDEX_NAME'
186                .' WHERE I.RDB$FIELD_NAME=\'' . $field_name . '\''
187                .'  AND UPPER(R.RDB$RELATION_NAME)=\'' . strtoupper($table_name) . '\'';
188
189         $result = $db->_doQuery($query);
190         if (PEAR::isError($result)) {
191             return $result;
192         }
193
194         $flags = '';
195         if ($obj = @ibase_fetch_object($result)) {
196             @ibase_free_result($result);
197             if (isset($obj->CTYPE)  && trim($obj->CTYPE) == 'PRIMARY KEY') {
198                 $flags.= 'primary_key ';
199             }
200             if (isset($obj->CTYPE)  && trim($obj->CTYPE) == 'UNIQUE') {
201                 $flags.= 'unique_key ';
202             }
203         }
204
205         $query = 'SELECT R.RDB$NULL_FLAG AS NFLAG,'
206                .'  R.RDB$DEFAULT_SOURCE AS DSOURCE,'
207                .'  F.RDB$FIELD_TYPE AS FTYPE,'
208                .'  F.RDB$COMPUTED_SOURCE AS CSOURCE'
209                .' FROM RDB$RELATION_FIELDS R '
210                .'  JOIN RDB$FIELDS F ON R.RDB$FIELD_SOURCE=F.RDB$FIELD_NAME'
211                .' WHERE UPPER(R.RDB$RELATION_NAME)=\'' . strtoupper($table_name) . '\''
212                .'  AND R.RDB$FIELD_NAME=\'' . $field_name . '\'';
213
214         $result = $db->_doQuery($query);
215         if (PEAR::isError($result)) {
216             return $result;
217         }
218
219         if ($obj = @ibase_fetch_object($result)) {
220             @ibase_free_result($result);
221             if (isset($obj->NFLAG)) {
222                 $flags.= 'not_null ';
223             }
224             if (isset($obj->DSOURCE)) {
225                 $flags.= 'default ';
226             }
227             if (isset($obj->CSOURCE)) {
228                 $flags.= 'computed ';
229             }
230             if (isset($obj->FTYPE)  && $obj->FTYPE == 261) {
231                 $flags.= 'blob ';
232             }
233         }
234
235         return trim($flags);
236     }
237 }
238 ?>