You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Database.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace SCF\Core;
  3. use PDO;
  4. use PDOException;
  5. class Database {
  6. /**
  7. * @var null|PDO
  8. */
  9. private $db = NULL;
  10. /**
  11. * @var
  12. */
  13. private $stmt;
  14. /**
  15. * @var string
  16. */
  17. private $error;
  18. /**
  19. * @var int
  20. */
  21. private $queryCounter = 0;
  22. /**
  23. * @var array
  24. */
  25. private $sqlQueries = [];
  26. /**
  27. * Database constructor.
  28. * @param $dbext
  29. * @param $dbhost
  30. * @param $dbuser
  31. * @param $dbpass
  32. * @param $dbname
  33. */
  34. public function __construct($dbext, $dbhost, $dbuser, $dbpass, $dbname)
  35. {
  36. $dsn = $dbext.':dbname='.$dbname.';host='.$dbhost.';charset=utf8';
  37. $options = [
  38. PDO::ATTR_PERSISTENT => true,
  39. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  40. ];
  41. try {
  42. $this->db = new PDO($dsn, $dbuser, $dbpass, $options);
  43. }
  44. catch (PDOException $e) {
  45. $this->error = $e->getMessage();
  46. }
  47. }
  48. /**
  49. * @param $query
  50. */
  51. public function query($query)
  52. {
  53. $this->stmt = $this->db->prepare($query);
  54. $this->queryCounter++;
  55. $this->setSqlQueries($query);
  56. }
  57. /**
  58. * @param $param
  59. * @param $value
  60. * @param null $type
  61. */
  62. public function bind($param, $value, $type = null)
  63. {
  64. if(is_null($type)) {
  65. switch(true) {
  66. case is_int($value):
  67. $type = PDO::PARAM_INT;
  68. break;
  69. case is_bool($value):
  70. $type = PDO::PARAM_BOOL;
  71. break;
  72. case is_null($value):
  73. $type = PDO::PARAM_NULL;
  74. break;
  75. default:
  76. $type = PDO::PARAM_STR;
  77. }
  78. }
  79. $this->stmt->bindValue($param,$value,$type);
  80. }
  81. /**
  82. * @return mixed
  83. */
  84. public function execute()
  85. {
  86. try {
  87. return $this->stmt->execute();
  88. }
  89. catch (PDOException $e) {
  90. $this->error = $e->getMessage();
  91. }
  92. }
  93. /**
  94. * @return mixed
  95. */
  96. public function fetchArray()
  97. {
  98. $this->execute();
  99. return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
  100. }
  101. /**
  102. * @return mixed
  103. */
  104. public function single()
  105. {
  106. $this->execute();
  107. return $this->stmt->fetch(PDO::FETCH_ASSOC);
  108. }
  109. /**
  110. * @return mixed
  111. */
  112. public function rowCount()
  113. {
  114. return $this->stmt->rowCount();
  115. }
  116. /**
  117. * @return mixed
  118. */
  119. public function lastInsertId()
  120. {
  121. return $this->stmt->lastInsertId();
  122. }
  123. /**
  124. * @return string
  125. */
  126. public function getError()
  127. {
  128. return $this->error;
  129. }
  130. /**
  131. * @return int
  132. */
  133. public function getQueryCounter()
  134. {
  135. return $this->queryCounter;
  136. }
  137. /**
  138. * @param array $sqlQueries
  139. */
  140. public function setSqlQueries($sqlQueries)
  141. {
  142. $this->sqlQueries[] = $sqlQueries;
  143. }
  144. /**
  145. * @return array
  146. */
  147. public function getSqlQueries()
  148. {
  149. return $this->sqlQueries;
  150. }
  151. }