Passed
Push — 4.x ( b027a1...820cf2 )
by Kit Loong
62:09
created

PgSQLRepository::getCheckConstraintDefinition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 25
ccs 0
cts 0
cp 0
crap 6
rs 10
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: liow.kitloong
5
 * Date: 2020/04/07
6
 */
7
8
namespace KitLoong\MigrationsGenerator\Repositories;
9
10
use Illuminate\Support\Collection;
11
use KitLoong\MigrationsGenerator\MigrationsGeneratorSetting;
12
13
class PgSQLRepository extends Repository
14 6
{
15
    public function getTypeByColumnName(string $table, string $columnName): ?string
16
    {
17 6
        /** @var MigrationsGeneratorSetting $setting */
18
        $setting = app(MigrationsGeneratorSetting::class);
19 6
20 6
        $column = $setting->getConnection()
21
            ->select("
22
                SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) as datatype
23
                FROM
24
                    pg_catalog.pg_attribute a
25
                WHERE
26
                    a.attnum > 0
27
                    AND NOT a.attisdropped
28
                    AND a.attrelid = (
29
                        SELECT c.oid
30
                        FROM pg_catalog.pg_class c
31 6
                            LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
32
                        WHERE c.relname ~ '^(${table})$'
33
                            AND pg_catalog.pg_table_is_visible(c.oid)
34 6
                    )
35 6
                    AND a.attname='${columnName}'");
36 3
        if (count($column) > 0) {
37
            return $column[0]->datatype;
38 3
        }
39
        return null;
40
    }
41
42
    public function getCheckConstraintDefinition(string $table, string $column): ?string
43
    {
44
        $setting = app(MigrationsGeneratorSetting::class);
45
        $column = $setting->getConnection()
46
            ->select("
47
                SELECT pgc.conname AS constraint_name,
48
                       pgc.contype,
49
                       ccu.table_schema AS table_schema,
50
                       ccu.table_name,
51
                       ccu.column_name,
52
                       pgc.consrc AS definition
53
                FROM pg_constraint pgc
54
                JOIN pg_namespace nsp ON nsp.oid = pgc.connamespace
55
                JOIN pg_class  cls ON pgc.conrelid = cls.oid
56
                LEFT JOIN information_schema.constraint_column_usage ccu
57
                          ON pgc.conname = ccu.constraint_name
58
                          AND nsp.nspname = ccu.constraint_schema
59
                WHERE contype ='c'
60
                    AND ccu.table_name='${table}'
61
                    AND ccu.column_name='${column}';
62
            ");
63
        if (count($column) > 0) {
64
            return $column[0]->definition;
65
        }
66
        return null;
67
    }
68
69
    public function getSpatialIndexNames(string $table): Collection
70
    {
71
        $setting = app(MigrationsGeneratorSetting::class);
72
        $columns = $setting->getConnection()
73
            ->select("
74
                SELECT tablename,
75
                       indexname,
76
                       indexdef
77
                FROM pg_indexes
78
                WHERE tablename = '${table}'
79
                    AND indexdef LIKE '% USING gist %'");
80
        $definitions = collect([]);
81
        if (count($columns) > 0) {
82
            foreach ($columns as $column) {
83
                $definitions->push($column->indexname);
84
            }
85
        }
86
        return $definitions;
87
    }
88
}
89