<?php
/**
 * @copyright NetMonsters <team@netmonsters.ru>
 * @link http://netmonsters.ru
 * @package Majestic
 * @subpackage db
 * @since 2010-02-16
 * @version SVN: $Id$
 * @filesource $URL$
 */

abstract class DbDriver
{
    
    protected $identifier_quote = '`';
    
    /**
     * Database connection
     * 
     * @var object
     */
    protected $connection = null;
    
    /**
     * Configuration data
     * 
     * @var array
     */
    protected $config = array();
    
    
    public function __construct($config)
    {
        $this->checkConfig($config);
        $this->config = $config;
    }
    
    protected function checkConfig($config)
    {
        $required = array('database', 'username', 'password', 'hostname');
        foreach ($required as $option) {
            if (!isset($config[$option])) {
                throw new Exception('Configuration must have a "' . $option . '".');
            }
        }
    }
    
    public function getConnection()
    {
        $this->connect();
        return $this->connection;
    }
    
    public function beginTransaction()
    {
        $this->connect();
        $this->driverBeginTransaction();
        return $this;
    }
    
    public function commit()
    {
        $this->connect();
        $this->driverCommitTransaction();
        return $this;
    }
    
    public function rollback()
    {
        $this->connect();
        $this->driverRollbackTransaction();
        return $this;
    }
    
    /**
     * @param string $sql
     * @param mixed $params
     * @return DbStatement
     */
    public function query($sql, $params = array())
    {
        $this->connect();
        if (!is_array($params)) {
            $params = array($params);
        }
        $stmt = $this->prepare($sql);
        $stmt->execute($params);
        return $stmt;
    }
    
    /**
     * @param string $table
     * @param mixed $bind
     * @param mixed $on_duplicate
     * @return int Affected rows count
     */
    public function insert($table, $bind, $on_duplicate = array())
    {
        $columns = array();
        foreach ($bind as $col => $val) {
            $columns[] = $this->quoteIdentifier($col);
        }
        $values = array_values($bind);
        
        $sql = 'INSERT INTO ' . $this->quoteIdentifier($table)
             . ' (' . implode(', ', $columns) . ') VALUES (' . $this->quote($values) . ')';
        return $this->query($sql)->affectedRows();
    }
    
    /**
     * @param string $table
     * @param array $bind
     * @param mixed $where
     * @return int
     */
    public function update($table, $bind, $where = '')
    {
        $set = array();
        foreach ($bind as $col => $val) {
            $set[] = $this->quoteIdentifier($col) . '=' . $this->quote($val);
        }
        $where = $this->whereExpr($where);
        $sql = 'UPDATE ' . $this->quoteIdentifier($table) . ' SET ' . implode(', ', $set)
             . (($where) ? (' WHERE ' . $where) : '');
        return $this->query($sql)->affectedRows();
    }
    
    /**
     * @param string $table
     * @param mixed $where
     * @return int
     */
    public function delete($table, $where = '')
    {
        $where = $this->whereExpr($where);
        $sql = 'DELETE FROM ' . $this->quoteIdentifier($table) . (($where) ? (' WHERE ' . $where) : '');
        return $this->query($sql)->affectedRows();
    }
    
    /**
     * @param mixed $value
     * @return string
     */
    public function quote($value)
    {
        if ($value instanceof DbExpr) {
            return (string) $value;
        }
        if (is_array($value)) {
             foreach ($value as &$val) {
                 $val = $this->quote($val);
             }
             return implode(', ', $value);
        }
        return $this->driverQuote($value);
    }
    
    /**
     * @param string $ident
     * @return string
     */
    public function quoteIdentifier($ident)
    {
        $ident = explode('.', $ident);
        if (!is_array($ident)) {
            $ident = array($ident);
        }
        foreach ($ident as &$segment) {
            $segment = $this->identifier_quote . $segment . $this->identifier_quote;
        }
        return implode('.', $ident);
    }
    
    public function quoteInto($text, $value)
    {
        $pos = mb_strpos($text, '?');
        if ($pos === false) {
            return $text;
        }
        return mb_substr($text, 0, $pos) . $this->quote($value) . mb_substr($text, $pos + 1);
    }
    
    /**
     * @param mixed $where
     * @return string
     */
    protected function whereExpr($where)
    {
        if (empty($where)) {
            return $where;
        }
        
        if (!is_array($where)) {
            $where = array($where);
        }
        foreach ($where as $cond => &$term) {
            if (is_int($cond)) {
                if ($term instanceof DbExpr) {
                    $term = (string) $term;
                }
            } else {
                $term = $this->quoteInto($cond, $term);
            }
        }
        return implode(' AND ', $where);
    }
    
    /* Abstract methods  */
    
    /**
     * @return DbStatement
     */
    abstract public function prepare($sql);
    
    abstract public function getInsertId($table = null, $key = null);
    
    abstract public function isConnected();
    
    abstract public function disconnect();
    
    abstract protected function connect();
    
    abstract protected function driverQuote($value);
    
    abstract protected function driverBeginTransaction();
    
    abstract protected function driverCommitTransaction();
    
    abstract protected function driverRollbackTransaction();
}