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

AbstractDmlQuery::buildReturning()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
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 8
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
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 5
    public function hasCols()
38
    {
39 5
        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 68
    protected function addCol($col, ...$value)
54
    {
55 68
        $key = $this->quoter->quoteName($col);
56 68
        $this->col_values[$key] = ":$col";
57 68
        if (count($value) > 0) {
58 36
            $this->bindValue($col, $value[0]);
59
        }
60 68
        return $this;
61
    }
62
63
    /**
64
     *
65
     * 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
     * @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 67
    protected function addCols(array $cols)
77
    {
78 67
        foreach ($cols as $key => $val) {
79 67
            if (is_int($key)) {
80
                // integer key means the value is the column name
81 37
                $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 67
                $this->addCol($key, $val);
86
            }
87
        }
88 67
        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
     *
96
     * @param string $col The column name.
97
     *
98
     * @param string $value The column value expression.
99
     *
100
     * @return $this
101
     *
102
     */
103 46
    protected function setCol($col, $value)
104
    {
105 46
        if ($value === null) {
106 31
            $value = 'NULL';
107
        }
108
109 46
        $key = $this->quoter->quoteName($col);
110 46
        $value = $this->quoter->quoteNamesIn($value);
111 46
        $this->col_values[$key] = $value;
112 46
        return $this;
113
    }
114
}
115