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

DeleteQuery::between()   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\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 not condition to the query.
87
     *
88
     * @param string $field
89
     * @param string $condition condition value (optional)
90
     *
91
     * @return self
92
     */
93
    public function not($field, $condition = true)
94
    {
95
        $this->where->addCondition($field, $condition, '<>');
96
97
        return $this;
98
    }
99
100
    /**
101
     * Adds a where between condition to the query.
102
     *
103
     * @param string $field
104
     * @param mixed  $a     first between value
105
     * @param mixed  $b     second between value
106
     *
107
     * @return self
108
     */
109
    public function between($field, $a, $b)
110
    {
111
        $this->where->addBetweenCondition($field, $a, $b);
112
113
        return $this;
114
    }
115
116
    /**
117
     * Adds a where not between condition to the query.
118
     *
119
     * @param string $field
120
     * @param mixed  $a     first between value
121
     * @param mixed  $b     second between value
122
     *
123
     * @return self
124
     */
125
    public function notBetween($field, $a, $b)
126
    {
127
        $this->where->addNotBetweenCondition($field, $a, $b);
128
129
        return $this;
130
    }
131
132
    /**
133
     * Sets the limit for the query.
134
     *
135
     * @param int $limit
136
     * @param int $offset
137
     *
138
     * @return self
139
     */
140
    public function limit($limit, $offset = 0)
141
    {
142
        $this->limit->setLimit($limit, $offset);
143
144
        return $this;
145
    }
146
147
    /**
148
     * Sets the order for the query.
149
     *
150
     * @param string|array $fields
151
     * @param string       $direction
152
     *
153
     * @return self
154
     */
155
    public function orderBy($fields, $direction = false)
156
    {
157
        $this->orderBy->addFields($fields, $direction);
158
159
        return $this;
160
    }
161
162
    /**
163
     * Gets the from statement for the query.
164
     *
165
     * @return FromStatement
166
     */
167
    public function getFrom()
168
    {
169
        return $this->from;
170
    }
171
172
    /**
173
     * Gets the where statement for the query.
174
     *
175
     * @return WhereStatement
176
     */
177
    public function getWhere()
178
    {
179
        return $this->where;
180
    }
181
182
    /**
183
     * Gets the limit statement for the query.
184
     *
185
     * @return LimitStatement
186
     */
187
    public function getLimit()
188
    {
189
        return $this->limit;
190
    }
191
192
    /**
193
     * Gets the order by statement for the query.
194
     *
195
     * @return OrderByStatement
196
     */
197
    public function getOrderBy()
198
    {
199
        return $this->orderBy;
200
    }
201
202
    /**
203
     * Generates the raw SQL string for the query.
204
     *
205
     * @return string
206
     */
207
    public function build()
208
    {
209
        $sql = [
210
            'DELETE',
211
            $this->from->build(),
212
            $this->where->build(),
213
            $this->orderBy->build(),
214
            $this->limit->build(),
215
        ];
216
217
        $this->values = $this->where->getValues();
218
219
        return implode(' ', array_filter($sql));
220
    }
221
222
    public function __clone()
223
    {
224
        $this->from = clone $this->from;
225
        $this->where = clone $this->where;
226
        $this->orderBy = clone $this->orderBy;
227
        $this->limit = clone $this->limit;
228
    }
229
}
230