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

dg / dibi / 22284472534

22 Feb 2026 08:11PM UTC coverage: 76.31% (-0.2%) from 76.552%
22284472534

push

github

dg
phpstan

56 of 106 new or added lines in 23 files covered. (52.83%)

82 existing lines in 8 files now uncovered.

1762 of 2309 relevant lines covered (76.31%)

0.76 hits per line

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

87.5
/src/Dibi/Drivers/SqlsrvReflector.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
use function sprintf;
12

13

14
/**
15
 * The reflector for Microsoft SQL Server and SQL Azure databases.
16
 */
17
class SqlsrvReflector 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("SELECT TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES WHERE [TABLE_SCHEMA] = 'dbo'")
1✔
NEW
31
                        ?? throw new \LogicException('Unexpected null result.');
×
32
                $tables = [];
1✔
33
                while ($row = $res->fetch(false)) {
1✔
34
                        $tables[] = [
1✔
35
                                'name' => $row[0],
1✔
36
                                'view' => isset($row[1]) && $row[1] === 'VIEW',
1✔
37
                        ];
38
                }
39

40
                return $tables;
1✔
41
        }
42

43

44
        /**
45
         * Returns metadata for all columns in a table.
46
         */
47
        public function getColumns(string $table): array
1✔
48
        {
49
                $res = $this->driver->query("
1✔
50
                        SELECT c.name as COLUMN_NAME, c.is_identity AS AUTO_INCREMENT
51
                        FROM sys.columns c
52
                        INNER JOIN sys.tables t ON c.object_id = t.object_id
53
                        WHERE t.name = {$this->driver->escapeText($table)}
1✔
NEW
54
                ") ?? throw new \LogicException('Unexpected null result.');
×
55

56
                $autoIncrements = [];
1✔
57
                while ($row = $res->fetch(true)) {
1✔
58
                        $autoIncrements[$row['COLUMN_NAME']] = (bool) $row['AUTO_INCREMENT'];
1✔
59
                }
60

61
                $res = $this->driver->query("
1✔
62
                        SELECT C.COLUMN_NAME, C.DATA_TYPE, C.CHARACTER_MAXIMUM_LENGTH , C.COLUMN_DEFAULT  , C.NUMERIC_PRECISION, C.NUMERIC_SCALE , C.IS_NULLABLE, Case When Z.CONSTRAINT_NAME Is Null Then 0 Else 1 End As IsPartOfPrimaryKey
63
                        FROM INFORMATION_SCHEMA.COLUMNS As C
64
                        Outer Apply (
65
                                SELECT CCU.CONSTRAINT_NAME
66
                                FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS As TC
67
                                Join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE As CCU
68
                                        On CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
69
                                WHERE TC.TABLE_SCHEMA = C.TABLE_SCHEMA
70
                                        And TC.TABLE_NAME = C.TABLE_NAME
71
                                        And TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
72
                                        And CCU.COLUMN_NAME = C.COLUMN_NAME
73
                        ) As Z
74
                        WHERE C.TABLE_NAME = {$this->driver->escapeText($table)}
1✔
NEW
75
                ") ?? throw new \LogicException('Unexpected null result.');
×
76
                $columns = [];
1✔
77
                while ($row = $res->fetch(true)) {
1✔
78
                        $columns[] = [
1✔
79
                                'name' => $row['COLUMN_NAME'],
1✔
80
                                'table' => $table,
1✔
81
                                'nativetype' => strtoupper($row['DATA_TYPE']),
1✔
82
                                'size' => $row['CHARACTER_MAXIMUM_LENGTH'],
1✔
83
                                'nullable' => $row['IS_NULLABLE'] === 'YES',
1✔
84
                                'default' => $row['COLUMN_DEFAULT'],
1✔
85
                                'autoincrement' => $autoIncrements[$row['COLUMN_NAME']],
1✔
86
                                'vendor' => $row,
1✔
87
                        ];
88
                }
89

90
                return $columns;
1✔
91
        }
92

93

94
        /**
95
         * Returns metadata for all indexes in a table.
96
         */
97
        public function getIndexes(string $table): array
1✔
98
        {
99
                $keyUsagesRes = $this->driver->query(sprintf('EXEC [sys].[sp_helpindex] @objname = %s', $this->driver->escapeText($table)))
1✔
NEW
100
                        ?? throw new \LogicException('Unexpected null result.');
×
101
                $keyUsages = [];
1✔
102
                while ($row = $keyUsagesRes->fetch(true)) {
1✔
103
                        $keyUsages[$row['index_name']] = explode(',', $row['index_keys']);
1✔
104
                }
105

106
                $res = $this->driver->query("SELECT [i].* FROM [sys].[indexes] [i] INNER JOIN [sys].[tables] [t] ON [i].[object_id] = [t].[object_id] WHERE [t].[name] = {$this->driver->escapeText($table)}")
1✔
NEW
107
                        ?? throw new \LogicException('Unexpected null result.');
×
108
                $indexes = [];
1✔
109
                while ($row = $res->fetch(true)) {
1✔
110
                        $indexes[$row['name']]['name'] = $row['name'];
1✔
111
                        $indexes[$row['name']]['unique'] = $row['is_unique'] === 1;
1✔
112
                        $indexes[$row['name']]['primary'] = $row['is_primary_key'] === 1;
1✔
113
                        $indexes[$row['name']]['columns'] = $keyUsages[$row['name']] ?? [];
1✔
114
                }
115

116
                return array_values($indexes);
1✔
117
        }
118

119

120
        /**
121
         * Returns metadata for all foreign keys in a table.
122
         */
123
        public function getForeignKeys(string $table): array
124
        {
125
                throw new Dibi\NotImplementedException;
×
126
        }
127
}
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