|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Bluz Framework Component |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Bluz PHP Team |
|
6
|
|
|
* @link https://github.com/bluzphp/framework |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @namespace |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace Bluz\Db\Query\Traits; |
|
13
|
|
|
|
|
14
|
|
|
use Bluz\Db\Query\CompositeBuilder; |
|
15
|
|
|
use Bluz\Db\Query\Delete; |
|
16
|
|
|
use Bluz\Db\Query\Select; |
|
17
|
|
|
use Bluz\Db\Query\Update; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Order Trait |
|
21
|
|
|
* |
|
22
|
|
|
* Required for: |
|
23
|
|
|
* - Select Builder |
|
24
|
|
|
* - Update Builder |
|
25
|
|
|
* - Delete Builder |
|
26
|
|
|
* |
|
27
|
|
|
* @package Bluz\Db\Query\Traits |
|
28
|
|
|
* @author Anton Shevchuk |
|
29
|
|
|
* |
|
30
|
|
|
* @method Select|Update|Delete addQueryPart(string $sqlPartName, mixed $sqlPart, $append = 'true') |
|
31
|
|
|
* @method mixed getQueryPart(string $queryPartName) |
|
32
|
|
|
* @method string prepareCondition($args = array()) |
|
33
|
|
|
*/ |
|
34
|
|
|
trait Where |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* Set WHERE condition |
|
38
|
|
|
* |
|
39
|
|
|
* Specifies one or more restrictions to the query result |
|
40
|
|
|
* Replaces any previously specified restrictions, if any |
|
41
|
|
|
* <code> |
|
42
|
|
|
* $sb = new SelectBuilder(); |
|
43
|
|
|
* $sb |
|
44
|
|
|
* ->select('u.name') |
|
45
|
|
|
* ->from('users', 'u') |
|
46
|
|
|
* ->where('u.id = ?', $id) |
|
47
|
|
|
* ; |
|
48
|
|
|
* </code> |
|
49
|
|
|
* |
|
50
|
|
|
* @param string ...$condition optional the query restriction predicates |
|
51
|
|
|
* @return Select|Update|Delete |
|
52
|
|
|
*/ |
|
53
|
8 |
|
public function where(...$condition) |
|
54
|
|
|
{ |
|
55
|
8 |
|
$condition = $this->prepareCondition($condition); |
|
56
|
|
|
|
|
57
|
8 |
|
return $this->addQueryPart('where', $condition, false); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Add WHERE .. AND .. condition |
|
62
|
|
|
* |
|
63
|
|
|
* Adds one or more restrictions to the query results, forming a logical |
|
64
|
|
|
* conjunction with any previously specified restrictions. |
|
65
|
|
|
* <code> |
|
66
|
|
|
* $sb = new SelectBuilder(); |
|
67
|
|
|
* $sb |
|
68
|
|
|
* ->select('u') |
|
69
|
|
|
* ->from('users', 'u') |
|
70
|
|
|
* ->where('u.username LIKE ?', '%Smith%') |
|
71
|
|
|
* ->andWhere('u.is_active = ?', 1); |
|
72
|
|
|
* </code> |
|
73
|
|
|
* |
|
74
|
|
|
* @param string ...$condition Optional the query restriction predicates |
|
75
|
|
|
* @return Select|Update|Delete |
|
76
|
|
|
*/ |
|
77
|
2 |
|
public function andWhere(...$condition) |
|
78
|
|
|
{ |
|
79
|
2 |
|
$condition = $this->prepareCondition($condition); |
|
80
|
|
|
|
|
81
|
2 |
|
$where = $this->getQueryPart('where'); |
|
82
|
|
|
|
|
83
|
2 |
|
if ($where instanceof CompositeBuilder && $where->getType() == 'AND') { |
|
84
|
1 |
|
$where->add($condition); |
|
85
|
1 |
|
} else { |
|
86
|
2 |
|
$where = new CompositeBuilder([$where, $condition]); |
|
87
|
|
|
} |
|
88
|
2 |
|
return $this->addQueryPart('where', $where, false); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Add WHERE .. OR .. condition |
|
93
|
|
|
* |
|
94
|
|
|
* Adds one or more restrictions to the query results, forming a logical |
|
95
|
|
|
* disjunction with any previously specified restrictions. |
|
96
|
|
|
* <code> |
|
97
|
|
|
* $sb = new SelectBuilder(); |
|
98
|
|
|
* $sb |
|
99
|
|
|
* ->select('u.name') |
|
100
|
|
|
* ->from('users', 'u') |
|
101
|
|
|
* ->where('u.id = 1') |
|
102
|
|
|
* ->orWhere('u.id = ?', 2); |
|
103
|
|
|
* </code> |
|
104
|
|
|
* |
|
105
|
|
|
* @param string ...$condition Optional the query restriction predicates |
|
106
|
|
|
* @return Select|Update|Delete |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function orWhere(...$condition) |
|
109
|
|
|
{ |
|
110
|
1 |
|
$condition = $this->prepareCondition($condition); |
|
111
|
|
|
|
|
112
|
1 |
|
$where = $this->getQueryPart('where'); |
|
113
|
|
|
|
|
114
|
1 |
|
if ($where instanceof CompositeBuilder && $where->getType() == 'OR') { |
|
115
|
|
|
$where->add($condition); |
|
116
|
|
|
} else { |
|
117
|
1 |
|
$where = new CompositeBuilder([$where, $condition], 'OR'); |
|
118
|
|
|
} |
|
119
|
1 |
|
return $this->addQueryPart('where', $where, false); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|