File: indices.inc.php

package info (click to toggle)
ibwebadmin 0.98-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,916 kB
  • ctags: 1,950
  • sloc: php: 12,454; makefile: 7
file content (323 lines) | stat: -rw-r--r-- 11,571 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
// File           indices.inc.php / ibWebAdmin
// Purpose        functions working with indices, included from accessories.php
// Author         Lutz Brueckner <irie@gmx.de>
// Copyright      (c) 2000, 2001 by Lutz Brueckner,
//                published under the terms of the GNU General Public Licence v.2,
//                see file LICENCE for details
// Created        <01/10/10 20:52:58 lb>
//
// $Id: indices.inc.php,v 1.12 2004/04/22 19:05:38 lbrueckner Exp $


//
// create an index from the values in the index form
//
function create_index() {
    global $HTTP_POST_VARS, $dbhandle, $indices;
    global $ib_error, $lsql;

    $lsql = 'CREATE ';
    if (isset($HTTP_POST_VARS['def_index_uniq'])) {
        $lsql .= 'UNIQUE ';
    }
    $lsql .= $HTTP_POST_VARS['def_index_dir'].' INDEX '.$HTTP_POST_VARS['def_index_name'].' ';
    $lsql .= 'ON '.$HTTP_POST_VARS['def_index_table'].' ('.$HTTP_POST_VARS['def_index_segs'].')';

    if (DEBUG) add_debug('lsql', __FILE__, __LINE__);

    if (!@ibase_query($dbhandle, $lsql)) {
        $ib_error = ibase_errmsg();
    }

    if ((!isset($HTTP_POST_VARS['def_index_activ'])  
        ||  $HTTP_POST_VARS['def_index_activ'] == FALSE)
    && (empty($ib_error))) {
        alter_index($HTTP_POST_VARS['def_index_name'], 'INACTIVE');
    }

    if (empty($ib_error)) {
        $iname = strtoupper($HTTP_POST_VARS['def_index_name']);
        $indices[$iname]['table'] = $HTTP_POST_VARS['def_index_table'];
        $indices[$iname]['dir']   = $HTTP_POST_VARS['def_index_dir'];
        $indices[$iname]['uniq']  = (isset($HTTP_POST_VARS['def_index_uniq']))  ? TRUE : FALSE;
        $indices[$iname]['active']= (isset($HTTP_POST_VARS['def_index_activ'])) ? TRUE : FALSE;
        $segs = explode(',', strtoupper($HTTP_POST_VARS['def_index_segs']));
        $indices[$iname]['seg'] = array();
        foreach ($segs as $seg) {
            $indices[$iname]['seg'][] = $seg;
        } 
        return TRUE;
    }
    else {
       return FALSE;
    }
}


//
// try to modify the index $iname by recreate the index with the new settings
//
function modify_index($iname) {
    global $HTTP_POST_VARS, $dbhandle, $indices;
    global $ib_error, $lsql;

    // alter the active/inactive status if the change was selected
    if (isset($HTTP_POST_VARS['def_index_activ'])  && $indices[$iname]['active'] == FALSE) {
        if (alter_index($iname, 'ACTIVE')) {
            $indices[$iname]['active'] = TRUE;
        } 
        else {
            return FALSE;
        }
    }
    elseif (!isset($HTTP_POST_VARS['def_index_activ'])  && $indices[$iname]['active'] == TRUE) {
        if (alter_index($iname, 'INACTIVE')) {
            $indices[$iname]['active'] = FALSE;
        }
        else {
            return FALSE;
        }
    }

    // check if the index properties are modified
    $uniq_flag = (isset($HTTP_POST_VARS['def_index_uniq']))  ? TRUE : FALSE;
    $acti_flag = (isset($HTTP_POST_VARS['def_index_activ'])) ? TRUE : FALSE;
    if ($indices[$iname]['table'] != $HTTP_POST_VARS['def_index_table']
    ||  $iname != $HTTP_POST_VARS['def_index_name']
    ||  $indices[$iname]['dir']   != $HTTP_POST_VARS['def_index_dir']
    ||  $indices[$iname]['uniq']  != $uniq_flag 
    ||  $indices[$iname]['active']!= $acti_flag
    ||  implode(',', $indices[$iname]['seg']) !=  strtoupper($HTTP_POST_VARS['def_index_segs'])) {

        // drop the old index
        $lsql = 'DROP INDEX '.$iname;
        if (DEBUG) add_debug('lsql', __FILE__, __LINE__);
        if (!@ibase_query($dbhandle, $lsql)) {

            $ib_error = ibase_errmsg();
            return FALSE;
        }

        // try to recreate with the new properties        
        if (create_index()) {

            return TRUE;
        }

        // try to recreate the old one
        else {
            $lsql = 'CREATE ';
            if (isset($indices[$iname]['uniq'])) {
                $lsql .= 'UNIQUE ';
            }
            $lsql .= $indices[$iname]['dir']." INDEX $iname ON "
                    .$indices[$iname]['table'].' ('.implode(',', $indices[$iname]['seg']).')';
            if (DEBUG) add_debug('lsql', __FILE__, __LINE__);
            if (!@ibase_query($trans, $lsql)) {
                ibase_rollback($trans);
                $ib_error = ibase_errmsg();
            }

            return FALSE;
        }
    }

    return TRUE;
}


