3
* $RCSfile: PostgreSqlStorage.class,v $
5
* Gallery - a web based photo album viewer and editor
6
* Copyright (C) 2000-2006 Bharat Mediratta
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.
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.
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.
23
* @version $Revision: 1.9 $ $Date: 2006/02/20 19:13:09 $
24
* @package GalleryCore
25
* @author Bharat Mediratta <bharat@menalto.com>
28
GalleryCoreApi::requireOnce('modules/core/classes/GalleryStorage.class', true);
31
* PostgreSQL extension of the GalleryStorage class.
33
* This object implements the hooks for saving and restoring objects in a
34
* PostgreSQL database.
36
* @package GalleryCore
39
class PostgreSqlStorage extends GalleryStorage {
42
* The type of postgres database (postgres or postgrest)
44
* @var string $_postgresType
53
function PostgreSqlStorage($config) {
54
$this->GalleryStorage($config);
55
$this->_postgresType = $config['type'];
56
$this->_isTransactional = true;
60
* Return the type of this database
64
function getAdoDbType() {
65
return $this->_postgresType;
69
* Return the type of this database
78
* @see GalleryStorage::cleanStore
80
function cleanStore() {
81
$ret = parent::cleanStore();
83
return $ret->wrap(__FILE__, __LINE__);
87
* Create a temporary database connection and install our custom
90
list ($ret, $tmpDb) = $this->_getConnection(true);
92
return $ret->wrap(__FILE__, __LINE__);
96
$recordSet = $tmpDb->Execute('DROP AGGREGATE BIT_OR(bit)');
100
if (empty($recordSet)) {
101
return GalleryCoreApi::error(ERROR_STORAGE_FAILURE, __FILE__, __LINE__);
108
* @see GalleryStorage::configureStore
110
function configureStore($moduleId, $upgradeInfo=array()) {
111
if ($moduleId == 'core') {
113
CREATE AGGREGATE BIT_OR
121
* Create a temporary database connection and install our custom
122
* aggregate function.
124
list ($ret, $tmpDb) = $this->_getConnection(true);
126
return $ret->wrap(__FILE__, __LINE__);
129
$this->_traceStart();
130
$recordSet = $tmpDb->Execute($query);
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.
139
* XXX: At some point figure out a way to detect if the aggregate is
140
* already there before trying to install it again.
144
$ret = parent::configureStore($moduleId, $upgradeInfo);
146
return $ret->wrap(__FILE__, __LINE__);
153
* @see GalleryStorage::convertIntToBits
155
function convertIntToBits($intVal) {
156
return sprintf("%032b", $intVal);
160
* @see GalleryStorage::convertIntToBits
162
function convertBitsToInt($bitsVal) {
163
return bindec($bitsVal);
167
* @see GalleryStorage::getFunctionsSql
169
function getFunctionSql($functionName, $args) {
170
switch($functionName) {
172
$sql = implode(' || ', $args);
176
/* Cast any input values as the 'bit' type */
177
$args = str_replace('?', '?', $args);
178
$sql = $args[0] . ' & ' . $args[1];
182
$sql = 'BIT_OR(' . $args[0] . ')';
185
case 'UNIX_TIMESTAMP':
186
$sql = 'date_part(\'epoch\', ' . $args[0] . ')';
194
$sql = sprintf('SUBSTRING(%s)', implode(', ', $args));
198
$sql = sprintf('RANDOM(%s)', empty($args) ? '' : $args[0]);
202
$sql = $args[1] . ' LIMIT ' . $args[0];
207
while (count($args) > 1) {
208
$sql[] = 'WHEN ' . array_shift($args) . ' THEN ' . array_shift($args);
210
$sql = 'CASE ' . implode(' ', $sql) . ' ELSE ' . $args[0] . ' END';
214
$sql = $args[0] . ' LIKE ' . $args[1];
220
* 1 - array of column names
223
$markers = GalleryUtilities::makeMarkers(sizeof($args[1]));
224
$rowList = rtrim(str_repeat('SELECT ' . $markers . ' UNION ALL ', $args[2]),
226
$sql = 'INSERT INTO ' . $args[0] . ' (';
227
$sql .= join(', ', $args[1]);
228
$sql .= ') ' . $rowList;
232
$sql = sprintf('AVG(%s)', $args[0]);
236
return array(GalleryCoreApi::error(ERROR_UNIMPLEMENTED, __FILE__, __LINE__,
237
$functionName . ' ' . implode(' ', $args)), null);
240
return array(null, $sql);
244
* Get database version.
245
* @return string version
247
function getVersion() {
248
if (function_exists('pg_version')) {
249
return implode(' ', pg_version());
255
* @see GalleryStorage::_getOptimizeStatement
257
function _getOptimizeStatement() {
258
return 'VACUUM ANALYZE VERBOSE %s';
262
* @see GalleryStorage::encodeBlob
264
* Note: pg_escape_bytea is only available in PHP 4.2.0+ and is even a little
265
* slower than addcslashes.
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");
273
* @see GalleryStorage::decodeBlob
275
function decodeBlob($blob) {
276
return stripcslashes($blob);
b'\\ No newline at end of file'