FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
MySQL-Query-Builder/BasicQuery.php at master · tuffkid/MySQL-Query-Builder · GitHub
tuffkid
/
MySQL-Query-Builder
Public
forked from
indeyets/MySQL-Query-Builder
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
MySQL-Query-Builder
/
BasicQuery.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
279 lines (242 loc) · 7.81 KB
Breadcrumbs
MySQL-Query-Builder
/
BasicQuery.php
Copy path
File metadata and controls
279 lines (242 loc) · 7.81 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 enc=utf8: */
/**
* @author Alexey Zakhlestin
* @package mysql-query-builder
**/
/*
MySQL Query Builder
Copyright © 2005-2007 Alexey Zakhlestin <indeyets@gmail.com>
Copyright © 2005-2006 Konstantin Sedov <kostya.online@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* This class contains all the common logic shared by other query-classes
*
* @package mysql-query-builder
* @author Alexey Zakhlestin
*/
abstract
class
BasicQuery
{
private
$
conditions
=
null
;
private
$
parameters
;
private
$
sql
=
null
;
private
$
orderby
;
private
$
orderdirection
;
/**
* contains QBTable objects related to current query
*
* @var array
*/
protected
$
from
=
array
();
/**
* Constructor provides common logic (all queries are done on tables), but does not direct instantiation of BasicQuery
*
* @param mixed $tables
*/
protected
function
__construct
(
$
tables
)
{
$
this
->
setTables
(
$
tables
);
}
/**
* Sets which table(s) the query will be applied to
*
* @param mixed $tables Can be either string, QBTable instance or array of strings/QBTables
* @return void
* @throws InvalidArgumentException, LogicException
*/
public
function
setTables
(
$
tables
)
{
if
(
is_string
(
$
tables
)
or
$
tables
instanceof
QBTable)
$
tables
=
array
(
$
tables
);
if
(!
is_array
(
$
tables
))
throw
new
InvalidArgumentException
(
'
table(s) should be specified as a string, or array of strings
'
);
if
(
count
(
$
tables
) ==
0
)
throw
new
InvalidArgumentException
(
'
there were no tables, specified
'
);
$
this
->
from
=
array
();
foreach
(
$
tables
as
$
table
) {
if
(
is_string
(
$
table
)) {
$
this
->
from
[] =
new
QBTable
(
$
table
);
}
elseif
(
$
table
instanceof
QBTable) {
$
this
->
from
[] =
$
table
;
}
else
{
throw
new
LogicException
(
"
Invalid object is provided as a table
"
);
}
}
$
this
->
reset
();
}
/**
* Sets where-condition, which will be applied to query
* The most typical objects to use as parameters are Condition and AndOp
*
* @param MQB_Condition $conditions
* @return void
* @author Jimi Dini
*/
public
function
setWhere
(
MQB_Condition
$
conditions
=
null
)
{
if
(
null
===
$
conditions
) {
$
this
->
conditions
=
null
;
}
elseif
(
$
conditions
instanceof
MQB_Condition) {
$
this
->
conditions
=
clone
$
conditions
;
}
$
this
->
reset
();
}
/**
* setup "ORDER BY" clause of Query.
* $orderlist is supposed to be array of objects implementing MQB_Field (most-probably, Field objects).
* $orderdirectionlist is supposed to be array of booleans, where TRUE means DESC and FALSE means ASC.
* if number of elements of $orderdirectionlist is smaller that number of elements of $orderlist array, then ASC is applied to the tail-objects
*
* @param array $orderlist
* @param array $orderdirectionlist
* @return void
* @throws InvalidArgumentException
*/
public
function
setOrderby
(
array
$
orderlist
,
array
$
orderdirectionlist
=
array
())
{
foreach
(
$
orderlist
as
$
field
)
if
(!(
$
field
instanceof
MQB_Field))
throw
new
InvalidArgumentException
(
'
Only object implementing MQB_Field can be used in setOrderBy
'
);
$
this
->
orderby
=
$
orderlist
;
$
this
->
orderdirection
=
$
orderdirectionlist
;
$
this
->
reset
();
}
/**
* accessor, which returns array of table-names used in Query.
*
* @return array
*/
public
function
showTables
()
{
$
res
=
array
();
foreach
(
$
this
->
from
as
$
table
) {
$
res
[] =
$
table
->
getTable
();
}
return
$
res
;
}
/**
* accessor, which returns current-querys condition
*
* @return MQB_Condition
*/
public
function
showConditions
()
{
return
$
this
->
conditions
;
}
// internal stuff
/**
* This method should be overridden by descendents
*
* @param array $parameters
* @return void
* @throws LogicException
*/
protected
function
getSql
(
array
&
$
parameters
)
{
throw
new
LogicException
();
}
/**
* Returns "FROM" clause which can be used in various queries
*
* @param array $parameters
* @return void
*/
protected
function
getFrom
(
array
&
$
parameters
)
{
$
froms
=
array
();
for
(
$
i
=
0
;
$
i
<
count
(
$
this
->
from
);
$
i
++) {
$
froms
[] =
$
this
->
from
[
$
i
]->
__toString
().
'
AS `t
'
.
$
i
.
'
`
'
;
}
$
sql
=
'
FROM
'
.
implode
(
"
,
"
,
$
froms
);
return
$
sql
;
}
/**
* Returns "WHERE" clause which can be used in various queries
*
* @param array $parameters
* @return void
*/
protected
function
getWhere
(
array
&
$
parameters
)
{
if
(
null
===
$
this
->
conditions
)
return
""
;
$
sql
=
$
this
->
conditions
->
getSql
(
$
parameters
);
if
(
empty
(
$
sql
))
return
""
;
return
"
WHERE
"
.
$
sql
;
}
/**
* Returns "ORDER BY" clause which can be used in various queries
*
* @param array $parameters
* @return void
*/
protected
function
getOrderby
(
array
&
$
parameters
)
{
if
(!
$
this
->
orderby
|| !
is_array
(
$
this
->
orderby
))
return
""
;
foreach
(
$
this
->
orderby
as
$
i
=>
$
field
) {
if
(
array_key_exists
(
$
i
,
$
this
->
orderdirection
) &&
$
this
->
orderdirection
[
$
i
])
$
direction
=
'
DESC
'
;
else
$
direction
=
'
ASC
'
;
if
(
null
!==
$
alias
=
$
field
->
getAlias
())
$
sqls
[] =
$
alias
.
$
direction
;
else
$
sqls
[] =
$
field
->
getSql
(
$
parameters
).
$
direction
;
}
return
"
ORDER BY
"
.
implode
(
"
,
"
,
$
sqls
);
}
/**
* resets internal cache-structures, which are used for generation of sql-string and parameters-array
*
* @return void
*/
protected
function
reset
()
{
$
this
->
parameters
=
array
();
$
this
->
sql
=
null
;
}
/**
* rebuilds (if needed) and returns SQL-string, which can be used for "prepared" query
*
* @return string
*/
public
function
sql
(
array
&
$
parameters
=
null
)
{
if
(
null
===
$
this
->
sql
) {
if
(
is_array
(
$
parameters
)) {
$
this
->
parameters
= &
$
parameters
;
}
else
{
$
this
->
parameters
=
array
();
}
$
this
->
sql
=
$
this
->
getSql
(
$
this
->
parameters
);
}
return
$
this
->
sql
;
}
/**
* returns array of parameters, which can be used with SQL-string from ->sql() method.
* WARNING: this method does not rebuild SQL-Query. Be sure to call ->sql() before using it.
*
* @return array
* @author Jimi Dini
*/
public
function
parameters
()
{
if
(
null
===
$
this
->
sql
) {
throw
new
LogicException
(
'
->sql() method should be called, before calling ->parameters() method
'
);
}
return
$
this
->
parameters
;
}
}
Back
|
FazBrowse Home
|
New Git URL