1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* QueryBuilder |
4
|
|
|
* Clase encargada de generar consultas usando métodos de apoyo |
5
|
|
|
* @author nelson rojas |
6
|
|
|
*/ |
7
|
|
|
class QueryBuilder { |
8
|
|
|
|
9
|
|
|
protected $_table = ''; |
10
|
|
|
protected $_columns = ''; |
11
|
|
|
protected $_conditions = ''; |
12
|
|
|
protected $_joins = ''; |
13
|
|
|
protected $_limit = ''; |
14
|
|
|
protected $_group = ''; |
15
|
|
|
protected $_having = ''; |
16
|
|
|
protected $_orderBy = ''; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* constructor |
20
|
|
|
* @params table |
21
|
|
|
*/ |
22
|
|
|
public function __construct($tableName) { |
23
|
|
|
$this->_table = $tableName; |
24
|
|
|
return $this; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* columns |
29
|
|
|
* @params columns |
30
|
|
|
*/ |
31
|
|
|
public function columns($columns) { |
32
|
|
|
$this->_columns = $columns; |
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* where |
38
|
|
|
* @params condition |
39
|
|
|
* @params operator |
40
|
|
|
*/ |
41
|
|
|
public function where($condition) { |
42
|
|
|
$operator = 'AND'; |
43
|
|
|
if (empty($this->_conditions)) { |
44
|
|
|
$this->_conditions = $condition; |
45
|
|
|
} else { |
46
|
|
|
$this->_conditions .= $operator . ' ' . $condition; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->_conditions .= ' '; // extra space after condition |
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* orWhere |
55
|
|
|
* @params condition |
56
|
|
|
* @params operator |
57
|
|
|
*/ |
58
|
|
|
public function orWhere($condition) { |
59
|
|
|
$operator = 'OR'; |
60
|
|
|
if (empty($this->_conditions)) { |
61
|
|
|
$this->_conditions = $condition; |
62
|
|
|
} else { |
63
|
|
|
$this->_conditions .= $operator . ' ' . $condition; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->_conditions .= ' '; // extra space after condition |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* join |
72
|
|
|
* @params join |
73
|
|
|
*/ |
74
|
|
|
public function join($join) { |
75
|
|
|
$this->_joins .= $join . ' '; |
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* limit |
81
|
|
|
* @params limit |
82
|
|
|
*/ |
83
|
|
|
public function limit($limit) { |
84
|
|
|
$this->_limit = 'LIMIT ' . $limit; |
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* group |
90
|
|
|
* @params group |
91
|
|
|
*/ |
92
|
|
|
public function group($group) { |
93
|
|
|
$this->_group = ' GROUP BY ' . $group; |
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* having |
99
|
|
|
* @params having |
100
|
|
|
*/ |
101
|
|
|
public function having($having) { |
102
|
|
|
$this->_having = ' HAVING ' . $having; |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* orderBy |
108
|
|
|
* @params orderBy |
109
|
|
|
*/ |
110
|
|
|
public function orderBy($orderBy) { |
111
|
|
|
$this->_orderBy = ' ORDER BY ' . $orderBy; |
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* generar string con la consulta a partir de los métodos |
117
|
|
|
* cargados |
118
|
|
|
*/ |
119
|
|
|
public function __toString() { |
120
|
|
|
$sql = 'SELECT ' . (empty($this->_columns) ? '*' : $this->_columns) . |
121
|
|
|
' ' . 'FROM ' . $this->_table; |
122
|
|
|
|
123
|
|
|
$check_for = [ |
124
|
|
|
'_joins', |
125
|
|
|
'_conditions', |
126
|
|
|
'_group', |
127
|
|
|
'_having', |
128
|
|
|
'_limit', |
129
|
|
|
'_orderBy' |
130
|
|
|
]; |
131
|
|
|
|
132
|
|
|
if (!empty($this->_conditions)) { |
133
|
|
|
$this->_conditions = 'WHERE ' . $this->_conditions; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
foreach ($check_for as $element) : |
137
|
|
|
if (!empty(trim($this->$element))) : |
138
|
|
|
$sql .= ' ' . $this->$element . ' '; |
139
|
|
|
endif; |
140
|
|
|
|
141
|
|
|
endforeach; |
142
|
|
|
|
143
|
|
|
return $sql; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* clear |
148
|
|
|
*/ |
149
|
|
|
private function clear() { |
150
|
|
|
$clear_in = [ |
151
|
|
|
'_table', |
152
|
|
|
'_joins', |
153
|
|
|
'_conditions', |
154
|
|
|
'_group', |
155
|
|
|
'_having', |
156
|
|
|
'_limit', |
157
|
|
|
'_orderBy' |
158
|
|
|
]; |
159
|
|
|
foreach ($clear_in as $elem) : |
160
|
|
|
$this->$elem = ''; |
161
|
|
|
endforeach |
162
|
|
|
; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* destruct |
167
|
|
|
*/ |
168
|
|
|
public function __destruct() { |
169
|
|
|
$this->clear(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
} |
173
|
|
|
|