Update::mountQuery()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Willry\QueryBuilder;
4
5
class Update extends Base
6
{
7
    protected function mountQuery(): void
8
    {
9
10
        $dateSet = [];
11
        foreach ($this->fields as $bind => $value) {
12
            $dateSet[] = "{$bind} = ?";
13
        }
14
        $dateSet = implode(", ", $dateSet);
15
16
        $this->query = "UPDATE {$this->entity} {$this->joins} SET {$dateSet} {$this->where}";
17
    }
18
19
    /**
20
     * @param array $data
21
     */
22
    public function update(array $data): static
23
    {
24
        $this->fields = $data;
25
26
        return $this;
27
    }
28
29
    /**
30
     * @param array $data
31
     */
32
    public function exec(): ?int
33
    {
34
        try {
35
            $this->mountQuery();
36
37
            $stmt = $this->db->prepare($this->query);
38
39
            QueryHelpers::bind($stmt, $this->flatBindings());
40
41
            $stmt->execute();
42
43
            return $stmt->rowCount() ?? 1;
44
        } catch (\PDOException $exception) {
45
            $this->handleError($exception);
46
        }
47
    }
48
}
49