|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Zortje\MVC\Model; |
|
5
|
|
|
|
|
6
|
|
|
use Zortje\MVC\Model\Table\Entity\Exception\InvalidEntityPropertyException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class SQLCommand |
|
10
|
|
|
* |
|
11
|
|
|
* @package Zortje\MVC\Model |
|
12
|
|
|
*/ |
|
13
|
|
|
class SQLCommand |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var String Table name |
|
18
|
|
|
*/ |
|
19
|
|
|
private $tableName; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var String[] Table columns |
|
23
|
|
|
*/ |
|
24
|
|
|
private $columns; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $tableName |
|
28
|
|
|
* @param string[] $columns |
|
29
|
|
|
*/ |
|
30
|
1 |
|
public function __construct(string $tableName, array $columns) |
|
31
|
|
|
{ |
|
32
|
1 |
|
$this->tableName = $tableName; |
|
33
|
1 |
|
$this->columns = $columns; |
|
34
|
1 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Create INSERT INTO command |
|
38
|
|
|
* |
|
39
|
|
|
* @return string INSERT INTO query |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public function insertInto(): string |
|
42
|
|
|
{ |
|
43
|
1 |
|
$columnNames = $this->getColumnNames($this->columns); |
|
44
|
1 |
|
$columnValues = $this->getColumnValues($this->columns); |
|
45
|
|
|
|
|
46
|
1 |
|
return "INSERT INTO `$this->tableName` ($columnNames) VALUES ($columnValues);"; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Create UPDATE SET command with WHERE for updating a single row with UUID |
|
51
|
|
|
* |
|
52
|
|
|
* @param array $columns Columns to use in SET condition |
|
53
|
|
|
* |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function updateSetWhere(array $columns): string |
|
57
|
|
|
{ |
|
58
|
1 |
|
$set = $this->getEqualFromColumns(', ', $columns); |
|
59
|
1 |
|
$where = $this->getEqualFromColumns(' AND ', ['uuid']); |
|
60
|
|
|
|
|
61
|
1 |
|
return "UPDATE `$this->tableName` SET $set WHERE $where;"; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Create SELECT FROM command |
|
66
|
|
|
* |
|
67
|
|
|
* @return string SELECT FROM query |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function selectFrom(): string |
|
70
|
|
|
{ |
|
71
|
1 |
|
$tableColumnNames = $this->getColumnNames($this->columns); |
|
72
|
|
|
|
|
73
|
1 |
|
return "SELECT $tableColumnNames FROM `$this->tableName`;"; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Create SELECT FROM command with WHERE |
|
78
|
|
|
* |
|
79
|
|
|
* @param string[] $columns Columns to use in WHERE condition |
|
80
|
|
|
* |
|
81
|
|
|
* @return string SELECT FROM query |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function selectFromWhere($columns): string |
|
84
|
|
|
{ |
|
85
|
1 |
|
$tableColumnNames = $this->getColumnNames($this->columns); |
|
86
|
|
|
|
|
87
|
1 |
|
$where = $this->getEqualFromColumns(' AND ', $columns); |
|
88
|
|
|
|
|
89
|
1 |
|
return "SELECT $tableColumnNames FROM `$this->tableName` WHERE $where;"; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get columns names for SQL command |
|
94
|
|
|
* |
|
95
|
|
|
* @param String[] $columns |
|
96
|
|
|
* |
|
97
|
|
|
* @return string Column names for column list |
|
98
|
|
|
*/ |
|
99
|
1 |
|
protected function getColumnNames(array $columns): string |
|
100
|
|
|
{ |
|
101
|
1 |
|
$tableColumnNames = implode('`, `', array_keys($columns)); |
|
102
|
|
|
|
|
103
|
1 |
|
return "`{$tableColumnNames}`"; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Get columns values for SQL command |
|
108
|
|
|
* |
|
109
|
|
|
* @param String[] $columns |
|
110
|
|
|
* |
|
111
|
|
|
* @return string Column names for column values |
|
112
|
|
|
*/ |
|
113
|
1 |
|
protected function getColumnValues(array $columns): string |
|
114
|
|
|
{ |
|
115
|
1 |
|
$tableColumnValues = implode(', :', array_keys($columns)); |
|
116
|
|
|
|
|
117
|
1 |
|
return ":{$tableColumnValues}"; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get equal string for columns with glue |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $glue String glue between columns |
|
124
|
|
|
* @param array $columns Columns to use |
|
125
|
|
|
* |
|
126
|
|
|
* @return string |
|
127
|
|
|
* |
|
128
|
|
|
* @throws InvalidEntityPropertyException If provided with columns which the entity dosnt have |
|
129
|
|
|
*/ |
|
130
|
2 |
|
protected function getEqualFromColumns(string $glue, array $columns): string |
|
131
|
|
|
{ |
|
132
|
2 |
|
$equal = []; |
|
133
|
|
|
|
|
134
|
2 |
|
foreach ($columns as $column) { |
|
135
|
2 |
|
if (!isset($this->columns[$column])) { |
|
136
|
1 |
|
throw new InvalidEntityPropertyException([$this->tableName, $column]); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
1 |
|
$equal[] = "`$column` = :$column"; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
1 |
|
return implode($glue, $equal); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|