db.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * @author ThemePunch <info@themepunch.com>
  4. * @link http://www.themepunch.com/
  5. * @copyright 2015 ThemePunch
  6. */
  7. if( !defined( 'ABSPATH') ) exit();
  8. class RevSliderDB{
  9. private $lastRowID;
  10. /**
  11. *
  12. * constructor - set database object
  13. */
  14. public function __construct(){
  15. }
  16. /**
  17. *
  18. * throw error
  19. */
  20. private function throwError($message,$code=-1){
  21. RevSliderFunctions::throwError($message,$code);
  22. }
  23. //------------------------------------------------------------
  24. // validate for errors
  25. private function checkForErrors($prefix = ""){
  26. global $wpdb;
  27. if($wpdb->last_error !== ''){
  28. $query = $wpdb->last_query;
  29. $message = $wpdb->last_error;
  30. if($prefix) $message = $prefix.' - <b>'.$message.'</b>';
  31. if($query) $message .= '<br>---<br> Query: ' . esc_attr($query);
  32. $this->throwError($message);
  33. }
  34. }
  35. /**
  36. *
  37. * insert variables to some table
  38. */
  39. public function insert($table,$arrItems){
  40. global $wpdb;
  41. $wpdb->insert($table, $arrItems);
  42. $this->checkForErrors("Insert query error");
  43. $this->lastRowID = $wpdb->insert_id;
  44. return($this->lastRowID);
  45. }
  46. /**
  47. *
  48. * get last insert id
  49. */
  50. public function getLastInsertID(){
  51. global $wpdb;
  52. $this->lastRowID = $wpdb->insert_id;
  53. return($this->lastRowID);
  54. }
  55. /**
  56. *
  57. * delete rows
  58. */
  59. public function delete($table,$where){
  60. global $wpdb;
  61. RevSliderFunctions::validateNotEmpty($table,"table name");
  62. RevSliderFunctions::validateNotEmpty($where,"where");
  63. $query = "delete from $table where $where";
  64. $wpdb->query($query);
  65. $this->checkForErrors("Delete query error");
  66. }
  67. /**
  68. *
  69. * run some sql query
  70. */
  71. public function runSql($query){
  72. global $wpdb;
  73. $wpdb->query($query);
  74. $this->checkForErrors("Regular query error");
  75. }
  76. /**
  77. *
  78. * run some sql query
  79. */
  80. public function runSqlR($query){
  81. global $wpdb;
  82. $return = $wpdb->get_results($query, ARRAY_A);
  83. return $return;
  84. }
  85. /**
  86. *
  87. * insert variables to some table
  88. */
  89. public function update($table,$arrItems,$where){
  90. global $wpdb;
  91. $response = $wpdb->update($table, $arrItems, $where);
  92. return($wpdb->num_rows);
  93. }
  94. /**
  95. *
  96. * get data array from the database
  97. *
  98. */
  99. public function fetch($tableName,$where="",$orderField="",$groupByField="",$sqlAddon=""){
  100. global $wpdb;
  101. $query = "select * from $tableName";
  102. if($where) $query .= " where $where";
  103. if($orderField) $query .= " order by $orderField";
  104. if($groupByField) $query .= " group by $groupByField";
  105. if($sqlAddon) $query .= " ".$sqlAddon;
  106. $response = $wpdb->get_results($query,ARRAY_A);
  107. $this->checkForErrors("fetch");
  108. return($response);
  109. }
  110. /**
  111. *
  112. * fetch only one item. if not found - throw error
  113. */
  114. public function fetchSingle($tableName,$where="",$orderField="",$groupByField="",$sqlAddon=""){
  115. $response = $this->fetch($tableName, $where, $orderField, $groupByField, $sqlAddon);
  116. if(empty($response))
  117. $this->throwError("Record not found");
  118. $record = $response[0];
  119. return($record);
  120. }
  121. /**
  122. * prepare statement to avoid sql injections
  123. */
  124. public function prepare($query, $array){
  125. global $wpdb;
  126. $query = $wpdb->prepare($query, $array);
  127. return($query);
  128. }
  129. }
  130. /**
  131. * old classname extends new one (old classnames will be obsolete soon)
  132. * @since: 5.0
  133. **/
  134. class UniteDBRev extends RevSliderDB {}
  135. ?>