/
TemplateModel.php
132 lines (115 loc) · 4.5 KB
/
TemplateModel.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
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Export;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Dbal\ConnectionType;
use PhpMyAdmin\Identifiers\DatabaseName;
use PhpMyAdmin\Identifiers\TableName;
use PhpMyAdmin\Util;
use function sprintf;
final class TemplateModel
{
public function __construct(private DatabaseInterface $dbi)
{
}
public function create(DatabaseName $db, TableName $table, Template $template): string
{
$query = sprintf(
'INSERT INTO %s.%s (`username`, `export_type`, `template_name`, `template_data`)'
. ' VALUES (%s, %s, %s, %s);',
Util::backquote($db),
Util::backquote($table),
$this->dbi->quoteString($template->getUsername(), ConnectionType::ControlUser),
$this->dbi->quoteString($template->getExportType(), ConnectionType::ControlUser),
$this->dbi->quoteString($template->getName(), ConnectionType::ControlUser),
$this->dbi->quoteString($template->getData(), ConnectionType::ControlUser),
);
$result = $this->dbi->tryQueryAsControlUser($query);
if ($result !== false) {
return '';
}
return $this->dbi->getError(ConnectionType::ControlUser);
}
public function delete(DatabaseName $db, TableName $table, string $user, int $id): string
{
$query = sprintf(
'DELETE FROM %s.%s WHERE `id` = %d AND `username` = %s;',
Util::backquote($db),
Util::backquote($table),
$id,
$this->dbi->quoteString($user, ConnectionType::ControlUser),
);
$result = $this->dbi->tryQueryAsControlUser($query);
if ($result !== false) {
return '';
}
return $this->dbi->getError(ConnectionType::ControlUser);
}
public function load(DatabaseName $db, TableName $table, string $user, int $id): Template|string
{
$query = sprintf(
'SELECT * FROM %s.%s WHERE `id` = %d AND `username` = %s;',
Util::backquote($db),
Util::backquote($table),
$id,
$this->dbi->quoteString($user, ConnectionType::ControlUser),
);
$result = $this->dbi->tryQueryAsControlUser($query);
if ($result === false) {
return $this->dbi->getError(ConnectionType::ControlUser);
}
$data = [];
while ($row = $result->fetchAssoc()) {
$data = $row;
}
return Template::fromArray([
'id' => (int) $data['id'],
'username' => $data['username'],
'exportType' => $data['export_type'],
'name' => $data['template_name'],
'data' => $data['template_data'],
]);
}
public function update(DatabaseName $db, TableName $table, Template $template): string
{
$query = sprintf(
'UPDATE %s.%s SET `template_data` = %s WHERE `id` = %d AND `username` = %s;',
Util::backquote($db),
Util::backquote($table),
$this->dbi->quoteString($template->getData(), ConnectionType::ControlUser),
$template->getId(),
$this->dbi->quoteString($template->getUsername(), ConnectionType::ControlUser),
);
$result = $this->dbi->tryQueryAsControlUser($query);
if ($result !== false) {
return '';
}
return $this->dbi->getError(ConnectionType::ControlUser);
}
/** @return Template[]|string */
public function getAll(DatabaseName $db, TableName $table, string $user, string $exportType): array|string
{
$query = sprintf(
'SELECT * FROM %s.%s WHERE `username` = %s AND `export_type` = %s ORDER BY `template_name`;',
Util::backquote($db),
Util::backquote($table),
$this->dbi->quoteString($user, ConnectionType::ControlUser),
$this->dbi->quoteString($exportType, ConnectionType::ControlUser),
);
$result = $this->dbi->tryQueryAsControlUser($query);
if ($result === false) {
return $this->dbi->getError(ConnectionType::ControlUser);
}
$templates = [];
while ($row = $result->fetchAssoc()) {
$templates[] = Template::fromArray([
'id' => (int) $row['id'],
'username' => $row['username'],
'exportType' => $row['export_type'],
'name' => $row['template_name'],
'data' => $row['template_data'],
]);
}
return $templates;
}
}