• 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/PgSQL.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 PostgreSQL database.
16
 */
17
class PgSQL extends AbstractMetadata
18
{
19
    /**
20
     * Returns an array containing the names of all the tables in the database.
21
     *
22
     * @return array
23
     */
24
    public function getTableNames()
25
    {
26
        $query = "
×
27
            SELECT DISTINCT
28
                TABLE_NAME
29
            FROM INFORMATION_SCHEMA.TABLES
30
            WHERE
31
                TABLE_TYPE='BASE TABLE' AND
32
                TABLE_SCHEMA = ?
33
            ORDER BY TABLE_NAME
34
        ";
×
35

36
        $statement = $this->pdo->prepare($query);
×
37
        $statement->execute([$this->getSchema()]);
×
38

39
        $tableNames = [];
×
40

41
        while ($tableName = $statement->fetchColumn(0)) {
×
42
            $tableNames[] = $tableName;
×
43
        }
44

45
        return $tableNames;
×
46
    }
47

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

62
        return $this->columns[$tableName];
×
63
    }
64

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

79
        return $this->keys[$tableName];
×
80
    }
81

82
    /**
83
     * Returns the schema for the connection.
84
     *
85
     * @return string
86
     */
87
    public function getSchema()
88
    {
89
        if (empty($this->schema)) {
×
90
            return 'public';
×
91
        }
92

93
        return $this->schema;
×
94
    }
95

96
    /**
97
     * Returns true if the rdbms allows cascading
98
     *
99
     * @return bool
100
     */
101
    public function allowsCascading()
102
    {
103
        return true;
×
104
    }
105

106
    /**
107
     * Loads column info from a database table.
108
     *
109
     * @param string $tableName
110
     */
111
    protected function loadColumnInfo($tableName): void
112
    {
113
        $this->columns[$tableName] = [];
×
114
        $this->keys[$tableName]    = [];
×
115

116
        $columnQuery = '
×
117
            SELECT DISTINCT
118
                COLUMN_NAME, ORDINAL_POSITION
119
            FROM INFORMATION_SCHEMA.COLUMNS
120
            WHERE
121
                TABLE_NAME = ? AND
122
                TABLE_SCHEMA = ?
123
            ORDER BY ORDINAL_POSITION
124
        ';
×
125

126
        $columnStatement = $this->pdo->prepare($columnQuery);
×
127
        $columnStatement->execute([$tableName, $this->getSchema()]);
×
128

129
        while ($columnName = $columnStatement->fetchColumn()) {
×
130
            $this->columns[$tableName][] = $columnName;
×
131
        }
132

133
        $keyQuery = "
×
134
            SELECT
135
                KCU.COLUMN_NAME,
136
                KCU.ORDINAL_POSITION
137
            FROM
138
                INFORMATION_SCHEMA.KEY_COLUMN_USAGE as KCU
139
            LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS as TC
140
                ON TC.TABLE_NAME = KCU.TABLE_NAME AND
141
                TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME
142
            WHERE
143
                TC.CONSTRAINT_TYPE = 'PRIMARY KEY' AND
144
                TC.TABLE_NAME = ? AND
145
                TC.TABLE_SCHEMA = ?
146
            ORDER BY
147
                KCU.ORDINAL_POSITION ASC
148
        ";
×
149

150
        $keyStatement = $this->pdo->prepare($keyQuery);
×
151
        $keyStatement->execute([$tableName, $this->getSchema()]);
×
152

153
        while ($columnName = $keyStatement->fetchColumn()) {
×
154
            $this->keys[$tableName][] = $columnName;
×
155
        }
156
    }
157
}
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