- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuery2Builder.php
More file actions
184 lines (155 loc) · 3.93 KB
/
Query2Builder.php
File metadata and controls
184 lines (155 loc) · 3.93 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
184
<?php
/**
* Query2Builder is a class for building queries programatically
*
* @author Adam Zivner <adam.zivner@gmail.com>
* @license http://www.opensource.org/licenses/bsd-license.php New BSD license
* @package Query2
* @link http://query2.0o.cz
*/
class Query2Builder
{
protected $select = array(), $from = array(), $where = array(), $group_by = array(), $having = array(), $order_by = array();
protected $limit;
/** @return array of SQL chunks and arguments */
public function mergeQuery()
{
$args = array();
if (count($this->select)) {
$args = array_merge($args, array("SELECT"), array_slice($this->select, 1));
}
if (count($this->from)) {
$args = array_merge($args, array("FROM"), $this->from);
}
if (count($this->where)) {
$args = array_merge($args, array("WHERE", "("), array_slice($this->where, 1), array(")"));
}
if (count($this->group_by)) {
$args = array_merge($args, array("GROUP BY"), array_slice($this->group_by, 1));
}
if (count($this->having)) {
$args = array_merge($args, array("HAVING", "("), array_slice($this->having, 1), array(")"));
}
if (count($this->order_by)) {
$args = array_merge($args, array("ORDER BY"), array_slice($this->order_by, 1));
}
if (strlen($this->limit)) {
$args = array_merge($args, array("LIMIT", $this->limit));
}
return $args;
}
public function select()
{
$args = func_get_args();
$this->select = array_merge($this->select, array(","), $args);
return $this;
}
public function from()
{
$args = func_get_args();
$this->from = array_merge($this->from, $args);
return $this;
}
public function whereAnd()
{
$this->where[] = " AND ";
$this->where = $this->parseArgs(func_get_args(), $this->where);
return $this;
}
public function whereOr()
{
$this->where[] = " OR ";
$this->where = $this->parseArgs(func_get_args(), $this->where);
return $this;
}
public function havingAnd()
{
$this->having[] = " AND ";
$this->having = $this->parseArgs(func_get_args(), $this->having);
return $this;
}
public function havingOr()
{
$this->having[] = " OR ";
$this->having = $this->parseArgs(func_get_args(), $this->having);
return $this;
}
public function groupBy()
{
$args = func_get_args();
$this->group_by = array_merge($this->group_by, array(","), $args);
return $this;
}
public function orderBy()
{
$args = func_get_args();
$this->order_by = array_merge($this->order_by, array(","), $args);
return $this;
}
/**
* @param string /integer $skip either first part of the limit
* @param integer $num if $skip is not string then this is the second part of limit statement
* @return $this
*/
public function limit($skip, $num = -1)
{
$this->limit = (int)$skip . ($num != -1 ? ", " . (int)$num : "");
return $this;
}
// Clear methods allow to clear just some part of the query builder
public function clearSelect()
{
$this->select = array();
return $this;
}
public function clearFrom()
{
$this->from = array();
return $this;
}
public function clearWhere()
{
$this->where = array();
return $this;
}
public function clearOrderBy()
{
$this->order_by = array();
return $this;
}
public function clearGroupBy()
{
$this->group_by = array();
return $this;
}
public function clearHaving()
{
$this->having = array();
return $this;
}
public function clearLimit()
{
$this->limit = "";
return $this;
}
/**
* Adds $args into $arr. Instances of Query2Builder are merged into arrays.
* Used by whereOr(), whereAnd(), havingOr() and havingAnd()
*
* @param array $args - array of strings and Query2Builder objects
* @param array $arr
* @return array - array of strings
*/
protected function parseArgs($args, $arr)
{
foreach ($args as $arg) {
if (is_object($arg) && method_exists($arg, 'mergeQuery')) // is this instance of Query2Builder?
{
$arr = array_merge($arr, array_slice($arg->mergeQuery(), 1));
} else {
$arr[] = $arg;
}
}
return $arr;
}
}