Completed
Pull Request — 3.x (#131)
by Paul
01:50
created

AbstractDmlQuery::addReturning()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
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;
10
11
/**
12
 *
13
 * Abstract query object for data manipulation (Insert, Update, and Delete).
14
 *
15
 * @package Aura.SqlQuery
16
 *
17
 */
18
abstract class AbstractDmlQuery extends AbstractQuery
19
{
20
    /**
21
     *
22
     * Column values for INSERT or UPDATE queries; the key is the column name and the
23
     * value is the column value.
24
     *
25
     * @param array
26
     *
27
     */
28
    protected $col_values;
29
30
    /**
31
     *
32
     * Does the query have any columns in it?
33
     *
34
     * @return bool
35
     *
36
     */
37
    public function hasCols()
38
    {
39
        return !empty($this->col_values);
40
    }
41
42
    /**
43
     *
44
     * Sets one column value placeholder; if an optional second parameter is
45
     * passed, that value is bound to the placeholder.
46
     *
47
     * @param string $col The column name.
48
     *
49
     * @param array $value Value of the column
50
     *
51
     * @return $this
52
     */
53
    protected function addCol($col, ...$value)
54
    {
55
        $key = $this->quoter->quoteName($col);
56
        $this->col_values[$key] = ":$col";
57
        if (count($value) > 0) {
58
            $this->bindValue($col, $value[0]);
59
        }
60
        return $this;
61
    }
62 2
63
    /**
64
     *
65 2
     * Sets multiple column value placeholders. If an element is a key-value
66
     * pair, the key is treated as the column name and the value is bound to
67
     * that column.
68
     *
69 2
     * @param array $cols A list of column names, optionally as key-value
70
     * pairs where the key is a column name and the value is a bind value for
71
     * that column.
72
     *
73
     * @return $this
74
     *
75
     */
76
    protected function addCols(array $cols)
77
    {
78
        foreach ($cols as $key => $val) {
79
            if (is_int($key)) {
80
                // integer key means the value is the column name
81
                $this->addCol($val);
82
            } else {
83
                // the key is the column name and the value is a value to
84
                // be bound to that column
85 37
                $this->addCol($key, $val);
86
            }
87
        }
88
        return $this;
89
    }
90
91
    /**
92
     *
93
     * Sets a column value directly; the value will not be escaped, although
94
     * fully-qualified identifiers in the value will be quoted.
95 1
     *
96
     * @param string $col The column name.
97 37
     *
98 37
     * @param string $value The column value expression.
99
     *
100
     * @return $this
101
     *
102
     */
103
    protected function setCol($col, $value)
104
    {
105
        if ($value === null) {
106
            $value = 'NULL';
107
        }
108
109
        $key = $this->quoter->quoteName($col);
110
        $value = $this->quoter->quoteNamesIn($value);
111
        $this->col_values[$key] = $value;
112 31
        return $this;
113
    }
114
}
115