Completed
Push — 4.x ( da046c...d9a6cf )
by Kit Loong
08:01
created

PgSQLRepository   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 3
dl 0
loc 29
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getTypeByColumnName() 0 26 2
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\Facades\DB;
11
use KitLoong\MigrationsGenerator\MigrationGeneratorSetting;
12
13
class PgSQLRepository
14
{
15
    public function getTypeByColumnName(string $table, string $columnName): ?string
16
    {
17
        /** @var MigrationGeneratorSetting $setting */
18
        $setting = app(MigrationGeneratorSetting::class);
19
20
        $column = DB::connection($setting->getConnection())
21
            ->select("SELECT
22
    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
            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
    )
35
    AND a.attname='${columnName}'");
36
        if (count($column) > 0) {
37
            return $column[0]->datatype;
38
        }
39
        return null;
40
    }
41
}
42