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

misantron / dbunit / 4562384438

pending completion
4562384438

push

github

Aleksandr Ivanov
Fix build status badge display

635 of 1200 relevant lines covered (52.92%)

5.65 hits per line

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

0.0
/src/Database/Metadata/SqlSrv.php
1
<?php
2

3
/*
4
 * This file is part of DbUnit.
5
 *
6
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace PHPUnit\DbUnit\Database\Metadata;
13

14
use PDO;
15
use PDOException;
16

17
/**
18
 * Provides functionality to retrieve meta data from a Microsoft SQL Server database.
19
 */
20
class SqlSrv extends AbstractMetadata
21
{
22
    /**
23
     * No character used to quote schema objects.
24
     *
25
     * @var string
26
     */
27
    protected $schemaObjectQuoteChar = '';
28

29
    /**
30
     * The command used to perform a TRUNCATE operation.
31
     *
32
     * @var string
33
     */
34
    protected $truncateCommand = 'TRUNCATE TABLE';
35

36
    /**
37
     * Returns an array containing the names of all the tables in the database.
38
     *
39
     * @return array
40
     */
41
    public function getTableNames()
42
    {
43
        $query = "SELECT name
×
44
                    FROM sysobjects
45
                   WHERE type='U'";
×
46

47
        $statement = $this->pdo->prepare($query);
×
48
        $statement->execute();
×
49

50
        $tableNames = [];
×
51

52
        while ($tableName = $statement->fetchColumn()) {
×
53
            $tableNames[] = $tableName;
×
54
        }
55

56
        return $tableNames;
×
57
    }
58

59
    /**
60
     * Returns an array containing the names of all the columns in the
61
     * $tableName table.
62
     *
63
     * @param string $tableName
64
     *
65
     * @return array
66
     */
67
    public function getTableColumns($tableName)
68
    {
69
        $query = "SELECT c.name
×
70
                    FROM syscolumns c
71
               LEFT JOIN sysobjects o ON c.id = o.id
72
                   WHERE o.name = '$tableName'";
×
73

74
        $statement = $this->pdo->prepare($query);
×
75
        $statement->execute();
×
76

77
        $columnNames = [];
×
78

79
        while ($columnName = $statement->fetchColumn()) {
×
80
            $columnNames[] = $columnName;
×
81
        }
82

83
        return $columnNames;
×
84
    }
85

86
    /**
87
     * Returns an array containing the names of all the primary key columns in
88
     * the $tableName table.
89
     *
90
     * @param string $tableName
91
     *
92
     * @return array
93
     */
94
    public function getTablePrimaryKeys($tableName)
95
    {
96
        $query     = "EXEC sp_statistics '$tableName'";
×
97
        $statement = $this->pdo->prepare($query);
×
98
        $statement->execute();
×
99
        $statement->setFetchMode(PDO::FETCH_ASSOC);
×
100

101
        $columnNames = [];
×
102

103
        while ($column = $statement->fetch()) {
×
104
            if ((int) $column['TYPE'] === 1) {
×
105
                $columnNames[] = $column['COLUMN_NAME'];
×
106
            }
107
        }
108

109
        return $columnNames;
×
110
    }
111

112
    /**
113
     * Allow overwriting identities for the given table.
114
     *
115
     * @param string $tableName
116
     */
117
    public function disablePrimaryKeys($tableName): void
118
    {
119
        try {
120
            $query = "SET IDENTITY_INSERT $tableName ON";
×
121
            $this->pdo->exec($query);
×
122
        } catch (PDOException $e) {
×
123
            // ignore the error here - can happen if primary key is not an identity
124
        }
125
    }
126

127
    /**
128
     * Reenable auto creation of identities for the given table.
129
     *
130
     * @param string $tableName
131
     */
132
    public function enablePrimaryKeys($tableName): void
133
    {
134
        try {
135
            $query = "SET IDENTITY_INSERT $tableName OFF";
×
136
            $this->pdo->exec($query);
×
137
        } catch (PDOException $e) {
×
138
            // ignore the error here - can happen if primary key is not an identity
139
        }
140
    }
141
}
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