//
// set the index $iname active or inactive
//
function alter_index($iname, $state) {
    global $dbhandle, $ib_error, $lsql;

    $lsql = "ALTER INDEX $iname $state";
    if (DEBUG) add_debug('lsql', __FILE__, __LINE__);
    if (!@ibase_query($dbhandle, $lsql)) {

        $ib_error = ibase_errmsg();
        return FALSE;
    }

    return TRUE;
}


//
// drop the index $name off the database
//
function drop_index($name) {
    global $indices, $dbhandle;
    global $ib_error, $lsql;

    $lsql = 'DROP INDEX '.$name;
    if (DEBUG) add_debug('lsql', __FILE__, __LINE__);
    if (!@ibase_query($dbhandle, $lsql)) {

        $ib_error = ibase_errmsg();
        return TRUE;
    }
    else {

        unset($indices[$name]);
        return TRUE;
    }
}


//
// return an array with the properties of the defined indeces 
//
function get_indices($order, $dir) {
    global $dbhandle;

    $order_field = ($order == 'name') ? 'I.RDB$INDEX_NAME' : 'I.RDB$RELATION_NAME';

    $sql = 'SELECT I.RDB$INDEX_NAME AS INAME, '
                 .'I.RDB$RELATION_NAME AS RNAME, '
                 .'I.RDB$UNIQUE_FLAG AS UFLAG, '
                 .'I.RDB$INDEX_INACTIVE AS IFLAG, '
                 .'I.RDB$INDEX_TYPE AS ITYPE, '
                 .'S.RDB$FIELD_NAME AS FNAME, '
                 .'S.RDB$FIELD_POSITION AS POS '
            .'FROM RDB$INDICES I '
            .'JOIN RDB$INDEX_SEGMENTS S '
              .'ON S.RDB$INDEX_NAME=I.RDB$INDEX_NAME '
           .'WHERE (I.RDB$SYSTEM_FLAG IS NULL  OR  I.RDB$SYSTEM_FLAG=0)'
             .'AND I.RDB$FOREIGN_KEY IS NULL '
             ."AND I.RDB\$INDEX_NAME NOT STARTING WITH 'RDB\$' "
           .'ORDER BY '.$order_field.' '.$dir;
    $trans = ibase_trans(TRANS_READ, $dbhandle);
    $res = ibase_query($trans, $sql) or ib_error();

    $indices = array();
    while ($obj = ibase_fetch_object($res)) {
        if (!isset($indices[$obj->INAME])) {
            $iname = trim($obj->INAME);
            $indices[$iname]['table'] = trim($obj->RNAME);
            $indices[$iname]['dir']   = (isset($obj->ITYPE)  &&  $obj->ITYPE == 1) ? 'DESC' : 'ASC';
            $indices[$iname]['uniq']  = (isset($obj->UFLAG)) ? TRUE  : FALSE;
            $indices[$iname]['active']= (isset($obj->IFLAG)  && $obj->IFLAG == 1) ? FALSE : TRUE;
            $indices[$iname]['pos']   = $obj->POS;
        }
        $indices[$iname]['seg'][$obj->POS] = trim($obj->FNAME);
    }
    ibase_commit($trans);

    return $indices;
}


