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

dg / dibi / 22282875037

22 Feb 2026 06:32PM UTC coverage: 76.552% (-0.5%) from 77.004%
22282875037

push

github

dg
phpstan

55 of 96 new or added lines in 23 files covered. (57.29%)

1 existing line in 1 file now uncovered.

1763 of 2303 relevant lines covered (76.55%)

0.77 hits per line

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

69.7
/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
NEW
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)})") ?? throw new \LogicException('Unexpected null result.');
1✔
51
                $columns = [];
1✔
52
                while ($row = $res->fetch(true)) {
1✔
53
                        $column = $row['name'];
1✔
54
                        $type = explode('(', $row['type']);
1✔
55
                        $columns[] = [
1✔
56
                                'name' => $column,
1✔
57
                                'table' => $table,
1✔
58
                                'fullname' => "$table.$column",
1✔
59
                                'nativetype' => strtoupper($type[0]),
1✔
60
                                'size' => isset($type[1]) ? (int) $type[1] : null,
1✔
61
                                'nullable' => $row['notnull'] === 0,
1✔
62
                                'default' => $row['dflt_value'],
1✔
63
                                'autoincrement' => $row['pk'] && $type[0] === 'INTEGER',
1✔
64
                                'vendor' => $row,
1✔
65
                        ];
66
                }
67

68
                return $columns;
1✔
69
        }
70

71

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

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

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

103
                        $indexes[$index]['primary'] = (bool) $primary;
1✔
104
                }
105

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

120
                return array_values($indexes);
1✔
121
        }
122

123

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

NEW
140
                        if (($keys[$id]['foreign'][0] ?? null) == null) {
×
NEW
141
                                $keys[$id]['foreign'] = [];
×
142
                        }
143
                }
144

145
                return array_values($keys);
×
146
        }
147
}
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