<?php

namespace dominion\db;

use \Yii;
use \Exception;
use dominion\amqp\AmqpLoger;
/**
 * Description of Command
 *
 * @author noname
 */
class Command extends \yii\db\Command
{

    public function prepare($forRead = null)
    {
        try
        {
            parent::prepare($forRead);
        }
        catch (Exception $ex)
        {
            if(isset($ex->errorInfo, $ex->errorInfo[1]) && $ex->errorInfo[1] >= 2000 && $ex->errorInfo[1] < 3000)
            {
                AmqpLoger::log('reconnectDB', $ex->errorInfo);
                $this->db->close();
                $this->db->open();
                parent::prepare($forRead);
            }
            else
            {
                throw $ex;
            }
        }
    }

    protected function internalExecute($rawSql)
    {
        try
        {
            parent::internalExecute($rawSql);
        }
        catch (Exception $ex)
        {
            if(isset($ex->errorInfo, $ex->errorInfo[1]) && $ex->errorInfo[1] >= 2000 && $ex->errorInfo[1] < 3000)
            {
                AmqpLoger::log('reconnectDB', $ex->errorInfo);
                $this->db->close();
                $this->db->open();
                $this->pdoStatement = $this->db->pdo->prepare($rawSql);
                parent::internalExecute($rawSql);
            }
            else
            {
                throw $ex;
            }
        }
    }

}