-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimplequery.class.php
225 lines (194 loc) · 5.1 KB
/
simplequery.class.php
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
<?php
/*
TODO:
Add a better solution for field and table aliases
Add other query types
*/
class SimpleQuery{
var $condition_operator = "AND";
var $fields = array();
function __construct($type,$primary_table){
$this->type = $type;
$this->primary_table = $primary_table;
}
function add_condition($field,$operator,$value,$identifier = null){
if(!$identifier){
$this->conditions[] = array($field,$operator,$value);
}else{
$this->conditions[$identifier] = array($field,$operator,$value);
}
return $this;
}
function add_fields(array $fields){
foreach ($fields as $f) {
$this->add_field($f);
}
return $this;
}
function add_field($f){
if(!in_array($f,$this->fields)){
$this->fields[] = $f;
}
return $this;
}
function add_alias($field,$alias){
$this->aliases[$field] = $alias;
return $this;
}
function add_table($table,$local_key,$foreign_key,$join_type = 'INNER'){
if(strpos($local_key,'.') === false){
$local_key = $table.'.'.$local_key;
}
$this->join_tables[$table] = array(
'local_key' => $local_key,
'foreign_key' => $foreign_key,
'join_type' => $join_type
);
return $this;
}
function add_order($field){
$this->order = $field;
return $this;
}
function add_order_direction($dir){
$this->order_direction = $dir;
return $this;
}
function set_condition_structure($structure){
$this->condition_structure = $structure;
/*
$structure = array(
'AND' => array(
'condition1',
'OR' => array(
'condition2',
'AND' => array('condition4','condition5')
),
'condition3'
)
);
Output:
WHERE condition1 AND (condition2 OR (condition4 AND condition5)) AND condition3
*/
return $this;
}
function callback_join($array,$join,$callback){
$out = '';
$join_str = '';
if(is_callable($callback)){
foreach ($array as $key => $value) {
$out .= $join_str;
$out .= $callback($value,$key);
$join_str = $join;
}
}
return $out;
}
function recurse_structure($element,$type){
$out = array();
foreach ($element as $key => $value) {
if(is_array($value)){
$out[] = '('.$this->recurse_structure($value,$key).')';
}else{
$out[] = $this->condition_from_id($value);
}
}
return join($out,' '.$type.' ');
}
/*
function recurse_structure(&$sql,$element,$type){
foreach ($element as $key => $value) {
if(is_array($value)){
$this->recurse_structure($sql,$value,$key);
}else{
$sql .=
}
if($key == 'AND'){
$this->recurse_structure($value,'AND');
}elseif($key == 'OR'){
$this->recurse_structure($value,'OR');
}else{
$out .= $this->conditions[$value].' '.$type;
}
}
}
*/
function set_limit($limit){
$this->limit = $limit;
return $this;
}
function set_offset($offset){
$this->offset = $offset;
return $this;
}
function set_groupby($field){
$this->groupby = $field;
return $this;
}
function set_type($type){
$this->type = $type;
return $this;
}
function backticks($field){
if(preg_match('/^\w+\.\w+$/',$field)){
return '`'.str_replace('.','`.`',$field).'`';
}else{
return $field;
}
}
function alias($field){
if($this->aliases[$field]){
return ' as '.$this->aliases[$field];
}
}
function get(){
if($this->type == "SELECT"){
$sql = "SELECT ";
if(count($this->fields) > 0){
$sql .= $this->callback_join($this->fields,', ',function($field){return $this->backticks($field).$this->alias($field);}).' ';
}else{
$sql .= '* ';
}
$sql .= "FROM ";
$sql .= '`'.$this->primary_table.'` ';
if(count($this->join_tables) > 0){
foreach ($this->join_tables as $t => $ta) {
$sql .= $ta['join_type'].' JOIN '.$t.' ON '.$this->backticks($ta['local_key']).' = '.$this->backticks($ta['foreign_key']).' ';
}
}
if(count($this->conditions) > 0){
if(!$this->condition_structure){
$sql .= "WHERE ";
$cond_strs = array();
foreach ($this->conditions as $id => $condition) {
$cond_strs[] = $this->backticks($condition[0]).' '.$condition[1]." '".$condition[2]."' ";
}
$sql .= join($cond_strs,$this->condition_operator.' ');
}else{
$sql .= "WHERE ".$this->recurse_structure($this->condition_structure,$this->condition_operator)." ";
}
}
if($this->groupby){
$sql.= "GROUP BY ".$this->groupby.' ';
}
if($this->order){
$sql.= "ORDER BY ".$this->order.' ';
}
if($this->order_direction){
$sql.= $this->order_direction.' ';
}
if($this->limit){
$sql.= "LIMIT ".$this->limit.' ';
}
if($this->offset){
$sql.= "OFFSET ".$this->offset.' ';
}
}
return $sql;
}
function condition_from_id($id){
$c = $this->conditions[$id];
return $this->backticks($c[0]).' '.$c[1]." '".$c[2]."' ";
}
}
?>