QueryEngine::getCountQuery()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2014-2018 James Ekow Abaka Ainooson
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace ntentan\nibii;
28
29
class QueryEngine
30
{
31
    private $db;
32
33 32
    public function setDriver($driver)
34
    {
35 32
        $this->db = $driver;
36 32
    }
37
38
    /**
39
     * Generates an SQL insert query string for the model based on the fields
40
     * currently stored in the model.
41
     *
42
     * @param RecordWrapper $model
43
     *
44
     * @return string
45
     */
46 4
    public function getInsertQuery($model)
47
    {
48 4
        $data = $model->getData();
49 4
        $table = $model->getDBStoreInformation()['quoted_table'];
50 4
        $fields = array_keys($data[0]);
51 4
        $quotedFields = [];
52 4
        $valueFields = [];
53
54 4
        foreach ($fields as $field) {
55 4
            $quotedFields[] = $this->db->quoteIdentifier($field);
56 4
            $valueFields[] = ":{$field}";
57
        }
58
59 4
        return 'INSERT INTO '.$table.
60 4
            ' ('.implode(', ', $quotedFields).') VALUES ('.implode(', ', $valueFields).')';
61
    }
62
63 6
    public function getBulkUpdateQuery($data, $parameters)
64
    {
65 6
        $updateData = [];
66 6
        foreach ($data as $field => $value) {
67 6
            $updateData[] = "{$this->db->quoteIdentifier($field)} = :$field";
68
        }
69
70 6
        return sprintf(
71 6
            'UPDATE %s SET %s %s',
72 6
            $parameters->getTable(),
73 6
            implode(', ', $updateData),
74 6
            $parameters->getWhereClause()
75
        );
76
    }
77
78
    /**
79
     * Generates an SQL update query string for the model based on the data
80
     * currently stored in the model.
81
     *
82
     * @param RecordWrapper $model
83
     *
84
     * @return string
85
     */
86 2
    public function getUpdateQuery($model)
87
    {
88 2
        $data = $model->getData();
89 2
        $fields = array_keys($data[0]);
90 2
        $valueFields = [];
91 2
        $conditions = [];
92 2
        $primaryKey = $model->getDescription()->getPrimaryKey();
93
94 2
        foreach ($fields as $field) {
95 2
            $quotedField = $this->db->quoteIdentifier($field);
96
97 2
            if (array_search($field, $primaryKey) !== false) {
98 2
                $conditions[] = "{$quotedField} = :{$field}";
99
            } else {
100 2
                $valueFields[] = "{$quotedField} = :{$field}";
101
            }
102
        }
103
104
        return 'UPDATE '.
105 2
            $model->getDBStoreInformation()['quoted_table'].
106 2
            ' SET '.implode(', ', $valueFields).
107 2
            ' WHERE '.implode(' AND ', $conditions);
108
    }
109
110 24
    public function getSelectQuery($parameters)
111
    {
112 24
        return sprintf(
113 24
            'SELECT %s FROM %s%s%s%s%s',
114 24
            $parameters->getFields(),
115 24
            $parameters->getTable(),
116 24
            $parameters->getWhereClause(),
117 24
            $parameters->getSorts(),
118 24
            $parameters->getLimit(),
119 24
            $parameters->getOffset()
120
        );
121
    }
122
123
    public function getCountQuery($parameters)
124
    {
125
        return sprintf(
126
            'SELECT count(*) as count FROM %s%s',
127
            $parameters->getTable(),
128
            $parameters->getWhereClause()
129
        );
130
    }
131
132 2
    public function getDeleteQuery($parameters)
133
    {
134 2
        return sprintf(
135 2
            'DELETE FROM %s%s',
136 2
            $parameters->getTable(),
137 2
            $parameters->getWhereClause()
138
        );
139
    }
140
}
141