Completed
Push — master ( f9ab88...d8c240 )
by Jared
02:22
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\Statement\FromStatement;
14
use JAQB\Statement\LimitStatement;
15
use JAQB\Statement\OrderStatement;
16
use JAQB\Statement\WhereStatement;
17
18
class DeleteQuery extends Query
19
{
20
    /**
21
     * @var FromStatement
22
     */
23
    protected $from;
24
25
    /**
26
     * @var WhereStatement
27
     */
28
    protected $where;
29
30
    /**
31
     * @var OrderStatement
32
     */
33
    protected $orderBy;
34
35
    /**
36
     * @var LimitStatement
37
     */
38
    protected $limit;
39
40 View Code Duplication
    public function initialize()
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...
41
    {
42
        $this->from = new FromStatement();
43
        $this->where = new WhereStatement();
44
        $this->orderBy = new OrderStatement();
45
        $this->limit = new LimitStatement();
46
    }
47
48
    /**
49
     * Sets the table for the query.
50
     *
51
     * @param string $table table name
52
     *
53
     * @return self
54
     */
55
    public function from($table)
56
    {
57
        $this->from->addTable($table);
58
59
        return $this;
60
    }
61
62
    /**
63
     * Sets the where conditions for the query.
64
     *
65
     * @param array|string $field
66
     * @param string|bool  $condition condition value (optional)
67
     * @param string       $operator  operator (optional)
68
     *
69
     * @return self
70
     */
71 View Code Duplication
    public function where($field, $condition = false, $operator = '=')
72
    {
73
        if (func_num_args() >= 2) {
74
            $this->where->addCondition($field, $condition, $operator);
75
        } else {
76
            $this->where->addCondition($field);
77
        }
78
79
        return $this;
80
    }
81
82
    /**
83
     * Sets the limit for the query.
84
     *
85
     * @param int $limit
86
     * @param int $offset
87
     *
88
     * @return self
89
     */
90
    public function limit($limit, $offset = 0)
91
    {
92
        $this->limit->setLimit($limit, $offset);
93
94
        return $this;
95
    }
96
97
    /**
98
     * Sets the order for the query.
99
     *
100
     * @param string|array $fields
101
     * @param string       $direction
102
     *
103
     * @return self
104
     */
105
    public function orderBy($fields, $direction = false)
106
    {
107
        $this->orderBy->addFields($fields, $direction);
108
109
        return $this;
110
    }
111
112
    /**
113
     * Gets the from statement for the query.
114
     *
115
     * @return FromStatement
116
     */
117
    public function getFrom()
118
    {
119
        return $this->from;
120
    }
121
122
    /**
123
     * Gets the where statement for the query.
124
     *
125
     * @return WhereStatement
126
     */
127
    public function getWhere()
128
    {
129
        return $this->where;
130
    }
131
132
    /**
133
     * Gets the limit statement for the query.
134
     *
135
     * @return LimitStatement
136
     */
137
    public function getLimit()
138
    {
139
        return $this->limit;
140
    }
141
142
    /**
143
     * Gets the order by statement for the query.
144
     *
145
     * @return OrderByStatement
146
     */
147
    public function getOrderBy()
148
    {
149
        return $this->orderBy;
150
    }
151
152
    /**
153
     * Generates the raw SQL string for the query.
154
     *
155
     * @return string
156
     */
157
    public function build()
158
    {
159
        $sql = [
160
            'DELETE',
161
            $this->from->build(),
162
            $this->where->build(),
163
            $this->orderBy->build(),
164
            $this->limit->build(),
165
        ];
166
167
        $this->values = $this->where->getValues();
168
169
        return implode(' ', array_filter($sql));
170
    }
171
}
172