Guest User

Untitled

a guest
Nov 22nd, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. <?php
  2.  
  3. class PdoAdapter implements DatabaseAdapterInterface
  4. {
  5. protected $config = array();
  6. protected $connection;
  7. protected $statement;
  8. protected $fetchMode = \PDO::FETCH_ASSOC;
  9.  
  10. public function __construct($dsn, $username = null,
  11. $password = null, array $driverOptions = array()) {
  12. $this->config = compact("dsn", "username", "password",
  13. "driverOptions");
  14. }
  15.  
  16. public function getStatement() {
  17. if ($this->statement === null) {
  18. throw new \PDOException(
  19. "There is no PDOStatement object for use.");
  20. }
  21. return $this->statement;
  22. }
  23.  
  24. public function connect() {
  25. // if there is a PDO object already, return early
  26. if ($this->connection) {
  27. return;
  28. }
  29.  
  30. try {
  31. $this->connection = new \PDO(
  32. $this->config["dsn"],
  33. $this->config["username"],
  34. $this->config["password"],
  35. $this->config["driverOptions"]);
  36. $this->connection->setAttribute(\PDO::ATTR_ERRMODE,
  37. \PDO::ERRMODE_EXCEPTION);
  38. $this->connection->setAttribute(
  39. \PDO::ATTR_EMULATE_PREPARES, false);
  40. }
  41. catch (\PDOException $e) {
  42. throw new \RunTimeException($e->getMessage());
  43. }
  44. }
  45.  
  46. public function disconnect() {
  47. $this->connection = null;
  48. }
  49.  
  50. public function prepare($sql, array $options = array() {
  51. $this->connect();
  52. try {
  53. $this->statement = $this->connection->prepare($sql,
  54. $options);
  55. return $this;
  56. }
  57. catch (\PDOException $e) {
  58. throw new \RunTimeException($e->getMessage());
  59. }
  60. }
  61.  
  62. public function execute(array $parameters = array()) {
  63. try {
  64. $this->getStatement()->execute($parameters);
  65. return $this;
  66. }
  67. catch (\PDOException $e) {
  68. throw new \RunTimeException($e->getMessage());
  69. }
  70. }
  71.  
  72. public function countAffectedRows() {
  73. try {
  74. return $this->getStatement()->rowCount();
  75. }
  76. catch (\PDOException $e) {
  77. throw new \RunTimeException($e->getMessage());
  78. }
  79. }
  80.  
  81. public function getLastInsertId($name = null) {
  82. $this->connect();
  83. return $this->connection->lastInsertId($name);
  84. }
  85.  
  86. public function fetch($fetchStyle = null,
  87. $cursorOrientation = null, $cursorOffset = null) {
  88. if ($fetchStyle === null) {
  89. $fetchStyle = $this->fetchMode;
  90. }
  91.  
  92. try {
  93. return $this->getStatement()->fetch($fetchStyle,
  94. $cursorOrientation, $cursorOffset);
  95. }
  96. catch (\PDOException $e) {
  97. throw new \RunTimeException($e->getMessage());
  98. }
  99. }
  100.  
  101. public function fetchAll($fetchStyle = null, $column = 0) {
  102. if ($fetchStyle === null) {
  103. $fetchStyle = $this->fetchMode;
  104. }
  105.  
  106. try {
  107. return $fetchStyle === \PDO::FETCH_COLUMN
  108. ? $this->getStatement()->fetchAll($fetchStyle, $column)
  109. : $this->getStatement()->fetchAll($fetchStyle);
  110. }
  111. catch (\PDOException $e) {
  112. throw new \RunTimeException($e->getMessage());
  113. }
  114. }
  115.  
  116. public function select($table, array $bind = array(),
  117. $boolOperator = "AND") {
  118. if ($bind) {
  119. $where = array();
  120. foreach ($bind as $col => $value) {
  121. unset($bind[$col]);
  122. $bind[":" . $col] = $value;
  123. $where[] = $col . " = :" . $col;
  124. }
  125. }
  126.  
  127. $sql = "SELECT * FROM " . $table
  128. . (($bind) ? " WHERE "
  129. . implode(" " . $boolOperator . " ", $where) : " ");
  130. $this->prepare($sql)
  131. ->execute($bind);
  132. return $this;
  133. }
  134.  
  135. public function insert($table, array $bind) {
  136. $cols = implode(", ", array_keys($bind));
  137. $values = implode(", :", array_keys($bind));
  138. foreach ($bind as $col => $value) {
  139. unset($bind[$col]);
  140. $bind[":" . $col] = $value;
  141. }
  142.  
  143. $sql = "INSERT INTO " . $table
  144. . " (" . $cols . ") VALUES (:" . $values . ")";
  145. return (int) $this->prepare($sql)
  146. ->execute($bind)
  147. ->getLastInsertId();
  148. }
  149.  
  150. public function update($table, array $bind, $where = "") {
  151. $set = array();
  152. foreach ($bind as $col => $value) {
  153. unset($bind[$col]);
  154. $bind[":" . $col] = $value;
  155. $set[] = $col . " = :" . $col;
  156. }
  157.  
  158. $sql = "UPDATE " . $table . " SET " . implode(", ", $set)
  159. . (($where) ? " WHERE " . $where : " ");
  160. return $this->prepare($sql)
  161. ->execute($bind)
  162. ->countAffectedRows();
  163. }
  164.  
  165. public function delete($table, $where = "") {
  166. $sql = "DELETE FROM " . $table . (($where) ? " WHERE " . $where : " ");
  167. return $this->prepare($sql)
  168. ->execute()
  169. ->countAffectedRows();
  170. }
  171. }
Add Comment
Please, Sign In to add comment