Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Update
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
5 / 5
9
100.00% covered (success)
100.00%
1 / 1
 sql
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 valuesSQL
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 params
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 reset
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 allTablesReferenced
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace BO\Zmsdb\Query\Builder;
4
5/**
6 * Update
7 *
8 * Generates an SQL query for an UPDATE operation.
9 *
10 * @package     BO\Zmsdb\Query\Builder
11 * @author      Alex Gisby<alex@solution10.com>
12 * @license     MIT
13 */
14class Update extends Query
15{
16    use TableName;
17    use Values;
18    use Where;
19    use Paginate;
20
21    /**
22     * @var     string      The base part of the query
23     */
24    protected $queryBase = 'UPDATE';
25
26    /**
27     * Generates the full SQL statement for this query with all the composite parts.
28     *
29     * @return  string
30     */
31    #[\Override]
32    public function sql()
33    {
34        if ($this->table === null) {
35            return '';
36        }
37
38        $candidateParts = [
39            $this->queryBase,
40            $this->dialect->quoteTable($this->table),
41            $this->valuesSQL(),
42            $this->buildWhereSQL($this->dialect),
43            $this->buildPaginateSQL()
44        ];
45
46        return implode(' ', $candidateParts);
47    }
48
49    /**
50     * Returns the values part of a query for an UPDATE statement (so key = value, key = value)
51     *
52     * @return  string
53     */
54    protected function valuesSQL()
55    {
56        $sql = '';
57        if (!empty($this->values)) {
58            $sql .= 'SET ';
59            $parts = [];
60            foreach (array_keys($this->values) as $field) {
61                $parts[] = $this->dialect->quoteField($field) . ' = ?';
62            }
63            $sql .= implode(', ', $parts);
64        }
65        return $sql;
66    }
67
68    /**
69     * Returns all the parameters, in the correct order, to pass into PDO.
70     *
71     * @return  array
72     */
73    #[\Override]
74    public function params()
75    {
76        return array_merge(array_values($this->values), $this->getWhereParams());
77    }
78
79    /**
80     * Resets the entire query.
81     *
82     * @return  $this
83     */
84    #[\Override]
85    public function reset()
86    {
87        $this->table = null;
88        $this->resetValues();
89        $this->resetWhere();
90        return $this;
91    }
92
93    /*
94     * ------------------- All Tables ------------------------
95     */
96
97    /**
98     * Returns all the tables that this query makes mention of, in FROMs and JOINs
99     *
100     * @return  array
101     */
102    #[\Override]
103    public function allTablesReferenced()
104    {
105        if ($this->table()) {
106            return [$this->table()];
107        }
108        return [];
109    }
110}