summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/sql/src/Update.php
blob: 356a6105b753fd84402b67d921f1c11e14030e15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php

namespace ipl\Sql;

use InvalidArgumentException;

use function ipl\Stdlib\arrayval;

/**
 * SQL UPDATE query
 */
class Update implements CommonTableExpressionInterface, WhereInterface
{
    use CommonTableExpression;
    use Where;

    /** @var array|null The table for the UPDATE query */
    protected $table;

    /** @var array|null The columns to update in terms of column-value pairs */
    protected $set = [];

    /**
     * Get the table for the UPDATE query
     *
     * @return array|null
     */
    public function getTable()
    {
        return $this->table;
    }

    /**
     * Set the table for the UPDATE query
     *
     * Note that this method does NOT quote the table you specify for the UPDATE.
     * If you allow user input here, you must protected yourself against SQL injection using
     * {@link Connection::quoteIdentifier()} for the table names passed to this method.
     * If you are using special table names, e.g. reserved keywords for your DBMS, you are required to use
     * {@link Connection::quoteIdentifier()} as well.
     *
     * @param string|array $table The table to update. The table specification must be in one of the
     *                            following formats: 'table', 'table alias', ['alias' => 'table']
     *
     * @return $this
     */
    public function table($table)
    {
        $this->table = is_array($table) ? $table : [$table];

        return $this;
    }

    /**
     * Get the columns to update in terms of column-value pairs
     *
     * @return array|null
     */
    public function getSet()
    {
        return $this->set;
    }

    /**
     * Set the columns to update in terms of column-value pairs
     *
     * Values may either be plain or expressions or scalar subqueries.
     *
     * Note that this method does NOT quote the columns you specify for the UPDATE.
     * If you allow user input here, you must protected yourself against SQL injection using
     * {@link Connection::quoteIdentifier()} for the column names passed to this method.
     * If you are using special column names, e.g. reserved keywords for your DBMS, you are required to use
     * {@link Connection::quoteIdentifier()} as well.
     *
     * @param iterable $set Associative set of column-value pairs
     *
     * @return $this
     *
     * @throws InvalidArgumentException If set type is invalid
     */
    public function set($set)
    {
        $this->set = arrayval($set);

        return $this;
    }

    public function __clone()
    {
        $this->cloneCte();
        $this->cloneWhere();

        foreach ($this->set as &$value) {
            if ($value instanceof ExpressionInterface || $value instanceof Select) {
                $value = clone $value;
            }
        }
        unset($value);
    }
}