FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
database/src/SelectQuery.php at master · selective-php/database · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Nov 26, 2022. It is now read-only.
selective-php
/
database
Public archive
Notifications
You must be signed in to change notification settings
Fork
4
Star
23
Code
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
database
/
src
/
SelectQuery.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
707 lines (608 loc) · 15.5 KB
Breadcrumbs
database
/
src
/
SelectQuery.php
Copy path
File metadata and controls
707 lines (608 loc) · 15.5 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
<?php
namespace
Selective
\
Database
;
use
PDO
;
use
PDOStatement
;
use
RuntimeException
;
/**
* Select Query.
*/
final
class
SelectQuery
implements
QueryInterface
{
/**
* @var Connection
*/
private
Connection
$
connection
;
/**
* @var PDO
*/
private
PDO
$
pdo
;
/**
* @var Quoter
*/
private
Quoter
$
quoter
;
/**
* @var array
*/
private
array
$
columns
= [];
/**
* @var string|null
*/
private
?
string
$
alias
=
null
;
/**
* @var string|array
*/
private
$
from
=
''
;
/**
* @var array
*/
private
array
$
join
= [];
/**
* @var array
*/
private
array
$
union
= [];
/**
* @var Condition Where conditions
*/
private
Condition
$
condition
;
/**
* @var array
*/
private
array
$
orderBy
= [];
/**
* @var array
*/
private
array
$
groupBy
= [];
/**
* @var int|null
*/
private
?
int
$
limit
=
null
;
/**
* @var int|null
*/
private
?
int
$
offset
=
null
;
/**
* @var string
*/
private
string
$
distinct
=
''
;
/**
* @var string
*/
private
string
$
calcFoundRows
=
''
;
/**
* @var string
*/
private
string
$
bufferResult
=
''
;
/**
* @var string
*/
private
string
$
resultSize
=
''
;
/**
* @var string
*/
private
string
$
straightJoin
=
''
;
/**
* @var string
*/
private
string
$
highPriority
=
''
;
/**
* The constructor.
*
* @param Connection $connection The connection
*/
public
function
__construct
(
Connection
$
connection
)
{
$
this
->
connection
=
$
connection
;
$
this
->
pdo
=
$
connection
->
getPdo
();
$
this
->
quoter
=
$
connection
->
getQuoter
();
$
this
->
condition
=
new
Condition
(
$
connection
,
$
this
);
}
/**
* Distinct.
*
* @return self
*/
public
function
distinct
():
self
{
$
this
->
distinct
=
'
DISTINCT
'
;
return
$
this
;
}
/**
* Distinct row.
*
* @return self
*/
public
function
distinctRow
():
self
{
$
this
->
distinct
=
'
DISTINCTROW
'
;
return
$
this
;
}
/**
* Distinct row.
*
* @return self
*/
public
function
straightJoin
():
self
{
$
this
->
straightJoin
=
'
STRAIGHT_JOIN
'
;
return
$
this
;
}
/**
* High Priority.
*
* @return self
*/
public
function
highPriority
():
self
{
$
this
->
highPriority
=
'
HIGH_PRIORITY
'
;
return
$
this
;
}
/**
* Small Result.
*
* @return self
*/
public
function
smallResult
():
self
{
$
this
->
resultSize
=
'
SQL_SMALL_RESULT
'
;
return
$
this
;
}
/**
* Big Result.
*
* @return self
*/
public
function
bigResult
():
self
{
$
this
->
resultSize
=
'
SQL_BIG_RESULT
'
;
return
$
this
;
}
/**
* Buffer Result.
*
* @return self
*/
public
function
bufferResult
():
self
{
$
this
->
bufferResult
=
'
SQL_BUFFER_RESULT
'
;
return
$
this
;
}
/**
* Calc Found Rows.
*
* @return self
*/
public
function
calcFoundRows
():
self
{
$
this
->
calcFoundRows
=
'
SQL_CALC_FOUND_ROWS
'
;
return
$
this
;
}
/**
* Adds new fields to be returned by a `SELECT` statement when this query is
* executed. Fields can be passed as an array of strings, array of expression
* objects, a single expression or a single string.
*
* If an array is passed, keys will be used to alias fields using the value as the
* real field to be aliased. It is possible to alias strings, Expression objects or
* even other Query objects.
*
* This method will append any passed argument to the list of fields to be selected.
*
* @param array $columns The columns as array
*
* @return self
*/
public
function
columns
(
array
$
columns
):
self
{
if
(
empty
(
$
this
->
columns
)) {
$
this
->
columns
=
$
columns
;
}
else
{
foreach
(
$
columns
as
$
column
) {
$
this
->
columns
[] =
$
column
;
}
}
return
$
this
;
}
/**
* Alias for sub selects.
*
* @param string $alias The alias
*
* @return self
*/
public
function
alias
(
string
$
alias
):
self
{
$
this
->
alias
=
$
alias
;
return
$
this
;
}
/**
* From.
*
* @param string|array $table Table name
*
* @return self
*/
public
function
from
(
$
table
):
self
{
$
this
->
from
=
$
table
;
return
$
this
;
}
/**
* UNION is used to combine the result from multiple
* SELECT statements into a single result set.
*
* @param SelectQuery $query The query to combine
*
* @return self
*/
public
function
union
(
SelectQuery
$
query
):
self
{
$
this
->
union
[] = [
''
,
$
query
->
build
(
false
)];
return
$
this
;
}
/**
* UNION ALL is used to combine the result from multiple
* SELECT statements into a single result set.
*
* @param SelectQuery $query The query to combine
*
* @return self
*/
public
function
unionAll
(
SelectQuery
$
query
):
self
{
$
this
->
union
[] = [
'
ALL
'
,
$
query
->
build
(
false
)];
return
$
this
;
}
/**
* UNION DISTINCT is used to combine the result from multiple
* SELECT statements into a single result set.
*
* @param SelectQuery $query The query to combine
*
* @return self
*/
public
function
unionDistinct
(
SelectQuery
$
query
):
self
{
$
this
->
union
[] = [
'
DISTINCT
'
,
$
query
->
build
(
false
)];
return
$
this
;
}
/**
* Join.
*
* @param string|array $table Table name
* @param string $leftField Name of the left field
* @param string $comparison Comparison (=,<,>,<=,>=,<>,in, not in, between, not between)
* @param mixed $rightField Value of the right field
*
* @return self
*/
public
function
join
(
$
table
,
string
$
leftField
,
string
$
comparison
,
$
rightField
):
self
{
$
this
->
join
[] = [
'
inner
'
,
$
table
,
$
leftField
,
$
comparison
,
$
rightField
];
return
$
this
;
}
/**
* Inner Join (alias).
*
* @param string|array $table Table name
* @param string $leftField Name of the left field
* @param string $comparison Comparison (=,<,>,<=,>=,<>,in, not in, between, not between)
* @param mixed $rightField Value of the right field
*
* @return self
*/
public
function
innerJoin
(
$
table
,
string
$
leftField
,
string
$
comparison
,
$
rightField
):
self
{
return
$
this
->
join
(
$
table
,
$
leftField
,
$
comparison
,
$
rightField
);
}
/**
* Left Join.
*
* @param string|array $table Table name
* @param string $leftField Name of the left field
* @param string $comparison Comparison (=,<,>,<=,>=,<>,in, not in, between, not between)
* @param mixed $rightField Value of the right field
*
* @return self
*/
public
function
leftJoin
(
$
table
,
string
$
leftField
,
string
$
comparison
,
$
rightField
):
self
{
$
this
->
join
[] = [
'
left
'
,
$
table
,
$
leftField
,
$
comparison
,
$
rightField
];
return
$
this
;
}
/**
* Join with complex conditions.
*
* @param string|array $table Table name
* @param string $conditions The ON conditions e.g. 'user.id = article.user_id'
*
* @return self
*/
public
function
joinRaw
(
$
table
,
string
$
conditions
):
self
{
$
this
->
join
[] = [
'
inner
'
,
$
table
,
new
RawExp
(
$
conditions
),
null
,
null
,
null
];
return
$
this
;
}
/**
* Left join with complex conditions.
*
* @param string|array $table Table name
* @param string $conditions The ON conditions e.g. 'user.id = article.user_id'
*
* @return self
*/
public
function
leftJoinRaw
(
$
table
,
string
$
conditions
):
self
{
$
this
->
join
[] = [
'
left
'
,
$
table
,
new
RawExp
(
$
conditions
),
null
,
null
,
null
];
return
$
this
;
}
/**
* Where AND condition.
*
* @param array ...$conditions The conditions (field, comparison, value)
* or (field, comparison, new RawExp('table.field'))
* or new RawExp('...')
*
* @return self
*/
public
function
where
(...
$
conditions
):
self
{
$
this
->
condition
->
where
(
$
conditions
);
return
$
this
;
}
/**
* Add a raw AND WHERE condition.
*
* @param string $condition The raw where conditions e.g. 'user.id = article.user_id'
*
* @return self
*/
public
function
whereRaw
(
string
$
condition
):
self
{
$
this
->
condition
->
where
([
new
RawExp
(
$
condition
)]);
return
$
this
;
}
/**
* Where OR condition.
*
* @param array ...$conditions The conditions (field, comparison, value)
* or (field, comparison, new RawExp('table.field'))
* or new RawExp('...')
*
* @return self
*/
public
function
orWhere
(...
$
conditions
):
self
{
$
this
->
condition
->
orWhere
(
$
conditions
);
return
$
this
;
}
/**
* Add a raw OR WHERE condition.
*
* @param string $condition The raw where conditions e.g. 'user.id = article.user_id'
*
* @return self
*/
public
function
orWhereRaw
(
string
$
condition
):
self
{
$
this
->
condition
->
orWhere
([
new
RawExp
(
$
condition
)]);
return
$
this
;
}
/**
* The whereColumn method may be used to verify that two columns are equal.
*
* @param string $column Name of the first column
* @param string $comparison The comparison type (=,>=,<=,<>,is,is not, ....)
* @param string $secondColumn Name of the second column
*
* @return self
*/
public
function
whereColumn
(
string
$
column
,
string
$
comparison
,
string
$
secondColumn
):
self
{
$
secondColumn
=
$
this
->
quoter
->
quoteName
(
$
secondColumn
);
$
this
->
condition
->
where
([
$
column
,
$
comparison
,
new
RawExp
(
$
secondColumn
)]);
return
$
this
;
}
/**
* The whereColumn method may be used to verify that two columns are equal.
*
* @param string $column Name of the first column
* @param string $comparison The comparison (=,>=,<=,<>,is,is not, ....)
* @param string $secondColumn Name of the second column
*
* @return self
*/
public
function
orWhereColumn
(
string
$
column
,
string
$
comparison
,
string
$
secondColumn
):
self
{
$
secondColumn
=
$
this
->
quoter
->
quoteName
(
$
secondColumn
);
$
this
->
condition
->
orWhere
([
$
column
,
$
comparison
,
new
RawExp
(
$
secondColumn
)]);
return
$
this
;
}
/**
* Order by.
*
* @param array ...$fields Column name(s)
*
* @return self
*/
public
function
orderBy
(...
$
fields
):
self
{
$
this
->
orderBy
=
$
fields
;
return
$
this
;
}
/**
* Group by.
*
* @param array ...$fields The fields
*
* @return self
*/
public
function
groupBy
(...
$
fields
):
self
{
$
this
->
groupBy
=
$
fields
;
return
$
this
;
}
/**
* Add AND having condition.
*
* @param array ...$conditions The conditions (field, comparison, value)
* or (field, comparison, new RawExp('table.field'))
* or new RawExp('...')
*
* @return self
*/
public
function
having
(...
$
conditions
):
self
{
$
this
->
condition
->
having
(
$
conditions
);
return
$
this
;
}
/**
* Add OR having condition.
*
* @param array ...$conditions The conditions (field, comparison, value)
* or (field, comparison, new RawExp('table.field'))
* or new RawExp('...')
*
* @return self
*/
public
function
orHaving
(...
$
conditions
):
self
{
$
this
->
condition
->
orHaving
(
$
conditions
);
return
$
this
;
}
/**
* Add AND having condition.
*
* @param string $condition The raw HAVING conditions e.g. 'user.id = article.user_id'
*
* @return self
*/
public
function
havingRaw
(
string
$
condition
):
self
{
$
this
->
condition
->
having
([
new
RawExp
(
$
condition
)]);
return
$
this
;
}
/**
* Add OR having condition.
*
* @param string $condition The raw HAVING conditions e.g. 'user.id = article.user_id'
*
* @return self
*/
public
function
orHavingRaw
(
string
$
condition
):
self
{
$
this
->
condition
->
orHaving
([
new
RawExp
(
$
condition
)]);
return
$
this
;
}
/**
* Limit the number of rows returned.
*
* @param int $rowCount Row count
*
* @return self
*/
public
function
limit
(
int
$
rowCount
):
self
{
$
this
->
limit
=
$
rowCount
;
return
$
this
;
}
/**
* Offset of the first row to return.
*
* @param int $offset Offset
*
* @return self
*/
public
function
offset
(
int
$
offset
):
self
{
$
this
->
offset
=
$
offset
;
return
$
this
;
}
/**
* Returns a Raw Expression.
*
* @param string $value A raw value. Be careful!
*
* @return RawExp Raw Expression
*/
public
function
raw
(
string
$
value
):
RawExp
{
return
new
RawExp
(
$
value
);
}
/**
* Executes an SQL statement, returning a result set as a PDOStatement object.
*
* @throws RuntimeException
*
* @return PDOStatement The pdo statement
*/
public
function
execute
():
PDOStatement
{
$
statement
=
$
this
->
pdo
->
query
(
$
this
->
build
());
if
(!
$
statement
instanceof
PDOStatement) {
throw
new
RuntimeException
(
'
Query could not be created
'
);
}
return
$
statement
;
}
/**
* Prepares a statement for execution and returns a statement object.
*
* @throws RuntimeException
*
* @return PDOStatement The pdo statement
*/
public
function
prepare
():
PDOStatement
{
$
statement
=
$
this
->
pdo
->
prepare
(
$
this
->
build
());
if
(!
$
statement
instanceof
PDOStatement) {
throw
new
RuntimeException
(
'
The database statement could not be prepared.
'
);
}
return
$
statement
;
}
/**
* SQL functions.
*
* @return FunctionBuilder The function builder
*/
public
function
func
():
FunctionBuilder
{
return
new
FunctionBuilder
(
$
this
->
connection
);
}
/**
* Build a SQL string.
*
* @param bool $complete The complete
*
* @return string SQL string
*/
public
function
build
(
bool
$
complete
=
true
):
string
{
$
builder
=
new
SelectQueryBuilder
(
$
this
->
connection
);
$
sql
= [];
$
sql
=
$
builder
->
getSelectSql
(
$
sql
,
[
$
this
->
distinct
,
$
this
->
highPriority
,
$
this
->
straightJoin
,
$
this
->
resultSize
,
$
this
->
bufferResult
,
$
this
->
calcFoundRows
,
]
);
$
sql
=
$
builder
->
getColumnsSql
(
$
sql
,
$
this
->
columns
);
$
sql
=
$
builder
->
getFromSql
(
$
sql
,
$
this
->
from
);
$
sql
=
$
builder
->
getJoinSql
(
$
sql
,
$
this
->
join
);
$
sql
=
$
this
->
condition
->
getWhereSql
(
$
sql
);
$
sql
=
$
builder
->
getGroupBySql
(
$
sql
,
$
this
->
groupBy
);
$
sql
=
$
this
->
condition
->
getHavingSql
(
$
sql
);
$
sql
=
$
builder
->
getOrderBySql
(
$
sql
,
$
this
->
orderBy
);
$
sql
=
$
builder
->
getLimitSql
(
$
sql
,
$
this
->
limit
,
$
this
->
offset
);
$
sql
=
$
builder
->
getUnionSql
(
$
sql
,
$
this
->
union
);
$
result
=
trim
(
implode
(
'
'
,
$
sql
));
$
result
=
$
builder
->
getAliasSql
(
$
result
,
$
this
->
alias
);
if
(
$
complete
) {
$
result
=
trim
(
$
result
) .
'
;
'
;
}
return
$
result
;
}
}
Back
|
FazBrowse Home
|
New Git URL