Completed
Push — master ( 94753f...609ee9 )
by Jared
02:22
created

UpdateQuery::not()   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 2
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 not condition to the query.
94
     *
95
     * @param string $field
96
     * @param string $condition condition value (optional)
97
     *
98
     * @return self
99
     */
100
    public function not($field, $condition = true)
101
    {
102
        $this->where->addCondition($field, $condition, '<>');
103
104
        return $this;
105
    }
106
107
    /**
108
     * Adds a where between condition to the query.
109
     *
110
     * @param string $field
111
     * @param mixed  $a     first between value
112
     * @param mixed  $b     second between value
113
     *
114
     * @return self
115
     */
116
    public function between($field, $a, $b)
117
    {
118
        $this->where->addBetweenCondition($field, $a, $b);
119
120
        return $this;
121
    }
122
123
    /**
124
     * Adds a where not between condition to the query.
125
     *
126
     * @param string $field
127
     * @param mixed  $a     first between value
128
     * @param mixed  $b     second between value
129
     *
130
     * @return self
131
     */
132
    public function notBetween($field, $a, $b)
133
    {
134
        $this->where->addNotBetweenCondition($field, $a, $b);
135
136
        return $this;
137
    }
138
139
    /**
140
     * Sets the values for the query.
141
     *
142
     * @param array $values
143
     *
144
     * @return self
145
     */
146
    public function values(array $values)
147
    {
148
        $this->set->addValues($values);
149
150
        return $this;
151
    }
152
153
    /**
154
     * Sets the limit for the query.
155
     *
156
     * @param int $limit
157
     * @param int $offset
158
     *
159
     * @return self
160
     */
161
    public function limit($limit, $offset = 0)
162
    {
163
        $this->limit->setLimit($limit, $offset);
164
165
        return $this;
166
    }
167
168
    /**
169
     * Sets the order for the query.
170
     *
171
     * @param string|array $fields
172
     * @param string       $direction
173
     *
174
     * @return self
175
     */
176
    public function orderBy($fields, $direction = false)
177
    {
178
        $this->orderBy->addFields($fields, $direction);
179
180
        return $this;
181
    }
182
183
    /**
184
     * Gets the table name for the query.
185
     *
186
     * @return FromStatement
187
     */
188
    public function getTable()
189
    {
190
        return $this->table;
191
    }
192
193
    /**
194
     * Gets the values for the query.
195
     *
196
     * @return array
197
     */
198
    public function getSet()
199
    {
200
        return $this->set;
201
    }
202
203
    /**
204
     * Gets the where statement for the query.
205
     *
206
     * @return WhereStatement
207
     */
208
    public function getWhere()
209
    {
210
        return $this->where;
211
    }
212
213
    /**
214
     * Gets the order by statement for the query.
215
     *
216
     * @return OrderByStatement
217
     */
218
    public function getOrderBy()
219
    {
220
        return $this->orderBy;
221
    }
222
223
    /**
224
     * Gets the limit statement for the query.
225
     *
226
     * @return LimitStatement
227
     */
228
    public function getLimit()
229
    {
230
        return $this->limit;
231
    }
232
233
    /**
234
     * Generates the raw SQL string for the query.
235
     *
236
     * @return string
237
     */
238
    public function build()
239
    {
240
        $sql = [
241
            'UPDATE',
242
            $this->table->build(),
243
            $this->set->build(),
244
            $this->where->build(),
245
            $this->orderBy->build(),
246
            $this->limit->build(),
247
        ];
248
249
        $this->values = array_merge(
250
            array_values($this->set->getValues()),
251
            $this->where->getValues());
252
253
        return implode(' ', array_filter($sql));
254
    }
255
256
    public function __clone()
257
    {
258
        $this->table = clone $this->table;
259
        $this->set = clone $this->set;
260
        $this->where = clone $this->where;
261
        $this->orderBy = clone $this->orderBy;
262
        $this->limit = clone $this->limit;
263
    }
264
}
265