-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSqlQueryBuilder.php
More file actions
183 lines (157 loc) · 5.41 KB
/
SqlQueryBuilder.php
File metadata and controls
183 lines (157 loc) · 5.41 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
182
183
<?php
/**
* Description of SqlQueryBuilder
* Written by Ziyahan Albeniz
* http://ziyahanalbeniz.blogspot.com
*/
class SqlQueryBuilder {
private $_select = array();
private $_joins = array();
private $_from = "";
private $_limit=null;
private $_start=0;
private $_fetch = 0;
private $_sqlText = "";
private $_mainTable="";
private $_where = null;
private $_orderBy=null;
private $_groupBy=null;
private $_debug=false;
private function regenerateQuery() {
$this->_select = array();
$this->_joins = array();
$this->_from = array();
$this->_limit = null;
$this->_start = 0;
$this->_fetch = 0;
$this->_sqlText = "";
$this->_mainTable="";
$this->_where = null;
$this->_orderBy = null;
$this->_groupBy = null;
}
public function limits($limits=null) {
$this->_limit = $limits;
return $this;
}
public function orderBy($orderBy=array()) {
$this->_orderBy = $orderBy;
return $this;
}
public function groupBy($groupBy=array()) {
$this->_groupBy = $groupBy;
return $this;
}
public function from($from) {
$this->_mainTable = $from;
return $this;
}
public function mainTable($mainTable) {
$this->_mainTable = $mainTable;
return $this;
}
public function where($where) {
$this->_where = $where;
return $this;
}
public function select($selectArr=array()) {
$this->regenerateQuery();
$this->_select = $selectArr;
return $this;
}
public function join($type="", $table="", $on=array()) {
array_push($this->_joins, array("type"=>$type, "table"=>$table, "on"=>$on));
return $this;
}
public function order($order=array()) {
$this->order=$order;
return $this;
}
private function conditions($param=array(), $logic="and") {
if(is_array($param)) {
$isMultiDimensional = @is_array( $param[0]);
if($isMultiDimensional) {
foreach($param as $item) {
if($this->_debug) {
print_r($item);exit();
}
$operator="=";
if(isset($item["type"])) {
if($item["type"]=="subset") {
if(isset($item["items"])) {
if(is_array($item["items"])) {
$this->_sqlText.=" ".$logic." ( 1<>1 ";
foreach($item["items"] as $subsetItem) {
$this->conditions($subsetItem, "or");
}
$this->_sqlText.=")";
continue;
}
}
}
}
if(isset($item["operator"])) {
$operator = $item["operator"];
}
if($operator=="in" || $operator=="is") {
$this->_sqlText.=" ".$logic." ".$item["column"]." ".$operator." ".$item["value"]." ";
}
else {
$this->_sqlText.=" ".$logic." ".$item["column"].$operator."'".$item["value"]."'";
}
}
} else {
if(count($param)) {
$operator="=";
if(isset($param["operator"])) {
$operator = $param["operator"];
}
if($operator=="in" || $operator=="is") {
$this->_sqlText.=" ".$logic." ".$param["column"]." ".$operator." ".$param["value"]." ";
}
else {
$this->_sqlText.=" ".$logic." ".$param["column"].$operator."'".$param["value"]."'";
}
}
}
}
}
public function build() {
$this->_sqlText="";
$this->_sqlText.="select ".implode(", ", $this->_select);
$this->_sqlText.=" from ".$this->_mainTable;
//joins
foreach($this->_joins as $item) {
$this->_sqlText.=" ".$item["type"]." join ".$item["table"]." on ".implode(" and ", $item["on"]);
}
//where block
$this->_sqlText.=" where 1=1 ";
$this->conditions($this->_where);
//groupBy block
if($this->_groupBy) {
$groupStr = " group by ";
foreach($this->_groupBy as $item) {
$groupStr.=$item.",";
}
$groupStr = substr($groupStr, 0, strlen($groupStr)-1);
$this->_sqlText.=$groupStr;
}
//orderBy block
if($this->_orderBy) {
if(!is_array( $this->_orderBy[0])) {
$this->_orderBy=array($this->_orderBy);
}
$orderStr = " order by ";
foreach($this->_orderBy as $item) {
$orderStr.=$item["field"]." ".$item["dir"].",";
}
$orderStr = substr($orderStr, 0, strlen($orderStr)-1);
$this->_sqlText.=$orderStr;
}
// limits
if($this->_limit) {
$this->_sqlText.=" limit ".$this->_limit["start"].",".$this->_limit["limit"];
}
return $this->_sqlText;
}
}