<?php
/*
 * $RCSfile: MySqlDatabaseStorage.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.25 $ $Date: 2005/08/23 03:49:04 $
 * @package GalleryCore
 * @author Bharat Mediratta <bharat@menalto.com>
 */

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

    /**
     * The type of mysql database (mysql or mysqlt)
     *
     * @var string $_mysqlType
     * @access private
     */
    var $_mysqlType;

    /**
     * Constructor
     */
    function MySqlDatabaseStorage($config) {
	$this->DatabaseStorage($config);
	$this->_mysqlType = $config['type'];
	if ($this->_mysqlType == 'mysqlt') {
	    $this->_isTransactional = true;
	}
    }

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

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

    /**
     * @see DatabaseStorage::getSqlReplacements
     */
    function getSqlReplacements() {

	/* Use InnoDB for transactional tables */
	if ($this->_isTransactional) {
	    $tableType = 'InnoDB';
	} else {
	    $tableType = 'MyISAM';
	}

	return array('DB_TABLE_TYPE' => $tableType);
    }

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

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

	case 'UNIX_TIMESTAMP':
	    $sql = sprintf('UNIX_TIMESTAMP(%s)', $args[0]);
	    break;

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

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

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

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

	case 'CASE': // condition value (condition value)* else-value
	    if (count($args) == 3) {
		$sql = sprintf('IF(%s)', implode(', ', $args));
	    } else {
		$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);
    }

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

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

    /**
     * Get database version.
     * @return string version
     */
    function getVersion() {
	return mysql_get_server_info();
    }

    /**
     * @see DatabaseStorage::getOptimizeStatement
     */
    function getOptimizeStatement() {
	return 'OPTIMIZE TABLE %s';
    }
}
?>
