• 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

66.67
/src/Dibi/Drivers/SqliteReflector.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 SQLite database.
15
 */
16
class SqliteReflector implements Dibi\Reflector
17
{
18
        public function __construct(
1✔
19
                private readonly Dibi\Driver $driver,
20
        ) {
21
        }
1✔
22

23

24
        /**
25
         * Returns list of tables.
26
         */
27
        public function getTables(): array
28
        {
29
                $res = $this->driver->query("
1✔
30
                        SELECT name, type = 'view' as view FROM sqlite_master WHERE type IN ('table', 'view')
31
                        UNION ALL
32
                        SELECT name, type = 'view' as view FROM sqlite_temp_master WHERE type IN ('table', 'view')
33
                        ORDER BY name
34
                ") ?? throw new \LogicException('Unexpected null result.');
×
35
                $tables = [];
1✔
36
                while ($row = $res->fetch(true)) {
1✔
37
                        $tables[] = $row;
1✔
38
                }
39

40
                /** @var list<array{name: string, view: bool}> */
41
                return $tables;
1✔
42
        }
43

44

45
        /**
46
         * Returns metadata for all columns in a table.
47
         */
48
        public function getColumns(string $table): array
1✔
49
        {
50
                $res = $this->driver->query("PRAGMA table_info({$this->driver->escapeIdentifier($table)})")
1✔
51
                        ?? throw new \LogicException('Unexpected null result.');
×
52
                $columns = [];
1✔
53
                while ($row = $res->fetch(true)) {
1✔
54
                        $column = $row['name'];
1✔
55
                        $type = explode('(', $row['type']);
1✔
56
                        $columns[] = [
1✔
57
                                'name' => $column,
1✔
58
                                'table' => $table,
1✔
59
                                'fullname' => "$table.$column",
1✔
60
                                'nativetype' => strtoupper($type[0]),
1✔
61
                                'size' => isset($type[1]) ? (int) $type[1] : null,
1✔
62
                                'nullable' => $row['notnull'] === 0,
1✔
63
                                'default' => $row['dflt_value'],
1✔
64
                                'autoincrement' => $row['pk'] && $type[0] === 'INTEGER',
1✔
65
                                'vendor' => $row,
1✔
66
                        ];
67
                }
68

69
                return $columns;
1✔
70
        }
71

72

73
        /**
74
         * Returns metadata for all indexes in a table.
75
         */
76
        public function getIndexes(string $table): array
1✔
77
        {
78
                $res = $this->driver->query("PRAGMA index_list({$this->driver->escapeIdentifier($table)})")
1✔
79
                        ?? throw new \LogicException('Unexpected null result.');
×
80
                $indexes = [];
1✔
81
                while ($row = $res->fetch(true)) {
1✔
82
                        $indexes[$row['name']]['name'] = $row['name'];
1✔
83
                        $indexes[$row['name']]['unique'] = (bool) $row['unique'];
1✔
84
                        $indexes[$row['name']]['columns'] = [];
1✔
85
                }
86

87
                foreach ($indexes as $index => $values) {
1✔
88
                        $res = $this->driver->query("PRAGMA index_info({$this->driver->escapeIdentifier($index)})")
1✔
89
                                ?? throw new \LogicException('Unexpected null result.');
×
90
                        while ($row = $res->fetch(true)) {
1✔
91
                                $indexes[$index]['columns'][$row['seqno']] = $row['name'];
1✔
92
                        }
93
                }
94

95
                $columns = $this->getColumns($table);
1✔
96
                foreach ($indexes as $index => $values) {
1✔
97
                        $column = $indexes[$index]['columns'][0];
1✔
98
                        $primary = false;
1✔
99
                        foreach ($columns as $info) {
1✔
100
                                if ($column === $info['name']) {
1✔
101
                                        $primary = $info['vendor']['pk'];
1✔
102
                                        break;
1✔
103
                                }
104
                        }
105

106
                        $indexes[$index]['primary'] = (bool) $primary;
1✔
107
                }
108

109
                if (!$indexes) { // @see http://www.sqlite.org/lang_createtable.html#rowid
1✔
110
                        foreach ($columns as $column) {
×
111
                                if ($column['vendor']['pk'] ?? false) {
×
112
                                        $indexes[] = [
×
113
                                                'name' => 'ROWID',
×
114
                                                'unique' => true,
115
                                                'primary' => true,
116
                                                'columns' => [$column['name']],
×
117
                                        ];
118
                                        break;
×
119
                                }
120
                        }
121
                }
122

123
                return array_values($indexes);
1✔
124
        }
125

126

127
        /**
128
         * Returns metadata for all foreign keys in a table.
129
         */
130
        public function getForeignKeys(string $table): array
131
        {
132
                $res = $this->driver->query("PRAGMA foreign_key_list({$this->driver->escapeIdentifier($table)})")
×
133
                        ?? throw new \LogicException('Unexpected null result.');
×
134
                $keys = [];
×
135
                while ($row = $res->fetch(true)) {
×
136
                        $keys[$row['id']]['name'] = $row['id']; // foreign key name
×
137
                        $keys[$row['id']]['local'][$row['seq']] = $row['from']; // local columns
×
138
                        $keys[$row['id']]['table'] = $row['table']; // referenced table
×
139
                        $keys[$row['id']]['foreign'][$row['seq']] = $row['to']; // referenced columns
×
140
                        $keys[$row['id']]['onDelete'] = $row['on_delete'];
×
141
                        $keys[$row['id']]['onUpdate'] = $row['on_update'];
×
142

143
                        if ($keys[$row['id']]['foreign'][0] == null) {
×
144
                                $keys[$row['id']]['foreign'] = null;
×
145
                        }
146
                }
147

148
                return array_values($keys);
×
149
        }
150
}
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