<?php
namespace Qii\Driver;

/**
 * 数据库连接基类
 *
 * @author Jinhui Zhu<jinhui.zhu@live.cn> 2015-12-10 14:34
 */
class ConnBase
{
	const VERSION = '1.2';
	/**
	 * @var array $allowExec  读写对应的数据库配置文件
	 */
	public $allowExec = array("WRITE" => 'master', 'READ' => 'slave');

    /**
     * @var array $_dbInfo 数据库配置
     */
    protected $_dbInfo;

    /**
     * @var resource $_writeConnection  获取写的链接
     */
    protected static $_writeConnection = null;
    /**
     * @var resource$_readConnection 获取读的链接
     */
    protected static $_readConnection = null;

	/**
	 * 获取数据库配置中指定的key的值，不指定则获取全部
	 * @param string key 数据库配置中指定的key
	 */
	public function getDBInfo($key = null)
	{
		if ($key != null) return isset($this->_dbInfo[$key]) ? $this->_dbInfo[$key] : false;
		return $this->_dbInfo;
	}

	/**
	 * 通过sql语句判断是读还是写操作
	 * @param $sql  读/写的sql语句
	 */
	protected function prepare($sql)
	{
		$default = "READ";
		//$readMode = "/^SELECT\s/u";
		$writeMode = "/^(UPDATE)|(REPLACE)|(DELETE)|(INSERT)\s/u";
		//$isRead = preg_match($readMode, $sql);
		$isWrite = preg_match($writeMode, $sql);
		if ($isWrite) $default = "WRITE";
		if (!isset($this->allowExec[$default])) $default = 'WRITE';
		return $default;
	}

	/**
	 * 通过sql获取连接资源
	 *
	 * @param String $sql 通过sql语句获取读/写操作对应的res
	 * @return res
	 */
	public function getConnectionBySQL($sql)
	{
		$default = $this->prepare($sql);
		if (isset($this->_connections[$default])) return $this->_connections[$default];
		switch ($default) {
			case 'READ':
				$connection = $this->_connections['READ'] = $this->getReadConnection();
				break;
			default:
                $connection = $this->_connections['WRITE'] = $this->getWriteConnection();
				break;
		}

        if(get_called_class() == 'Qii\Driver\Pdo\Connection'){
            $connection->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
            $connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
            $connection->query('set names utf8');
        }
        return $connection;
	}

    /**
     * 获取DBInfo 和 Salve
     *
     * @return array
     */
    protected function getDBInfoAndSalve() {
        $dbInfo = $this->_dbInfo['master'];
        $useSlave = false;

        if ($this->_dbInfo['readOrWriteSeparation'] && $this->_dbInfo['slave']) {
            $i = rand(0, count($this->_dbInfo['slave']) - 1);
            $dbInfo = $this->_dbInfo['slave'][$i];
            $useSlave = true;
        }
        return array($dbInfo, $useSlave);
    }
}