~ubuntu-branches/ubuntu/hardy/gallery2/hardy-security

« back to all changes in this revision

Viewing changes to modules/core/classes/GalleryStorage/OracleStorage.class

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2006-04-16 16:42:35 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060416164235-8uy0u4bfjdxpge2o
Tags: 2.1.1-1
* New upstream release (Closes: #362936)
  + Bugfixes for Postgres7 (Closes: #359000, #362152)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
 * $RCSfile: OracleStorage.class,v $
 
4
 *
 
5
 * Gallery - a web based photo album viewer and editor
 
6
 * Copyright (C) 2000-2006 Bharat Mediratta
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License as published by
 
10
 * the Free Software Foundation; either version 2 of the License, or (at
 
11
 * your option) any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful, but
 
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 * General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program; if not, write to the Free Software
 
20
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 
21
 */
 
22
/**
 
23
 * @version $Revision: 1.10.2.1 $ $Date: 2006/04/10 15:36:16 $
 
24
 * @package GalleryCore
 
25
 * @author Alan Harder <alan.harder@sun.com>
 
26
 */
 
27
 
 
28
GalleryCoreApi::requireOnce('modules/core/classes/GalleryStorage.class', true);
 
29
 
 
30
/**
 
31
 * Oracle extension of the GalleryStorage class.
 
32
 *
 
33
 * This object implements the hooks for saving and restoring objects in an
 
34
 * Oracle database.
 
35
 *
 
36
 * @package GalleryCore
 
37
 * @subpackage Storage
 
38
 */
 
39
class OracleStorage extends GalleryStorage {
 
40
 
 
41
    /**
 
42
     * The type of Oracle database (oci8, oci8po, oci805 or oracle)
 
43
     *
 
44
     * @var string $_oracleDbType
 
45
     * @access private
 
46
     */
 
47
    var $_oracleDbType;
 
48
 
 
49
    /**
 
50
     * Constructor
 
51
     *
 
52
     */
 
53
    function OracleStorage($config) {
 
54
        $this->GalleryStorage($config);
 
55
        $this->_oracleDbType = $config['type'];
 
56
        $this->_isTransactional = true;
 
57
        $this->_isEmptyAllowedForNotNullColumn = false;
 
58
    }
 
59
 
 
60
    /**
 
61
     * Return the type of this database
 
62
     *
 
63
     * @return string
 
64
     */
 
65
    function getAdoDbType() {
 
66
        return $this->_oracleDbType;
 
67
    }
 
68
 
 
69
    /**
 
70
     * Return the type of this database
 
71
     *
 
72
     * @return string
 
73
     */
 
74
    function getType() {
 
75
        return 'oracle';
 
76
    }
 
77
 
 
78
    /**
 
79
     * @see GalleryStorage::_setConnectionSettings
 
80
     */
 
81
    function _setConnectionSettings(&$db) {
 
82
        /* May cause "ORA-12705: Cannot access NLS data files or invalid environment specified" */
 
83
        /* @putenv("NLS_LANG=UTF8"); */
 
84
        
 
85
        return null;
 
86
    }
 
87
 
 
88
    /**
 
89
     * @see GalleryStorage::getFunctionsSql
 
90
     */
 
91
    function getFunctionSql($functionName, $args) {
 
92
        switch($functionName) {
 
93
        case 'CONCAT':
 
94
            $sql = implode(' || ', $args);
 
95
            break;
 
96
 
 
97
        case 'BITAND':
 
98
            $sql = 'BITAND(' . $args[0] . ',' . $args[1] . ')';
 
99
            break;
 
100
 
 
101
        case 'BIT_OR':
 
102
            $sql = 'BIT_OR(' . $args[0] . ')';
 
103
            break;
 
104
            
 
105
        case 'AS':
 
106
            $sql = '';
 
107
            break;
 
108
 
 
109
        case 'SUBSTRING':
 
110
            $sql = sprintf('SUBSTR(%s)', implode(', ', $args));
 
111
            break;
 
112
 
 
113
        case 'RAND':
 
114
            if (!empty($args)) {
 
115
                return array(GalleryCoreApi::error(ERROR_UNIMPLEMENTED, __FILE__, __LINE__,
 
116
                                                  $functionName . ' ' . implode(' ', $args)), null);
 
117
            }
 
118
            $sql = 'DBMS_RANDOM.RANDOM';
 
119
            break;
 
120
 
 
121
        case 'LIMIT':
 
122
            $sql = 'SELECT * FROM (' . $args[1] . ') WHERE ROWNUM <= ' . $args[0];
 
123
            break;
 
124
 
 
125
        case 'CASE':
 
126
            $sql = array();
 
127
            while (count($args) > 1) {
 
128
                $sql[] = 'WHEN ' . array_shift($args) . ' THEN ' . array_shift($args);
 
129
            }
 
130
            $sql = 'CASE ' . implode(' ', $sql) . ' ELSE ' . $args[0] . ' END';
 
131
            break;
 
132
 
 
133
        case 'LIKE':
 
134
            $sql = $args[0] . ' LIKE ' . $args[1];
 
135
            break;
 
136
 
 
137
        case 'MULTI_INSERT':
 
138
            /* 
 
139
             * 0 - table name
 
140
             * 1 - array of column names
 
141
             * 2 - number of rows
 
142
             */
 
143
            $markers = GalleryUtilities::makeMarkers(sizeof($args[1]));
 
144
            $rowList = array(); //php4.2: array-fill(1, $args[2], 'SELECT '.$markers.' FROM DUAL');
 
145
            for ($i = 0; $i < $args[2]; $i++) {
 
146
                $rowList[] = 'SELECT ' . $markers . ' FROM DUAL';
 
147
            }
 
148
            $sql = 'INSERT INTO ' . $args[0] . ' (';
 
149
            $sql .= implode(', ', $args[1]);
 
150
            $sql .= ') ' . implode(' UNION ALL ', $rowList);
 
151
            break;
 
152
 
 
153
        case 'AVG':
 
154
            $sql = sprintf('AVG(%s)', $args[0]);
 
155
            break;
 
156
 
 
157
        default:
 
158
            return array(GalleryCoreApi::error(ERROR_UNIMPLEMENTED, __FILE__, __LINE__,
 
159
                                              $functionName . ' ' . implode(' ', $args)), null);
 
160
        }
 
161
 
 
162
        return array(null, $sql);
 
163
    }
 
164
 
 
165
    /**
 
166
     * @see GalleryStorage::cleanStore
 
167
     */
 
168
    function cleanStore() {
 
169
        $ret = parent::cleanStore();
 
170
        if ($ret) {
 
171
            return $ret->wrap(__FILE__, __LINE__);
 
172
        }
 
173
 
 
174
        /*
 
175
         * Create a temporary database connection and remove our custom
 
176
         * aggregate function.
 
177
         */
 
178
        list ($ret, $tmpDb) = $this->_getConnection(true);
 
179
        if ($ret) {
 
180
            return $ret->wrap(__FILE__, __LINE__);
 
181
        }
 
182
 
 
183
        $this->_traceStart();
 
184
        $recordSet1 = $tmpDb->Execute('DROP FUNCTION BIT_OR');
 
185
        $recordSet2 = $tmpDb->Execute('DROP TYPE BIT_OR_IMPL');
 
186
        $tmpDb->Close();
 
187
        $this->_traceStop();
 
188
 
 
189
        if (empty($recordSet1) || empty($recordSet2)) {
 
190
            return GalleryCoreApi::error(ERROR_STORAGE_FAILURE, __FILE__, __LINE__);
 
191
        }
 
192
 
 
193
        return null;
 
194
    }
 
195
 
 
196
    /**
 
197
     * @see GalleryStorage::configureStore
 
198
     */
 
199
    function configureStore($moduleId, $upgradeInfo=array()) {
 
200
        if ($moduleId == 'core') {
 
201
            /* Install our custom aggregate function */
 
202
            $ret = $this->_executeSqlFile(dirname(__FILE__) . '/OracleCreateBitOr.sql');
 
203
 
 
204
            /*
 
205
             * Ignore errors here, since we'll get them every time we try to
 
206
             * install the aggregate, which will happen every time we upgrade core
 
207
             * or install in a database with another Gallery already present.
 
208
             *
 
209
             * XXX: At some point figure out a way to detect if the aggregate is
 
210
             * already there before trying to install it again.
 
211
             */
 
212
        }
 
213
 
 
214
        $ret = parent::configureStore($moduleId, $upgradeInfo);
 
215
        if ($ret) {
 
216
            return $ret->wrap(__FILE__, __LINE__);
 
217
        }
 
218
 
 
219
        return null;
 
220
     }
 
221
 
 
222
    /**
 
223
     * Get database version.
 
224
     * @return string version
 
225
     */
 
226
    function getVersion() {
 
227
        if (function_exists('oci_server_version')) {
 
228
            return oci_server_version();
 
229
        }
 
230
        return ociserverversion();
 
231
    }
 
232
 
 
233
    /**
 
234
     * @see GalleryStorage::_getOptimizeStatement
 
235
     */
 
236
    function _getOptimizeStatement() {
 
237
        /* Is this the right command to use? */
 
238
        return 'ANALYZE TABLE %s COMPUTE STATISTICS';
 
239
    }
 
240
 
 
241
    /**
 
242
     * @see GalleryStorage::_normalizeValue
 
243
     */
 
244
    function _normalizeValue($value, $memberData, $fromDb=false) {
 
245
        if ($fromDb && is_null($value) && ($memberData['type'] == STORAGE_TYPE_STRING
 
246
                                        || $memberData['type'] == STORAGE_TYPE_TEXT)) {
 
247
            return '';
 
248
        } else {
 
249
            return parent::_normalizeValue($value, $memberData, $fromDb);
 
250
        }
 
251
    }
 
252
}
 
253
?>