Completed
Push — master ( d8c240...9dd0a9 )
by Jared
02:18
created

DeleteQuery::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 14
rs 9.4286
cc 1
eloc 9
nc 1
nop 0
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 Query
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
     * Sets the limit for the query.
87
     *
88
     * @param int $limit
89
     * @param int $offset
90
     *
91
     * @return self
92
     */
93
    public function limit($limit, $offset = 0)
94
    {
95
        $this->limit->setLimit($limit, $offset);
96
97
        return $this;
98
    }
99
100
    /**
101
     * Sets the order for the query.
102
     *
103
     * @param string|array $fields
104
     * @param string       $direction
105
     *
106
     * @return self
107
     */
108
    public function orderBy($fields, $direction = false)
109
    {
110
        $this->orderBy->addFields($fields, $direction);
111
112
        return $this;
113
    }
114
115
    /**
116
     * Gets the from statement for the query.
117
     *
118
     * @return FromStatement
119
     */
120
    public function getFrom()
121
    {
122
        return $this->from;
123
    }
124
125
    /**
126
     * Gets the where statement for the query.
127
     *
128
     * @return WhereStatement
129
     */
130
    public function getWhere()
131
    {
132
        return $this->where;
133
    }
134
135
    /**
136
     * Gets the limit statement for the query.
137
     *
138
     * @return LimitStatement
139
     */
140
    public function getLimit()
141
    {
142
        return $this->limit;
143
    }
144
145
    /**
146
     * Gets the order by statement for the query.
147
     *
148
     * @return OrderByStatement
149
     */
150
    public function getOrderBy()
151
    {
152
        return $this->orderBy;
153
    }
154
155
    /**
156
     * Generates the raw SQL string for the query.
157
     *
158
     * @return string
159
     */
160
    public function build()
161
    {
162
        $sql = [
163
            'DELETE',
164
            $this->from->build(),
165
            $this->where->build(),
166
            $this->orderBy->build(),
167
            $this->limit->build(),
168
        ];
169
170
        $this->values = $this->where->getValues();
171
172
        return implode(' ', array_filter($sql));
173
    }
174
}
175