-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueryBuilderController.php
427 lines (354 loc) · 14 KB
/
QueryBuilderController.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<?php
namespace App\Controller;
use App\Entity\Product;
use App\Entity\ProductCategory;
use App\Entity\PurchaseOrder;
use App\Entity\PurchaseOrderProduct;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class QueryBuilderController
* @package App\Controller
* @Route("/query-builder")
*/
class QueryBuilderController extends AbstractController
{
// https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/query-builder.html
// https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/dql-doctrine-query-language.html
/**
* @var EntityManagerInterface
*/
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* @Route("/", name="qb")
*/
public function index()
{
return $this->json(['QueryBuilderController@index']);
}
/**
* Example: /query-builder/like?likeString=/1
* @Route("/like", name="qb_like")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function like(Request $request)
{
$likeString = $request->query->get('likeString') ?? "/4";
$qb = $this->entityManager->createQueryBuilder();
$query = $qb->select('p.id, p.name')
->from(Product::class, 'p')
->andWhere('p.name like :val')
->setParameter('val', "%$likeString%")
->getQuery();
return $this->json([
'URL example' => '/query-builder/like?likeString=/1',
'query' => $query->getSQL(),
'parameter:likeString' => $likeString,
'result' => $query->getResult(),
]);
}
/**
* @Route("/order-by", name="qb_order_by")
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function orderBy()
{
$qb = $this->entityManager->createQueryBuilder();
$query = $qb->select('p.id', 'p.description', 'p.isAvailable')
->from(Product::class, 'p')
->addOrderBy('p.isAvailable', 'DESC')
->addOrderBy('p.description', 'ASC')
->getQuery();
return $this->json([
'NOTE:' => '-> orderBy() overrides all previously set ordering conditions. When having multiple ->orderBy() => use ->addOrderBy()',
'query' => $query->getSQL(),
'result' => $query->getResult(),
]);
}
/**
* @Route("/limit", name="qb_limit")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function limit(Request $request)
{
$maxResults = $request->query->get('maxResults') ?? "2";
$qb = $this->entityManager->createQueryBuilder();
$query = $qb->select('p')
->from(Product::class, 'p')
->setMaxResults($maxResults)
->getQuery();
$data = [
'URL example' => '/limit?maxResults=2',
'query' => $query->getSQL(),
'parameter:maxResults' => $maxResults,
'result' => $query->getResult(),
];
return $this->json($data, 200, [], ['groups' => 'products:read']);
}
/**
* @Route("/max", name="qb_max")
*/
public function max()
{
// https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/query-builder.html
$qb = $this->entityManager->createQueryBuilder();
$query = $qb->select("MAX(p.price) AS MAX_PRICE")
->from(Product::class, 'p')
->getQuery();
$qb = $this->entityManager->createQueryBuilder();
$query_expr = $qb
->select($qb->expr()->max('pr.price') . " AS MAX_PRICE")
->from(Product::class, 'pr')
->getQuery();
return $this->json([
$query->getSQL(),
$query->getResult(),
$query_expr->getSQL(),
$query_expr->getResult(),
]);
}
/**
* @Route("/having", name="qb_having")
*/
public function having(Request $request)
{
$minNumber = $request->query->get('minNumber') ?? 2;
$qb = $this->entityManager->createQueryBuilder();
$query = $qb->select('pc.name', "COUNT(p) AS PRODUCT_COUNT")
->from(Product::class, 'p')
->leftJoin('p.productCategory', 'pc')
->groupBy('pc.name')
->having("PRODUCT_COUNT > $minNumber")
->getQuery();
// Important! Create another query builder for the second query!
$qb = $this->entityManager->createQueryBuilder();
$query_expr = $qb->select('pct.name', $qb->expr()->count('pr') . 'AS PRODUCT_COUNT')
->from(Product::class, 'pr')
->leftJoin('pr.productCategory', 'pct')
->groupBy('pct.name')
->having($qb->expr()->gt('PRODUCT_COUNT', $minNumber))
->getQuery();
$data = [
"info" => [
'The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.',
'The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country".
The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.',
],
'explain query' => "Product categories having more than {$minNumber} products",
'URL example' => '/query-builder/having?minNumber=1',
"query" => [
$query->getSQL(),
$query->getResult(),
],
"query_expr" => [
$query_expr->getSQL(),
$query_expr->getResult(),
],
];
return $this->json($data);
}
/**
* @Route("/inner-join", name="qb_inner_join")
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function innerJoin()
{
$qb = $this->entityManager->createQueryBuilder();
$query = $qb->select('u.id', 'u.email')
->from(User::class, 'u')
->innerJoin('u.purchaseOrders', 'po')
->getQuery();
$result = $query->getResult();
$data = [
'info' => 'The INNER JOIN keyword selects records that have matching values in both tables.',
'query' => $query->getSQL(),
'result' => $result,
];
return $this->json($data, 200, [], ['groups' => 'users:read']);
}
/**
* @Route("/inner-join-with-group-by", name="qb_inner_join_with_group_by")
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function innerJoinWithGroupBy()
{
// Important: other ways to do this: using [exists()] or [in()]
$qb = $this->entityManager->createQueryBuilder();
// Users having at least one order
$query = $qb->select('u.id', 'u.email')
->from(User::class, 'u')
->innerJoin('u.purchaseOrders', 'po')
->groupBy('u.id')
->getQuery();
$data = [
'info' => [
'The INNER JOIN keyword selects records that have matching values in both tables.',
'The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country".
The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.',
],
'explain query' => 'Users having at least one order',
'query' => $query->getSQL(),
'result' => $query->getResult(),
];
return $this->json($data, 200, [], ['groups' => 'users:read']);
}
/**
* @Route("/left-join", name="qb_left_join")
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function leftJoin()
{
$qb = $this->entityManager->createQueryBuilder();
$productCategoryName = 'Prd Categ 3';
$query = $qb->select('u.id', 'u.email')
->from(User::class, 'u')
->leftJoin('u.purchaseOrders', 'po')
->leftJoin('po.purchaseOrderProducts', 'pop')
->leftJoin('pop.product', 'p')
->leftJoin('p.productCategory', 'pc')
->where('pc.name like :pc_name')
->setParameter(':pc_name', $productCategoryName)
->groupBy('u.id')
->getQuery();
$data = [
'info' => [
"The LEFT JOIN keyword returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL from the right side, if there is no match.",
'The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country".
The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.',
],
'explain query' => "Users who bought products that belongs to category '{$productCategoryName}'",
'query' => $query->getSQL(),
'result' => $query->getResult(),
];
return $this->json($data, 200, [], ['groups' => 'users:read']);
}
/**
* @Route("/not-exists", name="qb_not_exists")
*/
public function notExists()
{
$sqb = $this->entityManager->createQueryBuilder();
$subQuery = $sqb->select('u')
->from(PurchaseOrder::class, 'po')
->where('po.user = u.id');
$qb = $this->entityManager->createQueryBuilder();
$query = $qb->select("u.id")
->from(User::class, "u")
->where(
$qb->expr()->not(
$qb->expr()->exists($subQuery)
)
)
->orderBy("u.id")
->getQuery();
$data = [
'info' => 'The EXISTS operator is used to test for the existence of any record in a subquery.
The EXISTS operator returns true if the subquery returns one or more records.',
'query' => $query->getSQL(),
'result' => $query->getResult(),
];
return $this->json($data);
}
/**
* @Route("/exists", name="qb_exists")
*/
public function exists()
{
// Users having at least one order
$sqb = $this->entityManager->createQueryBuilder();
$subQuery = $sqb->select('u')
->from(PurchaseOrder::class, 'po')
->where('po.user = u.id');
$qb = $this->entityManager->createQueryBuilder();
$query = $qb->select("u.id")
->from(User::class, "u")
->where(
$qb->expr()->exists($subQuery)
)
->orderBy("u.id")
->getQuery();
$data = [
'info' => 'The EXISTS operator is used to test for the existence of any record in a subquery.
The EXISTS operator returns true if the subquery returns one or more records.',
'query' => $query->getSQL(),
'result' => $query->getResult(),
];
return $this->json($data);
}
/**
* @Route("/sum", name="qb_sum")
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function sum()
{
$qb = $this->entityManager->createQueryBuilder();
$query = $qb->select("SUM(pop.quantity) as TOTAL_PURCHASED_PRODUCT_QUANTITY")
->from(PurchaseOrderProduct::class, 'pop')
->getQuery();
$data = [
'info' => [
"The SUM() function returns the total sum of a numeric column.",
"The AVG() function returns the average value of a numeric column.",
"The COUNT() function returns the number of rows that matches a specified criteria.",
],
'query' => $query->getSQL(),
'result' => $query->getResult(),
];
return $this->json($data);
}
/**
* @Route("/avg", name="qb_avg")
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function avg()
{
$qb = $this->entityManager->createQueryBuilder();
$query = $qb->select("pc.name as CATEGORY_NAME", "AVG(p.price) AS AVERAGE_PRICE")
->from(ProductCategory::class, 'pc')
->leftJoin('pc.products', 'p')
->groupBy('pc.name')
->getQuery();
$data = [
'info' => [
"The AVG() function returns the average value of a numeric column.",
"The SUM() function returns the total sum of a numeric column.",
"The COUNT() function returns the number of rows that matches a specified criteria.",
],
'query' => $query->getSQL(),
'result' => $query->getResult(),
];
return $this->json($data);
}
/**
* @Route("/count", name="qb_count")
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function count()
{
$qb = $this->entityManager->createQueryBuilder();
$query = $qb->select("u.id as USER_ID", "count(po.id) AS COUNT_PURCHASE_ORDER")
->from(User::class, 'u')
->leftJoin('u.purchaseOrders', 'po')
->groupBy('u.id')
->getQuery();
$data = [
'info' => [
"The COUNT() function returns the number of rows that matches a specified criteria.",
"The AVG() function returns the average value of a numeric column.",
"The SUM() function returns the total sum of a numeric column.",
],
'query' => $query->getSQL(),
'result' => $query->getResult(),
];
return $this->json($data);
}
}