-
Notifications
You must be signed in to change notification settings - Fork 1
/
SelectQuery.php
195 lines (174 loc) · 5.1 KB
/
SelectQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
namespace Tivins\Database;
use Tivins\Database\Tests\SelectTest;
class SelectQuery extends Query
{
private string $tableAlias;
private array $fields = [];
private array $expressions = [];
private array $joins = [];
private array $orders = [];
private array $limits = [];
private ?Conditions $having = null;
private string $groupByExp;
public function __construct(Database $db, string $tableName, string $tableAlias)
{
parent::__construct($db, $tableName);
$this->tableAlias = $tableAlias;
}
/**
*
*/
public function addField(string $tableAlias, string $field, string $fieldAlias = ''): self
{
$this->fields[] = "`$tableAlias`.`$field`" . ($fieldAlias ? " as `$fieldAlias`" : '');
return $this;
}
/**
*
*/
public function addFields(string $tableAlias, ?array $fields = null): self
{
if (is_null($fields)) {
$this->fields[] = "`$tableAlias`.*";
} else {
foreach ($fields as $field) {
$this->fields[] = "`$tableAlias`.`$field`";
}
}
return $this;
}
/**
*
*/
public function addExpression(string $expression, string $fieldAlias, array $values = []): self
{
$this->expressions[] = ['sql' => "$expression as `$fieldAlias`", 'data' => $values];
return $this;
}
/**
* Add a count() field in the current query.
*
* ```php
* $query->addCount('*');
* $query->addCount('b.*');
* $query->addCount('b.author', 'nb_authors');
*
* $total = $db->select('table','t')->addCount('*')->execute()->fetchField();
*
* ```
* @param string $field The field to count. It could be '*'.
* @param string|null $fieldAlias The alias for the expression. If null, no alias will be generated.
*/
public function addCount(string $field, ?string $fieldAlias = null): self
{
$this->expressions[] = ['sql' => "count($field)" . ($fieldAlias ? " as `$fieldAlias`" : ''), 'data' => []];
return $this;
}
/**
*
*/
public function leftJoin(string $tableName, string $tableAlias, string $condition): self
{
$tableName = $this->db->prefixTableName($tableName);
$this->joins[] = "left join `$tableName` `$tableAlias` on $condition";
return $this;
}
/**
*
*/
public function rightJoin(string $tableName, string $tableAlias, string $condition): self
{
$tableName = $this->db->prefixTableName($tableName);
$this->joins[] = "right join `$tableName` `$tableAlias` on $condition";
return $this;
}
/**
*
*/
public function innerJoin(string $tableName, string $tableAlias, string $condition): self
{
$tableName = $this->db->prefixTableName($tableName);
$this->joins[] = "inner join `$tableName` `$tableAlias` on $condition";
return $this;
}
/**
*
*/
public function orderBy($field, $dir): self
{
$this->orders[] = "$field $dir";
return $this;
}
/**
*
*/
public function groupBy($exp): self
{
$this->groupByExp = $exp;
return $this;
}
/**
* @see SelectTest::testLimits()
*/
public function limit($count): self
{
$this->limits = [$count];
return $this;
}
/**
*
*/
public function limitFrom($offset, $count): self
{
$this->limits = [$offset, $count];
return $this;
}
/**
*
*/
public function having(Conditions $condition): self
{
$this->having = $condition;
return $this;
}
/**
*
*/
public function build(): QueryData
{
$args = [];
// fields
$what = $this->fields;
if (!empty($this->expressions)) {
$what = array_merge($what, array_column($this->expressions, 'sql'));
$args = array_merge($args, ...array_column($this->expressions, 'data'));
}
$what = implode(',', $what);
$from = "$this->tableName `$this->tableAlias`";
$joins = empty($this->joins) ? '' : ' ' . implode(' ', $this->joins);
$queryData = $this->buildConditions();
$condSql = $queryData->getPrefixed(' where ');
$args = array_merge($args, $queryData->parameters);
$order = '';
$group = '';
$having = '';
if (!empty($this->orders)) {
$order .= ' order by ' . implode(', ', $this->orders);
}
if (!empty($this->groupByExp)) {
$group .= ' group by ' . $this->groupByExp;
}
$limits = '';
if (!empty($this->limits)) {
$limits = ' limit ' . join(',', $this->limits);
}
if (!is_null($this->having)) {
$queryData = $this->having->buildConditions();
$having = $queryData->getPrefixed(' having ');
$args = array_merge($args, $queryData->parameters);
}
$sql = trim("select $what from $from$joins$condSql$group$having$order$limits");
return new QueryData($sql, $args);
}
}