Passed
Push — master ( 125489...69b805 )
by Joao
04:53
created

Query::getInsert()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 8
cp 0.875
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 0
crap 2.0078
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 3
    public function fields(array $fields)
34
    {
35 3
        $this->fields = array_merge($this->fields, $fields);
36
        
37 3
        return $this;
38
    }
39
40
    /**
41
     * Example
42
     *    $query->table('product');
43
     * 
44
     * @param string $table
45
     * @return $this
46
     */
47 7
    public function table($table) 
48
    {
49 7
        $this->table = $table;
50
51 7
        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];
65 1
        return $this;
66
    }
67
68
    /**
69
     * Example:
70
     *    $query->filter('price > [[amount]]', [ 'amount' => 1000] );
71
     * 
72
     * @param string $filter
73
     * @param array $params
74
     * @return $this
75
     */
76 7
    public function where($filter, array $params = [])
77
    {
78 7
        $this->where[] = [ 'filter' => $filter, 'params' => $params  ];
79 7
        return $this;
80
    }
81
82
    /**
83
     * Example:
84
     *    $query->groupBy(['name']);
85
     * 
86
     * @param array $fields
87
     * @return $this
88
     */
89
    public function groupBy(array $fields)
90
    {
91
        $this->groupBy = array_merge($this->groupBy, $fields);
92
    
93
        return $this;
94
    }
95
96
    /**
97
     * Example:
98
     *     $query->orderBy(['price desc']);
99
     * 
100
     * @param array $fields
101
     * @return $this
102
     */
103 2
    public function orderBy(array $fields)
104
    {
105 2
        $this->orderBy = array_merge($this->orderBy, $fields);
106
107 2
        return $this;
108
    }
109
110
    public function limit($start, $end)
111
    {
112
        $this->limitStart = $start;
113
        $this->limitEnd = $end;
114
        return $this;
115
    }
116
117
    public function top($top)
118
    {
119
        $this->top = $top;
120
121
        return $this;
122
    }
123
    
124
    public function forUpdate()
125
    {
126
        $this->forUpdate = true;
127
        
128
        return $this;
129
    }
130
    
131 7
    protected function getFields()
132
    {
133 7
        if (empty($this->fields)) {
134 6
            return ' * ';
135
        }
136
137 1
        return ' ' . implode(', ', $this->fields) . ' ';
138
    }
139
    
140 7
    protected function getJoin()
141
    {
142 7
        $join = $this->table;
143 7
        foreach ($this->join as $item) {
144 1
            $join .= ' INNER JOIN ' . $item['table'] . ' ON ' . $item['filter'];
145 7
        }
146 7
        return $join;
147
    }
148
    
149 7
    protected function getWhere() 
150
    {
151 7
        $where = [];
152 7
        $params = [];
153
154 7
        foreach ($this->where as $item) {
155 7
            $where[] = $item['filter'];
156 7
            $params = array_merge($params, $item['params']);
157 7
        }
158
        
159 7
        if (empty($where)) {
160
            return null;
161
        }
162
        
163 7
        return [ implode(' AND ', $where), $params ];
164
    }
165
166
    /**
167
     * @return array
168
     */
169 7
    public function getSelect()
170
    {
171
        $sql = "SELECT " .
172 7
            $this->getFields() . 
173 7
            "FROM " . $this->getJoin();
174
        
175 7
        $where = $this->getWhere();
176 7
        $params = null;
177 7
        if (!is_null($where)) {
178 7
            $sql .= ' WHERE ' . $where[0];
179 7
            $params = $where[1];
180 7
        }
181
        
182 7
        if (!empty($this->groupBy)) {
183
            $sql .= ' GROUP BY ' . implode(', ', $this->groupBy);
184
        }
185
186 7
        if (!empty($this->orderBy)) {
187 2
            $sql .= ' ORDER BY ' . implode(', ', $this->orderBy);
188 2
        }
189
        
190 7
        if ($this->forUpdate) {
191
            $sql .= ' FOR UPDATE ';
192
        } 
193
        
194 7
        return [ 'sql' => $sql, 'params' => $params ];
195
    }
196
197
    /**
198
     * @return string
199
     * @throws \Exception
200
     */
201 1
    public function getInsert()
202
    {
203 1
        if (empty($this->fields)) {
204
            throw new \Exception('You must specifiy the fields for insert');
205
        }
206
        
207
        $sql = 'INSERT INTO '
208 1
            . $this->table
209 1
            . '( ' . implode(', ', $this->fields) . ' ) '
210 1
            . ' values '
211 1
            . '( [[' . implode(']], [[', $this->fields) . ']] ) ';
212
        
213 1
        return $sql;
214
    }
215
216
    /**
217
     * @return array
218
     * @throws \Exception
219
     */
220 1
    public function getUpdate()
221
    {
222 1
        if (empty($this->fields)) {
223
            throw new \Exception('You must specifiy the fields for insert');
224
        }
225
        
226 1
        $fields = [];
227 1
        foreach ($this->fields as $field) {
228 1
            $fields[] = "$field = [[$field]] ";
229 1
        }
230
        
231 1
        $where = $this->getWhere();
232 1
        if (is_null($where)) {
233
            throw new \Exception('You must specifiy a where clause');
234
        }
235
236 1
        $sql = 'UPDATE ' . $this->table . ' SET '
237 1
            . implode(', ', $fields)
238 1
            . ' WHERE ' . $where[0];
239
240 1
        return [ 'sql' => $sql, 'params' => $where[1] ];
241
    }
242
243
    /**
244
     * @return array
245
     * @throws \Exception
246
     */
247 2
    public function getDelete()
248
    {
249 2
        $where = $this->getWhere();
250 2
        if (is_null($where)) {
251
            throw new \Exception('You must specifiy a where clause');
252
        }
253
254 2
        $sql = 'DELETE FROM ' . $this->table
255 2
            . ' WHERE ' . $where[0];
256
257 2
        return [ 'sql' => $sql, 'params' => $where[1] ];
258
    }
259
260
}
261