Completed
Push — master ( 0eed13...125489 )
by Joao Gilberto
02:44
created

Query::forUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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
    public function fields(array $fields)
34
    {
35
        $this->fields = array_merge($this->fields, $fields);
36
        
37
        return $this;
38
    }
39
40
    /**
41
     * Example
42
     *    $query->table('product');
43
     * 
44
     * @param string $table
45
     * @return $this
46
     */
47
    public function table($table) 
48
    {
49
        $this->table = $table;
50
51
        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
    public function join($table, $filter)
63
    {
64
        $this->join[] = [ 'table'=>$table, 'filter'=>$filter];
65
        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
    public function where($filter, array $params = [])
77
    {
78
        $this->where[] = [ 'filter' => $filter, 'params' => $params  ];
79
        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
    public function orderBy(array $fields)
104
    {
105
        $this->orderBy = array_merge($this->orderBy, $fields);
106
107
        return $this;
108
    }
109
110
    public function limit($start, $end)
0 ignored issues
show
Unused Code introduced by Joao Gilberto Magalhães
The parameter $start is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by Joao Gilberto Magalhães
The parameter $end is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111
    {
112
113
        return $this;
114
    }
115
116
    public function top($top)
0 ignored issues
show
Unused Code introduced by Joao Gilberto Magalhães
The parameter $top is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
117
    {
118
119
        return $this;
120
    }
121
    
122
    public function forUpdate()
123
    {
124
        $this->forUpdate = true;
125
        
126
        return $this;
127
    }
128
    
129
    protected function getFields()
130
    {
131
        if (empty($this->fields)) {
132
            return ' * ';
133
        }
134
135
        return ' ' . implode(', ', $this->fields) . ' ';
136
    }
137
    
138
    protected function getJoin()
139
    {
140
        $join = $this->table;
141
        foreach ($this->join as $item) {
142
            $join .= ' INNER JOIN ' . $item['table'] . ' ON ' . $item['filter'];
143
        }
144
        return $join;
145
    }
146
    
147
    protected function getWhere() 
148
    {
149
        $where = [];
150
        $params = [];
151
152
        foreach ($this->where as $item) {
153
            $where[] = $item['filter'];
154
            $params = array_merge($params, $item['params']);
155
        }
156
        
157
        if (empty($where)) {
158
            return null;
159
        }
160
        
161
        return [ implode(' AND ', $where), $params ];
162
    }
163
164
    /**
165
     * @return array
166
     */
167
    public function getSelect()
168
    {
169
        $sql = "SELECT " .
170
            $this->getFields() . 
171
            "FROM " . $this->getJoin();
172
        
173
        $where = $this->getWhere();
174
        $params = null;
175
        if (!is_null($where)) {
176
            $sql .= ' WHERE ' . $where[0];
177
            $params = $where[1];
178
        }
179
        
180
        if (!empty($this->groupBy)) {
181
            $sql .= ' GROUP BY ' . implode(', ', $this->groupBy);
182
        }
183
184
        if (!empty($this->orderBy)) {
185
            $sql .= ' ORDER BY ' . implode(', ', $this->orderBy);
186
        }
187
        
188
        if ($this->forUpdate) {
189
            $sql .= ' FOR UPDATE ';
190
        } 
191
        
192
        return [ 'sql' => $sql, 'params' => $params ];
193
    }
194
195
    /**
196
     * @return string
197
     * @throws \Exception
198
     */
199
    public function getInsert()
200
    {
201
        if (empty($this->fields)) {
202
            throw new \Exception('You must specifiy the fields for insert');
203
        }
204
        
205
        $sql = 'INSERT INTO '
206
            . $this->table
207
            . '( ' . implode(', ', $this->fields) . ' ) '
208
            . ' values '
209
            . '( [[' . implode(']], [[', $this->fields) . ']] ) ';
210
        
211
        return $sql;
212
    }
213
214
    /**
215
     * @return array
216
     * @throws \Exception
217
     */
218
    public function getUpdate()
219
    {
220
        if (empty($this->fields)) {
221
            throw new \Exception('You must specifiy the fields for insert');
222
        }
223
        
224
        $fields = [];
225
        foreach ($this->fields as $field) {
226
            $fields[] = "$field = [[$field]] ";
227
        }
228
        
229
        $where = $this->getWhere();
230
        if (is_null($where)) {
231
            throw new \Exception('You must specifiy a where clause');
232
        }
233
234
        $sql = 'UPDATE ' . $this->table . ' SET '
235
            . implode(', ', $fields)
236
            . ' WHERE ' . $where[0];
237
238
        return [ 'sql' => $sql, 'params' => $where[1] ];
239
    }
240
241
242
}
243