Completed
Push — master ( 4deaab...ecf49a )
by Joao
04:00 queued 01:34
created

Query::getWhere()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 4
nop 0
crap 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jg
5
 * Date: 21/06/16
6
 * Time: 12:01
7
 */
8
9
namespace ByJG\MicroOrm;
10
11
12
use ByJG\AnyDataset\DbFunctionsInterface;
13
14
class Query
15
{
16
    protected $fields = [];
17
    protected $table = "";
18
    protected $where = [];
19
    protected $groupBy = [];
20
    protected $orderBy = [];
21
    protected $join = [];
22
    
23
    protected $forUpdate = false;
24
25
    /**
26
     * Example:
27
     *   $query->fields(['name', 'price']);
28
     * 
29
     * @param array $fields
30
     * @return $this
31
     */
32 14
    public function fields(array $fields)
33
    {
34 14
        $this->fields = array_merge($this->fields, (array)$fields);
35
        
36 14
        return $this;
37
    }
38
39
    /**
40
     * Example
41
     *    $query->table('product');
42
     * 
43
     * @param string $table
44
     * @return $this
45
     */
46 25
    public function table($table) 
47
    {
48 25
        $this->table = $table;
49
50 25
        return $this;
51
    }
52
53
    /**
54
     * Example:
55
     *    $query->join('sales', 'product.id = sales.id');
56
     * 
57
     * @param string $table
58
     * @param string $filter
59
     * @return $this
60
     */
61 1
    public function join($table, $filter)
62
    {
63 1
        $this->join[] = [ 'table'=>$table, 'filter'=>$filter, 'type' => 'INNER'];
64 1
        return $this;
65
    }
66
67
    /**
68
     * Example:
69
     *    $query->join('sales', 'product.id = sales.id');
70
     *
71
     * @param string $table
72
     * @param string $filter
73
     * @return $this
74
     */
75 1
    public function leftJoin($table, $filter)
76
    {
77 1
        $this->join[] = [ 'table'=>$table, 'filter'=>$filter, 'type' => 'LEFT'];
78 1
        return $this;
79
    }
80
81
    /**
82
     * Example:
83
     *    $query->filter('price > [[amount]]', [ 'amount' => 1000] );
84
     * 
85
     * @param string $filter
86
     * @param array $params
87
     * @return $this
88
     */
89 20
    public function where($filter, array $params = [])
90
    {
91 20
        $this->where[] = [ 'filter' => $filter, 'params' => $params  ];
92 20
        return $this;
93
    }
94
95
    /**
96
     * Example:
97
     *    $query->groupBy(['name']);
98
     * 
99
     * @param array $fields
100
     * @return $this
101
     */
102 1
    public function groupBy(array $fields)
103
    {
104 1
        $this->groupBy = array_merge($this->groupBy, $fields);
105
    
106 1
        return $this;
107
    }
108
109
    /**
110
     * Example:
111
     *     $query->orderBy(['price desc']);
112
     * 
113
     * @param array $fields
114
     * @return $this
115
     */
116 4
    public function orderBy(array $fields)
117
    {
118 4
        $this->orderBy = array_merge($this->orderBy, $fields);
119
120 4
        return $this;
121
    }
122
123
    public function forUpdate()
124
    {
125
        $this->forUpdate = true;
126
        
127
        return $this;
128
    }
129
    
130 20
    protected function getFields()
131
    {
132 20
        if (empty($this->fields)) {
133 18
            return ' * ';
134
        }
135
136 3
        return ' ' . implode(', ', $this->fields) . ' ';
137
    }
138
    
139 20
    protected function getJoin()
140
    {
141 20
        $join = $this->table;
142 20
        foreach ($this->join as $item) {
143 2
            $join .= ' ' . $item['type'] . ' JOIN ' . $item['table'] . ' ON ' . $item['filter'];
144 20
        }
145 20
        return $join;
146
    }
147
    
148 24
    protected function getWhere() 
149
    {
150 24
        $where = [];
151 24
        $params = [];
152
153 24
        foreach ($this->where as $item) {
154 20
            $where[] = $item['filter'];
155 20
            $params = array_merge($params, $item['params']);
156 24
        }
157
        
158 24
        if (empty($where)) {
159 5
            return null;
160
        }
161
        
162 20
        return [ implode(' AND ', $where), $params ];
163
    }
164
165
    /**
166
     * @return array
167
     */
168 20
    public function getSelect()
169
    {
170
        $sql = "SELECT " .
171 20
            $this->getFields() . 
172 20
            "FROM " . $this->getJoin();
173
        
174 20
        $where = $this->getWhere();
175 20
        $params = null;
176 20
        if (!is_null($where)) {
177 18
            $sql .= ' WHERE ' . $where[0];
178 18
            $params = $where[1];
179 18
        }
180
        
181 20
        if (!empty($this->groupBy)) {
182 1
            $sql .= ' GROUP BY ' . implode(', ', $this->groupBy);
183 1
        }
184
185 20
        if (!empty($this->orderBy)) {
186 4
            $sql .= ' ORDER BY ' . implode(', ', $this->orderBy);
187 4
        }
188
        
189 20
        if ($this->forUpdate) {
190
            $sql .= ' FOR UPDATE ';
191
        }
192
193 20
        return [ 'sql' => $sql, 'params' => $params ];
194
    }
195
196
    /**
197
     * @param \ByJG\AnyDataset\DbFunctionsInterface|null $dbHelper
198
     * @return string
199
     * @throws \Exception
200
     */
201 6
    public function getInsert(DbFunctionsInterface $dbHelper = null)
202
    {
203 6
        if (empty($this->fields)) {
204
            throw new \Exception('You must specifiy the fields for insert');
205
        }
206
207 6
        $fields = $this->fields;
208 6
        if (!is_null($dbHelper)) {
209 6
            $fields = $dbHelper->delimiterField($fields);
210 6
        }
211
212 6
        $table = $this->table;
213 6
        if (!is_null($dbHelper)) {
214 6
            $table = $dbHelper->delimiterTable($table);
215 6
        }
216
217
        $sql = 'INSERT INTO '
218
            . $table
219 6
            . '( ' . implode(', ', $fields) . ' ) '
220 6
            . ' values '
221 6
            . '( [[' . implode(']], [[', $this->fields) . ']] ) ';
222
        
223 6
        return $sql;
224
    }
225
226
    /**
227
     * @param \ByJG\AnyDataset\DbFunctionsInterface|null $dbHelper
228
     * @return array
229
     * @throws \Exception
230
     */
231 5
    public function getUpdate(DbFunctionsInterface $dbHelper = null)
232
    {
233 5
        if (empty($this->fields)) {
234
            throw new \Exception('You must specifiy the fields for insert');
235
        }
236
        
237 5
        $fields = [];
238 5
        foreach ($this->fields as $field) {
239 5
            $fieldName = $field;
240 5
            if (!is_null($dbHelper)) {
241 4
                $fieldName = $dbHelper->delimiterField($fieldName);
242 4
            }
243 5
            $fields[] = "$fieldName = [[$field]] ";
244 5
        }
245
        
246 5
        $where = $this->getWhere();
247 5
        if (is_null($where)) {
248 1
            throw new \Exception('You must specifiy a where clause');
249
        }
250
251 4
        $tableName = $this->table;
252 4
        if (!is_null($dbHelper)) {
253 4
            $tableName = $dbHelper->delimiterTable($tableName);
254 4
        }
255
256 4
        $sql = 'UPDATE ' . $tableName . ' SET '
257 4
            . implode(', ', $fields)
258 4
            . ' WHERE ' . $where[0];
259
260 4
        return [ 'sql' => $sql, 'params' => $where[1] ];
261
    }
262
263
    /**
264
     * @return array
265
     * @throws \Exception
266
     */
267 5
    public function getDelete()
268
    {
269 5
        $where = $this->getWhere();
270 5
        if (is_null($where)) {
271 1
            throw new \Exception('You must specifiy a where clause');
272
        }
273
274 4
        $sql = 'DELETE FROM ' . $this->table
275 4
            . ' WHERE ' . $where[0];
276
277 4
        return [ 'sql' => $sql, 'params' => $where[1] ];
278
    }
279
280
}
281