Completed
Push — master ( 391062...ff47c5 )
by Rasmus
02:37
created

UpdateQuery::assign()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
3
namespace mindplay\sql\model;
4
5
use mindplay\sql\framework\Driver;
6
use mindplay\sql\framework\TypeProvider;
7
8
/**
9
 * This class represents an UPDATE query.
10
 */
11
class UpdateQuery extends ProjectionQuery
12
{
13
    /**
14
     * @var mixed[] map where Column name => literal SQL expression to assign
15
     */
16
    private $assignments = [];
17
18
    /**
19
     * @param Column|string $column Column to update (or Column name)
20
     * @param mixed         $value  value to apply
21
     *
22
     * @return $this
23
     */
24 1
    public function setValue($column, $value)
25
    {
26 1 View Code Duplication
        if ($column instanceof Column) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27 1
            $name = $this->getPlaceholder($column);
28
29 1
            $quoted_name = "{$column}";
30
        } else {
31
            $name = $column;
32
33
            $quoted_name = $this->driver->quoteName($name);
34
        }
35
36 1
        $this->assignments[$name] = "{$quoted_name} = :{$name}";
37
38 1
        $this->bind($name, $value, $column->getType());
0 ignored issues
show
Bug introduced by
It seems like $column is not always an object, but can also be of type string. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
39
40 1
        return $this;
41
    }
42
43
    /**
44
     * @param Column|string $column Column to update (or Column name)
45
     * @param string        $expr   literal SQL expression
46
     *
47
     * @return $this
48
     */
49 1
    public function setExpr($column, $expr)
50
    {
51 1 View Code Duplication
        if ($column instanceof Column) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52 1
            $name = $this->getPlaceholder($column);
53
54 1
            $quoted_name = "{$column}";
55
        } else {
56
            $name = $column;
57
58
            $quoted_name = $this->driver->quoteName($name);
59
        }
60
61 1
        $this->assignments[$name] = "{$quoted_name} = {$expr}";
62
63 1
        return $this;
64
    }
65
66
    /**
67
     * @param array $values map where Column name => scalar values to assign
68
     *
69
     * @return $this
70
     */
71 1
    public function assign(array $values)
72
    {
73 1
        $columns = $this->root->listColumns();
74
75 1
        foreach ($columns as $column) {
76 1
            $name = $column->getName();
77
78 1
            if (array_key_exists($name, $values)) {
79 1
                $this->setValue($column, $values[$name]);
80
            }
81
        }
82
83 1
        return $this;
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89 1 View Code Duplication
    public function getSQL()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91 1
        $update = "UPDATE " . $this->buildNodes();
92
93 1
        $set = "\nSET " . implode(",\n    ", $this->assignments);
94
95 1
        $where = count($this->conditions)
96 1
            ? "\nWHERE " . $this->buildConditions()
97 1
            : ''; // no conditions present
98
99 1
        $order = count($this->order)
100
            ? "\nORDER BY " . $this->buildOrderTerms()
101 1
            : ''; // no order terms
102
103 1
        $limit = $this->limit !== null
104
            ? "\nLIMIT {$this->limit}"
105
            . ($this->offset !== null ? " OFFSET {$this->offset}" : '')
106 1
            : ''; // no limit or offset
107
108 1
        return "{$update}{$set}{$where}{$order}{$limit}";
109
    }
110
111
    /**
112
     * @param Column $column
113
     *
114
     * @return string
115
     */
116 1
    private function getPlaceholder(Column $column)
117
    {
118 1
        $table = $column->getTable();
119
120 1
        $table_name = $table->getAlias() ?: $table->getName();
121
122 1
        $column_name = $column->getName();
123
124 1
        $name = "{$table_name}_{$column_name}";
125
126 1
        return $name;
127
    }
128
}
129