Completed
Push — master ( 5df011...747e00 )
by Changwan
03:50
created

UpdateQuery   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 70%

Importance

Changes 0
Metric Value
dl 0
loc 62
ccs 14
cts 20
cp 0.7
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A set() 0 5 1
A addSet() 0 5 1
A getBindings() 0 4 1
A toSql() 0 12 3
1
<?php
2
namespace Wandu\Database\Query;
3
4
use Wandu\Database\Contracts\QueryInterface;
5
use Wandu\Database\Query\Expression\HasWhereExpression;
6
use Wandu\Database\Support\Helper;
7
8
class UpdateQuery extends HasWhereExpression implements QueryInterface
9
{
10
    /** @var string */
11
    protected $table;
12
13
    /** @var array */
14
    protected $attributes;
15
    
16
    /**
17
     * @param string $table
18
     * @param array $attributes
19
     */
20 2
    public function __construct($table, array $attributes = [])
21
    {
22 2
        $this->table = $table;
23 2
        $this->attributes = $attributes;
24 2
    }
25
26
    /**
27
     * @param array $attributes
28
     * @return static
29
     */
30
    public function set(array $attributes = [])
31
    {
32
        $this->attributes = $attributes;
33
        return $this;
34
    }
35
36
    /**
37
     * @param array $attributes
38
     * @return static
39
     */
40
    public function addSet(array $attributes = [])
41
    {
42
        $this->attributes = array_merge($this->attributes, $attributes);
43
        return $this;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 2
    public function toSql()
50
    {
51 2
        $parts = ['UPDATE `' . $this->table . '`'];
52 2
        if (count($this->attributes)) {
53 2
            $columns = array_keys($this->attributes);
54 2
            $parts[] = "SET " . Helper::arrayImplode(', ', $columns, "`", "` = ?");
55
        }
56 2
        if ($part = parent::toSql()) {
57 2
            $parts[] = $part;
58
        }
59 2
        return implode(' ', $parts);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 2
    public function getBindings()
66
    {
67 2
        return array_merge(array_values($this->attributes), parent::getBindings());
68
    }
69
}
70