Completed
Push — master ( 609ee9...07eb26 )
by Jared
02:17
created

UpdateQuery::notBetween()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
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
     * Sets the values for the query.
161
     *
162
     * @param array $values
163
     *
164
     * @return self
165
     */
166
    public function values(array $values)
167
    {
168
        $this->set->addValues($values);
169
170
        return $this;
171
    }
172
173
    /**
174
     * Sets the limit for the query.
175
     *
176
     * @param int $limit
177
     * @param int $offset
178
     *
179
     * @return self
180
     */
181
    public function limit($limit, $offset = 0)
182
    {
183
        $this->limit->setLimit($limit, $offset);
184
185
        return $this;
186
    }
187
188
    /**
189
     * Sets the order for the query.
190
     *
191
     * @param string|array $fields
192
     * @param string       $direction
193
     *
194
     * @return self
195
     */
196
    public function orderBy($fields, $direction = false)
197
    {
198
        $this->orderBy->addFields($fields, $direction);
199
200
        return $this;
201
    }
202
203
    /**
204
     * Gets the table name for the query.
205
     *
206
     * @return FromStatement
207
     */
208
    public function getTable()
209
    {
210
        return $this->table;
211
    }
212
213
    /**
214
     * Gets the values for the query.
215
     *
216
     * @return array
217
     */
218
    public function getSet()
219
    {
220
        return $this->set;
221
    }
222
223
    /**
224
     * Gets the where statement for the query.
225
     *
226
     * @return WhereStatement
227
     */
228
    public function getWhere()
229
    {
230
        return $this->where;
231
    }
232
233
    /**
234
     * Gets the order by statement for the query.
235
     *
236
     * @return OrderByStatement
237
     */
238
    public function getOrderBy()
239
    {
240
        return $this->orderBy;
241
    }
242
243
    /**
244
     * Gets the limit statement for the query.
245
     *
246
     * @return LimitStatement
247
     */
248
    public function getLimit()
249
    {
250
        return $this->limit;
251
    }
252
253
    /**
254
     * Generates the raw SQL string for the query.
255
     *
256
     * @return string
257
     */
258
    public function build()
259
    {
260
        $sql = [
261
            'UPDATE',
262
            $this->table->build(),
263
            $this->set->build(),
264
            $this->where->build(),
265
            $this->orderBy->build(),
266
            $this->limit->build(),
267
        ];
268
269
        $this->values = array_merge(
270
            array_values($this->set->getValues()),
271
            $this->where->getValues());
272
273
        return implode(' ', array_filter($sql));
274
    }
275
276
    public function __clone()
277
    {
278
        $this->table = clone $this->table;
279
        $this->set = clone $this->set;
280
        $this->where = clone $this->where;
281
        $this->orderBy = clone $this->orderBy;
282
        $this->limit = clone $this->limit;
283
    }
284
}
285