Completed
Push — master ( ad83ed...4c8d32 )
by James Ekow Abaka
03:09
created

QueryEngine::getUpdateQuery()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 24
ccs 0
cts 15
cp 0
rs 8.9713
cc 3
eloc 16
nc 3
nop 1
crap 12
1
<?php
2
namespace ntentan\nibii;
3
4
class QueryEngine
5
{
6
7
    private $db;
8
9 26
    public function setDriver($driver)
10
    {
11 26
        $this->db = $driver;
12 26
    }
13
    
14 26
    private function filter($query)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
15
    {
16 26
        return $query;
17
    }
18
19
    public function getInsertQuery($model)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
20
    {
21
        $data = $model->getData();
22
        $fields = array_keys($data[0]);
23
        $quotedFields = [];
24
        $valueFields = [];
25
26
        foreach ($fields as $field) {
27
            $quotedFields[] = $this->db->quoteIdentifier($field);
28
            $valueFields[] = ":{$field}";
29
        }
30
31
        return $this->filter(
32
            "INSERT INTO " . $this->db->quoteIdentifier($model->getTable()) .
33
            " (" . implode(", ", $quotedFields) . ") VALUES (" . implode(', ', $valueFields) . ")"
34
        );
35
    }
36
37 6
    public function getBulkUpdateQuery($data, $parameters)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
38
    {
39 6
        $updateData = [];
40 6
        foreach($data as $field => $value) {
41 6
            $updateData[] = "{$this->db->quoteIdentifier($field)} = :$field";
42
        }
43
44 6
        return $this->filter(sprintf(
45 6
            "UPDATE %s SET %s %s",
46 6
            $parameters->getTable(),
47 6
            implode(', ', $updateData),
48 6
            $parameters->getWhereClause()
49
        ));
50
    }
51
52
    public function getUpdateQuery($model)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
53
    {
54
        $data = $model->getData();
55
        $fields = array_keys($data[0]);
56
        $valueFields = [];
57
        $conditions = [];
58
        $primaryKey = $model->getDescription()->getPrimaryKey();
59
60
        foreach ($fields as $field) {
61
            $quotedField = $this->db->quoteIdentifier($field);
62
63
            if(array_search($field, $primaryKey) !== false) {
64
                $conditions[] = "{$quotedField} = :{$field}";
65
            } else {
66
                $valueFields[] = "{$quotedField} = :{$field}";
67
            }
68
        }
69
70
        return $this->filter("UPDATE " .
71
            $this->db->quoteIdentifier($model->getTable()) .
72
            " SET " . implode(', ', $valueFields) .
73
            " WHERE " . implode(' AND ', $conditions)
74
        );
75
    }
76
77 18
    public function getSelectQuery($parameters)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
78
    {
79 18
        return $this->filter(sprintf(
80 18
                "SELECT %s FROM %s%s%s%s%s",
81 18
                $parameters->getFields(),
82 18
                $parameters->getTable(),
83 18
                $parameters->getWhereClause(),
84 18
                $parameters->getSorts(),
85 18
                $parameters->getLimit(),
86 18
                $parameters->getOffset()
87
            )
88
        );
89
    }
90
    
91
    public function getCountQuery($parameters)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
92
    {
93
        return $this->filter(sprintf(
94
                "SELECT count(*) as count FROM %s%s",
95
                $parameters->getTable(),
96
                $parameters->getWhereClause()
97
            )
98
        );
99
    }
100
101 2
    public function getDeleteQuery($parameters)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
102
    {
103 2
        return $this->filter(sprintf(
104 2
                "DELETE FROM %s%s",
105 2
                $parameters->getTable(),
106 2
                $parameters->getWhereClause()
107
            )
108
        );
109
    }
110
}
111