Advertisement
MirazMac

src/framework/PDO/Database.php

Aug 16th, 2022
830
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.09 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @license MIT
  5.  * @license http://opensource.org/licenses/MIT
  6.  */
  7.  
  8. namespace Slim\PDO;
  9.  
  10. use Slim\PDO\Statement\SelectStatement;
  11. use Slim\PDO\Statement\InsertStatement;
  12. use Slim\PDO\Statement\UpdateStatement;
  13. use Slim\PDO\Statement\DeleteStatement;
  14.  
  15. /**
  16.  * Class Database.
  17.  *
  18.  * @author Fabian de Laender <fabian@faapz.nl>
  19.  */
  20. class Database extends \PDO
  21. {
  22.     protected $instance;
  23.  
  24.     /**
  25.      * Constructor.
  26.      *
  27.      * @param $dsn
  28.      * @param null  $usr
  29.      * @param null  $pwd
  30.      * @param array $options
  31.      */
  32.     public function __construct($dsn, $usr = null, $pwd = null, array $options = array())
  33.     {
  34.         $options = $this->getDefaultOptions() + $options;
  35.  
  36.         $this->instance = @parent::__construct($dsn, $usr, $pwd, $options);
  37.     }
  38.  
  39.     public function __destruct()
  40.     {
  41.         $this->instance = null;
  42.     }
  43.  
  44.     /**
  45.      * @return array
  46.      */
  47.     protected function getDefaultOptions()
  48.     {
  49.         return array(
  50.             \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
  51.             \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
  52.             \PDO::ATTR_EMULATE_PREPARES => false,
  53.             \PDO::ATTR_STATEMENT_CLASS => array('Slim\\PDO\\Statement', array($this)),
  54.         );
  55.     }
  56.  
  57.     /**
  58.      * @param array $columns
  59.      *
  60.      * @return SelectStatement
  61.      */
  62.     public function select(array $columns = array('*'))
  63.     {
  64.         return new SelectStatement($this, $columns);
  65.     }
  66.  
  67.     /**
  68.      * @param array $columnsOrPairs
  69.      *
  70.      * @return InsertStatement
  71.      */
  72.     public function insert(array $columnsOrPairs = array())
  73.     {
  74.         return new InsertStatement($this, $columnsOrPairs);
  75.     }
  76.  
  77.     /**
  78.      * @param array $pairs
  79.      *
  80.      * @return UpdateStatement
  81.      */
  82.     public function update(array $pairs = array())
  83.     {
  84.         return new UpdateStatement($this, $pairs);
  85.     }
  86.  
  87.     /**
  88.      * @param null $table
  89.      *
  90.      * @return DeleteStatement
  91.      */
  92.     public function delete($table = null)
  93.     {
  94.         return new DeleteStatement($this, $table);
  95.     }
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement