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

« back to all changes in this revision

Viewing changes to modules/core/classes/GalleryStorage/PostgreSqlStorage.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: PostgreSqlStorage.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.9 $ $Date: 2006/02/20 19:13:09 $
 
24
 * @package GalleryCore
 
25
 * @author Bharat Mediratta <bharat@menalto.com>
 
26
 */
 
27
 
 
28
GalleryCoreApi::requireOnce('modules/core/classes/GalleryStorage.class', true);
 
29
 
 
30
/**
 
31
 * PostgreSQL extension of the GalleryStorage class.
 
32
 *
 
33
 * This object implements the hooks for saving and restoring objects in a
 
34
 * PostgreSQL database.
 
35
 *
 
36
 * @package GalleryCore
 
37
 * @subpackage Storage
 
38
 */
 
39
class PostgreSqlStorage extends GalleryStorage {
 
40
 
 
41
    /**
 
42
     * The type of postgres database (postgres or postgrest)
 
43
     *
 
44
     * @var string $_postgresType
 
45
     * @access private
 
46
     */
 
47
    var $_postgresType;
 
48
 
 
49
    /**
 
50
     * Constructor
 
51
     *
 
52
     */
 
53
    function PostgreSqlStorage($config) {
 
54
        $this->GalleryStorage($config);
 
55
        $this->_postgresType = $config['type'];
 
56
        $this->_isTransactional = true;
 
57
    }
 
58
 
 
59
    /**
 
60
     * Return the type of this database
 
61
     *
 
62
     * @return string
 
63
     */
 
64
    function getAdoDbType() {
 
65
        return $this->_postgresType;
 
66
    }
 
67
 
 
68
    /**
 
69
     * Return the type of this database
 
70
     *
 
71
     * @return string
 
72
     */
 
73
    function getType() {
 
74
        return 'postgres';
 
75
    }
 
76
 
 
77
    /**
 
78
     * @see GalleryStorage::cleanStore
 
79
     */
 
80
    function cleanStore() {
 
81
        $ret = parent::cleanStore();
 
82
        if ($ret) {
 
83
            return $ret->wrap(__FILE__, __LINE__);
 
84
        }
 
85
 
 
86
        /*
 
87
         * Create a temporary database connection and install our custom
 
88
         * aggregate function.
 
89
         */
 
90
        list ($ret, $tmpDb) = $this->_getConnection(true);
 
91
        if ($ret) {
 
92
            return $ret->wrap(__FILE__, __LINE__);
 
93
        }
 
94
 
 
95
        $this->_traceStart();
 
96
        $recordSet = $tmpDb->Execute('DROP AGGREGATE BIT_OR(bit)');
 
97
        $tmpDb->Close();
 
98
        $this->_traceStop();
 
99
 
 
100
        if (empty($recordSet)) {
 
101
            return GalleryCoreApi::error(ERROR_STORAGE_FAILURE, __FILE__, __LINE__);
 
102
        }
 
103
 
 
104
        return null;
 
105
    }
 
106
 
 
107
    /**
 
108
     * @see GalleryStorage::configureStore
 
109
     */
 
110
    function configureStore($moduleId, $upgradeInfo=array()) {
 
111
        if ($moduleId == 'core') {
 
112
            $query = '
 
113
            CREATE AGGREGATE BIT_OR
 
114
            (
 
115
              basetype = bit,
 
116
              sfunc = bitor,
 
117
              stype = bit
 
118
            )';
 
119
 
 
120
            /*
 
121
             * Create a temporary database connection and install our custom
 
122
             * aggregate function.
 
123
             */
 
124
            list ($ret, $tmpDb) = $this->_getConnection(true);
 
125
            if ($ret) {
 
126
                return $ret->wrap(__FILE__, __LINE__);
 
127
            }
 
128
 
 
129
            $this->_traceStart();
 
130
            $recordSet = $tmpDb->Execute($query);
 
131
            $tmpDb->Close();
 
132
            $this->_traceStop();
 
133
 
 
134
            /*
 
135
             * Ignore errors here, since we'll get them every time we try to
 
136
             * install the aggregate, which will happen every time we upgrade core
 
137
             * or install in a database with another Gallery already present.
 
138
             *
 
139
             * XXX: At some point figure out a way to detect if the aggregate is
 
140
             * already there before trying to install it again.
 
141
             */
 
142
        }
 
143
 
 
144
        $ret = parent::configureStore($moduleId, $upgradeInfo);
 
145
        if ($ret) {
 
146
            return $ret->wrap(__FILE__, __LINE__);
 
147
        }
 
148
 
 
149
        return null;
 
150
     }
 
151
 
 
152
    /**
 
153
     * @see GalleryStorage::convertIntToBits
 
154
     */
 
155
    function convertIntToBits($intVal) {
 
156
        return sprintf("%032b", $intVal);
 
157
    }
 
158
 
 
159
    /**
 
160
     * @see GalleryStorage::convertIntToBits
 
161
     */
 
162
    function convertBitsToInt($bitsVal) {
 
163
        return bindec($bitsVal);
 
164
    }
 
165
 
 
166
    /**
 
167
     * @see GalleryStorage::getFunctionsSql
 
168
     */
 
169
    function getFunctionSql($functionName, $args) {
 
170
        switch($functionName) {
 
171
        case 'CONCAT':
 
172
            $sql = implode(' || ', $args);
 
173
            break;
 
174
 
 
175
        case 'BITAND':
 
176
            /* Cast any input values as the 'bit' type */
 
177
            $args = str_replace('?', '?', $args);
 
178
            $sql = $args[0] . ' & ' . $args[1];
 
179
            break;
 
180
 
 
181
        case 'BIT_OR':
 
182
            $sql = 'BIT_OR(' . $args[0] . ')';
 
183
            break;
 
184
 
 
185
        case 'UNIX_TIMESTAMP':
 
186
            $sql = 'date_part(\'epoch\', ' . $args[0] . ')';
 
187
            break;
 
188
 
 
189
        case 'AS':
 
190
            $sql = 'AS';
 
191
            break;
 
192
 
 
193
        case 'SUBSTRING':
 
194
            $sql = sprintf('SUBSTRING(%s)', implode(', ', $args));
 
195
            break;
 
196
 
 
197
        case 'RAND':
 
198
            $sql = sprintf('RANDOM(%s)', empty($args) ? '' : $args[0]);
 
199
            break;
 
200
 
 
201
        case 'LIMIT':
 
202
            $sql = $args[1] . ' LIMIT ' . $args[0];
 
203
            break;
 
204
 
 
205
        case 'CASE':
 
206
            $sql = array();
 
207
            while (count($args) > 1) {
 
208
                $sql[] = 'WHEN ' . array_shift($args) . ' THEN ' . array_shift($args);
 
209
            }
 
210
            $sql = 'CASE ' . implode(' ', $sql) . ' ELSE ' . $args[0] . ' END';
 
211
            break;
 
212
 
 
213
        case 'LIKE':
 
214
            $sql = $args[0] . ' LIKE ' . $args[1];
 
215
            break;
 
216
 
 
217
        case 'MULTI_INSERT':
 
218
            /*
 
219
             * 0 - table name
 
220
             * 1 - array of column names
 
221
             * 2 - number of rows
 
222
             */
 
223
            $markers = GalleryUtilities::makeMarkers(sizeof($args[1]));
 
224
            $rowList = rtrim(str_repeat('SELECT ' . $markers . ' UNION ALL ', $args[2]),
 
225
                             'UNION ALL ');
 
226
            $sql = 'INSERT INTO ' . $args[0] . ' (';
 
227
            $sql .= join(', ', $args[1]);
 
228
            $sql .= ') ' . $rowList;
 
229
            break;
 
230
 
 
231
        case 'AVG':
 
232
            $sql = sprintf('AVG(%s)', $args[0]);
 
233
            break;
 
234
 
 
235
        default:
 
236
            return array(GalleryCoreApi::error(ERROR_UNIMPLEMENTED, __FILE__, __LINE__,
 
237
                                              $functionName . ' ' . implode(' ', $args)), null);
 
238
        }
 
239
 
 
240
        return array(null, $sql);
 
241
    }
 
242
 
 
243
    /**
 
244
     * Get database version.
 
245
     * @return string version
 
246
     */
 
247
    function getVersion() {
 
248
        if (function_exists('pg_version')) {
 
249
            return implode(' ', pg_version());
 
250
        }
 
251
        return null;
 
252
    }
 
253
 
 
254
    /**
 
255
     * @see GalleryStorage::_getOptimizeStatement
 
256
     */
 
257
    function _getOptimizeStatement() {
 
258
        return 'VACUUM ANALYZE VERBOSE %s';
 
259
    }
 
260
 
 
261
    /**
 
262
     * @see GalleryStorage::encodeBlob
 
263
     *
 
264
     * Note: pg_escape_bytea is only available in PHP 4.2.0+ and is even a little
 
265
     * slower than addcslashes. 
 
266
     */
 
267
    function encodeBlob($blob) {
 
268
        /* See: http://www.postgresql.org/docs/8.1/interactive/datatype-binary.html */
 
269
        return addcslashes($blob, "\000..\037\047\134\177..\377");
 
270
    }
 
271
 
 
272
    /**
 
273
     * @see GalleryStorage::decodeBlob
 
274
     */
 
275
    function decodeBlob($blob) {
 
276
        return stripcslashes($blob);
 
277
    }
 
278
}
 
279
?>
 
 
b'\\ No newline at end of file'