|
1
|
|
|
<?php |
|
2
|
|
|
namespace Kir\MySQL\Builder; |
|
3
|
|
|
|
|
4
|
|
|
use Kir\MySQL\Builder\Traits\GroupByBuilder; |
|
5
|
|
|
use Kir\MySQL\Builder\Traits\HavingBuilder; |
|
6
|
|
|
use Kir\MySQL\Builder\Traits\OffsetBuilder; |
|
7
|
|
|
use Kir\MySQL\Builder\Traits\OrderByBuilder; |
|
8
|
|
|
use Kir\MySQL\Builder\Traits\TableBuilder; |
|
9
|
|
|
use Kir\MySQL\Builder\Traits\JoinBuilder; |
|
10
|
|
|
use Kir\MySQL\Builder\Traits\LimitBuilder; |
|
11
|
|
|
use Kir\MySQL\Builder\Traits\TableNameBuilder; |
|
12
|
|
|
use Kir\MySQL\Builder\Traits\UnionBuilder; |
|
13
|
|
|
use Kir\MySQL\Builder\Traits\WhereBuilder; |
|
14
|
|
|
|
|
15
|
|
|
class Select extends Statement { |
|
16
|
|
|
use TableNameBuilder; |
|
17
|
|
|
use TableBuilder; |
|
18
|
|
|
use JoinBuilder; |
|
19
|
|
|
use WhereBuilder; |
|
20
|
|
|
use HavingBuilder; |
|
21
|
|
|
use GroupByBuilder; |
|
22
|
|
|
use OrderByBuilder; |
|
23
|
|
|
use LimitBuilder; |
|
24
|
|
|
use OffsetBuilder; |
|
25
|
|
|
use UnionBuilder; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string[] */ |
|
28
|
|
|
private $fields = array(); |
|
29
|
|
|
/** @var bool */ |
|
30
|
|
|
private $calcFoundRows = false; |
|
31
|
|
|
/** @var bool */ |
|
32
|
|
|
private $forUpdate = false; |
|
33
|
|
|
/** @var bool */ |
|
34
|
|
|
private $distinct = false; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param bool $distinct |
|
38
|
|
|
* @return $this |
|
39
|
|
|
*/ |
|
40
|
|
|
public function distinct($distinct = true) { |
|
41
|
|
|
$this->distinct = $distinct; |
|
42
|
|
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $expression |
|
47
|
|
|
* @param string $alias |
|
48
|
|
|
* @return $this |
|
49
|
|
|
*/ |
|
50
|
|
|
public function field($expression, $alias = null) { |
|
51
|
|
|
if (is_object($expression)) { |
|
52
|
|
|
$expression = (string) $expression; |
|
53
|
|
|
$expression = trim($expression); |
|
54
|
|
|
$expression = rtrim($expression, ';'); |
|
55
|
|
|
$expression = trim($expression); |
|
56
|
|
|
$lines = explode("\n", $expression); |
|
57
|
|
|
$lines = array_map(function($line) { return "\t\t{$line}"; }, $lines); |
|
58
|
|
|
$expression = join("\n", $lines); |
|
59
|
|
|
$expression = sprintf("(\n%s\n\t)", $expression); |
|
60
|
|
|
} |
|
61
|
|
|
if ($alias === null) { |
|
62
|
|
|
$this->fields[] = $expression; |
|
63
|
|
|
} else { |
|
64
|
|
|
$this->fields[$alias] = $expression; |
|
65
|
|
|
} |
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param array $fields |
|
71
|
|
|
* @return $this |
|
72
|
|
|
*/ |
|
73
|
|
|
public function fields(array $fields) { |
|
74
|
|
|
foreach ($fields as $alias => $expression) { |
|
75
|
|
|
$this->field($expression, $alias); |
|
76
|
|
|
} |
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getFields() { |
|
84
|
|
|
return $this->fields; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param bool $enabled |
|
89
|
|
|
* @return $this |
|
90
|
|
|
*/ |
|
91
|
|
|
public function forUpdate($enabled = true) { |
|
92
|
|
|
$this->forUpdate = $enabled; |
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getCalcFoundRows() { |
|
100
|
|
|
return $this->calcFoundRows; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param bool $calcFoundRows |
|
105
|
|
|
* @throws \Exception |
|
106
|
|
|
* @return $this |
|
107
|
|
|
*/ |
|
108
|
|
|
public function setCalcFoundRows($calcFoundRows = true) { |
|
109
|
|
|
if (ini_get("mysql.trace_mode")) { |
|
110
|
|
|
throw new \Exception('This function cant operate with mysql.trace_mode is set.'); |
|
111
|
|
|
} |
|
112
|
|
|
$this->calcFoundRows = $calcFoundRows; |
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string $alias |
|
118
|
|
|
* @param string $tableName |
|
119
|
|
|
* @return $this |
|
120
|
|
|
*/ |
|
121
|
|
|
public function from($alias, $tableName = null) { |
|
122
|
|
|
$this->addTable($alias, $tableName); |
|
123
|
|
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
public function __toString() { |
|
130
|
|
|
$query = "SELECT"; |
|
131
|
|
|
if ($this->calcFoundRows) { |
|
132
|
|
|
$query .= " SQL_CALC_FOUND_ROWS"; |
|
133
|
|
|
} |
|
134
|
|
|
if ($this->distinct) { |
|
135
|
|
|
$query .= " DISTINCT"; |
|
136
|
|
|
} |
|
137
|
|
|
$query .= "\n"; |
|
138
|
|
|
$query = $this->buildFields($query); |
|
139
|
|
|
if (count($this->getTables())) { |
|
140
|
|
|
$query .= "FROM\n"; |
|
141
|
|
|
} |
|
142
|
|
|
$query = $this->buildTables($query); |
|
143
|
|
|
$query = $this->buildJoins($query); |
|
144
|
|
|
$query = $this->buildWhereConditions($query); |
|
145
|
|
|
$query = $this->buildGroups($query); |
|
146
|
|
|
$query = $this->buildHavingConditions($query); |
|
147
|
|
|
$query = $this->buildOrder($query); |
|
148
|
|
|
$query = $this->buildLimit($query); |
|
149
|
|
|
$query = $this->buildOffset($query); |
|
150
|
|
|
$query = $this->buildUnions($query); |
|
151
|
|
|
$query = $this->buildForUpdate($query); |
|
152
|
|
|
return $query; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param string $query |
|
157
|
|
|
* @return string |
|
158
|
|
|
*/ |
|
159
|
|
|
private function buildFields($query) { |
|
160
|
|
|
$fields = array(); |
|
161
|
|
|
if (count($this->fields)) { |
|
162
|
|
|
foreach ($this->fields as $alias => $expression) { |
|
163
|
|
|
if (is_numeric($alias)) { |
|
164
|
|
|
$fields[] = "\t{$expression}"; |
|
165
|
|
|
} else { |
|
166
|
|
|
$fields[] = "\t{$expression} AS `{$alias}`"; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
} else { |
|
170
|
|
|
$fields[] = "\t*"; |
|
171
|
|
|
} |
|
172
|
|
|
return $query.join(",\n", $fields)."\n"; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @param string $query |
|
177
|
|
|
* @return string |
|
178
|
|
|
*/ |
|
179
|
|
|
private function buildForUpdate($query) { |
|
180
|
|
|
if ($this->forUpdate) { |
|
181
|
|
|
$query .= "FOR UPDATE\n"; |
|
182
|
|
|
} |
|
183
|
|
|
return $query; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|