1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Yolk - Gamer Network's PHP Framework. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2015 Gamer Network Ltd. |
6
|
|
|
* |
7
|
|
|
* Distributed under the MIT License, a copy of which is available in the |
8
|
|
|
* LICENSE file that was bundled with this package, or online at: |
9
|
|
|
* https://github.com/gamernetwork/yolk-database |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace yolk\database\query; |
13
|
|
|
|
14
|
|
|
use yolk\contracts\database\DatabaseConnection; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Generic. |
18
|
|
|
*/ |
19
|
|
|
class Select extends BaseQuery { |
20
|
|
|
|
21
|
|
|
protected $cols; |
22
|
|
|
protected $distinct; |
23
|
|
|
protected $from; |
24
|
|
|
protected $group; |
25
|
|
|
protected $having; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
public function __construct( DatabaseConnection $db ) { |
29
|
|
|
parent::__construct($db); |
30
|
|
|
$this->cols = []; |
31
|
|
|
$this->distinct = false; |
32
|
|
|
$this->from = ''; |
33
|
|
|
$this->group = []; |
34
|
|
|
$this->having = []; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function cols( $columns = ['*'] ) { |
38
|
|
|
|
39
|
|
|
// default to everything |
40
|
|
|
if( !$columns ) |
|
|
|
|
41
|
|
|
$columns = ['*']; |
42
|
|
|
|
43
|
|
|
// if we don't have an array of columns then they were specified as individual arguments |
44
|
|
|
elseif( !is_array($columns) ) |
45
|
|
|
$columns = func_get_args(); |
46
|
|
|
|
47
|
|
|
// $columns = [ |
48
|
|
|
// 'column', |
49
|
|
|
// ['column', 'alias'], |
|
|
|
|
50
|
|
|
// 'id', |
51
|
|
|
// ['related_id', 'related'], |
|
|
|
|
52
|
|
|
// ]; |
53
|
|
|
|
54
|
|
|
$this->cols = $columns; |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// use raw columns statement |
61
|
|
|
public function colsRaw( $sql ) { |
62
|
|
|
$this->cols = $sql; |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function distinct( $distinct = true ) { |
67
|
|
|
$this->distinct = (bool) $distinct; |
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function from( $table ) { |
72
|
|
|
$this->from = $this->quoteIdentifier($table); |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function fromRaw( $sql ) { |
77
|
|
|
$this->from = $sql; |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function groupBy( $columns ) { |
82
|
|
|
|
83
|
|
|
if( !is_array($columns) ) |
84
|
|
|
$columns = [$columns]; |
85
|
|
|
|
86
|
|
|
foreach( $columns as $column ) { |
87
|
|
|
$this->group[] = $this->quoteIdentifier($column); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function having( $having ) { |
95
|
|
|
$this->having = [$having]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function __call( $method, $args ) { |
99
|
|
|
|
100
|
|
|
if( !in_array($method, ['getOne', 'getCol', 'getRow', 'getAssoc', 'getAll']) ) |
101
|
|
|
throw new \BadMethodCallException("Unknown Method: {$method}"); |
102
|
|
|
|
103
|
|
|
return $this->db->$method( |
104
|
|
|
$this->__toString(), |
105
|
|
|
$this->params |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function compile() { |
111
|
|
|
|
112
|
|
|
$cols = $this->cols; |
113
|
|
|
|
114
|
|
|
if( is_array($cols) ) |
115
|
|
|
$cols = $this->compileCols($cols); |
116
|
|
|
|
117
|
|
|
return array_merge( |
118
|
|
|
[ |
119
|
|
|
($this->distinct ? 'SELECT DISTINCT' : 'SELECT'). ' '. $cols, |
120
|
|
|
'FROM '. $this->from, |
121
|
|
|
], |
122
|
|
|
$this->compileJoins(), |
123
|
|
|
$this->compileWhere(), |
124
|
|
|
$this->compileGroupBy(), |
125
|
|
|
$this->having, |
126
|
|
|
$this->compileOrderBy(), |
127
|
|
|
$this->compileOffsetLimit() |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
protected function compileCols( array $cols ) { |
133
|
|
|
|
134
|
|
|
foreach( $cols as &$col ) { |
135
|
|
|
|
136
|
|
|
// if column is an array is should have two elements |
137
|
|
|
// the first being the column name, the second being the alias |
138
|
|
|
if( is_array($col) ) { |
139
|
|
|
list($column, $alias) = $col; |
140
|
|
|
$col = sprintf( |
141
|
|
|
'%s AS %s', |
142
|
|
|
$this->quoteIdentifier($column), |
143
|
|
|
$this->db->quoteIdentifier($alias) |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
else { |
147
|
|
|
$col = $this->quoteIdentifier($col); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return implode(', ', $cols); |
153
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
protected function compileGroupBy() { |
157
|
|
|
|
158
|
|
|
$sql = []; |
159
|
|
|
|
160
|
|
|
if( $this->group ) |
|
|
|
|
161
|
|
|
$sql[] = 'GROUP BY '. implode(', ', $this->group); |
162
|
|
|
|
163
|
|
|
return $sql; |
164
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
// EOF |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.