Completed
Pull Request — 2.x (#95)
by Paul
02:07
created

Insert::buildValuesForUpdateOnDuplicateKey()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 0
crap 3
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 mixed,... $val Optional: a value to bind to the placeholder.
0 ignored issues
show
Documentation introduced by
The doc-type mixed,... could not be parsed: Expected "|" or "end of type", but got "," at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
Bug introduced by
There is no parameter named $val. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
101
     *
102
     * @return $this
103
     *
104
     */
105 1
    public function onDuplicateKeyUpdateCol($col)
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
        $args = func_get_args();
111 1
        if (count($args) > 1) {
112 1
            $this->bindValue($bind, $args[1]);
113
        }
114 1
        return $this;
115
    }
116
117
    /**
118
     *
119
     * Sets multiple column value placeholders in ON DUPLICATE KEY UPDATE
120
     * section. If an element is a key-value pair, the key is treated as the
121
     * column name and the value is bound to that column.
122
     *
123
     * @param array $cols A list of column names, optionally as key-value
124
     * pairs where the key is a column name and the value is a bind value for
125
     * that column.
126
     *
127
     * @return $this
128
     *
129
     */
130 1
    public function onDuplicateKeyUpdateCols(array $cols)
131
    {
132 1
        foreach ($cols as $key => $val) {
133 1
            if (is_int($key)) {
134
                // integer key means the value is the column name
135 1
                $this->onDuplicateKeyUpdateCol($val);
136
            } else {
137
                // the key is the column name and the value is a value to
138
                // be bound to that column
139 1
                $this->onDuplicateKeyUpdateCol($key, $val);
140
            }
141
        }
142 1
        return $this;
143
    }
144
145
    /**
146
     *
147
     * Sets a column value directly in ON DUPLICATE KEY UPDATE section; the
148
     * value will not be escaped, although fully-qualified identifiers in the
149
     * value will be quoted.
150
     *
151
     * @param string $col The column name.
152
     *
153
     * @param string $value The column value expression.
154
     *
155
     * @return $this
156
     *
157
     */
158 1
    public function onDuplicateKeyUpdate($col, $value)
159
    {
160 1
        if ($value === null) {
161 1
            $value = 'NULL';
162
        }
163
164 1
        $key = $this->quoter->quoteName($col);
165 1
        $value = $this->quoter->quoteNamesIn($value);
166 1
        $this->col_on_update_values[$key] = $value;
167 1
        return $this;
168
    }
169
170
    /**
171
     *
172
     * Builds this query object into a string.
173
     *
174
     * @return string
175
     *
176
     */
177 11
    protected function build()
178
    {
179
        return 'INSERT'
180 11
            . $this->buildFlags()
181 11
            . $this->buildInto()
182 11
            . $this->buildValuesForInsert()
183 11
            . $this->buildValuesForUpdateOnDuplicateKey()
184 11
            . $this->buildReturning();
185
    }
186
187
    /**
188
     *
189
     * Builds the UPDATE ON DUPLICATE KEY part of the statement.
190
     *
191
     * @return string
192
     *
193
     */
194 11
    protected function buildValuesForUpdateOnDuplicateKey()
195
    {
196 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...
197 10
            return ''; // not applicable
198
        }
199
200 1
        $values = array();
201 1
        foreach ($this->col_on_update_values as $key => $row) {
202 1
            $values[] = $this->indent(array($key . ' = ' . $row));
203
        }
204
205
        return ' ON DUPLICATE KEY UPDATE'
206 1
            . implode (',', $values);
207
    }
208
}
209