1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpCfdi\SatCatalogosPopulate\Database; |
6
|
|
|
|
7
|
|
|
use LogicException; |
8
|
|
|
use PDOException; |
9
|
|
|
|
10
|
|
|
class DataTableGateway |
11
|
|
|
{ |
12
|
|
|
/** @var DataTable */ |
13
|
|
|
private $dataTable; |
14
|
|
|
|
15
|
|
|
/** @var Repository */ |
16
|
|
|
private $repository; |
17
|
|
|
|
18
|
12 |
|
public function __construct(DataTable $dataTable, Repository $repository) |
19
|
|
|
{ |
20
|
12 |
|
$this->dataTable = $dataTable; |
21
|
12 |
|
$this->repository = $repository; |
22
|
12 |
|
} |
23
|
|
|
|
24
|
8 |
|
public function dataTable(): DataTable |
25
|
|
|
{ |
26
|
8 |
|
return $this->dataTable; |
27
|
|
|
} |
28
|
|
|
|
29
|
9 |
|
public function recreate(): void |
30
|
|
|
{ |
31
|
9 |
|
$this->drop(); |
32
|
9 |
|
$this->create(); |
33
|
9 |
|
} |
34
|
|
|
|
35
|
12 |
|
private function sqlDiscoverField(DataFieldInterface $field): string |
36
|
|
|
{ |
37
|
12 |
|
$sqlName = $this->repository->escapeName($field->name()); |
38
|
12 |
|
if ($field instanceof TextDataField || $field instanceof DateDataField) { |
39
|
12 |
|
return $sqlName . ' text not null'; |
40
|
|
|
} |
41
|
3 |
|
if ($field instanceof IntegerDataField || $field instanceof BoolDataField) { |
42
|
3 |
|
return $sqlName . ' int not null'; |
43
|
|
|
} |
44
|
|
|
if ($field instanceof FloatDataField) { |
45
|
|
|
return $sqlName . ' real not null'; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
throw new LogicException("Don't know what to do with " . get_class($field)); |
49
|
|
|
} |
50
|
|
|
|
51
|
9 |
|
public function drop(): void |
52
|
|
|
{ |
53
|
9 |
|
$sql = 'DROP TABLE IF EXISTS ' . $this->repository->escapeName($this->dataTable->name()) . ';'; |
54
|
9 |
|
$this->repository->execute($sql); |
55
|
9 |
|
} |
56
|
|
|
|
57
|
12 |
|
public function create(): void |
58
|
|
|
{ |
59
|
12 |
|
$fields = []; |
60
|
12 |
|
foreach ($this->dataTable->fields() as $field) { |
61
|
12 |
|
$fields[] = $this->sqlDiscoverField($field); |
62
|
|
|
} |
63
|
|
|
|
64
|
12 |
|
$pkDefinition = ''; |
65
|
12 |
|
if (count($this->dataTable->primaryKey()) > 0) { |
66
|
|
|
$pkDefinition = 'PRIMARY KEY (' |
67
|
11 |
|
. implode(', ', array_map(function (string $input): string { |
68
|
11 |
|
return $this->repository->escapeName($input); |
69
|
11 |
|
}, $this->dataTable->primaryKey())) |
70
|
11 |
|
. ')'; |
71
|
|
|
} |
72
|
|
|
|
73
|
12 |
|
$sql = 'CREATE TABLE ' . $this->repository->escapeName($this->dataTable->name()) |
74
|
12 |
|
. ' ( ' . implode(', ', array_filter(array_merge($fields, [$pkDefinition]))) . ' )' |
75
|
12 |
|
. ';'; |
76
|
12 |
|
$this->repository->execute($sql); |
77
|
12 |
|
} |
78
|
|
|
|
79
|
11 |
|
public function insert(array $input): void |
80
|
|
|
{ |
81
|
11 |
|
$fieldNames = []; |
82
|
11 |
|
$preparedNames = []; |
83
|
11 |
|
foreach ($this->dataTable->fields() as $dataField) { |
84
|
11 |
|
$fieldNames[] = $this->repository->escapeName($dataField->name()); |
85
|
11 |
|
$preparedNames[] = ':' . $dataField->name(); |
86
|
|
|
} |
87
|
|
|
|
88
|
11 |
|
$sql = 'INSERT INTO ' . $this->repository->escapeName($this->dataTable->name()) |
89
|
11 |
|
. ' (' . implode(', ', $fieldNames) . ')' |
90
|
11 |
|
. ' VALUES (' . implode(', ', $preparedNames) . ')' |
91
|
11 |
|
. ';'; |
92
|
|
|
|
93
|
|
|
try { |
94
|
11 |
|
$this->repository->execute($sql, $input); |
95
|
1 |
|
} catch (PDOException $exception) { |
96
|
1 |
|
$message = sprintf('Unable to run %s using %s', $sql, json_encode($input)); |
97
|
1 |
|
throw new PDOException($message, 0, $exception); |
98
|
|
|
} |
99
|
11 |
|
} |
100
|
|
|
} |
101
|
|
|
|