Completed
Push — master ( 145f77...f73370 )
by James Ekow Abaka
04:01
created

QueryEngine::getSelectQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 3
Metric Value
c 3
b 0
f 3
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
crap 1
1
<?php
2
namespace ntentan\nibii;
3
4
class QueryEngine
5
{
6
7
    private $db;
8
9 32
    public function setDriver($driver)
10
    {
11 32
        $this->db = $driver;
12 32
    }
13
    
14 32
    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 32
        return $query;
17
    }
18
19 4
    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 4
        $data = $model->getData();
22 4
        $fields = array_keys($data[0]);
23 4
        $quotedFields = [];
24 4
        $valueFields = [];
25
26 4
        foreach ($fields as $field) {
27 4
            $quotedFields[] = $this->db->quoteIdentifier($field);
28 4
            $valueFields[] = ":{$field}";
29
        }
30
31 4
        return $this->filter(
32 4
            "INSERT INTO " . $this->db->quoteIdentifier($model->getTable()) .
33 4
            " (" . 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 2
    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 2
        $data = $model->getData();
55 2
        $fields = array_keys($data[0]);
56 2
        $valueFields = [];
57 2
        $conditions = [];
58 2
        $primaryKey = $model->getDescription()->getPrimaryKey();
59
60 2
        foreach ($fields as $field) {
61 2
            $quotedField = $this->db->quoteIdentifier($field);
62
63 2
            if(array_search($field, $primaryKey) !== false) {
64 2
                $conditions[] = "{$quotedField} = :{$field}";
65
            } else {
66 2
                $valueFields[] = "{$quotedField} = :{$field}";
67
            }
68
        }
69
70 2
        return $this->filter("UPDATE " .
71 2
            $this->db->quoteIdentifier($model->getTable()) .
72 2
            " SET " . implode(', ', $valueFields) .
73 2
            " WHERE " . implode(' AND ', $conditions)
74
        );
75
    }
76
77 24
    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 24
        return $this->filter(sprintf(
80 24
                "SELECT %s FROM %s%s%s%s%s",
81 24
                $parameters->getFields(),
82 24
                $parameters->getTable(),
83 24
                $parameters->getWhereClause(),
84 24
                $parameters->getSorts(),
85 24
                $parameters->getLimit(),
86 24
                $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