1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hgraca\MicroDbal\Crud\QueryBuilder\Sql; |
4
|
|
|
|
5
|
|
|
use Hgraca\Helper\ArrayHelper; |
6
|
|
|
use Hgraca\MicroDbal\Crud\CrudQueryBuilderInterface; |
7
|
|
|
|
8
|
|
|
final class SqlQueryBuilder implements CrudQueryBuilderInterface |
9
|
|
|
{ |
10
|
4 |
|
public function buildCreateQuery(string $table, array $data): array |
11
|
|
|
{ |
12
|
4 |
|
$createTokenCollection = ArrayHelper::isTwoDimensional($data) |
13
|
2 |
|
? new CreateTokenCollectionCollection($data) |
14
|
4 |
|
: new CreateTokenCollection($data); |
15
|
|
|
|
16
|
4 |
|
$columnNamesList = $createTokenCollection->getColumnList(); |
17
|
4 |
|
$columnPlaceholdersList = $createTokenCollection->getPlaceholderList(); |
18
|
4 |
|
$bindings = $createTokenCollection->getBindingsList(); |
19
|
|
|
|
20
|
|
|
return [ |
21
|
4 |
|
"INSERT INTO `$table` $columnNamesList VALUES $columnPlaceholdersList", |
22
|
4 |
|
$bindings, |
23
|
|
|
]; |
24
|
|
|
} |
25
|
|
|
|
26
|
11 |
|
public function buildReadQuery( |
27
|
|
|
string $table, |
28
|
|
|
array $filter = [], |
29
|
|
|
array $orderBy = [], |
30
|
|
|
int $limit = null, |
31
|
|
|
int $offset = 1 |
32
|
|
|
): array { |
33
|
11 |
|
$sqlSelect = "SELECT * FROM `$table`"; |
34
|
11 |
|
list($sqlFilter, $filterBindings) = $this->createWhere($filter); |
35
|
11 |
|
$sqlOrderBy = $this->createOrderBy($orderBy); |
36
|
11 |
|
$sqlLimit = $this->createLimit($limit); |
37
|
11 |
|
$sqlOffset = $this->createOffset($offset); |
38
|
|
|
|
39
|
|
|
return [ |
40
|
11 |
|
$sqlSelect . $sqlFilter . $sqlOrderBy . $sqlLimit . $sqlOffset, |
41
|
11 |
|
$filterBindings |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
public function buildUpdateQuery(string $table, array $data = [], array $filter = []): array |
46
|
|
|
{ |
47
|
2 |
|
list($sqlUpdate, $updateBindings) = $this->createUpdate($table, $data); |
48
|
2 |
|
list($sqlWhere, $whereBindings) = $this->createWhere($filter); |
49
|
|
|
|
50
|
2 |
|
return [$sqlUpdate . $sqlWhere, array_merge($updateBindings, $whereBindings)]; |
51
|
|
|
} |
52
|
|
|
|
53
|
4 |
|
public function buildDeleteQuery(string $table, array $filter = []): array |
54
|
|
|
{ |
55
|
4 |
|
list($sqlFilter, $bindings) = $this->createWhere($filter); |
56
|
|
|
|
57
|
|
|
return [ |
58
|
4 |
|
"DELETE FROM `$table`" . $sqlFilter, |
59
|
4 |
|
$bindings, |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
private function createUpdate(string $tableName, array $dataList): array |
64
|
|
|
{ |
65
|
2 |
|
$updateTokenCollection = new UpdateTokenCollection($dataList); |
66
|
|
|
|
67
|
|
|
return [ |
68
|
2 |
|
"UPDATE `$tableName` SET " . $updateTokenCollection->toString(), |
69
|
2 |
|
$updateTokenCollection->getBindingsList() |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
14 |
|
private function createWhere(array $filter = []): array |
74
|
|
|
{ |
75
|
14 |
|
if (empty($filter)) { |
76
|
4 |
|
return ['', []]; |
77
|
|
|
} |
78
|
|
|
|
79
|
10 |
|
$filterCollection = new WhereTokenCollection($filter); |
80
|
|
|
|
81
|
10 |
|
return [' WHERE ' . $filterCollection->toString(), $filterCollection->getBindingsList()]; |
82
|
|
|
} |
83
|
|
|
|
84
|
11 |
|
private function createOrderBy(array $orderBy = []): string |
85
|
|
|
{ |
86
|
11 |
|
if (empty($orderBy)) { |
87
|
9 |
|
return ''; |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
$orderByItems = []; |
91
|
2 |
|
foreach ($orderBy as $column => $direction) { |
92
|
2 |
|
$orderByItems[] = '`' . $column . '` ' . $direction; |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
return ' ORDER BY ' . implode(', ', $orderByItems); |
96
|
|
|
} |
97
|
|
|
|
98
|
11 |
|
private function createLimit(int $limit = null): string |
99
|
|
|
{ |
100
|
11 |
|
return $limit === null ? '' : " LIMIT $limit"; |
101
|
|
|
} |
102
|
|
|
|
103
|
11 |
|
private function createOffset(int $offset = 1): string |
104
|
|
|
{ |
105
|
11 |
|
return $offset === 1 ? '' : " OFFSET $offset"; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|