UpdateQueryBuilder   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 50
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A update() 0 39 3
1
<?php
2
/**
3
 * The Update Query builder Class.
4
 *
5
 * @author RN Kushwaha <[email protected]>
6
 * @since v0.0.8 <Date: 29th Dec, 2019>
7
 */
8
9
namespace Dolphin\Builders;
10
11
use Dolphin\Connections\Connection;
12
use \Exception;
13
14
/**
15
 * This class provides the mechanism to build the update Queries.
16
 */
17
class UpdateQueryBuilder extends QueryBuilder
18
{
19
    /**
20
     * It updates the matching rows
21
     *
22
     * @param array $rows
23
     * @return boolean
24
     * @throws Exception
25
     * @author RN Kushwaha <[email protected]>
26
     * @since v0.0.8
27
     */
28
    public function update(
29
      $row,
30
      $table,
31
      $where,
32
      $whereRaw,
33
      $whereIn,
34
      $whereNotIn,
35
      $whereNull,
36
      $whereNotNull
37
    ): bool
38
    {
39
        $wqb   = new WhereQueryBuilder();
40
        $query = "UPDATE ".$table." SET ";
41
        $ar    = [];
42
43
        foreach($row as $key => $val){
44
            $ar[':'.$key] = $val;
45
            $query.= $this->quote($key)." =:".$key.",";
46
        }
47
48
        $query = rtrim($query, ",");
49
50
        try{
51
            $whereQuery = $wqb->buildAllWhereQuery(
52
                                $where,
53
                                $whereRaw,
54
                                $whereIn,
55
                                $whereNotIn,
56
                                $whereNull,
57
                                $whereNotNull
58
                            );
59
            $query.= " ".join(" ", $whereQuery);
60
            $stmt = Connection::get()->prepare($this->queryPrefix($query));
61
            $stmt->execute($ar);
62
        } catch(Exception $e){
63
            throw new Exception($e->getMessage());
64
        }
65
66
        return true;
67
    }
68
}
69