Passed
Pull Request — master (#397)
by Wilmer
02:40
created

DbHelper::getCommmentsFromColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 30
rs 9.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests\Support;
6
7
use Yiisoft\Cache\ArrayCache;
8
use Yiisoft\Cache\Cache;
9
use Yiisoft\Cache\CacheInterface;
10
use Yiisoft\Db\Cache\QueryCache;
11
use Yiisoft\Db\Cache\SchemaCache;
12
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface;
13
use Yiisoft\Db\Exception\Exception;
14
use Yiisoft\Db\Exception\InvalidConfigException;
15
16
use function explode;
17
use function file_get_contents;
18
use function preg_replace;
19
use function str_replace;
20
use function trim;
21
22
final class DbHelper
23
{
24
    public static function getCache(): CacheInterface
25
    {
26
        return new Cache(new ArrayCache());
27
    }
28
29
    public static function getCommmentsFromColumn(
30
        string $table,
31
        string $column,
32
        ConnectionPDOInterface $db
33
    ): array|null {
34
        return match ($db->getName()) {
35
            'pgsql' => $db->createCommand(
36
                <<<SQL
37
                SELECT
38
                    pgd.description
39
                FROM
40
                    pg_catalog.pg_statio_all_tables as st
41
                INNER JOIN pg_catalog.pg_description pgd ON (pgd.objoid=st.relid)
42
                INNER JOIN pg_catalog.pg_attribute pga ON (pga.attrelid=st.relid AND pga.attnum=pgd.objsubid)
43
                WHERE
44
                    st.relname=:table AND pga.attname=:column
45
                SQL,
46
                ['table' => $table, 'column' => $column]
47
            )->queryOne(),
48
            'sqlsrv' => $db->createCommand(
49
                <<<SQL
50
                SELECT
51
                    value as comment
52
                FROM
53
                    sys.extended_properties
54
                WHERE
55
                    major_id = OBJECT_ID(:table) AND minor_id = COLUMNPROPERTY(major_id, :column, 'ColumnId')
56
                SQL,
57
                ['table' => $table, 'column' => $column]
58
            )->queryOne(),
59
        };
60
    }
61
62
    public static function getCommmentsFromTable(
63
        string $table,
64
        ConnectionPDOInterface $db,
65
        string $schema = 'yiitest'
66
    ): array|null {
67
        return match ($db->getName()) {
68
            'mysql' => $db->createCommand(
69
                <<<SQL
70
                SELECT
71
                    TABLE_COMMENT as comment
72
                FROM
73
                    information_schema.TABLES
74
                WHERE
75
                    TABLE_SCHEMA = :schema AND TABLE_NAME = :table AND TABLE_COMMENT != ''
76
                SQL,
77
                ['schema' => $schema, 'table' => $table]
78
            )->queryOne(),
79
            'pgsql' => $db->createCommand(
80
                <<<SQL
81
                SELECT obj_description(oid, 'pg_class') as description FROM pg_class WHERE relname= :table
82
                SQL,
83
                ['table' => $table]
84
            )->queryOne(),
85
            'sqlsrv' => $db->createCommand(
86
                <<<SQL
87
                SELECT
88
                    value as comment
89
                FROM
90
                    sys.extended_properties
91
                WHERE
92
                    major_id = OBJECT_ID(:table) AND minor_id = 0
93
                SQL,
94
                ['table' => $table]
95
            )->queryOne(),
96
        };
97
    }
98
99
    public static function getQueryCache(): QueryCache
100
    {
101
        return new QueryCache(self::getCache());
102
    }
103
104
    public static function getSchemaCache(): SchemaCache
105
    {
106
        return new SchemaCache(self::getCache());
107
    }
108
109
    /**
110
     * Loads the fixture into the database.
111
     *
112
     * @throws Exception
113
     * @throws InvalidConfigException
114
     */
115
    public static function loadFixture(ConnectionPDOInterface $db, string $fixture): void
116
    {
117
        $db->open();
118
        $lines = explode(';', file_get_contents($fixture));
119
120
        foreach ($lines as $line) {
121
            if (trim($line) !== '') {
122
                $db->getPDO()?->exec($line);
123
            }
124
        }
125
    }
126
127
    /**
128
     * Adjust dbms specific escaping.
129
     *
130
     * @param string $sql string SQL statement to adjust.
131
     * @param string $driverName string DBMS name.
132
     *
133
     * @return mixed
134
     */
135
    public static function replaceQuotes(string $sql, string $driverName): string
136
    {
137
        return match ($driverName) {
138
            'mysql', 'sqlite' => str_replace(['[[', ']]'], '`', $sql),
139
            'oci' => str_replace(['[[', ']]'], '"', $sql),
140
            'pgsql' => str_replace(['\\[', '\\]'], ['[', ']'], preg_replace('/(\[\[)|((?<!(\[))]])/', '"', $sql)),
141
            'db', 'sqlsrv' => str_replace(['[[', ']]'], ['[', ']'], $sql),
142
            default => $sql,
143
        };
144
    }
145
}
146