Advertisement
Guest User

MasterSlaveAdapter for Phalcon\Db\Adapter\Pdo

a guest
Aug 26th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.64 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Base\Database\Adapter;
  4.  
  5. use Phalcon\Db\Adapter\Pdo;
  6.  
  7. class MasterSlaveAdapter extends Pdo
  8. {
  9.     /**
  10.      * @var Pdo
  11.      */
  12.     protected $masterConnection;
  13.  
  14.     /**
  15.      * @var Pdo
  16.      */
  17.     protected $slaveConnection;
  18.  
  19.     /**
  20.      * @param Pdo $masterConnection
  21.      * @param Pdo $slaveConnection
  22.      */
  23.     public function __construct(
  24.         Pdo $masterConnection,
  25.         Pdo $slaveConnection
  26.     ) {
  27.         $this->masterConnection = $masterConnection;
  28.         $this->slaveConnection = $slaveConnection;
  29.     }
  30.  
  31.     /**
  32.      * @return Pdo
  33.      */
  34.     public function getMasterConnection()
  35.     {
  36.         return $this->masterConnection;
  37.     }
  38.  
  39.     /**
  40.      * @return Pdo
  41.      */
  42.     public function getSlaveConnection()
  43.     {
  44.         return $this->slaveConnection;
  45.     }
  46.  
  47.     /**
  48.      * @param  array|null $descriptor
  49.      * @return bool
  50.      */
  51.     public function connect($descriptor = null)
  52.     {
  53.         $this->getMasterConnection()->connect($descriptor);
  54.         $this->getSlaveConnection()->connect($descriptor);
  55.  
  56.         return true;
  57.     }
  58.  
  59.     /**
  60.      * @param  string $sqlStatement
  61.      * @return ?
  62.      */
  63.     public function prepare($sqlStatement)
  64.     {
  65.         return $this->getMasterConnection()->prepare($sqlStatement);
  66.     }
  67.  
  68.     /**
  69.      * @param  \PDOStatement $statement    [description]
  70.      * @param  array         $placeholders [description]
  71.      * @param  [type]        $dataTypes    [description]
  72.      * @return [type]                      [description]
  73.      */
  74.     public function executePrepared(\PDOStatement $statement, array $placeholders, $dataTypes)
  75.     {
  76.         return $this->getMasterConnection()->executePrepared(
  77.             $statement,
  78.             $placeholders,
  79.             $dataTypes
  80.         );
  81.     }
  82.  
  83.     /**
  84.      * @param  string $sqlStatement
  85.      * @param  array $bindParams
  86.      * @param  array $bindTypes
  87.      * @return bool
  88.      */
  89.     public function execute($sqlStatement, $bindParams = null, $bindTypes = null)
  90.     {
  91.         return $this->getMasterConnection()->execute(
  92.             $sqlStatement,
  93.             $bindParams,
  94.             $bindTypes
  95.         );
  96.     }
  97.  
  98.  
  99.     /**
  100.      * @param  string $sqlStatement
  101.      * @param  array $bindParams
  102.      * @param  array $bindTypes
  103.      * @return bool
  104.      */
  105.     public function query($sqlStatement, $bindParams = null, $bindTypes = null)
  106.     {
  107.         return $this->getSlaveConnection()->query(
  108.             $sqlStatement,
  109.             $bindParams,
  110.             $bindTypes
  111.         );
  112.     }
  113.  
  114.     /**
  115.      * @return int
  116.      */
  117.     public function affectedRows()
  118.     {
  119.         return $this->getMasterConnection()->affectedRows();
  120.     }
  121.  
  122.     /**
  123.      * @return bool
  124.      */
  125.     public function close()
  126.     {
  127.         $this->getMasterConnection()->close();
  128.         $this->getSlaveConnection()->close();
  129.  
  130.         return true;
  131.     }
  132.  
  133.     /**
  134.      * @param  string $string
  135.      * @return string
  136.      */
  137.     public function escapeString($string)
  138.     {
  139.         return $this->getMasterConnection()->escapeString($string);
  140.     }
  141.  
  142.     /**
  143.      * @param  string $sequenceName
  144.      * @return int | bool
  145.      */
  146.     public function lastInsertId($sequenceName = null)
  147.     {
  148.         return $this->getMasterConnection()->lastInsertId($sequenceName);
  149.     }
  150.  
  151.     /**
  152.      * @param  bool $nesting
  153.      * @return bool
  154.      */
  155.     public function begin($nesting = true)
  156.     {
  157.         return $this->getMasterConnection()->begin($nesting);
  158.     }
  159.  
  160.     /**
  161.      * @param  bool $nesting
  162.      * @return bool
  163.      */
  164.     public function rollback($nesting = true)
  165.     {
  166.         return $this->getMasterConnection()->rollback($nesting);
  167.     }
  168.  
  169.     /**
  170.      * @param  bool $nesting
  171.      * @return bool
  172.      */
  173.     public function commit($nesting = true)
  174.     {
  175.         return $this->getMasterConnection()->commit($nesting);
  176.     }
  177.  
  178.     /**
  179.      * @return int
  180.      */
  181.     public function getTransactionLevel()
  182.     {
  183.         return $this->getMasterConnection()->getTransactionLevel();
  184.     }
  185.  
  186.     /**
  187.      * @return boolean
  188.      */
  189.     public function isUnderTransaction()
  190.     {
  191.         return $this->getMasterConnection()->isUnderTransaction();
  192.     }
  193.  
  194.     /**
  195.      * @return \Pdo
  196.      */
  197.     public function getInternalHandler()
  198.     {
  199.         return $this->getMasterConnection()->getInternalHandler();
  200.     }
  201.  
  202.     /**
  203.      * @return array
  204.      */
  205.     public function getErrorInfo()
  206.     {
  207.         return [
  208.             'master' => $this->getMasterConnection()->getErrorInfo(),
  209.             'slave' => $this->getSlaveConnection()->getErrorInfo()
  210.         ];
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement