• 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/InformationSchema.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
/**
15
 * Provides functionality to retrieve meta data from a database with information_schema support.
16
 */
17
class InformationSchema extends AbstractMetadata
18
{
19
    protected $columns = [];
20

21
    protected $keys = [];
22

23
    /**
24
     * Returns an array containing the names of all the tables in the database.
25
     *
26
     * @return array
27
     */
28
    public function getTableNames()
29
    {
30
        $query = "
×
31
            SELECT DISTINCT
32
                TABLE_NAME
33
            FROM INFORMATION_SCHEMA.TABLES
34
            WHERE
35
                TABLE_TYPE='BASE TABLE' AND
36
                TABLE_SCHEMA = ?
37
            ORDER BY TABLE_NAME
38
        ";
×
39

40
        $statement = $this->pdo->prepare($query);
×
41
        $statement->execute([$this->getSchema()]);
×
42

43
        $tableNames = [];
×
44

45
        while ($tableName = $statement->fetchColumn()) {
×
46
            $tableNames[] = $tableName;
×
47
        }
48

49
        return $tableNames;
×
50
    }
51

52
    /**
53
     * Returns an array containing the names of all the columns in the
54
     * $tableName table,
55
     *
56
     * @param string $tableName
57
     *
58
     * @return array
59
     */
60
    public function getTableColumns($tableName)
61
    {
62
        if (!isset($this->columns[$tableName])) {
×
63
            $this->loadColumnInfo($tableName);
×
64
        }
65

66
        return $this->columns[$tableName];
×
67
    }
68

69
    /**
70
     * Returns an array containing the names of all the primary key columns in
71
     * the $tableName table.
72
     *
73
     * @param string $tableName
74
     *
75
     * @return array
76
     */
77
    public function getTablePrimaryKeys($tableName)
78
    {
79
        if (!isset($this->keys[$tableName])) {
×
80
            $this->loadColumnInfo($tableName);
×
81
        }
82

83
        return $this->keys[$tableName];
×
84
    }
85

86
    /**
87
     * Loads column info from a sqlite database.
88
     *
89
     * @param string $tableName
90
     */
91
    protected function loadColumnInfo($tableName): void
92
    {
93
        $this->columns[$tableName] = [];
×
94
        $this->keys[$tableName]    = [];
×
95

96
        $columnQuery = '
×
97
            SELECT DISTINCT
98
                COLUMN_NAME
99
            FROM INFORMATION_SCHEMA.COLUMNS
100
            WHERE
101
                TABLE_NAME = ? AND
102
                TABLE_SCHEMA = ?
103
            ORDER BY ORDINAL_POSITION
104
        ';
×
105

106
        $columnStatement = $this->pdo->prepare($columnQuery);
×
107
        $columnStatement->execute([$tableName, $this->getSchema()]);
×
108

109
        while ($columnName = $columnStatement->fetchColumn()) {
×
110
            $this->columns[$tableName][] = $columnName;
×
111
        }
112

113
        $keyQuery = "
×
114
            SELECT
115
                KCU.COLUMN_NAME
116
            FROM
117
                INFORMATION_SCHEMA.TABLE_CONSTRAINTS as TC,
118
                INFORMATION_SCHEMA.KEY_COLUMN_USAGE as KCU
119
            WHERE
120
                TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND
121
                TC.TABLE_NAME = KCU.TABLE_NAME AND
122
                TC.TABLE_SCHEMA = KCU.TABLE_SCHEMA AND
123
                TC.CONSTRAINT_TYPE = 'PRIMARY KEY' AND
124
                TC.TABLE_NAME = ? AND
125
                TC.TABLE_SCHEMA = ?
126
            ORDER BY
127
                KCU.ORDINAL_POSITION ASC
128
        ";
×
129

130
        $keyStatement = $this->pdo->prepare($keyQuery);
×
131
        $keyStatement->execute([$tableName, $this->getSchema()]);
×
132

133
        while ($columnName = $keyStatement->fetchColumn()) {
×
134
            $this->keys[$tableName][] = $columnName;
×
135
        }
136
    }
137
}
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

© 2025 Coveralls, Inc