<?php 
/**
* @version		$Id: adapter.class.php 1138 2012-05-18 17:13:22Z datahell $
* @package		Elxis
* @subpackage	Database
* @copyright	Copyright (c) 2006-2012 Elxis CMS (http://www.elxis.org). All rights reserved.
* @license		Elxis Public License ( http://www.elxis.org/elxis-public-license.html )
* @author		Elxis Team ( http://www.elxis.org )
* @description 	Elxis CMS is free software. Read the license for copyright notices and details
*/

defined('_ELXIS_') or die ('Direct access to this location is not allowed.');


abstract class elxisDbAdapter {

	protected $pdo = null;
	protected $quote_indentifier = '';


	/*************/
	/* CONSTRUCT */
	/*************/
	public function __construct($pdo=null) {
		$this->pdo = $pdo;
	}


	/********************************************************/
	/* APPLY QUOTE IDENTIFIER TO A STRING (i.e. TABLE NAME) */
	/********************************************************/
	public function quoteIdentifier($string) {
		return $this->quote_indentifier.$string.$this->quote_indentifier;
	}


	/**********************************/
	/* CHANGE THE CASE OF THE STRINGS */
	/**********************************/
    public function foldCase($key) {
        switch ($this->pdo->getAttribute(PDO::ATTR_CASE)) {
            case PDO::CASE_LOWER: $value = strtolower((string)$key); break;
            case PDO::CASE_UPPER: $value = strtoupper((string)$key); break;
            case PDO::CASE_NATURAL: default: $value = (string)$key; break;
        }
        return $value;
    }


	/****************************************************/
	/* GET OPTION VALUE (EG. DB NAME) FROM A DSN STRING */
	/****************************************************/
	protected function getFromDSN($dsn, $option) {
		$option = strtolower(trim($option));
		if ($option == '') { return false; }
		if (stripos($dsn, $option) === false) { return false; }
		$parts = preg_split('#\:#', $dsn, 2, PREG_SPLIT_NO_EMPTY);
		$str = (count($parts) == 2) ? $parts[1] : $parts[0];
		$parts2 = explode(';', $str);
		if ($parts2) {
			foreach ($parts2 as $part2) {
				$parts3 = preg_split('#\=#', $part2, 2, PREG_SPLIT_NO_EMPTY);
				if ((strtolower(trim($parts3[0])) == $option) && isset($parts3[1])) {
					return $parts3[1];
				}
			}
		}
		return false;
	}


	abstract public function addLimit($sql, $offset=-1, $limit=-1);


	abstract public function listTables();


	abstract public function backup($params);


	/*****************************************************************/
	/* GET THE LAST ID GENERATED BY AN IDENTITY/AUTOINCREMENT COLUMN */
	/*****************************************************************/
    public function lastInsertId($tableName = null, $primaryKey = null) {
        return $this->pdo->lastInsertId($tableName);
    }

}

?>