Completed
Push — master ( f9ab88...d8c240 )
by Jared
02:22
created

UpdateQuery::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 17
rs 9.4286
cc 1
eloc 12
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\SetStatement;
17
use JAQB\Statement\WhereStatement;
18
19
class UpdateQuery extends Query
20
{
21
    /**
22
     * @var FromStatement
23
     */
24
    protected $table;
25
26
    /**
27
     * @var SetStatement
28
     */
29
    protected $set;
30
31
    /**
32
     * @var WhereStatement
33
     */
34
    protected $where;
35
36
    /**
37
     * @var OrderStatement
38
     */
39
    protected $orderBy;
40
41
    /**
42
     * @var LimitStatement
43
     */
44
    protected $limit;
45
46 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...
47
    {
48
        $this->table = new FromStatement(false);
49
        $this->set = new SetStatement();
50
        $this->where = new WhereStatement();
51
        $this->orderBy = new OrderStatement();
52
        $this->limit = new LimitStatement();
53
    }
54
55
    /**
56
     * Sets the table for the query.
57
     *
58
     * @param string $table table name
59
     *
60
     * @return self
61
     */
62
    public function table($table)
63
    {
64
        $this->table->addTable($table);
65
66
        return $this;
67
    }
68
69
    /**
70
     * Sets the where conditions for the query.
71
     *
72
     * @param array|string $field
73
     * @param string|bool  $condition condition value (optional)
74
     * @param string       $operator  operator (optional)
75
     *
76
     * @return self
77
     */
78 View Code Duplication
    public function where($field, $condition = false, $operator = '=')
79
    {
80
        if (func_num_args() >= 2) {
81
            $this->where->addCondition($field, $condition, $operator);
82
        } else {
83
            $this->where->addCondition($field);
84
        }
85
86
        return $this;
87
    }
88
89
    /**
90
     * Sets the values for the query.
91
     *
92
     * @param array $values
93
     *
94
     * @return self
95
     */
96
    public function values(array $values)
97
    {
98
        $this->set->addValues($values);
99
100
        return $this;
101
    }
102
103
    /**
104
     * Sets the limit for the query.
105
     *
106
     * @param int $limit
107
     * @param int $offset
108
     *
109
     * @return self
110
     */
111
    public function limit($limit, $offset = 0)
112
    {
113
        $this->limit->setLimit($limit, $offset);
114
115
        return $this;
116
    }
117
118
    /**
119
     * Sets the order for the query.
120
     *
121
     * @param string|array $fields
122
     * @param string       $direction
123
     *
124
     * @return self
125
     */
126
    public function orderBy($fields, $direction = false)
127
    {
128
        $this->orderBy->addFields($fields, $direction);
129
130
        return $this;
131
    }
132
133
    /**
134
     * Gets the table name for the query.
135
     *
136
     * @return FromStatement
137
     */
138
    public function getTable()
139
    {
140
        return $this->table;
141
    }
142
143
    /**
144
     * Gets the values for the query.
145
     *
146
     * @return array
147
     */
148
    public function getSet()
149
    {
150
        return $this->set;
151
    }
152
153
    /**
154
     * Gets the where statement for the query.
155
     *
156
     * @return WhereStatement
157
     */
158
    public function getWhere()
159
    {
160
        return $this->where;
161
    }
162
163
    /**
164
     * Gets the order by statement for the query.
165
     *
166
     * @return OrderByStatement
167
     */
168
    public function getOrderBy()
169
    {
170
        return $this->orderBy;
171
    }
172
173
    /**
174
     * Gets the limit statement for the query.
175
     *
176
     * @return LimitStatement
177
     */
178
    public function getLimit()
179
    {
180
        return $this->limit;
181
    }
182
183
    /**
184
     * Generates the raw SQL string for the query.
185
     *
186
     * @return string
187
     */
188
    public function build()
189
    {
190
        $sql = [
191
            'UPDATE',
192
            $this->table->build(),
193
            $this->set->build(),
194
            $this->where->build(),
195
            $this->orderBy->build(),
196
            $this->limit->build(),
197
        ];
198
199
        $this->values = array_merge(
200
            array_values($this->set->getValues()),
201
            $this->where->getValues());
202
203
        return implode(' ', array_filter($sql));
204
    }
205
}
206