ValuesStatement::getInsertValues()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @see http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
12
namespace JAQB\Statement;
13
14
class ValuesStatement extends Statement
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $insertRows = [];
20
21
    /**
22
     * Adds values to the statement.
23
     *
24
     * @return self
25
     */
26
    public function addValues(array $values)
27
    {
28
        // Check if this is a multi-dimensional array
29
        $isMultiDimensional = 0 == count($values) || (isset($values[0]) && is_array($values[0]));
30
31
        if ($isMultiDimensional) {
32
            $this->insertRows = array_merge($this->insertRows, $values);
33
        } else {
34
            // If a single row is provided then it is added
35
            // to the last row, or a new row if there are none.
36
            // This is done to maintain BC
37
            if (count($this->insertRows) > 0) {
38
                $k = count($this->insertRows) - 1;
39
                $this->insertRows[$k] = array_replace($this->insertRows[$k], $values);
40
            } else {
41
                $this->insertRows = [$values];
42
            }
43
        }
44
45
        return $this;
46
    }
47
48
    /**
49
     * @deprecated
50
     * Gets the values being inserted (first row only).
51
     *
52
     * WARNING: this only returns the first row for BC
53
     *
54
     * @return array
55
     */
56
    public function getInsertValues()
57
    {
58
        return count($this->insertRows) > 0 ? $this->insertRows[0] : [];
59
    }
60
61
    /**
62
     * Gets the rows being inserted.
63
     *
64
     * @return array
65
     */
66
    public function getInsertRows()
67
    {
68
        return $this->insertRows;
69
    }
70
71
    public function build()
72
    {
73
        // reset the parameterized values
74
        $this->values = [];
75
76
        // get the list of fields from the first row
77
        $keys = [];
78
        $fields = [];
79
        if (count($this->insertRows) > 0) {
80
            foreach (array_keys($this->insertRows[0]) as $key) {
81
                if ($id = $this->escapeIdentifier($key)) {
82
                    $fields[] = $id;
83
                    $keys[] = $key;
84
                }
85
            }
86
        }
87
88
        if (0 == count($fields)) {
89
            return '';
90
        }
91
92
        foreach ($this->insertRows as $row) {
93
            foreach ($keys as $key) {
94
                $this->values[] = isset($row[$key]) ? $row[$key] : null;
95
            }
96
        }
97
98
        $rowPlaceholders = '('.implode(', ', array_fill(0, count($fields), '?')).')';
99
100
        // produces "(`col1`,`col2`,`col3`) VALUES (?,?,?), (?,?,?)"
101
        return '('.implode(', ', $fields).') VALUES '
102
            .implode(', ', array_fill(0, count($this->insertRows), $rowPlaceholders));
103
    }
104
}
105