Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Driver
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
2 / 2
7
100.00% covered (success)
100.00%
1 / 1
 __construct
n/a
0 / 0
n/a
0 / 0
2
 getSchemas
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getFks
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
1<?php declare(strict_types=1);
2/**
3 * Query
4 *
5 * SQL Query Builder / Database Abstraction Layer
6 *
7 * PHP version 8.1
8 *
9 * @package     Query
10 * @author      Timothy J. Warren <tim@timshome.page>
11 * @copyright   2012 - 2023 Timothy J. Warren
12 * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
13 * @link        https://git.timshomepage.net/aviat/Query
14 * @version     4.0.0
15 */
16
17namespace Query\Drivers\Pgsql;
18
19use Query\Drivers\AbstractDriver;
20
21/**
22 * PostgreSQL specific class
23 */
24class Driver extends AbstractDriver
25{
26    /**
27     * Connect to a PosgreSQL database
28     *
29     * @codeCoverageIgnore
30     */
31    public function __construct(string $dsn, ?string $username=NULL, ?string $password=NULL, array $options=[])
32    {
33        if ( ! str_contains($dsn, 'pgsql'))
34        {
35            $dsn = 'pgsql:' . $dsn;
36        }
37
38        parent::__construct($dsn, $username, $password, $options);
39    }
40
41    /**
42     * Get a list of schemas for the current connection
43     */
44    public function getSchemas(): ?array
45    {
46        $sql = <<<SQL
47            SELECT DISTINCT "schemaname" FROM "pg_tables"
48            WHERE "schemaname" NOT LIKE 'pg\_%'
49            AND "schemaname" != 'information_schema'
50SQL;
51
52        return $this->driverQuery($sql);
53    }
54
55    /**
56     * Retrieve foreign keys for the table
57     */
58    public function getFks(string $table): array
59    {
60        $valueMap = [
61            'c' => 'CASCADE',
62            'r' => 'RESTRICT',
63        ];
64
65        $keys = parent::getFks($table);
66
67        foreach ($keys as &$key)
68        {
69            foreach (['update', 'delete'] as $type)
70            {
71                if ( ! isset($valueMap[$key[$type]]))
72                {
73                    // @codeCoverageIgnoreStart
74                    continue;
75                    // @codeCoverageIgnoreEnd
76                }
77
78                $key[$type] = $valueMap[$key[$type]];
79            }
80        }
81
82        return $keys;
83    }
84}