• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dg / dibi / 22284747548

22 Feb 2026 08:29PM UTC coverage: 76.399% (+0.09%) from 76.31%
22284747548

push

github

dg
added CLAUDE.md

1761 of 2305 relevant lines covered (76.4%)

0.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

63.46
/src/Dibi/Drivers/MySqlReflector.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * This file is part of the Dibi, smart database abstraction layer (https://dibi.nette.org)
5
 * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace Dibi\Drivers;
9

10
use Dibi;
11

12

13
/**
14
 * The reflector for MySQL databases.
15
 * @internal
16
 */
17
class MySqlReflector implements Dibi\Reflector
18
{
19
        public function __construct(
1✔
20
                private readonly Dibi\Driver $driver,
21
        ) {
22
        }
1✔
23

24

25
        /**
26
         * Returns list of tables.
27
         */
28
        public function getTables(): array
29
        {
30
                $res = $this->driver->query('SHOW FULL TABLES') ?? throw new \LogicException('Unexpected null result.');
1✔
31
                $tables = [];
1✔
32
                while ($row = $res->fetch(false)) {
1✔
33
                        $tables[] = [
1✔
34
                                'name' => $row[0],
1✔
35
                                'view' => isset($row[1]) && $row[1] === 'VIEW',
1✔
36
                        ];
37
                }
38

39
                return $tables;
1✔
40
        }
41

42

43
        /**
44
         * Returns metadata for all columns in a table.
45
         */
46
        public function getColumns(string $table): array
1✔
47
        {
48
                $res = $this->driver->query("SHOW FULL COLUMNS FROM {$this->driver->escapeIdentifier($table)}")
1✔
49
                        ?? throw new \LogicException('Unexpected null result.');
×
50
                $columns = [];
1✔
51
                while ($row = $res->fetch(true)) {
1✔
52
                        $type = explode('(', $row['Type']);
1✔
53
                        $columns[] = [
1✔
54
                                'name' => $row['Field'],
1✔
55
                                'table' => $table,
1✔
56
                                'nativetype' => strtoupper($type[0]),
1✔
57
                                'size' => isset($type[1]) ? (int) $type[1] : null,
1✔
58
                                'nullable' => $row['Null'] === 'YES',
1✔
59
                                'default' => $row['Default'],
1✔
60
                                'autoincrement' => $row['Extra'] === 'auto_increment',
1✔
61
                                'vendor' => $row,
1✔
62
                        ];
63
                }
64

65
                return $columns;
1✔
66
        }
67

68

69
        /**
70
         * Returns metadata for all indexes in a table.
71
         */
72
        public function getIndexes(string $table): array
1✔
73
        {
74
                $res = $this->driver->query("SHOW INDEX FROM {$this->driver->escapeIdentifier($table)}")
1✔
75
                        ?? throw new \LogicException('Unexpected null result.');
×
76
                $indexes = [];
1✔
77
                while ($row = $res->fetch(true)) {
1✔
78
                        $indexes[$row['Key_name']]['name'] = $row['Key_name'];
1✔
79
                        $indexes[$row['Key_name']]['unique'] = !$row['Non_unique'];
1✔
80
                        $indexes[$row['Key_name']]['primary'] = $row['Key_name'] === 'PRIMARY';
1✔
81
                        $indexes[$row['Key_name']]['columns'][$row['Seq_in_index'] - 1] = $row['Column_name'];
1✔
82
                }
83

84
                return array_values($indexes);
1✔
85
        }
86

87

88
        /**
89
         * Returns metadata for all foreign keys in a table.
90
         * @throws Dibi\NotSupportedException
91
         */
92
        public function getForeignKeys(string $table): array
93
        {
94
                $data = ($this->driver->query("SELECT `ENGINE` FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = {$this->driver->escapeText($table)}")
×
95
                        ?? throw new \LogicException('Unexpected null result.'))->fetch(true);
×
96

97
                if ($data['ENGINE'] !== 'InnoDB') {
×
98
                        throw new Dibi\NotSupportedException("Foreign keys are not supported in {$data['ENGINE']} tables.");
×
99
                }
100

101
                $res = $this->driver->query("
×
102
                        SELECT rc.CONSTRAINT_NAME, rc.UPDATE_RULE, rc.DELETE_RULE, kcu.REFERENCED_TABLE_NAME,
103
                                GROUP_CONCAT(kcu.REFERENCED_COLUMN_NAME ORDER BY kcu.ORDINAL_POSITION) AS REFERENCED_COLUMNS,
104
                                GROUP_CONCAT(kcu.COLUMN_NAME ORDER BY kcu.ORDINAL_POSITION) AS COLUMNS
105
                        FROM information_schema.REFERENTIAL_CONSTRAINTS rc
106
                        INNER JOIN information_schema.KEY_COLUMN_USAGE kcu ON
107
                                kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
108
                                AND kcu.CONSTRAINT_SCHEMA = rc.CONSTRAINT_SCHEMA
109
                        WHERE rc.CONSTRAINT_SCHEMA = DATABASE()
110
                                AND rc.TABLE_NAME = {$this->driver->escapeText($table)}
×
111
                        GROUP BY rc.CONSTRAINT_NAME
112
                ") ?? throw new \LogicException('Unexpected null result.');
×
113

114
                $foreignKeys = [];
×
115
                while ($row = $res->fetch(true)) {
×
116
                        $keyName = $row['CONSTRAINT_NAME'];
×
117

118
                        $foreignKeys[$keyName]['name'] = $keyName;
×
119
                        $foreignKeys[$keyName]['local'] = explode(',', $row['COLUMNS']);
×
120
                        $foreignKeys[$keyName]['table'] = $row['REFERENCED_TABLE_NAME'];
×
121
                        $foreignKeys[$keyName]['foreign'] = explode(',', $row['REFERENCED_COLUMNS']);
×
122
                        $foreignKeys[$keyName]['onDelete'] = $row['DELETE_RULE'];
×
123
                        $foreignKeys[$keyName]['onUpdate'] = $row['UPDATE_RULE'];
×
124
                }
125

126
                return array_values($foreignKeys);
×
127
        }
128
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc