-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrTable.class.php
More file actions
181 lines (151 loc) · 4.53 KB
/
rTable.class.php
File metadata and controls
181 lines (151 loc) · 4.53 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
class rTableClass
{
var $db = null;
var $table = '';
function __construct($dbLink, $table) {
$this->db = $dbLink;
$this->setTable($table);
}
function setTable($table){ $this->table = $table; }
function getTable(){ return $this->table; }
function get($id, $field = null){
if(is_array($id)){
$w = array();
foreach($id as $n=>$v) $w[] = "`$n` = '".mysqli_real_escape_string($this->db->link, $v)."'";
$w = implode(' AND ', $w);
return $this->db->selectRow('SELECT * FROM ?# WHERE '.$w.' LIMIT 1',
$this->table);
}
if($field !== null){
return $this->db->selectRow('SELECT * FROM ?# WHERE ?# = ? LIMIT 1',
$this->table, $field, $id);
}
return $this->db->selectRow('SELECT * FROM ?# WHERE id = ?',
$this->table, $id);
}
function getCell($field, $where){
if(is_numeric($where)){
$where = 'id = '.$where;
}
return $this->db->selectCell('SELECT ?# FROM ?# WHERE '.$where, $field, $this->table);
}
function put($id, $data, $allowed = null)
{
if($allowed && is_array($allowed)){
$data2 = array();
foreach($allowed as $f) if(isset($data[$f])) $data2[$f] = $data[$f];
$data = $data2;
}
if(is_array($id)){
//if(isset($id[0]) && is_numeric($id[0]))
$this->db->query('UPDATE ?# SET ?a WHERE id IN (?a)', $this->table, $data, $id);
}elseif(is_numeric($id)){
$this->db->query('UPDATE ?# SET ?a WHERE id = ?d', $this->table, $data, $id);
}else{
$this->db->query('UPDATE ?# SET ?a WHERE id = ?', $this->table, $data, $id);
}
return true;
}
function add($data, $allowed = null)
{
if($allowed && is_array($allowed)){
$data2 = array();
foreach($allowed as $f) if(@$data[$f]) $data2[$f] = $data[$f];
$data = $data2;
}
$id = $this->db->query('INSERT INTO ?# SET ?a', $this->table, $data);
return $id;
}
function remove($id)
{
if(is_array($id)){
$this->db->query('DElETE FROM ?# WHERE id IN (?a)', $this->table, $id);
}elseif(is_numeric($id)){
$this->db->query('DElETE FROM ?# WHERE id = ?', $this->table, $id);
}else{
$this->db->query('DELETE FROM ?# WHERE '.$id, $this->table);
}
}
function getList($where = '', $order = '', $limit = ''){
if($where)
$where = ' WHERE '.$this->formatWhere($where).' ';
else
$where = '';
if($order)
$order = ' ORDER BY '.$order.' ';
else
$order = '';
if($limit)
$limit = ' LIMIT '.$limit.' ';
else
$limit = '';
return $this->db->select('SELECT * FROM ?#'.$where.$order.$limit, $this->table);
}
function getRow($where = '', $order = ''){
if($where)
$where = ' WHERE '.$where.' ';
else
$where = '';
if($order)
$order = ' ORDER BY '.$order.' ';
else
$order = '';
return $this->db->selectRow('SELECT * FROM ?#'.$where.$order.' LIMIT 1', $this->table);
}
function getCount($where = '')
{
if($where)
$where = ' WHERE '.$where;
else
$where = '';
return $this->db->selectCell('SELECT COUNT(*) FROM ?#'.$where, $this->table);
}
function inc($field, $rowID)
{
$where = is_numeric($rowID) ? ' WHERE id = '.$rowID : ' WHERE '.$rowID;
$this->db->query('UPDATE ?# SET ?# = ?# + 1 '.$where, $this->table, $field, $field);
}
function dec($field, $rowID)
{
$where = is_numeric($rowID) ? ' WHERE id = '.$rowID : ' WHERE '.$rowID;
$this->db->query('UPDATE ?# SET ?# = ?# - 1 '.$where, $this->table, $field, $field);
}
function max($field, $where = ''){
if($where){
$where = ' WHERE '.$this->formatWhere($where);
}
return $this->db->selectCell('SELECT MAX(?#) FROM ?# '.$where, $field, $this->table);
}
function min($field, $where = ''){
if($where){
$where = 'WHERE '.$where;
}
return $this->db->selectCell('SELECT MAX(?#) FROM ?# '.$where, $field, $this->table);
}
function sum($field, $where = ''){
if($where){
$where = 'WHERE '.$where;
}
return $this->db->selectCell('SELECT SUM(?#) FROM ?# '.$where, $field, $this->table);
}
/**
* Превращает массив с условиями WHERE в sql-строку
* @var mixed $where исходные данные
* @return string sql-строка
*/
protected function formatWhere($where = false){
if(is_array($where)){
$where1 = array();
foreach($where as $f=>$v){
if($v === null){
$where1[] = "ISNULL(`$f`)";
}else{
$where1[] = $f . " = '".mysql_escape_string($v)."'";
}
}
$where = implode(' AND ', $where1);
}
return $where;
}
}