Completed
Push — master ( debdfd...7461bf )
by Joao
02:41
created

Query   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 263
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
wmc 30
lcom 3
cbo 0
dl 0
loc 263
ccs 77
cts 99
cp 0.7778
rs 10
c 0
b 0
f 0

17 Methods

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