-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
BaseBuilder.php
3514 lines (2955 loc) · 97 KB
/
BaseBuilder.php
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
<?php
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Database;
use Closure;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Database\Exceptions\DataException;
use CodeIgniter\Traits\ConditionalTrait;
use InvalidArgumentException;
/**
* Class BaseBuilder
*
* Provides the core Query Builder methods.
* Database-specific Builders might need to override
* certain methods to make them work.
*/
class BaseBuilder
{
use ConditionalTrait;
/**
* Reset DELETE data flag
*
* @var bool
*/
protected $resetDeleteData = false;
/**
* QB SELECT data
*
* @var array
*/
protected $QBSelect = [];
/**
* QB DISTINCT flag
*
* @var bool
*/
protected $QBDistinct = false;
/**
* QB FROM data
*
* @var array
*/
protected $QBFrom = [];
/**
* QB JOIN data
*
* @var array
*/
protected $QBJoin = [];
/**
* QB WHERE data
*
* @var array
*/
protected $QBWhere = [];
/**
* QB GROUP BY data
*
* @var array
*/
public $QBGroupBy = [];
/**
* QB HAVING data
*
* @var array
*/
protected $QBHaving = [];
/**
* QB keys
* list of column names.
*
* @var list<string>
*/
protected $QBKeys = [];
/**
* QB LIMIT data
*
* @var bool|int
*/
protected $QBLimit = false;
/**
* QB OFFSET data
*
* @var bool|int
*/
protected $QBOffset = false;
/**
* QB ORDER BY data
*
* @var array|string|null
*/
public $QBOrderBy = [];
/**
* QB UNION data
*
* @var array<string>
*/
protected array $QBUnion = [];
/**
* QB NO ESCAPE data
*
* @var array
*/
public $QBNoEscape = [];
/**
* QB data sets
*
* @var array<string, string>|list<list<int|string>>
*/
protected $QBSet = [];
/**
* QB WHERE group started flag
*
* @var bool
*/
protected $QBWhereGroupStarted = false;
/**
* QB WHERE group count
*
* @var int
*/
protected $QBWhereGroupCount = 0;
/**
* Ignore data that cause certain
* exceptions, for example in case of
* duplicate keys.
*
* @var bool
*/
protected $QBIgnore = false;
/**
* QB Options data
* Holds additional options and data used to render SQL
* and is reset by resetWrite()
*
* @var array{
* updateFieldsAdditional?: array,
* tableIdentity?: string,
* updateFields?: array,
* constraints?: array,
* setQueryAsData?: string,
* sql?: string,
* alias?: string
* }
*/
protected $QBOptions;
/**
* A reference to the database connection.
*
* @var BaseConnection
*/
protected $db;
/**
* Name of the primary table for this instance.
* Tracked separately because $QBFrom gets escaped
* and prefixed.
*
* When $tableName to the constructor has multiple tables,
* the value is empty string.
*
* @var string
*/
protected $tableName;
/**
* ORDER BY random keyword
*
* @var array
*/
protected $randomKeyword = [
'RAND()',
'RAND(%d)',
];
/**
* COUNT string
*
* @used-by CI_DB_driver::count_all()
* @used-by BaseBuilder::count_all_results()
*
* @var string
*/
protected $countString = 'SELECT COUNT(*) AS ';
/**
* Collects the named parameters and
* their values for later binding
* in the Query object.
*
* @var array
*/
protected $binds = [];
/**
* Collects the key count for named parameters
* in the Query object.
*
* @var array
*/
protected $bindsKeyCount = [];
/**
* Some databases, like SQLite, do not by default
* allow limiting of delete clauses.
*
* @var bool
*/
protected $canLimitDeletes = true;
/**
* Some databases do not by default
* allow limit update queries with WHERE.
*
* @var bool
*/
protected $canLimitWhereUpdates = true;
/**
* Specifies which sql statements
* support the ignore option.
*
* @var array
*/
protected $supportedIgnoreStatements = [];
/**
* Builder testing mode status.
*
* @var bool
*/
protected $testMode = false;
/**
* Tables relation types
*
* @var array
*/
protected $joinTypes = [
'LEFT',
'RIGHT',
'OUTER',
'INNER',
'LEFT OUTER',
'RIGHT OUTER',
];
/**
* Strings that determine if a string represents a literal value or a field name
*
* @var string[]
*/
protected $isLiteralStr = [];
/**
* RegExp used to get operators
*
* @var string[]
*/
protected $pregOperators = [];
/**
* Constructor
*
* @param array|string $tableName tablename or tablenames with or without aliases
*
* Examples of $tableName: `mytable`, `jobs j`, `jobs j, users u`, `['jobs j','users u']`
*
* @throws DatabaseException
*/
public function __construct($tableName, ConnectionInterface $db, ?array $options = null)
{
if (empty($tableName)) {
throw new DatabaseException('A table must be specified when creating a new Query Builder.');
}
/**
* @var BaseConnection $db
*/
$this->db = $db;
// If it contains `,`, it has multiple tables
if (is_string($tableName) && strpos($tableName, ',') === false) {
$this->tableName = $tableName; // @TODO remove alias if exists
} else {
$this->tableName = '';
}
$this->from($tableName);
if ($options !== null && $options !== []) {
foreach ($options as $key => $value) {
if (property_exists($this, $key)) {
$this->{$key} = $value;
}
}
}
}
/**
* Returns the current database connection
*
* @return BaseConnection|ConnectionInterface
*/
public function db(): ConnectionInterface
{
return $this->db;
}
/**
* Sets a test mode status.
*
* @return $this
*/
public function testMode(bool $mode = true)
{
$this->testMode = $mode;
return $this;
}
/**
* Gets the name of the primary table.
*/
public function getTable(): string
{
return $this->tableName;
}
/**
* Returns an array of bind values and their
* named parameters for binding in the Query object later.
*/
public function getBinds(): array
{
return $this->binds;
}
/**
* Ignore
*
* Set ignore Flag for next insert,
* update or delete query.
*
* @return $this
*/
public function ignore(bool $ignore = true)
{
$this->QBIgnore = $ignore;
return $this;
}
/**
* Generates the SELECT portion of the query
*
* @param array|RawSql|string $select
*
* @return $this
*/
public function select($select = '*', ?bool $escape = null)
{
// If the escape value was not set, we will base it on the global setting
if (! is_bool($escape)) {
$escape = $this->db->protectIdentifiers;
}
if ($select instanceof RawSql) {
$this->QBSelect[] = $select;
return $this;
}
if (is_string($select)) {
$select = $escape === false ? [$select] : explode(',', $select);
}
foreach ($select as $val) {
$val = trim($val);
if ($val !== '') {
$this->QBSelect[] = $val;
/*
* When doing 'SELECT NULL as field_alias FROM table'
* null gets taken as a field, and therefore escaped
* with backticks.
* This prevents NULL being escaped
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1169
*/
if (mb_stripos(trim($val), 'NULL') === 0) {
$escape = false;
}
$this->QBNoEscape[] = $escape;
}
}
return $this;
}
/**
* Generates a SELECT MAX(field) portion of a query
*
* @return $this
*/
public function selectMax(string $select = '', string $alias = '')
{
return $this->maxMinAvgSum($select, $alias);
}
/**
* Generates a SELECT MIN(field) portion of a query
*
* @return $this
*/
public function selectMin(string $select = '', string $alias = '')
{
return $this->maxMinAvgSum($select, $alias, 'MIN');
}
/**
* Generates a SELECT AVG(field) portion of a query
*
* @return $this
*/
public function selectAvg(string $select = '', string $alias = '')
{
return $this->maxMinAvgSum($select, $alias, 'AVG');
}
/**
* Generates a SELECT SUM(field) portion of a query
*
* @return $this
*/
public function selectSum(string $select = '', string $alias = '')
{
return $this->maxMinAvgSum($select, $alias, 'SUM');
}
/**
* Generates a SELECT COUNT(field) portion of a query
*
* @return $this
*/
public function selectCount(string $select = '', string $alias = '')
{
return $this->maxMinAvgSum($select, $alias, 'COUNT');
}
/**
* Adds a subquery to the selection
*/
public function selectSubquery(BaseBuilder $subquery, string $as): self
{
$this->QBSelect[] = $this->buildSubquery($subquery, true, $as);
return $this;
}
/**
* SELECT [MAX|MIN|AVG|SUM|COUNT]()
*
* @used-by selectMax()
* @used-by selectMin()
* @used-by selectAvg()
* @used-by selectSum()
*
* @return $this
*
* @throws DatabaseException
* @throws DataException
*/
protected function maxMinAvgSum(string $select = '', string $alias = '', string $type = 'MAX')
{
if ($select === '') {
throw DataException::forEmptyInputGiven('Select');
}
if (strpos($select, ',') !== false) {
throw DataException::forInvalidArgument('column name not separated by comma');
}
$type = strtoupper($type);
if (! in_array($type, ['MAX', 'MIN', 'AVG', 'SUM', 'COUNT'], true)) {
throw new DatabaseException('Invalid function type: ' . $type);
}
if ($alias === '') {
$alias = $this->createAliasFromTable(trim($select));
}
$sql = $type . '(' . $this->db->protectIdentifiers(trim($select)) . ') AS ' . $this->db->escapeIdentifiers(trim($alias));
$this->QBSelect[] = $sql;
$this->QBNoEscape[] = null;
return $this;
}
/**
* Determines the alias name based on the table
*/
protected function createAliasFromTable(string $item): string
{
if (strpos($item, '.') !== false) {
$item = explode('.', $item);
return end($item);
}
return $item;
}
/**
* Sets a flag which tells the query string compiler to add DISTINCT
*
* @return $this
*/
public function distinct(bool $val = true)
{
$this->QBDistinct = $val;
return $this;
}
/**
* Generates the FROM portion of the query
*
* @param array|string $from
*
* @return $this
*/
public function from($from, bool $overwrite = false): self
{
if ($overwrite === true) {
$this->QBFrom = [];
$this->db->setAliasedTables([]);
}
foreach ((array) $from as $table) {
if (strpos($table, ',') !== false) {
$this->from(explode(',', $table));
} else {
$table = trim($table);
if ($table === '') {
continue;
}
$this->trackAliases($table);
$this->QBFrom[] = $this->db->protectIdentifiers($table, true, null, false);
}
}
return $this;
}
/**
* @param BaseBuilder $from Expected subquery
* @param string $alias Subquery alias
*
* @return $this
*/
public function fromSubquery(BaseBuilder $from, string $alias): self
{
$table = $this->buildSubquery($from, true, $alias);
$this->db->addTableAlias($alias);
$this->QBFrom[] = $table;
return $this;
}
/**
* Generates the JOIN portion of the query
*
* @param RawSql|string $cond
*
* @return $this
*/
public function join(string $table, $cond, string $type = '', ?bool $escape = null)
{
if ($type !== '') {
$type = strtoupper(trim($type));
if (! in_array($type, $this->joinTypes, true)) {
$type = '';
} else {
$type .= ' ';
}
}
// Extract any aliases that might exist. We use this information
// in the protectIdentifiers to know whether to add a table prefix
$this->trackAliases($table);
if (! is_bool($escape)) {
$escape = $this->db->protectIdentifiers;
}
// Do we want to escape the table name?
if ($escape === true) {
$table = $this->db->protectIdentifiers($table, true, null, false);
}
if ($cond instanceof RawSql) {
$this->QBJoin[] = $type . 'JOIN ' . $table . ' ON ' . $cond;
return $this;
}
if (! $this->hasOperator($cond)) {
$cond = ' USING (' . ($escape ? $this->db->escapeIdentifiers($cond) : $cond) . ')';
} elseif ($escape === false) {
$cond = ' ON ' . $cond;
} else {
// Split multiple conditions
if (preg_match_all('/\sAND\s|\sOR\s/i', $cond, $joints, PREG_OFFSET_CAPTURE)) {
$conditions = [];
$joints = $joints[0];
array_unshift($joints, ['', 0]);
for ($i = count($joints) - 1, $pos = strlen($cond); $i >= 0; $i--) {
$joints[$i][1] += strlen($joints[$i][0]); // offset
$conditions[$i] = substr($cond, $joints[$i][1], $pos - $joints[$i][1]);
$pos = $joints[$i][1] - strlen($joints[$i][0]);
$joints[$i] = $joints[$i][0];
}
ksort($conditions);
} else {
$conditions = [$cond];
$joints = [''];
}
$cond = ' ON ';
foreach ($conditions as $i => $condition) {
$operator = $this->getOperator($condition);
$cond .= $joints[$i];
$cond .= preg_match('/(\(*)?([\[\]\w\.\'-]+)' . preg_quote($operator, '/') . '(.*)/i', $condition, $match) ? $match[1] . $this->db->protectIdentifiers($match[2]) . $operator . $this->db->protectIdentifiers($match[3]) : $condition;
}
}
// Assemble the JOIN statement
$this->QBJoin[] = $type . 'JOIN ' . $table . $cond;
return $this;
}
/**
* Generates the WHERE portion of the query.
* Separates multiple calls with 'AND'.
*
* @param array|RawSql|string $key
* @param mixed $value
*
* @return $this
*/
public function where($key, $value = null, ?bool $escape = null)
{
return $this->whereHaving('QBWhere', $key, $value, 'AND ', $escape);
}
/**
* OR WHERE
*
* Generates the WHERE portion of the query.
* Separates multiple calls with 'OR'.
*
* @param array|RawSql|string $key
* @param mixed $value
*
* @return $this
*/
public function orWhere($key, $value = null, ?bool $escape = null)
{
return $this->whereHaving('QBWhere', $key, $value, 'OR ', $escape);
}
/**
* @used-by where()
* @used-by orWhere()
* @used-by having()
* @used-by orHaving()
*
* @param array|RawSql|string $key
* @param mixed $value
*
* @return $this
*/
protected function whereHaving(string $qbKey, $key, $value = null, string $type = 'AND ', ?bool $escape = null)
{
$rawSqlOnly = false;
if ($key instanceof RawSql) {
if ($value === null) {
$keyValue = [(string) $key => $key];
$rawSqlOnly = true;
} else {
$keyValue = [(string) $key => $value];
}
} elseif (! is_array($key)) {
$keyValue = [$key => $value];
} else {
$keyValue = $key;
}
// If the escape value was not set will base it on the global setting
if (! is_bool($escape)) {
$escape = $this->db->protectIdentifiers;
}
foreach ($keyValue as $k => $v) {
$prefix = empty($this->{$qbKey}) ? $this->groupGetType('') : $this->groupGetType($type);
if ($rawSqlOnly === true) {
$k = '';
$op = '';
} elseif ($v !== null) {
$op = $this->getOperatorFromWhereKey($k);
if (! empty($op)) {
$k = trim($k);
end($op);
$op = trim(current($op));
// Does the key end with operator?
if (substr($k, -strlen($op)) === $op) {
$k = rtrim(substr($k, 0, -strlen($op)));
$op = " {$op}";
} else {
$op = '';
}
} else {
$op = ' =';
}
if ($this->isSubquery($v)) {
$v = $this->buildSubquery($v, true);
} else {
$bind = $this->setBind($k, $v, $escape);
$v = " :{$bind}:";
}
} elseif (! $this->hasOperator($k) && $qbKey !== 'QBHaving') {
// value appears not to have been set, assign the test to IS NULL
$op = ' IS NULL';
} elseif (
// The key ends with !=, =, <>, IS, IS NOT
preg_match(
'/\s*(!?=|<>|IS(?:\s+NOT)?)\s*$/i',
$k,
$match,
PREG_OFFSET_CAPTURE
)
) {
$k = substr($k, 0, $match[0][1]);
$op = $match[1][0] === '=' ? ' IS NULL' : ' IS NOT NULL';
} else {
$op = '';
}
if ($v instanceof RawSql) {
$this->{$qbKey}[] = [
'condition' => $v->with($prefix . $k . $op . $v),
'escape' => $escape,
];
} else {
$this->{$qbKey}[] = [
'condition' => $prefix . $k . $op . $v,
'escape' => $escape,
];
}
}
return $this;
}
/**
* Generates a WHERE field IN('item', 'item') SQL query,
* joined with 'AND' if appropriate.
*
* @param array|BaseBuilder|Closure|string $values The values searched on, or anonymous function with subquery
*
* @return $this
*/
public function whereIn(?string $key = null, $values = null, ?bool $escape = null)
{
return $this->_whereIn($key, $values, false, 'AND ', $escape);
}
/**
* Generates a WHERE field IN('item', 'item') SQL query,
* joined with 'OR' if appropriate.
*
* @param array|BaseBuilder|Closure|string $values The values searched on, or anonymous function with subquery
*
* @return $this
*/
public function orWhereIn(?string $key = null, $values = null, ?bool $escape = null)
{
return $this->_whereIn($key, $values, false, 'OR ', $escape);
}
/**
* Generates a WHERE field NOT IN('item', 'item') SQL query,
* joined with 'AND' if appropriate.
*
* @param array|BaseBuilder|Closure|string $values The values searched on, or anonymous function with subquery
*
* @return $this
*/
public function whereNotIn(?string $key = null, $values = null, ?bool $escape = null)
{
return $this->_whereIn($key, $values, true, 'AND ', $escape);
}
/**
* Generates a WHERE field NOT IN('item', 'item') SQL query,
* joined with 'OR' if appropriate.
*
* @param array|BaseBuilder|Closure|string $values The values searched on, or anonymous function with subquery
*
* @return $this
*/
public function orWhereNotIn(?string $key = null, $values = null, ?bool $escape = null)
{
return $this->_whereIn($key, $values, true, 'OR ', $escape);
}
/**
* Generates a HAVING field IN('item', 'item') SQL query,
* joined with 'AND' if appropriate.
*
* @param array|BaseBuilder|Closure|string $values The values searched on, or anonymous function with subquery
*
* @return $this
*/
public function havingIn(?string $key = null, $values = null, ?bool $escape = null)
{
return $this->_whereIn($key, $values, false, 'AND ', $escape, 'QBHaving');
}
/**
* Generates a HAVING field IN('item', 'item') SQL query,
* joined with 'OR' if appropriate.
*
* @param array|BaseBuilder|Closure|string $values The values searched on, or anonymous function with subquery
*
* @return $this
*/
public function orHavingIn(?string $key = null, $values = null, ?bool $escape = null)
{
return $this->_whereIn($key, $values, false, 'OR ', $escape, 'QBHaving');
}
/**
* Generates a HAVING field NOT IN('item', 'item') SQL query,
* joined with 'AND' if appropriate.
*
* @param array|BaseBuilder|Closure|string $values The values searched on, or anonymous function with subquery
*
* @return $this
*/
public function havingNotIn(?string $key = null, $values = null, ?bool $escape = null)
{
return $this->_whereIn($key, $values, true, 'AND ', $escape, 'QBHaving');
}
/**
* Generates a HAVING field NOT IN('item', 'item') SQL query,
* joined with 'OR' if appropriate.
*
* @param array|BaseBuilder|Closure|string $values The values searched on, or anonymous function with subquery
*
* @return $this
*/
public function orHavingNotIn(?string $key = null, $values = null, ?bool $escape = null)
{
return $this->_whereIn($key, $values, true, 'OR ', $escape, 'QBHaving');
}
/**
* @used-by WhereIn()
* @used-by orWhereIn()
* @used-by whereNotIn()
* @used-by orWhereNotIn()
*
* @param non-empty-string|null $key
* @param array|BaseBuilder|Closure|null $values The values searched on, or anonymous function with subquery
*
* @return $this
*
* @throws InvalidArgumentException
*/
protected function _whereIn(?string $key = null, $values = null, bool $not = false, string $type = 'AND ', ?bool $escape = null, string $clause = 'QBWhere')
{
if ($key === null || $key === '') {
throw new InvalidArgumentException(sprintf('%s() expects $key to be a non-empty string', debug_backtrace(0, 2)[1]['function']));
}
if ($values === null || (! is_array($values) && ! $this->isSubquery($values))) {
throw new InvalidArgumentException(sprintf('%s() expects $values to be of type array or closure', debug_backtrace(0, 2)[1]['function']));
}
if (! is_bool($escape)) {
$escape = $this->db->protectIdentifiers;
}
$ok = $key;
if ($escape === true) {
$key = $this->db->protectIdentifiers($key);
}
$not = ($not) ? ' NOT' : '';
if ($this->isSubquery($values)) {
$whereIn = $this->buildSubquery($values, true);
$escape = false;
} else {
$whereIn = array_values($values);
}
$ok = $this->setBind($ok, $whereIn, $escape);
$prefix = empty($this->{$clause}) ? $this->groupGetType('') : $this->groupGetType($type);
$whereIn = [
'condition' => "{$prefix}{$key}{$not} IN :{$ok}:",
'escape' => false,
];
$this->{$clause}[] = $whereIn;
return $this;
}
/**
* Generates a %LIKE% portion of the query.
* Separates multiple calls with 'AND'.
*
* @param array|RawSql|string $field
*
* @return $this
*/
public function like($field, string $match = '', string $side = 'both', ?bool $escape = null, bool $insensitiveSearch = false)
{
return $this->_like($field, $match, 'AND ', $side, '', $escape, $insensitiveSearch);
}
/**
* Generates a NOT LIKE portion of the query.
* Separates multiple calls with 'AND'.
*
* @param array|RawSql|string $field
*
* @return $this
*/
public function notLike($field, string $match = '', string $side = 'both', ?bool $escape = null, bool $insensitiveSearch = false)
{
return $this->_like($field, $match, 'AND ', $side, 'NOT', $escape, $insensitiveSearch);
}
/**
* Generates a %LIKE% portion of the query.