db

Форк
0
/
Where.php 
190 строк · 5.8 Кб
1
<?php
2

3
namespace Upside\Db\SQL;
4

5
class Where
6
{
7
    /** @var    string|Expression */
8
    protected $column;
9

10
    /** @var    string */
11
    protected $separator;
12

13
    protected SQLStatement $sql;
14
    protected WhereStatement $statement;
15

16
    public function __construct(WhereStatement $statement, SQLStatement $sql)
17
    {
18
        $this->sql = $sql;
19
        $this->statement = $statement;
20
    }
21

22
    public function __clone()
23
    {
24
        if ($this->column instanceof Expression) {
25
            $this->column = clone $this->column;
26
        }
27
        $this->sql = clone $this->sql;
28
        $this->statement = new WhereStatement($this->sql);
29
    }
30

31
    public function init(Expression|string|\Closure $column, string $separator): self
32
    {
33
        if ($column instanceof \Closure) {
34
            $column = Expression::from_closure($column);
35
        }
36
        $this->column = $column;
37
        $this->separator = $separator;
38

39
        return $this;
40
    }
41

42
    protected function add_condition(mixed $value, string $operator, bool $isColumn = false): WhereStatement|Select|Delete|Update
43
    {
44
        if ($isColumn && \is_string($value)) {
45
            $value = static function (Expression $expr) use ($value) {
46
                $expr->column($value);
47
            };
48
        }
49
        $this->sql->add_where_condition($this->column, $value, $operator, $this->separator);
50

51
        return $this->statement;
52
    }
53

54
    protected function add_between_condition(float|int|string $value1, float|int|string $value2, bool $not): WhereStatement|Select|Delete|Update
55
    {
56
        $this->sql->add_where_between_condition($this->column, $value1, $value2, $this->separator, $not);
57

58
        return $this->statement;
59
    }
60

61
    protected function add_like_condition(string $pattern, bool $not): WhereStatement|Select|Delete|Update
62
    {
63
        $this->sql->add_where_like_condition($this->column, $pattern, $this->separator, $not);
64

65
        return $this->statement;
66
    }
67

68
    protected function add_in_condition(mixed $value, bool $not): WhereStatement|Select|Delete|Update
69
    {
70
        $this->sql->add_where_in_condition($this->column, $value, $this->separator, $not);
71

72
        return $this->statement;
73
    }
74

75
    protected function add_null_condition(bool $not): WhereStatement|Select|Delete|Update
76
    {
77
        $this->sql->add_where_null_condition($this->column, $this->separator, $not);
78

79
        return $this->statement;
80
    }
81

82
    public function is(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
83
    {
84
        return $this->add_condition($value, '=', $is_column);
85
    }
86

87
    public function is_not(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
88
    {
89
        return $this->add_condition($value, '!=', $is_column);
90
    }
91

92
    public function less_than(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
93
    {
94
        return $this->add_condition($value, '<', $is_column);
95
    }
96

97
    public function greater_than(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
98
    {
99
        return $this->add_condition($value, '>', $is_column);
100
    }
101

102
    public function at_least(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
103
    {
104
        return $this->add_condition($value, '>=', $is_column);
105
    }
106

107
    public function at_most(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
108
    {
109
        return $this->add_condition($value, '<=', $is_column);
110
    }
111

112
    public function between(float|int|string $value1, float|int|string $value2): WhereStatement|Select|Delete|Update
113
    {
114
        return $this->add_between_condition($value1, $value2, false);
115
    }
116

117
    public function not_between(float|int|string $value1, float|int|string $value2): WhereStatement|Select|Delete|Update
118
    {
119
        return $this->add_between_condition($value1, $value2, true);
120
    }
121

122
    public function like(string $value): WhereStatement|Select|Delete|Update
123
    {
124
        return $this->add_like_condition($value, false);
125
    }
126

127
    public function not_like(string $value): WhereStatement|Select|Delete|Update
128
    {
129
        return $this->add_like_condition($value, true);
130
    }
131

132
    public function in(array|\Closure $value): WhereStatement|Select|Delete|Update
133
    {
134
        return $this->add_in_condition($value, false);
135
    }
136

137
    public function not_in(array|\Closure $value): WhereStatement|Select|Delete|Update
138
    {
139
        return $this->add_in_condition($value, true);
140
    }
141

142
    public function is_null(): WhereStatement|Select|Delete|Update
143
    {
144
        return $this->add_null_condition(false);
145
    }
146

147
    public function not_null(): WhereStatement|Select|Delete|Update
148
    {
149
        return $this->add_null_condition(true);
150
    }
151

152
    //Aliases
153

154
    public function eq(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
155
    {
156
        return $this->is($value, $is_column);
157
    }
158

159
    public function ne(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
160
    {
161
        return $this->is_not($value, $is_column);
162
    }
163

164
    public function lt(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
165
    {
166
        return $this->less_than($value, $is_column);
167
    }
168

169
    public function gt(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
170
    {
171
        return $this->greater_than($value, $is_column);
172
    }
173

174
    public function gte(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
175
    {
176
        return $this->at_least($value, $is_column);
177
    }
178

179
    public function lte(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
180
    {
181
        return $this->at_most($value, $is_column);
182
    }
183

184
    public function nop(): WhereStatement|Select|Delete|Update
185
    {
186
        $this->sql->add_where_nop($this->column, $this->separator);
187

188
        return $this->statement;
189
    }
190
}
191

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.