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

UpdateQuery::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

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