• 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

93.33
/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'") ?? 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("
1✔
49
                        SELECT c.name as COLUMN_NAME, c.is_identity AS AUTO_INCREMENT
50
                        FROM sys.columns c
51
                        INNER JOIN sys.tables t ON c.object_id = t.object_id
52
                        WHERE t.name = {$this->driver->escapeText($table)}
1✔
NEW
53
                ") ?? throw new \LogicException('Unexpected null result.');
×
54

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

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

89
                return $columns;
1✔
90
        }
91

92

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

104
                $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)}") ?? throw new \LogicException('Unexpected null result.');
1✔
105
                $indexes = [];
1✔
106
                while ($row = $res->fetch(true)) {
1✔
107
                        $indexes[$row['name']]['name'] = $row['name'];
1✔
108
                        $indexes[$row['name']]['unique'] = $row['is_unique'] === 1;
1✔
109
                        $indexes[$row['name']]['primary'] = $row['is_primary_key'] === 1;
1✔
110
                        $indexes[$row['name']]['columns'] = $keyUsages[$row['name']] ?? [];
1✔
111
                }
112

113
                return array_values($indexes);
1✔
114
        }
115

116

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