Completed
Push — 3.x ( e5eb1c...76cecd )
by Paul
9s
created

Insert::delayed()   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
     * if true, use a REPLACE sql command instead of INSERT
25
     *
26
     * @var bool
27
     *
28
     */
29
    protected $use_replace = false;
30
31
    /**
32
     *
33
     * Column values for ON DUPLICATE KEY UPDATE section of query; the key is
34
     * the column name and the value is the column value.
35
     *
36
     * @param array
37
     *
38
     */
39
    protected $col_on_update_values;
40
41
    /**
42
     *
43
     * Use a REPLACE statement.
44
     * Matches similar orReplace() function for Sqlite
45
     *
46
     * @param bool $enable Set or unset flag (default true).
47
     *
48
     * @return $this
49
     *
50
     */
51 1
    public function orReplace($enable = true)
52
    {
53 1
        $this->use_replace = $enable;
54 1
        return $this;
55
    }
56
57
    /**
58
     *
59
     * Adds or removes HIGH_PRIORITY flag.
60
     *
61
     * @param bool $enable Set or unset flag (default true).
62
     *
63
     * @return $this
64
     *
65
     */
66 1
    public function highPriority($enable = true)
67
    {
68 1
        $this->setFlag('HIGH_PRIORITY', $enable);
69 1
        return $this;
70
    }
71
72
    /**
73
     *
74
     * Adds or removes LOW_PRIORITY flag.
75
     *
76
     * @param bool $enable Set or unset flag (default true).
77
     *
78
     * @return $this
79
     *
80
     */
81 1
    public function lowPriority($enable = true)
82
    {
83 1
        $this->setFlag('LOW_PRIORITY', $enable);
84 1
        return $this;
85
    }
86
87
    /**
88
     *
89
     * Adds or removes IGNORE flag.
90
     *
91
     * @param bool $enable Set or unset flag (default true).
92
     *
93
     * @return $this
94
     *
95
     */
96 1
    public function ignore($enable = true)
97
    {
98 1
        $this->setFlag('IGNORE', $enable);
99 1
        return $this;
100
    }
101
102
    /**
103
     *
104
     * Adds or removes DELAYED flag.
105
     *
106
     * @param bool $enable Set or unset flag (default true).
107
     *
108
     * @return $this
109
     *
110
     */
111 1
    public function delayed($enable = true)
112
    {
113 1
        $this->setFlag('DELAYED', $enable);
114 1
        return $this;
115
    }
116
117
    /**
118
     *
119
     * Sets one column value placeholder in ON DUPLICATE KEY UPDATE section;
120
     * if an optional second parameter is passed, that value is bound to the
121
     * placeholder.
122
     *
123
     * @param string $col The column name.
124
     *
125
     * @param array $value Optional: a value to bind to the placeholder.
126
     *
127
     * @return $this
128
     *
129
     */
130 1
    public function onDuplicateKeyUpdateCol($col, ...$value)
131
    {
132 1
        $key = $this->quoter->quoteName($col);
133 1
        $bind = $col . '__on_duplicate_key';
134 1
        $this->col_on_update_values[$key] = ":$bind";
135 1
        if (count($value) > 0) {
136 1
            $this->bindValue($bind, $value[0]);
137
        }
138 1
        return $this;
139
    }
140
141
    /**
142
     *
143
     * Sets multiple column value placeholders in ON DUPLICATE KEY UPDATE
144
     * section. If an element is a key-value pair, the key is treated as the
145
     * column name and the value is bound to that column.
146
     *
147
     * @param array $cols A list of column names, optionally as key-value
148
     * pairs where the key is a column name and the value is a bind value for
149
     * that column.
150
     *
151
     * @return $this
152
     *
153
     */
154 1
    public function onDuplicateKeyUpdateCols(array $cols)
155
    {
156 1
        foreach ($cols as $key => $val) {
157 1
            if (is_int($key)) {
158
                // integer key means the value is the column name
159 1
                $this->onDuplicateKeyUpdateCol($val);
160
            } else {
161
                // the key is the column name and the value is a value to
162
                // be bound to that column
163 1
                $this->onDuplicateKeyUpdateCol($key, $val);
164
            }
165
        }
166 1
        return $this;
167
    }
168
169
    /**
170
     *
171
     * Sets a column value directly in ON DUPLICATE KEY UPDATE section; the
172
     * value will not be escaped, although fully-qualified identifiers in the
173
     * value will be quoted.
174
     *
175
     * @param string $col The column name.
176
     *
177
     * @param string $value The column value expression.
178
     *
179
     * @return $this
180
     *
181
     */
182 1
    public function onDuplicateKeyUpdate($col, $value)
183
    {
184 1
        if ($value === null) {
185 1
            $value = 'NULL';
186
        }
187
188 1
        $key = $this->quoter->quoteName($col);
189 1
        $value = $this->quoter->quoteNamesIn($value);
190 1
        $this->col_on_update_values[$key] = $value;
191 1
        return $this;
192
    }
193
194
    /**
195
     *
196
     * Builds this query object into a string.
197
     *
198
     * @return string
199
     *
200
     */
201 12
    protected function build()
202
    {
203 12
        $stm = parent::build();
204
205 12
        if ($this->use_replace) {
206
            // change INSERT to REPLACE
207 1
            $stm = 'REPLACE' . substr($stm, 6);
208
        }
209
210
        return $stm
211 12
            . $this->builder->buildValuesForUpdateOnDuplicateKey($this->col_on_update_values);
0 ignored issues
show
Bug introduced by
The property builder does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
212
    }
213
}
214