Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- # /Framework/ORM/Adapter.php
- namespace Framework\ORM;
- interface Adapter
- {
- /**
- * @param $hostname string
- * @param $source string
- * @param $username string
- * @param $password string
- * @param $options array
- */
- public function __construct($hostname, $source = null, $username = null, $password = null, array $options = array());
- /**
- * @param $source string
- * @param $data array
- */
- public function create($source, array $data);
- /**
- * @param $source string
- * @param $where array
- */
- public function delete($source, array $where);
- /**
- * @param $string string
- * @return string
- */
- public function escape($string);
- /**
- * @param $source string
- * @param $where array
- * @param $offset array
- */
- public function read($source, array $fields = array(), array $where = array());
- /**
- * @param $source string
- * @param $data array
- * @param $where array
- * @param $offset array
- */
- public function update($source, array $data, array $where = array());
- }
- # -----
- # /Framework/ORM/Adapter/PDO.php
- # -----
- namespace Framework\ORM;
- abstract PDO implements Adapter
- {
- /**
- * @var \PDO
- */
- private $_connection;
- /**
- * @var string
- */
- private $_hostname;
- /**
- * @var array
- */
- private $_options;
- /**
- * @var string
- */
- private $_password;
- /**
- * @var string
- */
- private $_source;
- /**
- * @var string
- */
- private $_username;
- /**
- * @return string
- */
- abstract protected function _getDsn();
- /**
- * @param $hostname string
- * @param $source string
- * @param $username string
- * @param $password string
- * @param $options array
- */
- public function __construct($hostname, $source = null, $username = null, $password = null, array $options = array())
- {
- $this->setHostname($hostname);
- $this->setSource($source);
- $this->setUsername($username);
- $this->setPassword($password);
- $this->setOptions($options);
- try {
- $connection = new \PDO($this->getDsn(), $this->getUsername(), $this->getPassword(), $this->getOptions();
- $this->setConnection($connection);
- $this->getConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- } catch (Exception $e) {
- throw new Exception($e);
- }
- }
- /**
- * @param $source string
- * @param $data array
- */
- public function create($source, array $data)
- {
- $keys = array_keys($data);
- $binds = array_values($data);
- $placeholders = array_map(function() { return '?'; }, $binds);
- $sql = "INSERT INTO " . $source . " (" . join(", ", $keys) . ") VALUES (" . join(", ", $placeholders) . ")";
- $statement = $this->getConnection()->prepare($sql);
- $statement->execute($binds);
- }
- /**
- * @param $source string
- * @param $where array
- * @return bool
- * @todo complex conditions
- * @todo offset
- */
- public function delete($source, array $where)
- {
- $binds = array();
- $wheres = array();
- foreach ($where as $field => $value) {
- $wheres[] = $field . ' = ?';
- $binds[] = $value;
- }
- $sql = "DELETE FROM " . $source . " WHERE " . join(" AND ", $wheres);
- $statement = $this->getConnection()->prepare($sql);
- return $statement->execute($binds);
- }
- /**
- * @param $string string
- * @return string
- */
- public function escape($string)
- {
- return $this->getConnection()->quote($string);
- }
- /**
- * @return \PDO
- */
- public function getConnection()
- {
- return $this->_connection;
- }
- /**
- * @param $source string
- * @param $fields array
- * @param $where array
- * @todo complex conditions
- * @todo offset
- */
- public function read($source, array $fields = array(), array $where = array())
- {
- $binds = array();
- if (!$fields) {
- $fields[] = '*'
- }
- $wheres = array();
- foreach ($where as $field => $value) {
- $wheres[] = $field . ' = ?';
- $binds[] = $value;
- }
- $sql = "SELECT " . join(", ", $fields) . " FROM " . $source;
- if ($wheres) {
- $sql .= " WHERE " . join(" AND ", $wheres);
- }
- $statement = $this->getConnection()->prepare($sql);
- $statement->execute($binds);
- }
- /**
- * @param $connection \PDO
- * @return self
- */
- public function setConnection(\PDO $connection)
- {
- $this->_connection = $connection;
- return $this;
- }
- /**
- * @param $source string
- * @param $data array
- * @param $where array
- * @param $offset array
- * @return bool
- * @todo complex conditions
- * @todo offset
- */
- public function update($source, array $data, array $where = array())
- {
- $binds = array();
- $sets = array();
- foreach ($data as $field => $value) {
- $sets[] = $field . ' = ?';
- $binds[] = $value;
- }
- $wheres = array();
- foreach ($where as $field => $value) {
- $wheres[] = $field . ' = ?';
- $binds[] = $value;
- }
- $sql = "UPDATE " . $source . " SET " . join(", ", $sets);
- if ($wheres) {
- $sql .= " WHERE " . join(" AND ", $wheres);
- }
- $statement = $this->getConnection()->prepare($sql);
- return $statement->execute($binds);
- }
- }
- # -----
- # /Framework/ORM/Adapter/MySQL.php
- # -----
- namespace Framework\ORM;
- class MySQL extends PDO
- {
- public function getDsn()
- {
- $dsn = 'mysql:host=' . $this->getHostname() . ';dbname=' . $this->getDatabase();
- return $dsn;
- }
- }
Add Comment
Please, Sign In to add comment