<?php
/*
 * $RCSfile: PostgreSqlDatabaseStorage.class,v $
 *
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2005 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */
/**
 * @version $Revision: 1.22 $ $Date: 2005/08/23 03:49:04 $
 * @package GalleryCore
 * @author Bharat Mediratta <bharat@menalto.com>
 */

/**
 * PostgreSQL extension of the DatabaseStorage class.
 *
 * This object implements the hooks for saving and restoring objects in a
 * PostgreSQL database.
 *
 * @package GalleryCore
 * @subpackage Storage
 */
class PostgreSqlDatabaseStorage extends DatabaseStorage {

    /**
     * The type of postgres database (postgres or postgrest)
     *
     * @var string $_postgresType
     * @access private
     */
    var $_postgresType;

    /**
     * Constructor
     *
     */
    function PostgreSqlDatabaseStorage($config) {
	$this->DatabaseStorage($config);
	$this->_postgresType = $config['type'];
	$this->_isTransactional = true;
    }

    /**
     * Return the type of this database
     *
     * @return string
     */
    function getAdoDbType() {
	return $this->_postgresType;
    }

    /**
     * Return the type of this database
     *
     * @return string
     */
    function getType() {
	return 'postgres';
    }

    /**
     * @see DatabaseStorage::cleanStore
     */
    function cleanStore() {
	$ret = parent::cleanStore();
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	/*
	 * Create a temporary database connection and install our custom
	 * aggregate function.
	 */
	list ($ret, $tmpDb) = $this->_getConnection(true);
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	$this->_traceStart();
	$recordSet = $tmpDb->Execute('DROP AGGREGATE BIT_OR(bit)');
	$tmpDb->Close();
	$this->_traceStop();

	if (empty($recordSet)) {
	    return GalleryStatus::error(ERROR_STORAGE_FAILURE, __FILE__, __LINE__);
	}

	return GalleryStatus::success();
    }

    /**
     * @see DatabaseStorage::configureStore 	 
     */ 	 
    function configureStore($moduleId) { 	 
	if ($moduleId == 'core') { 	 
	    $query = '
	    CREATE AGGREGATE BIT_OR
	    (
	      basetype = bit,
	      sfunc = bitor,
	      stype = bit
	    )';

	    /*
	     * Create a temporary database connection and install our custom
	     * aggregate function.
	     */
	    list ($ret, $tmpDb) = $this->_getConnection(true);
	    if ($ret->isError()) {
		return $ret->wrap(__FILE__, __LINE__);
	    }

	    $this->_traceStart();
	    $recordSet = $tmpDb->Execute($query);
	    $tmpDb->Close();
	    $this->_traceStop();

	    /*
	     * Ignore errors here, since we'll get them every time we try to
	     * install the aggregate, which will happen every time we upgrade core
	     * or install in a database with another Gallery already present.
	     *
	     * XXX: At some point figure out a way to detect if the aggregate is
	     * already there before trying to install it again.
	     */
	}

	$ret = parent::configureStore($moduleId); 	 
	if ($ret->isError()) { 	 
	    return $ret->wrap(__FILE__, __LINE__); 	 
	} 	 

	return GalleryStatus::success(); 	 
     } 	 
  	 
    /**
     * @see GalleryStorage::convertIntToBits
     */
    function convertIntToBits($intVal) {
	return sprintf("%032b", $intVal);
    }

    /**
     * @see GalleryStorage::convertIntToBits
     */
    function convertBitsToInt($bitsVal) {
	return bindec($bitsVal);
    }

    /**
     * @see GalleryStorage::getFunctionsSql
     */
    function getFunctionSql($functionName, $args) {
	switch($functionName) {
	case 'CONCAT':
	    $sql = implode(' || ', $args);
	    break;

	case 'BITAND':
	    /* Cast any input values as the 'bit' type */
	    $args = str_replace('?', 'B?', $args);
	    $sql = $args[0] . ' & ' . $args[1];
	    break;

	case 'UNIX_TIMESTAMP':
	    $sql = 'date_part(\'epoch\', ' . $args[0] . ')';
	    break;

	case 'AS':
	    $sql = 'AS';
	    break;

	case 'SUBSTRING':
	    $sql = sprintf('SUBSTRING(%s)', implode(', ', $args));
	    break;

	case 'RAND':
	    $sql = sprintf('RANDOM(%s)', empty($args) ? '' : $args[0]);
	    break;

	case 'LIMIT':
	    $sql = $args[1] . ' LIMIT ' . $args[0];
	    break;

	case 'CASE':
	    $sql = array();
	    while (count($args) > 1) {
		$sql[] = 'WHEN ' . array_shift($args) . ' THEN ' . array_shift($args);
	    }
	    $sql = 'CASE ' . implode(' ', $sql) . ' ELSE ' . $args[0] . ' END';
	    break;

	default:
	    return array(GalleryStatus::ERROR_UNIMPLEMENTED(__FILE__, __LINE__), null);
	}

	return array(GalleryStatus::success(), $sql);
    }

    /**
     * Get database version.
     * @return string version
     */
    function getVersion() {
	if (function_exists('pg_version')) {
	    return implode(' ', pg_version());
	}
	return null;
    }


    /**
     * @see DatabaseStorage::getOptimizeStatement
     */
    function getOptimizeStatement() {
	return 'VACUUM ANALYZE VERBOSE %s';
    }
}
?>
