1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpCfdi\SatCatalogosPopulate\Database; |
6
|
|
|
|
7
|
|
|
use PDO; |
8
|
|
|
use PDOException; |
9
|
|
|
use RuntimeException; |
10
|
|
|
|
11
|
|
|
class Repository |
12
|
|
|
{ |
13
|
|
|
/** @var PDO */ |
14
|
|
|
private $pdo; |
15
|
|
|
|
16
|
12 |
|
public function __construct(string $dbfile) |
17
|
|
|
{ |
18
|
12 |
|
if (':memory:' !== $dbfile) { |
19
|
|
|
// TODO: validate other sources ? |
20
|
|
|
$dbfile = '//' . $dbfile; |
21
|
|
|
} |
22
|
12 |
|
$this->pdo = new PDO('sqlite:' . $dbfile, '', '', [ |
23
|
12 |
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
24
|
|
|
]); |
25
|
12 |
|
} |
26
|
|
|
|
27
|
|
|
public function pdo(): PDO |
28
|
|
|
{ |
29
|
|
|
return $this->pdo; |
30
|
|
|
} |
31
|
|
|
|
32
|
5 |
|
public function hasTable(string $table): bool |
33
|
|
|
{ |
34
|
5 |
|
$sql = 'SELECT count(*) FROM sqlite_master WHERE type = :type AND name = :table;'; |
35
|
5 |
|
return (1 === (int) $this->queryOne($sql, ['type' => 'table', 'table' => $table])); |
36
|
|
|
} |
37
|
|
|
|
38
|
3 |
|
public function getRecordCount(string $table): int |
39
|
|
|
{ |
40
|
3 |
|
$sql = 'SELECT count(*) FROM ' . $this->escapeName($table) . ';'; |
41
|
3 |
|
return (int) $this->queryOne($sql); |
42
|
|
|
} |
43
|
|
|
|
44
|
12 |
|
public function execute(string $sql, array $values = []): void |
45
|
|
|
{ |
46
|
12 |
|
$stmt = $this->pdo->prepare($sql); |
47
|
12 |
|
if (false === $stmt->execute($values)) { |
48
|
|
|
$errorInfo = $stmt->errorInfo(); |
49
|
|
|
throw new PDOException(sprintf('[%s] %s', $errorInfo[1] ?? 'UNDEF', $errorInfo[2] ?? 'Unknown error')); |
50
|
|
|
} |
51
|
12 |
|
} |
52
|
|
|
|
53
|
1 |
|
public function queryArray(string $sql, array $values = []): array |
54
|
|
|
{ |
55
|
1 |
|
$stmt = $this->pdo->prepare($sql); |
56
|
1 |
|
if (false === $stmt->execute($values)) { |
57
|
|
|
throw new RuntimeException("Unable to execute $sql"); |
58
|
|
|
} |
59
|
1 |
|
$table = []; |
60
|
1 |
|
while (false !== $row = $stmt->fetch(PDO::FETCH_ASSOC)) { |
61
|
1 |
|
$table[] = $row; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
return $table; |
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
public function queryRow(string $sql, array $values = []): array |
68
|
|
|
{ |
69
|
4 |
|
$stmt = $this->pdo->prepare($sql); |
70
|
4 |
|
if (false === $stmt->execute($values)) { |
71
|
|
|
throw new RuntimeException("Unable to execute $sql"); |
72
|
|
|
} |
73
|
4 |
|
$row = $stmt->fetch(PDO::FETCH_ASSOC); |
74
|
4 |
|
if (is_array($row)) { |
75
|
4 |
|
return $row; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return []; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $sql |
83
|
|
|
* @param mixed[] $values |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
8 |
|
public function queryOne(string $sql, array $values = []) |
87
|
|
|
{ |
88
|
8 |
|
$stmt = $this->pdo->prepare($sql); |
89
|
8 |
|
$stmt->execute($values); |
90
|
8 |
|
return $stmt->fetchColumn(0); |
91
|
|
|
} |
92
|
|
|
|
93
|
12 |
|
public function escapeName(string $name): string |
94
|
|
|
{ |
95
|
12 |
|
return '"' . str_replace('"', '""', $name) . '"'; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|