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