Passed
Pull Request — master (#396)
by Wilmer
02:32
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\Db\Driver\PDO\ConnectionPDOInterface;
8
9
final class DbHelper
10
{
11
    public static function getCommmentsFromColumn(
12
        string $table,
13
        string $column,
14
        ConnectionPDOInterface $db
15
    ): array|null {
16
        return match ($db->getName()) {
17
            'pgsql' => $db->createCommand(
18
                <<<SQL
19
                SELECT
20
                    pgd.description
21
                FROM
22
                    pg_catalog.pg_statio_all_tables as st
23
                INNER JOIN pg_catalog.pg_description pgd ON (pgd.objoid=st.relid)
24
                INNER JOIN pg_catalog.pg_attribute pga ON (pga.attrelid=st.relid AND pga.attnum=pgd.objsubid)
25
                WHERE
26
                    st.relname=:table AND pga.attname=:column
27
                SQL,
28
                ['table' => $table, 'column' => $column]
29
            )->queryOne(),
30
            'sqlsrv' => $db->createCommand(
31
                <<<SQL
32
                SELECT
33
                    value
34
                FROM
35
                    sys.extended_properties
36
                WHERE
37
                    major_id = OBJECT_ID(:table) AND minor_id = COLUMNPROPERTY(major_id, :column, 'ColumnId')
38
                SQL,
39
                ['table' => $table, 'column' => $column]
40
            )->queryOne(),
41
        };
42
    }
43
44
    public static function getCommmentsFromTable(
45
        string $table,
46
        ConnectionPDOInterface $db
47
    ): array|null {
48
        return match ($db->getName()) {
49
            'pgsql' => $db->createCommand(
50
                <<<SQL
51
                SELECT obj_description(oid, 'pg_class') as description FROM pg_class WHERE relname= :table
52
                SQL,
53
                ['table' => $table]
54
            )->queryOne(),
55
            'sqlsrv' => $db->createCommand(
56
                <<<SQL
57
                SELECT
58
                    value
59
                FROM
60
                    sys.extended_properties
61
                WHERE
62
                    major_id = OBJECT_ID(:table) AND minor_id = 0
63
                SQL,
64
                ['table' => $table]
65
            )->queryOne(),
66
        };
67
    }
68
69
    /**
70
     * Adjust dbms specific escaping.
71
     *
72
     * @param string $sql string SQL statement to adjust.
73
     * @param string $drivername string DBMS name.
74
     *
75
     * @return mixed
76
     */
77
    public static function replaceQuotes(string $sql, string $drivername): string
78
    {
79
        return match ($drivername) {
80
            'mysql', 'sqlite' => str_replace(['[[', ']]'], '`', $sql),
81
            'oci' => str_replace(['[[', ']]'], '"', $sql),
82
            'pgsql' => str_replace(['\\[', '\\]'], ['[', ']'], preg_replace('/(\[\[)|((?<!(\[))]])/', '"', $sql)),
83
            'db', 'sqlsrv' => str_replace(['[[', ']]'], ['[', ']'], $sql),
84
            default => $sql,
85
        };
86
    }
87
}
88