//
// output a html-table with a form to define/modify an index 
//
// Variables:  $indexname  name of the index to modify
//             $title      headline-string for the table
function index_definition($indexname, $title) {
    global $indices, $HTTP_POST_VARS, $acc_strings;

    if ($indexname != NULL  &&  !isset($HTTP_POST_VARS['acc_modind_doit'])) {
        $name   = $indexname;
        $table  = $indices[$indexname]['table'];
        $dir    = $indices[$indexname]['dir'];
        $uniq   = $indices[$indexname]['uniq'];
        $active = $indices[$indexname]['active'];
        $segs   = implode(',', $indices[$indexname]['seg']);
    }
    else {
        $name   = (isset($HTTP_POST_VARS['def_index_name']))  ? $HTTP_POST_VARS['def_index_name']  : '';
        $table  = (isset($HTTP_POST_VARS['def_index_table'])) ? $HTTP_POST_VARS['def_index_table'] : '';
        $dir    = (isset($HTTP_POST_VARS['def_index_dir']))   ? $HTTP_POST_VARS['def_index_dir']   : 'ASC';
        $uniq   = (isset($HTTP_POST_VARS['def_index_uniq']))  ? TRUE : FALSE;
        $active = (isset($HTTP_POST_VARS['def_index_activ'])) ? TRUE : FALSE;
        $segs   = (isset($HTTP_POST_VARS['def_index_segs']))  ? $HTTP_POST_VARS['def_index_segs']  : '';
    }

?>
<table border cellpadding="3" cellspacing="0">
  <tr>
    <th colspan="6" align="left"><?php echo $title; ?></th>
  </tr>
  <tr>
    <td valign="top"><b><?php echo $acc_strings['Name']; ?></b><br>
        <input type="text" size="20" maxlength="31" name="def_index_name" value="<?php echo $name; ?>">
    </td>
    <td valign="top"><b><?php echo $acc_strings['Table']; ?></b><br>
        <select name="def_index_table">
          <?php build_table_options($table, array('noviews', 'owner')); ?>
        </select>
    </td>
    <td align="center" valign="top"><b><?php echo $acc_strings['Active']; ?></b><br>
      <input type="checkbox" name="def_index_activ" <?php if ($active) echo 'checked'; ?>> 
    </td>
    <td align="center" valign="top"><b><?php echo $acc_strings['Unique']; ?></b><br>
      <input type="checkbox" name="def_index_uniq" <?php if ($uniq) echo 'checked'; ?>> 
    </td>
    <td align="center" valign="top"><b><?php echo $acc_strings['Sort']; ?></b><br>
      <select name="def_index_dir">
        <option<?php if ($dir == 'ASC') echo ' selected'; ?>> ASC
        <option<?php if ($dir == 'DESC') echo ' selected'; ?>> DESC
      </select>
    </td>
    <td valign="top"><b><?php echo $acc_strings['ColExpl']; ?></b><br>
        <input type="text" size="30" maxlength="128" name="def_index_segs" value="<?php echo $segs; ?>">
    </td> 
  </tr>
</table>
<?php

}


//
// return the html displaying the index details in a table
//
function &get_index_table(&$indices, $order, $dir) {
    global $HTTP_SERVER_VARS, $acc_strings;

    $name_url  = url_session($HTTP_SERVER_VARS['PHP_SELF'].'?idxorder=1&order=name');
    $table_url = url_session($HTTP_SERVER_VARS['PHP_SELF'].'?idxorder=1&order=table');

    if ($order == 'name') {
        $name_title = ($dir == 'ASC') ? '*&nbsp;'.$acc_strings['Name'] : $acc_strings['Name'].'&nbsp;*';
        $table_title = $acc_strings['Table'];
    }
    else {
        $name_title  = $acc_strings['Name'];
        $table_title = ($dir == 'ASC') ? '*&nbsp;'.$acc_strings['Table'] : $acc_strings['Table'].'&nbsp;*';
    }

    $html = "<table cellpadding=\"0\" cellspacing=\"0\" border>\n"
           ."  <tr align=\"left\">\n"
           .'    <th class="detail"><a href="'.$name_url.'">'.$name_title."</a></th>\n"
           .'    <th class="detail">'.$acc_strings['Active']."</th>\n"
           .'    <th class="detail">'.$acc_strings['Unique']."</th>\n"
           .'    <th class="detail">'.$acc_strings['Sort']."</th>\n"
           .'    <th class="detail"><a href="'.$table_url.'">'.$table_title."</a></th>\n"
           .'    <th class="detail">'.$acc_strings['Columns']."</th>\n"
           ."  </tr>\n";

    foreach($indices as $iname => $index) {
        $uniq_flag   = ($index['uniq'] == TRUE)   ? $acc_strings['Yes'] : $acc_strings['No'];
        $active_flag = ($index['active'] == TRUE) ? $acc_strings['Yes'] : $acc_strings['No'];
        $segs = implode(',&nbsp;', $index['seg']);


        $html .= "  <tr>\n"
                .'    <td class="detail">'.$iname."</td>\n"
                .'    <td align="center" class="detail">'.$active_flag."</td>\n"
                .'    <td align="center" class="detail">'.$uniq_flag."</td>\n"
                .'    <td class="detail">'.$index['dir']."</td>\n"
                .'    <td class="detail">'.$index['table']."</td>\n"
                .'    <td class="detail">'.$segs."</td>\n"
                ."  </tr>\n";
    }

    $html .= "</table>\n";

    return $html;
}

?>