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

DeleteQuery::orWhere()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
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\WhereStatement;
18
19
class DeleteQuery extends AbstractQuery
20
{
21
    use Executable;
22
23
    /**
24
     * @var FromStatement
25
     */
26
    protected $from;
27
28
    /**
29
     * @var WhereStatement
30
     */
31
    protected $where;
32
33
    /**
34
     * @var OrderStatement
35
     */
36
    protected $orderBy;
37
38
    /**
39
     * @var LimitStatement
40
     */
41
    protected $limit;
42
43 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...
44
    {
45
        $this->from = new FromStatement();
46
        $this->where = new WhereStatement();
47
        $this->orderBy = new OrderStatement();
48
        $this->limit = new LimitStatement();
49
    }
50
51
    /**
52
     * Sets the table for the query.
53
     *
54
     * @param string $table table name
55
     *
56
     * @return self
57
     */
58
    public function from($table)
59
    {
60
        $this->from->addTable($table);
61
62
        return $this;
63
    }
64
65
    /**
66
     * Sets the where conditions for the query.
67
     *
68
     * @param array|string $field
69
     * @param string|bool  $condition condition value (optional)
70
     * @param string       $operator  operator (optional)
71
     *
72
     * @return self
73
     */
74 View Code Duplication
    public function where($field, $condition = false, $operator = '=')
75
    {
76
        if (func_num_args() >= 2) {
77
            $this->where->addCondition($field, $condition, $operator);
78
        } else {
79
            $this->where->addCondition($field);
80
        }
81
82
        return $this;
83
    }
84
85
    /**
86
     * Adds a where or condition to the query.
87
     *
88
     * @param array|string $field
89
     * @param string       $condition condition value (optional)
90
     * @param string       $operator  operator (optional)
91
     *
92
     * @return self
93
     */
94 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...
95
    {
96
        if (func_num_args() >= 2) {
97
            $this->where->addConditionOr($field, $condition, $operator);
98
        } else {
99
            $this->where->addConditionOr($field);
100
        }
101
102
        return $this;
103
    }
104
105
    /**
106
     * Adds a where not condition to the query.
107
     *
108
     * @param string $field
109
     * @param string $condition condition value (optional)
110
     *
111
     * @return self
112
     */
113
    public function not($field, $condition = true)
114
    {
115
        $this->where->addCondition($field, $condition, '<>');
116
117
        return $this;
118
    }
119
120
    /**
121
     * Adds a where between condition to the query.
122
     *
123
     * @param string $field
124
     * @param mixed  $a     first between value
125
     * @param mixed  $b     second between value
126
     *
127
     * @return self
128
     */
129
    public function between($field, $a, $b)
130
    {
131
        $this->where->addBetweenCondition($field, $a, $b);
132
133
        return $this;
134
    }
135
136
    /**
137
     * Adds a where not between condition to the query.
138
     *
139
     * @param string $field
140
     * @param mixed  $a     first between value
141
     * @param mixed  $b     second between value
142
     *
143
     * @return self
144
     */
145
    public function notBetween($field, $a, $b)
146
    {
147
        $this->where->addNotBetweenCondition($field, $a, $b);
148
149
        return $this;
150
    }
151
152
    /**
153
     * Sets the limit for the query.
154
     *
155
     * @param int $limit
156
     * @param int $offset
157
     *
158
     * @return self
159
     */
160
    public function limit($limit, $offset = 0)
161
    {
162
        $this->limit->setLimit($limit, $offset);
163
164
        return $this;
165
    }
166
167
    /**
168
     * Sets the order for the query.
169
     *
170
     * @param string|array $fields
171
     * @param string       $direction
172
     *
173
     * @return self
174
     */
175
    public function orderBy($fields, $direction = false)
176
    {
177
        $this->orderBy->addFields($fields, $direction);
178
179
        return $this;
180
    }
181
182
    /**
183
     * Gets the from statement for the query.
184
     *
185
     * @return FromStatement
186
     */
187
    public function getFrom()
188
    {
189
        return $this->from;
190
    }
191
192
    /**
193
     * Gets the where statement for the query.
194
     *
195
     * @return WhereStatement
196
     */
197
    public function getWhere()
198
    {
199
        return $this->where;
200
    }
201
202
    /**
203
     * Gets the limit statement for the query.
204
     *
205
     * @return LimitStatement
206
     */
207
    public function getLimit()
208
    {
209
        return $this->limit;
210
    }
211
212
    /**
213
     * Gets the order by statement for the query.
214
     *
215
     * @return OrderByStatement
216
     */
217
    public function getOrderBy()
218
    {
219
        return $this->orderBy;
220
    }
221
222
    /**
223
     * Generates the raw SQL string for the query.
224
     *
225
     * @return string
226
     */
227
    public function build()
228
    {
229
        $sql = [
230
            'DELETE',
231
            $this->from->build(),
232
            $this->where->build(),
233
            $this->orderBy->build(),
234
            $this->limit->build(),
235
        ];
236
237
        $this->values = $this->where->getValues();
238
239
        return implode(' ', array_filter($sql));
240
    }
241
242
    public function __clone()
243
    {
244
        $this->from = clone $this->from;
245
        $this->where = clone $this->where;
246
        $this->orderBy = clone $this->orderBy;
247
        $this->limit = clone $this->limit;
248
    }
249
}
250