query.php
39.9 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
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
<?php
/**
* @package FrameworkOnFramework
* @subpackage database
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
* @note This file has been modified by the Joomla! Project and no longer reflects the original work of its author.
*
* This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning FOFTable objects
* instead of plain stdClass objects
*/
// Protect from unauthorized access
defined('FOF_INCLUDED') or die;
/**
* Query Building Class.
*
* @since 11.1
*
* @method string q() q($text, $escape = true) Alias for quote method
* @method string qn() qn($name, $as = null) Alias for quoteName method
* @method string e() e($text, $extra = false) Alias for escape method
* @property-read FOFDatabaseQueryElement $type
* @property-read FOFDatabaseQueryElement $select
* @property-read FOFDatabaseQueryElement $group
* @property-read FOFDatabaseQueryElement $having
*/
abstract class FOFDatabaseQuery
{
/**
* @var FOFDatabaseDriver The database driver.
* @since 11.1
*/
protected $db = null;
/**
* @var string The SQL query (if a direct query string was provided).
* @since 12.1
*/
protected $sql = null;
/**
* @var string The query type.
* @since 11.1
*/
protected $type = '';
/**
* @var FOFDatabaseQueryElement The query element for a generic query (type = null).
* @since 11.1
*/
protected $element = null;
/**
* @var FOFDatabaseQueryElement The select element.
* @since 11.1
*/
protected $select = null;
/**
* @var FOFDatabaseQueryElement The delete element.
* @since 11.1
*/
protected $delete = null;
/**
* @var FOFDatabaseQueryElement The update element.
* @since 11.1
*/
protected $update = null;
/**
* @var FOFDatabaseQueryElement The insert element.
* @since 11.1
*/
protected $insert = null;
/**
* @var FOFDatabaseQueryElement The from element.
* @since 11.1
*/
protected $from = null;
/**
* @var FOFDatabaseQueryElement The join element.
* @since 11.1
*/
protected $join = null;
/**
* @var FOFDatabaseQueryElement The set element.
* @since 11.1
*/
protected $set = null;
/**
* @var FOFDatabaseQueryElement The where element.
* @since 11.1
*/
protected $where = null;
/**
* @var FOFDatabaseQueryElement The group by element.
* @since 11.1
*/
protected $group = null;
/**
* @var FOFDatabaseQueryElement The having element.
* @since 11.1
*/
protected $having = null;
/**
* @var FOFDatabaseQueryElement The column list for an INSERT statement.
* @since 11.1
*/
protected $columns = null;
/**
* @var FOFDatabaseQueryElement The values list for an INSERT statement.
* @since 11.1
*/
protected $values = null;
/**
* @var FOFDatabaseQueryElement The order element.
* @since 11.1
*/
protected $order = null;
/**
* @var object The auto increment insert field element.
* @since 11.1
*/
protected $autoIncrementField = null;
/**
* @var FOFDatabaseQueryElement The call element.
* @since 12.1
*/
protected $call = null;
/**
* @var FOFDatabaseQueryElement The exec element.
* @since 12.1
*/
protected $exec = null;
/**
* @var FOFDatabaseQueryElement The union element.
* @since 12.1
*/
protected $union = null;
/**
* @var FOFDatabaseQueryElement The unionAll element.
* @since 13.1
*/
protected $unionAll = null;
/**
* Magic method to provide method alias support for quote() and quoteName().
*
* @param string $method The called method.
* @param array $args The array of arguments passed to the method.
*
* @return string The aliased method's return value or null.
*
* @since 11.1
*/
public function __call($method, $args)
{
if (empty($args))
{
return;
}
switch ($method)
{
case 'q':
return $this->quote($args[0], isset($args[1]) ? $args[1] : true);
break;
case 'qn':
return $this->quoteName($args[0], isset($args[1]) ? $args[1] : null);
break;
case 'e':
return $this->escape($args[0], isset($args[1]) ? $args[1] : false);
break;
}
}
/**
* Class constructor.
*
* @param FOFDatabaseDriver $db The database driver.
*
* @since 11.1
*/
public function __construct(FOFDatabaseDriver $db = null)
{
$this->db = $db;
}
/**
* Magic function to convert the query to a string.
*
* @return string The completed query.
*
* @since 11.1
*/
public function __toString()
{
$query = '';
if ($this->sql)
{
return $this->sql;
}
switch ($this->type)
{
case 'element':
$query .= (string) $this->element;
break;
case 'select':
$query .= (string) $this->select;
$query .= (string) $this->from;
if ($this->join)
{
// Special case for joins
foreach ($this->join as $join)
{
$query .= (string) $join;
}
}
if ($this->where)
{
$query .= (string) $this->where;
}
if ($this->group)
{
$query .= (string) $this->group;
}
if ($this->having)
{
$query .= (string) $this->having;
}
if ($this->order)
{
$query .= (string) $this->order;
}
if ($this->union)
{
$query .= (string) $this->union;
}
break;
case 'delete':
$query .= (string) $this->delete;
$query .= (string) $this->from;
if ($this->join)
{
// Special case for joins
foreach ($this->join as $join)
{
$query .= (string) $join;
}
}
if ($this->where)
{
$query .= (string) $this->where;
}
if ($this->order)
{
$query .= (string) $this->order;
}
break;
case 'update':
$query .= (string) $this->update;
if ($this->join)
{
// Special case for joins
foreach ($this->join as $join)
{
$query .= (string) $join;
}
}
$query .= (string) $this->set;
if ($this->where)
{
$query .= (string) $this->where;
}
if ($this->order)
{
$query .= (string) $this->order;
}
break;
case 'insert':
$query .= (string) $this->insert;
// Set method
if ($this->set)
{
$query .= (string) $this->set;
}
// Columns-Values method
elseif ($this->values)
{
if ($this->columns)
{
$query .= (string) $this->columns;
}
$elements = $this->values->getElements();
if (!($elements[0] instanceof $this))
{
$query .= ' VALUES ';
}
$query .= (string) $this->values;
}
break;
case 'call':
$query .= (string) $this->call;
break;
case 'exec':
$query .= (string) $this->exec;
break;
}
if ($this instanceof FOFDatabaseQueryLimitable)
{
$query = $this->processLimit($query, $this->limit, $this->offset);
}
return $query;
}
/**
* Magic function to get protected variable value
*
* @param string $name The name of the variable.
*
* @return mixed
*
* @since 11.1
*/
public function __get($name)
{
return isset($this->$name) ? $this->$name : null;
}
/**
* Add a single column, or array of columns to the CALL clause of the query.
*
* Note that you must not mix insert, update, delete and select method calls when building a query.
* The call method can, however, be called multiple times in the same query.
*
* Usage:
* $query->call('a.*')->call('b.id');
* $query->call(array('a.*', 'b.id'));
*
* @param mixed $columns A string or an array of field names.
*
* @return FOFDatabaseQuery Returns this object to allow chaining.
*
* @since 12.1
*/
public function call($columns)
{
$this->type = 'call';
if (is_null($this->call))
{
$this->call = new FOFDatabaseQueryElement('CALL', $columns);
}
else
{
$this->call->append($columns);
}
return $this;
}
/**
* Casts a value to a char.
*
* Ensure that the value is properly quoted before passing to the method.
*
* Usage:
* $query->select($query->castAsChar('a'));
*
* @param string $value The value to cast as a char.
*
* @return string Returns the cast value.
*
* @since 11.1
*/
public function castAsChar($value)
{
return $value;
}
/**
* Gets the number of characters in a string.
*
* Note, use 'length' to find the number of bytes in a string.
*
* Usage:
* $query->select($query->charLength('a'));
*
* @param string $field A value.
* @param string $operator Comparison operator between charLength integer value and $condition
* @param string $condition Integer value to compare charLength with.
*
* @return string The required char length call.
*
* @since 11.1
*/
public function charLength($field, $operator = null, $condition = null)
{
return 'CHAR_LENGTH(' . $field . ')' . (isset($operator) && isset($condition) ? ' ' . $operator . ' ' . $condition : '');
}
/**
* Clear data from the query or a specific clause of the query.
*
* @param string $clause Optionally, the name of the clause to clear, or nothing to clear the whole query.
*
* @return FOFDatabaseQuery Returns this object to allow chaining.
*
* @since 11.1
*/
public function clear($clause = null)
{
$this->sql = null;
switch ($clause)
{
case 'select':
$this->select = null;
$this->type = null;
break;
case 'delete':
$this->delete = null;
$this->type = null;
break;
case 'update':
$this->update = null;
$this->type = null;
break;
case 'insert':
$this->insert = null;
$this->type = null;
$this->autoIncrementField = null;
break;
case 'from':
$this->from = null;
break;
case 'join':
$this->join = null;
break;
case 'set':
$this->set = null;
break;
case 'where':
$this->where = null;
break;
case 'group':
$this->group = null;
break;
case 'having':
$this->having = null;
break;
case 'order':
$this->order = null;
break;
case 'columns':
$this->columns = null;
break;
case 'values':
$this->values = null;
break;
case 'exec':
$this->exec = null;
$this->type = null;
break;
case 'call':
$this->call = null;
$this->type = null;
break;
case 'limit':
$this->offset = 0;
$this->limit = 0;
break;
case 'offset':
$this->offset = 0;
break;
case 'union':
$this->union = null;
break;
case 'unionAll':
$this->unionAll = null;
break;
default:
$this->type = null;
$this->select = null;
$this->delete = null;
$this->update = null;
$this->insert = null;
$this->from = null;
$this->join = null;
$this->set = null;
$this->where = null;
$this->group = null;
$this->having = null;
$this->order = null;
$this->columns = null;
$this->values = null;
$this->autoIncrementField = null;
$this->exec = null;
$this->call = null;
$this->union = null;
$this->unionAll = null;
$this->offset = 0;
$this->limit = 0;
break;
}
return $this;
}
/**
* Adds a column, or array of column names that would be used for an INSERT INTO statement.
*
* @param mixed $columns A column name, or array of column names.
*
* @return FOFDatabaseQuery Returns this object to allow chaining.
*
* @since 11.1
*/
public function columns($columns)
{
if (is_null($this->columns))
{
$this->columns = new FOFDatabaseQueryElement('()', $columns);
}
else
{
$this->columns->append($columns);
}
return $this;
}
/**
* Concatenates an array of column names or values.
*
* Usage:
* $query->select($query->concatenate(array('a', 'b')));
*
* @param array $values An array of values to concatenate.
* @param string $separator As separator to place between each value.
*
* @return string The concatenated values.
*
* @since 11.1
*/
public function concatenate($values, $separator = null)
{
if ($separator)
{
return 'CONCATENATE(' . implode(' || ' . $this->quote($separator) . ' || ', $values) . ')';
}
else
{
return 'CONCATENATE(' . implode(' || ', $values) . ')';
}
}
/**
* Gets the current date and time.
*
* Usage:
* $query->where('published_up < '.$query->currentTimestamp());
*
* @return string
*
* @since 11.1
*/
public function currentTimestamp()
{
return 'CURRENT_TIMESTAMP()';
}
/**
* Returns a PHP date() function compliant date format for the database driver.
*
* This method is provided for use where the query object is passed to a function for modification.
* If you have direct access to the database object, it is recommended you use the getDateFormat method directly.
*
* @return string The format string.
*
* @since 11.1
*/
public function dateFormat()
{
if (!($this->db instanceof FOFDatabaseDriver))
{
throw new RuntimeException('JLIB_DATABASE_ERROR_INVALID_DB_OBJECT');
}
return $this->db->getDateFormat();
}
/**
* Creates a formatted dump of the query for debugging purposes.
*
* Usage:
* echo $query->dump();
*
* @return string
*
* @since 11.3
*/
public function dump()
{
return '<pre class="FOFDatabasequery">' . str_replace('#__', $this->db->getPrefix(), $this) . '</pre>';
}
/**
* Add a table name to the DELETE clause of the query.
*
* Note that you must not mix insert, update, delete and select method calls when building a query.
*
* Usage:
* $query->delete('#__a')->where('id = 1');
*
* @param string $table The name of the table to delete from.
*
* @return FOFDatabaseQuery Returns this object to allow chaining.
*
* @since 11.1
*/
public function delete($table = null)
{
$this->type = 'delete';
$this->delete = new FOFDatabaseQueryElement('DELETE', null);
if (!empty($table))
{
$this->from($table);
}
return $this;
}
/**
* Method to escape a string for usage in an SQL statement.
*
* This method is provided for use where the query object is passed to a function for modification.
* If you have direct access to the database object, it is recommended you use the escape method directly.
*
* Note that 'e' is an alias for this method as it is in FOFDatabaseDriver.
*
* @param string $text The string to be escaped.
* @param boolean $extra Optional parameter to provide extra escaping.
*
* @return string The escaped string.
*
* @since 11.1
* @throws RuntimeException if the internal db property is not a valid object.
*/
public function escape($text, $extra = false)
{
if (!($this->db instanceof FOFDatabaseDriver))
{
throw new RuntimeException('JLIB_DATABASE_ERROR_INVALID_DB_OBJECT');
}
return $this->db->escape($text, $extra);
}
/**
* Add a single column, or array of columns to the EXEC clause of the query.
*
* Note that you must not mix insert, update, delete and select method calls when building a query.
* The exec method can, however, be called multiple times in the same query.
*
* Usage:
* $query->exec('a.*')->exec('b.id');
* $query->exec(array('a.*', 'b.id'));
*
* @param mixed $columns A string or an array of field names.
*
* @return FOFDatabaseQuery Returns this object to allow chaining.
*
* @since 12.1
*/
public function exec($columns)
{
$this->type = 'exec';
if (is_null($this->exec))
{
$this->exec = new FOFDatabaseQueryElement('EXEC', $columns);
}
else
{
$this->exec->append($columns);
}
return $this;
}
/**
* Add a table to the FROM clause of the query.
*
* Note that while an array of tables can be provided, it is recommended you use explicit joins.
*
* Usage:
* $query->select('*')->from('#__a');
*
* @param mixed $tables A string or array of table names.
* This can be a FOFDatabaseQuery object (or a child of it) when used
* as a subquery in FROM clause along with a value for $subQueryAlias.
* @param string $subQueryAlias Alias used when $tables is a FOFDatabaseQuery.
*
* @return FOFDatabaseQuery Returns this object to allow chaining.
*
* @throws RuntimeException
*
* @since 11.1
*/
public function from($tables, $subQueryAlias = null)
{
if (is_null($this->from))
{
if ($tables instanceof $this)
{
if (is_null($subQueryAlias))
{
throw new RuntimeException('JLIB_DATABASE_ERROR_NULL_SUBQUERY_ALIAS');
}
$tables = '( ' . (string) $tables . ' ) AS ' . $this->quoteName($subQueryAlias);
}
$this->from = new FOFDatabaseQueryElement('FROM', $tables);
}
else
{
$this->from->append($tables);
}
return $this;
}
/**
* Used to get a string to extract year from date column.
*
* Usage:
* $query->select($query->year($query->quoteName('dateColumn')));
*
* @param string $date Date column containing year to be extracted.
*
* @return string Returns string to extract year from a date.
*
* @since 12.1
*/
public function year($date)
{
return 'YEAR(' . $date . ')';
}
/**
* Used to get a string to extract month from date column.
*
* Usage:
* $query->select($query->month($query->quoteName('dateColumn')));
*
* @param string $date Date column containing month to be extracted.
*
* @return string Returns string to extract month from a date.
*
* @since 12.1
*/
public function month($date)
{
return 'MONTH(' . $date . ')';
}
/**
* Used to get a string to extract day from date column.
*
* Usage:
* $query->select($query->day($query->quoteName('dateColumn')));
*
* @param string $date Date column containing day to be extracted.
*
* @return string Returns string to extract day from a date.
*
* @since 12.1
*/
public function day($date)
{
return 'DAY(' . $date . ')';
}
/**
* Used to get a string to extract hour from date column.
*
* Usage:
* $query->select($query->hour($query->quoteName('dateColumn')));
*
* @param string $date Date column containing hour to be extracted.
*
* @return string Returns string to extract hour from a date.
*
* @since 12.1
*/
public function hour($date)
{
return 'HOUR(' . $date . ')';
}
/**
* Used to get a string to extract minute from date column.
*
* Usage:
* $query->select($query->minute($query->quoteName('dateColumn')));
*
* @param string $date Date column containing minute to be extracted.
*
* @return string Returns string to extract minute from a date.
*
* @since 12.1
*/
public function minute($date)
{
return 'MINUTE(' . $date . ')';
}
/**
* Used to get a string to extract seconds from date column.
*
* Usage:
* $query->select($query->second($query->quoteName('dateColumn')));
*
* @param string $date Date column containing second to be extracted.
*
* @return string Returns string to extract second from a date.
*
* @since 12.1
*/
public function second($date)
{
return 'SECOND(' . $date . ')';
}
/**
* Add a grouping column to the GROUP clause of the query.
*
* Usage:
* $query->group('id');
*
* @param mixed $columns A string or array of ordering columns.
*
* @return FOFDatabaseQuery Returns this object to allow chaining.
*
* @since 11.1
*/
public function group($columns)
{
if (is_null($this->group))
{
$this->group = new FOFDatabaseQueryElement('GROUP BY', $columns);
}
else
{
$this->group->append($columns);
}
return $this;
}
/**
* A conditions to the HAVING clause of the query.
*
* Usage:
* $query->group('id')->having('COUNT(id) > 5');
*
* @param mixed $conditions A string or array of columns.
* @param string $glue The glue by which to join the conditions. Defaults to AND.
*
* @return FOFDatabaseQuery Returns this object to allow chaining.
*
* @since 11.1
*/
public function having($conditions, $glue = 'AND')
{
if (is_null($this->having))
{
$glue = strtoupper($glue);
$this->having = new FOFDatabaseQueryElement('HAVING', $conditions, " $glue ");
}
else
{
$this->having->append($conditions);
}
return $this;
}
/**
* Add an INNER JOIN clause to the query.
*
* Usage:
* $query->innerJoin('b ON b.id = a.id')->innerJoin('c ON c.id = b.id');
*
* @param string $condition The join condition.
*
* @return FOFDatabaseQuery Returns this object to allow chaining.
*
* @since 11.1
*/
public function innerJoin($condition)
{
$this->join('INNER', $condition);
return $this;
}
/**
* Add a table name to the INSERT clause of the query.
*
* Note that you must not mix insert, update, delete and select method calls when building a query.
*
* Usage:
* $query->insert('#__a')->set('id = 1');
* $query->insert('#__a')->columns('id, title')->values('1,2')->values('3,4');
* $query->insert('#__a')->columns('id, title')->values(array('1,2', '3,4'));
*
* @param mixed $table The name of the table to insert data into.
* @param boolean $incrementField The name of the field to auto increment.
*
* @return FOFDatabaseQuery Returns this object to allow chaining.
*
* @since 11.1
*/
public function insert($table, $incrementField=false)
{
$this->type = 'insert';
$this->insert = new FOFDatabaseQueryElement('INSERT INTO', $table);
$this->autoIncrementField = $incrementField;
return $this;
}
/**
* Add a JOIN clause to the query.
*
* Usage:
* $query->join('INNER', 'b ON b.id = a.id);
*
* @param string $type The type of join. This string is prepended to the JOIN keyword.
* @param string $conditions A string or array of conditions.
*
* @return FOFDatabaseQuery Returns this object to allow chaining.
*
* @since 11.1
*/
public function join($type, $conditions)
{
if (is_null($this->join))
{
$this->join = array();
}
$this->join[] = new FOFDatabaseQueryElement(strtoupper($type) . ' JOIN', $conditions);
return $this;
}
/**
* Add a LEFT JOIN clause to the query.
*
* Usage:
* $query->leftJoin('b ON b.id = a.id')->leftJoin('c ON c.id = b.id');
*
* @param string $condition The join condition.
*
* @return FOFDatabaseQuery Returns this object to allow chaining.
*
* @since 11.1
*/
public function leftJoin($condition)
{
$this->join('LEFT', $condition);
return $this;
}
/**
* Get the length of a string in bytes.
*
* Note, use 'charLength' to find the number of characters in a string.
*
* Usage:
* query->where($query->length('a').' > 3');
*
* @param string $value The string to measure.
*
* @return int
*
* @since 11.1
*/
public function length($value)
{
return 'LENGTH(' . $value . ')';
}
/**
* Get the null or zero representation of a timestamp for the database driver.
*
* This method is provided for use where the query object is passed to a function for modification.
* If you have direct access to the database object, it is recommended you use the nullDate method directly.
*
* Usage:
* $query->where('modified_date <> '.$query->nullDate());
*
* @param boolean $quoted Optionally wraps the null date in database quotes (true by default).
*
* @return string Null or zero representation of a timestamp.
*
* @since 11.1
*/
public function nullDate($quoted = true)
{
if (!($this->db instanceof FOFDatabaseDriver))
{
throw new RuntimeException('JLIB_DATABASE_ERROR_INVALID_DB_OBJECT');
}
$result = $this->db->getNullDate($quoted);
if ($quoted)
{
return $this->db->quote($result);
}
return $result;
}
/**
* Add a ordering column to the ORDER clause of the query.
*
* Usage:
* $query->order('foo')->order('bar');
* $query->order(array('foo','bar'));
*
* @param mixed $columns A string or array of ordering columns.
*
* @return FOFDatabaseQuery Returns this object to allow chaining.
*
* @since 11.1
*/
public function order($columns)
{
if (is_null($this->order))
{
$this->order = new FOFDatabaseQueryElement('ORDER BY', $columns);
}
else
{
$this->order->append($columns);
}
return $this;
}
/**
* Add an OUTER JOIN clause to the query.
*
* Usage:
* $query->outerJoin('b ON b.id = a.id')->outerJoin('c ON c.id = b.id');
*
* @param string $condition The join condition.
*
* @return FOFDatabaseQuery Returns this object to allow chaining.
*
* @since 11.1
*/
public function outerJoin($condition)
{
$this->join('OUTER', $condition);
return $this;
}
/**
* Method to quote and optionally escape a string to database requirements for insertion into the database.
*
* This method is provided for use where the query object is passed to a function for modification.
* If you have direct access to the database object, it is recommended you use the quote method directly.
*
* Note that 'q' is an alias for this method as it is in FOFDatabaseDriver.
*
* Usage:
* $query->quote('fulltext');
* $query->q('fulltext');
* $query->q(array('option', 'fulltext'));
*
* @param mixed $text A string or an array of strings to quote.
* @param boolean $escape True to escape the string, false to leave it unchanged.
*
* @return string The quoted input string.
*
* @since 11.1
* @throws RuntimeException if the internal db property is not a valid object.
*/
public function quote($text, $escape = true)
{
if (!($this->db instanceof FOFDatabaseDriver))
{
throw new RuntimeException('JLIB_DATABASE_ERROR_INVALID_DB_OBJECT');
}
return $this->db->quote($text, $escape);
}
/**
* Wrap an SQL statement identifier name such as column, table or database names in quotes to prevent injection
* risks and reserved word conflicts.
*
* This method is provided for use where the query object is passed to a function for modification.
* If you have direct access to the database object, it is recommended you use the quoteName method directly.
*
* Note that 'qn' is an alias for this method as it is in FOFDatabaseDriver.
*
* Usage:
* $query->quoteName('#__a');
* $query->qn('#__a');
*
* @param mixed $name The identifier name to wrap in quotes, or an array of identifier names to wrap in quotes.
* Each type supports dot-notation name.
* @param mixed $as The AS query part associated to $name. It can be string or array, in latter case it has to be
* same length of $name; if is null there will not be any AS part for string or array element.
*
* @return mixed The quote wrapped name, same type of $name.
*
* @since 11.1
* @throws RuntimeException if the internal db property is not a valid object.
*/
public function quoteName($name, $as = null)
{
if (!($this->db instanceof FOFDatabaseDriver))
{
throw new RuntimeException('JLIB_DATABASE_ERROR_INVALID_DB_OBJECT');
}
return $this->db->quoteName($name, $as);
}
/**
* Add a RIGHT JOIN clause to the query.
*
* Usage:
* $query->rightJoin('b ON b.id = a.id')->rightJoin('c ON c.id = b.id');
*
* @param string $condition The join condition.
*
* @return FOFDatabaseQuery Returns this object to allow chaining.
*
* @since 11.1
*/
public function rightJoin($condition)
{
$this->join('RIGHT', $condition);
return $this;
}
/**
* Add a single column, or array of columns to the SELECT clause of the query.
*
* Note that you must not mix insert, update, delete and select method calls when building a query.
* The select method can, however, be called multiple times in the same query.
*
* Usage:
* $query->select('a.*')->select('b.id');
* $query->select(array('a.*', 'b.id'));
*
* @param mixed $columns A string or an array of field names.
*
* @return FOFDatabaseQuery Returns this object to allow chaining.
*
* @since 11.1
*/
public function select($columns)
{
$this->type = 'select';
if (is_null($this->select))
{
$this->select = new FOFDatabaseQueryElement('SELECT', $columns);
}
else
{
$this->select->append($columns);
}
return $this;
}
/**
* Add a single condition string, or an array of strings to the SET clause of the query.
*
* Usage:
* $query->set('a = 1')->set('b = 2');
* $query->set(array('a = 1', 'b = 2');
*
* @param mixed $conditions A string or array of string conditions.
* @param string $glue The glue by which to join the condition strings. Defaults to ,.
* Note that the glue is set on first use and cannot be changed.
*
* @return FOFDatabaseQuery Returns this object to allow chaining.
*
* @since 11.1
*/
public function set($conditions, $glue = ',')
{
if (is_null($this->set))
{
$glue = strtoupper($glue);
$this->set = new FOFDatabaseQueryElement('SET', $conditions, "\n\t$glue ");
}
else
{
$this->set->append($conditions);
}
return $this;
}
/**
* Allows a direct query to be provided to the database
* driver's setQuery() method, but still allow queries
* to have bounded variables.
*
* Usage:
* $query->setQuery('select * from #__users');
*
* @param mixed $sql An SQL Query
*
* @return FOFDatabaseQuery Returns this object to allow chaining.
*
* @since 12.1
*/
public function setQuery($sql)
{
$this->sql = $sql;
return $this;
}
/**
* Add a table name to the UPDATE clause of the query.
*
* Note that you must not mix insert, update, delete and select method calls when building a query.
*
* Usage:
* $query->update('#__foo')->set(...);
*
* @param string $table A table to update.
*
* @return FOFDatabaseQuery Returns this object to allow chaining.
*
* @since 11.1
*/
public function update($table)
{
$this->type = 'update';
$this->update = new FOFDatabaseQueryElement('UPDATE', $table);
return $this;
}
/**
* Adds a tuple, or array of tuples that would be used as values for an INSERT INTO statement.
*
* Usage:
* $query->values('1,2,3')->values('4,5,6');
* $query->values(array('1,2,3', '4,5,6'));
*
* @param string $values A single tuple, or array of tuples.
*
* @return FOFDatabaseQuery Returns this object to allow chaining.
*
* @since 11.1
*/
public function values($values)
{
if (is_null($this->values))
{
$this->values = new FOFDatabaseQueryElement('()', $values, '),(');
}
else
{
$this->values->append($values);
}
return $this;
}
/**
* Add a single condition, or an array of conditions to the WHERE clause of the query.
*
* Usage:
* $query->where('a = 1')->where('b = 2');
* $query->where(array('a = 1', 'b = 2'));
*
* @param mixed $conditions A string or array of where conditions.
* @param string $glue The glue by which to join the conditions. Defaults to AND.
* Note that the glue is set on first use and cannot be changed.
*
* @return FOFDatabaseQuery Returns this object to allow chaining.
*
* @since 11.1
*/
public function where($conditions, $glue = 'AND')
{
if (is_null($this->where))
{
$glue = strtoupper($glue);
$this->where = new FOFDatabaseQueryElement('WHERE', $conditions, " $glue ");
}
else
{
$this->where->append($conditions);
}
return $this;
}
/**
* Method to provide deep copy support to nested objects and
* arrays when cloning.
*
* @return void
*
* @since 11.3
*/
public function __clone()
{
foreach ($this as $k => $v)
{
if ($k === 'db')
{
continue;
}
if (is_object($v) || is_array($v))
{
$this->{$k} = unserialize(serialize($v));
}
}
}
/**
* Add a query to UNION with the current query.
* Multiple unions each require separate statements and create an array of unions.
*
* Usage (the $query base query MUST be a select query):
* $query->union('SELECT name FROM #__foo')
* $query->union('SELECT name FROM #__foo', true)
* $query->union(array('SELECT name FROM #__foo','SELECT name FROM #__bar'))
* $query->union($query2)->union($query3)
* $query->union(array($query2, $query3))
*
* @param mixed $query The FOFDatabaseQuery object or string to union.
* @param boolean $distinct True to only return distinct rows from the union.
* @param string $glue The glue by which to join the conditions.
*
* @return mixed The FOFDatabaseQuery object on success or boolean false on failure.
*
* @see http://dev.mysql.com/doc/refman/5.0/en/union.html
*
* @since 12.1
*/
public function union($query, $distinct = false, $glue = '')
{
// Set up the DISTINCT flag, the name with parentheses, and the glue.
if ($distinct)
{
$name = 'UNION DISTINCT ()';
$glue = ')' . PHP_EOL . 'UNION DISTINCT (';
}
else
{
$glue = ')' . PHP_EOL . 'UNION (';
$name = 'UNION ()';
}
// Get the FOFDatabaseQueryElement if it does not exist
if (is_null($this->union))
{
$this->union = new FOFDatabaseQueryElement($name, $query, "$glue");
}
// Otherwise append the second UNION.
else
{
$this->union->append($query);
}
return $this;
}
/**
* Add a query to UNION DISTINCT with the current query. Simply a proxy to union with the DISTINCT keyword.
*
* Usage:
* $query->unionDistinct('SELECT name FROM #__foo')
*
* @param mixed $query The FOFDatabaseQuery object or string to union.
* @param string $glue The glue by which to join the conditions.
*
* @return mixed The FOFDatabaseQuery object on success or boolean false on failure.
*
* @see union
*
* @since 12.1
*/
public function unionDistinct($query, $glue = '')
{
$distinct = true;
// Apply the distinct flag to the union.
return $this->union($query, $distinct, $glue);
}
/**
* Find and replace sprintf-like tokens in a format string.
* Each token takes one of the following forms:
* %% - A literal percent character.
* %[t] - Where [t] is a type specifier.
* %[n]$[x] - Where [n] is an argument specifier and [t] is a type specifier.
*
* Types:
* a - Numeric: Replacement text is coerced to a numeric type but not quoted or escaped.
* e - Escape: Replacement text is passed to $this->escape().
* E - Escape (extra): Replacement text is passed to $this->escape() with true as the second argument.
* n - Name Quote: Replacement text is passed to $this->quoteName().
* q - Quote: Replacement text is passed to $this->quote().
* Q - Quote (no escape): Replacement text is passed to $this->quote() with false as the second argument.
* r - Raw: Replacement text is used as-is. (Be careful)
*
* Date Types:
* - Replacement text automatically quoted (use uppercase for Name Quote).
* - Replacement text should be a string in date format or name of a date column.
* y/Y - Year
* m/M - Month
* d/D - Day
* h/H - Hour
* i/I - Minute
* s/S - Second
*
* Invariable Types:
* - Takes no argument.
* - Argument index not incremented.
* t - Replacement text is the result of $this->currentTimestamp().
* z - Replacement text is the result of $this->nullDate(false).
* Z - Replacement text is the result of $this->nullDate(true).
*
* Usage:
* $query->format('SELECT %1$n FROM %2$n WHERE %3$n = %4$a', 'foo', '#__foo', 'bar', 1);
* Returns: SELECT `foo` FROM `#__foo` WHERE `bar` = 1
*
* Notes:
* The argument specifier is optional but recommended for clarity.
* The argument index used for unspecified tokens is incremented only when used.
*
* @param string $format The formatting string.
*
* @return string Returns a string produced according to the formatting string.
*
* @since 12.3
*/
public function format($format)
{
$query = $this;
$args = array_slice(func_get_args(), 1);
array_unshift($args, null);
$i = 1;
$func = function ($match) use ($query, $args, &$i)
{
if (isset($match[6]) && $match[6] == '%')
{
return '%';
}
// No argument required, do not increment the argument index.
switch ($match[5])
{
case 't':
return $query->currentTimestamp();
break;
case 'z':
return $query->nullDate(false);
break;
case 'Z':
return $query->nullDate(true);
break;
}
// Increment the argument index only if argument specifier not provided.
$index = is_numeric($match[4]) ? (int) $match[4] : $i++;
if (!$index || !isset($args[$index]))
{
// TODO - What to do? sprintf() throws a Warning in these cases.
$replacement = '';
}
else
{
$replacement = $args[$index];
}
switch ($match[5])
{
case 'a':
return 0 + $replacement;
break;
case 'e':
return $query->escape($replacement);
break;
case 'E':
return $query->escape($replacement, true);
break;
case 'n':
return $query->quoteName($replacement);
break;
case 'q':
return $query->quote($replacement);
break;
case 'Q':
return $query->quote($replacement, false);
break;
case 'r':
return $replacement;
break;
// Dates
case 'y':
return $query->year($query->quote($replacement));
break;
case 'Y':
return $query->year($query->quoteName($replacement));
break;
case 'm':
return $query->month($query->quote($replacement));
break;
case 'M':
return $query->month($query->quoteName($replacement));
break;
case 'd':
return $query->day($query->quote($replacement));
break;
case 'D':
return $query->day($query->quoteName($replacement));
break;
case 'h':
return $query->hour($query->quote($replacement));
break;
case 'H':
return $query->hour($query->quoteName($replacement));
break;
case 'i':
return $query->minute($query->quote($replacement));
break;
case 'I':
return $query->minute($query->quoteName($replacement));
break;
case 's':
return $query->second($query->quote($replacement));
break;
case 'S':
return $query->second($query->quoteName($replacement));
break;
}
return '';
};
/**
* Regexp to find an replace all tokens.
* Matched fields:
* 0: Full token
* 1: Everything following '%'
* 2: Everything following '%' unless '%'
* 3: Argument specifier and '$'
* 4: Argument specifier
* 5: Type specifier
* 6: '%' if full token is '%%'
*/
return preg_replace_callback('#%(((([\d]+)\$)?([aeEnqQryYmMdDhHiIsStzZ]))|(%))#', $func, $format);
}
/**
* Add to the current date and time.
* Usage:
* $query->select($query->dateAdd());
* Prefixing the interval with a - (negative sign) will cause subtraction to be used.
* Note: Not all drivers support all units.
*
* @param datetime $date The date to add to. May be date or datetime
* @param string $interval The string representation of the appropriate number of units
* @param string $datePart The part of the date to perform the addition on
*
* @return string The string with the appropriate sql for addition of dates
*
* @link http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add
* @since 13.1
*/
public function dateAdd($date, $interval, $datePart)
{
return trim("DATE_ADD('" . $date . "', INTERVAL " . $interval . ' ' . $datePart . ')');
}
/**
* Add a query to UNION ALL with the current query.
* Multiple unions each require separate statements and create an array of unions.
*
* Usage:
* $query->union('SELECT name FROM #__foo')
* $query->union(array('SELECT name FROM #__foo','SELECT name FROM #__bar'))
*
* @param mixed $query The FOFDatabaseQuery object or string to union.
* @param boolean $distinct Not used - ignored.
* @param string $glue Not used - ignored.
*
* @return mixed The FOFDatabaseQuery object on success or boolean false on failure.
*
* @see union
*
* @since 13.1
*/
public function unionAll($query, $distinct = false, $glue = '')
{
$glue = ')' . PHP_EOL . 'UNION ALL (';
$name = 'UNION ALL ()';
// Get the FOFDatabaseQueryElement if it does not exist
if (is_null($this->unionAll))
{
$this->unionAll = new FOFDatabaseQueryElement($name, $query, "$glue");
}
// Otherwise append the second UNION.
else
{
$this->unionAll->append($query);
}
return $this;
}
}