Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. <?php
  2. /**
  3. * Database
  4. * Access databases using PDO and MySQL. Supports prepared statements.
  5. *
  6. * @package com.codeante.lib.base
  7. * @author GIDIX
  8. */
  9.  
  10. class Database {
  11. private $db;
  12. public $lastQuery;
  13.  
  14. /**
  15. * This variable determines whether {@see Database} should handle errors
  16. * or pass it on via an exception. If this is set to true, {@see Database} handles all errors.
  17. *
  18. * @var boolean
  19. */
  20. public $handleError = false;
  21.  
  22. /**
  23. * @param string $host MySQL Host
  24. * @param string $username MySQL Username
  25. * @param string $password MySQL Password
  26. * @param string $database MySQL Database
  27. */
  28. public function __construct($host, $username, $password, $database) {
  29. $this->db = new PDO('mysql:host=' . $host . ';dbname=' . $database . ';charset=utf8', $username, $password);
  30. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  31. $this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  32. }
  33.  
  34. public function query($sql, array $args = null) {
  35. try {
  36. if (!is_null($args)) {
  37. $this->lastQuery = $this->db->prepare($sql);
  38. $this->lastQuery->execute($args);
  39. } else {
  40. $this->lastQuery = $this->db->query($sql);
  41. }
  42.  
  43. return $this->lastQuery;
  44. } catch (PDOException $e) {
  45. if ($this->handleError) {
  46. $this->error($e->getMessage(), $sql, $e->getCode());
  47. } else {
  48. throw $e;
  49. }
  50. }
  51. }
  52.  
  53. /**
  54. * Prepare a statement and return it without executing.
  55. *
  56. * @param string $query Query to send.
  57. */
  58. public function prepare($sql) {
  59. try {
  60. return $this->db->prepare($sql);
  61. } catch (PDOException $e) {
  62. if ($this->handleError) {
  63. $this->error($e->getMessage(), $sql, $e->getCode());
  64. } else {
  65. throw $e;
  66. }
  67. }
  68. }
  69.  
  70. /**
  71. * Fetch an object using its {@see PDOStatement} $stmt or the last query.
  72. *
  73. * @param PDOStatement $stmt (optional, null)
  74. *
  75. * @return stdClass
  76. */
  77. public function fetchObject(PDOStatement $stmt = null) {
  78. if (!is_null($stmt)) {
  79. return $stmt->fetch(PDO::FETCH_OBJ);
  80. } else {
  81. return $this->lastQuery->fetch(PDO::FETCH_OBJ);
  82. }
  83. }
  84.  
  85. public function fetch(PDOStatement $stmt = null, $fetch_style = FETCH_ASSOC) {
  86. if (!is_null($stmt)) {
  87. return $stmt->fetch(PDO::$fetch_style);
  88. } else {
  89. return $this->lastQuery->fetch(PDO::$fetch_style);
  90. }
  91. }
  92.  
  93. /**
  94. * Get the number of affected rows by $stmt or the last query.
  95. *
  96. * @param PDOStatement $stmt (optional, null)
  97. *
  98. * @return int
  99. */
  100. public function numRows(PDOStatement $stmt = null) {
  101. if (!is_null($stmt)) {
  102. return $stmt->rowCount();
  103. } else {
  104. return $this->lastQuery->rowCount();
  105. }
  106. }
  107.  
  108. /**
  109. * Get the last inserted ID.
  110. *
  111. * @return int
  112. */
  113. public function insertID() {
  114. return $this->db->lastInsertId();
  115. }
  116.  
  117. /**
  118. * Begin a transaction for committing and rollbacks.
  119. */
  120. public function beginTransaction() {
  121. return $this->db->beginTransaction();
  122. }
  123.  
  124. /**
  125. * Commit a transaction.
  126. */
  127. public function commit() {
  128. return $this->db->commit();
  129. }
  130.  
  131. /**
  132. * Rollback recent changes before they were commited.
  133. */
  134. public function rollback() {
  135. return $this->db->rollBack();
  136. }
  137.  
  138. public function errorInfo() {
  139. return $this->db->errorinfo();
  140. }
  141.  
  142. protected function error($msg, $sql, $code) {
  143. Helpers::log(Helpers::LOG_ERROR, $msg . '<br /><br /><code class="important">' . $sql . '</code>');
  144. }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement