Completed
Push — master ( 2b9193...abd76f )
by James Ekow Abaka
03:07
created

QueryEngine::getUpdateQuery()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
ccs 15
cts 15
cp 1
cc 3
eloc 16
nc 3
nop 1
crap 3
1
<?php
2
3
namespace ntentan\nibii;
4
5
class QueryEngine {
6
7
    private $db;
8
9 34
    public function setDriver($driver) {
10 34
        $this->db = $driver;
11 34
    }
12
 
13 34
    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...
14 34
        return $query;
15
    }
16
17
    /**
18
     * Generates an SQL insert query string for the model based on the fields
19
     * currently stored in the model.
20
     * 
21
     * @param RecordWrapper $model
22
     * @return string
23
     */
24 4
    public function getInsertQuery($model) {
25 4
        $data = $model->getData();
26 4
        $table = $model->getDBStoreInformation()['quoted_table'];
27 4
        $fields = array_keys($data[0]);
28 4
        $quotedFields = [];
29 4
        $valueFields = [];
30
31 4
        foreach ($fields as $field) {
32 4
            $quotedFields[] = $this->db->quoteIdentifier($field);
33 4
            $valueFields[] = ":{$field}";
34
        }
35
36 4
        return $this->filter(
37 4
            "INSERT INTO " . $table .
38 4
            " (" . implode(", ", $quotedFields) . ") VALUES (" . implode(', ', $valueFields) . ")"
39
        );
40
    }
41
42 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...
43 6
        $updateData = [];
44 6
        foreach ($data as $field => $value) {
45 6
            $updateData[] = "{$this->db->quoteIdentifier($field)} = :$field";
46
        }
47
48 6
        return $this->filter(sprintf(
49 6
            "UPDATE %s SET %s %s", 
50 6
            $parameters->getTable(), 
51 6
            implode(', ', $updateData), 
52 6
            $parameters->getWhereClause()
53
        ));
54
    }
55
56
    /**
57
     * Generates an SQL update query string for the model based on the data
58
     * currently stored in the model.
59
     * 
60
     * @param RecordWrapper $model
61
     * @return string
62
     */    
63 2
    public function getUpdateQuery($model) {
64 2
        $data = $model->getData();
65 2
        $fields = array_keys($data[0]);
66 2
        $valueFields = [];
67 2
        $conditions = [];
68 2
        $primaryKey = $model->getDescription()->getPrimaryKey();
69
70 2
        foreach ($fields as $field) {
71 2
            $quotedField = $this->db->quoteIdentifier($field);
72
73 2
            if (array_search($field, $primaryKey) !== false) {
74 2
                $conditions[] = "{$quotedField} = :{$field}";
75
            } else {
76 2
                $valueFields[] = "{$quotedField} = :{$field}";
77
            }
78
        }
79
80 2
        return $this->filter("UPDATE " .
81 2
            $model->getDBStoreInformation()['quoted_table'] .
82 2
            " SET " . implode(', ', $valueFields) .
83 2
            " WHERE " . implode(' AND ', $conditions)
84
        );
85
    }
86
87 26
    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...
88 26
        return $this->filter(sprintf(
89 26
                "SELECT %s FROM %s%s%s%s%s", 
90 26
                $parameters->getFields(), 
91 26
                $parameters->getTable(), 
92 26
                $parameters->getWhereClause(), 
93 26
                $parameters->getSorts(), 
94 26
                $parameters->getLimit(), 
95 26
                $parameters->getOffset()
96
            )
97
        );
98
    }
99
100
    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...
101
        return $this->filter(sprintf(
102
                "SELECT count(*) as count FROM %s%s", 
103
                $parameters->getTable(), 
104
                $parameters->getWhereClause()
105
            )
106
        );
107
    }
108
109 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...
110 2
        return $this->filter(sprintf(
111 2
                "DELETE FROM %s%s", 
112 2
                $parameters->getTable(), 
113 2
                $parameters->getWhereClause()
114
            )
115
        );
116
    }
117
118
}
119