Completed
Push — 3.x ( bfc500...4ede47 )
by Hari
9s
created

Insert::highPriority()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\SqlQuery\Mysql;
10
11
use Aura\SqlQuery\Common;
12
13
/**
14
 *
15
 * An object for MySQL INSERT queries.
16
 *
17
 * @package Aura.SqlQuery
18
 *
19
 */
20
class Insert extends Common\Insert
21
{
22
    /**
23
     *
24
     * Column values for ON DUPLICATE KEY UPDATE section of query; the key is
25
     * the column name and the value is the column value.
26
     *
27
     * @param array
28
     *
29
     */
30
    protected $col_on_update_values;
31
32
    /**
33
     *
34
     * Adds or removes HIGH_PRIORITY flag.
35
     *
36
     * @param bool $enable Set or unset flag (default true).
37
     *
38
     * @return $this
39
     *
40
     */
41 1
    public function highPriority($enable = true)
42
    {
43 1
        $this->setFlag('HIGH_PRIORITY', $enable);
44 1
        return $this;
45
    }
46
47
    /**
48
     *
49
     * Adds or removes LOW_PRIORITY flag.
50
     *
51
     * @param bool $enable Set or unset flag (default true).
52
     *
53
     * @return $this
54
     *
55
     */
56 1
    public function lowPriority($enable = true)
57
    {
58 1
        $this->setFlag('LOW_PRIORITY', $enable);
59 1
        return $this;
60
    }
61
62
    /**
63
     *
64
     * Adds or removes IGNORE flag.
65
     *
66
     * @param bool $enable Set or unset flag (default true).
67
     *
68
     * @return $this
69
     *
70
     */
71 1
    public function ignore($enable = true)
72
    {
73 1
        $this->setFlag('IGNORE', $enable);
74 1
        return $this;
75
    }
76
77
    /**
78
     *
79
     * Adds or removes DELAYED flag.
80
     *
81
     * @param bool $enable Set or unset flag (default true).
82
     *
83
     * @return $this
84
     *
85
     */
86 1
    public function delayed($enable = true)
87
    {
88 1
        $this->setFlag('DELAYED', $enable);
89 1
        return $this;
90
    }
91
92
    /**
93
     *
94
     * Sets one column value placeholder in ON DUPLICATE KEY UPDATE section;
95
     * if an optional second parameter is passed, that value is bound to the
96
     * placeholder.
97
     *
98
     * @param string $col The column name.
99
     *
100
     * @param array $value Optional: a value to bind to the placeholder.
101
     *
102
     * @return $this
103
     *
104
     */
105 1
    public function onDuplicateKeyUpdateCol($col, ...$value)
106
    {
107 1
        $key = $this->quoter->quoteName($col);
108 1
        $bind = $col . '__on_duplicate_key';
109 1
        $this->col_on_update_values[$key] = ":$bind";
110 1
        if (count($value) > 0) {
111 1
            $this->bindValue($bind, $value[0]);
112 1
        }
113 1
        return $this;
114
    }
115
116
    /**
117
     *
118
     * Sets multiple column value placeholders in ON DUPLICATE KEY UPDATE
119
     * section. If an element is a key-value pair, the key is treated as the
120
     * column name and the value is bound to that column.
121
     *
122
     * @param array $cols A list of column names, optionally as key-value
123
     * pairs where the key is a column name and the value is a bind value for
124
     * that column.
125
     *
126
     * @return $this
127
     *
128
     */
129 1
    public function onDuplicateKeyUpdateCols(array $cols)
130
    {
131 1
        foreach ($cols as $key => $val) {
132 1
            if (is_int($key)) {
133
                // integer key means the value is the column name
134 1
                $this->onDuplicateKeyUpdateCol($val);
135 1
            } else {
136
                // the key is the column name and the value is a value to
137
                // be bound to that column
138 1
                $this->onDuplicateKeyUpdateCol($key, $val);
139
            }
140 1
        }
141 1
        return $this;
142
    }
143
144
    /**
145
     *
146
     * Sets a column value directly in ON DUPLICATE KEY UPDATE section; the
147
     * value will not be escaped, although fully-qualified identifiers in the
148
     * value will be quoted.
149
     *
150
     * @param string $col The column name.
151
     *
152
     * @param string $value The column value expression.
153
     *
154
     * @return $this
155
     *
156
     */
157 1
    public function onDuplicateKeyUpdate($col, $value)
158
    {
159 1
        if ($value === null) {
160 1
            $value = 'NULL';
161 1
        }
162
163 1
        $key = $this->quoter->quoteName($col);
164 1
        $value = $this->quoter->quoteNamesIn($value);
165 1
        $this->col_on_update_values[$key] = $value;
166 1
        return $this;
167
    }
168
169
    /**
170
     *
171
     * Builds this query object into a string.
172
     *
173
     * @return string
174
     *
175
     */
176 11
    protected function build()
177
    {
178
        return 'INSERT'
179 11
            . $this->buildFlags()
180 11
            . $this->buildInto()
181 11
            . $this->buildValuesForInsert()
182 11
            . $this->buildValuesForUpdateOnDuplicateKey()
183 11
            . $this->buildReturning();
184
    }
185
186
    /**
187
     *
188
     * Builds the UPDATE ON DUPLICATE KEY part of the statement.
189
     *
190
     * @return string
191
     *
192
     */
193 11
    protected function buildValuesForUpdateOnDuplicateKey()
194
    {
195 11
        if (! $this->col_on_update_values) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->col_on_update_values of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
196 10
            return ''; // not applicable
197
        }
198
199 1
        $values = array();
200 1
        foreach ($this->col_on_update_values as $key => $row) {
201 1
            $values[] = $this->indent(array($key . ' = ' . $row));
202 1
        }
203
204
        return ' ON DUPLICATE KEY UPDATE'
205 1
            . implode (',', $values);
206
    }
207
}
208