Completed
Push — master ( 07eb26...46e6ae )
by Jared
02:23
created

UpdateQuery::notExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @link http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
namespace JAQB\Query;
12
13
use JAQB\Operations\Executable;
14
use JAQB\Statement\FromStatement;
15
use JAQB\Statement\LimitStatement;
16
use JAQB\Statement\OrderStatement;
17
use JAQB\Statement\SetStatement;
18
use JAQB\Statement\WhereStatement;
19
20
class UpdateQuery extends AbstractQuery
21
{
22
    use Executable;
23
24
    /**
25
     * @var FromStatement
26
     */
27
    protected $table;
28
29
    /**
30
     * @var SetStatement
31
     */
32
    protected $set;
33
34
    /**
35
     * @var WhereStatement
36
     */
37
    protected $where;
38
39
    /**
40
     * @var OrderStatement
41
     */
42
    protected $orderBy;
43
44
    /**
45
     * @var LimitStatement
46
     */
47
    protected $limit;
48
49 View Code Duplication
    public function __construct()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        $this->table = new FromStatement(false);
52
        $this->set = new SetStatement();
53
        $this->where = new WhereStatement();
54
        $this->orderBy = new OrderStatement();
55
        $this->limit = new LimitStatement();
56
    }
57
58
    /**
59
     * Sets the table for the query.
60
     *
61
     * @param string $table table name
62
     *
63
     * @return self
64
     */
65
    public function table($table)
66
    {
67
        $this->table->addTable($table);
68
69
        return $this;
70
    }
71
72
    /**
73
     * Sets the where conditions for the query.
74
     *
75
     * @param array|string $field
76
     * @param string|bool  $condition condition value (optional)
77
     * @param string       $operator  operator (optional)
78
     *
79
     * @return self
80
     */
81 View Code Duplication
    public function where($field, $condition = false, $operator = '=')
82
    {
83
        if (func_num_args() >= 2) {
84
            $this->where->addCondition($field, $condition, $operator);
85
        } else {
86
            $this->where->addCondition($field);
87
        }
88
89
        return $this;
90
    }
91
92
    /**
93
     * Adds a where or condition to the query.
94
     *
95
     * @param array|string $field
96
     * @param string       $condition condition value (optional)
97
     * @param string       $operator  operator (optional)
98
     *
99
     * @return self
100
     */
101 View Code Duplication
    public function orWhere($field, $condition = false, $operator = '=')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    {
103
        if (func_num_args() >= 2) {
104
            $this->where->addConditionOr($field, $condition, $operator);
105
        } else {
106
            $this->where->addConditionOr($field);
107
        }
108
109
        return $this;
110
    }
111
112
    /**
113
     * Adds a where not condition to the query.
114
     *
115
     * @param string $field
116
     * @param string $condition condition value (optional)
117
     *
118
     * @return self
119
     */
120
    public function not($field, $condition = true)
121
    {
122
        $this->where->addCondition($field, $condition, '<>');
123
124
        return $this;
125
    }
126
127
    /**
128
     * Adds a where between condition to the query.
129
     *
130
     * @param string $field
131
     * @param mixed  $a     first between value
132
     * @param mixed  $b     second between value
133
     *
134
     * @return self
135
     */
136
    public function between($field, $a, $b)
137
    {
138
        $this->where->addBetweenCondition($field, $a, $b);
139
140
        return $this;
141
    }
142
143
    /**
144
     * Adds a where not between condition to the query.
145
     *
146
     * @param string $field
147
     * @param mixed  $a     first between value
148
     * @param mixed  $b     second between value
149
     *
150
     * @return self
151
     */
152
    public function notBetween($field, $a, $b)
153
    {
154
        $this->where->addNotBetweenCondition($field, $a, $b);
155
156
        return $this;
157
    }
158
159
    /**
160
     * Adds an exists condition to the query.
161
     *
162
     * @param callable $f
163
     *
164
     * @return self
165
     */
166
    public function exists(callable $f)
167
    {
168
        $this->where->addExistsCondition($f);
169
170
        return $this;
171
    }
172
173
    /**
174
     * Adds a not exists condition to the query.
175
     *
176
     * @param callable $f
177
     *
178
     * @return self
179
     */
180
    public function notExists(callable $f)
181
    {
182
        $this->where->addNotExistsCondition($f);
183
184
        return $this;
185
    }
186
187
    /**
188
     * Sets the values for the query.
189
     *
190
     * @param array $values
191
     *
192
     * @return self
193
     */
194
    public function values(array $values)
195
    {
196
        $this->set->addValues($values);
197
198
        return $this;
199
    }
200
201
    /**
202
     * Sets the limit for the query.
203
     *
204
     * @param int $limit
205
     * @param int $offset
206
     *
207
     * @return self
208
     */
209
    public function limit($limit, $offset = 0)
210
    {
211
        $this->limit->setLimit($limit, $offset);
212
213
        return $this;
214
    }
215
216
    /**
217
     * Sets the order for the query.
218
     *
219
     * @param string|array $fields
220
     * @param string       $direction
221
     *
222
     * @return self
223
     */
224
    public function orderBy($fields, $direction = false)
225
    {
226
        $this->orderBy->addFields($fields, $direction);
227
228
        return $this;
229
    }
230
231
    /**
232
     * Gets the table name for the query.
233
     *
234
     * @return FromStatement
235
     */
236
    public function getTable()
237
    {
238
        return $this->table;
239
    }
240
241
    /**
242
     * Gets the values for the query.
243
     *
244
     * @return array
245
     */
246
    public function getSet()
247
    {
248
        return $this->set;
249
    }
250
251
    /**
252
     * Gets the where statement for the query.
253
     *
254
     * @return WhereStatement
255
     */
256
    public function getWhere()
257
    {
258
        return $this->where;
259
    }
260
261
    /**
262
     * Gets the order by statement for the query.
263
     *
264
     * @return OrderByStatement
265
     */
266
    public function getOrderBy()
267
    {
268
        return $this->orderBy;
269
    }
270
271
    /**
272
     * Gets the limit statement for the query.
273
     *
274
     * @return LimitStatement
275
     */
276
    public function getLimit()
277
    {
278
        return $this->limit;
279
    }
280
281
    /**
282
     * Generates the raw SQL string for the query.
283
     *
284
     * @return string
285
     */
286
    public function build()
287
    {
288
        $sql = [
289
            'UPDATE',
290
            $this->table->build(),
291
            $this->set->build(),
292
            $this->where->build(),
293
            $this->orderBy->build(),
294
            $this->limit->build(),
295
        ];
296
297
        $this->values = array_merge(
298
            array_values($this->set->getValues()),
299
            $this->where->getValues());
300
301
        return implode(' ', array_filter($sql));
302
    }
303
304
    public function __clone()
305
    {
306
        $this->table = clone $this->table;
307
        $this->set = clone $this->set;
308
        $this->where = clone $this->where;
309
        $this->orderBy = clone $this->orderBy;
310
        $this->limit = clone $this->limit;
311
    }
312
}
313