|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* Author: Adrian Dumitru |
|
5
|
|
|
* Date: 4/22/2017 4:17 AM |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Qpdb\QueryBuilder\Statements; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
use Qpdb\PdoWrapper\PdoWrapperService; |
|
12
|
|
|
use Qpdb\QueryBuilder\Dependencies\QueryHelper; |
|
13
|
|
|
use Qpdb\QueryBuilder\Dependencies\QueryStructure; |
|
14
|
|
|
use Qpdb\QueryBuilder\QueryBuild; |
|
15
|
|
|
use Qpdb\QueryBuilder\Traits\DefaultPriority; |
|
16
|
|
|
use Qpdb\QueryBuilder\Traits\Distinct; |
|
17
|
|
|
use Qpdb\QueryBuilder\Traits\GroupBy; |
|
18
|
|
|
use Qpdb\QueryBuilder\Traits\Having; |
|
19
|
|
|
use Qpdb\QueryBuilder\Traits\HighPriority; |
|
20
|
|
|
use Qpdb\QueryBuilder\Traits\Join; |
|
21
|
|
|
use Qpdb\QueryBuilder\Traits\Limit; |
|
22
|
|
|
use Qpdb\QueryBuilder\Traits\OrderBy; |
|
23
|
|
|
use Qpdb\QueryBuilder\Traits\Replacement; |
|
24
|
|
|
use Qpdb\QueryBuilder\Traits\SelectFields; |
|
25
|
|
|
use Qpdb\QueryBuilder\Traits\Utilities; |
|
26
|
|
|
use Qpdb\QueryBuilder\Traits\Where; |
|
27
|
|
|
use Qpdb\QueryBuilder\Traits\WhereAndHavingBuilder; |
|
28
|
|
|
|
|
29
|
|
|
class QuerySelect extends QueryStatement implements QueryStatementInterface |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
use SelectFields, Limit, Distinct, Where, Having, WhereAndHavingBuilder, Replacement, OrderBy, GroupBy, Join, DefaultPriority, HighPriority, Utilities; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $statement = self::QUERY_STATEMENT_SELECT; |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* QuerySelect constructor. |
|
42
|
|
|
* @param QueryBuild $queryBuild |
|
43
|
|
|
* @param string|QueryStatement $table |
|
44
|
|
|
* @throws \Qpdb\QueryBuilder\Dependencies\QueryException |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct( QueryBuild $queryBuild, $table ) |
|
47
|
|
|
{ |
|
48
|
|
|
parent::__construct( $queryBuild, $table ); |
|
49
|
|
|
|
|
50
|
|
|
if ( is_a( $table, QuerySelect::class ) ) { |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var QuerySelect $table |
|
54
|
|
|
*/ |
|
55
|
|
|
$tableName = '( ' . $table->getSyntax() . ' )'; |
|
56
|
|
|
$this->queryStructure->setElement( QueryStructure::TABLE, $tableName ); |
|
57
|
|
|
|
|
58
|
|
|
$tableSelectParams = $table->getBindParams(); |
|
59
|
|
|
foreach ( $tableSelectParams as $key => $value ) |
|
60
|
|
|
$this->queryStructure->setParams( $key, $value ); |
|
61
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return $this |
|
67
|
|
|
* @throws \Qpdb\QueryBuilder\Dependencies\QueryException |
|
68
|
|
|
*/ |
|
69
|
|
|
public function first() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->queryStructure->setElement( QueryStructure::FIRST, 1 ); |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return $this |
|
78
|
|
|
* @throws \Qpdb\QueryBuilder\Dependencies\QueryException |
|
79
|
|
|
*/ |
|
80
|
|
|
public function count() |
|
81
|
|
|
{ |
|
82
|
|
|
$this->queryStructure->setElement( QueryStructure::COUNT, 1 ); |
|
83
|
|
|
|
|
84
|
|
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param $column |
|
89
|
|
|
* @return $this |
|
90
|
|
|
* @throws \Qpdb\QueryBuilder\Dependencies\QueryException |
|
91
|
|
|
*/ |
|
92
|
|
|
public function column( $column ) |
|
93
|
|
|
{ |
|
94
|
|
|
$column = QueryHelper::clearMultipleSpaces( $column ); |
|
95
|
|
|
$this->queryStructure->setElement( QueryStructure::COLUMN, $column ); |
|
96
|
|
|
|
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param bool|int $replacement |
|
103
|
|
|
* @return mixed|string |
|
104
|
|
|
* @throws \Qpdb\QueryBuilder\Dependencies\QueryException |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getSyntax( $replacement = self::REPLACEMENT_NONE ) |
|
107
|
|
|
{ |
|
108
|
|
|
|
|
109
|
|
|
if ( $this->queryStructure->getElement( QueryStructure::COUNT ) ) { |
|
110
|
|
|
$this->queryStructure->replaceElement( QueryStructure::FIELDS, 'COUNT(*)' ); |
|
111
|
|
|
$this->queryStructure->setElement( QueryStructure::LIMIT, 1 ); |
|
112
|
|
|
$this->queryStructure->setElement( QueryStructure::DISTINCT, 0 ); //??? |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if ( $this->queryStructure->getElement( QueryStructure::FIRST ) ) |
|
116
|
|
|
$this->queryStructure->setElement( QueryStructure::LIMIT, 1 ); |
|
117
|
|
|
|
|
118
|
|
|
$syntax = array(); |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Explain |
|
122
|
|
|
*/ |
|
123
|
|
|
$syntax[] = $this->getExplainSyntax(); |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* SELECT statement |
|
127
|
|
|
*/ |
|
128
|
|
|
$syntax[] = $this->statement; |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* PRIORITY |
|
132
|
|
|
*/ |
|
133
|
|
|
$syntax[] = $this->queryStructure->getElement( QueryStructure::PRIORITY ); |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* DISTINCT clause |
|
137
|
|
|
*/ |
|
138
|
|
|
$syntax[] = $this->getDistinctSyntax(); |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* FIELDS |
|
142
|
|
|
*/ |
|
143
|
|
|
$syntax[] = $this->getSelectFieldsSyntax(); |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* FROM table or queryStructure |
|
147
|
|
|
*/ |
|
148
|
|
|
$syntax[] = 'FROM ' . $this->queryStructure->getElement( QueryStructure::TABLE ); |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* JOIN CLAUSES |
|
152
|
|
|
*/ |
|
153
|
|
|
$syntax[] = $this->getJoinSyntax(); |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* WHERE clause |
|
157
|
|
|
*/ |
|
158
|
|
|
$syntax[] = $this->getWhereSyntax(); |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* GROUP BY clause |
|
162
|
|
|
*/ |
|
163
|
|
|
$syntax[] = $this->getGroupBySyntax(); |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* HAVING clause |
|
167
|
|
|
*/ |
|
168
|
|
|
$syntax[] = $this->getHavingSyntax(); |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* ORDER BY clause |
|
172
|
|
|
*/ |
|
173
|
|
|
$syntax[] = $this->getOrderBySyntax(); |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* LIMIT clause |
|
177
|
|
|
*/ |
|
178
|
|
|
$syntax[] = $this->getLimitSyntax(); |
|
179
|
|
|
|
|
180
|
|
|
$syntax = implode( ' ', $syntax ); |
|
181
|
|
|
|
|
182
|
|
|
return $this->getSyntaxReplace( $syntax, $replacement ); |
|
183
|
|
|
|
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @return array |
|
188
|
|
|
* @throws \Qpdb\QueryBuilder\Dependencies\QueryException |
|
189
|
|
|
*/ |
|
190
|
|
|
public function execute() |
|
191
|
|
|
{ |
|
192
|
|
|
switch ( true ) { |
|
193
|
|
|
case $this->queryStructure->getElement( QueryStructure::COUNT ): |
|
194
|
|
|
return PdoWrapperService::getInstance()->queryFetch( $this->getSyntax(), $this->queryStructure->getElement( QueryStructure::BIND_PARAMS ), \PDO::FETCH_COLUMN ); |
|
195
|
|
|
case $this->queryStructure->getElement( QueryStructure::FIRST ): |
|
196
|
|
|
if ( $this->queryStructure->getElement( QueryStructure::COLUMN ) ) { |
|
197
|
|
|
return PdoWrapperService::getInstance()->queryFetch( $this->getSyntax(), $this->queryStructure->getElement( QueryStructure::BIND_PARAMS ), \PDO::FETCH_COLUMN ); |
|
198
|
|
|
} |
|
199
|
|
|
return PdoWrapperService::getInstance()->queryFetch( $this->getSyntax(), $this->queryStructure->getElement( QueryStructure::BIND_PARAMS ), \PDO::FETCH_ASSOC ); |
|
200
|
|
|
case $this->queryStructure->getElement( QueryStructure::COLUMN ): |
|
201
|
|
|
return PdoWrapperService::getInstance()->queryFetchAll( $this->getSyntax(), $this->queryStructure->getElement( QueryStructure::BIND_PARAMS ), \PDO::FETCH_COLUMN ); |
|
202
|
|
|
default: |
|
203
|
|
|
return PdoWrapperService::getInstance()->queryFetchAll($this->getSyntax(), $this->queryStructure->getElement( QueryStructure::BIND_PARAMS ), \PDO::FETCH_ASSOC); |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
} |