<?php
/*
 * $RCSfile: OracleDatabaseStorage.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.14 $ $Date: 2005/08/23 03:49:04 $
 * @package GalleryCore
 * @author Alan Harder <alan.harder@sun.com>
 */

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

    /**
     * The type of Oracle database (oci8, oci8po, oci805 or oracle)
     *
     * @var string $_oracleDbType
     * @access private
     */
    var $_oracleDbType;

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

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

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

    /**
     * @see GalleryStorage::convertIntToBits
     */
    function convertIntToBits($intVal) {
	return $intVal;
    }

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

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

	case 'BITAND':
	    $sql = 'BITAND(' . $args[0] . ',' . $args[1] . ')';
	    break;

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

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

	case 'RAND':
	    if (!empty($args)) {
		return array(GalleryStatus::error(ERROR_UNIMPLEMENTED, __FILE__, __LINE__,
						  $functionName . ' ' . implode(' ', $args)), null);
	    }
	    $sql = 'DBMS_RANDOM.RANDOM';
	    break;

	case 'LIMIT':
	    $sql = 'SELECT * FROM (' . $args[1] . ') WHERE ROWNUM <= ' . $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(ERROR_UNIMPLEMENTED, __FILE__, __LINE__,
					      $functionName . ' ' . implode(' ', $args)), null);
	}

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

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

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

	$this->_traceStart();
	$recordSet1 = $tmpDb->Execute('DROP FUNCTION BIT_OR');
	$recordSet2 = $tmpDb->Execute('DROP TYPE BIT_OR_IMPL');
	$tmpDb->Close();
	$this->_traceStop();

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

	return GalleryStatus::success();
    }

    /**
     * @see DatabaseStorage::configureStore
     */
    function configureStore($moduleId) {
	if ($moduleId == 'core') {
	    /* Install our custom aggregate function */
	    $ret = $this->_executeSqlFile(
		dirname(__FILE__) . '/schema/platform/oracle/CreateBitOr_sql');

	    /*
	     * 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();
     }

    /**
     * Get database version.
     * @return string version
     */
    function getVersion() {
	if (function_exists('oci_server_version')) {
	    return oci_server_version();
	}
	return ociserverversion();
    }
}
?>
