Guest User

Untitled

a guest
Sep 25th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.23 KB | None | 0 0
  1. <?php
  2.  
  3. # /Framework/ORM/Adapter.php
  4.  
  5. namespace Framework\ORM;
  6.  
  7. interface Adapter
  8. {
  9.  
  10.     /**
  11.      * @param $hostname string
  12.      * @param $source string
  13.      * @param $username string
  14.      * @param $password string
  15.      * @param $options array
  16.      */
  17.     public function __construct($hostname, $source = null, $username = null, $password = null, array $options = array());
  18.  
  19.     /**
  20.      * @param $source string
  21.      * @param $data array
  22.      */
  23.     public function create($source, array $data);
  24.  
  25.     /**
  26.      * @param $source string
  27.      * @param $where array
  28.      */
  29.     public function delete($source, array $where);
  30.  
  31.     /**
  32.      * @param $string string
  33.      * @return string
  34.      */
  35.     public function escape($string);
  36.  
  37.     /**
  38.      * @param $source string
  39.      * @param $where array
  40.      * @param $offset array
  41.      */
  42.     public function read($source, array $fields = array(), array $where = array());
  43.  
  44.     /**
  45.      * @param $source string
  46.      * @param $data array
  47.      * @param $where array
  48.      * @param $offset array
  49.      */
  50.     public function update($source, array $data, array $where = array());
  51.  
  52. }
  53.  
  54.  
  55. # -----
  56. # /Framework/ORM/Adapter/PDO.php
  57. # -----
  58.  
  59. namespace Framework\ORM;
  60.  
  61. abstract PDO implements Adapter
  62. {
  63.  
  64.     /**
  65.      * @var \PDO
  66.      */
  67.     private $_connection;
  68.  
  69.     /**
  70.      * @var string
  71.      */
  72.     private $_hostname;
  73.  
  74.     /**
  75.      * @var array
  76.      */
  77.     private $_options;
  78.  
  79.     /**
  80.      * @var string
  81.      */
  82.     private $_password;
  83.  
  84.     /**
  85.      * @var string
  86.      */
  87.     private $_source;
  88.  
  89.     /**
  90.      * @var string
  91.      */
  92.     private $_username;
  93.  
  94.     /**
  95.      * @return string
  96.      */
  97.     abstract protected function _getDsn();
  98.  
  99.     /**
  100.      * @param $hostname string
  101.      * @param $source string
  102.      * @param $username string
  103.      * @param $password string
  104.      * @param $options array
  105.      */
  106.     public function __construct($hostname, $source = null, $username = null, $password = null, array $options = array())
  107.     {
  108.         $this->setHostname($hostname);
  109.         $this->setSource($source);
  110.         $this->setUsername($username);
  111.         $this->setPassword($password);
  112.         $this->setOptions($options);
  113.         try {
  114.             $connection = new \PDO($this->getDsn(), $this->getUsername(), $this->getPassword(), $this->getOptions();
  115.             $this->setConnection($connection);
  116.             $this->getConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  117.         } catch (Exception $e) {
  118.             throw new Exception($e);
  119.         }
  120.     }
  121.  
  122.     /**
  123.      * @param $source string
  124.      * @param $data array
  125.      */
  126.     public function create($source, array $data)
  127.     {
  128.         $keys = array_keys($data);
  129.         $binds = array_values($data);
  130.         $placeholders = array_map(function() { return '?'; }, $binds);
  131.         $sql = "INSERT INTO " . $source . " (" . join(", ", $keys) . ") VALUES (" . join(", ", $placeholders) . ")";
  132.         $statement = $this->getConnection()->prepare($sql);
  133.         $statement->execute($binds);
  134.     }
  135.  
  136.     /**
  137.      * @param $source string
  138.      * @param $where array
  139.      * @return bool
  140.      * @todo complex conditions
  141.      * @todo offset
  142.      */
  143.     public function delete($source, array $where)
  144.     {
  145.         $binds = array();
  146.         $wheres = array();
  147.         foreach ($where as $field => $value) {
  148.             $wheres[] = $field . ' = ?';
  149.             $binds[] = $value;
  150.         }
  151.         $sql = "DELETE FROM " . $source . " WHERE " . join(" AND ", $wheres);
  152.         $statement = $this->getConnection()->prepare($sql);
  153.         return $statement->execute($binds);
  154.     }
  155.  
  156.     /**
  157.      * @param $string string
  158.      * @return string
  159.      */
  160.     public function escape($string)
  161.     {
  162.         return $this->getConnection()->quote($string);
  163.     }
  164.  
  165.     /**
  166.      * @return \PDO
  167.      */
  168.     public function getConnection()
  169.     {
  170.         return $this->_connection;
  171.     }
  172.  
  173.     /**
  174.      * @param $source string
  175.      * @param $fields array
  176.      * @param $where array
  177.      * @todo complex conditions
  178.      * @todo offset
  179.      */
  180.     public function read($source, array $fields = array(), array $where = array())
  181.     {
  182.         $binds = array();
  183.         if (!$fields) {
  184.             $fields[] = '*'
  185.         }
  186.         $wheres = array();
  187.         foreach ($where as $field => $value) {
  188.             $wheres[] = $field . ' = ?';
  189.             $binds[] = $value;
  190.         }
  191.         $sql = "SELECT " . join(", ", $fields) . " FROM " . $source;
  192.         if ($wheres) {
  193.             $sql .= " WHERE " . join(" AND ", $wheres);
  194.         }
  195.         $statement = $this->getConnection()->prepare($sql);
  196.         $statement->execute($binds);
  197.     }
  198.  
  199.     /**
  200.      * @param $connection \PDO
  201.      * @return self
  202.      */
  203.     public function setConnection(\PDO $connection)
  204.     {
  205.         $this->_connection = $connection;
  206.         return $this;
  207.     }
  208.  
  209.     /**
  210.      * @param $source string
  211.      * @param $data array
  212.      * @param $where array
  213.      * @param $offset array
  214.      * @return bool
  215.      * @todo complex conditions
  216.      * @todo offset
  217.      */
  218.     public function update($source, array $data, array $where = array())
  219.     {
  220.         $binds = array();
  221.         $sets = array();
  222.         foreach ($data as $field => $value) {
  223.             $sets[] = $field . ' = ?';
  224.             $binds[] = $value;
  225.         }
  226.         $wheres = array();
  227.         foreach ($where as $field => $value) {
  228.             $wheres[] = $field . ' = ?';
  229.             $binds[] = $value;
  230.         }
  231.         $sql = "UPDATE " . $source . " SET " . join(", ", $sets);
  232.         if ($wheres) {
  233.             $sql .= " WHERE " . join(" AND ", $wheres);
  234.         }
  235.         $statement = $this->getConnection()->prepare($sql);
  236.         return $statement->execute($binds);
  237.     }
  238.  
  239. }
  240.  
  241.  
  242. # -----
  243. # /Framework/ORM/Adapter/MySQL.php
  244. # -----
  245.  
  246. namespace Framework\ORM;
  247.  
  248. class MySQL extends PDO
  249. {
  250.  
  251.         public function getDsn()
  252.         {
  253.             $dsn = 'mysql:host=' . $this->getHostname() . ';dbname=' . $this->getDatabase();
  254.             return $dsn;
  255.         }
  256.  
  257. }
Add Comment
Please, Sign In to add comment