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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
<?php
namespace Dhtmlx\Connector\Data;
use \Exception;
/*! manager of data request
**/
class DataRequestConfig {
private $action_mode = "";
private $filters; //!< array of filtering rules
private $relation=false; //!< ID or other element used for linking hierarchy
private $sort_by; //!< sorting field
private $start; //!< start of requested data
private $count; //!< length of requested data
private $order = false;
private $user;
private $version;
//for render_sql
private $source; //!< souce table or another source destination
private $fieldset; //!< set of data, which need to be retrieved from source
/*! constructor
@param proto
DataRequestConfig object, optional, if provided then new request object will copy all properties from provided one
*/
public function __construct($proto=false){
if ($proto)
$this->copy($proto);
else{
$start=0;
$this->filters=array();
$this->sort_by=array();
}
}
/*! copy parameters of source object into self
@param proto
source object
*/
public function copy($proto){
$this->filters =$proto->get_filters();
$this->sort_by =$proto->get_sort_by();
$this->count =$proto->get_count();
$this->start =$proto->get_start();
$this->source =$proto->get_source();
$this->fieldset =$proto->get_fieldset();
$this->relation =$proto->get_relation();
$this->user = $proto->user;
$this->version = $proto->version;
$this->action_mode = $proto->action_mode;
}
/*! convert self to string ( for logs )
@return
self as plain string,
*/
public function __toString(){
$str="Source:{$this->source}\nFieldset:{$this->fieldset}\nWhere:";
for ($i=0; $i < sizeof($this->filters); $i++)
$str.=$this->filters[$i]["name"]." ".$this->filters[$i]["operation"]." ".$this->filters[$i]["value"].";";
$str.="\nStart:{$this->start}\nCount:{$this->count}\n";
for ($i=0; $i < sizeof($this->sort_by); $i++)
$str.=$this->sort_by[$i]["name"]."=".$this->sort_by[$i]["direction"].";";
$str.="\nRelation:{$this->relation}";
return $str;
}
public function set_action_mode($action_mode) {
$this->action_mode = $action_mode;
return $this;
}
public function get_action_mode() {
return $this->action_mode;
}
/*! returns set of filtering rules
@return
set of filtering rules
*/
public function get_filters(){
return $this->filters;
}
public function &get_filters_ref(){
return $this->filters;
}
public function set_filters($data){
$this->filters=$data;
}
public function get_order(){
return $this->order;
}
public function set_order($order){
$this->order = $order;
}
public function get_user(){
return $this->user;
}
public function set_user($user){
$this->user = $user;
}
public function get_version(){
return $this->version;
}
public function set_version($version){
$this->version = $version;
}
/*! returns list of used fields
@return
list of used fields
*/
public function get_fieldset(){
return $this->fieldset;
}
/*! returns name of source table
@return
name of source table
*/
public function get_source(){
return $this->source;
}
/*! returns set of sorting rules
@return
set of sorting rules
*/
public function get_sort_by(){
return $this->sort_by;
}
public function &get_sort_by_ref(){
return $this->sort_by;
}
public function set_sort_by($data){
$this->sort_by=$data;
}
/*! returns start index
@return
start index
*/
public function get_start(){
return $this->start;
}
/*! returns count of requested records
@return
count of requested records
*/
public function get_count(){
return $this->count;
}
/*! returns name of relation id
@return
relation id name
*/
public function get_relation(){
return $this->relation;
}
/*! sets sorting rule
@param field
name of column
@param order
direction of sorting
*/
public function set_sort($field,$order=false){
if (!$field && !$order)
$this->sort_by=array();
else{
if ($order===false)
$this->sort_by[] = $field;
else {
$order=strtolower($order)=="asc"?"ASC":"DESC";
$this->sort_by[]=array("name"=>$field,"direction" => $order);
}
}
}
/*! sets filtering rule
@param field
name of column
@param value
value for filtering
@param operation
operation for filtering, optional , LIKE by default
*/
public function set_filter($field,$value=false,$operation=false){
if ($value === false)
array_push($this->filters,$field);
else
array_push($this->filters,array("name"=>$field,"value"=>$value,"operation"=>$operation));
}
/*! sets list of used fields
@param value
list of used fields
*/
public function set_fieldset($value){
$this->fieldset=$value;
}
/*! sets name of source table
@param value
name of source table
*/
public function set_source($value){
if (is_string($value))
$value = trim($value);
$this->source = $value;
if (!$this->source) throw new Exception("Source of data can't be empty");
}
/*! sets data limits
@param start
start index
@param count
requested count of data
*/
public function set_limit($start,$count){
$this->start=$start;
$this->count=$count;
}
/*! sets name of relation id
@param value
name of relation id field
*/
public function set_relation($value){
$this->relation=$value;
}
/*! parse incoming sql, to fill other properties
@param sql
incoming sql string
*/
public function parse_sql($sql, $as_is = false){
if ($as_is){
$this->fieldset = $sql;
return;
}
$sql= preg_replace("/[ \n\t]+limit[\n\t ,0-9]*$/i","",$sql);
$data = preg_split("/[ \n\t]+\\_from\\_/i",$sql,2);
if (count($data)!=2)
$data = preg_split("/[ \n\t]+from/i",$sql,2);
$this->fieldset = preg_replace("/^[\s]*select/i","",$data[0],1);
//Ignore next type of calls
//direct call to stored procedure without FROM
if ((count($data) == 1) ||
//UNION select
preg_match("#[ \n\r\t]union[ \n\t\r]#i", $sql)){
$this->fieldset = $sql;
return;
}
$table_data = preg_split("/[ \n\t]+where/i",$data[1],2);
/*
if sql code contains group_by we will place all sql query in the FROM
it will not allow to use any filtering against the query
still it is better than just generate incorrect sql commands for any group by query
*/
if (sizeof($table_data)>1 && !preg_match("#.*group by.*#i",$table_data[1])){ //where construction exists
$this->set_source($table_data[0]);
$where_data = preg_split("/[ \n\t]+order[ ]+by/i",$table_data[1],2);
$this->filters[]=$where_data[0];
if (sizeof($where_data)==1) return; //end of line detected
$data=$where_data[1];
} else {
$table_data = preg_split("/[ \n\t]+order[ ]+by/i",$data[1],2);
$this->set_source($table_data[0]);
if (sizeof($table_data)==1) return; //end of line detected
$data=$table_data[1];
}
if (trim($data)){ //order by construction exists
$s_data = preg_split("/\\,/",trim($data));
for ($i=0; $i < count($s_data); $i++) {
$data=preg_split("/[ ]+/",trim($s_data[$i]),2);
if (sizeof($data)>1)
$this->set_sort($data[0],$data[1]);
else
$this->set_sort($data[0]);
}
}
}
}
|