-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathPdoMysqlQueryReflector.php
More file actions
132 lines (109 loc) · 3.97 KB
/
PdoMysqlQueryReflector.php
File metadata and controls
132 lines (109 loc) · 3.97 KB
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 staabm\PHPStanDba\QueryReflection;
use Iterator;
use PDO;
use PDOException;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Type;
use staabm\PHPStanDba\TypeMapping\MysqlTypeMapper;
use staabm\PHPStanDba\TypeMapping\TypeMapper;
/**
* @phpstan-import-type ColumnMeta from BasePdoQueryReflector
*
* @final
*/
class PdoMysqlQueryReflector extends BasePdoQueryReflector
{
/**
* @api
*/
public const NAME = 'pdo-mysql';
public function __construct(PDO $pdo)
{
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
parent::__construct($pdo);
}
/** @return PDOException|list<ColumnMeta>|null */
protected function simulateQuery(string $queryString)
{
if (\array_key_exists($queryString, $this->cache)) {
return $this->cache[$queryString];
}
if (\count($this->cache) > self::MAX_CACHE_SIZE) {
// make room for the next element by randomly removing a existing one
array_shift($this->cache);
}
$simulatedQuery = QuerySimulation::simulate($queryString);
if (null === $simulatedQuery) {
return $this->cache[$queryString] = null;
}
$this->pdo->beginTransaction();
try {
$stmt = $this->pdo->query($simulatedQuery);
} catch (PDOException $e) {
return $this->cache[$queryString] = $e;
} finally {
$this->pdo->rollBack();
}
$this->cache[$queryString] = [];
$columnCount = $stmt->columnCount();
$columnIndex = 0;
while ($columnIndex < $columnCount) {
$columnMeta = $stmt->getColumnMeta($columnIndex);
if (false === $columnMeta || !\array_key_exists('table', $columnMeta)) {
throw new ShouldNotHappenException('Failed to get column meta for column index '.$columnIndex);
}
// Native type may not be set, for example in case of JSON column.
if (!\array_key_exists('native_type', $columnMeta)) {
$columnMeta['native_type'] = \PDO::PARAM_INT === $columnMeta['pdo_type'] ? 'INT' : 'STRING';
}
$flags = $this->emulateFlags($columnMeta['native_type'], $columnMeta['table'], $columnMeta['name']);
foreach ($flags as $flag) {
$columnMeta['flags'][] = $flag;
}
$this->cache[$queryString][$columnIndex] = $columnMeta;
++$columnIndex;
}
$stmt->closeCursor();
return $this->cache[$queryString];
}
public function setupDbaApi(?DbaApi $dbaApi): void
{
$this->typeMapper = new MysqlTypeMapper($dbaApi);
}
/**
* @return Iterator<string, TypeMapper::FLAG_*>
*/
protected function checkInformationSchema(string $tableName): Iterator
{
if (null === $this->stmt) {
$this->stmt = $this->pdo->prepare(
// EXTRA, COLUMN_NAME seems to be nullable in mariadb
'SELECT
coalesce(COLUMN_NAME, "") as COLUMN_NAME,
coalesce(EXTRA, "") as EXTRA,
COLUMN_TYPE
FROM information_schema.columns
WHERE table_name = ? AND table_schema = DATABASE()'
);
}
try {
$this->stmt->execute([$tableName]);
$result = $this->stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $row) {
$extra = $row['EXTRA'];
$columnType = $row['COLUMN_TYPE'];
$columnName = $row['COLUMN_NAME'];
if (str_contains($extra, 'auto_increment')) {
yield $columnName => TypeMapper::FLAG_AUTO_INCREMENT;
}
if (str_contains($columnType, 'unsigned')) {
yield $columnName => TypeMapper::FLAG_UNSIGNED;
}
}
} finally {
$this->stmt->closeCursor();
}
}
}